More small fixes on `C-u C-c C-c' and checkboxes.
[org-mode/org-kjn.git] / lisp / org-agenda.el
blob4b7a821963f140385fcb5750eb03c4ad962cdf1f
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004-2011 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file contains the code for creating and using the Agenda for Org-mode.
29 ;; The functions `org-batch-agenda', `org-batch-agenda-csv', and
30 ;; `org-batch-store-agenda-views' are implemented as macros to provide
31 ;; a conveniant way for extracting agenda information from the command
32 ;; line. The Lisp does not evaluate parameters of a macro call; thus
33 ;; it is not necessary to quote the parameters passed to one of those
34 ;; functions. E.g. you can write:
36 ;; emacs -batch -l ~/.emacs -eval '(org-batch-agenda "a" org-agenda-span 7)'
38 ;; To export an agenda spanning 7 days. If `org-batch-agenda' would
39 ;; have been implemented as a regular function you'd have to quote the
40 ;; symbol org-agenda-span. Moreover: To use a symbol as parameter
41 ;; value you would have to double quote the symbol.
43 ;; This is a hack, but it works even when running Org byte-compiled.
46 ;;; Code:
48 (require 'org)
49 (eval-when-compile
50 (require 'cl))
52 (declare-function diary-add-to-list "diary-lib"
53 (date string specifier &optional marker globcolor literal))
54 (declare-function calendar-absolute-from-iso "cal-iso" (date))
55 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
56 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
57 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
58 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
59 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
60 (declare-function calendar-french-date-string "cal-french" (&optional date))
61 (declare-function calendar-goto-date "cal-move" (date))
62 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
63 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
64 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
65 (declare-function calendar-iso-from-absolute "cal-iso" (date))
66 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
67 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
68 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
69 (declare-function calendar-check-holidays "holidays" (date))
71 (declare-function org-datetree-find-date-create "org-datetree"
72 (date &optional keep-restriction))
73 (declare-function org-columns-quit "org-colview" ())
74 (declare-function diary-date-display-form "diary-lib" (&optional type))
75 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
76 (declare-function org-habit-insert-consistency-graphs
77 "org-habit" (&optional line))
78 (declare-function org-is-habit-p "org-habit" (&optional pom))
79 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
80 (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
81 (declare-function org-pop-to-buffer-same-window "org-compat"
82 (&optional buffer-or-name norecord label))
84 (defvar calendar-mode-map)
85 (defvar org-clock-current-task) ; defined in org-clock.el
86 (defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
87 (defvar org-habit-show-habits)
88 (defvar org-habit-show-habits-only-for-today)
90 ;; Defined somewhere in this file, but used before definition.
91 (defvar org-agenda-buffer-name)
92 (defvar org-agenda-overriding-header)
93 (defvar org-agenda-title-append nil)
94 (defvar entry)
95 (defvar date)
96 (defvar org-agenda-undo-list)
97 (defvar org-agenda-pending-undo-list)
98 (defvar original-date) ; dynamically scoped, calendar.el does scope this
100 (defcustom org-agenda-confirm-kill 1
101 "When set, remote killing from the agenda buffer needs confirmation.
102 When t, a confirmation is always needed. When a number N, confirmation is
103 only needed when the text to be killed contains more than N non-white lines."
104 :group 'org-agenda
105 :type '(choice
106 (const :tag "Never" nil)
107 (const :tag "Always" t)
108 (integer :tag "When more than N lines")))
110 (defcustom org-agenda-compact-blocks nil
111 "Non-nil means make the block agenda more compact.
112 This is done globally by leaving out lines like the agenda span
113 name and week number or the separator lines."
114 :group 'org-agenda
115 :type 'boolean)
117 (defcustom org-agenda-block-separator ?=
118 "The separator between blocks in the agenda.
119 If this is a string, it will be used as the separator, with a newline added.
120 If it is a character, it will be repeated to fill the window width.
121 If nil the separator is disabled. In `org-agenda-custom-commands' this
122 addresses the separator between the current and the previous block."
123 :group 'org-agenda
124 :type '(choice
125 (const :tag "Disabled" nil)
126 (character)
127 (string)))
129 (defgroup org-agenda-export nil
130 "Options concerning exporting agenda views in Org-mode."
131 :tag "Org Agenda Export"
132 :group 'org-agenda)
134 (defcustom org-agenda-with-colors t
135 "Non-nil means use colors in agenda views."
136 :group 'org-agenda-export
137 :type 'boolean)
139 (defcustom org-agenda-exporter-settings nil
140 "Alist of variable/value pairs that should be active during agenda export.
141 This is a good place to set options for ps-print and for htmlize.
142 Note that the way this is implemented, the values will be evaluated
143 before assigned to the variables. So make sure to quote values you do
144 *not* want evaluated, for example
146 (setq org-agenda-exporter-settings
147 '((ps-print-color-p 'black-white)))"
148 :group 'org-agenda-export
149 :type '(repeat
150 (list
151 (variable)
152 (sexp :tag "Value"))))
154 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
155 "Hook run in temporary buffer before writing it to an export file.
156 A useful function is `org-agenda-add-entry-text'."
157 :group 'org-agenda-export
158 :type 'hook
159 :options '(org-agenda-add-entry-text))
161 (defcustom org-agenda-add-entry-text-maxlines 0
162 "Maximum number of entry text lines to be added to agenda.
163 This is only relevant when `org-agenda-add-entry-text' is part of
164 `org-agenda-before-write-hook', which it is by default.
165 When this is 0, nothing will happen. When it is greater than 0, it
166 specifies the maximum number of lines that will be added for each entry
167 that is listed in the agenda view.
169 Note that this variable is not used during display, only when exporting
170 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
171 and `org-agenda-entry-text-maxlines'."
172 :group 'org-agenda
173 :type 'integer)
175 (defcustom org-agenda-add-entry-text-descriptive-links t
176 "Non-nil means export org-links as descriptive links in agenda added text.
177 This variable applies to the text added to the agenda when
178 `org-agenda-add-entry-text-maxlines' is larger than 0.
179 When this variable nil, the URL will (also) be shown."
180 :group 'org-agenda
181 :type 'boolean)
183 (defcustom org-agenda-export-html-style ""
184 "The style specification for exported HTML Agenda files.
185 If this variable contains a string, it will replace the default <style>
186 section as produced by `htmlize'.
187 Since there are different ways of setting style information, this variable
188 needs to contain the full HTML structure to provide a style, including the
189 surrounding HTML tags. The style specifications should include definitions
190 the fonts used by the agenda, here is an example:
192 <style type=\"text/css\">
193 p { font-weight: normal; color: gray; }
194 .org-agenda-structure {
195 font-size: 110%;
196 color: #003399;
197 font-weight: 600;
199 .org-todo {
200 color: #cc6666;
201 font-weight: bold;
203 .org-agenda-done {
204 color: #339933;
206 .org-done {
207 color: #339933;
209 .title { text-align: center; }
210 .todo, .deadline { color: red; }
211 .done { color: green; }
212 </style>
214 or, if you want to keep the style in a file,
216 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
218 As the value of this option simply gets inserted into the HTML <head> header,
219 you can \"misuse\" it to also add other text to the header. However,
220 <style>...</style> is required, if not present the variable will be ignored."
221 :group 'org-agenda-export
222 :group 'org-export-html
223 :type 'string)
225 (defcustom org-agenda-persistent-filter nil
226 "When set, keep filters from one agenda view to the next."
227 :group 'org-agenda
228 :type 'boolean)
230 (defgroup org-agenda-custom-commands nil
231 "Options concerning agenda views in Org-mode."
232 :tag "Org Agenda Custom Commands"
233 :group 'org-agenda)
235 (defconst org-sorting-choice
236 '(choice
237 (const time-up) (const time-down)
238 (const category-keep) (const category-up) (const category-down)
239 (const tag-down) (const tag-up)
240 (const priority-up) (const priority-down)
241 (const todo-state-up) (const todo-state-down)
242 (const effort-up) (const effort-down)
243 (const habit-up) (const habit-down)
244 (const alpha-up) (const alpha-down)
245 (const user-defined-up) (const user-defined-down))
246 "Sorting choices.")
248 (defconst org-agenda-custom-commands-local-options
249 `(repeat :tag "Local settings for this command. Remember to quote values"
250 (choice :tag "Setting"
251 (list :tag "Heading for this block"
252 (const org-agenda-overriding-header)
253 (string :tag "Headline"))
254 (list :tag "Files to be searched"
255 (const org-agenda-files)
256 (list
257 (const :format "" quote)
258 (repeat (file))))
259 (list :tag "Sorting strategy"
260 (const org-agenda-sorting-strategy)
261 (list
262 (const :format "" quote)
263 (repeat
264 ,org-sorting-choice)))
265 (list :tag "Prefix format"
266 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
267 (string))
268 (list :tag "Number of days in agenda"
269 (const org-agenda-span)
270 (choice (const :tag "Day" 'day)
271 (const :tag "Week" 'week)
272 (const :tag "Month" 'month)
273 (const :tag "Year" 'year)
274 (integer :tag "Custom")))
275 (list :tag "Fixed starting date"
276 (const org-agenda-start-day)
277 (string :value "2007-11-01"))
278 (list :tag "Start on day of week"
279 (const org-agenda-start-on-weekday)
280 (choice :value 1
281 (const :tag "Today" nil)
282 (integer :tag "Weekday No.")))
283 (list :tag "Include data from diary"
284 (const org-agenda-include-diary)
285 (boolean))
286 (list :tag "Deadline Warning days"
287 (const org-deadline-warning-days)
288 (integer :value 1))
289 (list :tag "Tags filter preset"
290 (const org-agenda-filter-preset)
291 (list
292 (const :format "" quote)
293 (repeat
294 (string :tag "+tag or -tag"))))
295 (list :tag "Set daily/weekly entry types"
296 (const org-agenda-entry-types)
297 (set :greedy t :value (:deadline :scheduled :timestamp :sexp)
298 (const :deadline)
299 (const :scheduled)
300 (const :timestamp)
301 (const :sexp)))
302 (list :tag "Standard skipping condition"
303 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
304 (const org-agenda-skip-function)
305 (list
306 (const :format "" quote)
307 (list
308 (choice
309 :tag "Skipping range"
310 (const :tag "Skip entry" org-agenda-skip-entry-if)
311 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
312 (repeat :inline t :tag "Conditions for skipping"
313 (choice
314 :tag "Condition type"
315 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
316 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
317 (list :tag "TODO state is" :inline t
318 (const 'todo)
319 (choice
320 (const :tag "any not-done state" 'todo)
321 (const :tag "any done state" 'done)
322 (const :tag "any state" 'any)
323 (list :tag "Keyword list"
324 (const :format "" quote)
325 (repeat (string :tag "Keyword")))))
326 (list :tag "TODO state is not" :inline t
327 (const 'nottodo)
328 (choice
329 (const :tag "any not-done state" 'todo)
330 (const :tag "any done state" 'done)
331 (const :tag "any state" 'any)
332 (list :tag "Keyword list"
333 (const :format "" quote)
334 (repeat (string :tag "Keyword")))))
335 (const :tag "scheduled" 'scheduled)
336 (const :tag "not scheduled" 'notscheduled)
337 (const :tag "deadline" 'deadline)
338 (const :tag "no deadline" 'notdeadline)
339 (const :tag "timestamp" 'timestamp)
340 (const :tag "no timestamp" 'nottimestamp))))))
341 (list :tag "Non-standard skipping condition"
342 :value (org-agenda-skip-function)
343 (const org-agenda-skip-function)
344 (sexp :tag "Function or form (quoted!)"))
345 (list :tag "Any variable"
346 (variable :tag "Variable")
347 (sexp :tag "Value (sexp)"))))
348 "Selection of examples for agenda command settings.
349 This will be spliced into the custom type of
350 `org-agenda-custom-commands'.")
353 (defcustom org-agenda-custom-commands '(("n" "Agenda and all TODO's"
354 ((agenda "") (alltodo))))
355 "Custom commands for the agenda.
356 These commands will be offered on the splash screen displayed by the
357 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
359 (key desc type match settings files)
361 key The key (one or more characters as a string) to be associated
362 with the command.
363 desc A description of the command, when omitted or nil, a default
364 description is built using MATCH.
365 type The command type, any of the following symbols:
366 agenda The daily/weekly agenda.
367 todo Entries with a specific TODO keyword, in all agenda files.
368 search Entries containing search words entry or headline.
369 tags Tags/Property/TODO match in all agenda files.
370 tags-todo Tags/P/T match in all agenda files, TODO entries only.
371 todo-tree Sparse tree of specific TODO keyword in *current* file.
372 tags-tree Sparse tree with all tags matches in *current* file.
373 occur-tree Occur sparse tree for *current* file.
374 ... A user-defined function.
375 match What to search for:
376 - a single keyword for TODO keyword searches
377 - a tags match expression for tags searches
378 - a word search expression for text searches.
379 - a regular expression for occur searches
380 For all other commands, this should be the empty string.
381 settings A list of option settings, similar to that in a let form, so like
382 this: ((opt1 val1) (opt2 val2) ...). The values will be
383 evaluated at the moment of execution, so quote them when needed.
384 files A list of files file to write the produced agenda buffer to
385 with the command `org-store-agenda-views'.
386 If a file name ends in \".html\", an HTML version of the buffer
387 is written out. If it ends in \".ps\", a postscript version is
388 produced. Otherwise, only the plain text is written to the file.
390 You can also define a set of commands, to create a composite agenda buffer.
391 In this case, an entry looks like this:
393 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
395 where
397 desc A description string to be displayed in the dispatcher menu.
398 cmd An agenda command, similar to the above. However, tree commands
399 are no allowed, but instead you can get agenda and global todo list.
400 So valid commands for a set are:
401 (agenda \"\" settings)
402 (alltodo \"\" settings)
403 (stuck \"\" settings)
404 (todo \"match\" settings files)
405 (search \"match\" settings files)
406 (tags \"match\" settings files)
407 (tags-todo \"match\" settings files)
409 Each command can carry a list of options, and another set of options can be
410 given for the whole set of commands. Individual command options take
411 precedence over the general options.
413 When using several characters as key to a command, the first characters
414 are prefix commands. For the dispatcher to display useful information, you
415 should provide a description for the prefix, like
417 (setq org-agenda-custom-commands
418 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
419 (\"hl\" tags \"+HOME+Lisa\")
420 (\"hp\" tags \"+HOME+Peter\")
421 (\"hk\" tags \"+HOME+Kim\")))"
422 :group 'org-agenda-custom-commands
423 :type `(repeat
424 (choice :value ("x" "Describe command here" tags "" nil)
425 (list :tag "Single command"
426 (string :tag "Access Key(s) ")
427 (option (string :tag "Description"))
428 (choice
429 (const :tag "Agenda" agenda)
430 (const :tag "TODO list" alltodo)
431 (const :tag "Search words" search)
432 (const :tag "Stuck projects" stuck)
433 (const :tag "Tags/Property match (all agenda files)" tags)
434 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
435 (const :tag "TODO keyword search (all agenda files)" todo)
436 (const :tag "Tags sparse tree (current buffer)" tags-tree)
437 (const :tag "TODO keyword tree (current buffer)" todo-tree)
438 (const :tag "Occur tree (current buffer)" occur-tree)
439 (sexp :tag "Other, user-defined function"))
440 (string :tag "Match (only for some commands)")
441 ,org-agenda-custom-commands-local-options
442 (option (repeat :tag "Export" (file :tag "Export to"))))
443 (list :tag "Command series, all agenda files"
444 (string :tag "Access Key(s)")
445 (string :tag "Description ")
446 (repeat :tag "Component"
447 (choice
448 (list :tag "Agenda"
449 (const :format "" agenda)
450 (const :tag "" :format "" "")
451 ,org-agenda-custom-commands-local-options)
452 (list :tag "TODO list (all keywords)"
453 (const :format "" alltodo)
454 (const :tag "" :format "" "")
455 ,org-agenda-custom-commands-local-options)
456 (list :tag "Search words"
457 (const :format "" search)
458 (string :tag "Match")
459 ,org-agenda-custom-commands-local-options)
460 (list :tag "Stuck projects"
461 (const :format "" stuck)
462 (const :tag "" :format "" "")
463 ,org-agenda-custom-commands-local-options)
464 (list :tag "Tags search"
465 (const :format "" tags)
466 (string :tag "Match")
467 ,org-agenda-custom-commands-local-options)
468 (list :tag "Tags search, TODO entries only"
469 (const :format "" tags-todo)
470 (string :tag "Match")
471 ,org-agenda-custom-commands-local-options)
472 (list :tag "TODO keyword search"
473 (const :format "" todo)
474 (string :tag "Match")
475 ,org-agenda-custom-commands-local-options)
476 (list :tag "Other, user-defined function"
477 (symbol :tag "function")
478 (string :tag "Match")
479 ,org-agenda-custom-commands-local-options)))
481 (repeat :tag "Settings for entire command set"
482 (list (variable :tag "Any variable")
483 (sexp :tag "Value")))
484 (option (repeat :tag "Export" (file :tag "Export to"))))
485 (cons :tag "Prefix key documentation"
486 (string :tag "Access Key(s)")
487 (string :tag "Description ")))))
489 (defcustom org-agenda-query-register ?o
490 "The register holding the current query string.
491 The purpose of this is that if you construct a query string interactively,
492 you can then use it to define a custom command."
493 :group 'org-agenda-custom-commands
494 :type 'character)
496 (defcustom org-stuck-projects
497 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
498 "How to identify stuck projects.
499 This is a list of four items:
500 1. A tags/todo/property matcher string that is used to identify a project.
501 See the manual for a description of tag and property searches.
502 The entire tree below a headline matched by this is considered one project.
503 2. A list of TODO keywords identifying non-stuck projects.
504 If the project subtree contains any headline with one of these todo
505 keywords, the project is considered to be not stuck. If you specify
506 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
507 3. A list of tags identifying non-stuck projects.
508 If the project subtree contains any headline with one of these tags,
509 the project is considered to be not stuck. If you specify \"*\" as
510 a tag, any tag will mark the project unstuck. Note that this is about
511 the explicit presence of a tag somewhere in the subtree, inherited
512 tags to not count here. If inherited tags make a project not stuck,
513 use \"-TAG\" in the tags part of the matcher under (1.) above.
514 4. An arbitrary regular expression matching non-stuck projects.
516 If the project turns out to be not stuck, search continues also in the
517 subtree to see if any of the subtasks have project status.
519 See also the variable `org-tags-match-list-sublevels' which applies
520 to projects matched by this search as well.
522 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
523 or `C-c a #' to produce the list."
524 :group 'org-agenda-custom-commands
525 :type '(list
526 (string :tag "Tags/TODO match to identify a project")
527 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
528 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
529 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
531 (defcustom org-agenda-filter-effort-default-operator "<"
532 "The default operator for effort estimate filtering.
533 If you select an effort estimate limit without first pressing an operator,
534 this one will be used."
535 :group 'org-agenda-custom-commands
536 :type '(choice (const :tag "less or equal" "<")
537 (const :tag "greater or equal"">")
538 (const :tag "equal" "=")))
540 (defgroup org-agenda-skip nil
541 "Options concerning skipping parts of agenda files."
542 :tag "Org Agenda Skip"
543 :group 'org-agenda)
545 (defcustom org-agenda-skip-function-global nil
546 "Function to be called at each match during agenda construction.
547 If this function returns nil, the current match should not be skipped.
548 If the function decided to skip an agenda match, is must return the
549 buffer position from which the search should be continued.
550 This may also be a Lisp form, which will be evaluated.
552 This variable will be applied to every agenda match, including
553 tags/property searches and TODO lists. So try to make the test function
554 do its checking as efficiently as possible. To implement a skipping
555 condition just for specific agenda commands, use the variable
556 `org-agenda-skip-function' which can be set in the options section
557 of custom agenda commands."
558 :group 'org-agenda-skip
559 :type 'sexp)
561 (defgroup org-agenda-daily/weekly nil
562 "Options concerning the daily/weekly agenda."
563 :tag "Org Agenda Daily/Weekly"
564 :group 'org-agenda)
565 (defgroup org-agenda-todo-list nil
566 "Options concerning the global todo list agenda view."
567 :tag "Org Agenda Todo List"
568 :group 'org-agenda)
569 (defgroup org-agenda-match-view nil
570 "Options concerning the general tags/property/todo match agenda view."
571 :tag "Org Agenda Match View"
572 :group 'org-agenda)
573 (defgroup org-agenda-search-view nil
574 "Options concerning the general tags/property/todo match agenda view."
575 :tag "Org Agenda Match View"
576 :group 'org-agenda)
578 (defvar org-agenda-archives-mode nil
579 "Non-nil means the agenda will include archived items.
580 If this is the symbol `trees', trees in the selected agenda scope
581 that are marked with the ARCHIVE tag will be included anyway. When this is
582 t, also all archive files associated with the current selection of agenda
583 files will be included.")
585 (defcustom org-agenda-skip-comment-trees t
586 "Non-nil means skip trees that start with the COMMENT keyword.
587 When nil, these trees are also scanned by agenda commands."
588 :group 'org-agenda-skip
589 :type 'boolean)
591 (defcustom org-agenda-todo-list-sublevels t
592 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
593 When nil, the sublevels of a TODO entry are not checked, resulting in
594 potentially much shorter TODO lists."
595 :group 'org-agenda-skip
596 :group 'org-agenda-todo-list
597 :type 'boolean)
599 (defcustom org-agenda-todo-ignore-with-date nil
600 "Non-nil means don't show entries with a date in the global todo list.
601 You can use this if you prefer to mark mere appointments with a TODO keyword,
602 but don't want them to show up in the TODO list.
603 When this is set, it also covers deadlines and scheduled items, the settings
604 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
605 will be ignored.
606 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
607 :group 'org-agenda-skip
608 :group 'org-agenda-todo-list
609 :type 'boolean)
611 (defcustom org-agenda-todo-ignore-timestamp nil
612 "Non-nil means don't show entries with a timestamp.
613 This applies when creating the global todo list.
614 Valid values are:
616 past Don't show entries for today or in the past.
618 future Don't show entries with a timestamp in the future.
619 The idea behind this is that if it has a future
620 timestamp, you don't want to think about it until the
621 date.
623 all Don't show any entries with a timestamp in the global todo list.
624 The idea behind this is that by setting a timestamp, you
625 have already \"taken care\" of this item.
627 This variable can also have an integer as a value. If positive (N),
628 todos with a timestamp N or more days in the future will be ignored. If
629 negative (-N), todos with a timestamp N or more days in the past will be
630 ignored. If 0, todos with a timestamp either today or in the future will
631 be ignored. For example, a value of -1 will exclude todos with a
632 timestamp in the past (yesterday or earlier), while a value of 7 will
633 exclude todos with a timestamp a week or more in the future.
635 See also `org-agenda-todo-ignore-with-date'.
636 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
637 to make his option also apply to the tags-todo list."
638 :group 'org-agenda-skip
639 :group 'org-agenda-todo-list
640 :type '(choice
641 (const :tag "Ignore future timestamp todos" future)
642 (const :tag "Ignore past or present timestamp todos" past)
643 (const :tag "Ignore all timestamp todos" all)
644 (const :tag "Show timestamp todos" nil)
645 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
647 (defcustom org-agenda-todo-ignore-scheduled nil
648 "Non-nil means, ignore some scheduled TODO items when making TODO list.
649 This applies when creating the global todo list.
650 Valid values are:
652 past Don't show entries scheduled today or in the past.
654 future Don't show entries scheduled in the future.
655 The idea behind this is that by scheduling it, you don't want to
656 think about it until the scheduled date.
658 all Don't show any scheduled entries in the global todo list.
659 The idea behind this is that by scheduling it, you have already
660 \"taken care\" of this item.
662 t Same as `all', for backward compatibility.
664 This variable can also have an integer as a value. See
665 `org-agenda-todo-ignore-timestamp' for more details.
667 See also `org-agenda-todo-ignore-with-date'.
668 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
669 to make his option also apply to the tags-todo list."
670 :group 'org-agenda-skip
671 :group 'org-agenda-todo-list
672 :type '(choice
673 (const :tag "Ignore future-scheduled todos" future)
674 (const :tag "Ignore past- or present-scheduled todos" past)
675 (const :tag "Ignore all scheduled todos" all)
676 (const :tag "Ignore all scheduled todos (compatibility)" t)
677 (const :tag "Show scheduled todos" nil)
678 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
680 (defcustom org-agenda-todo-ignore-deadlines nil
681 "Non-nil means ignore some deadlined TODO items when making TODO list.
682 There are different motivations for using different values, please think
683 carefully when configuring this variable.
685 This applies when creating the global todo list.
686 Valid values are:
688 near Don't show near deadline entries. A deadline is near when it is
689 closer than `org-deadline-warning-days' days. The idea behind this
690 is that such items will appear in the agenda anyway.
692 far Don't show TODO entries where a deadline has been defined, but
693 the deadline is not near. This is useful if you don't want to
694 use the todo list to figure out what to do now.
696 past Don't show entries with a deadline timestamp for today or in the past.
698 future Don't show entries with a deadline timestamp in the future, not even
699 when they become `near' ones. Use it with caution.
701 all Ignore all TODO entries that do have a deadline.
703 t Same as `near', for backward compatibility.
705 This variable can also have an integer as a value. See
706 `org-agenda-todo-ignore-timestamp' for more details.
708 See also `org-agenda-todo-ignore-with-date'.
709 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
710 to make his option also apply to the tags-todo list."
711 :group 'org-agenda-skip
712 :group 'org-agenda-todo-list
713 :type '(choice
714 (const :tag "Ignore near deadlines" near)
715 (const :tag "Ignore near deadlines (compatibility)" t)
716 (const :tag "Ignore far deadlines" far)
717 (const :tag "Ignore all TODOs with a deadlines" all)
718 (const :tag "Show all TODOs, even if they have a deadline" nil)
719 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
721 (defcustom org-agenda-tags-todo-honor-ignore-options nil
722 "Non-nil means honor todo-list ...ignore options also in tags-todo search.
723 The variables
724 `org-agenda-todo-ignore-with-date',
725 `org-agenda-todo-ignore-timestamp',
726 `org-agenda-todo-ignore-scheduled',
727 `org-agenda-todo-ignore-deadlines'
728 make the global TODO list skip entries that have time stamps of certain
729 kinds. If this option is set, the same options will also apply for the
730 tags-todo search, which is the general tags/property matcher
731 restricted to unfinished TODO entries only."
732 :group 'org-agenda-skip
733 :group 'org-agenda-todo-list
734 :group 'org-agenda-match-view
735 :type 'boolean)
737 (defcustom org-agenda-skip-scheduled-if-done nil
738 "Non-nil means don't show scheduled items in agenda when they are done.
739 This is relevant for the daily/weekly agenda, not for the TODO list. And
740 it applies only to the actual date of the scheduling. Warnings about
741 an item with a past scheduling dates are always turned off when the item
742 is DONE."
743 :group 'org-agenda-skip
744 :group 'org-agenda-daily/weekly
745 :type 'boolean)
747 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
748 "Non-nil means skip scheduling line if same entry shows because of deadline.
749 In the agenda of today, an entry can show up multiple times because
750 it is both scheduled and has a nearby deadline, and maybe a plain time
751 stamp as well.
752 When this variable is t, then only the deadline is shown and the fact that
753 the entry is scheduled today or was scheduled previously is not shown.
754 When this variable is nil, the entry will be shown several times. When
755 the variable is the symbol `not-today', then skip scheduled previously,
756 but not scheduled today."
757 :group 'org-agenda-skip
758 :group 'org-agenda-daily/weekly
759 :type '(choice
760 (const :tag "Never" nil)
761 (const :tag "Always" t)
762 (const :tag "Not when scheduled today" not-today)))
764 (defcustom org-agenda-skip-deadline-if-done nil
765 "Non-nil means don't show deadlines when the corresponding item is done.
766 When nil, the deadline is still shown and should give you a happy feeling.
767 This is relevant for the daily/weekly agenda. And it applied only to the
768 actually date of the deadline. Warnings about approaching and past-due
769 deadlines are always turned off when the item is DONE."
770 :group 'org-agenda-skip
771 :group 'org-agenda-daily/weekly
772 :type 'boolean)
774 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
775 "Non-nil means skip deadline prewarning when entry is also scheduled.
776 This will apply on all days where a prewarning for the deadline would
777 be shown, but not at the day when the entry is actually due. On that day,
778 the deadline will be shown anyway.
779 This variable may be set to nil, t, or a number which will then give
780 the number of days before the actual deadline when the prewarnings
781 should resume.
782 This can be used in a workflow where the first showing of the deadline will
783 trigger you to schedule it, and then you don't want to be reminded of it
784 because you will take care of it on the day when scheduled."
785 :group 'org-agenda-skip
786 :group 'org-agenda-daily/weekly
787 :type '(choice
788 (const :tag "Alwas show prewarning" nil)
789 (const :tag "Remove prewarning if entry is scheduled" t)
790 (integer :tag "Restart prewarning N days before deadline")))
792 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
793 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
794 When non-nil, after the search for timestamps has matched once in an
795 entry, the rest of the entry will not be searched."
796 :group 'org-agenda-skip
797 :type 'boolean)
799 (defcustom org-agenda-skip-timestamp-if-done nil
800 "Non-nil means don't select item by timestamp or -range if it is DONE."
801 :group 'org-agenda-skip
802 :group 'org-agenda-daily/weekly
803 :type 'boolean)
805 (defcustom org-agenda-dim-blocked-tasks t
806 "Non-nil means dim blocked tasks in the agenda display.
807 This causes some overhead during agenda construction, but if you
808 have turned on `org-enforce-todo-dependencies',
809 `org-enforce-todo-checkbox-dependencies', or any other blocking
810 mechanism, this will create useful feedback in the agenda.
812 Instead of t, this variable can also have the value `invisible'.
813 Then blocked tasks will be invisible and only become visible when
814 they become unblocked. An exemption to this behavior is when a task is
815 blocked because of unchecked checkboxes below it. Since checkboxes do
816 not show up in the agenda views, making this task invisible you remove any
817 trace from agenda views that there is something to do. Therefore, a task
818 that is blocked because of checkboxes will never be made invisible, it
819 will only be dimmed."
820 :group 'org-agenda-daily/weekly
821 :group 'org-agenda-todo-list
822 :type '(choice
823 (const :tag "Do not dim" nil)
824 (const :tag "Dim to a gray face" t)
825 (const :tag "Make invisible" invisible)))
827 (defcustom org-timeline-show-empty-dates 3
828 "Non-nil means `org-timeline' also shows dates without an entry.
829 When nil, only the days which actually have entries are shown.
830 When t, all days between the first and the last date are shown.
831 When an integer, show also empty dates, but if there is a gap of more than
832 N days, just insert a special line indicating the size of the gap."
833 :group 'org-agenda-skip
834 :type '(choice
835 (const :tag "None" nil)
836 (const :tag "All" t)
837 (integer :tag "at most")))
839 (defgroup org-agenda-startup nil
840 "Options concerning initial settings in the Agenda in Org Mode."
841 :tag "Org Agenda Startup"
842 :group 'org-agenda)
844 (defcustom org-agenda-menu-show-matcher t
845 "Non-nil means show the match string in the agenda dispatcher menu.
846 When nil, the matcher string is not shown, but is put into the help-echo
847 property so than moving the mouse over the command shows it.
848 Setting it to nil is good if matcher strings are very long and/or if
849 you want to use two-column display (see `org-agenda-menu-two-column')."
850 :group 'org-agenda
851 :type 'boolean)
853 (defcustom org-agenda-menu-two-column nil
854 "Non-nil means, use two columns to show custom commands in the dispatcher.
855 If you use this, you probably want to set `org-agenda-menu-show-matcher'
856 to nil."
857 :group 'org-agenda
858 :type 'boolean)
860 (defcustom org-finalize-agenda-hook nil
861 "Hook run just before displaying an agenda buffer."
862 :group 'org-agenda-startup
863 :type 'hook)
865 (defcustom org-agenda-mouse-1-follows-link nil
866 "Non-nil means mouse-1 on a link will follow the link in the agenda.
867 A longer mouse click will still set point. Does not work on XEmacs.
868 Needs to be set before org.el is loaded."
869 :group 'org-agenda-startup
870 :type 'boolean)
872 (defcustom org-agenda-start-with-follow-mode nil
873 "The initial value of follow mode in a newly created agenda window."
874 :group 'org-agenda-startup
875 :type 'boolean)
877 (defcustom org-agenda-follow-indirect nil
878 "Non-nil means `org-agenda-follow-mode' displays only the
879 current item's tree, in an indirect buffer."
880 :group 'org-agenda
881 :type 'boolean)
883 (defcustom org-agenda-show-outline-path t
884 "Non-nil means show outline path in echo area after line motion."
885 :group 'org-agenda-startup
886 :type 'boolean)
888 (defcustom org-agenda-start-with-entry-text-mode nil
889 "The initial value of entry-text-mode in a newly created agenda window."
890 :group 'org-agenda-startup
891 :type 'boolean)
893 (defcustom org-agenda-entry-text-maxlines 5
894 "Number of text lines to be added when `E' is pressed in the agenda.
896 Note that this variable only used during agenda display. Add add entry text
897 when exporting the agenda, configure the variable
898 `org-agenda-add-entry-ext-maxlines'."
899 :group 'org-agenda
900 :type 'integer)
902 (defcustom org-agenda-entry-text-exclude-regexps nil
903 "List of regular expressions to clean up entry text.
904 The complete matches of all regular expressions in this list will be
905 removed from entry text before it is shown in the agenda."
906 :group 'org-agenda
907 :type '(repeat (regexp)))
909 (defvar org-agenda-entry-text-cleanup-hook nil
910 "Hook that is run after basic cleanup of entry text to be shown in agenda.
911 This cleanup is done in a temporary buffer, so the function may inspect and
912 change the entire buffer.
913 Some default stuff like drawers and scheduling/deadline dates will already
914 have been removed when this is called, as will any matches for regular
915 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
917 (defvar org-agenda-include-inactive-timestamps nil
918 "Non-nil means include inactive time stamps in agenda and timeline.")
920 (defgroup org-agenda-windows nil
921 "Options concerning the windows used by the Agenda in Org Mode."
922 :tag "Org Agenda Windows"
923 :group 'org-agenda)
925 (defcustom org-agenda-window-setup 'reorganize-frame
926 "How the agenda buffer should be displayed.
927 Possible values for this option are:
929 current-window Show agenda in the current window, keeping all other windows.
930 other-window Use `switch-to-buffer-other-window' to display agenda.
931 reorganize-frame Show only two windows on the current frame, the current
932 window and the agenda.
933 other-frame Use `switch-to-buffer-other-frame' to display agenda.
934 Also, when exiting the agenda, kill that frame.
935 See also the variable `org-agenda-restore-windows-after-quit'."
936 :group 'org-agenda-windows
937 :type '(choice
938 (const current-window)
939 (const other-frame)
940 (const other-window)
941 (const reorganize-frame)))
943 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
944 "The min and max height of the agenda window as a fraction of frame height.
945 The value of the variable is a cons cell with two numbers between 0 and 1.
946 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
947 :group 'org-agenda-windows
948 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
950 (defcustom org-agenda-restore-windows-after-quit nil
951 "Non-nil means restore window configuration upon exiting agenda.
952 Before the window configuration is changed for displaying the agenda,
953 the current status is recorded. When the agenda is exited with
954 `q' or `x' and this option is set, the old state is restored. If
955 `org-agenda-window-setup' is `other-frame', the value of this
956 option will be ignored."
957 :group 'org-agenda-windows
958 :type 'boolean)
960 (defcustom org-agenda-ndays nil
961 "Number of days to include in overview display.
962 Should be 1 or 7.
963 Obsolete, see `org-agenda-span'."
964 :group 'org-agenda-daily/weekly
965 :type 'integer)
967 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
969 (defcustom org-agenda-span 'week
970 "Number of days to include in overview display.
971 Can be day, week, month, year, or any number of days.
972 Custom commands can set this variable in the options section."
973 :group 'org-agenda-daily/weekly
974 :type '(choice (const :tag "Day" day)
975 (const :tag "Week" week)
976 (const :tag "Month" month)
977 (const :tag "Year" year)
978 (integer :tag "Custom")))
980 (defcustom org-agenda-start-on-weekday 1
981 "Non-nil means start the overview always on the specified weekday.
982 0 denotes Sunday, 1 denotes Monday etc.
983 When nil, always start on the current day.
984 Custom commands can set this variable in the options section."
985 :group 'org-agenda-daily/weekly
986 :type '(choice (const :tag "Today" nil)
987 (integer :tag "Weekday No.")))
989 (defcustom org-agenda-show-all-dates t
990 "Non-nil means `org-agenda' shows every day in the selected range.
991 When nil, only the days which actually have entries are shown."
992 :group 'org-agenda-daily/weekly
993 :type 'boolean)
995 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
996 "Format string for displaying dates in the agenda.
997 Used by the daily/weekly agenda and by the timeline. This should be
998 a format string understood by `format-time-string', or a function returning
999 the formatted date as a string. The function must take a single argument,
1000 a calendar-style date list like (month day year)."
1001 :group 'org-agenda-daily/weekly
1002 :type '(choice
1003 (string :tag "Format string")
1004 (function :tag "Function")))
1006 (defun org-agenda-format-date-aligned (date)
1007 "Format a date string for display in the daily/weekly agenda, or timeline.
1008 This function makes sure that dates are aligned for easy reading."
1009 (require 'cal-iso)
1010 (let* ((dayname (calendar-day-name date))
1011 (day (cadr date))
1012 (day-of-week (calendar-day-of-week date))
1013 (month (car date))
1014 (monthname (calendar-month-name month))
1015 (year (nth 2 date))
1016 (iso-week (org-days-to-iso-week
1017 (calendar-absolute-from-gregorian date)))
1018 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1019 (1- year))
1020 ((and (= month 12) (<= iso-week 1))
1021 (1+ year))
1022 (t year)))
1023 (weekstring (if (= day-of-week 1)
1024 (format " W%02d" iso-week)
1025 "")))
1026 (format "%-10s %2d %s %4d%s"
1027 dayname day monthname year weekstring)))
1029 (defcustom org-agenda-time-leading-zero nil
1030 "Non-nil means use leading zero for military times in agenda.
1031 For example, 9:30am would become 09:30 rather than 9:30."
1032 :group 'org-agenda-daily/weekly
1033 :type 'boolean)
1035 (defcustom org-agenda-timegrid-use-ampm nil
1036 "When set, show AM/PM style timestamps on the timegrid."
1037 :group 'org-agenda
1038 :type 'boolean)
1040 (defun org-agenda-time-of-day-to-ampm (time)
1041 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1042 (let* ((hour-number (string-to-number (substring time 0 -3)))
1043 (minute (substring time -2))
1044 (ampm "am"))
1045 (cond
1046 ((equal hour-number 12)
1047 (setq ampm "pm"))
1048 ((> hour-number 12)
1049 (setq ampm "pm")
1050 (setq hour-number (- hour-number 12))))
1051 (concat
1052 (if org-agenda-time-leading-zero
1053 (format "%02d" hour-number)
1054 (format "%02s" (number-to-string hour-number)))
1055 ":" minute ampm)))
1057 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1058 "Conditionally convert TIME to AM/PM format
1059 based on `org-agenda-timegrid-use-ampm'"
1060 (if org-agenda-timegrid-use-ampm
1061 (org-agenda-time-of-day-to-ampm time)
1062 time))
1064 (defcustom org-agenda-weekend-days '(6 0)
1065 "Which days are weekend?
1066 These days get the special face `org-agenda-date-weekend' in the agenda
1067 and timeline buffers."
1068 :group 'org-agenda-daily/weekly
1069 :type '(set :greedy t
1070 (const :tag "Monday" 1)
1071 (const :tag "Tuesday" 2)
1072 (const :tag "Wednesday" 3)
1073 (const :tag "Thursday" 4)
1074 (const :tag "Friday" 5)
1075 (const :tag "Saturday" 6)
1076 (const :tag "Sunday" 0)))
1078 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1079 "Non-nil means jumpt to today when moving a past date forward in time.
1080 When using S-right in the agenda to move a a date forward, and the date
1081 stamp currently points to the past, the first key press will move it
1082 to today. WHen nil, just move one day forward even if the date stays
1083 in the past."
1084 :group 'org-agenda-daily/weekly
1085 :type 'boolean)
1087 (defcustom org-agenda-include-diary nil
1088 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1089 Custom commands can set this variable in the options section."
1090 :group 'org-agenda-daily/weekly
1091 :type 'boolean)
1093 (defcustom org-agenda-include-deadlines t
1094 "If non-nil, include entries within their deadline warning period.
1095 Custom commands can set this variable in the options section."
1096 :group 'org-agenda-daily/weekly
1097 :type 'boolean)
1099 (defcustom org-agenda-repeating-timestamp-show-all t
1100 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1101 When set to a list of strings, only show occurrences of repeating
1102 stamps for these TODO keywords. When nil, only one occurrence is
1103 shown, either today or the nearest into the future."
1104 :group 'org-agenda-daily/weekly
1105 :type '(choice
1106 (const :tag "Show repeating stamps" t)
1107 (repeat :tag "Show repeating stamps for these TODO keywords"
1108 (string :tag "TODO Keyword"))
1109 (const :tag "Don't show repeating stamps" nil)))
1111 (defcustom org-scheduled-past-days 10000
1112 "No. of days to continue listing scheduled items that are not marked DONE.
1113 When an item is scheduled on a date, it shows up in the agenda on this
1114 day and will be listed until it is marked done for the number of days
1115 given here."
1116 :group 'org-agenda-daily/weekly
1117 :type 'integer)
1119 (defcustom org-agenda-log-mode-items '(closed clock)
1120 "List of items that should be shown in agenda log mode.
1121 This list may contain the following symbols:
1123 closed Show entries that have been closed on that day.
1124 clock Show entries that have received clocked time on that day.
1125 state Show all logged state changes.
1126 Note that instead of changing this variable, you can also press `C-u l' in
1127 the agenda to display all available LOG items temporarily."
1128 :group 'org-agenda-daily/weekly
1129 :type '(set :greedy t (const closed) (const clock) (const state)))
1131 (defcustom org-agenda-clock-consistency-checks
1132 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1133 :gap-ok-around ("4:00")
1134 :default-face ((:background "DarkRed") (:foreground "white"))
1135 :overlap-face nil :gap-face nil :no-end-time-face nil
1136 :long-face nil :short-face nil)
1137 "This is a property list, with the following keys:
1139 :max-duration Mark clocking chunks that are longer than this time.
1140 This is a time string like \"HH:MM\", or the number
1141 of minutes as an integer.
1143 :min-duration Mark clocking chunks that are shorter that this.
1144 This is a time string like \"HH:MM\", or the number
1145 of minutes as an integer.
1147 :max-gap Mark gaps between clocking chunks that are longer than
1148 this duration. A number of minutes, or a string
1149 like \"HH:MM\".
1151 :gap-ok-around List of times during the day which are usually not working
1152 times. When a gap is detected, but the gap contains any
1153 of these times, the gap is *not* reported. For example,
1154 if this is (\"4:00\" \"13:00\") then gaps that contain
1155 4:00 in the morning (i.e. the night) and 13:00
1156 (i.e. a typical lunch time) do not cause a warning.
1157 You should have at least one time during the night in this
1158 list, or otherwise the first task each morning will trigger
1159 a warning because it follows a long gap.
1161 Furthermore, the following properties can be used to define faces for
1162 issue display.
1164 :default-face the default face, if the specific face is undefined
1165 :overlap-face face for overlapping clocks
1166 :gap-face face for gaps between clocks
1167 :no-end-time-face face for incomplete clocks
1168 :long-face face for clock intervals that are too long
1169 :short-face face for clock intervals that are too short"
1170 :group 'org-agenda-daily/weekly
1171 :group 'org-clock
1172 :type 'plist)
1174 (defcustom org-agenda-log-mode-add-notes t
1175 "Non-nil means add first line of notes to log entries in agenda views.
1176 If a log item like a state change or a clock entry is associated with
1177 notes, the first line of these notes will be added to the entry in the
1178 agenda display."
1179 :group 'org-agenda-daily/weekly
1180 :type 'boolean)
1182 (defcustom org-agenda-start-with-log-mode nil
1183 "The initial value of log-mode in a newly created agenda window."
1184 :group 'org-agenda-startup
1185 :group 'org-agenda-daily/weekly
1186 :type 'boolean)
1188 (defcustom org-agenda-start-with-clockreport-mode nil
1189 "The initial value of clockreport-mode in a newly created agenda window."
1190 :group 'org-agenda-startup
1191 :group 'org-agenda-daily/weekly
1192 :type 'boolean)
1194 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1195 "Property list with parameters for the clocktable in clockreport mode.
1196 This is the display mode that shows a clock table in the daily/weekly
1197 agenda, the properties for this dynamic block can be set here.
1198 The usual clocktable parameters are allowed here, but you cannot set
1199 the properties :name, :tstart, :tend, :block, and :scope - these will
1200 be overwritten to make sure the content accurately reflects the
1201 current display in the agenda."
1202 :group 'org-agenda-daily/weekly
1203 :type 'plist)
1205 (defcustom org-agenda-search-view-always-boolean nil
1206 "Non-nil means the search string is interpreted as individual parts.
1208 The search string for search view can either be interpreted as a phrase,
1209 or as a list of snippets that define a boolean search for a number of
1210 strings.
1212 When this is non-nil, the string will be split on whitespace, and each
1213 snippet will be searched individually, and all must match in order to
1214 select an entry. A snippet is then a single string of non-white
1215 characters, or a string in double quotes, or a regexp in {} braces.
1216 If a snippet is preceded by \"-\", the snippet must *not* match.
1217 \"+\" is syntactic sugar for positive selection. Each snippet may
1218 be found as a full word or a partial word, but see the variable
1219 `org-agenda-search-view-force-full-words'.
1221 When this is nil, search will look for the entire search phrase as one,
1222 with each space character matching any amount of whitespace, including
1223 line breaks.
1225 Even when this is nil, you can still switch to Boolean search dynamically
1226 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1227 is a regexp marked with braces like \"{abc}\", this will also switch to
1228 boolean search."
1229 :group 'org-agenda-search-view
1230 :type 'boolean)
1232 (if (fboundp 'defvaralias)
1233 (defvaralias 'org-agenda-search-view-search-words-only
1234 'org-agenda-search-view-always-boolean))
1236 (defcustom org-agenda-search-view-force-full-words nil
1237 "Non-nil means, search words must be matches as complete words.
1238 When nil, they may also match part of a word."
1239 :group 'org-agenda-search-view
1240 :type 'boolean)
1242 (defgroup org-agenda-time-grid nil
1243 "Options concerning the time grid in the Org-mode Agenda."
1244 :tag "Org Agenda Time Grid"
1245 :group 'org-agenda)
1247 (defcustom org-agenda-search-headline-for-time t
1248 "Non-nil means search headline for a time-of-day.
1249 If the headline contains a time-of-day in one format or another, it will
1250 be used to sort the entry into the time sequence of items for a day.
1251 Some people have time stamps in the headline that refer to the creation
1252 time or so, and then this produces an unwanted side effect. If this is
1253 the case for your, use this variable to turn off searching the headline
1254 for a time."
1255 :group 'org-agenda-time-grid
1256 :type 'boolean)
1258 (defcustom org-agenda-use-time-grid t
1259 "Non-nil means show a time grid in the agenda schedule.
1260 A time grid is a set of lines for specific times (like every two hours between
1261 8:00 and 20:00). The items scheduled for a day at specific times are
1262 sorted in between these lines.
1263 For details about when the grid will be shown, and what it will look like, see
1264 the variable `org-agenda-time-grid'."
1265 :group 'org-agenda-time-grid
1266 :type 'boolean)
1268 (defcustom org-agenda-time-grid
1269 '((daily today require-timed)
1270 "----------------"
1271 (800 1000 1200 1400 1600 1800 2000))
1273 "The settings for time grid for agenda display.
1274 This is a list of three items. The first item is again a list. It contains
1275 symbols specifying conditions when the grid should be displayed:
1277 daily if the agenda shows a single day
1278 weekly if the agenda shows an entire week
1279 today show grid on current date, independent of daily/weekly display
1280 require-timed show grid only if at least one item has a time specification
1282 The second item is a string which will be placed behind the grid time.
1284 The third item is a list of integers, indicating the times that should have
1285 a grid line."
1286 :group 'org-agenda-time-grid
1287 :type
1288 '(list
1289 (set :greedy t :tag "Grid Display Options"
1290 (const :tag "Show grid in single day agenda display" daily)
1291 (const :tag "Show grid in weekly agenda display" weekly)
1292 (const :tag "Always show grid for today" today)
1293 (const :tag "Show grid only if any timed entries are present"
1294 require-timed)
1295 (const :tag "Skip grid times already present in an entry"
1296 remove-match))
1297 (string :tag "Grid String")
1298 (repeat :tag "Grid Times" (integer :tag "Time"))))
1300 (defcustom org-agenda-show-current-time-in-grid t
1301 "Non-nil means show the current time in the time grid."
1302 :group 'org-agenda-time-grid
1303 :type 'boolean)
1305 (defcustom org-agenda-current-time-string
1306 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1307 "The string for the current time marker in the agenda."
1308 :group 'org-agenda-time-grid
1309 :type 'string)
1311 (defgroup org-agenda-sorting nil
1312 "Options concerning sorting in the Org-mode Agenda."
1313 :tag "Org Agenda Sorting"
1314 :group 'org-agenda)
1316 (defcustom org-agenda-sorting-strategy
1317 '((agenda habit-down time-up priority-down category-keep)
1318 (todo priority-down category-keep)
1319 (tags priority-down category-keep)
1320 (search category-keep))
1321 "Sorting structure for the agenda items of a single day.
1322 This is a list of symbols which will be used in sequence to determine
1323 if an entry should be listed before another entry. The following
1324 symbols are recognized:
1326 time-up Put entries with time-of-day indications first, early first
1327 time-down Put entries with time-of-day indications first, late first
1328 category-keep Keep the default order of categories, corresponding to the
1329 sequence in `org-agenda-files'.
1330 category-up Sort alphabetically by category, A-Z.
1331 category-down Sort alphabetically by category, Z-A.
1332 tag-up Sort alphabetically by last tag, A-Z.
1333 tag-down Sort alphabetically by last tag, Z-A.
1334 priority-up Sort numerically by priority, high priority last.
1335 priority-down Sort numerically by priority, high priority first.
1336 todo-state-up Sort by todo state, tasks that are done last.
1337 todo-state-down Sort by todo state, tasks that are done first.
1338 effort-up Sort numerically by estimated effort, high effort last.
1339 effort-down Sort numerically by estimated effort, high effort first.
1340 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1341 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1342 habit-up Put entries that are habits first
1343 habit-down Put entries that are habits last
1344 alpha-up Sort headlines alphabetically
1345 alpha-down Sort headlines alphabetically, reversed
1347 The different possibilities will be tried in sequence, and testing stops
1348 if one comparison returns a \"not-equal\". For example, the default
1349 '(time-up category-keep priority-down)
1350 means: Pull out all entries having a specified time of day and sort them,
1351 in order to make a time schedule for the current day the first thing in the
1352 agenda listing for the day. Of the entries without a time indication, keep
1353 the grouped in categories, don't sort the categories, but keep them in
1354 the sequence given in `org-agenda-files'. Within each category sort by
1355 priority.
1357 Leaving out `category-keep' would mean that items will be sorted across
1358 categories by priority.
1360 Instead of a single list, this can also be a set of list for specific
1361 contents, with a context symbol in the car of the list, any of
1362 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1364 Custom commands can bind this variable in the options section."
1365 :group 'org-agenda-sorting
1366 :type `(choice
1367 (repeat :tag "General" ,org-sorting-choice)
1368 (list :tag "Individually"
1369 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1370 (repeat ,org-sorting-choice))
1371 (cons (const :tag "Strategy for TODO lists" todo)
1372 (repeat ,org-sorting-choice))
1373 (cons (const :tag "Strategy for Tags matches" tags)
1374 (repeat ,org-sorting-choice))
1375 (cons (const :tag "Strategy for search matches" search)
1376 (repeat ,org-sorting-choice)))))
1378 (defcustom org-agenda-cmp-user-defined nil
1379 "A function to define the comparison `user-defined'.
1380 This function must receive two arguments, agenda entry a and b.
1381 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1382 the user comparison, return nil.
1383 When this is defined, you can make `user-defined-up' and `user-defined-down'
1384 part of an agenda sorting strategy."
1385 :group 'org-agenda-sorting
1386 :type 'symbol)
1388 (defcustom org-sort-agenda-notime-is-late t
1389 "Non-nil means items without time are considered late.
1390 This is only relevant for sorting. When t, items which have no explicit
1391 time like 15:30 will be considered as 99:01, i.e. later than any items which
1392 do have a time. When nil, the default time is before 0:00. You can use this
1393 option to decide if the schedule for today should come before or after timeless
1394 agenda entries."
1395 :group 'org-agenda-sorting
1396 :type 'boolean)
1398 (defcustom org-sort-agenda-noeffort-is-high t
1399 "Non-nil means items without effort estimate are sorted as high effort.
1400 This also applies when filtering an agenda view with respect to the
1401 < or > effort operator. Then, tasks with no effort defined will be treated
1402 as tasks with high effort.
1403 When nil, such items are sorted as 0 minutes effort."
1404 :group 'org-agenda-sorting
1405 :type 'boolean)
1407 (defgroup org-agenda-line-format nil
1408 "Options concerning the entry prefix in the Org-mode agenda display."
1409 :tag "Org Agenda Line Format"
1410 :group 'org-agenda)
1412 (defcustom org-agenda-prefix-format
1413 '((agenda . " %i %-12:c%?-12t% s")
1414 (timeline . " % s")
1415 (todo . " %i %-12:c")
1416 (tags . " %i %-12:c")
1417 (search . " %i %-12:c"))
1418 "Format specifications for the prefix of items in the agenda views.
1419 An alist with five entries, each for the different agenda types. The
1420 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1421 The values are format strings.
1423 This format works similar to a printf format, with the following meaning:
1425 %c the category of the item, \"Diary\" for entries from the diary,
1426 or as given by the CATEGORY keyword or derived from the file name
1427 %e the effort required by the item
1428 %i the icon category of the item, see `org-agenda-category-icon-alist'
1429 %T the last tag of the item (ignore inherited tags, which come first)
1430 %t the HH:MM time-of-day specification if one applies to the entry
1431 %s Scheduling/Deadline information, a short string
1432 %(expression) Eval EXPRESSION and replace the control string
1433 by the result
1435 All specifiers work basically like the standard `%s' of printf, but may
1436 contain two additional characters: a question mark just after the `%'
1437 and a whitespace/punctuation character just before the final letter.
1439 If the first character after `%' is a question mark, the entire field
1440 will only be included if the corresponding value applies to the current
1441 entry. This is useful for fields which should have fixed width when
1442 present, but zero width when absent. For example, \"%?-12t\" will
1443 result in a 12 character time field if a time of the day is specified,
1444 but will completely disappear in entries which do not contain a time.
1446 If there is punctuation or whitespace character just before the final
1447 format letter, this character will be appended to the field value if
1448 the value is not empty. For example, the format \"%-12:c\" leads to
1449 \"Diary: \" if the category is \"Diary\". If the category were be
1450 empty, no additional colon would be inserted.
1452 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1453 which means:
1455 - Indent the line with two space characters
1456 - Give the category a 12 chars wide field, padded with whitespace on
1457 the right (because of `-'). Append a colon if there is a category
1458 (because of `:').
1459 - If there is a time-of-day, put it into a 12 chars wide field. If no
1460 time, don't put in an empty field, just skip it (because of '?').
1461 - Finally, put the scheduling information.
1463 See also the variables `org-agenda-remove-times-when-in-prefix' and
1464 `org-agenda-remove-tags'.
1466 Custom commands can set this variable in the options section."
1467 :type '(choice
1468 (string :tag "General format")
1469 (list :greedy t :tag "View dependent"
1470 (cons (const agenda) (string :tag "Format"))
1471 (cons (const timeline) (string :tag "Format"))
1472 (cons (const todo) (string :tag "Format"))
1473 (cons (const tags) (string :tag "Format"))
1474 (cons (const search) (string :tag "Format"))))
1475 :group 'org-agenda-line-format)
1477 (defvar org-prefix-format-compiled nil
1478 "The compiled version of the most recently used prefix format.
1479 See the variable `org-agenda-prefix-format'.")
1481 (defcustom org-agenda-todo-keyword-format "%-1s"
1482 "Format for the TODO keyword in agenda lines.
1483 Set this to something like \"%-12s\" if you want all TODO keywords
1484 to occupy a fixed space in the agenda display."
1485 :group 'org-agenda-line-format
1486 :type 'string)
1488 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1489 "Text preceding timerange entries in the agenda view.
1490 This is a list with two strings. The first applies when the range
1491 is entirely on one day. The second applies if the range spans several days.
1492 The strings may have two \"%d\" format specifiers which will be filled
1493 with the sequence number of the days, and the total number of days in the
1494 range, respectively."
1495 :group 'org-agenda-line-format
1496 :type '(list
1497 (string :tag "Deadline today ")
1498 (choice :tag "Deadline relative"
1499 (string :tag "Format string")
1500 (function))))
1502 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1503 "Text preceding scheduled items in the agenda view.
1504 This is a list with two strings. The first applies when the item is
1505 scheduled on the current day. The second applies when it has been scheduled
1506 previously, it may contain a %d indicating that this is the nth time that
1507 this item is scheduled, due to automatic rescheduling of unfinished items
1508 for the following day. So this number is one larger than the number of days
1509 that passed since this item was scheduled first."
1510 :group 'org-agenda-line-format
1511 :type '(list
1512 (string :tag "Scheduled today ")
1513 (string :tag "Scheduled previously")))
1515 (defcustom org-agenda-inactive-leader "["
1516 "Text preceding item pulled into the agenda by inactive time stamps.
1517 These entries are added to the agenda when pressing \"[\"."
1518 :group 'org-agenda-line-format
1519 :type '(list
1520 (string :tag "Scheduled today ")
1521 (string :tag "Scheduled previously")))
1523 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
1524 "Text preceding deadline items in the agenda view.
1525 This is a list with two strings. The first applies when the item has its
1526 deadline on the current day. The second applies when it is in the past or
1527 in the future, it may contain %d to capture how many days away the deadline
1528 is (was)."
1529 :group 'org-agenda-line-format
1530 :type '(list
1531 (string :tag "Deadline today ")
1532 (choice :tag "Deadline relative"
1533 (string :tag "Format string")
1534 (function))))
1536 (defcustom org-agenda-remove-times-when-in-prefix t
1537 "Non-nil means remove duplicate time specifications in agenda items.
1538 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1539 time-of-day specification in a headline or diary entry is extracted and
1540 placed into the prefix. If this option is non-nil, the original specification
1541 \(a timestamp or -range, or just a plain time(range) specification like
1542 11:30-4pm) will be removed for agenda display. This makes the agenda less
1543 cluttered.
1544 The option can be t or nil. It may also be the symbol `beg', indicating
1545 that the time should only be removed when it is located at the beginning of
1546 the headline/diary entry."
1547 :group 'org-agenda-line-format
1548 :type '(choice
1549 (const :tag "Always" t)
1550 (const :tag "Never" nil)
1551 (const :tag "When at beginning of entry" beg)))
1553 (defcustom org-agenda-remove-timeranges-from-blocks nil
1554 "Non-nil means remove time ranges specifications in agenda
1555 items that span on several days."
1556 :group 'org-agenda-line-format
1557 :type 'boolean)
1559 (defcustom org-agenda-default-appointment-duration nil
1560 "Default duration for appointments that only have a starting time.
1561 When nil, no duration is specified in such cases.
1562 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1563 :group 'org-agenda-line-format
1564 :type '(choice
1565 (integer :tag "Minutes")
1566 (const :tag "No default duration")))
1568 (defcustom org-agenda-show-inherited-tags t
1569 "Non-nil means show inherited tags in each agenda line."
1570 :group 'org-agenda-line-format
1571 :type 'boolean)
1573 (defcustom org-agenda-hide-tags-regexp nil
1574 "Regular expression used to filter away specific tags in agenda views.
1575 This means that these tags will be present, but not be shown in the agenda
1576 line. Secondary filtering will still work on the hidden tags.
1577 Nil means don't hide any tags."
1578 :group 'org-agenda-line-format
1579 :type '(choice
1580 (const :tag "Hide none" nil)
1581 (string :tag "Regexp ")))
1583 (defcustom org-agenda-remove-tags nil
1584 "Non-nil means remove the tags from the headline copy in the agenda.
1585 When this is the symbol `prefix', only remove tags when
1586 `org-agenda-prefix-format' contains a `%T' specifier."
1587 :group 'org-agenda-line-format
1588 :type '(choice
1589 (const :tag "Always" t)
1590 (const :tag "Never" nil)
1591 (const :tag "When prefix format contains %T" prefix)))
1593 (if (fboundp 'defvaralias)
1594 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1595 'org-agenda-remove-tags))
1597 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1598 "Shift tags in agenda items to this column.
1599 If this number is positive, it specifies the column. If it is negative,
1600 it means that the tags should be flushright to that column. For example,
1601 -80 works well for a normal 80 character screen."
1602 :group 'org-agenda-line-format
1603 :type 'integer)
1605 (if (fboundp 'defvaralias)
1606 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
1608 (defcustom org-agenda-fontify-priorities 'cookies
1609 "Non-nil means highlight low and high priorities in agenda.
1610 When t, the highest priority entries are bold, lowest priority italic.
1611 However, settings in `org-priority-faces' will overrule these faces.
1612 When this variable is the symbol `cookies', only fontify the
1613 cookies, not the entire task.
1614 This may also be an association list of priority faces, whose
1615 keys are the character values of `org-highest-priority',
1616 `org-default-priority', and `org-lowest-priority' (the default values
1617 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1618 color as a string, or a list like `(:background \"Red\")'.
1619 If it is a color, the variable `org-faces-easy-properties'
1620 determines if it is a foreground or a background color."
1621 :group 'org-agenda-line-format
1622 :type '(choice
1623 (const :tag "Never" nil)
1624 (const :tag "Defaults" t)
1625 (const :tag "Cookies only" cookies)
1626 (repeat :tag "Specify"
1627 (list (character :tag "Priority" :value ?A)
1628 (choice :tag "Face "
1629 (string :tag "Color")
1630 (sexp :tag "Face"))))))
1632 (defcustom org-agenda-day-face-function nil
1633 "Function called to determine what face should be used to display a day.
1634 The only argument passed to that function is the day. It should
1635 returns a face, or nil if does not want to specify a face and let
1636 the normal rules apply."
1637 :group 'org-agenda-line-format
1638 :type 'function)
1640 (defcustom org-agenda-category-icon-alist nil
1641 "Alist of category icon to be displayed in agenda views.
1643 Each entry should have the following format:
1645 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1647 Where CATEGORY-REGEXP is a regexp matching the categories where
1648 the icon should be displayed.
1649 FILE-OR-DATA either a file path or a string containing image data.
1651 The other fields can be omited safely if not needed:
1652 TYPE indicates the image type.
1653 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1654 image data.
1655 PROPS are additional image attributes to assign to the image,
1656 like, e.g. `:ascent center'.
1658 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1660 If you want to set the display properties yourself, just put a
1661 list as second element:
1663 (CATEGORY-REGEXP (MY PROPERTY LIST))
1665 For example, to display a 16px horizontal space for Emacs
1666 category, you can use:
1668 (\"Emacs\" '(space . (:width (16))))"
1669 :group 'org-agenda-line-format
1670 :type '(alist :key-type (string :tag "Regexp matching category")
1671 :value-type (choice (list :tag "Icon"
1672 (string :tag "File or data")
1673 (symbol :tag "Type")
1674 (boolean :tag "Data?")
1675 (repeat :tag "Extra image properties" :inline t symbol))
1676 (list :tag "Display properties" sexp))))
1678 (defgroup org-agenda-column-view nil
1679 "Options concerning column view in the agenda."
1680 :tag "Org Agenda Column View"
1681 :group 'org-agenda)
1683 (defcustom org-agenda-columns-show-summaries t
1684 "Non-nil means show summaries for columns displayed in the agenda view."
1685 :group 'org-agenda-column-view
1686 :type 'boolean)
1688 (defcustom org-agenda-columns-compute-summary-properties t
1689 "Non-nil means recompute all summary properties before column view.
1690 When column view in the agenda is listing properties that have a summary
1691 operator, it can go to all relevant buffers and recompute the summaries
1692 there. This can mean overhead for the agenda column view, but is necessary
1693 to have thing up to date.
1694 As a special case, a CLOCKSUM property also makes sure that the clock
1695 computations are current."
1696 :group 'org-agenda-column-view
1697 :type 'boolean)
1699 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1700 "Non-nil means the duration of an appointment will add to day effort.
1701 The property to which appointment durations will be added is the one given
1702 in the option `org-effort-property'. If an appointment does not have
1703 an end time, `org-agenda-default-appointment-duration' will be used. If that
1704 is not set, an appointment without end time will not contribute to the time
1705 estimate."
1706 :group 'org-agenda-column-view
1707 :type 'boolean)
1709 (defcustom org-agenda-auto-exclude-function nil
1710 "A function called with a tag to decide if it is filtered on '/ RET'.
1711 The sole argument to the function, which is called once for each
1712 possible tag, is a string giving the name of the tag. The
1713 function should return either nil if the tag should be included
1714 as normal, or \"-<TAG>\" to exclude the tag.
1715 Note that for the purpose of tag filtering, only the lower-case version of
1716 all tags will be considered, so that this function will only ever see
1717 the lower-case version of all tags."
1718 :group 'org-agenda
1719 :type 'function)
1721 (defcustom org-agenda-bulk-custom-functions nil
1722 "Alist of characters and custom functions for bulk actions.
1723 For example, this value makes those two functions available:
1725 '((?R set-category)
1726 (?C bulk-cut))
1728 With selected entries in an agenda buffer, `B R' will call
1729 the custom function `set-category' on the selected entries.
1730 Note that functions in this alist don't need to be quoted."
1731 :type 'alist
1732 :group 'org-agenda)
1734 (eval-when-compile
1735 (require 'cl))
1736 (require 'org)
1738 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
1739 "Execute BODY with point at location given by `org-hd-marker' property.
1740 If STRING is non-nil, the text property will be fetched from position 0
1741 in that string. If STRING is nil, it will be fetched from the beginning
1742 of the current line."
1743 (org-with-gensyms (marker)
1744 `(let ((,marker (get-text-property (if string 0 (point-at-bol))
1745 'org-hd-marker ,string)))
1746 (with-current-buffer (marker-buffer ,marker)
1747 (save-excursion
1748 (goto-char ,marker)
1749 ,@body)))))
1750 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
1752 (defun org-add-agenda-custom-command (entry)
1753 "Replace or add a command in `org-agenda-custom-commands'.
1754 This is mostly for hacking and trying a new command - once the command
1755 works you probably want to add it to `org-agenda-custom-commands' for good."
1756 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
1757 (if ass
1758 (setcdr ass (cdr entry))
1759 (push entry org-agenda-custom-commands))))
1761 ;;; Define the Org-agenda-mode
1763 (defvar org-agenda-mode-map (make-sparse-keymap)
1764 "Keymap for `org-agenda-mode'.")
1765 (if (fboundp 'defvaralias)
1766 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map))
1768 (defvar org-agenda-menu) ; defined later in this file.
1769 (defvar org-agenda-restrict) ; defined later in this file.
1770 (defvar org-agenda-follow-mode nil)
1771 (defvar org-agenda-entry-text-mode nil)
1772 (defvar org-agenda-clockreport-mode nil)
1773 (defvar org-agenda-show-log nil)
1774 (defvar org-agenda-redo-command nil)
1775 (defvar org-agenda-query-string nil)
1776 (defvar org-agenda-mode-hook nil
1777 "Hook for `org-agenda-mode', run after the mode is turned on.")
1778 (defvar org-agenda-type nil)
1779 (defvar org-agenda-force-single-file nil)
1780 (defvar org-agenda-bulk-marked-entries) ;; Defined further down in this file
1782 (defun org-agenda-mode ()
1783 "Mode for time-sorted view on action items in Org-mode files.
1785 The following commands are available:
1787 \\{org-agenda-mode-map}"
1788 (interactive)
1789 (kill-all-local-variables)
1790 (setq org-agenda-undo-list nil
1791 org-agenda-pending-undo-list nil
1792 org-agenda-bulk-marked-entries nil)
1793 (setq major-mode 'org-agenda-mode)
1794 ;; Keep global-font-lock-mode from turning on font-lock-mode
1795 (org-set-local 'font-lock-global-modes (list 'not major-mode))
1796 (setq mode-name "Org-Agenda")
1797 (use-local-map org-agenda-mode-map)
1798 (easy-menu-add org-agenda-menu)
1799 (if org-startup-truncated (setq truncate-lines t))
1800 (org-set-local 'line-move-visual nil)
1801 (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
1802 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
1803 ;; Make sure properties are removed when copying text
1804 (when (boundp 'buffer-substring-filters)
1805 (org-set-local 'buffer-substring-filters
1806 (cons (lambda (x)
1807 (set-text-properties 0 (length x) nil x) x)
1808 buffer-substring-filters)))
1809 (unless org-agenda-keep-modes
1810 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
1811 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
1812 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
1813 org-agenda-show-log org-agenda-start-with-log-mode))
1815 (easy-menu-change
1816 '("Agenda") "Agenda Files"
1817 (append
1818 (list
1819 (vector
1820 (if (get 'org-agenda-files 'org-restrict)
1821 "Restricted to single file"
1822 "Edit File List")
1823 '(org-edit-agenda-file-list)
1824 (not (get 'org-agenda-files 'org-restrict)))
1825 "--")
1826 (mapcar 'org-file-menu-entry (org-agenda-files))))
1827 (org-agenda-set-mode-name)
1828 (apply
1829 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
1830 (list 'org-agenda-mode-hook)))
1832 (substitute-key-definition 'undo 'org-agenda-undo
1833 org-agenda-mode-map global-map)
1834 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
1835 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
1836 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
1837 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
1838 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
1839 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
1840 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
1841 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
1842 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-remove-all-marks)
1843 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
1844 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
1845 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
1846 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
1847 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
1848 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
1849 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
1850 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
1851 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
1852 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
1853 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
1854 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
1855 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
1856 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
1857 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
1858 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
1859 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
1860 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
1861 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
1862 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
1863 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
1864 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
1865 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
1866 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
1867 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
1868 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
1869 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
1870 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
1871 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
1872 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
1873 (org-defkey org-agenda-mode-map "k" 'org-agenda-action)
1874 (org-defkey org-agenda-mode-map "\C-c\C-x\C-k" 'org-agenda-action)
1875 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
1876 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
1877 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
1878 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
1880 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
1881 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
1882 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
1883 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
1884 (while l (org-defkey org-agenda-mode-map
1885 (int-to-string (pop l)) 'digit-argument)))
1887 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
1888 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
1889 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
1890 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
1891 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
1892 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
1893 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
1894 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
1895 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
1896 (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
1897 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
1898 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
1899 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
1900 'org-clock-modify-effort-estimate)
1901 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
1902 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
1903 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
1904 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
1905 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
1906 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
1907 (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
1908 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
1909 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
1910 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
1911 (substitute-key-definition 'next-line 'org-agenda-next-line
1912 org-agenda-mode-map global-map)
1913 (substitute-key-definition 'previous-line 'org-agenda-previous-line
1914 org-agenda-mode-map global-map)
1915 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
1916 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
1917 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
1918 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
1919 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
1920 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
1921 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
1922 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
1923 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
1924 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
1925 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
1926 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
1927 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
1928 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
1929 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
1930 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
1931 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
1932 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
1933 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
1934 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
1935 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
1936 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
1937 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
1938 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
1939 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
1940 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
1941 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
1942 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
1943 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
1944 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
1946 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
1947 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
1948 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
1949 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
1950 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
1951 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
1952 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
1953 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
1954 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
1955 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
1957 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
1958 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
1959 (when org-agenda-mouse-1-follows-link
1960 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
1961 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
1962 '("Agenda"
1963 ("Agenda Files")
1964 "--"
1965 ("Agenda Dates"
1966 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
1967 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
1968 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
1969 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
1970 "--"
1971 ("View"
1972 ["Day View" org-agenda-day-view
1973 :active (org-agenda-check-type nil 'agenda)
1974 :style radio :selected (eq org-agenda-current-span 'day)
1975 :keys "v d (or just d)"]
1976 ["Week View" org-agenda-week-view
1977 :active (org-agenda-check-type nil 'agenda)
1978 :style radio :selected (eq org-agenda-current-span 'week)
1979 :keys "v w (or just w)"]
1980 ["Month View" org-agenda-month-view
1981 :active (org-agenda-check-type nil 'agenda)
1982 :style radio :selected (eq org-agenda-current-span 'month)
1983 :keys "v m"]
1984 ["Year View" org-agenda-year-view
1985 :active (org-agenda-check-type nil 'agenda)
1986 :style radio :selected (eq org-agenda-current-span 'year)
1987 :keys "v y"]
1988 "--"
1989 ["Include Diary" org-agenda-toggle-diary
1990 :style toggle :selected org-agenda-include-diary
1991 :active (org-agenda-check-type nil 'agenda)]
1992 ["Include Deadlines" org-agenda-toggle-deadlines
1993 :style toggle :selected org-agenda-include-deadlines
1994 :active (org-agenda-check-type nil 'agenda)]
1995 ["Use Time Grid" org-agenda-toggle-time-grid
1996 :style toggle :selected org-agenda-use-time-grid
1997 :active (org-agenda-check-type nil 'agenda)]
1998 "--"
1999 ["Show clock report" org-agenda-clockreport-mode
2000 :style toggle :selected org-agenda-clockreport-mode
2001 :active (org-agenda-check-type nil 'agenda)]
2002 ["Show some entry text" org-agenda-entry-text-mode
2003 :style toggle :selected org-agenda-entry-text-mode
2004 :active t]
2005 "--"
2006 ["Show Logbook entries" org-agenda-log-mode
2007 :style toggle :selected org-agenda-show-log
2008 :active (org-agenda-check-type nil 'agenda 'timeline)
2009 :keys "v l (or just l)"]
2010 ["Include archived trees" org-agenda-archives-mode
2011 :style toggle :selected org-agenda-archives-mode :active t
2012 :keys "v a"]
2013 ["Include archive files" (org-agenda-archives-mode t)
2014 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2015 :keys "v A"]
2016 "--"
2017 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2018 ["Write view to file" org-write-agenda t]
2019 ["Rebuild buffer" org-agenda-redo t]
2020 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2021 "--"
2022 ["Show original entry" org-agenda-show t]
2023 ["Go To (other window)" org-agenda-goto t]
2024 ["Go To (this window)" org-agenda-switch-to t]
2025 ["Follow Mode" org-agenda-follow-mode
2026 :style toggle :selected org-agenda-follow-mode :active t]
2027 ; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2028 "--"
2029 ("TODO"
2030 ["Cycle TODO" org-agenda-todo t]
2031 ["Next TODO set" org-agenda-todo-nextset t]
2032 ["Previous TODO set" org-agenda-todo-previousset t]
2033 ["Add note" org-agenda-add-note t])
2034 ("Archive/Refile/Delete"
2035 ["Archive default" org-agenda-archive-default t]
2036 ["Archive default" org-agenda-archive-default-with-confirmation t]
2037 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2038 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2039 ["Archive subtree" org-agenda-archive t]
2040 "--"
2041 ["Refile" org-agenda-refile t]
2042 "--"
2043 ["Delete subtree" org-agenda-kill t])
2044 ("Bulk action"
2045 ["Mark entry" org-agenda-bulk-mark t]
2046 ["Mark matching regexp" org-agenda-bulk-mark-regexp t]
2047 ["Unmark entry" org-agenda-bulk-unmark t]
2048 ["Unmark all entries" org-agenda-bulk-remove-all-marks :active t :keys "C-u s"])
2049 ["Act on all marked" org-agenda-bulk-action t]
2050 "--"
2051 ("Tags and Properties"
2052 ["Show all Tags" org-agenda-show-tags t]
2053 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2054 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2055 "--"
2056 ["Column View" org-columns t])
2057 ("Deadline/Schedule"
2058 ["Schedule" org-agenda-schedule t]
2059 ["Set Deadline" org-agenda-deadline t]
2060 "--"
2061 ["Mark item" org-agenda-action :active t :keys "k m"]
2062 ["Show mark item" org-agenda-action :active t :keys "k v"]
2063 ["Schedule marked item" org-agenda-action :active t :keys "k s"]
2064 ["Set Deadline for marked item" org-agenda-action :active t :keys "k d"]
2065 "--"
2066 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2067 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2068 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2069 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2070 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2071 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2072 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2073 ("Clock and Effort"
2074 ["Clock in" org-agenda-clock-in t]
2075 ["Clock out" org-agenda-clock-out t]
2076 ["Clock cancel" org-agenda-clock-cancel t]
2077 ["Goto running clock" org-clock-goto t]
2078 "--"
2079 ["Set Effort" org-agenda-set-effort t]
2080 ["Change clocked effort" org-clock-modify-effort-estimate
2081 (org-clock-is-active)])
2082 ("Priority"
2083 ["Set Priority" org-agenda-priority t]
2084 ["Increase Priority" org-agenda-priority-up t]
2085 ["Decrease Priority" org-agenda-priority-down t]
2086 ["Show Priority" org-agenda-show-priority t])
2087 ("Calendar/Diary"
2088 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2089 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2090 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2091 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2092 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2093 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2094 "--"
2095 ["Create iCalendar File" org-export-icalendar-combine-agenda-files t])
2096 "--"
2097 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2098 "--"
2099 ("MobileOrg"
2100 ["Push Files and Views" org-mobile-push t]
2101 ["Get Captured and Flagged" org-mobile-pull t]
2102 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2103 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2104 "--"
2105 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2106 "--"
2107 ["Quit" org-agenda-quit t]
2108 ["Exit and Release Buffers" org-agenda-exit t]
2111 ;;; Agenda undo
2113 (defvar org-agenda-allow-remote-undo t
2114 "Non-nil means allow remote undo from the agenda buffer.")
2115 (defvar org-agenda-undo-list nil
2116 "List of undoable operations in the agenda since last refresh.")
2117 (defvar org-agenda-undo-has-started-in nil
2118 "Buffers that have already seen `undo-start' in the current undo sequence.")
2119 (defvar org-agenda-pending-undo-list nil
2120 "In a series of undo commands, this is the list of remaining undo items.")
2122 (defun org-agenda-undo ()
2123 "Undo a remote editing step in the agenda.
2124 This undoes changes both in the agenda buffer and in the remote buffer
2125 that have been changed along."
2126 (interactive)
2127 (or org-agenda-allow-remote-undo
2128 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2129 (if (not (eq this-command last-command))
2130 (setq org-agenda-undo-has-started-in nil
2131 org-agenda-pending-undo-list org-agenda-undo-list))
2132 (if (not org-agenda-pending-undo-list)
2133 (error "No further undo information"))
2134 (let* ((entry (pop org-agenda-pending-undo-list))
2135 buf line cmd rembuf)
2136 (setq cmd (pop entry) line (pop entry))
2137 (setq rembuf (nth 2 entry))
2138 (org-with-remote-undo rembuf
2139 (while (bufferp (setq buf (pop entry)))
2140 (if (pop entry)
2141 (with-current-buffer buf
2142 (let ((last-undo-buffer buf)
2143 (inhibit-read-only t))
2144 (unless (memq buf org-agenda-undo-has-started-in)
2145 (push buf org-agenda-undo-has-started-in)
2146 (make-local-variable 'pending-undo-list)
2147 (undo-start))
2148 (while (and pending-undo-list
2149 (listp pending-undo-list)
2150 (not (car pending-undo-list)))
2151 (pop pending-undo-list))
2152 (undo-more 1))))))
2153 (org-goto-line line)
2154 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2156 (defun org-verify-change-for-undo (l1 l2)
2157 "Verify that a real change occurred between the undo lists L1 and L2."
2158 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2159 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2160 (not (eq l1 l2)))
2162 ;;; Agenda dispatch
2164 (defvar org-agenda-restrict nil)
2165 (defvar org-agenda-restrict-begin (make-marker))
2166 (defvar org-agenda-restrict-end (make-marker))
2167 (defvar org-agenda-last-dispatch-buffer nil)
2168 (defvar org-agenda-overriding-restriction nil)
2170 ;;;###autoload
2171 (defun org-agenda (&optional arg keys restriction)
2172 "Dispatch agenda commands to collect entries to the agenda buffer.
2173 Prompts for a command to execute. Any prefix arg will be passed
2174 on to the selected command. The default selections are:
2176 a Call `org-agenda-list' to display the agenda for current day or week.
2177 t Call `org-todo-list' to display the global todo list.
2178 T Call `org-todo-list' to display the global todo list, select only
2179 entries with a specific TODO keyword (the user gets a prompt).
2180 m Call `org-tags-view' to display headlines with tags matching
2181 a condition (the user is prompted for the condition).
2182 M Like `m', but select only TODO entries, no ordinary headlines.
2183 L Create a timeline for the current buffer.
2184 e Export views to associated files.
2185 s Search entries for keywords.
2186 / Multi occur across all agenda files and also files listed
2187 in `org-agenda-text-search-extra-files'.
2188 < Restrict agenda commands to buffer, subtree, or region.
2189 Press several times to get the desired effect.
2190 > Remove a previous restriction.
2191 # List \"stuck\" projects.
2192 ! Configure what \"stuck\" means.
2193 C Configure custom agenda commands.
2195 More commands can be added by configuring the variable
2196 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2197 searches can be pre-defined in this way.
2199 If the current buffer is in Org-mode and visiting a file, you can also
2200 first press `<' once to indicate that the agenda should be temporarily
2201 \(until the next use of \\[org-agenda]) restricted to the current file.
2202 Pressing `<' twice means to restrict to the current subtree or region
2203 \(if active)."
2204 (interactive "P")
2205 (catch 'exit
2206 (let* ((prefix-descriptions nil)
2207 (org-agenda-window-setup (if (equal (buffer-name)
2208 org-agenda-buffer-name)
2209 'current-window
2210 org-agenda-window-setup))
2211 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2212 (org-agenda-custom-commands
2213 ;; normalize different versions
2214 (delq nil
2215 (mapcar
2216 (lambda (x)
2217 (cond ((stringp (cdr x))
2218 (push x prefix-descriptions)
2219 nil)
2220 ((stringp (nth 1 x)) x)
2221 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2222 (t (cons (car x) (cons "" (cdr x))))))
2223 org-agenda-custom-commands)))
2224 (buf (current-buffer))
2225 (bfn (buffer-file-name (buffer-base-buffer)))
2226 entry key type match lprops ans)
2227 ;; Turn off restriction unless there is an overriding one,
2228 (unless org-agenda-overriding-restriction
2229 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2230 ;; There is a request to keep the file list in place
2231 (put 'org-agenda-files 'org-restrict nil))
2232 (setq org-agenda-restrict nil)
2233 (move-marker org-agenda-restrict-begin nil)
2234 (move-marker org-agenda-restrict-end nil))
2235 ;; Delete old local properties
2236 (put 'org-agenda-redo-command 'org-lprops nil)
2237 ;; Delete previously set last-arguments
2238 (put 'org-agenda-redo-command 'last-args nil)
2239 ;; Remember where this call originated
2240 (setq org-agenda-last-dispatch-buffer (current-buffer))
2241 (unless keys
2242 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2243 keys (car ans)
2244 restriction (cdr ans)))
2245 ;; Establish the restriction, if any
2246 (when (and (not org-agenda-overriding-restriction) restriction)
2247 (put 'org-agenda-files 'org-restrict (list bfn))
2248 (cond
2249 ((eq restriction 'region)
2250 (setq org-agenda-restrict t)
2251 (move-marker org-agenda-restrict-begin (region-beginning))
2252 (move-marker org-agenda-restrict-end (region-end)))
2253 ((eq restriction 'subtree)
2254 (save-excursion
2255 (setq org-agenda-restrict t)
2256 (org-back-to-heading t)
2257 (move-marker org-agenda-restrict-begin (point))
2258 (move-marker org-agenda-restrict-end
2259 (progn (org-end-of-subtree t)))))))
2261 ;; For example the todo list should not need it (but does...)
2262 (cond
2263 ((setq entry (assoc keys org-agenda-custom-commands))
2264 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2265 (progn
2266 (setq type (nth 2 entry) match (eval (nth 3 entry))
2267 lprops (nth 4 entry))
2268 (put 'org-agenda-redo-command 'org-lprops lprops)
2269 (cond
2270 ((eq type 'agenda)
2271 (org-let lprops '(org-agenda-list current-prefix-arg)))
2272 ((eq type 'alltodo)
2273 (org-let lprops '(org-todo-list current-prefix-arg)))
2274 ((eq type 'search)
2275 (org-let lprops '(org-search-view current-prefix-arg match nil)))
2276 ((eq type 'stuck)
2277 (org-let lprops '(org-agenda-list-stuck-projects
2278 current-prefix-arg)))
2279 ((eq type 'tags)
2280 (org-let lprops '(org-tags-view current-prefix-arg match)))
2281 ((eq type 'tags-todo)
2282 (org-let lprops '(org-tags-view '(4) match)))
2283 ((eq type 'todo)
2284 (org-let lprops '(org-todo-list match)))
2285 ((eq type 'tags-tree)
2286 (org-check-for-org-mode)
2287 (org-let lprops '(org-match-sparse-tree current-prefix-arg match)))
2288 ((eq type 'todo-tree)
2289 (org-check-for-org-mode)
2290 (org-let lprops
2291 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2292 (regexp-quote match) "\\>"))))
2293 ((eq type 'occur-tree)
2294 (org-check-for-org-mode)
2295 (org-let lprops '(org-occur match)))
2296 ((functionp type)
2297 (org-let lprops '(funcall type match)))
2298 ((fboundp type)
2299 (org-let lprops '(funcall type match)))
2300 (t (error "Invalid custom agenda command type %s" type))))
2301 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2302 ((equal keys "C")
2303 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2304 (customize-variable 'org-agenda-custom-commands))
2305 ((equal keys "a") (call-interactively 'org-agenda-list))
2306 ((equal keys "s") (call-interactively 'org-search-view))
2307 ((equal keys "t") (call-interactively 'org-todo-list))
2308 ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2309 ((equal keys "m") (call-interactively 'org-tags-view))
2310 ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2311 ((equal keys "e") (call-interactively 'org-store-agenda-views))
2312 ((equal keys "?") (org-tags-view nil "+FLAGGED")
2313 (org-add-hook
2314 'post-command-hook
2315 (lambda ()
2316 (unless (current-message)
2317 (let* ((m (org-agenda-get-any-marker))
2318 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2319 (when note
2320 (message (concat
2321 "FLAGGING-NOTE ([?] for more info): "
2322 (org-add-props
2323 (replace-regexp-in-string
2324 "\\\\n" "//"
2325 (copy-sequence note))
2326 nil 'face 'org-warning)))))))
2327 t t))
2328 ((equal keys "L")
2329 (unless (eq major-mode 'org-mode)
2330 (error "This is not an Org-mode file"))
2331 (unless restriction
2332 (put 'org-agenda-files 'org-restrict (list bfn))
2333 (org-call-with-arg 'org-timeline arg)))
2334 ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2335 ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
2336 ((equal keys "!") (customize-variable 'org-stuck-projects))
2337 (t (error "Invalid agenda key"))))))
2339 (defun org-agenda-append-agenda ()
2340 "Append another agenda view to the current one.
2341 This function allows interactive building of block agendas.
2342 Agenda views are separated by `org-agenda-block-separator'."
2343 (interactive)
2344 (unless (string= (buffer-name) org-agenda-buffer-name)
2345 (error "Can only append from within agenda buffer"))
2346 (let ((org-agenda-multi t))
2347 (org-agenda)
2348 (widen)))
2350 (defun org-agenda-normalize-custom-commands (cmds)
2351 (delq nil
2352 (mapcar
2353 (lambda (x)
2354 (cond ((stringp (cdr x)) nil)
2355 ((stringp (nth 1 x)) x)
2356 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2357 (t (cons (car x) (cons "" (cdr x))))))
2358 cmds)))
2360 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2361 "The user interface for selecting an agenda command."
2362 (catch 'exit
2363 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2364 (restrict-ok (and bfn (eq major-mode 'org-mode)))
2365 (region-p (org-region-active-p))
2366 (custom org-agenda-custom-commands)
2367 (selstring "")
2368 restriction second-time
2369 c entry key type match prefixes rmheader header-end custom1 desc
2370 line lines left right n n1)
2371 (save-window-excursion
2372 (delete-other-windows)
2373 (org-switch-to-buffer-other-window " *Agenda Commands*")
2374 (erase-buffer)
2375 (insert (eval-when-compile
2376 (let ((header
2378 Press key for an agenda command: < Buffer, subtree/region restriction
2379 -------------------------------- > Remove restriction
2380 a Agenda for current week or day e Export agenda views
2381 t List of all TODO entries T Entries with special TODO kwd
2382 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2383 L Timeline for current buffer # List stuck projects (!=configure)
2384 s Search for keywords C Configure custom agenda commands
2385 / Multi-occur ? Find :FLAGGED: entries
2387 (start 0))
2388 (while (string-match
2389 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2390 header start)
2391 (setq start (match-end 0))
2392 (add-text-properties (match-beginning 2) (match-end 2)
2393 '(face bold) header))
2394 header)))
2395 (setq header-end (move-marker (make-marker) (point)))
2396 (while t
2397 (setq custom1 custom)
2398 (when (eq rmheader t)
2399 (org-goto-line 1)
2400 (re-search-forward ":" nil t)
2401 (delete-region (match-end 0) (point-at-eol))
2402 (forward-char 1)
2403 (looking-at "-+")
2404 (delete-region (match-end 0) (point-at-eol))
2405 (move-marker header-end (match-end 0)))
2406 (goto-char header-end)
2407 (delete-region (point) (point-max))
2409 ;; Produce all the lines that describe custom commands and prefixes
2410 (setq lines nil)
2411 (while (setq entry (pop custom1))
2412 (setq key (car entry) desc (nth 1 entry)
2413 type (nth 2 entry)
2414 match (nth 3 entry))
2415 (if (> (length key) 1)
2416 (add-to-list 'prefixes (string-to-char key))
2417 (setq line
2418 (format
2419 "%-4s%-14s"
2420 (org-add-props (copy-sequence key)
2421 '(face bold))
2422 (cond
2423 ((string-match "\\S-" desc) desc)
2424 ((eq type 'agenda) "Agenda for current week or day")
2425 ((eq type 'alltodo) "List of all TODO entries")
2426 ((eq type 'search) "Word search")
2427 ((eq type 'stuck) "List of stuck projects")
2428 ((eq type 'todo) "TODO keyword")
2429 ((eq type 'tags) "Tags query")
2430 ((eq type 'tags-todo) "Tags (TODO)")
2431 ((eq type 'tags-tree) "Tags tree")
2432 ((eq type 'todo-tree) "TODO kwd tree")
2433 ((eq type 'occur-tree) "Occur tree")
2434 ((functionp type) (if (symbolp type)
2435 (symbol-name type)
2436 "Lambda expression"))
2437 (t "???"))))
2438 (if org-agenda-menu-show-matcher
2439 (setq line
2440 (concat line ": "
2441 (cond
2442 ((stringp match)
2443 (setq match (copy-sequence match))
2444 (org-add-props match nil 'face 'org-warning))
2445 (match
2446 (format "set of %d commands" (length match)))
2447 (t ""))))
2448 (if (org-string-nw-p match)
2449 (add-text-properties
2450 0 (length line) (list 'help-echo
2451 (concat "Matcher: "match)) line)))
2452 (push line lines)))
2453 (setq lines (nreverse lines))
2454 (when prefixes
2455 (mapc (lambda (x)
2456 (push
2457 (format "%s %s"
2458 (org-add-props (char-to-string x)
2459 nil 'face 'bold)
2460 (or (cdr (assoc (concat selstring
2461 (char-to-string x))
2462 prefix-descriptions))
2463 "Prefix key"))
2464 lines))
2465 prefixes))
2467 ;; Check if we should display in two columns
2468 (if org-agenda-menu-two-column
2469 (progn
2470 (setq n (length lines)
2471 n1 (+ (/ n 2) (mod n 2))
2472 right (nthcdr n1 lines)
2473 left (copy-sequence lines))
2474 (setcdr (nthcdr (1- n1) left) nil))
2475 (setq left lines right nil))
2476 (while left
2477 (insert "\n" (pop left))
2478 (when right
2479 (if (< (current-column) 40)
2480 (move-to-column 40 t)
2481 (insert " "))
2482 (insert (pop right))))
2484 ;; Make the window the right size
2485 (goto-char (point-min))
2486 (if second-time
2487 (if (not (pos-visible-in-window-p (point-max)))
2488 (org-fit-window-to-buffer))
2489 (setq second-time t)
2490 (org-fit-window-to-buffer))
2492 ;; Ask for selection
2493 (message "Press key for agenda command%s:"
2494 (if (or restrict-ok org-agenda-overriding-restriction)
2495 (if org-agenda-overriding-restriction
2496 " (restriction lock active)"
2497 (if restriction
2498 (format " (restricted to %s)" restriction)
2499 " (unrestricted)"))
2500 ""))
2501 (setq c (read-char-exclusive))
2502 (message "")
2503 (cond
2504 ((assoc (char-to-string c) custom)
2505 (setq selstring (concat selstring (char-to-string c)))
2506 (throw 'exit (cons selstring restriction)))
2507 ((memq c prefixes)
2508 (setq selstring (concat selstring (char-to-string c))
2509 prefixes nil
2510 rmheader (or rmheader t)
2511 custom (delq nil (mapcar
2512 (lambda (x)
2513 (if (or (= (length (car x)) 1)
2514 (/= (string-to-char (car x)) c))
2516 (cons (substring (car x) 1) (cdr x))))
2517 custom))))
2518 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
2519 (message "Restriction is only possible in Org-mode buffers")
2520 (ding) (sit-for 1))
2521 ((eq c ?1)
2522 (org-agenda-remove-restriction-lock 'noupdate)
2523 (setq restriction 'buffer))
2524 ((eq c ?0)
2525 (org-agenda-remove-restriction-lock 'noupdate)
2526 (setq restriction (if region-p 'region 'subtree)))
2527 ((eq c ?<)
2528 (org-agenda-remove-restriction-lock 'noupdate)
2529 (setq restriction
2530 (cond
2531 ((eq restriction 'buffer)
2532 (if region-p 'region 'subtree))
2533 ((memq restriction '(subtree region))
2534 nil)
2535 (t 'buffer))))
2536 ((eq c ?>)
2537 (org-agenda-remove-restriction-lock 'noupdate)
2538 (setq restriction nil))
2539 ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
2540 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
2541 ((and (> (length selstring) 0) (eq c ?\d))
2542 (delete-window)
2543 (org-agenda-get-restriction-and-command prefix-descriptions))
2545 ((equal c ?q) (error "Abort"))
2546 (t (error "Invalid key %c" c))))))))
2548 (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
2549 (defvar org-agenda-last-arguments nil
2550 "The arguments of the previous call to `org-agenda'.")
2551 (defun org-agenda-run-series (name series)
2552 (org-let (nth 1 series) '(org-prepare-agenda name))
2553 (let* ((org-agenda-multi t)
2554 (redo (list 'org-agenda-run-series name (list 'quote series)))
2555 (org-agenda-overriding-arguments
2556 (or org-agenda-overriding-arguments
2557 (unless (null (delq nil (get 'org-agenda-redo-command 'last-args)))
2558 (get 'org-agenda-redo-command 'last-args))))
2559 (cmds (car series))
2560 (gprops (nth 1 series))
2561 match ;; The byte compiler incorrectly complains about this. Keep it!
2562 cmd type lprops)
2563 (while (setq cmd (pop cmds))
2564 (setq type (car cmd) match (eval (nth 1 cmd)) lprops (nth 2 cmd))
2565 (cond
2566 ((eq type 'agenda)
2567 (org-let2 gprops lprops
2568 '(call-interactively 'org-agenda-list)))
2569 ((eq type 'alltodo)
2570 (org-let2 gprops lprops
2571 '(call-interactively 'org-todo-list)))
2572 ((eq type 'search)
2573 (org-let2 gprops lprops
2574 '(org-search-view current-prefix-arg match nil)))
2575 ((eq type 'stuck)
2576 (org-let2 gprops lprops
2577 '(call-interactively 'org-agenda-list-stuck-projects)))
2578 ((eq type 'tags)
2579 (org-let2 gprops lprops
2580 '(org-tags-view current-prefix-arg match)))
2581 ((eq type 'tags-todo)
2582 (org-let2 gprops lprops
2583 '(org-tags-view '(4) match)))
2584 ((eq type 'todo)
2585 (org-let2 gprops lprops
2586 '(org-todo-list match)))
2587 ((fboundp type)
2588 (org-let2 gprops lprops
2589 '(funcall type match)))
2590 (t (error "Invalid type in command series"))))
2591 (widen)
2592 (setq org-agenda-redo-command redo)
2593 (put 'org-agenda-redo-command 'last-args org-agenda-last-arguments)
2594 (goto-char (point-min)))
2595 (org-fit-agenda-window)
2596 (org-let (nth 1 series) '(org-finalize-agenda)))
2598 ;;;###autoload
2599 (defmacro org-batch-agenda (cmd-key &rest parameters)
2600 "Run an agenda command in batch mode and send the result to STDOUT.
2601 If CMD-KEY is a string of length 1, it is used as a key in
2602 `org-agenda-custom-commands' and triggers this command. If it is a
2603 longer string it is used as a tags/todo match string.
2604 Parameters are alternating variable names and values that will be bound
2605 before running the agenda command."
2606 (org-eval-in-environment (org-make-parameter-alist parameters)
2607 (if (> (length cmd-key) 2)
2608 (org-tags-view nil cmd-key)
2609 (org-agenda nil cmd-key)))
2610 (set-buffer org-agenda-buffer-name)
2611 (princ (buffer-string)))
2612 (def-edebug-spec org-batch-agenda (form &rest sexp))
2614 (defvar org-agenda-info nil)
2616 ;;;###autoload
2617 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
2618 "Run an agenda command in batch mode and send the result to STDOUT.
2619 If CMD-KEY is a string of length 1, it is used as a key in
2620 `org-agenda-custom-commands' and triggers this command. If it is a
2621 longer string it is used as a tags/todo match string.
2622 Parameters are alternating variable names and values that will be bound
2623 before running the agenda command.
2625 The output gives a line for each selected agenda item. Each
2626 item is a list of comma-separated values, like this:
2628 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
2630 category The category of the item
2631 head The headline, without TODO kwd, TAGS and PRIORITY
2632 type The type of the agenda entry, can be
2633 todo selected in TODO match
2634 tagsmatch selected in tags match
2635 diary imported from diary
2636 deadline a deadline on given date
2637 scheduled scheduled on given date
2638 timestamp entry has timestamp on given date
2639 closed entry was closed on given date
2640 upcoming-deadline warning about deadline
2641 past-scheduled forwarded scheduled item
2642 block entry has date block including g. date
2643 todo The todo keyword, if any
2644 tags All tags including inherited ones, separated by colons
2645 date The relevant date, like 2007-2-14
2646 time The time, like 15:00-16:50
2647 extra Sting with extra planning info
2648 priority-l The priority letter if any was given
2649 priority-n The computed numerical priority
2650 agenda-day The day in the agenda where this is listed"
2651 (org-eval-in-environment (append '((org-agenda-remove-tags t))
2652 (org-make-parameter-alist parameters))
2653 (if (> (length cmd-key) 2)
2654 (org-tags-view nil cmd-key)
2655 (org-agenda nil cmd-key)))
2656 (set-buffer org-agenda-buffer-name)
2657 (let* ((lines (org-split-string (buffer-string) "\n"))
2658 line)
2659 (while (setq line (pop lines))
2660 (catch 'next
2661 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
2662 (setq org-agenda-info
2663 (org-fix-agenda-info (text-properties-at 0 line)))
2664 (princ
2665 (mapconcat 'org-agenda-export-csv-mapper
2666 '(org-category txt type todo tags date time extra
2667 priority-letter priority agenda-day)
2668 ","))
2669 (princ "\n")))))
2670 (def-edebug-spec org-batch-agenda-csv (form &rest sexp))
2672 (defun org-fix-agenda-info (props)
2673 "Make sure all properties on an agenda item have a canonical form.
2674 This ensures the export commands can easily use it."
2675 (let (tmp re)
2676 (when (setq tmp (plist-get props 'tags))
2677 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
2678 (when (setq tmp (plist-get props 'date))
2679 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2680 (let ((calendar-date-display-form '(year "-" month "-" day)))
2681 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
2683 (setq tmp (calendar-date-string tmp)))
2684 (setq props (plist-put props 'date tmp)))
2685 (when (setq tmp (plist-get props 'day))
2686 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2687 (let ((calendar-date-display-form '(year "-" month "-" day)))
2688 (setq tmp (calendar-date-string tmp)))
2689 (setq props (plist-put props 'day tmp))
2690 (setq props (plist-put props 'agenda-day tmp)))
2691 (when (setq tmp (plist-get props 'txt))
2692 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
2693 (plist-put props 'priority-letter (match-string 1 tmp))
2694 (setq tmp (replace-match "" t t tmp)))
2695 (when (and (setq re (plist-get props 'org-todo-regexp))
2696 (setq re (concat "\\`\\.*" re " ?"))
2697 (string-match re tmp))
2698 (plist-put props 'todo (match-string 1 tmp))
2699 (setq tmp (replace-match "" t t tmp)))
2700 (plist-put props 'txt tmp)))
2701 props)
2703 (defun org-agenda-export-csv-mapper (prop)
2704 (let ((res (plist-get org-agenda-info prop)))
2705 (setq res
2706 (cond
2707 ((not res) "")
2708 ((stringp res) res)
2709 (t (prin1-to-string res))))
2710 (while (string-match "," res)
2711 (setq res (replace-match ";" t t res)))
2712 (org-trim res)))
2715 ;;;###autoload
2716 (defun org-store-agenda-views (&rest parameters)
2717 (interactive)
2718 (eval (list 'org-batch-store-agenda-views)))
2720 ;;;###autoload
2721 (defmacro org-batch-store-agenda-views (&rest parameters)
2722 "Run all custom agenda commands that have a file argument."
2723 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
2724 (pop-up-frames nil)
2725 (dir default-directory)
2726 (pars (org-make-parameter-alist parameters))
2727 cmd thiscmdkey files opts cmd-or-set)
2728 (save-window-excursion
2729 (while cmds
2730 (setq cmd (pop cmds)
2731 thiscmdkey (car cmd)
2732 cmd-or-set (nth 2 cmd)
2733 opts (nth (if (listp cmd-or-set) 3 4) cmd)
2734 files (nth (if (listp cmd-or-set) 4 5) cmd))
2735 (if (stringp files) (setq files (list files)))
2736 (when files
2737 (org-eval-in-environment (append org-agenda-exporter-settings
2738 opts pars)
2739 (org-agenda nil thiscmdkey))
2740 (set-buffer org-agenda-buffer-name)
2741 (while files
2742 (org-eval-in-environment (append org-agenda-exporter-settings
2743 opts pars)
2744 (org-write-agenda (expand-file-name (pop files) dir) nil t)))
2745 (and (get-buffer org-agenda-buffer-name)
2746 (kill-buffer org-agenda-buffer-name)))))))
2747 (def-edebug-spec org-batch-store-agenda-views (&rest sexp))
2749 (defun org-agenda-mark-header-line (pos)
2750 "Mark the line at POS as an agenda structure header."
2751 (save-excursion
2752 (goto-char pos)
2753 (put-text-property (point-at-bol) (point-at-eol)
2754 'org-agenda-structural-header t)
2755 (when org-agenda-title-append
2756 (put-text-property (point-at-bol) (point-at-eol)
2757 'org-agenda-title-append org-agenda-title-append))))
2759 (defvar org-mobile-creating-agendas)
2760 (defun org-write-agenda (file &optional open nosettings)
2761 "Write the current buffer (an agenda view) as a file.
2762 Depending on the extension of the file name, plain text (.txt),
2763 HTML (.html or .htm) or Postscript (.ps) is produced.
2764 If the extension is .ics, run icalendar export over all files used
2765 to construct the agenda and limit the export to entries listed in the
2766 agenda now.
2767 With prefix argument OPEN, open the new file immediately.
2768 If NOSETTINGS is given, do not scope the settings of
2769 `org-agenda-exporter-settings' into the export commands. This is used when
2770 the settings have already been scoped and we do not wish to overrule other,
2771 higher priority settings."
2772 (interactive "FWrite agenda to file: \nP")
2773 (if (not (file-writable-p file))
2774 (error "Cannot write agenda to file %s" file))
2775 (org-let (if nosettings nil org-agenda-exporter-settings)
2776 '(save-excursion
2777 (save-window-excursion
2778 (org-agenda-mark-filtered-text)
2779 (let ((bs (copy-sequence (buffer-string))) beg)
2780 (org-agenda-unmark-filtered-text)
2781 (with-temp-buffer
2782 (rename-buffer "Agenda View" t)
2783 (set-buffer-modified-p nil)
2784 (insert bs)
2785 (org-agenda-remove-marked-text 'org-filtered)
2786 (while (setq beg (text-property-any (point-min) (point-max)
2787 'org-filtered t))
2788 (delete-region
2789 beg (or (next-single-property-change beg 'org-filtered)
2790 (point-max))))
2791 (run-hooks 'org-agenda-before-write-hook)
2792 (cond
2793 ((org-bound-and-true-p org-mobile-creating-agendas)
2794 (org-mobile-write-agenda-for-mobile file))
2795 ((string-match "\\.html?\\'" file)
2796 (require 'htmlize)
2797 (set-buffer (htmlize-buffer (current-buffer)))
2799 (when (and org-agenda-export-html-style
2800 (string-match "<style>" org-agenda-export-html-style))
2801 ;; replace <style> section with org-agenda-export-html-style
2802 (goto-char (point-min))
2803 (kill-region (- (search-forward "<style") 6)
2804 (search-forward "</style>"))
2805 (insert org-agenda-export-html-style))
2806 (write-file file)
2807 (kill-buffer (current-buffer))
2808 (message "HTML written to %s" file))
2809 ((string-match "\\.ps\\'" file)
2810 (require 'ps-print)
2811 (ps-print-buffer-with-faces file)
2812 (message "Postscript written to %s" file))
2813 ((string-match "\\.pdf\\'" file)
2814 (require 'ps-print)
2815 (ps-print-buffer-with-faces
2816 (concat (file-name-sans-extension file) ".ps"))
2817 (call-process "ps2pdf" nil nil nil
2818 (expand-file-name
2819 (concat (file-name-sans-extension file) ".ps"))
2820 (expand-file-name file))
2821 (delete-file (concat (file-name-sans-extension file) ".ps"))
2822 (message "PDF written to %s" file))
2823 ((string-match "\\.ics\\'" file)
2824 (require 'org-icalendar)
2825 (let ((org-agenda-marker-table
2826 (org-create-marker-find-array
2827 (org-agenda-collect-markers)))
2828 (org-icalendar-verify-function 'org-check-agenda-marker-table)
2829 (org-combined-agenda-icalendar-file file))
2830 (apply 'org-export-icalendar 'combine
2831 (org-agenda-files nil 'ifmode))))
2833 (let ((bs (buffer-string)))
2834 (find-file file)
2835 (erase-buffer)
2836 (insert bs)
2837 (save-buffer 0)
2838 (kill-buffer (current-buffer))
2839 (message "Plain text written to %s" file))))))))
2840 (set-buffer org-agenda-buffer-name))
2841 (when open (org-open-file file)))
2843 (defvar org-agenda-filter-overlays nil)
2845 (defun org-agenda-mark-filtered-text ()
2846 "Mark all text hidden by filtering with a text property."
2847 (let ((inhibit-read-only t))
2848 (mapc
2849 (lambda (o)
2850 (when (equal (overlay-buffer o) (current-buffer))
2851 (put-text-property
2852 (overlay-start o) (overlay-end o)
2853 'org-filtered t)))
2854 org-agenda-filter-overlays)))
2856 (defun org-agenda-unmark-filtered-text ()
2857 "Remove the filtering text property."
2858 (let ((inhibit-read-only t))
2859 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
2861 (defun org-agenda-remove-marked-text (property &optional value)
2862 "Delete all text marked with VALUE of PROPERTY.
2863 VALUE defaults to t."
2864 (let (beg)
2865 (setq value (or value t))
2866 (while (setq beg (text-property-any (point-min) (point-max)
2867 property value))
2868 (delete-region
2869 beg (or (next-single-property-change beg 'org-filtered)
2870 (point-max))))))
2872 (defun org-agenda-add-entry-text ()
2873 "Add entry text to agenda lines.
2874 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
2875 entry text following headings shown in the agenda.
2876 Drawers will be excluded, also the line with scheduling/deadline info."
2877 (when (and (> org-agenda-add-entry-text-maxlines 0)
2878 (not (org-bound-and-true-p org-mobile-creating-agendas)))
2879 (let (m txt)
2880 (goto-char (point-min))
2881 (while (not (eobp))
2882 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
2883 (beginning-of-line 2)
2884 (setq txt (org-agenda-get-some-entry-text
2885 m org-agenda-add-entry-text-maxlines " > "))
2886 (end-of-line 1)
2887 (if (string-match "\\S-" txt)
2888 (insert "\n" txt)
2889 (or (eobp) (forward-char 1))))))))
2891 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
2892 &rest keep)
2893 "Extract entry text from MARKER, at most N-LINES lines.
2894 This will ignore drawers etc, just get the text.
2895 If INDENT is given, prefix every line with this string. If KEEP is
2896 given, it is a list of symbols, defining stuff that should not be
2897 removed from the entry content. Currently only `planning' is allowed here."
2898 (let (txt drawer-re kwd-time-re ind)
2899 (save-excursion
2900 (with-current-buffer (marker-buffer marker)
2901 (if (not (eq major-mode 'org-mode))
2902 (setq txt "")
2903 (save-excursion
2904 (save-restriction
2905 (widen)
2906 (goto-char marker)
2907 (end-of-line 1)
2908 (setq txt (buffer-substring
2909 (min (1+ (point)) (point-max))
2910 (progn (outline-next-heading) (point)))
2911 drawer-re org-drawer-regexp
2912 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
2913 ".*\n?"))
2914 (with-temp-buffer
2915 (insert txt)
2916 (when org-agenda-add-entry-text-descriptive-links
2917 (goto-char (point-min))
2918 (while (org-activate-bracket-links (point-max))
2919 (add-text-properties (match-beginning 0) (match-end 0)
2920 '(face org-link))))
2921 (goto-char (point-min))
2922 (while (re-search-forward org-bracket-link-regexp (point-max) t)
2923 (set-text-properties (match-beginning 0) (match-end 0)
2924 nil))
2925 (goto-char (point-min))
2926 (while (re-search-forward drawer-re nil t)
2927 (delete-region
2928 (match-beginning 0)
2929 (progn (re-search-forward
2930 "^[ \t]*:END:.*\n?" nil 'move)
2931 (point))))
2932 (unless (member 'planning keep)
2933 (goto-char (point-min))
2934 (while (re-search-forward kwd-time-re nil t)
2935 (replace-match "")))
2936 (goto-char (point-min))
2937 (when org-agenda-entry-text-exclude-regexps
2938 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
2939 (while (setq re (pop re-list))
2940 (goto-char (point-min))
2941 (while (re-search-forward re nil t)
2942 (replace-match "")))))
2943 (goto-char (point-max))
2944 (skip-chars-backward " \t\n")
2945 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
2947 ;; find and remove min common indentation
2948 (goto-char (point-min))
2949 (untabify (point-min) (point-max))
2950 (setq ind (org-get-indentation))
2951 (while (not (eobp))
2952 (unless (looking-at "[ \t]*$")
2953 (setq ind (min ind (org-get-indentation))))
2954 (beginning-of-line 2))
2955 (goto-char (point-min))
2956 (while (not (eobp))
2957 (unless (looking-at "[ \t]*$")
2958 (move-to-column ind)
2959 (delete-region (point-at-bol) (point)))
2960 (beginning-of-line 2))
2962 (run-hooks 'org-agenda-entry-text-cleanup-hook)
2964 (goto-char (point-min))
2965 (when indent
2966 (while (and (not (eobp)) (re-search-forward "^" nil t))
2967 (replace-match indent t t)))
2968 (goto-char (point-min))
2969 (while (looking-at "[ \t]*\n") (replace-match ""))
2970 (goto-char (point-max))
2971 (when (> (org-current-line)
2972 n-lines)
2973 (org-goto-line (1+ n-lines))
2974 (backward-char 1))
2975 (setq txt (buffer-substring (point-min) (point)))))))))
2976 txt))
2978 (defun org-agenda-collect-markers ()
2979 "Collect the markers pointing to entries in the agenda buffer."
2980 (let (m markers)
2981 (save-excursion
2982 (goto-char (point-min))
2983 (while (not (eobp))
2984 (when (setq m (or (org-get-at-bol 'org-hd-marker)
2985 (org-get-at-bol 'org-marker)))
2986 (push m markers))
2987 (beginning-of-line 2)))
2988 (nreverse markers)))
2990 (defun org-create-marker-find-array (marker-list)
2991 "Create a alist of files names with all marker positions in that file."
2992 (let (f tbl m a p)
2993 (while (setq m (pop marker-list))
2994 (setq p (marker-position m)
2995 f (buffer-file-name (or (buffer-base-buffer
2996 (marker-buffer m))
2997 (marker-buffer m))))
2998 (if (setq a (assoc f tbl))
2999 (push (marker-position m) (cdr a))
3000 (push (list f p) tbl)))
3001 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
3002 tbl)))
3004 (defvar org-agenda-marker-table nil) ; dynamically scoped parameter
3005 (defun org-check-agenda-marker-table ()
3006 "Check of the current entry is on the marker list."
3007 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
3009 (and (setq a (assoc file org-agenda-marker-table))
3010 (save-match-data
3011 (save-excursion
3012 (org-back-to-heading t)
3013 (member (point) (cdr a)))))))
3015 (defun org-check-for-org-mode ()
3016 "Make sure current buffer is in org-mode. Error if not."
3017 (or (eq major-mode 'org-mode)
3018 (error "Cannot execute org-mode agenda command on buffer in %s"
3019 major-mode)))
3021 (defun org-fit-agenda-window ()
3022 "Fit the window to the buffer size."
3023 (and (memq org-agenda-window-setup '(reorganize-frame))
3024 (fboundp 'fit-window-to-buffer)
3025 (org-fit-window-to-buffer
3027 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3028 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
3030 ;;; Agenda prepare and finalize
3032 (defvar org-agenda-multi nil) ; dynamically scoped
3033 (defvar org-agenda-buffer-name "*Org Agenda*")
3034 (defvar org-pre-agenda-window-conf nil)
3035 (defvar org-agenda-columns-active nil)
3036 (defvar org-agenda-name nil)
3037 (defvar org-agenda-filter nil)
3038 (defvar org-agenda-filter-while-redo nil)
3039 (defvar org-agenda-filter-preset nil
3040 "A preset of the tags filter used for secondary agenda filtering.
3041 This must be a list of strings, each string must be a single tag preceded
3042 by \"+\" or \"-\".
3043 This variable should not be set directly, but agenda custom commands can
3044 bind it in the options section. The preset filter is a global property of
3045 the entire agenda view. In a block agenda, it will not work reliably to
3046 define a filter for one of the individual blocks. You need to set it in
3047 the global options and expect it to be applied to the entire view.")
3049 (defun org-prepare-agenda (&optional name)
3050 (setq org-todo-keywords-for-agenda nil)
3051 (setq org-done-keywords-for-agenda nil)
3052 (setq org-drawers-for-agenda nil)
3053 (unless org-agenda-persistent-filter
3054 (setq org-agenda-filter nil))
3055 (put 'org-agenda-filter :preset-filter org-agenda-filter-preset)
3056 (if org-agenda-multi
3057 (progn
3058 (setq buffer-read-only nil)
3059 (goto-char (point-max))
3060 (unless (or (bobp) org-agenda-compact-blocks
3061 (not org-agenda-block-separator))
3062 (insert "\n"
3063 (if (stringp org-agenda-block-separator)
3064 org-agenda-block-separator
3065 (make-string (window-width) org-agenda-block-separator))
3066 "\n"))
3067 (narrow-to-region (point) (point-max)))
3068 (org-agenda-reset-markers)
3069 (setq org-agenda-contributing-files nil)
3070 (setq org-agenda-columns-active nil)
3071 (org-prepare-agenda-buffers (org-agenda-files nil 'ifmode))
3072 (setq org-todo-keywords-for-agenda
3073 (org-uniquify org-todo-keywords-for-agenda))
3074 (setq org-done-keywords-for-agenda
3075 (org-uniquify org-done-keywords-for-agenda))
3076 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
3077 (let* ((abuf (get-buffer-create org-agenda-buffer-name))
3078 (awin (get-buffer-window abuf)))
3079 (cond
3080 ((equal (current-buffer) abuf) nil)
3081 (awin (select-window awin))
3082 ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
3083 ((equal org-agenda-window-setup 'current-window)
3084 (org-pop-to-buffer-same-window abuf))
3085 ((equal org-agenda-window-setup 'other-window)
3086 (org-switch-to-buffer-other-window abuf))
3087 ((equal org-agenda-window-setup 'other-frame)
3088 (switch-to-buffer-other-frame abuf))
3089 ((equal org-agenda-window-setup 'reorganize-frame)
3090 (delete-other-windows)
3091 (org-switch-to-buffer-other-window abuf)))
3092 ;; additional test in case agenda is invoked from within agenda
3093 ;; buffer via elisp link
3094 (unless (equal (current-buffer) abuf)
3095 (org-pop-to-buffer-same-window abuf)))
3096 (setq buffer-read-only nil)
3097 (let ((inhibit-read-only t)) (erase-buffer))
3098 (org-agenda-mode)
3099 (and name (not org-agenda-name)
3100 (org-set-local 'org-agenda-name name)))
3101 (setq buffer-read-only nil))
3103 (defun org-finalize-agenda ()
3104 "Finishing touch for the agenda buffer, called just before displaying it."
3105 (unless org-agenda-multi
3106 (save-excursion
3107 (let ((inhibit-read-only t))
3108 (goto-char (point-min))
3109 (while (org-activate-bracket-links (point-max))
3110 (add-text-properties (match-beginning 0) (match-end 0)
3111 '(face org-link)))
3112 (org-agenda-align-tags)
3113 (unless org-agenda-with-colors
3114 (remove-text-properties (point-min) (point-max) '(face nil))))
3115 (if (and (boundp 'org-agenda-overriding-columns-format)
3116 org-agenda-overriding-columns-format)
3117 (org-set-local 'org-agenda-overriding-columns-format
3118 org-agenda-overriding-columns-format))
3119 (if (and (boundp 'org-agenda-view-columns-initially)
3120 org-agenda-view-columns-initially)
3121 (org-agenda-columns))
3122 (when org-agenda-fontify-priorities
3123 (org-agenda-fontify-priorities))
3124 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3125 (org-agenda-dim-blocked-tasks))
3126 (org-agenda-mark-clocking-task)
3127 (when org-agenda-entry-text-mode
3128 (org-agenda-entry-text-hide)
3129 (org-agenda-entry-text-show))
3130 (if (functionp 'org-habit-insert-consistency-graphs)
3131 (org-habit-insert-consistency-graphs))
3132 (run-hooks 'org-finalize-agenda-hook)
3133 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3134 (when (or org-agenda-filter (get 'org-agenda-filter :preset-filter))
3135 (org-agenda-filter-apply org-agenda-filter))
3138 (defun org-agenda-mark-clocking-task ()
3139 "Mark the current clock entry in the agenda if it is present."
3140 (mapc (lambda (o)
3141 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3142 (delete-overlay o)))
3143 (overlays-in (point-min) (point-max)))
3144 (when (marker-buffer org-clock-hd-marker)
3145 (save-excursion
3146 (goto-char (point-min))
3147 (let (s ov)
3148 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3149 (goto-char s)
3150 (when (equal (org-get-at-bol 'org-hd-marker)
3151 org-clock-hd-marker)
3152 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3153 (overlay-put ov 'type 'org-agenda-clocking)
3154 (overlay-put ov 'face 'org-agenda-clocking)
3155 (overlay-put ov 'help-echo
3156 "The clock is running in this item")))))))
3158 (defun org-agenda-fontify-priorities ()
3159 "Make highest priority lines bold, and lowest italic."
3160 (interactive)
3161 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3162 (delete-overlay o)))
3163 (overlays-in (point-min) (point-max)))
3164 (save-excursion
3165 (let ((inhibit-read-only t)
3166 b e p ov h l)
3167 (goto-char (point-min))
3168 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3169 (setq h (or (get-char-property (point) 'org-highest-priority)
3170 org-highest-priority)
3171 l (or (get-char-property (point) 'org-lowest-priority)
3172 org-lowest-priority)
3173 p (string-to-char (match-string 1))
3174 b (match-beginning 0)
3175 e (if (eq org-agenda-fontify-priorities 'cookies)
3176 (match-end 0)
3177 (point-at-eol))
3178 ov (make-overlay b e))
3179 (overlay-put
3180 ov 'face
3181 (cond ((org-face-from-face-or-color
3182 'priority nil
3183 (cdr (assoc p org-priority-faces))))
3184 ((and (listp org-agenda-fontify-priorities)
3185 (org-face-from-face-or-color
3186 'priority nil
3187 (cdr (assoc p org-agenda-fontify-priorities)))))
3188 ((equal p l) 'italic)
3189 ((equal p h) 'bold)))
3190 (overlay-put ov 'org-type 'org-priority)))))
3192 (defun org-agenda-dim-blocked-tasks ()
3193 "Dim currently blocked TODO's in the agenda display."
3194 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3195 (delete-overlay o)))
3196 (overlays-in (point-min) (point-max)))
3197 (save-excursion
3198 (let ((inhibit-read-only t)
3199 (org-depend-tag-blocked nil)
3200 (invis (eq org-agenda-dim-blocked-tasks 'invisible))
3201 org-blocked-by-checkboxes
3202 invis1 b e p ov h l)
3203 (goto-char (point-min))
3204 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3205 (and pos (goto-char (1+ pos))))
3206 (setq org-blocked-by-checkboxes nil invis1 invis)
3207 (let ((marker (org-get-at-bol 'org-hd-marker)))
3208 (when (and marker
3209 (with-current-buffer (marker-buffer marker)
3210 (save-excursion (goto-char marker)
3211 (org-entry-blocked-p))))
3212 (if org-blocked-by-checkboxes (setq invis1 nil))
3213 (setq b (if invis1
3214 (max (point-min) (1- (point-at-bol)))
3215 (point-at-bol))
3216 e (point-at-eol)
3217 ov (make-overlay b e))
3218 (if invis1
3219 (overlay-put ov 'invisible t)
3220 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3221 (overlay-put ov 'org-type 'org-blocked-todo)))))))
3223 (defvar org-agenda-skip-function nil
3224 "Function to be called at each match during agenda construction.
3225 If this function returns nil, the current match should not be skipped.
3226 Otherwise, the function must return a position from where the search
3227 should be continued.
3228 This may also be a Lisp form, it will be evaluated.
3229 Never set this variable using `setq' or so, because then it will apply
3230 to all future agenda commands. If you do want a global skipping condition,
3231 use the option `org-agenda-skip-function-global' instead.
3232 The correct usage for `org-agenda-skip-function' is to bind it with
3233 `let' to scope it dynamically into the agenda-constructing command.
3234 A good way to set it is through options in `org-agenda-custom-commands'.")
3236 (defun org-agenda-skip ()
3237 "Throw to `:skip' in places that should be skipped.
3238 Also moves point to the end of the skipped region, so that search can
3239 continue from there."
3240 (let ((p (point-at-bol)) to)
3241 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3242 (get-text-property p :org-archived)
3243 (org-end-of-subtree t)
3244 (throw :skip t))
3245 (and org-agenda-skip-comment-trees
3246 (get-text-property p :org-comment)
3247 (org-end-of-subtree t)
3248 (throw :skip t))
3249 (if (equal (char-after p) ?#) (throw :skip t))
3250 (when (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3251 (org-agenda-skip-eval org-agenda-skip-function)))
3252 (goto-char to)
3253 (throw :skip t))))
3255 (defun org-agenda-skip-eval (form)
3256 "If FORM is a function or a list, call (or eval) is and return result.
3257 `save-excursion' and `save-match-data' are wrapped around the call, so point
3258 and match data are returned to the previous state no matter what these
3259 functions do."
3260 (let (fp)
3261 (and form
3262 (or (setq fp (functionp form))
3263 (consp form))
3264 (save-excursion
3265 (save-match-data
3266 (if fp
3267 (funcall form)
3268 (eval form)))))))
3270 (defvar org-agenda-markers nil
3271 "List of all currently active markers created by `org-agenda'.")
3272 (defvar org-agenda-last-marker-time (org-float-time)
3273 "Creation time of the last agenda marker.")
3275 (defun org-agenda-new-marker (&optional pos)
3276 "Return a new agenda marker.
3277 Org-mode keeps a list of these markers and resets them when they are
3278 no longer in use."
3279 (let ((m (copy-marker (or pos (point)))))
3280 (setq org-agenda-last-marker-time (org-float-time))
3281 (push m org-agenda-markers)
3284 (defun org-agenda-reset-markers ()
3285 "Reset markers created by `org-agenda'."
3286 (while org-agenda-markers
3287 (move-marker (pop org-agenda-markers) nil)))
3289 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3290 "Save relative positions of markers in region."
3291 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3292 org-agenda-markers))
3294 ;;; Entry text mode
3296 (defun org-agenda-entry-text-show-here ()
3297 "Add some text from the entry as context to the current line."
3298 (let (m txt o)
3299 (setq m (org-get-at-bol 'org-hd-marker))
3300 (unless (marker-buffer m)
3301 (error "No marker points to an entry here"))
3302 (setq txt (concat "\n" (org-no-properties
3303 (org-agenda-get-some-entry-text
3304 m org-agenda-entry-text-maxlines " > "))))
3305 (when (string-match "\\S-" txt)
3306 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3307 (overlay-put o 'evaporate t)
3308 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3309 (overlay-put o 'after-string txt))))
3311 (defun org-agenda-entry-text-show ()
3312 "Add entry context for all agenda lines."
3313 (interactive)
3314 (save-excursion
3315 (goto-char (point-max))
3316 (beginning-of-line 1)
3317 (while (not (bobp))
3318 (when (org-get-at-bol 'org-hd-marker)
3319 (org-agenda-entry-text-show-here))
3320 (beginning-of-line 0))))
3322 (defun org-agenda-entry-text-hide ()
3323 "Remove any shown entry context."
3324 (delq nil
3325 (mapcar (lambda (o)
3326 (if (eq (overlay-get o 'org-overlay-type)
3327 'agenda-entry-content)
3328 (progn (delete-overlay o) t)))
3329 (overlays-in (point-min) (point-max)))))
3331 (defun org-agenda-get-day-face (date)
3332 "Return the face DATE should be displayed with."
3333 (or (and (functionp org-agenda-day-face-function)
3334 (funcall org-agenda-day-face-function date))
3335 (cond ((org-agenda-todayp date)
3336 'org-agenda-date-today)
3337 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3338 'org-agenda-date-weekend)
3339 (t 'org-agenda-date))))
3341 ;;; Agenda timeline
3343 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3345 (defun org-timeline (&optional dotodo)
3346 "Show a time-sorted view of the entries in the current org file.
3347 Only entries with a time stamp of today or later will be listed. With
3348 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
3349 under the current date.
3350 If the buffer contains an active region, only check the region for
3351 dates."
3352 (interactive "P")
3353 (org-compile-prefix-format 'timeline)
3354 (org-set-sorting-strategy 'timeline)
3355 (let* ((dopast t)
3356 (doclosed org-agenda-show-log)
3357 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3358 (current-buffer))))
3359 (date (calendar-current-date))
3360 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3361 (end (if (org-region-active-p) (region-end) (point-max)))
3362 (day-numbers (org-get-all-dates beg end 'no-ranges
3363 t doclosed ; always include today
3364 org-timeline-show-empty-dates))
3365 (org-deadline-warning-days 0)
3366 (org-agenda-only-exact-dates t)
3367 (today (org-today))
3368 (past t)
3369 args
3370 s e rtn d emptyp)
3371 (setq org-agenda-redo-command
3372 (list 'progn
3373 (list 'org-switch-to-buffer-other-window (current-buffer))
3374 (list 'org-timeline (list 'quote dotodo))))
3375 (if (not dopast)
3376 ;; Remove past dates from the list of dates.
3377 (setq day-numbers (delq nil (mapcar (lambda(x)
3378 (if (>= x today) x nil))
3379 day-numbers))))
3380 (org-prepare-agenda (concat "Timeline " (file-name-nondirectory entry)))
3381 (if doclosed (push :closed args))
3382 (push :timestamp args)
3383 (push :deadline args)
3384 (push :scheduled args)
3385 (push :sexp args)
3386 (if dotodo (push :todo args))
3387 (insert "Timeline of file " entry "\n")
3388 (add-text-properties (point-min) (point)
3389 (list 'face 'org-agenda-structure))
3390 (org-agenda-mark-header-line (point-min))
3391 (while (setq d (pop day-numbers))
3392 (if (and (listp d) (eq (car d) :omitted))
3393 (progn
3394 (setq s (point))
3395 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
3396 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
3397 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
3398 (if (and (>= d today)
3399 dopast
3400 past)
3401 (progn
3402 (setq past nil)
3403 (insert (make-string 79 ?-) "\n")))
3404 (setq date (calendar-gregorian-from-absolute d))
3405 (setq s (point))
3406 (setq rtn (and (not emptyp)
3407 (apply 'org-agenda-get-day-entries entry
3408 date args)))
3409 (if (or rtn (equal d today) org-timeline-show-empty-dates)
3410 (progn
3411 (insert
3412 (if (stringp org-agenda-format-date)
3413 (format-time-string org-agenda-format-date
3414 (org-time-from-absolute date))
3415 (funcall org-agenda-format-date date))
3416 "\n")
3417 (put-text-property s (1- (point)) 'face
3418 (org-agenda-get-day-face date))
3419 (put-text-property s (1- (point)) 'org-date-line t)
3420 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3421 (if (equal d today)
3422 (put-text-property s (1- (point)) 'org-today t))
3423 (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
3424 (put-text-property s (1- (point)) 'day d)))))
3425 (goto-char (point-min))
3426 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
3427 (point-min)))
3428 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
3429 (org-finalize-agenda)
3430 (setq buffer-read-only t)))
3432 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
3433 "Return a list of all relevant day numbers from BEG to END buffer positions.
3434 If NO-RANGES is non-nil, include only the start and end dates of a range,
3435 not every single day in the range. If FORCE-TODAY is non-nil, make
3436 sure that TODAY is included in the list. If INACTIVE is non-nil, also
3437 inactive time stamps (those in square brackets) are included.
3438 When EMPTY is non-nil, also include days without any entries."
3439 (let ((re (concat
3440 (if pre-re pre-re "")
3441 (if inactive org-ts-regexp-both org-ts-regexp)))
3442 dates dates1 date day day1 day2 ts1 ts2 pos)
3443 (if force-today
3444 (setq dates (list (org-today))))
3445 (save-excursion
3446 (goto-char beg)
3447 (while (re-search-forward re end t)
3448 (setq day (time-to-days (org-time-string-to-time
3449 (substring (match-string 1) 0 10)
3450 (current-buffer) (match-beginning 0))))
3451 (or (memq day dates) (push day dates)))
3452 (unless no-ranges
3453 (goto-char beg)
3454 (while (re-search-forward org-tr-regexp end t)
3455 (setq pos (match-beginning 0))
3456 (setq ts1 (substring (match-string 1) 0 10)
3457 ts2 (substring (match-string 2) 0 10)
3458 day1 (time-to-days (org-time-string-to-time
3459 ts1 (current-buffer) pos))
3460 day2 (time-to-days (org-time-string-to-time
3461 ts2 (current-buffer) pos)))
3462 (while (< (setq day1 (1+ day1)) day2)
3463 (or (memq day1 dates) (push day1 dates)))))
3464 (setq dates (sort dates '<))
3465 (when empty
3466 (while (setq day (pop dates))
3467 (setq day2 (car dates))
3468 (push day dates1)
3469 (when (and day2 empty)
3470 (if (or (eq empty t)
3471 (and (numberp empty) (<= (- day2 day) empty)))
3472 (while (< (setq day (1+ day)) day2)
3473 (push (list day) dates1))
3474 (push (cons :omitted (- day2 day)) dates1))))
3475 (setq dates (nreverse dates1)))
3476 dates)))
3478 ;;; Agenda Daily/Weekly
3480 (defvar org-agenda-start-day nil ; dynamically scoped parameter
3481 "Start day for the agenda view.
3482 Custom commands can set this variable in the options section.")
3483 (defvar org-starting-day nil) ; local variable in the agenda buffer
3484 (defvar org-agenda-current-span nil
3485 "The current span used in the agenda view.") ; local variable in the agenda buffer
3486 (defvar org-arg-loc nil) ; local variable
3488 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
3489 "List of types searched for when creating the daily/weekly agenda.
3490 This variable is a list of symbols that controls the types of
3491 items that appear in the daily/weekly agenda. Allowed symbols in this
3492 list are are
3494 :timestamp List items containing a date stamp or date range matching
3495 the selected date. This includes sexp entries in
3496 angular brackets.
3498 :sexp List entries resulting from plain diary-like sexps.
3500 :deadline List deadline due on that date. When the date is today,
3501 also list any deadlines past due, or due within
3502 `org-deadline-warning-days'. `:deadline' must appear before
3503 `:scheduled' if the setting of
3504 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
3505 any effect.
3507 :scheduled List all items which are scheduled for the given date.
3508 The diary for *today* also contains items which were
3509 scheduled earlier and are not yet marked DONE.
3511 By default, all four types are turned on.
3513 Never set this variable globally using `setq', because then it
3514 will apply to all future agenda commands. Instead, bind it with
3515 `let' to scope it dynamically into the agenda-constructing
3516 command. A good way to set it is through options in
3517 `org-agenda-custom-commands'. For a more flexible (though
3518 somewhat less efficient) way of determining what is included in
3519 the daily/weekly agenda, see `org-agenda-skip-function'.")
3521 ;;;###autoload
3522 (defun org-agenda-list (&optional arg start-day span)
3523 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
3524 The view will be for the current day or week, but from the overview buffer
3525 you will be able to go to other days/weeks.
3527 With a numeric prefix argument in an interactive call, the agenda will
3528 span ARG days. Lisp programs should instead specify SPAN to change
3529 the number of days. SPAN defaults to `org-agenda-span'.
3531 START-DAY defaults to TODAY, or to the most recent match for the weekday
3532 given in `org-agenda-start-on-weekday'."
3533 (interactive "P")
3534 (if (and (integerp arg) (> arg 0))
3535 (setq span arg arg nil))
3536 (setq start-day (or start-day org-agenda-start-day))
3537 (if org-agenda-overriding-arguments
3538 (setq arg (car org-agenda-overriding-arguments)
3539 start-day (nth 1 org-agenda-overriding-arguments)
3540 span (nth 2 org-agenda-overriding-arguments)))
3541 (if (stringp start-day)
3542 ;; Convert to an absolute day number
3543 (setq start-day (time-to-days (org-read-date nil t start-day))))
3544 (setq org-agenda-last-arguments (list arg start-day span))
3545 (org-compile-prefix-format 'agenda)
3546 (org-set-sorting-strategy 'agenda)
3547 (let* ((span (org-agenda-ndays-to-span
3548 (or span org-agenda-ndays org-agenda-span)))
3549 (today (org-today))
3550 (sd (or start-day today))
3551 (ndays (org-agenda-span-to-ndays span sd))
3552 (org-agenda-start-on-weekday
3553 (if (eq ndays 7)
3554 org-agenda-start-on-weekday))
3555 (thefiles (org-agenda-files nil 'ifmode))
3556 (files thefiles)
3557 (start (if (or (null org-agenda-start-on-weekday)
3558 (< ndays 7))
3560 (let* ((nt (calendar-day-of-week
3561 (calendar-gregorian-from-absolute sd)))
3562 (n1 org-agenda-start-on-weekday)
3563 (d (- nt n1)))
3564 (- sd (+ (if (< d 0) 7 0) d)))))
3565 (day-numbers (list start))
3566 (day-cnt 0)
3567 (inhibit-redisplay (not debug-on-error))
3568 s e rtn rtnall file date d start-pos end-pos todayp
3569 clocktable-start clocktable-end filter)
3570 (setq org-agenda-redo-command
3571 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span)))
3572 (dotimes (n (1- ndays))
3573 (push (1+ (car day-numbers)) day-numbers))
3574 (setq day-numbers (nreverse day-numbers))
3575 (setq clocktable-start (car day-numbers)
3576 clocktable-end (1+ (or (org-last day-numbers) 0)))
3577 (org-prepare-agenda "Day/Week")
3578 (org-set-local 'org-starting-day (car day-numbers))
3579 (org-set-local 'org-arg-loc arg)
3580 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
3581 (unless org-agenda-compact-blocks
3582 (let* ((d1 (car day-numbers))
3583 (d2 (org-last day-numbers))
3584 (w1 (org-days-to-iso-week d1))
3585 (w2 (org-days-to-iso-week d2)))
3586 (setq s (point))
3587 (if org-agenda-overriding-header
3588 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3589 nil 'face 'org-agenda-structure) "\n")
3590 (insert (org-agenda-span-name span)
3591 "-agenda"
3592 (if (< (- d2 d1) 350)
3593 (if (= w1 w2)
3594 (format " (W%02d)" w1)
3595 (format " (W%02d-W%02d)" w1 w2))
3597 ":\n")))
3598 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
3599 'org-date-line t))
3600 (org-agenda-mark-header-line s))
3601 (while (setq d (pop day-numbers))
3602 (setq date (calendar-gregorian-from-absolute d)
3603 s (point))
3604 (if (or (setq todayp (= d today))
3605 (and (not start-pos) (= d sd)))
3606 (setq start-pos (point))
3607 (if (and start-pos (not end-pos))
3608 (setq end-pos (point))))
3609 (setq files thefiles
3610 rtnall nil)
3611 (while (setq file (pop files))
3612 (catch 'nextfile
3613 (org-check-agenda-file file)
3614 (let ((org-agenda-entry-types org-agenda-entry-types))
3615 (unless org-agenda-include-deadlines
3616 (setq org-agenda-entry-types
3617 (delq :deadline org-agenda-entry-types)))
3618 (cond
3619 ((memq org-agenda-show-log '(only clockcheck))
3620 (setq rtn (org-agenda-get-day-entries
3621 file date :closed)))
3622 (org-agenda-show-log
3623 (setq rtn (apply 'org-agenda-get-day-entries
3624 file date
3625 (append '(:closed) org-agenda-entry-types))))
3627 (setq rtn (apply 'org-agenda-get-day-entries
3628 file date
3629 org-agenda-entry-types)))))
3630 (setq rtnall (append rtnall rtn)))) ;; all entries
3631 (if org-agenda-include-diary
3632 (let ((org-agenda-search-headline-for-time t))
3633 (require 'diary-lib)
3634 (setq rtn (org-get-entries-from-diary date))
3635 (setq rtnall (append rtnall rtn))))
3636 (if (or rtnall org-agenda-show-all-dates)
3637 (progn
3638 (setq day-cnt (1+ day-cnt))
3639 (insert
3640 (if (stringp org-agenda-format-date)
3641 (format-time-string org-agenda-format-date
3642 (org-time-from-absolute date))
3643 (funcall org-agenda-format-date date))
3644 "\n")
3645 (put-text-property s (1- (point)) 'face
3646 (org-agenda-get-day-face date))
3647 (put-text-property s (1- (point)) 'org-date-line t)
3648 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3649 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
3650 (when todayp
3651 (put-text-property s (1- (point)) 'org-today t))
3652 (if rtnall (insert ;; all entries
3653 (org-finalize-agenda-entries
3654 (org-agenda-add-time-grid-maybe
3655 rtnall ndays todayp))
3656 "\n"))
3657 (put-text-property s (1- (point)) 'day d)
3658 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
3659 (when (and org-agenda-clockreport-mode clocktable-start)
3660 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
3661 ;; the above line is to ensure the restricted range!
3662 (p (copy-sequence org-agenda-clockreport-parameter-plist))
3663 tbl)
3664 (setq p (org-plist-delete p :block))
3665 (setq p (plist-put p :tstart clocktable-start))
3666 (setq p (plist-put p :tend clocktable-end))
3667 (setq p (plist-put p :scope 'agenda))
3668 (when (and (eq org-agenda-clockreport-mode 'with-filter)
3669 (setq filter (or org-agenda-filter-while-redo
3670 (get 'org-agenda-filter :preset-filter))))
3671 (setq p (plist-put p :tags (mapconcat (lambda (x)
3672 (if (string-match "[<>=]" x)
3675 filter ""))))
3676 (setq tbl (apply 'org-get-clocktable p))
3677 (insert tbl)))
3678 (goto-char (point-min))
3679 (or org-agenda-multi (org-fit-agenda-window))
3680 (unless (and (pos-visible-in-window-p (point-min))
3681 (pos-visible-in-window-p (point-max)))
3682 (goto-char (1- (point-max)))
3683 (recenter -1)
3684 (if (not (pos-visible-in-window-p (or start-pos 1)))
3685 (progn
3686 (goto-char (or start-pos 1))
3687 (recenter 1))))
3688 (goto-char (or start-pos 1))
3689 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
3690 (if (eq org-agenda-show-log 'clockcheck)
3691 (org-agenda-show-clocking-issues))
3692 (org-finalize-agenda)
3693 (setq buffer-read-only t)
3694 (message "")))
3696 (defun org-agenda-ndays-to-span (n)
3697 "Return a span symbol for a span of N days, or N if none matches."
3698 (cond ((symbolp n) n)
3699 ((= n 1) 'day)
3700 ((= n 7) 'week)
3701 (t n)))
3703 (defun org-agenda-span-to-ndays (span start-day)
3704 "Return ndays from SPAN starting at START-DAY."
3705 (cond ((numberp span) span)
3706 ((eq span 'day) 1)
3707 ((eq span 'week) 7)
3708 ((eq span 'month)
3709 (let ((date (calendar-gregorian-from-absolute start-day)))
3710 (calendar-last-day-of-month (car date) (caddr date))))
3711 ((eq span 'year)
3712 (let ((date (calendar-gregorian-from-absolute start-day)))
3713 (if (calendar-leap-year-p (caddr date)) 366 365)))))
3715 (defun org-agenda-span-name (span)
3716 "Return a SPAN name."
3717 (if (null span)
3719 (if (symbolp span)
3720 (capitalize (symbol-name span))
3721 (format "%d days" span))))
3723 ;;; Agenda word search
3725 (defvar org-agenda-search-history nil)
3726 (defvar org-todo-only nil)
3728 (defvar org-search-syntax-table nil
3729 "Special syntax table for org-mode search.
3730 In this table, we have single quotes not as word constituents, to
3731 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
3733 (defun org-search-syntax-table ()
3734 (unless org-search-syntax-table
3735 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
3736 (modify-syntax-entry ?' "." org-search-syntax-table)
3737 (modify-syntax-entry ?` "." org-search-syntax-table))
3738 org-search-syntax-table)
3740 (defvar org-agenda-last-search-view-search-was-boolean nil)
3742 ;;;###autoload
3743 (defun org-search-view (&optional todo-only string edit-at)
3744 "Show all entries that contain a phrase or words or regular expressions.
3746 With optional prefix argument TODO-ONLY, only consider entries that are
3747 TODO entries. The argument STRING can be used to pass a default search
3748 string into this function. If EDIT-AT is non-nil, it means that the
3749 user should get a chance to edit this string, with cursor at position
3750 EDIT-AT.
3752 The search string can be viewed either as a phrase that should be found as
3753 is, or it can be broken into a number of snippets, each of which must match
3754 in a Boolean way to select an entry. The default depends on the variable
3755 `org-agenda-search-view-always-boolean'.
3756 Even if this is turned off (the default) you can always switch to
3757 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
3759 The default is a direct search of the whole phrase, where each space in
3760 the search string can expand to an arbitrary amount of whitespace,
3761 including newlines.
3763 If using a Boolean search, the search string is split on whitespace and
3764 each snippet is searched separately, with logical AND to select an entry.
3765 Words prefixed with a minus must *not* occur in the entry. Words without
3766 a prefix or prefixed with a plus must occur in the entry. Matching is
3767 case-insensitive. Words are enclosed by word delimiters (i.e. they must
3768 match whole words, not parts of a word) if
3769 `org-agenda-search-view-force-full-words' is set (default is nil).
3771 Boolean search snippets enclosed by curly braces are interpreted as
3772 regular expressions that must or (when preceded with \"-\") must not
3773 match in the entry. Snippets enclosed into double quotes will be taken
3774 as a whole, to include whitespace.
3776 - If the search string starts with an asterisk, search only in headlines.
3777 - If (possibly after the leading star) the search string starts with an
3778 exclamation mark, this also means to look at TODO entries only, an effect
3779 that can also be achieved with a prefix argument.
3780 - If (possibly after star and exclamation mark) the search string starts
3781 with a colon, this will mean that the (non-regexp) snippets of the
3782 Boolean search must match as full words.
3784 This command searches the agenda files, and in addition the files listed
3785 in `org-agenda-text-search-extra-files'."
3786 (interactive "P")
3787 (org-compile-prefix-format 'search)
3788 (org-set-sorting-strategy 'search)
3789 (org-prepare-agenda "SEARCH")
3790 (let* ((props (list 'face nil
3791 'done-face 'org-agenda-done
3792 'org-not-done-regexp org-not-done-regexp
3793 'org-todo-regexp org-todo-regexp
3794 'org-complex-heading-regexp org-complex-heading-regexp
3795 'mouse-face 'highlight
3796 'help-echo (format "mouse-2 or RET jump to location")))
3797 (full-words org-agenda-search-view-force-full-words)
3798 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
3799 regexp rtn rtnall files file pos
3800 marker category org-category-pos tags c neg re boolean
3801 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
3802 (unless (and (not edit-at)
3803 (stringp string)
3804 (string-match "\\S-" string))
3805 (setq string (read-string
3806 (if org-agenda-search-view-always-boolean
3807 "[+-]Word/{Regexp} ...: "
3808 "Phrase, or [+-]Word/{Regexp} ...: ")
3809 (cond
3810 ((integerp edit-at) (cons string edit-at))
3811 (edit-at string))
3812 'org-agenda-search-history)))
3813 (org-set-local 'org-todo-only todo-only)
3814 (setq org-agenda-redo-command
3815 (list 'org-search-view (if todo-only t nil) string
3816 '(if current-prefix-arg 1 nil)))
3817 (setq org-agenda-query-string string)
3819 (if (equal (string-to-char string) ?*)
3820 (setq hdl-only t
3821 words (substring string 1))
3822 (setq words string))
3823 (when (equal (string-to-char words) ?!)
3824 (setq todo-only t
3825 words (substring words 1)))
3826 (when (equal (string-to-char words) ?:)
3827 (setq full-words t
3828 words (substring words 1)))
3829 (if (or org-agenda-search-view-always-boolean
3830 (member (string-to-char words) '(?- ?+ ?\{)))
3831 (setq boolean t))
3832 (setq words (org-split-string words))
3833 (let (www w)
3834 (while (setq w (pop words))
3835 (while (and (string-match "\\\\\\'" w) words)
3836 (setq w (concat (substring w 0 -1) " " (pop words))))
3837 (push w www))
3838 (setq words (nreverse www) www nil)
3839 (while (setq w (pop words))
3840 (when (and (string-match "\\`[-+]?{" w)
3841 (not (string-match "}\\'" w)))
3842 (while (and words (not (string-match "}\\'" (car words))))
3843 (setq w (concat w " " (pop words))))
3844 (setq w (concat w " " (pop words))))
3845 (push w www))
3846 (setq words (nreverse www)))
3847 (setq org-agenda-last-search-view-search-was-boolean boolean)
3848 (when boolean
3849 (let (wds w)
3850 (while (setq w (pop words))
3851 (if (or (equal (substring w 0 1) "\"")
3852 (and (> (length w) 1)
3853 (member (substring w 0 1) '("+" "-"))
3854 (equal (substring w 1 2) "\"")))
3855 (while (and words (not (equal (substring w -1) "\"")))
3856 (setq w (concat w " " (pop words)))))
3857 (and (string-match "\\`\\([-+]?\\)\"" w)
3858 (setq w (replace-match "\\1" nil nil w)))
3859 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
3860 (push w wds))
3861 (setq words (nreverse wds))))
3862 (if boolean
3863 (mapc (lambda (w)
3864 (setq c (string-to-char w))
3865 (if (equal c ?-)
3866 (setq neg t w (substring w 1))
3867 (if (equal c ?+)
3868 (setq neg nil w (substring w 1))
3869 (setq neg nil)))
3870 (if (string-match "\\`{.*}\\'" w)
3871 (setq re (substring w 1 -1))
3872 (if full-words
3873 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
3874 (setq re (regexp-quote (downcase w)))))
3875 (if neg (push re regexps-) (push re regexps+)))
3876 words)
3877 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
3878 regexps+))
3879 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
3880 (if (not regexps+)
3881 (setq regexp org-outline-regexp-bol)
3882 (setq regexp (pop regexps+))
3883 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
3884 regexp))))
3885 (setq files (org-agenda-files nil 'ifmode))
3886 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
3887 (pop org-agenda-text-search-extra-files)
3888 (setq files (org-add-archive-files files)))
3889 (setq files (append files org-agenda-text-search-extra-files)
3890 rtnall nil)
3891 (while (setq file (pop files))
3892 (setq ee nil)
3893 (catch 'nextfile
3894 (org-check-agenda-file file)
3895 (setq buffer (if (file-exists-p file)
3896 (org-get-agenda-file-buffer file)
3897 (error "No such file %s" file)))
3898 (if (not buffer)
3899 ;; If file does not exist, make sure an error message is sent
3900 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
3901 file))))
3902 (with-current-buffer buffer
3903 (with-syntax-table (org-search-syntax-table)
3904 (unless (eq major-mode 'org-mode)
3905 (error "Agenda file %s is not in `org-mode'" file))
3906 (let ((case-fold-search t))
3907 (save-excursion
3908 (save-restriction
3909 (if org-agenda-restrict
3910 (narrow-to-region org-agenda-restrict-begin
3911 org-agenda-restrict-end)
3912 (widen))
3913 (goto-char (point-min))
3914 (unless (or (org-on-heading-p)
3915 (outline-next-heading))
3916 (throw 'nextfile t))
3917 (goto-char (max (point-min) (1- (point))))
3918 (while (re-search-forward regexp nil t)
3919 (org-back-to-heading t)
3920 (skip-chars-forward "* ")
3921 (setq beg (point-at-bol)
3922 beg1 (point)
3923 end (progn (outline-next-heading) (point)))
3924 (catch :skip
3925 (goto-char beg)
3926 (org-agenda-skip)
3927 (setq str (buffer-substring-no-properties
3928 (point-at-bol)
3929 (if hdl-only (point-at-eol) end)))
3930 (mapc (lambda (wr) (when (string-match wr str)
3931 (goto-char (1- end))
3932 (throw :skip t)))
3933 regexps-)
3934 (mapc (lambda (wr) (unless (string-match wr str)
3935 (goto-char (1- end))
3936 (throw :skip t)))
3937 (if todo-only
3938 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
3939 regexps+)
3940 regexps+))
3941 (goto-char beg)
3942 (setq marker (org-agenda-new-marker (point))
3943 category (org-get-category)
3944 org-category-pos (get-text-property (point) 'org-category-position)
3945 tags (org-get-tags-at (point))
3946 txt (org-agenda-format-item
3948 (buffer-substring-no-properties
3949 beg1 (point-at-eol))
3950 category tags))
3951 (org-add-props txt props
3952 'org-marker marker 'org-hd-marker marker
3953 'org-todo-regexp org-todo-regexp
3954 'org-complex-heading-regexp org-complex-heading-regexp
3955 'priority 1000 'org-category category
3956 'org-category-position org-category-pos
3957 'type "search")
3958 (push txt ee)
3959 (goto-char (1- end))))))))))
3960 (setq rtn (nreverse ee))
3961 (setq rtnall (append rtnall rtn)))
3962 (if org-agenda-overriding-header
3963 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3964 nil 'face 'org-agenda-structure) "\n")
3965 (insert "Search words: ")
3966 (add-text-properties (point-min) (1- (point))
3967 (list 'face 'org-agenda-structure))
3968 (setq pos (point))
3969 (insert string "\n")
3970 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3971 (setq pos (point))
3972 (unless org-agenda-multi
3973 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
3974 (add-text-properties pos (1- (point))
3975 (list 'face 'org-agenda-structure))))
3976 (org-agenda-mark-header-line (point-min))
3977 (when rtnall
3978 (insert (org-finalize-agenda-entries rtnall) "\n"))
3979 (goto-char (point-min))
3980 (or org-agenda-multi (org-fit-agenda-window))
3981 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
3982 (org-finalize-agenda)
3983 (setq buffer-read-only t)))
3985 ;;; Agenda TODO list
3987 (defvar org-select-this-todo-keyword nil)
3988 (defvar org-last-arg nil)
3990 ;;;###autoload
3991 (defun org-todo-list (arg)
3992 "Show all (not done) TODO entries from all agenda file in a single list.
3993 The prefix arg can be used to select a specific TODO keyword and limit
3994 the list to these. When using \\[universal-argument], you will be prompted
3995 for a keyword. A numeric prefix directly selects the Nth keyword in
3996 `org-todo-keywords-1'."
3997 (interactive "P")
3998 (org-compile-prefix-format 'todo)
3999 (org-set-sorting-strategy 'todo)
4000 (org-prepare-agenda "TODO")
4001 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4002 (let* ((today (org-today))
4003 (date (calendar-gregorian-from-absolute today))
4004 (kwds org-todo-keywords-for-agenda)
4005 (completion-ignore-case t)
4006 (org-select-this-todo-keyword
4007 (if (stringp arg) arg
4008 (and arg (integerp arg) (> arg 0)
4009 (nth (1- arg) kwds))))
4010 rtn rtnall files file pos)
4011 (when (equal arg '(4))
4012 (setq org-select-this-todo-keyword
4013 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4014 (mapcar 'list kwds) nil nil)))
4015 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4016 (org-set-local 'org-last-arg arg)
4017 (setq org-agenda-redo-command
4018 '(org-todo-list (or current-prefix-arg org-last-arg)))
4019 (setq files (org-agenda-files nil 'ifmode)
4020 rtnall nil)
4021 (while (setq file (pop files))
4022 (catch 'nextfile
4023 (org-check-agenda-file file)
4024 (setq rtn (org-agenda-get-day-entries file date :todo))
4025 (setq rtnall (append rtnall rtn))))
4026 (if org-agenda-overriding-header
4027 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4028 nil 'face 'org-agenda-structure) "\n")
4029 (insert "Global list of TODO items of type: ")
4030 (add-text-properties (point-min) (1- (point))
4031 (list 'face 'org-agenda-structure
4032 'short-heading
4033 (concat "ToDo: "
4034 (or org-select-this-todo-keyword "ALL"))))
4035 (org-agenda-mark-header-line (point-min))
4036 (setq pos (point))
4037 (insert (or org-select-this-todo-keyword "ALL") "\n")
4038 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4039 (setq pos (point))
4040 (unless org-agenda-multi
4041 (insert "Available with `N r': (0)ALL")
4042 (let ((n 0) s)
4043 (mapc (lambda (x)
4044 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4045 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4046 (insert "\n "))
4047 (insert " " s))
4048 kwds))
4049 (insert "\n"))
4050 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4051 (org-agenda-mark-header-line (point-min))
4052 (when rtnall
4053 (insert (org-finalize-agenda-entries rtnall) "\n"))
4054 (goto-char (point-min))
4055 (or org-agenda-multi (org-fit-agenda-window))
4056 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
4057 (org-finalize-agenda)
4058 (setq buffer-read-only t)))
4060 ;;; Agenda tags match
4062 ;;;###autoload
4063 (defun org-tags-view (&optional todo-only match)
4064 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4065 The prefix arg TODO-ONLY limits the search to TODO entries."
4066 (interactive "P")
4067 (org-compile-prefix-format 'tags)
4068 (org-set-sorting-strategy 'tags)
4069 (let* ((org-tags-match-list-sublevels
4070 org-tags-match-list-sublevels)
4071 (completion-ignore-case t)
4072 rtn rtnall files file pos matcher
4073 buffer)
4074 (when (and (stringp match) (not (string-match "\\S-" match)))
4075 (setq match nil))
4076 (setq matcher (org-make-tags-matcher match)
4077 match (car matcher) matcher (cdr matcher))
4078 (org-prepare-agenda (concat "TAGS " match))
4079 (setq org-agenda-query-string match)
4080 (setq org-agenda-redo-command
4081 (list 'org-tags-view (list 'quote todo-only)
4082 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
4083 (setq files (org-agenda-files nil 'ifmode)
4084 rtnall nil)
4085 (while (setq file (pop files))
4086 (catch 'nextfile
4087 (org-check-agenda-file file)
4088 (setq buffer (if (file-exists-p file)
4089 (org-get-agenda-file-buffer file)
4090 (error "No such file %s" file)))
4091 (if (not buffer)
4092 ;; If file does not exist, error message to agenda
4093 (setq rtn (list
4094 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4095 rtnall (append rtnall rtn))
4096 (with-current-buffer buffer
4097 (unless (eq major-mode 'org-mode)
4098 (error "Agenda file %s is not in `org-mode'" file))
4099 (save-excursion
4100 (save-restriction
4101 (if org-agenda-restrict
4102 (narrow-to-region org-agenda-restrict-begin
4103 org-agenda-restrict-end)
4104 (widen))
4105 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4106 (setq rtnall (append rtnall rtn))))))))
4107 (if org-agenda-overriding-header
4108 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4109 nil 'face 'org-agenda-structure) "\n")
4110 (insert "Headlines with TAGS match: ")
4111 (add-text-properties (point-min) (1- (point))
4112 (list 'face 'org-agenda-structure
4113 'short-heading
4114 (concat "Match: " match)))
4115 (setq pos (point))
4116 (insert match "\n")
4117 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4118 (setq pos (point))
4119 (unless org-agenda-multi
4120 (insert "Press `C-u r' to search again with new search string\n"))
4121 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4122 (org-agenda-mark-header-line (point-min))
4123 (when rtnall
4124 (insert (org-finalize-agenda-entries rtnall) "\n"))
4125 (goto-char (point-min))
4126 (or org-agenda-multi (org-fit-agenda-window))
4127 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
4128 (org-finalize-agenda)
4129 (setq buffer-read-only t)))
4131 ;;; Agenda Finding stuck projects
4133 (defvar org-agenda-skip-regexp nil
4134 "Regular expression used in skipping subtrees for the agenda.
4135 This is basically a temporary global variable that can be set and then
4136 used by user-defined selections using `org-agenda-skip-function'.")
4138 (defvar org-agenda-overriding-header nil
4139 "When set during agenda, todo and tags searches it replaces the header.
4140 This variable should not be set directly, but custom commands can bind it
4141 in the options section.")
4143 (defun org-agenda-skip-entry-when-regexp-matches ()
4144 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4145 If yes, it returns the end position of this entry, causing agenda commands
4146 to skip the entry but continuing the search in the subtree. This is a
4147 function that can be put into `org-agenda-skip-function' for the duration
4148 of a command."
4149 (let ((end (save-excursion (org-end-of-subtree t)))
4150 skip)
4151 (save-excursion
4152 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4153 (and skip end)))
4155 (defun org-agenda-skip-subtree-when-regexp-matches ()
4156 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4157 If yes, it returns the end position of this tree, causing agenda commands
4158 to skip this subtree. This is a function that can be put into
4159 `org-agenda-skip-function' for the duration of a command."
4160 (let ((end (save-excursion (org-end-of-subtree t)))
4161 skip)
4162 (save-excursion
4163 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4164 (and skip end)))
4166 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4167 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4168 If yes, it returns the end position of the current entry (NOT the tree),
4169 causing agenda commands to skip the entry but continuing the search in
4170 the subtree. This is a function that can be put into
4171 `org-agenda-skip-function' for the duration of a command. An important
4172 use of this function is for the stuck project list."
4173 (let ((end (save-excursion (org-end-of-subtree t)))
4174 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4175 skip)
4176 (save-excursion
4177 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4178 (and skip entry-end)))
4180 (defun org-agenda-skip-entry-if (&rest conditions)
4181 "Skip entry if any of CONDITIONS is true.
4182 See `org-agenda-skip-if' for details."
4183 (org-agenda-skip-if nil conditions))
4185 (defun org-agenda-skip-subtree-if (&rest conditions)
4186 "Skip entry if any of CONDITIONS is true.
4187 See `org-agenda-skip-if' for details."
4188 (org-agenda-skip-if t conditions))
4190 (defun org-agenda-skip-if (subtree conditions)
4191 "Checks current entity for CONDITIONS.
4192 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4193 the entry, i.e. the text before the next heading is checked.
4195 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4196 from different tests. Valid conditions are:
4198 scheduled Check if there is a scheduled cookie
4199 notscheduled Check if there is no scheduled cookie
4200 deadline Check if there is a deadline
4201 notdeadline Check if there is no deadline
4202 timestamp Check if there is a timestamp (also deadline or scheduled)
4203 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4204 regexp Check if regexp matches
4205 notregexp Check if regexp does not match.
4206 todo Check if TODO keyword matches
4207 nottodo Check if TODO keyword does not match
4209 The regexp is taken from the conditions list, it must come right after
4210 the `regexp' or `notregexp' element.
4212 `todo' and `nottodo' accept as an argument a list of todo
4213 keywords, which may include \"*\" to match any todo keyword.
4215 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4217 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4219 Instead of a list a keyword class may be given
4221 (org-agenda-skip-entry-if 'nottodo 'done)
4223 would skip entries that haven't been marked with any of \"DONE\"
4224 keywords. Possible classes are: `todo', `done', `any'.
4226 If any of these conditions is met, this function returns the end point of
4227 the entity, causing the search to continue from there. This is a function
4228 that can be put into `org-agenda-skip-function' for the duration of a command."
4229 (let (beg end m)
4230 (org-back-to-heading t)
4231 (setq beg (point)
4232 end (if subtree
4233 (progn (org-end-of-subtree t) (point))
4234 (progn (outline-next-heading) (1- (point)))))
4235 (goto-char beg)
4236 (and
4238 (and (memq 'scheduled conditions)
4239 (re-search-forward org-scheduled-time-regexp end t))
4240 (and (memq 'notscheduled conditions)
4241 (not (re-search-forward org-scheduled-time-regexp end t)))
4242 (and (memq 'deadline conditions)
4243 (re-search-forward org-deadline-time-regexp end t))
4244 (and (memq 'notdeadline conditions)
4245 (not (re-search-forward org-deadline-time-regexp end t)))
4246 (and (memq 'timestamp conditions)
4247 (re-search-forward org-ts-regexp end t))
4248 (and (memq 'nottimestamp conditions)
4249 (not (re-search-forward org-ts-regexp end t)))
4250 (and (setq m (memq 'regexp conditions))
4251 (stringp (nth 1 m))
4252 (re-search-forward (nth 1 m) end t))
4253 (and (setq m (memq 'notregexp conditions))
4254 (stringp (nth 1 m))
4255 (not (re-search-forward (nth 1 m) end t)))
4256 (and (or
4257 (setq m (memq 'todo conditions))
4258 (setq m (memq 'nottodo conditions)))
4259 (org-agenda-skip-if-todo m end)))
4260 end)))
4262 (defun org-agenda-skip-if-todo (args end)
4263 "Helper function for `org-agenda-skip-if', do not use it directly.
4264 ARGS is a list with first element either `todo' or `nottodo'.
4265 The remainder is either a list of TODO keywords, or a state symbol
4266 `todo' or `done' or `any'."
4267 (let ((kw (car args))
4268 (arg (cadr args))
4269 todo-wds todo-re)
4270 (setq todo-wds
4271 (org-uniquify
4272 (cond
4273 ((listp arg) ;; list of keywords
4274 (if (member "*" arg)
4275 (mapcar 'substring-no-properties org-todo-keywords-1)
4276 arg))
4277 ((symbolp arg) ;; keyword class name
4278 (cond
4279 ((eq arg 'todo)
4280 (org-delete-all org-done-keywords
4281 (mapcar 'substring-no-properties
4282 org-todo-keywords-1)))
4283 ((eq arg 'done) org-done-keywords)
4284 ((eq arg 'any)
4285 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4286 (setq todo-re
4287 (concat "^\\*+[ \t]+\\<\\("
4288 (mapconcat 'identity todo-wds "\\|")
4289 "\\)\\>"))
4290 (if (eq kw 'todo)
4291 (re-search-forward todo-re end t)
4292 (not (re-search-forward todo-re end t)))))
4294 ;;;###autoload
4295 (defun org-agenda-list-stuck-projects (&rest ignore)
4296 "Create agenda view for projects that are stuck.
4297 Stuck projects are project that have no next actions. For the definitions
4298 of what a project is and how to check if it stuck, customize the variable
4299 `org-stuck-projects'."
4300 (interactive)
4301 (let* ((org-agenda-skip-function
4302 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
4303 ;; We could have used org-agenda-skip-if here.
4304 (org-agenda-overriding-header
4305 (or org-agenda-overriding-header "List of stuck projects: "))
4306 (matcher (nth 0 org-stuck-projects))
4307 (todo (nth 1 org-stuck-projects))
4308 (todo-wds (if (member "*" todo)
4309 (progn
4310 (org-prepare-agenda-buffers (org-agenda-files
4311 nil 'ifmode))
4312 (org-delete-all
4313 org-done-keywords-for-agenda
4314 (copy-sequence org-todo-keywords-for-agenda)))
4315 todo))
4316 (todo-re (concat "^\\*+[ \t]+\\("
4317 (mapconcat 'identity todo-wds "\\|")
4318 "\\)\\>"))
4319 (tags (nth 2 org-stuck-projects))
4320 (tags-re (if (member "*" tags)
4321 (concat org-outline-regexp-bol
4322 (org-re ".*:[[:alnum:]_@#%]+:[ \t]*$"))
4323 (if tags
4324 (concat org-outline-regexp-bol
4325 ".*:\\("
4326 (mapconcat 'identity tags "\\|")
4327 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
4328 (gen-re (nth 3 org-stuck-projects))
4329 (re-list
4330 (delq nil
4331 (list
4332 (if todo todo-re)
4333 (if tags tags-re)
4334 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
4335 gen-re)))))
4336 (setq org-agenda-skip-regexp
4337 (if re-list
4338 (mapconcat 'identity re-list "\\|")
4339 (error "No information how to identify unstuck projects")))
4340 (org-tags-view nil matcher)
4341 (with-current-buffer org-agenda-buffer-name
4342 (setq org-agenda-redo-command
4343 '(org-agenda-list-stuck-projects
4344 (or current-prefix-arg org-last-arg))))))
4346 ;;; Diary integration
4348 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
4349 (defvar list-diary-entries-hook)
4350 (defvar diary-time-regexp)
4351 (defun org-get-entries-from-diary (date)
4352 "Get the (Emacs Calendar) diary entries for DATE."
4353 (require 'diary-lib)
4354 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
4355 (diary-display-hook '(fancy-diary-display))
4356 (diary-display-function 'fancy-diary-display)
4357 (pop-up-frames nil)
4358 (list-diary-entries-hook
4359 (cons 'org-diary-default-entry list-diary-entries-hook))
4360 (diary-file-name-prefix-function nil) ; turn this feature off
4361 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
4362 entries
4363 (org-disable-agenda-to-diary t))
4364 (save-excursion
4365 (save-window-excursion
4366 (funcall (if (fboundp 'diary-list-entries)
4367 'diary-list-entries 'list-diary-entries)
4368 date 1)))
4369 (if (not (get-buffer diary-fancy-buffer))
4370 (setq entries nil)
4371 (with-current-buffer diary-fancy-buffer
4372 (setq buffer-read-only nil)
4373 (if (zerop (buffer-size))
4374 ;; No entries
4375 (setq entries nil)
4376 ;; Omit the date and other unnecessary stuff
4377 (org-agenda-cleanup-fancy-diary)
4378 ;; Add prefix to each line and extend the text properties
4379 (if (zerop (buffer-size))
4380 (setq entries nil)
4381 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
4382 (setq entries
4383 (with-temp-buffer
4384 (insert entries) (goto-char (point-min))
4385 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
4386 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
4387 (replace-match (concat "; " (match-string 1)))))
4388 (buffer-string)))))
4389 (set-buffer-modified-p nil)
4390 (kill-buffer diary-fancy-buffer)))
4391 (when entries
4392 (setq entries (org-split-string entries "\n"))
4393 (setq entries
4394 (mapcar
4395 (lambda (x)
4396 (setq x (org-agenda-format-item "" x "Diary" nil 'time))
4397 ;; Extend the text properties to the beginning of the line
4398 (org-add-props x (text-properties-at (1- (length x)) x)
4399 'type "diary" 'date date 'face 'org-agenda-diary))
4400 entries)))))
4402 (defvar org-agenda-cleanup-fancy-diary-hook nil
4403 "Hook run when the fancy diary buffer is cleaned up.")
4405 (defun org-agenda-cleanup-fancy-diary ()
4406 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
4407 This gets rid of the date, the underline under the date, and
4408 the dummy entry installed by `org-mode' to ensure non-empty diary for each
4409 date. It also removes lines that contain only whitespace."
4410 (goto-char (point-min))
4411 (if (looking-at ".*?:[ \t]*")
4412 (progn
4413 (replace-match "")
4414 (re-search-forward "\n=+$" nil t)
4415 (replace-match "")
4416 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
4417 (re-search-forward "\n=+$" nil t)
4418 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
4419 (goto-char (point-min))
4420 (while (re-search-forward "^ +\n" nil t)
4421 (replace-match ""))
4422 (goto-char (point-min))
4423 (if (re-search-forward "^Org-mode dummy\n?" nil t)
4424 (replace-match ""))
4425 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
4427 ;; Make sure entries from the diary have the right text properties.
4428 (eval-after-load "diary-lib"
4429 '(if (boundp 'diary-modify-entry-list-string-function)
4430 ;; We can rely on the hook, nothing to do
4432 ;; Hook not available, must use advice to make this work
4433 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
4434 "Make the position visible."
4435 (if (and org-disable-agenda-to-diary ;; called from org-agenda
4436 (stringp string)
4437 buffer-file-name)
4438 (setq string (org-modify-diary-entry-string string))))))
4440 (defun org-modify-diary-entry-string (string)
4441 "Add text properties to string, allowing org-mode to act on it."
4442 (org-add-props string nil
4443 'mouse-face 'highlight
4444 'help-echo (if buffer-file-name
4445 (format "mouse-2 or RET jump to diary file %s"
4446 (abbreviate-file-name buffer-file-name))
4448 'org-agenda-diary-link t
4449 'org-marker (org-agenda-new-marker (point-at-bol))))
4451 (defun org-diary-default-entry ()
4452 "Add a dummy entry to the diary.
4453 Needed to avoid empty dates which mess up holiday display."
4454 ;; Catch the error if dealing with the new add-to-diary-alist
4455 (when org-disable-agenda-to-diary
4456 (condition-case nil
4457 (org-add-to-diary-list original-date "Org-mode dummy" "")
4458 (error
4459 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
4461 (defun org-add-to-diary-list (&rest args)
4462 (if (fboundp 'diary-add-to-list)
4463 (apply 'diary-add-to-list args)
4464 (apply 'add-to-diary-list args)))
4466 (defvar org-diary-last-run-time nil)
4468 ;;;###autoload
4469 (defun org-diary (&rest args)
4470 "Return diary information from org-files.
4471 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
4472 It accesses org files and extracts information from those files to be
4473 listed in the diary. The function accepts arguments specifying what
4474 items should be listed. For a list of arguments allowed here, see the
4475 variable `org-agenda-entry-types'.
4477 The call in the diary file should look like this:
4479 &%%(org-diary) ~/path/to/some/orgfile.org
4481 Use a separate line for each org file to check. Or, if you omit the file name,
4482 all files listed in `org-agenda-files' will be checked automatically:
4484 &%%(org-diary)
4486 If you don't give any arguments (as in the example above), the default
4487 arguments (:deadline :scheduled :timestamp :sexp) are used.
4488 So the example above may also be written as
4490 &%%(org-diary :deadline :timestamp :sexp :scheduled)
4492 The function expects the lisp variables `entry' and `date' to be provided
4493 by the caller, because this is how the calendar works. Don't use this
4494 function from a program - use `org-agenda-get-day-entries' instead."
4495 (when (> (- (org-float-time)
4496 org-agenda-last-marker-time)
4498 (org-agenda-reset-markers))
4499 (org-compile-prefix-format 'agenda)
4500 (org-set-sorting-strategy 'agenda)
4501 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4502 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
4503 (list entry)
4504 (org-agenda-files t)))
4505 (time (org-float-time))
4506 file rtn results)
4507 (when (or (not org-diary-last-run-time)
4508 (> (- time
4509 org-diary-last-run-time)
4511 (org-prepare-agenda-buffers files))
4512 (setq org-diary-last-run-time time)
4513 ;; If this is called during org-agenda, don't return any entries to
4514 ;; the calendar. Org Agenda will list these entries itself.
4515 (if org-disable-agenda-to-diary (setq files nil))
4516 (while (setq file (pop files))
4517 (setq rtn (apply 'org-agenda-get-day-entries file date args))
4518 (setq results (append results rtn)))
4519 (if results
4520 (concat (org-finalize-agenda-entries results) "\n"))))
4522 ;;; Agenda entry finders
4524 (defun org-agenda-get-day-entries (file date &rest args)
4525 "Does the work for `org-diary' and `org-agenda'.
4526 FILE is the path to a file to be checked for entries. DATE is date like
4527 the one returned by `calendar-current-date'. ARGS are symbols indicating
4528 which kind of entries should be extracted. For details about these, see
4529 the documentation of `org-diary'."
4530 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4531 (let* ((org-startup-folded nil)
4532 (org-startup-align-all-tables nil)
4533 (buffer (if (file-exists-p file)
4534 (org-get-agenda-file-buffer file)
4535 (error "No such file %s" file)))
4536 arg results rtn deadline-results)
4537 (if (not buffer)
4538 ;; If file does not exist, make sure an error message ends up in diary
4539 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4540 (with-current-buffer buffer
4541 (unless (eq major-mode 'org-mode)
4542 (error "Agenda file %s is not in `org-mode'" file))
4543 (let ((case-fold-search nil))
4544 (save-excursion
4545 (save-restriction
4546 (if org-agenda-restrict
4547 (narrow-to-region org-agenda-restrict-begin
4548 org-agenda-restrict-end)
4549 (widen))
4550 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
4551 (while (setq arg (pop args))
4552 (cond
4553 ((and (eq arg :todo)
4554 (equal date (calendar-gregorian-from-absolute
4555 (org-today))))
4556 (setq rtn (org-agenda-get-todos))
4557 (setq results (append results rtn)))
4558 ((eq arg :timestamp)
4559 (setq rtn (org-agenda-get-blocks))
4560 (setq results (append results rtn))
4561 (setq rtn (org-agenda-get-timestamps))
4562 (setq results (append results rtn)))
4563 ((eq arg :sexp)
4564 (setq rtn (org-agenda-get-sexps))
4565 (setq results (append results rtn)))
4566 ((eq arg :scheduled)
4567 (setq rtn (org-agenda-get-scheduled deadline-results))
4568 (setq results (append results rtn)))
4569 ((eq arg :closed)
4570 (setq rtn (org-agenda-get-progress))
4571 (setq results (append results rtn)))
4572 ((eq arg :deadline)
4573 (setq rtn (org-agenda-get-deadlines))
4574 (setq deadline-results (copy-sequence rtn))
4575 (setq results (append results rtn))))))))
4576 results))))
4578 (defvar org-heading-keyword-regexp-format) ; defined in org.el
4579 (defun org-agenda-get-todos ()
4580 "Return the TODO information for agenda display."
4581 (let* ((props (list 'face nil
4582 'done-face 'org-agenda-done
4583 'org-not-done-regexp org-not-done-regexp
4584 'org-todo-regexp org-todo-regexp
4585 'org-complex-heading-regexp org-complex-heading-regexp
4586 'mouse-face 'highlight
4587 'help-echo
4588 (format "mouse-2 or RET jump to org file %s"
4589 (abbreviate-file-name buffer-file-name))))
4590 (regexp (format org-heading-keyword-regexp-format
4591 (cond
4592 ((and org-select-this-todo-keyword
4593 (equal org-select-this-todo-keyword "*"))
4594 org-todo-regexp)
4595 (org-select-this-todo-keyword
4596 (concat "\\("
4597 (mapconcat 'identity
4598 (org-split-string
4599 org-select-this-todo-keyword
4600 "|")
4601 "\\|") "\\)"))
4602 (t org-not-done-regexp))))
4603 marker priority category org-category-pos tags todo-state
4604 ee txt beg end)
4605 (goto-char (point-min))
4606 (while (re-search-forward regexp nil t)
4607 (catch :skip
4608 (save-match-data
4609 (beginning-of-line)
4610 (org-agenda-skip)
4611 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
4612 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
4613 (goto-char (1+ beg))
4614 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
4615 (throw :skip nil)))
4616 (goto-char (match-beginning 2))
4617 (setq marker (org-agenda-new-marker (match-beginning 0))
4618 category (org-get-category)
4619 org-category-pos (get-text-property (point) 'org-category-position)
4620 txt (org-trim
4621 (buffer-substring (match-beginning 2) (match-end 0)))
4622 tags (org-get-tags-at (point))
4623 txt (org-agenda-format-item "" txt category tags)
4624 priority (1+ (org-get-priority txt))
4625 todo-state (org-get-todo-state))
4626 (org-add-props txt props
4627 'org-marker marker 'org-hd-marker marker
4628 'priority priority 'org-category category
4629 'org-category-position org-category-pos
4630 'type "todo" 'todo-state todo-state)
4631 (push txt ee)
4632 (if org-agenda-todo-list-sublevels
4633 (goto-char (match-end 2))
4634 (org-end-of-subtree 'invisible))))
4635 (nreverse ee)))
4637 (defun org-agenda-todo-custom-ignore-p (time n)
4638 "Check whether timestamp is farther away then n number of days.
4639 This function is invoked if `org-agenda-todo-ignore-deadlines',
4640 `org-agenda-todo-ignore-scheduled' or
4641 `org-agenda-todo-ignore-timestamp' is set to an integer."
4642 (let ((days (org-days-to-time time)))
4643 (if (>= n 0)
4644 (>= days n)
4645 (<= days n))))
4647 ;;;###autoload
4648 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
4649 (&optional end)
4650 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
4651 (when (or org-agenda-todo-ignore-with-date
4652 org-agenda-todo-ignore-scheduled
4653 org-agenda-todo-ignore-deadlines
4654 org-agenda-todo-ignore-timestamp)
4655 (setq end (or end (save-excursion (outline-next-heading) (point))))
4656 (save-excursion
4657 (or (and org-agenda-todo-ignore-with-date
4658 (re-search-forward org-ts-regexp end t))
4659 (and org-agenda-todo-ignore-scheduled
4660 (re-search-forward org-scheduled-time-regexp end t)
4661 (cond
4662 ((eq org-agenda-todo-ignore-scheduled 'future)
4663 (> (org-days-to-time (match-string 1)) 0))
4664 ((eq org-agenda-todo-ignore-scheduled 'past)
4665 (<= (org-days-to-time (match-string 1)) 0))
4666 ((numberp org-agenda-todo-ignore-scheduled)
4667 (org-agenda-todo-custom-ignore-p
4668 (match-string 1) org-agenda-todo-ignore-scheduled))
4669 (t)))
4670 (and org-agenda-todo-ignore-deadlines
4671 (re-search-forward org-deadline-time-regexp end t)
4672 (cond
4673 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
4674 ((eq org-agenda-todo-ignore-deadlines 'far)
4675 (not (org-deadline-close (match-string 1))))
4676 ((eq org-agenda-todo-ignore-deadlines 'future)
4677 (> (org-days-to-time (match-string 1)) 0))
4678 ((eq org-agenda-todo-ignore-deadlines 'past)
4679 (<= (org-days-to-time (match-string 1)) 0))
4680 ((numberp org-agenda-todo-ignore-deadlines)
4681 (org-agenda-todo-custom-ignore-p
4682 (match-string 1) org-agenda-todo-ignore-deadlines))
4683 (t (org-deadline-close (match-string 1)))))
4684 (and org-agenda-todo-ignore-timestamp
4685 (let ((buffer (current-buffer))
4686 (regexp
4687 (concat
4688 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
4689 (start (point)))
4690 ;; Copy current buffer into a temporary one
4691 (with-temp-buffer
4692 (insert-buffer-substring buffer start end)
4693 (goto-char (point-min))
4694 ;; Delete SCHEDULED and DEADLINE items
4695 (while (re-search-forward regexp end t)
4696 (delete-region (match-beginning 0) (match-end 0)))
4697 (goto-char (point-min))
4698 ;; No search for timestamp left
4699 (when (re-search-forward org-ts-regexp nil t)
4700 (cond
4701 ((eq org-agenda-todo-ignore-timestamp 'future)
4702 (> (org-days-to-time (match-string 1)) 0))
4703 ((eq org-agenda-todo-ignore-timestamp 'past)
4704 (<= (org-days-to-time (match-string 1)) 0))
4705 ((numberp org-agenda-todo-ignore-timestamp)
4706 (org-agenda-todo-custom-ignore-p
4707 (match-string 1) org-agenda-todo-ignore-timestamp))
4708 (t))))))))))
4710 (defconst org-agenda-no-heading-message
4711 "No heading for this item in buffer or region.")
4713 (defun org-agenda-get-timestamps ()
4714 "Return the date stamp information for agenda display."
4715 (let* ((props (list 'face 'org-agenda-calendar-event
4716 'org-not-done-regexp org-not-done-regexp
4717 'org-todo-regexp org-todo-regexp
4718 'org-complex-heading-regexp org-complex-heading-regexp
4719 'mouse-face 'highlight
4720 'help-echo
4721 (format "mouse-2 or RET jump to org file %s"
4722 (abbreviate-file-name buffer-file-name))))
4723 (d1 (calendar-absolute-from-gregorian date))
4724 (remove-re
4725 (concat
4726 (regexp-quote
4727 (format-time-string
4728 "<%Y-%m-%d"
4729 (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
4730 ".*?>"))
4731 (regexp
4732 (concat
4733 (if org-agenda-include-inactive-timestamps "[[<]" "<")
4734 (regexp-quote
4735 (substring
4736 (format-time-string
4737 (car org-time-stamp-formats)
4738 (apply 'encode-time ; DATE bound by calendar
4739 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4740 1 11))
4741 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
4742 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
4743 marker hdmarker deadlinep scheduledp clockp closedp inactivep
4744 donep tmp priority category org-category-pos ee txt timestr tags
4745 b0 b3 e3 head todo-state end-of-match show-all)
4746 (goto-char (point-min))
4747 (while (setq end-of-match (re-search-forward regexp nil t))
4748 (setq b0 (match-beginning 0)
4749 b3 (match-beginning 3) e3 (match-end 3)
4750 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
4751 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
4752 (member todo-state
4753 org-agenda-repeating-timestamp-show-all)))
4754 (catch :skip
4755 (and (org-at-date-range-p) (throw :skip nil))
4756 (org-agenda-skip)
4757 (if (and (match-end 1)
4758 (not (= d1 (org-time-string-to-absolute
4759 (match-string 1) d1 nil show-all
4760 (current-buffer) b0))))
4761 (throw :skip nil))
4762 (if (and e3
4763 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
4764 (throw :skip nil))
4765 (setq tmp (buffer-substring (max (point-min)
4766 (- b0 org-ds-keyword-length))
4768 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
4769 inactivep (= (char-after b0) ?\[)
4770 deadlinep (string-match org-deadline-regexp tmp)
4771 scheduledp (string-match org-scheduled-regexp tmp)
4772 closedp (and org-agenda-include-inactive-timestamps
4773 (string-match org-closed-string tmp))
4774 clockp (and org-agenda-include-inactive-timestamps
4775 (or (string-match org-clock-string tmp)
4776 (string-match "]-+\\'" tmp)))
4777 donep (member todo-state org-done-keywords))
4778 (if (or scheduledp deadlinep closedp clockp
4779 (and donep org-agenda-skip-timestamp-if-done))
4780 (throw :skip t))
4781 (if (string-match ">" timestr)
4782 ;; substring should only run to end of time stamp
4783 (setq timestr (substring timestr 0 (match-end 0))))
4784 (setq marker (org-agenda-new-marker b0)
4785 category (org-get-category b0)
4786 org-category-pos (get-text-property b0 'org-category-position))
4787 (save-excursion
4788 (if (not (re-search-backward org-outline-regexp-bol nil t))
4789 (setq txt org-agenda-no-heading-message)
4790 (goto-char (match-beginning 0))
4791 (setq hdmarker (org-agenda-new-marker)
4792 tags (org-get-tags-at))
4793 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4794 (setq head (or (match-string 1) ""))
4795 (setq txt (org-agenda-format-item
4796 (if inactivep org-agenda-inactive-leader nil)
4797 head category tags timestr
4798 remove-re)))
4799 (setq priority (org-get-priority txt))
4800 (org-add-props txt props
4801 'org-marker marker 'org-hd-marker hdmarker)
4802 (org-add-props txt nil 'priority priority
4803 'org-category category 'date date
4804 'org-category-position org-category-pos
4805 'todo-state todo-state
4806 'type "timestamp")
4807 (push txt ee))
4808 (if org-agenda-skip-additional-timestamps-same-entry
4809 (outline-next-heading)
4810 (goto-char end-of-match))))
4811 (nreverse ee)))
4813 (defun org-agenda-get-sexps ()
4814 "Return the sexp information for agenda display."
4815 (require 'diary-lib)
4816 (let* ((props (list 'face 'org-agenda-calendar-sexp
4817 'mouse-face 'highlight
4818 'help-echo
4819 (format "mouse-2 or RET jump to org file %s"
4820 (abbreviate-file-name buffer-file-name))))
4821 (regexp "^&?%%(")
4822 marker category org-category-pos ee txt tags entry
4823 result beg b sexp sexp-entry todo-state)
4824 (goto-char (point-min))
4825 (while (re-search-forward regexp nil t)
4826 (catch :skip
4827 (org-agenda-skip)
4828 (setq beg (match-beginning 0))
4829 (goto-char (1- (match-end 0)))
4830 (setq b (point))
4831 (forward-sexp 1)
4832 (setq sexp (buffer-substring b (point)))
4833 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
4834 (org-trim (match-string 1))
4835 ""))
4836 (setq result (org-diary-sexp-entry sexp sexp-entry date))
4837 (when result
4838 (setq marker (org-agenda-new-marker beg)
4839 category (org-get-category beg)
4840 org-category-pos (get-text-property beg 'org-category-position)
4841 todo-state (org-get-todo-state))
4843 (dolist (r (if (stringp result)
4844 (list result)
4845 result)) ;; we expect a list here
4846 (if (string-match "\\S-" r)
4847 (setq txt r)
4848 (setq txt "SEXP entry returned empty string"))
4850 (setq txt (org-agenda-format-item
4851 "" txt category tags 'time))
4852 (org-add-props txt props 'org-marker marker)
4853 (org-add-props txt nil
4854 'org-category category 'date date 'todo-state todo-state
4855 'org-category-position org-category-pos
4856 'type "sexp")
4857 (push txt ee)))))
4858 (nreverse ee)))
4860 ;; Calendar sanity: define some functions that are independent of
4861 ;; `calendar-date-style'.
4862 ;; Normally I would like to use ISO format when calling the diary functions,
4863 ;; but to make sure we still have Emacs 22 compatibility we bind
4864 ;; also `european-calendar-style' and use european format
4865 (defun org-anniversary (year month day &optional mark)
4866 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
4867 (org-no-warnings
4868 (let ((calendar-date-style 'european) (european-calendar-style t))
4869 (diary-anniversary day month year mark))))
4870 (defun org-cyclic (N year month day &optional mark)
4871 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
4872 (org-no-warnings
4873 (let ((calendar-date-style 'european) (european-calendar-style t))
4874 (diary-cyclic N day month year mark))))
4875 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
4876 "Like `diary-block', but with fixed (ISO) order of arguments."
4877 (org-no-warnings
4878 (let ((calendar-date-style 'european) (european-calendar-style t))
4879 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
4880 (defun org-date (year month day &optional mark)
4881 "Like `diary-date', but with fixed (ISO) order of arguments."
4882 (org-no-warnings
4883 (let ((calendar-date-style 'european) (european-calendar-style t))
4884 (diary-date day month year mark))))
4885 (defalias 'org-float 'diary-float)
4887 ;; Define the` org-class' function
4888 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
4889 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
4890 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
4891 SKIP-WEEKS is any number of ISO weeks in the block period for which the
4892 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
4893 `holidays', then any date that is known by the Emacs calendar to be a
4894 holidy will also be skipped."
4895 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
4896 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
4897 (d (calendar-absolute-from-gregorian date)))
4898 (and
4899 (<= date1 d)
4900 (<= d date2)
4901 (= (calendar-day-of-week date) dayname)
4902 (or (not skip-weeks)
4903 (progn
4904 (require 'cal-iso)
4905 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
4906 (not (and (memq 'holidays skip-weeks)
4907 (calendar-check-holidays date)))
4908 entry)))
4910 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
4911 "Like `org-class', but honor `calendar-date-style'.
4912 The order of the first 2 times 3 arguments depends on the variable
4913 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
4914 So for American calendars, give this as MONTH DAY YEAR, for European as
4915 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
4916 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
4917 is any number of ISO weeks in the block period for which the item should
4918 be skipped.
4920 This function is here only for backward compatibility and it is deprecated,
4921 please use `org-class' instead."
4922 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
4923 (date2 (org-order-calendar-date-args m2 d2 y2)))
4924 (org-class
4925 (nth 2 date1) (car date1) (nth 1 date1)
4926 (nth 2 date2) (car date2) (nth 1 date2)
4927 dayname skip-weeks)))
4928 (make-obsolete 'org-diary-class 'org-class "")
4930 (defalias 'org-get-closed 'org-agenda-get-progress)
4931 (defun org-agenda-get-progress ()
4932 "Return the logged TODO entries for agenda display."
4933 (let* ((props (list 'mouse-face 'highlight
4934 'org-not-done-regexp org-not-done-regexp
4935 'org-todo-regexp org-todo-regexp
4936 'org-complex-heading-regexp org-complex-heading-regexp
4937 'help-echo
4938 (format "mouse-2 or RET jump to org file %s"
4939 (abbreviate-file-name buffer-file-name))))
4940 (items (if (consp org-agenda-show-log)
4941 org-agenda-show-log
4942 (if (eq org-agenda-show-log 'clockcheck)
4943 '(clock)
4944 org-agenda-log-mode-items)))
4945 (parts
4946 (delq nil
4947 (list
4948 (if (memq 'closed items) (concat "\\<" org-closed-string))
4949 (if (memq 'clock items) (concat "\\<" org-clock-string))
4950 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
4951 (parts-re (if parts (mapconcat 'identity parts "\\|")
4952 (error "`org-agenda-log-mode-items' is empty")))
4953 (regexp (concat
4954 "\\(" parts-re "\\)"
4955 " *\\["
4956 (regexp-quote
4957 (substring
4958 (format-time-string
4959 (car org-time-stamp-formats)
4960 (apply 'encode-time ; DATE bound by calendar
4961 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4962 1 11))))
4963 (org-agenda-search-headline-for-time nil)
4964 marker hdmarker priority category org-category-pos tags closedp
4965 statep clockp state ee txt extra timestr rest clocked)
4966 (goto-char (point-min))
4967 (while (re-search-forward regexp nil t)
4968 (catch :skip
4969 (org-agenda-skip)
4970 (setq marker (org-agenda-new-marker (match-beginning 0))
4971 closedp (equal (match-string 1) org-closed-string)
4972 statep (equal (string-to-char (match-string 1)) ?-)
4973 clockp (not (or closedp statep))
4974 state (and statep (match-string 2))
4975 category (org-get-category (match-beginning 0))
4976 org-category-pos (get-text-property (match-beginning 0) 'org-category-position)
4977 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
4978 (when (string-match "\\]" timestr)
4979 ;; substring should only run to end of time stamp
4980 (setq rest (substring timestr (match-end 0))
4981 timestr (substring timestr 0 (match-end 0)))
4982 (if (and (not closedp) (not statep)
4983 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
4984 rest))
4985 (progn (setq timestr (concat (substring timestr 0 -1)
4986 "-" (match-string 1 rest) "]"))
4987 (setq clocked (match-string 2 rest)))
4988 (setq clocked "-")))
4989 (save-excursion
4990 (setq extra
4991 (cond
4992 ((not org-agenda-log-mode-add-notes) nil)
4993 (statep
4994 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
4995 (match-string 1)))
4996 (clockp
4997 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
4998 (match-string 1)))))
4999 (if (not (re-search-backward org-outline-regexp-bol nil t))
5000 (setq txt org-agenda-no-heading-message)
5001 (goto-char (match-beginning 0))
5002 (setq hdmarker (org-agenda-new-marker)
5003 tags (org-get-tags-at))
5004 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5005 (setq txt (match-string 1))
5006 (when extra
5007 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5008 (setq txt (concat (substring txt 0 (match-beginning 1))
5009 " - " extra " " (match-string 2 txt)))
5010 (setq txt (concat txt " - " extra))))
5011 (setq txt (org-agenda-format-item
5012 (cond
5013 (closedp "Closed: ")
5014 (statep (concat "State: (" state ")"))
5015 (t (concat "Clocked: (" clocked ")")))
5016 txt category tags timestr)))
5017 (setq priority 100000)
5018 (org-add-props txt props
5019 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5020 'priority priority 'org-category category
5021 'org-category-position org-category-pos
5022 'type "closed" 'date date
5023 'undone-face 'org-warning 'done-face 'org-agenda-done)
5024 (push txt ee))
5025 (goto-char (point-at-eol))))
5026 (nreverse ee)))
5028 (defun org-agenda-show-clocking-issues ()
5029 "Add overlays, showing issues with clocking.
5030 See also the user option `org-agenda-clock-consistency-checks'."
5031 (interactive)
5032 (let* ((pl org-agenda-clock-consistency-checks)
5033 (re (concat "^[ \t]*"
5034 org-clock-string
5035 "[ \t]+"
5036 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5037 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5038 (tlstart 0.)
5039 (tlend 0.)
5040 (maxtime (org-hh:mm-string-to-minutes
5041 (or (plist-get pl :max-duration) "24:00")))
5042 (mintime (org-hh:mm-string-to-minutes
5043 (or (plist-get pl :min-duration) 0)))
5044 (maxgap (org-hh:mm-string-to-minutes
5045 ;; default 30:00 means never complain
5046 (or (plist-get pl :max-gap) "30:00")))
5047 (gapok (mapcar 'org-hh:mm-string-to-minutes
5048 (plist-get pl :gap-ok-around)))
5049 (def-face (or (plist-get pl :default-face)
5050 '((:background "DarkRed") (:foreground "white"))))
5051 issue face m te ts dt ov)
5052 (goto-char (point-min))
5053 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5054 (setq issue nil face def-face)
5055 (catch 'next
5056 (setq m (org-get-at-bol 'org-marker)
5057 te nil ts nil)
5058 (unless (and m (markerp m))
5059 (setq issue "No valid clock line") (throw 'next t))
5060 (org-with-point-at m
5061 (save-excursion
5062 (goto-char (point-at-bol))
5063 (unless (looking-at re)
5064 (error "No valid Clock line")
5065 (throw 'next t))
5066 (unless (match-end 3)
5067 (setq issue "No end time"
5068 face (or (plist-get pl :no-end-time-face) face))
5069 (throw 'next t))
5070 (setq ts (match-string 1)
5071 te (match-string 3)
5072 ts (org-float-time
5073 (apply 'encode-time (org-parse-time-string ts)))
5074 te (org-float-time
5075 (apply 'encode-time (org-parse-time-string te)))
5076 dt (- te ts))))
5077 (cond
5078 ((> dt (* 60 maxtime))
5079 ;; a very long clocking chunk
5080 (setq issue (format "Clocking interval is very long: %s"
5081 (org-minutes-to-hh:mm-string
5082 (floor (/ (float dt) 60.))))
5083 face (or (plist-get pl :long-face) face)))
5084 ((< dt (* 60 mintime))
5085 ;; a very short clocking chunk
5086 (setq issue (format "Clocking interval is very short: %s"
5087 (org-minutes-to-hh:mm-string
5088 (floor (/ (float dt) 60.))))
5089 face (or (plist-get pl :short-face) face)))
5090 ((and (> tlend 0) (< ts tlend))
5091 ;; Two clock entries are overlapping
5092 (setq issue (format "Clocking overlap: %d minutes"
5093 (/ (- tlend ts) 60))
5094 face (or (plist-get pl :overlap-face) face)))
5095 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5096 ;; There is a gap, lets see if we need to report it
5097 (unless (org-agenda-check-clock-gap tlend ts gapok)
5098 (setq issue (format "Clocking gap: %d minutes"
5099 (/ (- ts tlend) 60))
5100 face (or (plist-get pl :gap-face) face))))
5101 (t nil)))
5102 (setq tlend (or te tlend) tlstart (or ts tlstart))
5103 (when issue
5104 ;; OK, there was some issue, add an overlay to show the issue
5105 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5106 (overlay-put ov 'before-string
5107 (concat
5108 (org-add-props
5109 (format "%-43s" (concat " " issue))
5111 'face face)
5112 "\n"))
5113 (overlay-put ov 'evaporate t)))))
5115 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5116 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5117 (catch 'exit
5118 (unless ok-list
5119 ;; there are no OK times for gaps...
5120 (throw 'exit nil))
5121 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5122 ;; This is more than 24 hours, so it is OK.
5123 ;; because we have at least one OK time, that must be in the
5124 ;; 24 hour interval.
5125 (throw 'exit t))
5126 ;; We have a shorter gap.
5127 ;; Now we have to get the minute of the day when these times are
5128 (let* ((t1dec (decode-time (seconds-to-time t1)))
5129 (t2dec (decode-time (seconds-to-time t2)))
5130 ;; compute the minute on the day
5131 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5132 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5133 (when (< min2 min1)
5134 ;; if min2 is smaller than min1, this means it is on the next day.
5135 ;; Wrap it to after midnight.
5136 (setq min2 (+ min2 1440)))
5137 ;; Now check if any of the OK times is in the gap
5138 (mapc (lambda (x)
5139 ;; Wrap the time to after midnight if necessary
5140 (if (< x min1) (setq x (+ x 1440)))
5141 ;; Check if in interval
5142 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5143 ok-list)
5144 ;; Nope, this gap is not OK
5145 nil)))
5147 (defun org-agenda-get-deadlines ()
5148 "Return the deadline information for agenda display."
5149 (let* ((props (list 'mouse-face 'highlight
5150 'org-not-done-regexp org-not-done-regexp
5151 'org-todo-regexp org-todo-regexp
5152 'org-complex-heading-regexp org-complex-heading-regexp
5153 'help-echo
5154 (format "mouse-2 or RET jump to org file %s"
5155 (abbreviate-file-name buffer-file-name))))
5156 (regexp org-deadline-time-regexp)
5157 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5158 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5159 d2 diff dfrac wdays pos pos1 category org-category-pos
5160 tags suppress-prewarning ee txt head face s todo-state
5161 show-all upcomingp donep timestr)
5162 (goto-char (point-min))
5163 (while (re-search-forward regexp nil t)
5164 (setq suppress-prewarning nil)
5165 (catch :skip
5166 (org-agenda-skip)
5167 (when (and org-agenda-skip-deadline-prewarning-if-scheduled
5168 (save-match-data
5169 (string-match org-scheduled-time-regexp
5170 (buffer-substring (point-at-bol)
5171 (point-at-eol)))))
5172 (setq suppress-prewarning
5173 (if (integerp org-agenda-skip-deadline-prewarning-if-scheduled)
5174 org-agenda-skip-deadline-prewarning-if-scheduled
5175 0)))
5176 (setq s (match-string 1)
5177 txt nil
5178 pos (1- (match-beginning 1))
5179 todo-state (save-match-data (org-get-todo-state))
5180 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5181 (member todo-state
5182 org-agenda-repeating-timestamp-show-all))
5183 d2 (org-time-string-to-absolute
5184 (match-string 1) d1 'past show-all
5185 (current-buffer) pos)
5186 diff (- d2 d1)
5187 wdays (if suppress-prewarning
5188 (let ((org-deadline-warning-days suppress-prewarning))
5189 (org-get-wdays s))
5190 (org-get-wdays s))
5191 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
5192 upcomingp (and todayp (> diff 0)))
5193 ;; When to show a deadline in the calendar:
5194 ;; If the expiration is within wdays warning time.
5195 ;; Past-due deadlines are only shown on the current date
5196 (if (and (or (and (<= diff wdays)
5197 (and todayp (not org-agenda-only-exact-dates)))
5198 (= diff 0)))
5199 (save-excursion
5200 ;; (setq todo-state (org-get-todo-state))
5201 (setq donep (member todo-state org-done-keywords))
5202 (if (and donep
5203 (or org-agenda-skip-deadline-if-done
5204 (not (= diff 0))))
5205 (setq txt nil)
5206 (setq category (org-get-category)
5207 org-category-pos (get-text-property (point) 'org-category-position))
5208 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5209 (setq txt org-agenda-no-heading-message)
5210 (goto-char (match-end 0))
5211 (setq pos1 (match-beginning 0))
5212 (setq tags (org-get-tags-at pos1))
5213 (setq head (buffer-substring-no-properties
5214 (point)
5215 (progn (skip-chars-forward "^\r\n")
5216 (point))))
5217 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5218 (setq timestr
5219 (concat (substring s (match-beginning 1)) " "))
5220 (setq timestr 'time))
5221 (setq txt (org-agenda-format-item
5222 (if (= diff 0)
5223 (car org-agenda-deadline-leaders)
5224 (if (functionp
5225 (nth 1 org-agenda-deadline-leaders))
5226 (funcall
5227 (nth 1 org-agenda-deadline-leaders)
5228 diff date)
5229 (format (nth 1 org-agenda-deadline-leaders)
5230 diff)))
5231 head category tags
5232 (if (not (= diff 0)) nil timestr)))))
5233 (when txt
5234 (setq face (org-agenda-deadline-face dfrac))
5235 (org-add-props txt props
5236 'org-marker (org-agenda-new-marker pos)
5237 'org-hd-marker (org-agenda-new-marker pos1)
5238 'priority (+ (- diff)
5239 (org-get-priority txt))
5240 'org-category category
5241 'org-category-position org-category-pos
5242 'todo-state todo-state
5243 'type (if upcomingp "upcoming-deadline" "deadline")
5244 'date (if upcomingp date d2)
5245 'face (if donep 'org-agenda-done face)
5246 'undone-face face 'done-face 'org-agenda-done)
5247 (push txt ee))))))
5248 (nreverse ee)))
5250 (defun org-agenda-deadline-face (fraction)
5251 "Return the face to displaying a deadline item.
5252 FRACTION is what fraction of the head-warning time has passed."
5253 (let ((faces org-agenda-deadline-faces) f)
5254 (catch 'exit
5255 (while (setq f (pop faces))
5256 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
5258 (defun org-agenda-get-scheduled (&optional deadline-results)
5259 "Return the scheduled information for agenda display."
5260 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
5261 'org-todo-regexp org-todo-regexp
5262 'org-complex-heading-regexp org-complex-heading-regexp
5263 'done-face 'org-agenda-done
5264 'mouse-face 'highlight
5265 'help-echo
5266 (format "mouse-2 or RET jump to org file %s"
5267 (abbreviate-file-name buffer-file-name))))
5268 (regexp org-scheduled-time-regexp)
5269 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5270 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5272 (deadline-position-alist
5273 (mapcar (lambda (a) (and (setq mm (get-text-property
5274 0 'org-hd-marker a))
5275 (cons (marker-position mm) a)))
5276 deadline-results))
5277 d2 diff pos pos1 category org-category-pos tags donep
5278 ee txt head pastschedp todo-state face timestr s habitp show-all)
5279 (goto-char (point-min))
5280 (while (re-search-forward regexp nil t)
5281 (catch :skip
5282 (org-agenda-skip)
5283 (setq s (match-string 1)
5284 txt nil
5285 pos (1- (match-beginning 1))
5286 todo-state (save-match-data (org-get-todo-state))
5287 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5288 (member todo-state
5289 org-agenda-repeating-timestamp-show-all))
5290 d2 (org-time-string-to-absolute
5291 (match-string 1) d1 'past show-all
5292 (current-buffer) pos)
5293 diff (- d2 d1))
5294 (setq pastschedp (and todayp (< diff 0)))
5295 ;; When to show a scheduled item in the calendar:
5296 ;; If it is on or past the date.
5297 (when (or (and (< diff 0)
5298 (< (abs diff) org-scheduled-past-days)
5299 (and todayp (not org-agenda-only-exact-dates)))
5300 (= diff 0))
5301 (save-excursion
5302 (setq donep (member todo-state org-done-keywords))
5303 (if (and donep
5304 (or org-agenda-skip-scheduled-if-done
5305 (not (= diff 0))
5306 (and (functionp 'org-is-habit-p)
5307 (org-is-habit-p))))
5308 (setq txt nil)
5309 (setq habitp (and (functionp 'org-is-habit-p)
5310 (org-is-habit-p)))
5311 (setq category (org-get-category)
5312 org-category-pos (get-text-property (point) 'org-category-position))
5313 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5314 (setq txt org-agenda-no-heading-message)
5315 (goto-char (match-end 0))
5316 (setq pos1 (match-beginning 0))
5317 (if habitp
5318 (if (or (not org-habit-show-habits)
5319 (and (not todayp)
5320 org-habit-show-habits-only-for-today))
5321 (throw :skip nil))
5322 (if (and
5323 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
5324 (and org-agenda-skip-scheduled-if-deadline-is-shown
5325 pastschedp))
5326 (setq mm (assoc pos1 deadline-position-alist)))
5327 (throw :skip nil)))
5328 (setq tags (org-get-tags-at))
5329 (setq head (buffer-substring-no-properties
5330 (point)
5331 (progn (skip-chars-forward "^\r\n") (point))))
5332 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5333 (setq timestr
5334 (concat (substring s (match-beginning 1)) " "))
5335 (setq timestr 'time))
5336 (setq txt (org-agenda-format-item
5337 (if (= diff 0)
5338 (car org-agenda-scheduled-leaders)
5339 (format (nth 1 org-agenda-scheduled-leaders)
5340 (- 1 diff)))
5341 head category tags
5342 (if (not (= diff 0)) nil timestr)
5343 nil habitp))))
5344 (when txt
5345 (setq face
5346 (cond
5347 ((and (not habitp) pastschedp)
5348 'org-scheduled-previously)
5349 (todayp 'org-scheduled-today)
5350 (t 'org-scheduled))
5351 habitp (and habitp (org-habit-parse-todo)))
5352 (org-add-props txt props
5353 'undone-face face
5354 'face (if donep 'org-agenda-done face)
5355 'org-marker (org-agenda-new-marker pos)
5356 'org-hd-marker (org-agenda-new-marker pos1)
5357 'type (if pastschedp "past-scheduled" "scheduled")
5358 'date (if pastschedp d2 date)
5359 'priority (if habitp
5360 (org-habit-get-priority habitp)
5361 (+ 94 (- 5 diff) (org-get-priority txt)))
5362 'org-category category
5363 'org-category-position org-category-pos
5364 'org-habit-p habitp
5365 'todo-state todo-state)
5366 (push txt ee))))))
5367 (nreverse ee)))
5369 (defun org-agenda-get-blocks ()
5370 "Return the date-range information for agenda display."
5371 (let* ((props (list 'face nil
5372 'org-not-done-regexp org-not-done-regexp
5373 'org-todo-regexp org-todo-regexp
5374 'org-complex-heading-regexp org-complex-heading-regexp
5375 'mouse-face 'highlight
5376 'help-echo
5377 (format "mouse-2 or RET jump to org file %s"
5378 (abbreviate-file-name buffer-file-name))))
5379 (regexp org-tr-regexp)
5380 (d0 (calendar-absolute-from-gregorian date))
5381 marker hdmarker ee txt d1 d2 s1 s2 category org-category-pos
5382 todo-state tags pos head donep)
5383 (goto-char (point-min))
5384 (while (re-search-forward regexp nil t)
5385 (catch :skip
5386 (org-agenda-skip)
5387 (setq pos (point))
5388 (let ((start-time (match-string 1))
5389 (end-time (match-string 2)))
5390 (setq s1 (match-string 1)
5391 s2 (match-string 2)
5392 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
5393 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
5394 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
5395 ;; Only allow days between the limits, because the normal
5396 ;; date stamps will catch the limits.
5397 (save-excursion
5398 (setq todo-state (org-get-todo-state))
5399 (setq donep (member todo-state org-done-keywords))
5400 (if (and donep org-agenda-skip-timestamp-if-done)
5401 (throw :skip t))
5402 (setq marker (org-agenda-new-marker (point)))
5403 (setq category (org-get-category)
5404 org-category-pos (get-text-property (point) 'org-category-position))
5405 (if (not (re-search-backward org-outline-regexp-bol nil t))
5406 (setq txt org-agenda-no-heading-message)
5407 (goto-char (match-beginning 0))
5408 (setq hdmarker (org-agenda-new-marker (point)))
5409 (setq tags (org-get-tags-at))
5410 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5411 (setq head (match-string 1))
5412 (let ((remove-re
5413 (if org-agenda-remove-timeranges-from-blocks
5414 (concat
5415 "<" (regexp-quote s1) ".*?>"
5416 "--"
5417 "<" (regexp-quote s2) ".*?>")
5418 nil)))
5419 (setq txt (org-agenda-format-item
5420 (format
5421 (nth (if (= d1 d2) 0 1)
5422 org-agenda-timerange-leaders)
5423 (1+ (- d0 d1)) (1+ (- d2 d1)))
5424 head category tags
5425 (cond ((and (= d1 d0) (= d2 d0))
5426 (concat "<" start-time ">--<" end-time ">"))
5427 ((= d1 d0)
5428 (concat "<" start-time ">"))
5429 ((= d2 d0)
5430 (concat "<" end-time ">"))
5431 (t nil))
5432 remove-re))))
5433 (org-add-props txt props
5434 'org-marker marker 'org-hd-marker hdmarker
5435 'type "block" 'date date
5436 'todo-state todo-state
5437 'priority (org-get-priority txt) 'org-category category
5438 'org-category-position org-category-pos)
5439 (push txt ee))))
5440 (goto-char pos)))
5441 ;; Sort the entries by expiration date.
5442 (nreverse ee)))
5444 ;;; Agenda presentation and sorting
5446 (defvar org-prefix-has-time nil
5447 "A flag, set by `org-compile-prefix-format'.
5448 The flag is set if the currently compiled format contains a `%t'.")
5449 (defvar org-prefix-has-tag nil
5450 "A flag, set by `org-compile-prefix-format'.
5451 The flag is set if the currently compiled format contains a `%T'.")
5452 (defvar org-prefix-has-effort nil
5453 "A flag, set by `org-compile-prefix-format'.
5454 The flag is set if the currently compiled format contains a `%e'.")
5455 (defvar org-prefix-category-length nil
5456 "Used by `org-compile-prefix-format' to remember the category field width.")
5457 (defvar org-prefix-category-max-length nil
5458 "Used by `org-compile-prefix-format' to remember the category field width.")
5460 (defun org-agenda-get-category-icon (category)
5461 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
5462 (dolist (entry org-agenda-category-icon-alist)
5463 (when (org-string-match-p (car entry) category)
5464 (if (listp (cadr entry))
5465 (return (cadr entry))
5466 (return (apply 'create-image (cdr entry)))))))
5468 (defun org-agenda-format-item (extra txt &optional category tags dotime
5469 remove-re habitp)
5470 "Format TXT to be inserted into the agenda buffer.
5471 In particular, it adds the prefix and corresponding text properties. EXTRA
5472 must be a string and replaces the `%s' specifier in the prefix format.
5473 CATEGORY (string, symbol or nil) may be used to overrule the default
5474 category taken from local variable or file name. It will replace the `%c'
5475 specifier in the format. DOTIME, when non-nil, indicates that a
5476 time-of-day should be extracted from TXT for sorting of this entry, and for
5477 the `%t' specifier in the format. When DOTIME is a string, this string is
5478 searched for a time before TXT is. TAGS can be the tags of the headline.
5479 Any match of REMOVE-RE will be removed from TXT."
5480 (save-match-data
5481 ;; Diary entries sometimes have extra whitespace at the beginning
5482 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
5484 ;; Fix the tags part in txt
5485 (setq txt (org-agenda-fix-displayed-tags
5486 txt tags
5487 org-agenda-show-inherited-tags
5488 org-agenda-hide-tags-regexp))
5489 (let* ((category (or category
5490 (if (stringp org-category)
5491 org-category
5492 (and org-category (symbol-name org-category)))
5493 (if buffer-file-name
5494 (file-name-sans-extension
5495 (file-name-nondirectory buffer-file-name))
5496 "")))
5497 (category-icon (org-agenda-get-category-icon category))
5498 (category-icon (if category-icon
5499 (propertize " " 'display category-icon)
5500 ""))
5501 ;; time, tag, effort are needed for the eval of the prefix format
5502 (tag (if tags (nth (1- (length tags)) tags) ""))
5503 time effort neffort
5504 (ts (if dotime (concat
5505 (if (stringp dotime) dotime "")
5506 (and org-agenda-search-headline-for-time txt))))
5507 (time-of-day (and dotime (org-get-time-of-day ts)))
5508 stamp plain s0 s1 s2 rtn srp l
5509 duration thecategory)
5510 (and (eq major-mode 'org-mode) buffer-file-name
5511 (add-to-list 'org-agenda-contributing-files buffer-file-name))
5512 (when (and dotime time-of-day)
5513 ;; Extract starting and ending time and move them to prefix
5514 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
5515 (setq plain (string-match org-plain-time-of-day-regexp ts)))
5516 (setq s0 (match-string 0 ts)
5517 srp (and stamp (match-end 3))
5518 s1 (match-string (if plain 1 2) ts)
5519 s2 (match-string (if plain 8 (if srp 4 6)) ts))
5521 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
5522 ;; them, we might want to remove them there to avoid duplication.
5523 ;; The user can turn this off with a variable.
5524 (if (and org-prefix-has-time
5525 org-agenda-remove-times-when-in-prefix (or stamp plain)
5526 (string-match (concat (regexp-quote s0) " *") txt)
5527 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
5528 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
5529 (= (match-beginning 0) 0)
5531 (setq txt (replace-match "" nil nil txt))))
5532 ;; Normalize the time(s) to 24 hour
5533 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
5534 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
5536 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
5537 (when (and s1 (not s2) org-agenda-default-appointment-duration)
5538 (setq s2
5539 (org-minutes-to-hh:mm-string
5540 (+ (org-hh:mm-string-to-minutes s1) org-agenda-default-appointment-duration))))
5542 ;; Compute the duration
5543 (when s2
5544 (setq duration (- (org-hh:mm-string-to-minutes s2)
5545 (org-hh:mm-string-to-minutes s1)))))
5547 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
5548 txt)
5549 ;; Tags are in the string
5550 (if (or (eq org-agenda-remove-tags t)
5551 (and org-agenda-remove-tags
5552 org-prefix-has-tag))
5553 (setq txt (replace-match "" t t txt))
5554 (setq txt (replace-match
5555 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
5556 (match-string 2 txt))
5557 t t txt))))
5558 (when (eq major-mode 'org-mode)
5559 (setq effort
5560 (condition-case nil
5561 (org-get-effort
5562 (or (get-text-property 0 'org-hd-marker txt)
5563 (get-text-property 0 'org-marker txt)))
5564 (error nil)))
5565 (when effort
5566 (setq neffort (org-duration-string-to-minutes effort)
5567 effort (setq effort (concat "[" effort "]")))))
5568 ;; prevent erroring out with %e format when there is no effort
5569 (or effort (setq effort ""))
5571 (when remove-re
5572 (while (string-match remove-re txt)
5573 (setq txt (replace-match "" t t txt))))
5575 ;; Set org-heading property on `txt' to mark the start of the
5576 ;; heading.
5577 (add-text-properties 0 (length txt) '(org-heading t) txt)
5579 ;; Prepare the variables needed in the eval of the compiled format
5580 (setq time (cond (s2 (concat
5581 (org-agenda-time-of-day-to-ampm-maybe s1)
5582 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
5583 (if org-agenda-timegrid-use-ampm " ")))
5584 (s1 (concat
5585 (org-agenda-time-of-day-to-ampm-maybe s1)
5586 (if org-agenda-timegrid-use-ampm
5587 "........ "
5588 "......")))
5589 (t ""))
5590 extra (or (and (not habitp) extra) "")
5591 category (if (symbolp category) (symbol-name category) category)
5592 thecategory (copy-sequence category))
5593 (if (string-match org-bracket-link-regexp category)
5594 (progn
5595 (setq l (if (match-end 3)
5596 (- (match-end 3) (match-beginning 3))
5597 (- (match-end 1) (match-beginning 1))))
5598 (when (< l (or org-prefix-category-length 0))
5599 (setq category (copy-sequence category))
5600 (org-add-props category nil
5601 'extra-space (make-string
5602 (- org-prefix-category-length l 1) ?\ ))))
5603 (if (and org-prefix-category-max-length
5604 (>= (length category) org-prefix-category-max-length))
5605 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
5606 ;; Evaluate the compiled format
5607 (setq rtn (concat (eval org-prefix-format-compiled) txt))
5609 ;; And finally add the text properties
5610 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
5611 (org-add-props rtn nil
5612 'org-category (if thecategory (downcase thecategory) category)
5613 'tags (mapcar 'org-downcase-keep-props tags)
5614 'org-highest-priority org-highest-priority
5615 'org-lowest-priority org-lowest-priority
5616 'time-of-day time-of-day
5617 'duration duration
5618 'effort effort
5619 'effort-minutes neffort
5620 'txt txt
5621 'time time
5622 'extra extra
5623 'format org-prefix-format-compiled
5624 'dotime dotime))))
5626 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
5627 "Remove tags string from TXT, and add a modified list of tags.
5628 The modified list may contain inherited tags, and tags matched by
5629 `org-agenda-hide-tags-regexp' will be removed."
5630 (when (or add-inherited hide-re)
5631 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
5632 (setq txt (substring txt 0 (match-beginning 0))))
5633 (setq tags
5634 (delq nil
5635 (mapcar (lambda (tg)
5636 (if (or (and hide-re (string-match hide-re tg))
5637 (and (not add-inherited)
5638 (get-text-property 0 'inherited tg)))
5640 tg))
5641 tags)))
5642 (when tags
5643 (let ((have-i (get-text-property 0 'inherited (car tags)))
5645 (setq txt (concat txt " :"
5646 (mapconcat
5647 (lambda (x)
5648 (setq i (get-text-property 0 'inherited x))
5649 (if (and have-i (not i))
5650 (progn
5651 (setq have-i nil)
5652 (concat ":" x))
5654 tags ":")
5655 (if have-i "::" ":"))))))
5656 txt)
5658 (defun org-downcase-keep-props (s)
5659 (let ((props (text-properties-at 0 s)))
5660 (setq s (downcase s))
5661 (add-text-properties 0 (length s) props s)
5664 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
5665 (defvar org-agenda-sorting-strategy-selected nil)
5667 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
5668 (catch 'exit
5669 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
5670 ((and todayp (member 'today (car org-agenda-time-grid))))
5671 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
5672 ((member 'weekly (car org-agenda-time-grid)))
5673 (t (throw 'exit list)))
5674 (let* ((have (delq nil (mapcar
5675 (lambda (x) (get-text-property 1 'time-of-day x))
5676 list)))
5677 (string (nth 1 org-agenda-time-grid))
5678 (gridtimes (nth 2 org-agenda-time-grid))
5679 (req (car org-agenda-time-grid))
5680 (remove (member 'remove-match req))
5681 new time)
5682 (if (and (member 'require-timed req) (not have))
5683 ;; don't show empty grid
5684 (throw 'exit list))
5685 (while (setq time (pop gridtimes))
5686 (unless (and remove (member time have))
5687 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
5688 (push (org-agenda-format-item
5689 nil string "" nil
5690 (concat (substring time 0 -2) ":" (substring time -2)))
5691 new)
5692 (put-text-property
5693 2 (length (car new)) 'face 'org-time-grid (car new))))
5694 (when (and todayp org-agenda-show-current-time-in-grid)
5695 (push (org-agenda-format-item
5697 org-agenda-current-time-string
5698 "" nil
5699 (format-time-string "%H:%M "))
5700 new)
5701 (put-text-property
5702 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
5704 (if (member 'time-up org-agenda-sorting-strategy-selected)
5705 (append new list)
5706 (append list new)))))
5708 (defun org-compile-prefix-format (key)
5709 "Compile the prefix format into a Lisp form that can be evaluated.
5710 The resulting form is returned and stored in the variable
5711 `org-prefix-format-compiled'."
5712 (setq org-prefix-has-time nil org-prefix-has-tag nil
5713 org-prefix-category-length nil
5714 org-prefix-has-effort nil)
5715 (let ((s (cond
5716 ((stringp org-agenda-prefix-format)
5717 org-agenda-prefix-format)
5718 ((assq key org-agenda-prefix-format)
5719 (cdr (assq key org-agenda-prefix-format)))
5720 (t " %-12:c%?-12t% s")))
5721 (start 0)
5722 varform vars var e c f opt)
5723 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctsei]\\|(.+)\\)"
5724 s start)
5725 (setq var (or (cdr (assoc (match-string 4 s)
5726 '(("c" . category) ("t" . time) ("s" . extra)
5727 ("i" . category-icon) ("T" . tag) ("e" . effort))))
5728 'eval)
5729 c (or (match-string 3 s) "")
5730 opt (match-beginning 1)
5731 start (1+ (match-beginning 0)))
5732 (if (equal var 'time) (setq org-prefix-has-time t))
5733 (if (equal var 'tag) (setq org-prefix-has-tag t))
5734 (if (equal var 'effort) (setq org-prefix-has-effort t))
5735 (setq f (concat "%" (match-string 2 s) "s"))
5736 (when (equal var 'category)
5737 (setq org-prefix-category-length
5738 (floor (abs (string-to-number (match-string 2 s)))))
5739 (setq org-prefix-category-max-length
5740 (let ((x (match-string 2 s)))
5741 (save-match-data
5742 (if (string-match "\\.[0-9]+" x)
5743 (string-to-number (substring (match-string 0 x) 1)))))))
5744 (if (eq var 'eval)
5745 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
5746 (if opt
5747 (setq varform
5748 `(if (equal "" ,var)
5750 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
5751 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
5752 (setq s (replace-match "%s" t nil s))
5753 (push varform vars))
5754 (setq vars (nreverse vars))
5755 (setq org-prefix-format-compiled `(format ,s ,@vars))))
5757 (defun org-set-sorting-strategy (key)
5758 (if (symbolp (car org-agenda-sorting-strategy))
5759 ;; the old format
5760 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
5761 (setq org-agenda-sorting-strategy-selected
5762 (or (cdr (assq key org-agenda-sorting-strategy))
5763 (cdr (assq 'agenda org-agenda-sorting-strategy))
5764 '(time-up category-keep priority-down)))))
5766 (defun org-get-time-of-day (s &optional string mod24)
5767 "Check string S for a time of day.
5768 If found, return it as a military time number between 0 and 2400.
5769 If not found, return nil.
5770 The optional STRING argument forces conversion into a 5 character wide string
5771 HH:MM."
5772 (save-match-data
5773 (when
5774 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
5775 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
5776 (let* ((h (string-to-number (match-string 1 s)))
5777 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
5778 (ampm (if (match-end 4) (downcase (match-string 4 s))))
5779 (am-p (equal ampm "am"))
5780 (h1 (cond ((not ampm) h)
5781 ((= h 12) (if am-p 0 12))
5782 (t (+ h (if am-p 0 12)))))
5783 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
5784 (mod h1 24) h1))
5785 (t0 (+ (* 100 h2) m))
5786 (t1 (concat (if (>= h1 24) "+" " ")
5787 (if (and org-agenda-time-leading-zero
5788 (< t0 1000)) "0" "")
5789 (if (< t0 100) "0" "")
5790 (if (< t0 10) "0" "")
5791 (int-to-string t0))))
5792 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
5794 (defvar org-agenda-before-sorting-filter-function nil
5795 "Function to be applied to agenda items prior to sorting.
5796 Prior to sorting also means just before they are inserted into the agenda.
5798 To aid sorting, you may revisit the original entries and add more text
5799 properties which will later be used by the sorting functions.
5801 The function should take a string argument, an agenda line.
5802 It has access to the text properties in that line, which contain among
5803 other things, the property `org-hd-marker' that points to the entry
5804 where the line comes from. Note that not all lines going into the agenda
5805 have this property, only most.
5807 The function should return the modified string. It is probably best
5808 to ONLY change text properties.
5810 You can also use this function as a filter, by returning nil for lines
5811 you don't want to have in the agenda at all. For this application, you
5812 could bind the variable in the options section of a custom command.")
5814 (defun org-finalize-agenda-entries (list &optional nosort)
5815 "Sort and concatenate the agenda items."
5816 (setq list (mapcar 'org-agenda-highlight-todo list))
5817 (if nosort
5818 list
5819 (when org-agenda-before-sorting-filter-function
5820 (setq list (delq nil (mapcar org-agenda-before-sorting-filter-function list))))
5821 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
5823 (defun org-agenda-highlight-todo (x)
5824 (let ((org-done-keywords org-done-keywords-for-agenda)
5825 (case-fold-search nil)
5827 (if (eq x 'line)
5828 (save-excursion
5829 (beginning-of-line 1)
5830 (setq re (org-get-at-bol 'org-todo-regexp))
5831 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
5832 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
5833 (add-text-properties (match-beginning 0) (match-end 1)
5834 (list 'face (org-get-todo-face 1)))
5835 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
5836 (delete-region (match-beginning 1) (1- (match-end 0)))
5837 (goto-char (match-beginning 1))
5838 (insert (format org-agenda-todo-keyword-format s)))))
5839 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
5840 (setq re (get-text-property 0 'org-todo-regexp x))
5841 (when (and re
5842 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
5843 x (or pl 0)) pl))
5844 (add-text-properties
5845 (or (match-end 1) (match-end 0)) (match-end 0)
5846 (list 'face (org-get-todo-face (match-string 2 x)))
5848 (when (match-end 1)
5849 (setq x (concat (substring x 0 (match-end 1))
5850 (format org-agenda-todo-keyword-format
5851 (match-string 2 x))
5852 (org-add-props " " (text-properties-at 0 x))
5853 (substring x (match-end 3)))))))
5854 x)))
5856 (defsubst org-cmp-priority (a b)
5857 "Compare the priorities of string A and B."
5858 (let ((pa (or (get-text-property 1 'priority a) 0))
5859 (pb (or (get-text-property 1 'priority b) 0)))
5860 (cond ((> pa pb) +1)
5861 ((< pa pb) -1)
5862 (t nil))))
5864 (defsubst org-cmp-effort (a b)
5865 "Compare the effort values of string A and B."
5866 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
5867 (ea (or (get-text-property 1 'effort-minutes a) def))
5868 (eb (or (get-text-property 1 'effort-minutes b) def)))
5869 (cond ((> ea eb) +1)
5870 ((< ea eb) -1)
5871 (t nil))))
5873 (defsubst org-cmp-category (a b)
5874 "Compare the string values of categories of strings A and B."
5875 (let ((ca (or (get-text-property 1 'org-category a) ""))
5876 (cb (or (get-text-property 1 'org-category b) "")))
5877 (cond ((string-lessp ca cb) -1)
5878 ((string-lessp cb ca) +1)
5879 (t nil))))
5881 (defsubst org-cmp-todo-state (a b)
5882 "Compare the todo states of strings A and B."
5883 (let* ((ma (or (get-text-property 1 'org-marker a)
5884 (get-text-property 1 'org-hd-marker a)))
5885 (mb (or (get-text-property 1 'org-marker b)
5886 (get-text-property 1 'org-hd-marker b)))
5887 (fa (and ma (marker-buffer ma)))
5888 (fb (and mb (marker-buffer mb)))
5889 (todo-kwds
5890 (or (and fa (with-current-buffer fa org-todo-keywords-1))
5891 (and fb (with-current-buffer fb org-todo-keywords-1))))
5892 (ta (or (get-text-property 1 'todo-state a) ""))
5893 (tb (or (get-text-property 1 'todo-state b) ""))
5894 (la (- (length (member ta todo-kwds))))
5895 (lb (- (length (member tb todo-kwds))))
5896 (donepa (member ta org-done-keywords-for-agenda))
5897 (donepb (member tb org-done-keywords-for-agenda)))
5898 (cond ((and donepa (not donepb)) -1)
5899 ((and (not donepa) donepb) +1)
5900 ((< la lb) -1)
5901 ((< lb la) +1)
5902 (t nil))))
5904 (defsubst org-cmp-alpha (a b)
5905 "Compare the headlines, alphabetically."
5906 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
5907 (plb (text-property-any 0 (length b) 'org-heading t b))
5908 (ta (and pla (substring a pla)))
5909 (tb (and plb (substring b plb))))
5910 (when pla
5911 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
5912 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
5913 (setq ta (substring ta (match-end 0))))
5914 (setq ta (downcase ta)))
5915 (when plb
5916 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
5917 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
5918 (setq tb (substring tb (match-end 0))))
5919 (setq tb (downcase tb)))
5920 (cond ((not ta) +1)
5921 ((not tb) -1)
5922 ((string-lessp ta tb) -1)
5923 ((string-lessp tb ta) +1)
5924 (t nil))))
5926 (defsubst org-cmp-tag (a b)
5927 "Compare the string values of the first tags of A and B."
5928 (let ((ta (car (last (get-text-property 1 'tags a))))
5929 (tb (car (last (get-text-property 1 'tags b)))))
5930 (cond ((not ta) +1)
5931 ((not tb) -1)
5932 ((string-lessp ta tb) -1)
5933 ((string-lessp tb ta) +1)
5934 (t nil))))
5936 (defsubst org-cmp-time (a b)
5937 "Compare the time-of-day values of strings A and B."
5938 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
5939 (ta (or (get-text-property 1 'time-of-day a) def))
5940 (tb (or (get-text-property 1 'time-of-day b) def)))
5941 (cond ((< ta tb) -1)
5942 ((< tb ta) +1)
5943 (t nil))))
5945 (defsubst org-cmp-habit-p (a b)
5946 "Compare the todo states of strings A and B."
5947 (let ((ha (get-text-property 1 'org-habit-p a))
5948 (hb (get-text-property 1 'org-habit-p b)))
5949 (cond ((and ha (not hb)) -1)
5950 ((and (not ha) hb) +1)
5951 (t nil))))
5953 (defsubst org-em (x y list) (or (memq x list) (memq y list)))
5955 (defun org-entries-lessp (a b)
5956 "Predicate for sorting agenda entries."
5957 ;; The following variables will be used when the form is evaluated.
5958 ;; So even though the compiler complains, keep them.
5959 (let* ((ss org-agenda-sorting-strategy-selected)
5960 (time-up (and (org-em 'time-up 'time-down ss)
5961 (org-cmp-time a b)))
5962 (time-down (if time-up (- time-up) nil))
5963 (priority-up (and (org-em 'priority-up 'priority-down ss)
5964 (org-cmp-priority a b)))
5965 (priority-down (if priority-up (- priority-up) nil))
5966 (effort-up (and (org-em 'effort-up 'effort-down ss)
5967 (org-cmp-effort a b)))
5968 (effort-down (if effort-up (- effort-up) nil))
5969 (category-up (and (or (org-em 'category-up 'category-down ss)
5970 (memq 'category-keep ss))
5971 (org-cmp-category a b)))
5972 (category-down (if category-up (- category-up) nil))
5973 (category-keep (if category-up +1 nil))
5974 (tag-up (and (org-em 'tag-up 'tag-down ss)
5975 (org-cmp-tag a b)))
5976 (tag-down (if tag-up (- tag-up) nil))
5977 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
5978 (org-cmp-todo-state a b)))
5979 (todo-state-down (if todo-state-up (- todo-state-up) nil))
5980 (habit-up (and (org-em 'habit-up 'habit-down ss)
5981 (org-cmp-habit-p a b)))
5982 (habit-down (if habit-up (- habit-up) nil))
5983 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
5984 (org-cmp-alpha a b)))
5985 (alpha-down (if alpha-up (- alpha-up) nil))
5986 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
5987 user-defined-up user-defined-down)
5988 (if (and need-user-cmp org-agenda-cmp-user-defined
5989 (functionp org-agenda-cmp-user-defined))
5990 (setq user-defined-up
5991 (funcall org-agenda-cmp-user-defined a b)
5992 user-defined-down (if user-defined-up (- user-defined-up) nil)))
5993 (cdr (assoc
5994 (eval (cons 'or org-agenda-sorting-strategy-selected))
5995 '((-1 . t) (1 . nil) (nil . nil))))))
5997 ;;; Agenda restriction lock
5999 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
6000 "Overlay to mark the headline to which agenda commands are restricted.")
6001 (overlay-put org-agenda-restriction-lock-overlay
6002 'face 'org-agenda-restriction-lock)
6003 (overlay-put org-agenda-restriction-lock-overlay
6004 'help-echo "Agendas are currently limited to this subtree.")
6005 (org-detach-overlay org-agenda-restriction-lock-overlay)
6007 (defun org-agenda-set-restriction-lock (&optional type)
6008 "Set restriction lock for agenda, to current subtree or file.
6009 Restriction will be the file if TYPE is `file', or if type is the
6010 universal prefix '(4), or if the cursor is before the first headline
6011 in the file. Otherwise, restriction will be to the current subtree."
6012 (interactive "P")
6013 (and (equal type '(4)) (setq type 'file))
6014 (setq type (cond
6015 (type type)
6016 ((org-at-heading-p) 'subtree)
6017 ((condition-case nil (org-back-to-heading t) (error nil))
6018 'subtree)
6019 (t 'file)))
6020 (if (eq type 'subtree)
6021 (progn
6022 (setq org-agenda-restrict t)
6023 (setq org-agenda-overriding-restriction 'subtree)
6024 (put 'org-agenda-files 'org-restrict
6025 (list (buffer-file-name (buffer-base-buffer))))
6026 (org-back-to-heading t)
6027 (move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
6028 (move-marker org-agenda-restrict-begin (point))
6029 (move-marker org-agenda-restrict-end
6030 (save-excursion (org-end-of-subtree t)))
6031 (message "Locking agenda restriction to subtree"))
6032 (put 'org-agenda-files 'org-restrict
6033 (list (buffer-file-name (buffer-base-buffer))))
6034 (setq org-agenda-restrict nil)
6035 (setq org-agenda-overriding-restriction 'file)
6036 (move-marker org-agenda-restrict-begin nil)
6037 (move-marker org-agenda-restrict-end nil)
6038 (message "Locking agenda restriction to file"))
6039 (setq current-prefix-arg nil)
6040 (org-agenda-maybe-redo))
6042 (defun org-agenda-remove-restriction-lock (&optional noupdate)
6043 "Remove the agenda restriction lock."
6044 (interactive "P")
6045 (org-detach-overlay org-agenda-restriction-lock-overlay)
6046 (org-detach-overlay org-speedbar-restriction-lock-overlay)
6047 (setq org-agenda-overriding-restriction nil)
6048 (setq org-agenda-restrict nil)
6049 (put 'org-agenda-files 'org-restrict nil)
6050 (move-marker org-agenda-restrict-begin nil)
6051 (move-marker org-agenda-restrict-end nil)
6052 (setq current-prefix-arg nil)
6053 (message "Agenda restriction lock removed")
6054 (or noupdate (org-agenda-maybe-redo)))
6056 (defun org-agenda-maybe-redo ()
6057 "If there is any window showing the agenda view, update it."
6058 (let ((w (get-buffer-window org-agenda-buffer-name t))
6059 (w0 (selected-window)))
6060 (when w
6061 (select-window w)
6062 (org-agenda-redo)
6063 (select-window w0)
6064 (if org-agenda-overriding-restriction
6065 (message "Agenda view shifted to new %s restriction"
6066 org-agenda-overriding-restriction)
6067 (message "Agenda restriction lock removed")))))
6069 ;;; Agenda commands
6071 (defun org-agenda-check-type (error &rest types)
6072 "Check if agenda buffer is of allowed type.
6073 If ERROR is non-nil, throw an error, otherwise just return nil."
6074 (if (memq org-agenda-type types)
6076 (if error
6077 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
6078 nil)))
6080 (defun org-agenda-quit ()
6081 "Exit agenda by removing the window or the buffer."
6082 (interactive)
6083 (if org-agenda-columns-active
6084 (org-columns-quit)
6085 (let ((buf (current-buffer)))
6086 (if (eq org-agenda-window-setup 'other-frame)
6087 (progn
6088 (kill-buffer buf)
6089 (org-agenda-reset-markers)
6090 (org-columns-remove-overlays)
6091 (setq org-agenda-archives-mode nil)
6092 (delete-frame))
6093 (and (not (eq org-agenda-window-setup 'current-window))
6094 (not (one-window-p))
6095 (delete-window))
6096 (kill-buffer buf)
6097 (org-agenda-reset-markers)
6098 (org-columns-remove-overlays)
6099 (setq org-agenda-archives-mode nil)))
6100 ;; Maybe restore the pre-agenda window configuration.
6101 (and org-agenda-restore-windows-after-quit
6102 (not (eq org-agenda-window-setup 'other-frame))
6103 org-pre-agenda-window-conf
6104 (set-window-configuration org-pre-agenda-window-conf))))
6106 (defun org-agenda-exit ()
6107 "Exit agenda by removing the window or the buffer.
6108 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
6109 Org-mode buffers visited directly by the user will not be touched."
6110 (interactive)
6111 (org-release-buffers org-agenda-new-buffers)
6112 (setq org-agenda-new-buffers nil)
6113 (org-agenda-quit))
6115 (defun org-agenda-execute (arg)
6116 "Execute another agenda command, keeping same window.
6117 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
6118 in the agenda."
6119 (interactive "P")
6120 (let ((org-agenda-window-setup 'current-window))
6121 (org-agenda arg)))
6123 (defun org-agenda-redo ()
6124 "Rebuild Agenda.
6125 When this is the global TODO list, a prefix argument will be interpreted."
6126 (interactive)
6127 (let* ((org-agenda-keep-modes t)
6128 (filter org-agenda-filter)
6129 (preset (get 'org-agenda-filter :preset-filter))
6130 (org-agenda-filter-while-redo (or filter preset))
6131 (cols org-agenda-columns-active)
6132 (line (org-current-line))
6133 (window-line (- line (org-current-line (window-start))))
6134 (lprops (get 'org-agenda-redo-command 'org-lprops)))
6135 (put 'org-agenda-filter :preset-filter nil)
6136 (and cols (org-columns-quit))
6137 (message "Rebuilding agenda buffer...")
6138 (org-let lprops '(eval org-agenda-redo-command))
6139 (setq org-agenda-undo-list nil
6140 org-agenda-pending-undo-list nil)
6141 (message "Rebuilding agenda buffer...done")
6142 (put 'org-agenda-filter :preset-filter preset)
6143 (and (or filter preset) (org-agenda-filter-apply filter))
6144 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
6145 (org-goto-line line)
6146 (recenter window-line)))
6149 (defvar org-global-tags-completion-table nil)
6150 (defvar org-agenda-filter-form nil)
6151 (defun org-agenda-filter-by-tag (strip &optional char narrow)
6152 "Keep only those lines in the agenda buffer that have a specific tag.
6153 The tag is selected with its fast selection letter, as configured.
6154 With prefix argument STRIP, remove all lines that do have the tag.
6155 A lisp caller can specify CHAR. NARROW means that the new tag should be
6156 used to narrow the search - the interactive user can also press `-' or `+'
6157 to switch to narrowing."
6158 (interactive "P")
6159 (let* ((alist org-tag-alist-for-agenda)
6160 (tag-chars (mapconcat
6161 (lambda (x) (if (and (not (symbolp (car x)))
6162 (cdr x))
6163 (char-to-string (cdr x))
6164 ""))
6165 alist ""))
6166 (efforts (org-split-string
6167 (or (cdr (assoc (concat org-effort-property "_ALL")
6168 org-global-properties))
6169 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
6170 "")))
6171 (effort-op org-agenda-filter-effort-default-operator)
6172 (effort-prompt "")
6173 (inhibit-read-only t)
6174 (current org-agenda-filter)
6175 maybe-refresh a n tag)
6176 (unless char
6177 (message
6178 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
6179 (if narrow "Narrow" "Filter") tag-chars
6180 (if org-agenda-auto-exclude-function "[RET], " ""))
6181 (setq char (read-char-exclusive)))
6182 (when (member char '(?+ ?-))
6183 ;; Narrowing down
6184 (cond ((equal char ?-) (setq strip t narrow t))
6185 ((equal char ?+) (setq strip nil narrow t)))
6186 (message
6187 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
6188 (setq char (read-char-exclusive)))
6189 (when (member char '(?< ?> ?= ??))
6190 ;; An effort operator
6191 (setq effort-op (char-to-string char))
6192 (setq alist nil) ; to make sure it will be interpreted as effort.
6193 (unless (equal char ??)
6194 (loop for i from 0 to 9 do
6195 (setq effort-prompt
6196 (concat
6197 effort-prompt " ["
6198 (if (= i 9) "0" (int-to-string (1+ i)))
6199 "]" (nth i efforts))))
6200 (message "Effort%s: %s " effort-op effort-prompt)
6201 (setq char (read-char-exclusive))
6202 (when (or (< char ?0) (> char ?9))
6203 (error "Need 1-9,0 to select effort" ))))
6204 (when (equal char ?\t)
6205 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
6206 (org-set-local 'org-global-tags-completion-table
6207 (org-global-tags-completion-table)))
6208 (let ((completion-ignore-case t))
6209 (setq tag (org-icompleting-read
6210 "Tag: " org-global-tags-completion-table))))
6211 (cond
6212 ((equal char ?\r)
6213 (org-agenda-filter-by-tag-show-all)
6214 (when org-agenda-auto-exclude-function
6215 (setq org-agenda-filter '())
6216 (dolist (tag (org-agenda-get-represented-tags))
6217 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
6218 (if modifier
6219 (push modifier org-agenda-filter))))
6220 (if (not (null org-agenda-filter))
6221 (org-agenda-filter-apply org-agenda-filter)))
6222 (setq maybe-refresh t))
6223 ((equal char ?/)
6224 (org-agenda-filter-by-tag-show-all)
6225 (when (get 'org-agenda-filter :preset-filter)
6226 (org-agenda-filter-apply org-agenda-filter))
6227 (setq maybe-refresh t))
6228 ((or (equal char ?\ )
6229 (setq a (rassoc char alist))
6230 (and (>= char ?0) (<= char ?9)
6231 (setq n (if (= char ?0) 9 (- char ?0 1))
6232 tag (concat effort-op (nth n efforts))
6233 a (cons tag nil)))
6234 (and (= char ??)
6235 (setq tag "?eff")
6236 a (cons tag nil))
6237 (and tag (setq a (cons tag nil))))
6238 (org-agenda-filter-by-tag-show-all)
6239 (setq tag (car a))
6240 (setq org-agenda-filter
6241 (cons (concat (if strip "-" "+") tag)
6242 (if narrow current nil)))
6243 (org-agenda-filter-apply org-agenda-filter)
6244 (setq maybe-refresh t))
6245 (t (error "Invalid tag selection character %c" char)))
6246 (when (and maybe-refresh
6247 (eq org-agenda-clockreport-mode 'with-filter))
6248 (org-agenda-redo))))
6250 (defun org-agenda-get-represented-tags ()
6251 "Get a list of all tags currently represented in the agenda."
6252 (let (p tags)
6253 (save-excursion
6254 (goto-char (point-min))
6255 (while (setq p (next-single-property-change (point) 'tags))
6256 (goto-char p)
6257 (mapc (lambda (x) (add-to-list 'tags x))
6258 (get-text-property (point) 'tags))))
6259 tags))
6261 (defun org-agenda-filter-by-tag-refine (strip &optional char)
6262 "Refine the current filter. See `org-agenda-filter-by-tag'."
6263 (interactive "P")
6264 (org-agenda-filter-by-tag strip char 'refine))
6266 (defun org-agenda-filter-make-matcher ()
6267 "Create the form that tests a line for the agenda filter."
6268 (let (f f1)
6269 (dolist (x (append (get 'org-agenda-filter :preset-filter)
6270 org-agenda-filter))
6271 (if (member x '("-" "+"))
6272 (setq f1 (if (equal x "-") 'tags '(not tags)))
6273 (if (string-match "[<=>?]" x)
6274 (setq f1 (org-agenda-filter-effort-form x))
6275 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
6276 (if (equal (string-to-char x) ?-)
6277 (setq f1 (list 'not f1))))
6278 (push f1 f))
6279 (cons 'and (nreverse f))))
6281 (defun org-agenda-filter-effort-form (e)
6282 "Return the form to compare the effort of the current line with what E says.
6283 E looks like \"+<2:25\"."
6284 (let (op)
6285 (setq e (substring e 1))
6286 (setq op (string-to-char e) e (substring e 1))
6287 (setq op (cond ((equal op ?<) '<=)
6288 ((equal op ?>) '>=)
6289 ((equal op ??) op)
6290 (t '=)))
6291 (list 'org-agenda-compare-effort (list 'quote op)
6292 (org-duration-string-to-minutes e))))
6294 (defun org-agenda-compare-effort (op value)
6295 "Compare the effort of the current line with VALUE, using OP.
6296 If the line does not have an effort defined, return nil."
6297 (let ((eff (org-get-at-bol 'effort-minutes)))
6298 (if (equal op ??)
6299 (not eff)
6300 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
6301 value))))
6303 (defun org-agenda-filter-apply (filter)
6304 "Set FILTER as the new agenda filter and apply it."
6305 (let (tags)
6306 (setq org-agenda-filter filter
6307 org-agenda-filter-form (org-agenda-filter-make-matcher))
6308 (org-agenda-set-mode-name)
6309 (save-excursion
6310 (goto-char (point-min))
6311 (while (not (eobp))
6312 (if (org-get-at-bol 'org-marker)
6313 (progn
6314 (setq tags (org-get-at-bol 'tags)) ; used in eval
6315 (if (not (eval org-agenda-filter-form))
6316 (org-agenda-filter-by-tag-hide-line))
6317 (beginning-of-line 2))
6318 (beginning-of-line 2))))
6319 (if (get-char-property (point) 'invisible)
6320 (org-agenda-previous-line))))
6322 (defun org-agenda-filter-by-tag-hide-line ()
6323 (let (ov)
6324 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
6325 (point-at-eol)))
6326 (overlay-put ov 'invisible t)
6327 (overlay-put ov 'type 'tags-filter)
6328 (push ov org-agenda-filter-overlays)))
6330 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
6331 (setq pos (or pos (point)))
6332 (save-excursion
6333 (dolist (ov (overlays-at pos))
6334 (when (and (overlay-get ov 'invisible)
6335 (eq (overlay-get ov 'type) 'tags-filter))
6336 (goto-char pos)
6337 (if (< (overlay-start ov) (point-at-eol))
6338 (move-overlay ov (point-at-eol)
6339 (overlay-end ov)))))))
6341 (defun org-agenda-filter-by-tag-show-all ()
6342 (mapc 'delete-overlay org-agenda-filter-overlays)
6343 (setq org-agenda-filter-overlays nil)
6344 (setq org-agenda-filter nil)
6345 (setq org-agenda-filter-form nil)
6346 (org-agenda-set-mode-name))
6348 (defun org-agenda-manipulate-query-add ()
6349 "Manipulate the query by adding a search term with positive selection.
6350 Positive selection means the term must be matched for selection of an entry."
6351 (interactive)
6352 (org-agenda-manipulate-query ?\[))
6353 (defun org-agenda-manipulate-query-subtract ()
6354 "Manipulate the query by adding a search term with negative selection.
6355 Negative selection means term must not be matched for selection of an entry."
6356 (interactive)
6357 (org-agenda-manipulate-query ?\]))
6358 (defun org-agenda-manipulate-query-add-re ()
6359 "Manipulate the query by adding a search regexp with positive selection.
6360 Positive selection means the regexp must match for selection of an entry."
6361 (interactive)
6362 (org-agenda-manipulate-query ?\{))
6363 (defun org-agenda-manipulate-query-subtract-re ()
6364 "Manipulate the query by adding a search regexp with negative selection.
6365 Negative selection means regexp must not match for selection of an entry."
6366 (interactive)
6367 (org-agenda-manipulate-query ?\}))
6368 (defun org-agenda-manipulate-query (char)
6369 (cond
6370 ((memq org-agenda-type '(timeline agenda))
6371 (let ((org-agenda-include-inactive-timestamps t))
6372 (org-agenda-redo))
6373 (message "Display now includes inactive timestamps as well"))
6374 ((eq org-agenda-type 'search)
6375 (org-add-to-string
6376 'org-agenda-query-string
6377 (if org-agenda-last-search-view-search-was-boolean
6378 (cdr (assoc char '((?\[ . " +") (?\] . " -")
6379 (?\{ . " +{}") (?\} . " -{}"))))
6380 " "))
6381 (setq org-agenda-redo-command
6382 (list 'org-search-view
6383 org-todo-only
6384 org-agenda-query-string
6385 (+ (length org-agenda-query-string)
6386 (if (member char '(?\{ ?\})) 0 1))))
6387 (set-register org-agenda-query-register org-agenda-query-string)
6388 (org-agenda-redo))
6389 (t (error "Cannot manipulate query for %s-type agenda buffers"
6390 org-agenda-type))))
6392 (defun org-add-to-string (var string)
6393 (set var (concat (symbol-value var) string)))
6395 (defun org-agenda-goto-date (date)
6396 "Jump to DATE in agenda."
6397 (interactive (list (let ((org-read-date-prefer-future
6398 (eval org-agenda-jump-prefer-future)))
6399 (org-read-date))))
6400 (org-agenda-list nil date))
6402 (defun org-agenda-goto-today ()
6403 "Go to today."
6404 (interactive)
6405 (org-agenda-check-type t 'timeline 'agenda)
6406 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
6407 (cond
6408 (tdpos (goto-char tdpos))
6409 ((eq org-agenda-type 'agenda)
6410 (let* ((sd (org-agenda-compute-starting-span
6411 (org-today) (or org-agenda-current-span org-agenda-ndays org-agenda-span)))
6412 (org-agenda-overriding-arguments org-agenda-last-arguments))
6413 (setf (nth 1 org-agenda-overriding-arguments) sd)
6414 (org-agenda-redo)
6415 (org-agenda-find-same-or-today-or-agenda)))
6416 (t (error "Cannot find today")))))
6418 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
6419 (goto-char
6420 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
6421 (text-property-any (point-min) (point-max) 'org-today t)
6422 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
6423 (point-min))))
6425 (defun org-agenda-later (arg)
6426 "Go forward in time by thee current span.
6427 With prefix ARG, go forward that many times the current span."
6428 (interactive "p")
6429 (org-agenda-check-type t 'agenda)
6430 (let* ((span org-agenda-current-span)
6431 (sd org-starting-day)
6432 (greg (calendar-gregorian-from-absolute sd))
6433 (cnt (org-get-at-bol 'org-day-cnt))
6434 greg2)
6435 (cond
6436 ((eq span 'day)
6437 (setq sd (+ arg sd)))
6438 ((eq span 'week)
6439 (setq sd (+ (* 7 arg) sd)))
6440 ((eq span 'month)
6441 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
6442 sd (calendar-absolute-from-gregorian greg2))
6443 (setcar greg2 (1+ (car greg2))))
6444 ((eq span 'year)
6445 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
6446 sd (calendar-absolute-from-gregorian greg2))
6447 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
6449 (setq sd (+ (* span arg) sd))))
6450 (let ((org-agenda-overriding-arguments
6451 (list (car org-agenda-last-arguments) sd span t)))
6452 (org-agenda-redo)
6453 (org-agenda-find-same-or-today-or-agenda cnt))))
6455 (defun org-agenda-earlier (arg)
6456 "Go backward in time by the current span.
6457 With prefix ARG, go backward that many times the current span."
6458 (interactive "p")
6459 (org-agenda-later (- arg)))
6461 (defun org-agenda-view-mode-dispatch ()
6462 "Call one of the view mode commands."
6463 (interactive)
6464 (message "View: [d]ay [w]eek [m]onth [y]ear [SPC]reset [q]uit/abort
6465 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
6466 [a]rch-trees [A]rch-files clock[R]eport include[D]iary
6467 [E]ntryText")
6468 (let ((a (read-char-exclusive)))
6469 (case a
6470 (?\ (call-interactively 'org-agenda-reset-view))
6471 (?d (call-interactively 'org-agenda-day-view))
6472 (?w (call-interactively 'org-agenda-week-view))
6473 (?m (call-interactively 'org-agenda-month-view))
6474 (?y (call-interactively 'org-agenda-year-view))
6475 (?l (call-interactively 'org-agenda-log-mode))
6476 (?L (org-agenda-log-mode '(4)))
6477 (?c (org-agenda-log-mode 'clockcheck))
6478 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
6479 (?a (call-interactively 'org-agenda-archives-mode))
6480 (?A (org-agenda-archives-mode 'files))
6481 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
6482 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
6483 (?G (call-interactively 'org-agenda-toggle-time-grid))
6484 (?D (call-interactively 'org-agenda-toggle-diary))
6485 (?\! (call-interactively 'org-agenda-toggle-deadlines))
6486 (?\[ (let ((org-agenda-include-inactive-timestamps t))
6487 (org-agenda-check-type t 'timeline 'agenda)
6488 (org-agenda-redo))
6489 (message "Display now includes inactive timestamps as well"))
6490 (?q (message "Abort"))
6491 (otherwise (error "Invalid key" )))))
6493 (defun org-agenda-reset-view ()
6494 "Switch to default view for agenda."
6495 (interactive)
6496 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
6497 (defun org-agenda-day-view (&optional day-of-year)
6498 "Switch to daily view for agenda.
6499 With argument DAY-OF-YEAR, switch to that day of the year."
6500 (interactive "P")
6501 (org-agenda-change-time-span 'day day-of-year))
6502 (defun org-agenda-week-view (&optional iso-week)
6503 "Switch to daily view for agenda.
6504 With argument ISO-WEEK, switch to the corresponding ISO week.
6505 If ISO-WEEK has more then 2 digits, only the last two encode the
6506 week. Any digits before this encode a year. So 200712 means
6507 week 12 of year 2007. Years in the range 1938-2037 can also be
6508 written as 2-digit years."
6509 (interactive "P")
6510 (org-agenda-change-time-span 'week iso-week))
6511 (defun org-agenda-month-view (&optional month)
6512 "Switch to monthly view for agenda.
6513 With argument MONTH, switch to that month."
6514 (interactive "P")
6515 (org-agenda-change-time-span 'month month))
6516 (defun org-agenda-year-view (&optional year)
6517 "Switch to yearly view for agenda.
6518 With argument YEAR, switch to that year.
6519 If MONTH has more then 2 digits, only the last two encode the
6520 month. Any digits before this encode a year. So 200712 means
6521 December year 2007. Years in the range 1938-2037 can also be
6522 written as 2-digit years."
6523 (interactive "P")
6524 (when year
6525 (setq year (org-small-year-to-year year)))
6526 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
6527 (org-agenda-change-time-span 'year year)
6528 (error "Abort")))
6530 (defun org-agenda-change-time-span (span &optional n)
6531 "Change the agenda view to SPAN.
6532 SPAN may be `day', `week', `month', `year'."
6533 (org-agenda-check-type t 'agenda)
6534 (if (and (not n) (equal org-agenda-current-span span))
6535 (error "Viewing span is already \"%s\"" span))
6536 (let* ((sd (or (org-get-at-bol 'day)
6537 org-starting-day))
6538 (sd (org-agenda-compute-starting-span sd span n))
6539 (org-agenda-overriding-arguments
6540 (or org-agenda-overriding-arguments
6541 (list (car org-agenda-last-arguments) sd span t))))
6542 (org-agenda-redo)
6543 (org-agenda-find-same-or-today-or-agenda))
6544 (org-agenda-set-mode-name)
6545 (message "Switched to %s view" span))
6547 (defun org-agenda-compute-starting-span (sd span &optional n)
6548 "Compute starting date for agenda.
6549 SPAN may be `day', `week', `month', `year'. The return value
6550 is a cons cell with the starting date and the number of days,
6551 so that the date SD will be in that range."
6552 (let* ((greg (calendar-gregorian-from-absolute sd))
6553 (dg (nth 1 greg))
6554 (mg (car greg))
6555 (yg (nth 2 greg)))
6556 (cond
6557 ((eq span 'day)
6558 (when n
6559 (setq sd (+ (calendar-absolute-from-gregorian
6560 (list mg 1 yg))
6561 n -1))))
6562 ((eq span 'week)
6563 (let* ((nt (calendar-day-of-week
6564 (calendar-gregorian-from-absolute sd)))
6565 (d (if org-agenda-start-on-weekday
6566 (- nt org-agenda-start-on-weekday)
6569 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
6570 (when n
6571 (require 'cal-iso)
6572 (when (> n 99)
6573 (setq y1 (org-small-year-to-year (/ n 100))
6574 n (mod n 100)))
6575 (setq sd
6576 (calendar-absolute-from-iso
6577 (list n 1
6578 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
6579 ((eq span 'month)
6580 (let (y1)
6581 (when (and n (> n 99))
6582 (setq y1 (org-small-year-to-year (/ n 100))
6583 n (mod n 100)))
6584 (setq sd (calendar-absolute-from-gregorian
6585 (list (or n mg) 1 (or y1 yg))))))
6586 ((eq span 'year)
6587 (setq sd (calendar-absolute-from-gregorian
6588 (list 1 1 (or n yg))))))
6589 sd))
6591 (defun org-agenda-next-date-line (&optional arg)
6592 "Jump to the next line indicating a date in agenda buffer."
6593 (interactive "p")
6594 (org-agenda-check-type t 'agenda 'timeline)
6595 (beginning-of-line 1)
6596 ;; This does not work if user makes date format that starts with a blank
6597 (if (looking-at "^\\S-") (forward-char 1))
6598 (if (not (re-search-forward "^\\S-" nil t arg))
6599 (progn
6600 (backward-char 1)
6601 (error "No next date after this line in this buffer")))
6602 (goto-char (match-beginning 0)))
6604 (defun org-agenda-previous-date-line (&optional arg)
6605 "Jump to the previous line indicating a date in agenda buffer."
6606 (interactive "p")
6607 (org-agenda-check-type t 'agenda 'timeline)
6608 (beginning-of-line 1)
6609 (if (not (re-search-backward "^\\S-" nil t arg))
6610 (error "No previous date before this line in this buffer")))
6612 ;; Initialize the highlight
6613 (defvar org-hl (make-overlay 1 1))
6614 (overlay-put org-hl 'face 'highlight)
6616 (defun org-highlight (begin end &optional buffer)
6617 "Highlight a region with overlay."
6618 (move-overlay org-hl begin end (or buffer (current-buffer))))
6620 (defun org-unhighlight ()
6621 "Detach overlay INDEX."
6622 (org-detach-overlay org-hl))
6624 ;; FIXME this is currently not used.
6625 (defun org-highlight-until-next-command (beg end &optional buffer)
6626 "Move the highlight overlay to BEG/END, remove it before the next command."
6627 (org-highlight beg end buffer)
6628 (add-hook 'pre-command-hook 'org-unhighlight-once))
6629 (defun org-unhighlight-once ()
6630 "Remove the highlight from its position, and this function from the hook."
6631 (remove-hook 'pre-command-hook 'org-unhighlight-once)
6632 (org-unhighlight))
6634 (defun org-agenda-follow-mode ()
6635 "Toggle follow mode in an agenda buffer."
6636 (interactive)
6637 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
6638 (org-agenda-set-mode-name)
6639 (org-agenda-do-context-action)
6640 (message "Follow mode is %s"
6641 (if org-agenda-follow-mode "on" "off")))
6643 (defun org-agenda-entry-text-mode (&optional arg)
6644 "Toggle entry text mode in an agenda buffer."
6645 (interactive "P")
6646 (setq org-agenda-entry-text-mode (or (integerp arg)
6647 (not org-agenda-entry-text-mode)))
6648 (org-agenda-entry-text-hide)
6649 (and org-agenda-entry-text-mode
6650 (let ((org-agenda-entry-text-maxlines
6651 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6652 (org-agenda-entry-text-show)))
6653 (org-agenda-set-mode-name)
6654 (message "Entry text mode is %s. Maximum number of lines is %d"
6655 (if org-agenda-entry-text-mode "on" "off")
6656 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6658 (defun org-agenda-clockreport-mode (&optional with-filter)
6659 "Toggle clocktable mode in an agenda buffer.
6660 With prefix arg WITH-FILTER, make the clocktable respect the current
6661 agenda filter."
6662 (interactive "P")
6663 (org-agenda-check-type t 'agenda)
6664 (if with-filter
6665 (setq org-agenda-clockreport-mode 'with-filter)
6666 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
6667 (org-agenda-set-mode-name)
6668 (org-agenda-redo)
6669 (message "Clocktable mode is %s"
6670 (if org-agenda-clockreport-mode "on" "off")))
6672 (defun org-agenda-log-mode (&optional special)
6673 "Toggle log mode in an agenda buffer.
6674 With argument SPECIAL, show all possible log items, not only the ones
6675 configured in `org-agenda-log-mode-items'.
6676 With a double `C-u' prefix arg, show *only* log items, nothing else."
6677 (interactive "P")
6678 (org-agenda-check-type t 'agenda 'timeline)
6679 (setq org-agenda-show-log
6680 (cond
6681 ((equal special '(16)) 'only)
6682 ((eq special 'clockcheck)
6683 (if (eq org-agenda-show-log 'clockcheck)
6684 nil 'clockcheck))
6685 (special '(closed clock state))
6686 (t (not org-agenda-show-log))))
6687 (org-agenda-set-mode-name)
6688 (org-agenda-redo)
6689 (message "Log mode is %s"
6690 (if org-agenda-show-log "on" "off")))
6692 (defun org-agenda-archives-mode (&optional with-files)
6693 "Toggle inclusion of items in trees marked with :ARCHIVE:.
6694 When called with a prefix argument, include all archive files as well."
6695 (interactive "P")
6696 (setq org-agenda-archives-mode
6697 (if with-files t (if org-agenda-archives-mode nil 'trees)))
6698 (org-agenda-set-mode-name)
6699 (org-agenda-redo)
6700 (message
6701 "%s"
6702 (cond
6703 ((eq org-agenda-archives-mode nil)
6704 "No archives are included")
6705 ((eq org-agenda-archives-mode 'trees)
6706 (format "Trees with :%s: tag are included" org-archive-tag))
6707 ((eq org-agenda-archives-mode t)
6708 (format "Trees with :%s: tag and all active archive files are included"
6709 org-archive-tag)))))
6711 (defun org-agenda-toggle-diary ()
6712 "Toggle diary inclusion in an agenda buffer."
6713 (interactive)
6714 (org-agenda-check-type t 'agenda)
6715 (setq org-agenda-include-diary (not org-agenda-include-diary))
6716 (org-agenda-redo)
6717 (org-agenda-set-mode-name)
6718 (message "Diary inclusion turned %s"
6719 (if org-agenda-include-diary "on" "off")))
6721 (defun org-agenda-toggle-deadlines ()
6722 "Toggle inclusion of entries with a deadline in an agenda buffer."
6723 (interactive)
6724 (org-agenda-check-type t 'agenda)
6725 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
6726 (org-agenda-redo)
6727 (org-agenda-set-mode-name)
6728 (message "Deadlines inclusion turned %s"
6729 (if org-agenda-include-deadlines "on" "off")))
6731 (defun org-agenda-toggle-time-grid ()
6732 "Toggle time grid in an agenda buffer."
6733 (interactive)
6734 (org-agenda-check-type t 'agenda)
6735 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
6736 (org-agenda-redo)
6737 (org-agenda-set-mode-name)
6738 (message "Time-grid turned %s"
6739 (if org-agenda-use-time-grid "on" "off")))
6741 (defun org-agenda-set-mode-name ()
6742 "Set the mode name to indicate all the small mode settings."
6743 (setq mode-name
6744 (list "Org-Agenda"
6745 (if (get 'org-agenda-files 'org-restrict) " []" "")
6747 '(:eval (org-agenda-span-name org-agenda-current-span))
6748 (if org-agenda-follow-mode " Follow" "")
6749 (if org-agenda-entry-text-mode " ETxt" "")
6750 (if org-agenda-include-diary " Diary" "")
6751 (if org-agenda-include-deadlines " Ddl" "")
6752 (if org-agenda-use-time-grid " Grid" "")
6753 (if (and (boundp 'org-habit-show-habits)
6754 org-habit-show-habits) " Habit" "")
6755 (cond
6756 ((consp org-agenda-show-log) " LogAll")
6757 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
6758 (org-agenda-show-log " Log")
6759 (t ""))
6760 ;; show tags used for filtering in a custom face
6761 (if (or org-agenda-filter (get 'org-agenda-filter
6762 :preset-filter))
6763 '(:eval (org-propertize
6764 (concat " {"
6765 (mapconcat
6766 'identity
6767 (append
6768 (get 'org-agenda-filter :preset-filter)
6769 org-agenda-filter)
6771 "}")
6772 'face 'org-agenda-filter-tags
6773 'help-echo "Tags used in filtering"))
6775 (if org-agenda-archives-mode
6776 (if (eq org-agenda-archives-mode t)
6777 " Archives"
6778 (format " :%s:" org-archive-tag))
6780 (if org-agenda-clockreport-mode
6781 (if (eq org-agenda-clockreport-mode 'with-filter)
6782 " Clock{}" " Clock")
6783 "")))
6784 (force-mode-line-update))
6786 (defun org-agenda-post-command-hook ()
6787 (setq org-agenda-type
6788 (or (get-text-property (point) 'org-agenda-type)
6789 (get-text-property (max (point-min) (1- (point)))
6790 'org-agenda-type))))
6792 (defun org-agenda-next-line ()
6793 "Move cursor to the next line, and show if follow mode is active."
6794 (interactive)
6795 (call-interactively 'next-line)
6796 (org-agenda-do-context-action))
6798 (defun org-agenda-previous-line ()
6799 "Move cursor to the previous line, and show if follow-mode is active."
6800 (interactive)
6801 (call-interactively 'previous-line)
6802 (org-agenda-do-context-action))
6804 (defun org-agenda-do-context-action ()
6805 "Show outline path and, maybe, follow mode window."
6806 (let ((m (org-get-at-bol 'org-marker)))
6807 (when (and (markerp m) (marker-buffer m))
6808 (and org-agenda-follow-mode
6809 (if org-agenda-follow-indirect
6810 (org-agenda-tree-to-indirect-buffer)
6811 (org-agenda-show)))
6812 (and org-agenda-show-outline-path
6813 (org-with-point-at m (org-display-outline-path t))))))
6815 (defun org-agenda-show-priority ()
6816 "Show the priority of the current item.
6817 This priority is composed of the main priority given with the [#A] cookies,
6818 and by additional input from the age of a schedules or deadline entry."
6819 (interactive)
6820 (let* ((pri (org-get-at-bol 'priority)))
6821 (message "Priority is %d" (if pri pri -1000))))
6823 (defun org-agenda-show-tags ()
6824 "Show the tags applicable to the current item."
6825 (interactive)
6826 (let* ((tags (org-get-at-bol 'tags)))
6827 (if tags
6828 (message "Tags are :%s:"
6829 (org-no-properties (mapconcat 'identity tags ":")))
6830 (message "No tags associated with this line"))))
6832 (defun org-agenda-goto (&optional highlight)
6833 "Go to the Org-mode file which contains the item at point."
6834 (interactive)
6835 (let* ((marker (or (org-get-at-bol 'org-marker)
6836 (org-agenda-error)))
6837 (buffer (marker-buffer marker))
6838 (pos (marker-position marker)))
6839 (switch-to-buffer-other-window buffer)
6840 (widen)
6841 (push-mark)
6842 (goto-char pos)
6843 (when (eq major-mode 'org-mode)
6844 (org-show-context 'agenda)
6845 (save-excursion
6846 (and (outline-next-heading)
6847 (org-flag-heading nil)))) ; show the next heading
6848 (when (outline-invisible-p)
6849 (show-entry)) ; display invisible text
6850 (recenter (/ (window-height) 2))
6851 (run-hooks 'org-agenda-after-show-hook)
6852 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
6854 (defvar org-agenda-after-show-hook nil
6855 "Normal hook run after an item has been shown from the agenda.
6856 Point is in the buffer where the item originated.")
6858 (defun org-agenda-kill ()
6859 "Kill the entry or subtree belonging to the current agenda entry."
6860 (interactive)
6861 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
6862 (let* ((marker (or (org-get-at-bol 'org-marker)
6863 (org-agenda-error)))
6864 (buffer (marker-buffer marker))
6865 (pos (marker-position marker))
6866 (type (org-get-at-bol 'type))
6867 dbeg dend (n 0) conf)
6868 (org-with-remote-undo buffer
6869 (with-current-buffer buffer
6870 (save-excursion
6871 (goto-char pos)
6872 (if (and (eq major-mode 'org-mode) (not (member type '("sexp"))))
6873 (setq dbeg (progn (org-back-to-heading t) (point))
6874 dend (org-end-of-subtree t t))
6875 (setq dbeg (point-at-bol)
6876 dend (min (point-max) (1+ (point-at-eol)))))
6877 (goto-char dbeg)
6878 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
6879 (setq conf (or (eq t org-agenda-confirm-kill)
6880 (and (numberp org-agenda-confirm-kill)
6881 (> n org-agenda-confirm-kill))))
6882 (and conf
6883 (not (y-or-n-p
6884 (format "Delete entry with %d lines in buffer \"%s\"? "
6885 n (buffer-name buffer))))
6886 (error "Abort"))
6887 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
6888 (with-current-buffer buffer (delete-region dbeg dend))
6889 (message "Agenda item and source killed"))))
6891 (defvar org-archive-default-command)
6892 (defun org-agenda-archive-default ()
6893 "Archive the entry or subtree belonging to the current agenda entry."
6894 (interactive)
6895 (require 'org-archive)
6896 (org-agenda-archive-with org-archive-default-command))
6898 (defun org-agenda-archive-default-with-confirmation ()
6899 "Archive the entry or subtree belonging to the current agenda entry."
6900 (interactive)
6901 (require 'org-archive)
6902 (org-agenda-archive-with org-archive-default-command 'confirm))
6904 (defun org-agenda-archive ()
6905 "Archive the entry or subtree belonging to the current agenda entry."
6906 (interactive)
6907 (org-agenda-archive-with 'org-archive-subtree))
6909 (defun org-agenda-archive-to-archive-sibling ()
6910 "Move the entry to the archive sibling."
6911 (interactive)
6912 (org-agenda-archive-with 'org-archive-to-archive-sibling))
6914 (defun org-agenda-archive-with (cmd &optional confirm)
6915 "Move the entry to the archive sibling."
6916 (interactive)
6917 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
6918 (let* ((marker (or (org-get-at-bol 'org-marker)
6919 (org-agenda-error)))
6920 (buffer (marker-buffer marker))
6921 (pos (marker-position marker)))
6922 (org-with-remote-undo buffer
6923 (with-current-buffer buffer
6924 (if (eq major-mode 'org-mode)
6925 (if (and confirm
6926 (not (y-or-n-p "Archive this subtree or entry? ")))
6927 (error "Abort")
6928 (save-excursion
6929 (goto-char pos)
6930 (org-remove-subtree-entries-from-agenda)
6931 (org-back-to-heading t)
6932 (funcall cmd)))
6933 (error "Archiving works only in Org-mode files"))))))
6935 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
6936 "Remove all lines in the agenda that correspond to a given subtree.
6937 The subtree is the one in buffer BUF, starting at BEG and ending at END.
6938 If this information is not given, the function uses the tree at point."
6939 (let ((buf (or buf (current-buffer))) m p)
6940 (save-excursion
6941 (unless (and beg end)
6942 (org-back-to-heading t)
6943 (setq beg (point))
6944 (org-end-of-subtree t)
6945 (setq end (point)))
6946 (set-buffer (get-buffer org-agenda-buffer-name))
6947 (save-excursion
6948 (goto-char (point-max))
6949 (beginning-of-line 1)
6950 (while (not (bobp))
6951 (when (and (setq m (org-get-at-bol 'org-marker))
6952 (equal buf (marker-buffer m))
6953 (setq p (marker-position m))
6954 (>= p beg)
6955 (< p end))
6956 (let ((inhibit-read-only t))
6957 (delete-region (point-at-bol) (1+ (point-at-eol)))))
6958 (beginning-of-line 0))))))
6960 (defun org-agenda-refile (&optional goto rfloc no-update)
6961 "Refile the item at point."
6962 (interactive "P")
6963 (if (equal goto '(16))
6964 (org-refile-goto-last-stored)
6965 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
6966 (org-agenda-error)))
6967 (buffer (marker-buffer marker))
6968 (pos (marker-position marker))
6969 (rfloc (or rfloc
6970 (org-refile-get-location
6971 (if goto "Goto" "Refile to") buffer
6972 org-refile-allow-creating-parent-nodes))))
6973 (with-current-buffer buffer
6974 (save-excursion
6975 (save-restriction
6976 (widen)
6977 (goto-char marker)
6978 (org-remove-subtree-entries-from-agenda)
6979 (org-refile goto buffer rfloc)))))
6980 (unless no-update (org-agenda-redo))))
6982 (defun org-agenda-open-link (&optional arg)
6983 "Follow the link in the current line, if any.
6984 This looks for a link in the displayed line in the agenda. It also looks
6985 at the text of the entry itself."
6986 (interactive "P")
6987 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
6988 (org-get-at-bol 'org-marker)))
6989 (buffer (and marker (marker-buffer marker)))
6990 (prefix (buffer-substring
6991 (point-at-bol) (point-at-eol))))
6992 (cond
6993 (buffer
6994 (with-current-buffer buffer
6995 (save-excursion
6996 (save-restriction
6997 (widen)
6998 (goto-char marker)
6999 (org-offer-links-in-entry arg prefix)))))
7000 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
7001 (save-excursion
7002 (beginning-of-line 1)
7003 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
7004 (org-open-link-from-string (match-string 1)))
7005 (t (error "No link to open here")))))
7007 (defun org-agenda-copy-local-variable (var)
7008 "Get a variable from a referenced buffer and install it here."
7009 (let ((m (org-get-at-bol 'org-marker)))
7010 (when (and m (buffer-live-p (marker-buffer m)))
7011 (org-set-local var (with-current-buffer (marker-buffer m)
7012 (symbol-value var))))))
7014 (defun org-agenda-switch-to (&optional delete-other-windows)
7015 "Go to the Org-mode file which contains the item at point."
7016 (interactive)
7017 (if (and org-return-follows-link
7018 (not (org-get-at-bol 'org-marker))
7019 (org-in-regexp org-bracket-link-regexp))
7020 (org-open-link-from-string (match-string 0))
7021 (let* ((marker (or (org-get-at-bol 'org-marker)
7022 (org-agenda-error)))
7023 (buffer (marker-buffer marker))
7024 (pos (marker-position marker)))
7025 (org-pop-to-buffer-same-window buffer)
7026 (and delete-other-windows (delete-other-windows))
7027 (widen)
7028 (goto-char pos)
7029 (when (eq major-mode 'org-mode)
7030 (org-show-context 'agenda)
7031 (save-excursion
7032 (and (outline-next-heading)
7033 (org-flag-heading nil))) ; show the next heading
7034 (when (outline-invisible-p)
7035 (show-entry)))))) ; display invisible text
7037 (defun org-agenda-goto-mouse (ev)
7038 "Go to the Org-mode file which contains the item at the mouse click."
7039 (interactive "e")
7040 (mouse-set-point ev)
7041 (org-agenda-goto))
7043 (defun org-agenda-show (&optional full-entry)
7044 "Display the Org-mode file which contains the item at point.
7045 With prefix argument FULL-ENTRY, make the entire entry visible
7046 if it was hidden in the outline."
7047 (interactive "P")
7048 (let ((win (selected-window)))
7049 (if full-entry
7050 (let ((org-show-entry-below t))
7051 (org-agenda-goto t))
7052 (org-agenda-goto t))
7053 (select-window win)))
7055 (defvar org-agenda-show-window nil)
7056 (defun org-agenda-show-and-scroll-up ()
7057 "Display the Org-mode file which contains the item at point.
7058 When called repeatedly, scroll the window that is displaying the buffer."
7059 (interactive)
7060 (let ((win (selected-window)))
7061 (if (and (window-live-p org-agenda-show-window)
7062 (eq this-command last-command))
7063 (progn
7064 (select-window org-agenda-show-window)
7065 (ignore-errors (scroll-up)))
7066 (org-agenda-goto t)
7067 (show-subtree)
7068 (setq org-agenda-show-window (selected-window)))
7069 (select-window win)))
7071 (defun org-agenda-show-scroll-down ()
7072 "Scroll down the window showing the agenda."
7073 (interactive)
7074 (let ((win (selected-window)))
7075 (when (window-live-p org-agenda-show-window)
7076 (select-window org-agenda-show-window)
7077 (ignore-errors (scroll-down))
7078 (select-window win))))
7080 (defun org-agenda-show-1 (&optional more)
7081 "Display the Org-mode file which contains the item at point.
7082 The prefix arg selects the amount of information to display:
7084 0 hide the subtree
7085 1 just show the entry according to defaults.
7086 2 show the children view
7087 3 show the subtree view
7088 4 show the entire subtree and any LOGBOOK drawers
7089 5 show the entire subtree and any drawers
7090 With prefix argument FULL-ENTRY, make the entire entry visible
7091 if it was hidden in the outline."
7092 (interactive "p")
7093 (let ((win (selected-window)))
7094 (org-agenda-goto t)
7095 (org-recenter-heading 1)
7096 (cond
7097 ((= more 0)
7098 (hide-subtree)
7099 (save-excursion
7100 (org-back-to-heading)
7101 (run-hook-with-args 'org-cycle-hook 'folded))
7102 (message "Remote: FOLDED"))
7103 ((and (org-called-interactively-p 'any) (= more 1))
7104 (message "Remote: show with default settings"))
7105 ((= more 2)
7106 (show-entry)
7107 (show-children)
7108 (save-excursion
7109 (org-back-to-heading)
7110 (run-hook-with-args 'org-cycle-hook 'children))
7111 (message "Remote: CHILDREN"))
7112 ((= more 3)
7113 (show-subtree)
7114 (save-excursion
7115 (org-back-to-heading)
7116 (run-hook-with-args 'org-cycle-hook 'subtree))
7117 (message "Remote: SUBTREE"))
7118 ((= more 4)
7119 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
7120 (org-drawer-regexp
7121 (concat "^[ \t]*:\\("
7122 (mapconcat 'regexp-quote org-drawers "\\|")
7123 "\\):[ \t]*$")))
7124 (show-subtree)
7125 (save-excursion
7126 (org-back-to-heading)
7127 (org-cycle-hide-drawers 'subtree)))
7128 (message "Remote: SUBTREE AND LOGBOOK"))
7129 ((> more 4)
7130 (show-subtree)
7131 (message "Remote: SUBTREE AND ALL DRAWERS")))
7132 (select-window win)))
7134 (defun org-recenter-heading (n)
7135 (save-excursion
7136 (org-back-to-heading)
7137 (recenter n)))
7139 (defvar org-agenda-cycle-counter nil)
7140 (defun org-agenda-cycle-show (&optional n)
7141 "Show the current entry in another window, with default settings.
7142 Default settings are taken from `org-show-hierarchy-above' and siblings.
7143 When use repeatedly in immediate succession, the remote entry will cycle
7144 through visibility
7146 children -> subtree -> folded
7148 When called with a numeric prefix arg, that arg will be passed through to
7149 `org-agenda-show-1'. For the interpretation of that argument, see the
7150 docstring of `org-agenda-show-1'."
7151 (interactive "P")
7152 (if (integerp n)
7153 (setq org-agenda-cycle-counter n)
7154 (if (not (eq last-command this-command))
7155 (setq org-agenda-cycle-counter 1)
7156 (if (equal org-agenda-cycle-counter 0)
7157 (setq org-agenda-cycle-counter 2)
7158 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
7159 (if (> org-agenda-cycle-counter 3)
7160 (setq org-agenda-cycle-counter 0)))))
7161 (org-agenda-show-1 org-agenda-cycle-counter))
7163 (defun org-agenda-recenter (arg)
7164 "Display the Org-mode file which contains the item at point and recenter."
7165 (interactive "P")
7166 (let ((win (selected-window)))
7167 (org-agenda-goto t)
7168 (recenter arg)
7169 (select-window win)))
7171 (defun org-agenda-show-mouse (ev)
7172 "Display the Org-mode file which contains the item at the mouse click."
7173 (interactive "e")
7174 (mouse-set-point ev)
7175 (org-agenda-show))
7177 (defun org-agenda-check-no-diary ()
7178 "Check if the entry is a diary link and abort if yes."
7179 (if (org-get-at-bol 'org-agenda-diary-link)
7180 (org-agenda-error)))
7182 (defun org-agenda-error ()
7183 (error "Command not allowed in this line"))
7185 (defun org-agenda-tree-to-indirect-buffer ()
7186 "Show the subtree corresponding to the current entry in an indirect buffer.
7187 This calls the command `org-tree-to-indirect-buffer' from the original
7188 Org-mode buffer.
7189 With numerical prefix arg ARG, go up to this level and then take that tree.
7190 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
7191 use the dedicated frame)."
7192 (interactive)
7193 (if (and current-prefix-arg (listp current-prefix-arg))
7194 (org-agenda-do-tree-to-indirect-buffer)
7195 (let ((agenda-window (selected-window))
7196 (indirect-window (get-buffer-window org-last-indirect-buffer)))
7197 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer))
7198 (unwind-protect
7199 (progn
7200 (unless indirect-window
7201 (setq indirect-window (split-window agenda-window)))
7202 (select-window indirect-window)
7203 (switch-to-buffer org-last-indirect-buffer :norecord)
7204 (fit-window-to-buffer indirect-window))
7205 (select-window agenda-window)))))
7207 (defun org-agenda-do-tree-to-indirect-buffer ()
7208 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
7209 (org-agenda-check-no-diary)
7210 (let* ((marker (or (org-get-at-bol 'org-marker)
7211 (org-agenda-error)))
7212 (buffer (marker-buffer marker))
7213 (pos (marker-position marker)))
7214 (with-current-buffer buffer
7215 (save-excursion
7216 (goto-char pos)
7217 (call-interactively 'org-tree-to-indirect-buffer)))))
7219 (defvar org-last-heading-marker (make-marker)
7220 "Marker pointing to the headline that last changed its TODO state
7221 by a remote command from the agenda.")
7223 (defun org-agenda-todo-nextset ()
7224 "Switch TODO entry to next sequence."
7225 (interactive)
7226 (org-agenda-todo 'nextset))
7228 (defun org-agenda-todo-previousset ()
7229 "Switch TODO entry to previous sequence."
7230 (interactive)
7231 (org-agenda-todo 'previousset))
7233 (defun org-agenda-todo (&optional arg)
7234 "Cycle TODO state of line at point, also in Org-mode file.
7235 This changes the line at point, all other lines in the agenda referring to
7236 the same tree node, and the headline of the tree node in the Org-mode file."
7237 (interactive "P")
7238 (org-agenda-check-no-diary)
7239 (let* ((col (current-column))
7240 (marker (or (org-get-at-bol 'org-marker)
7241 (org-agenda-error)))
7242 (buffer (marker-buffer marker))
7243 (pos (marker-position marker))
7244 (hdmarker (org-get-at-bol 'org-hd-marker))
7245 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
7246 (inhibit-read-only t)
7247 org-agenda-headline-snapshot-before-repeat newhead just-one)
7248 (org-with-remote-undo buffer
7249 (with-current-buffer buffer
7250 (widen)
7251 (goto-char pos)
7252 (org-show-context 'agenda)
7253 (save-excursion
7254 (and (outline-next-heading)
7255 (org-flag-heading nil))) ; show the next heading
7256 (let ((current-prefix-arg arg))
7257 (call-interactively 'org-todo))
7258 (and (bolp) (forward-char 1))
7259 (setq newhead (org-get-heading))
7260 (when (and (org-bound-and-true-p
7261 org-agenda-headline-snapshot-before-repeat)
7262 (not (equal org-agenda-headline-snapshot-before-repeat
7263 newhead))
7264 todayp)
7265 (setq newhead org-agenda-headline-snapshot-before-repeat
7266 just-one t))
7267 (save-excursion
7268 (org-back-to-heading)
7269 (move-marker org-last-heading-marker (point))))
7270 (beginning-of-line 1)
7271 (save-excursion
7272 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
7273 (org-move-to-column col))))
7275 (defun org-agenda-add-note (&optional arg)
7276 "Add a time-stamped note to the entry at point."
7277 (interactive "P")
7278 (org-agenda-check-no-diary)
7279 (let* ((marker (or (org-get-at-bol 'org-marker)
7280 (org-agenda-error)))
7281 (buffer (marker-buffer marker))
7282 (pos (marker-position marker))
7283 (hdmarker (org-get-at-bol 'org-hd-marker))
7284 (inhibit-read-only t))
7285 (with-current-buffer buffer
7286 (widen)
7287 (goto-char pos)
7288 (org-show-context 'agenda)
7289 (save-excursion
7290 (and (outline-next-heading)
7291 (org-flag-heading nil))) ; show the next heading
7292 (org-add-note))))
7294 (defun org-agenda-change-all-lines (newhead hdmarker
7295 &optional fixface just-this)
7296 "Change all lines in the agenda buffer which match HDMARKER.
7297 The new content of the line will be NEWHEAD (as modified by
7298 `org-agenda-format-item'). HDMARKER is checked with
7299 `equal' against all `org-hd-marker' text properties in the file.
7300 If FIXFACE is non-nil, the face of each item is modified according to
7301 the new TODO state.
7302 If JUST-THIS is non-nil, change just the current line, not all.
7303 If FORCE-TAGS is non nil, the car of it returns the new tags."
7304 (let* ((inhibit-read-only t)
7305 (line (org-current-line))
7306 (thetags (with-current-buffer (marker-buffer hdmarker)
7307 (save-excursion (save-restriction (widen)
7308 (goto-char hdmarker)
7309 (org-get-tags-at)))))
7310 props m pl undone-face done-face finish new dotime cat tags)
7311 (save-excursion
7312 (goto-char (point-max))
7313 (beginning-of-line 1)
7314 (while (not finish)
7315 (setq finish (bobp))
7316 (when (and (setq m (org-get-at-bol 'org-hd-marker))
7317 (or (not just-this) (= (org-current-line) line))
7318 (equal m hdmarker))
7319 (setq props (text-properties-at (point))
7320 dotime (org-get-at-bol 'dotime)
7321 cat (org-get-at-bol 'org-category)
7322 tags thetags
7324 (let ((org-prefix-format-compiled
7325 (or (get-text-property (point) 'format)
7326 org-prefix-format-compiled)))
7327 (with-current-buffer (marker-buffer hdmarker)
7328 (save-excursion
7329 (save-restriction
7330 (widen)
7331 (org-agenda-format-item (org-get-at-bol 'extra)
7332 newhead cat tags dotime)))))
7333 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
7334 undone-face (org-get-at-bol 'undone-face)
7335 done-face (org-get-at-bol 'done-face))
7336 (beginning-of-line 1)
7337 (cond
7338 ((equal new "")
7339 (and (looking-at ".*\n?") (replace-match "")))
7340 ((looking-at ".*")
7341 (replace-match new t t)
7342 (beginning-of-line 1)
7343 (add-text-properties (point-at-bol) (point-at-eol) props)
7344 (when fixface
7345 (add-text-properties
7346 (point-at-bol) (point-at-eol)
7347 (list 'face
7348 (if org-last-todo-state-is-todo
7349 undone-face done-face))))
7350 (org-agenda-highlight-todo 'line)
7351 (beginning-of-line 1))
7352 (t (error "Line update did not work"))))
7353 (beginning-of-line 0)))
7354 (org-finalize-agenda)))
7356 (defun org-agenda-align-tags (&optional line)
7357 "Align all tags in agenda items to `org-agenda-tags-column'."
7358 (let ((inhibit-read-only t) l c)
7359 (save-excursion
7360 (goto-char (if line (point-at-bol) (point-min)))
7361 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
7362 (if line (point-at-eol) nil) t)
7363 (add-text-properties
7364 (match-beginning 2) (match-end 2)
7365 (list 'face (delq nil (let ((prop (get-text-property
7366 (match-beginning 2) 'face)))
7367 (or (listp prop) (setq prop (list prop)))
7368 (if (memq 'org-tag prop)
7369 prop
7370 (cons 'org-tag prop))))))
7371 (setq l (- (match-end 2) (match-beginning 2))
7372 c (if (< org-agenda-tags-column 0)
7373 (- (abs org-agenda-tags-column) l)
7374 org-agenda-tags-column))
7375 (delete-region (match-beginning 1) (match-end 1))
7376 (goto-char (match-beginning 1))
7377 (insert (org-add-props
7378 (make-string (max 1 (- c (current-column))) ?\ )
7379 (plist-put (copy-sequence (text-properties-at (point)))
7380 'face nil))))
7381 (goto-char (point-min))
7382 (org-font-lock-add-tag-faces (point-max)))))
7384 (defun org-agenda-priority-up ()
7385 "Increase the priority of line at point, also in Org-mode file."
7386 (interactive)
7387 (org-agenda-priority 'up))
7389 (defun org-agenda-priority-down ()
7390 "Decrease the priority of line at point, also in Org-mode file."
7391 (interactive)
7392 (org-agenda-priority 'down))
7394 (defun org-agenda-priority (&optional force-direction)
7395 "Set the priority of line at point, also in Org-mode file.
7396 This changes the line at point, all other lines in the agenda referring to
7397 the same tree node, and the headline of the tree node in the Org-mode file."
7398 (interactive)
7399 (unless org-enable-priority-commands
7400 (error "Priority commands are disabled"))
7401 (org-agenda-check-no-diary)
7402 (let* ((marker (or (org-get-at-bol 'org-marker)
7403 (org-agenda-error)))
7404 (hdmarker (org-get-at-bol 'org-hd-marker))
7405 (buffer (marker-buffer hdmarker))
7406 (pos (marker-position hdmarker))
7407 (inhibit-read-only t)
7408 newhead)
7409 (org-with-remote-undo buffer
7410 (with-current-buffer buffer
7411 (widen)
7412 (goto-char pos)
7413 (org-show-context 'agenda)
7414 (save-excursion
7415 (and (outline-next-heading)
7416 (org-flag-heading nil))) ; show the next heading
7417 (funcall 'org-priority force-direction)
7418 (end-of-line 1)
7419 (setq newhead (org-get-heading)))
7420 (org-agenda-change-all-lines newhead hdmarker)
7421 (beginning-of-line 1))))
7423 ;; FIXME: should fix the tags property of the agenda line.
7424 (defun org-agenda-set-tags (&optional tag onoff)
7425 "Set tags for the current headline."
7426 (interactive)
7427 (org-agenda-check-no-diary)
7428 (if (and (org-region-active-p) (org-called-interactively-p 'any))
7429 (call-interactively 'org-change-tag-in-region)
7430 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7431 (org-agenda-error)))
7432 (buffer (marker-buffer hdmarker))
7433 (pos (marker-position hdmarker))
7434 (inhibit-read-only t)
7435 newhead)
7436 (org-with-remote-undo buffer
7437 (with-current-buffer buffer
7438 (widen)
7439 (goto-char pos)
7440 (save-excursion
7441 (org-show-context 'agenda))
7442 (save-excursion
7443 (and (outline-next-heading)
7444 (org-flag-heading nil))) ; show the next heading
7445 (goto-char pos)
7446 (if tag
7447 (org-toggle-tag tag onoff)
7448 (call-interactively 'org-set-tags))
7449 (end-of-line 1)
7450 (setq newhead (org-get-heading)))
7451 (org-agenda-change-all-lines newhead hdmarker)
7452 (beginning-of-line 1)))))
7454 (defun org-agenda-set-property ()
7455 "Set a property for the current headline."
7456 (interactive)
7457 (org-agenda-check-no-diary)
7458 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7459 (org-agenda-error)))
7460 (buffer (marker-buffer hdmarker))
7461 (pos (marker-position hdmarker))
7462 (inhibit-read-only t)
7463 newhead)
7464 (org-with-remote-undo buffer
7465 (with-current-buffer buffer
7466 (widen)
7467 (goto-char pos)
7468 (save-excursion
7469 (org-show-context 'agenda))
7470 (save-excursion
7471 (and (outline-next-heading)
7472 (org-flag-heading nil))) ; show the next heading
7473 (goto-char pos)
7474 (call-interactively 'org-set-property)))))
7476 (defun org-agenda-set-effort ()
7477 "Set the effort property for the current headline."
7478 (interactive)
7479 (org-agenda-check-no-diary)
7480 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7481 (org-agenda-error)))
7482 (buffer (marker-buffer hdmarker))
7483 (pos (marker-position hdmarker))
7484 (inhibit-read-only t)
7485 newhead)
7486 (org-with-remote-undo buffer
7487 (with-current-buffer buffer
7488 (widen)
7489 (goto-char pos)
7490 (save-excursion
7491 (org-show-context 'agenda))
7492 (save-excursion
7493 (and (outline-next-heading)
7494 (org-flag-heading nil))) ; show the next heading
7495 (goto-char pos)
7496 (call-interactively 'org-set-effort)
7497 (end-of-line 1)
7498 (setq newhead (org-get-heading)))
7499 (org-agenda-change-all-lines newhead hdmarker))))
7501 (defun org-agenda-toggle-archive-tag ()
7502 "Toggle the archive tag for the current entry."
7503 (interactive)
7504 (org-agenda-check-no-diary)
7505 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7506 (org-agenda-error)))
7507 (buffer (marker-buffer hdmarker))
7508 (pos (marker-position hdmarker))
7509 (inhibit-read-only t)
7510 newhead)
7511 (org-with-remote-undo buffer
7512 (with-current-buffer buffer
7513 (widen)
7514 (goto-char pos)
7515 (org-show-context 'agenda)
7516 (save-excursion
7517 (and (outline-next-heading)
7518 (org-flag-heading nil))) ; show the next heading
7519 (call-interactively 'org-toggle-archive-tag)
7520 (end-of-line 1)
7521 (setq newhead (org-get-heading)))
7522 (org-agenda-change-all-lines newhead hdmarker)
7523 (beginning-of-line 1))))
7525 (defun org-agenda-do-date-later (arg)
7526 (interactive "P")
7527 (cond
7528 ((or (equal arg '(16))
7529 (memq last-command
7530 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7531 (setq this-command 'org-agenda-date-later-minutes)
7532 (org-agenda-date-later-minutes 1))
7533 ((or (equal arg '(4))
7534 (memq last-command
7535 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7536 (setq this-command 'org-agenda-date-later-hours)
7537 (org-agenda-date-later-hours 1))
7539 (org-agenda-date-later (prefix-numeric-value arg)))))
7541 (defun org-agenda-do-date-earlier (arg)
7542 (interactive "P")
7543 (cond
7544 ((or (equal arg '(16))
7545 (memq last-command
7546 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7547 (setq this-command 'org-agenda-date-earlier-minutes)
7548 (org-agenda-date-earlier-minutes 1))
7549 ((or (equal arg '(4))
7550 (memq last-command
7551 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7552 (setq this-command 'org-agenda-date-earlier-hours)
7553 (org-agenda-date-earlier-hours 1))
7555 (org-agenda-date-earlier (prefix-numeric-value arg)))))
7557 (defun org-agenda-date-later (arg &optional what)
7558 "Change the date of this item to ARG day(s) later."
7559 (interactive "p")
7560 (org-agenda-check-type t 'agenda 'timeline)
7561 (org-agenda-check-no-diary)
7562 (let* ((marker (or (org-get-at-bol 'org-marker)
7563 (org-agenda-error)))
7564 (buffer (marker-buffer marker))
7565 (pos (marker-position marker))
7566 cdate today)
7567 (org-with-remote-undo buffer
7568 (with-current-buffer buffer
7569 (widen)
7570 (goto-char pos)
7571 (if (not (org-at-timestamp-p))
7572 (error "Cannot find time stamp"))
7573 (when (and org-agenda-move-date-from-past-immediately-to-today
7574 (equal arg 1)
7575 (or (not what) (eq what 'day))
7576 (not (save-match-data (org-at-date-range-p))))
7577 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
7578 cdate (calendar-absolute-from-gregorian
7579 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
7580 today (org-today))
7581 (if (> today cdate)
7582 ;; immediately shift to today
7583 (setq arg (- today cdate))))
7584 (org-timestamp-change arg (or what 'day))
7585 (when (and (org-at-date-range-p)
7586 (re-search-backward org-tr-regexp-both (point-at-bol)))
7587 (let ((end org-last-changed-timestamp))
7588 (org-timestamp-change arg (or what 'day))
7589 (setq org-last-changed-timestamp
7590 (concat org-last-changed-timestamp "--" end)))))
7591 (org-agenda-show-new-time marker org-last-changed-timestamp))
7592 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7594 (defun org-agenda-date-earlier (arg &optional what)
7595 "Change the date of this item to ARG day(s) earlier."
7596 (interactive "p")
7597 (org-agenda-date-later (- arg) what))
7599 (defun org-agenda-date-later-minutes (arg)
7600 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7601 (interactive "p")
7602 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7603 (org-agenda-date-later arg 'minute))
7605 (defun org-agenda-date-earlier-minutes (arg)
7606 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7607 (interactive "p")
7608 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7609 (org-agenda-date-earlier arg 'minute))
7611 (defun org-agenda-date-later-hours (arg)
7612 "Change the time of this item, in hour steps."
7613 (interactive "p")
7614 (org-agenda-date-later arg 'hour))
7616 (defun org-agenda-date-earlier-hours (arg)
7617 "Change the time of this item, in hour steps."
7618 (interactive "p")
7619 (org-agenda-date-earlier arg 'hour))
7621 (defun org-agenda-show-new-time (marker stamp &optional prefix)
7622 "Show new date stamp via text properties."
7623 ;; We use text properties to make this undoable
7624 (let ((inhibit-read-only t)
7625 (buffer-invisibility-spec))
7626 (setq stamp (concat " " prefix " => " stamp))
7627 (save-excursion
7628 (goto-char (point-max))
7629 (while (not (bobp))
7630 (when (equal marker (org-get-at-bol 'org-marker))
7631 (org-move-to-column (- (window-width) (length stamp)) t)
7632 (org-agenda-fix-tags-filter-overlays-at (point))
7633 (if (featurep 'xemacs)
7634 ;; Use `duplicable' property to trigger undo recording
7635 (let ((ex (make-extent nil nil))
7636 (gl (make-glyph stamp)))
7637 (set-glyph-face gl 'secondary-selection)
7638 (set-extent-properties
7639 ex (list 'invisible t 'end-glyph gl 'duplicable t))
7640 (insert-extent ex (1- (point)) (point-at-eol)))
7641 (add-text-properties
7642 (1- (point)) (point-at-eol)
7643 (list 'display (org-add-props stamp nil
7644 'face 'secondary-selection))))
7645 (beginning-of-line 1))
7646 (beginning-of-line 0)))))
7648 (defun org-agenda-date-prompt (arg)
7649 "Change the date of this item. Date is prompted for, with default today.
7650 The prefix ARG is passed to the `org-time-stamp' command and can therefore
7651 be used to request time specification in the time stamp."
7652 (interactive "P")
7653 (org-agenda-check-type t 'agenda 'timeline)
7654 (org-agenda-check-no-diary)
7655 (let* ((marker (or (org-get-at-bol 'org-marker)
7656 (org-agenda-error)))
7657 (buffer (marker-buffer marker))
7658 (pos (marker-position marker)))
7659 (org-with-remote-undo buffer
7660 (with-current-buffer buffer
7661 (widen)
7662 (goto-char pos)
7663 (if (not (org-at-timestamp-p t))
7664 (error "Cannot find time stamp"))
7665 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
7666 (org-agenda-show-new-time marker org-last-changed-timestamp))
7667 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7669 (defun org-agenda-schedule (arg &optional time)
7670 "Schedule the item at point.
7671 ARG is passed through to `org-schedule'."
7672 (interactive "P")
7673 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7674 (org-agenda-check-no-diary)
7675 (let* ((marker (or (org-get-at-bol 'org-marker)
7676 (org-agenda-error)))
7677 (type (marker-insertion-type marker))
7678 (buffer (marker-buffer marker))
7679 (pos (marker-position marker))
7680 (org-insert-labeled-timestamps-at-point nil)
7682 (set-marker-insertion-type marker t)
7683 (org-with-remote-undo buffer
7684 (with-current-buffer buffer
7685 (widen)
7686 (goto-char pos)
7687 (setq ts (org-schedule arg time)))
7688 (org-agenda-show-new-time marker ts "S"))
7689 (message "Item scheduled for %s" ts)))
7691 (defun org-agenda-deadline (arg &optional time)
7692 "Schedule the item at point.
7693 ARG is passed through to `org-deadline'."
7694 (interactive "P")
7695 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7696 (org-agenda-check-no-diary)
7697 (let* ((marker (or (org-get-at-bol 'org-marker)
7698 (org-agenda-error)))
7699 (buffer (marker-buffer marker))
7700 (pos (marker-position marker))
7701 (org-insert-labeled-timestamps-at-point nil)
7703 (org-with-remote-undo buffer
7704 (with-current-buffer buffer
7705 (widen)
7706 (goto-char pos)
7707 (setq ts (org-deadline arg time)))
7708 (org-agenda-show-new-time marker ts "D"))
7709 (message "Deadline for this item set to %s" ts)))
7711 (defun org-agenda-action ()
7712 "Select entry for agenda action, or execute an agenda action.
7713 This command prompts for another letter. Valid inputs are:
7715 m Mark the entry at point for an agenda action
7716 s Schedule the marked entry to the date at the cursor
7717 d Set the deadline of the marked entry to the date at the cursor
7718 r Call `org-remember' with cursor date as the default date
7719 c Call `org-capture' with cursor date as the default date
7720 SPC Show marked entry in other window
7721 TAB Visit marked entry in other window
7723 The cursor may be at a date in the calendar, or in the Org agenda."
7724 (interactive)
7725 (let (ans)
7726 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [c]apture [ ]show")
7727 (setq ans (read-char-exclusive))
7728 (cond
7729 ((equal ans ?m)
7730 ;; Mark this entry
7731 (if (eq major-mode 'org-agenda-mode)
7732 (let ((m (or (org-get-at-bol 'org-hd-marker)
7733 (org-get-at-bol 'org-marker))))
7734 (if m
7735 (progn
7736 (move-marker org-agenda-action-marker
7737 (marker-position m) (marker-buffer m))
7738 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
7739 (error "Don't know which entry to mark")))
7740 (error "This command works only in the agenda")))
7741 ((equal ans ?s)
7742 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
7743 ((equal ans ?d)
7744 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
7745 ((equal ans ?r)
7746 (org-agenda-do-action '(org-remember) t))
7747 ((equal ans ?c)
7748 (org-agenda-do-action '(org-capture) t))
7749 ((equal ans ?\ )
7750 (let ((cw (selected-window)))
7751 (org-switch-to-buffer-other-window
7752 (marker-buffer org-agenda-action-marker))
7753 (goto-char org-agenda-action-marker)
7754 (org-show-context 'agenda)
7755 (select-window cw)))
7756 ((equal ans ?\C-i)
7757 (org-switch-to-buffer-other-window
7758 (marker-buffer org-agenda-action-marker))
7759 (goto-char org-agenda-action-marker)
7760 (org-show-context 'agenda))
7761 (t (error "Invalid agenda action %c" ans)))))
7763 (defun org-agenda-do-action (form &optional current-buffer)
7764 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
7765 (let ((org-overriding-default-time (org-get-cursor-date)))
7766 (if current-buffer
7767 (eval form)
7768 (if (not (marker-buffer org-agenda-action-marker))
7769 (error "No entry has been selected for agenda action")
7770 (with-current-buffer (marker-buffer org-agenda-action-marker)
7771 (save-excursion
7772 (save-restriction
7773 (widen)
7774 (goto-char org-agenda-action-marker)
7775 (eval form))))))))
7777 (defun org-agenda-clock-in (&optional arg)
7778 "Start the clock on the currently selected item."
7779 (interactive "P")
7780 (org-agenda-check-no-diary)
7781 (if (equal arg '(4))
7782 (org-clock-in arg)
7783 (let* ((marker (or (org-get-at-bol 'org-marker)
7784 (org-agenda-error)))
7785 (hdmarker (or (org-get-at-bol 'org-hd-marker)
7786 marker))
7787 (pos (marker-position marker))
7788 newhead)
7789 (org-with-remote-undo (marker-buffer marker)
7790 (with-current-buffer (marker-buffer marker)
7791 (widen)
7792 (goto-char pos)
7793 (org-show-context 'agenda)
7794 (org-show-entry)
7795 (org-cycle-hide-drawers 'children)
7796 (org-clock-in arg)
7797 (setq newhead (org-get-heading)))
7798 (org-agenda-change-all-lines newhead hdmarker)))))
7800 (defun org-agenda-clock-out ()
7801 "Stop the currently running clock."
7802 (interactive)
7803 (unless (marker-buffer org-clock-marker)
7804 (error "No running clock"))
7805 (let ((marker (make-marker)) newhead)
7806 (org-with-remote-undo (marker-buffer org-clock-marker)
7807 (with-current-buffer (marker-buffer org-clock-marker)
7808 (save-excursion
7809 (save-restriction
7810 (widen)
7811 (goto-char org-clock-marker)
7812 (org-back-to-heading t)
7813 (move-marker marker (point))
7814 (org-clock-out)
7815 (setq newhead (org-get-heading))))))
7816 (org-agenda-change-all-lines newhead marker)
7817 (move-marker marker nil)))
7819 (defun org-agenda-clock-cancel (&optional arg)
7820 "Cancel the currently running clock."
7821 (interactive "P")
7822 (unless (marker-buffer org-clock-marker)
7823 (error "No running clock"))
7824 (org-with-remote-undo (marker-buffer org-clock-marker)
7825 (org-clock-cancel)))
7827 (defun org-agenda-clock-goto ()
7828 "Jump to the currently clocked in task within the agenda.
7829 If the currently clocked in task is not listed in the agenda
7830 buffer, display it in another window."
7831 (interactive)
7832 (let (pos)
7833 (mapc (lambda (o)
7834 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
7835 (setq pos (overlay-start o))))
7836 (overlays-in (point-min) (point-max)))
7837 (cond (pos (goto-char pos))
7838 ;; If the currently clocked entry is not in the agenda
7839 ;; buffer, we visit it in another window:
7840 (org-clock-current-task
7841 (org-switch-to-buffer-other-window (org-clock-goto)))
7842 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
7844 (defun org-agenda-diary-entry-in-org-file ()
7845 "Make a diary entry in the file `org-agenda-diary-file'."
7846 (let (d1 d2 char (text "") dp1 dp2)
7847 (if (equal (buffer-name) "*Calendar*")
7848 (setq d1 (calendar-cursor-to-date t)
7849 d2 (car calendar-mark-ring))
7850 (setq dp1 (get-text-property (point-at-bol) 'day))
7851 (unless dp1 (error "No date defined in current line"))
7852 (setq d1 (calendar-gregorian-from-absolute dp1)
7853 d2 (and (ignore-errors (mark))
7854 (save-excursion
7855 (goto-char (mark))
7856 (setq dp2 (get-text-property (point-at-bol) 'day)))
7857 (calendar-gregorian-from-absolute dp2))))
7858 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
7859 (setq char (read-char-exclusive))
7860 (cond
7861 ((equal char ?d)
7862 (setq text (read-string "Day entry: "))
7863 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
7864 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7865 ((equal char ?a)
7866 (setq d1 (list (car d1) (nth 1 d1)
7867 (read-number (format "Reference year [%d]: " (nth 2 d1))
7868 (nth 2 d1))))
7869 (setq text (read-string "Anniversary (use %d to show years): "))
7870 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
7871 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7872 ((equal char ?b)
7873 (setq text (read-string "Block entry: "))
7874 (unless (and d1 d2 (not (equal d1 d2)))
7875 (error "No block of days selected"))
7876 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
7877 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7878 ((equal char ?j)
7879 (org-switch-to-buffer-other-window
7880 (find-file-noselect org-agenda-diary-file))
7881 (require 'org-datetree)
7882 (org-datetree-find-date-create d1)
7883 (org-reveal t))
7884 (t (error "Invalid selection character `%c'" char)))))
7886 (defcustom org-agenda-insert-diary-strategy 'date-tree
7887 "Where in `org-agenda-diary-file' should new entries be added?
7888 Valid values:
7890 date-tree in the date tree, as child of the date
7891 top-level as top-level entries at the end of the file."
7892 :group 'org-agenda
7893 :type '(choice
7894 (const :tag "in a date tree" date-tree)
7895 (const :tag "as top level at end of file" top-level)))
7897 (defcustom org-agenda-insert-diary-extract-time nil
7898 "Non-nil means extract any time specification from the diary entry."
7899 :group 'org-agenda
7900 :type 'boolean)
7902 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
7903 "Add a diary entry with TYPE to `org-agenda-diary-file'.
7904 If TEXT is not empty, it will become the headline of the new entry, and
7905 the resulting entry will not be shown. When TEXT is empty, switch to
7906 `org-agenda-diary-file' and let the user finish the entry there."
7907 (let ((cw (current-window-configuration)))
7908 (org-switch-to-buffer-other-window
7909 (find-file-noselect org-agenda-diary-file))
7910 (widen)
7911 (goto-char (point-min))
7912 (cond
7913 ((eq type 'anniversary)
7914 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
7915 (progn
7916 (or (org-on-heading-p t)
7917 (progn
7918 (outline-next-heading)
7919 (insert "* Anniversaries\n\n")
7920 (beginning-of-line -1)))))
7921 (outline-next-heading)
7922 (org-back-over-empty-lines)
7923 (backward-char 1)
7924 (insert "\n")
7925 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
7926 (nth 2 d1) (car d1) (nth 1 d1) text)))
7927 ((eq type 'day)
7928 (let ((org-prefix-has-time t)
7929 (org-agenda-time-leading-zero t)
7930 fmt time time2)
7931 (if org-agenda-insert-diary-extract-time
7932 ;; Use org-agenda-format-item to parse text for a time-range and
7933 ;; remove it. FIXME: This is a hack, we should refactor
7934 ;; that function to make time extraction available separately
7935 (setq fmt (org-agenda-format-item nil text nil nil t)
7936 time (get-text-property 0 'time fmt)
7937 time2 (if (> (length time) 0)
7938 ;; split-string removes trailing ...... if
7939 ;; no end time given. First space
7940 ;; separates time from date.
7941 (concat " " (car (split-string time "\\.")))
7942 nil)
7943 text (get-text-property 0 'txt fmt)))
7944 (if (eq org-agenda-insert-diary-strategy 'top-level)
7945 (org-agenda-insert-diary-as-top-level text)
7946 (require 'org-datetree)
7947 (org-datetree-find-date-create d1)
7948 (org-agenda-insert-diary-make-new-entry text))
7949 (org-insert-time-stamp (org-time-from-absolute
7950 (calendar-absolute-from-gregorian d1))
7951 nil nil nil nil time2))
7952 (end-of-line 0))
7953 ((eq type 'block)
7954 (if (> (calendar-absolute-from-gregorian d1)
7955 (calendar-absolute-from-gregorian d2))
7956 (setq d1 (prog1 d2 (setq d2 d1))))
7957 (if (eq org-agenda-insert-diary-strategy 'top-level)
7958 (org-agenda-insert-diary-as-top-level text)
7959 (require 'org-datetree)
7960 (org-datetree-find-date-create d1)
7961 (org-agenda-insert-diary-make-new-entry text))
7962 (org-insert-time-stamp (org-time-from-absolute
7963 (calendar-absolute-from-gregorian d1)))
7964 (insert "--")
7965 (org-insert-time-stamp (org-time-from-absolute
7966 (calendar-absolute-from-gregorian d2)))
7967 (end-of-line 0)))
7968 (if (string-match "\\S-" text)
7969 (progn
7970 (set-window-configuration cw)
7971 (message "%s entry added to %s"
7972 (capitalize (symbol-name type))
7973 (abbreviate-file-name org-agenda-diary-file)))
7974 (org-reveal t)
7975 (message "Please finish entry here"))))
7977 (defun org-agenda-insert-diary-as-top-level (text)
7978 "Make new entry as a top-level entry at the end of the file.
7979 Add TEXT as headline, and position the cursor in the second line so that
7980 a timestamp can be added there."
7981 (widen)
7982 (goto-char (point-max))
7983 (or (bolp) (insert "\n"))
7984 (insert "* " text "\n")
7985 (if org-adapt-indentation (org-indent-to-column 2)))
7987 (defun org-agenda-insert-diary-make-new-entry (text)
7988 "Make new entry as last child of current entry.
7989 Add TEXT as headline, and position the cursor in the second line so that
7990 a timestamp can be added there."
7991 (let ((org-show-following-heading t)
7992 (org-show-siblings t)
7993 (org-show-hierarchy-above t)
7994 (org-show-entry-below t)
7995 col)
7996 (outline-next-heading)
7997 (org-back-over-empty-lines)
7998 (or (looking-at "[ \t]*$")
7999 (progn (insert "\n") (backward-char 1)))
8000 (org-insert-heading nil t)
8001 (org-do-demote)
8002 (setq col (current-column))
8003 (insert text "\n")
8004 (if org-adapt-indentation (org-indent-to-column col))
8005 (let ((org-show-following-heading t)
8006 (org-show-siblings t)
8007 (org-show-hierarchy-above t)
8008 (org-show-entry-below t))
8009 (org-show-context))))
8011 (defun org-agenda-diary-entry ()
8012 "Make a diary entry, like the `i' command from the calendar.
8013 All the standard commands work: block, weekly etc.
8014 When `org-agenda-diary-file' points to a file,
8015 `org-agenda-diary-entry-in-org-file' is called instead to create
8016 entries in that Org-mode file."
8017 (interactive)
8018 (org-agenda-check-type t 'agenda 'timeline)
8019 (if (not (eq org-agenda-diary-file 'diary-file))
8020 (org-agenda-diary-entry-in-org-file)
8021 (require 'diary-lib)
8022 (let* ((char (progn
8023 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
8024 (read-char-exclusive)))
8025 (cmd (cdr (assoc char
8026 '((?d . insert-diary-entry)
8027 (?w . insert-weekly-diary-entry)
8028 (?m . insert-monthly-diary-entry)
8029 (?y . insert-yearly-diary-entry)
8030 (?a . insert-anniversary-diary-entry)
8031 (?b . insert-block-diary-entry)
8032 (?c . insert-cyclic-diary-entry)))))
8033 (oldf (symbol-function 'calendar-cursor-to-date))
8034 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
8035 (point (point))
8036 (mark (or (mark t) (point))))
8037 (unless cmd
8038 (error "No command associated with <%c>" char))
8039 (unless (and (get-text-property point 'day)
8040 (or (not (equal ?b char))
8041 (get-text-property mark 'day)))
8042 (error "Don't know which date to use for diary entry"))
8043 ;; We implement this by hacking the `calendar-cursor-to-date' function
8044 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
8045 (let ((calendar-mark-ring
8046 (list (calendar-gregorian-from-absolute
8047 (or (get-text-property mark 'day)
8048 (get-text-property point 'day))))))
8049 (unwind-protect
8050 (progn
8051 (fset 'calendar-cursor-to-date
8052 (lambda (&optional error dummy)
8053 (calendar-gregorian-from-absolute
8054 (get-text-property point 'day))))
8055 (call-interactively cmd))
8056 (fset 'calendar-cursor-to-date oldf))))))
8058 (defun org-agenda-execute-calendar-command (cmd)
8059 "Execute a calendar command from the agenda, with the date associated to
8060 the cursor position."
8061 (org-agenda-check-type t 'agenda 'timeline)
8062 (require 'diary-lib)
8063 (unless (get-text-property (point) 'day)
8064 (error "Don't know which date to use for calendar command"))
8065 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
8066 (point (point))
8067 (date (calendar-gregorian-from-absolute
8068 (get-text-property point 'day)))
8069 ;; the following 2 vars are needed in the calendar
8070 (displayed-month (car date))
8071 (displayed-year (nth 2 date)))
8072 (unwind-protect
8073 (progn
8074 (fset 'calendar-cursor-to-date
8075 (lambda (&optional error dummy)
8076 (calendar-gregorian-from-absolute
8077 (get-text-property point 'day))))
8078 (call-interactively cmd))
8079 (fset 'calendar-cursor-to-date oldf))))
8081 (defun org-agenda-phases-of-moon ()
8082 "Display the phases of the moon for the 3 months around the cursor date."
8083 (interactive)
8084 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
8086 (defun org-agenda-holidays ()
8087 "Display the holidays for the 3 months around the cursor date."
8088 (interactive)
8089 (org-agenda-execute-calendar-command 'list-calendar-holidays))
8091 (defvar calendar-longitude)
8092 (defvar calendar-latitude)
8093 (defvar calendar-location-name)
8095 (defun org-agenda-sunrise-sunset (arg)
8096 "Display sunrise and sunset for the cursor date.
8097 Latitude and longitude can be specified with the variables
8098 `calendar-latitude' and `calendar-longitude'. When called with prefix
8099 argument, latitude and longitude will be prompted for."
8100 (interactive "P")
8101 (require 'solar)
8102 (let ((calendar-longitude (if arg nil calendar-longitude))
8103 (calendar-latitude (if arg nil calendar-latitude))
8104 (calendar-location-name
8105 (if arg "the given coordinates" calendar-location-name)))
8106 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
8108 (defun org-agenda-goto-calendar ()
8109 "Open the Emacs calendar with the date at the cursor."
8110 (interactive)
8111 (org-agenda-check-type t 'agenda 'timeline)
8112 (let* ((day (or (get-text-property (point) 'day)
8113 (error "Don't know which date to open in calendar")))
8114 (date (calendar-gregorian-from-absolute day))
8115 (calendar-move-hook nil)
8116 (calendar-view-holidays-initially-flag nil)
8117 (calendar-view-diary-initially-flag nil))
8118 (calendar)
8119 (calendar-goto-date date)))
8121 ;;;###autoload
8122 (defun org-calendar-goto-agenda ()
8123 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
8124 This is a command that has to be installed in `calendar-mode-map'."
8125 (interactive)
8126 (org-agenda-list nil (calendar-absolute-from-gregorian
8127 (calendar-cursor-to-date))
8128 nil))
8130 (defun org-agenda-convert-date ()
8131 (interactive)
8132 (org-agenda-check-type t 'agenda 'timeline)
8133 (let ((day (get-text-property (point) 'day))
8134 date s)
8135 (unless day
8136 (error "Don't know which date to convert"))
8137 (setq date (calendar-gregorian-from-absolute day))
8138 (setq s (concat
8139 "Gregorian: " (calendar-date-string date) "\n"
8140 "ISO: " (calendar-iso-date-string date) "\n"
8141 "Day of Yr: " (calendar-day-of-year-string date) "\n"
8142 "Julian: " (calendar-julian-date-string date) "\n"
8143 "Astron. JD: " (calendar-astro-date-string date)
8144 " (Julian date number at noon UTC)\n"
8145 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
8146 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
8147 "French: " (calendar-french-date-string date) "\n"
8148 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
8149 "Mayan: " (calendar-mayan-date-string date) "\n"
8150 "Coptic: " (calendar-coptic-date-string date) "\n"
8151 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
8152 "Persian: " (calendar-persian-date-string date) "\n"
8153 "Chinese: " (calendar-chinese-date-string date) "\n"))
8154 (with-output-to-temp-buffer "*Dates*"
8155 (princ s))
8156 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
8158 ;;; Bulk commands
8160 (defvar org-agenda-bulk-marked-entries nil
8161 "List of markers that refer to marked entries in the agenda.")
8163 (defun org-agenda-bulk-marked-p ()
8164 (eq (get-char-property (point-at-bol) 'type)
8165 'org-marked-entry-overlay))
8167 (defun org-agenda-bulk-mark (&optional arg)
8168 "Mark the entry at point for future bulk action."
8169 (interactive "p")
8170 (dotimes (i (max arg 1))
8171 (unless (org-get-at-bol 'org-agenda-diary-link)
8172 (let* ((m (org-get-at-bol 'org-hd-marker))
8174 (unless (org-agenda-bulk-marked-p)
8175 (unless m (error "Nothing to mark at point"))
8176 (push m org-agenda-bulk-marked-entries)
8177 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
8178 (org-overlay-display ov "> "
8179 (org-get-todo-face "TODO")
8180 'evaporate)
8181 (overlay-put ov 'type 'org-marked-entry-overlay))
8182 (beginning-of-line 2)
8183 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8184 (beginning-of-line 2))
8185 (message "%d entries marked for bulk action"
8186 (length org-agenda-bulk-marked-entries))))))
8188 (defun org-agenda-bulk-mark-regexp (regexp)
8189 "Mark entries match REGEXP."
8190 (interactive "sMark entries matching regexp: ")
8191 (let (entries-marked)
8192 (save-excursion
8193 (goto-char (point-min))
8194 (goto-char (next-single-property-change (point) 'txt))
8195 (while (re-search-forward regexp nil t)
8196 (when (string-match regexp (get-text-property (point) 'txt))
8197 (setq entries-marked (+ entries-marked 1))
8198 (call-interactively 'org-agenda-bulk-mark))))
8199 (if (not entries-marked)
8200 (message "No entry matching this regexp."))))
8202 (defun org-agenda-bulk-unmark ()
8203 "Unmark the entry at point for future bulk action."
8204 (interactive)
8205 (when (org-agenda-bulk-marked-p)
8206 (org-agenda-bulk-remove-overlays
8207 (point-at-bol) (+ 2 (point-at-bol)))
8208 (setq org-agenda-bulk-marked-entries
8209 (delete (org-get-at-bol 'org-hd-marker)
8210 org-agenda-bulk-marked-entries)))
8211 (beginning-of-line 2)
8212 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8213 (beginning-of-line 2))
8214 (message "%d entries marked for bulk action"
8215 (length org-agenda-bulk-marked-entries)))
8217 (defun org-agenda-bulk-toggle ()
8218 "Toggle marking the entry at point for bulk action."
8219 (interactive)
8220 (if (org-agenda-bulk-marked-p)
8221 (org-agenda-bulk-unmark)
8222 (org-agenda-bulk-mark)))
8224 (defun org-agenda-bulk-remove-overlays (&optional beg end)
8225 "Remove the mark overlays between BEG and END in the agenda buffer.
8226 BEG and END default to the buffer limits.
8228 This only removes the overlays, it does not remove the markers
8229 from the list in `org-agenda-bulk-marked-entries'."
8230 (interactive)
8231 (mapc (lambda (ov)
8232 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
8233 (delete-overlay ov)))
8234 (overlays-in (or beg (point-min)) (or end (point-max)))))
8236 (defun org-agenda-bulk-remove-all-marks ()
8237 "Remove all marks in the agenda buffer.
8238 This will remove the markers, and the overlays."
8239 (interactive)
8240 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
8241 (setq org-agenda-bulk-marked-entries nil)
8242 (org-agenda-bulk-remove-overlays (point-min) (point-max)))
8244 (defun org-agenda-bulk-action (&optional arg)
8245 "Execute an remote-editing action on all marked entries.
8246 The prefix arg is passed through to the command if possible."
8247 (interactive "P")
8248 ;; Make sure we have markers, and only valid ones
8249 (unless org-agenda-bulk-marked-entries (error "No entries are marked"))
8250 (mapc
8251 (lambda (m)
8252 (unless (and (markerp m)
8253 (marker-buffer m)
8254 (buffer-live-p (marker-buffer m))
8255 (marker-position m))
8256 (error "Marker %s for bulk command is invalid" m)))
8257 org-agenda-bulk-marked-entries)
8259 ;; Prompt for the bulk command
8260 (message (concat "Bulk: [r]efile [$]arch [A]rch->sib [t]odo"
8261 " [+/-]tag [s]chd [S]catter [d]eadline [f]unction"
8262 (when org-agenda-bulk-custom-functions
8263 (concat " Custom: ["
8264 (mapconcat (lambda(f) (char-to-string (car f)))
8265 org-agenda-bulk-custom-functions "")
8266 "]"))))
8267 (let* ((action (read-char-exclusive))
8268 (org-log-refile (if org-log-refile 'time nil))
8269 (entries (reverse org-agenda-bulk-marked-entries))
8270 redo-at-end
8271 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
8272 (cond
8273 ((equal action ?$)
8274 (setq cmd '(org-agenda-archive)))
8276 ((equal action ?A)
8277 (setq cmd '(org-agenda-archive-to-archive-sibling)))
8279 ((member action '(?r ?w))
8280 (setq rfloc (org-refile-get-location
8281 "Refile to"
8282 (marker-buffer (car org-agenda-bulk-marked-entries))
8283 org-refile-allow-creating-parent-nodes))
8284 (if (nth 3 rfloc)
8285 (setcar (nthcdr 3 rfloc)
8286 (move-marker (make-marker) (nth 3 rfloc)
8287 (or (get-file-buffer (nth 1 rfloc))
8288 (find-buffer-visiting (nth 1 rfloc))
8289 (error "This should not happen")))))
8291 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
8292 redo-at-end t))
8294 ((equal action ?t)
8295 (setq state (org-icompleting-read
8296 "Todo state: "
8297 (with-current-buffer (marker-buffer (car entries))
8298 (mapcar 'list org-todo-keywords-1))))
8299 (setq cmd `(let ((org-inhibit-blocking t)
8300 (org-inhibit-logging 'note))
8301 (org-agenda-todo ,state))))
8303 ((memq action '(?- ?+))
8304 (setq tag (org-icompleting-read
8305 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
8306 (with-current-buffer (marker-buffer (car entries))
8307 (delq nil
8308 (mapcar (lambda (x)
8309 (if (stringp (car x)) x)) org-tag-alist)))))
8310 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
8312 ((memq action '(?s ?d))
8313 (let* ((date (unless arg
8314 (org-read-date
8315 nil nil nil
8316 (if (eq action ?s) "(Re)Schedule to" "Set Deadline to"))))
8317 (ans (if arg nil org-read-date-final-answer))
8318 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
8319 (setq cmd `(let* ((bound (fboundp 'read-string))
8320 (old (and bound (symbol-function 'read-string))))
8321 (unwind-protect
8322 (progn
8323 (fset 'read-string (lambda (&rest ignore) ,ans))
8324 (eval '(,c1 arg)))
8325 (if bound
8326 (fset 'read-string old)
8327 (fmakunbound 'read-string)))))))
8329 ((equal action ?S)
8330 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
8331 (error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
8332 (let ((days (read-number
8333 (format "Scatter tasks across how many %sdays: "
8334 (if arg "week" "")) 7)))
8335 (setq cmd
8336 `(let ((distance (1+ (random ,days))))
8337 (if arg
8338 (let ((dist distance)
8339 (day-of-week
8340 (calendar-day-of-week
8341 (calendar-gregorian-from-absolute (org-today)))))
8342 (dotimes (i (1+ dist))
8343 (while (member day-of-week org-agenda-weekend-days)
8344 (incf distance)
8345 (incf day-of-week)
8346 (if (= day-of-week 7)
8347 (setq day-of-week 0)))
8348 (incf day-of-week)
8349 (if (= day-of-week 7)
8350 (setq day-of-week 0)))))
8351 ;; silently fail when try to replan a sexp entry
8352 (condition-case nil
8353 (let* ((date (calendar-gregorian-from-absolute
8354 (+ (org-today) distance)))
8355 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
8356 (nth 2 date))))
8357 (org-agenda-schedule nil time))
8358 (error nil)))))))
8360 ((assoc action org-agenda-bulk-custom-functions)
8361 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
8362 redo-at-end t))
8364 ((equal action ?f)
8365 (setq cmd (list (intern
8366 (org-icompleting-read "Function: "
8367 obarray 'fboundp t nil nil)))))
8369 (t (error "Invalid bulk action")))
8371 ;; Sort the markers, to make sure that parents are handled before children
8372 (setq entries (sort entries
8373 (lambda (a b)
8374 (cond
8375 ((equal (marker-buffer a) (marker-buffer b))
8376 (< (marker-position a) (marker-position b)))
8378 (string< (buffer-name (marker-buffer a))
8379 (buffer-name (marker-buffer b))))))))
8381 ;; Now loop over all markers and apply cmd
8382 (while (setq e (pop entries))
8383 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
8384 (if (not pos)
8385 (progn (message "Skipping removed entry at %s" e)
8386 (setq cntskip (1+ cntskip)))
8387 (goto-char pos)
8388 (let (org-loop-over-headlines-in-active-region)
8389 (eval cmd))
8390 (setq org-agenda-bulk-marked-entries
8391 (delete e org-agenda-bulk-marked-entries))
8392 (setq cnt (1+ cnt))))
8393 (setq org-agenda-bulk-marked-entries nil)
8394 (org-agenda-bulk-remove-all-marks)
8395 (when redo-at-end (org-agenda-redo))
8396 (message "Acted on %d entries%s"
8398 (if (= cntskip 0)
8400 (format ", skipped %d (disappeared before their turn)"
8401 cntskip)))))
8403 ;;; Flagging notes
8405 (defun org-agenda-show-the-flagging-note ()
8406 "Display the flagging note in the other window.
8407 When called a second time in direct sequence, offer to remove the FLAGGING
8408 tag and (if present) the flagging note."
8409 (interactive)
8410 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
8411 (win (selected-window))
8412 note heading newhead)
8413 (unless hdmarker
8414 (error "No linked entry at point"))
8415 (if (and (eq this-command last-command)
8416 (y-or-n-p "Unflag and remove any flagging note? "))
8417 (progn
8418 (org-agenda-remove-flag hdmarker)
8419 (let ((win (get-buffer-window "*Flagging Note*")))
8420 (and win (delete-window win)))
8421 (message "Entry unflaged"))
8422 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
8423 (unless note
8424 (error "No flagging note"))
8425 (org-kill-new note)
8426 (org-switch-to-buffer-other-window "*Flagging Note*")
8427 (erase-buffer)
8428 (insert note)
8429 (goto-char (point-min))
8430 (while (re-search-forward "\\\\n" nil t)
8431 (replace-match "\n" t t))
8432 (goto-char (point-min))
8433 (select-window win)
8434 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
8436 (defun org-agenda-remove-flag (marker)
8437 "Remove the FLAGGED tag and any flagging note in the entry."
8438 (let (newhead)
8439 (org-with-point-at marker
8440 (org-toggle-tag "FLAGGED" 'off)
8441 (org-entry-delete nil "THEFLAGGINGNOTE")
8442 (setq newhead (org-get-heading)))
8443 (org-agenda-change-all-lines newhead marker)
8444 (message "Entry unflaged")))
8446 (defun org-agenda-get-any-marker (&optional pos)
8447 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
8448 (get-text-property (or pos (point-at-bol)) 'org-marker)))
8450 ;;; Appointment reminders
8452 (defvar appt-time-msg-list)
8454 ;;;###autoload
8455 (defun org-agenda-to-appt (&optional refresh filter &rest args)
8456 "Activate appointments found in `org-agenda-files'.
8457 With a \\[universal-argument] prefix, refresh the list of
8458 appointments.
8460 If FILTER is t, interactively prompt the user for a regular
8461 expression, and filter out entries that don't match it.
8463 If FILTER is a string, use this string as a regular expression
8464 for filtering entries out.
8466 If FILTER is a function, filter out entries against which
8467 calling the function returns nil. This function takes one
8468 argument: an entry from `org-agenda-get-day-entries'.
8470 FILTER can also be an alist with the car of each cell being
8471 either 'headline or 'category. For example:
8473 '((headline \"IMPORTANT\")
8474 (category \"Work\"))
8476 will only add headlines containing IMPORTANT or headlines
8477 belonging to the \"Work\" category.
8479 ARGS are symbols indicating what kind of entries to consider.
8480 By default `org-agenda-to-appt' will use :deadline, :scheduled
8481 and :timestamp entries. See the docstring of `org-diary' for
8482 details and examples."
8483 (interactive "P")
8484 (if refresh (setq appt-time-msg-list nil))
8485 (if (eq filter t)
8486 (setq filter (read-from-minibuffer "Regexp filter: ")))
8487 (let* ((cnt 0) ; count added events
8488 (scope (or args '(:deadline :scheduled :timestamp)))
8489 (org-agenda-new-buffers nil)
8490 (org-deadline-warning-days 0)
8491 ;; Do not use `org-today' here because appt only takes
8492 ;; time and without date as argument, so it may pass wrong
8493 ;; information otherwise
8494 (today (org-date-to-gregorian
8495 (time-to-days (current-time))))
8496 (org-agenda-restrict nil)
8497 (files (org-agenda-files 'unrestricted)) entries file)
8498 ;; Get all entries which may contain an appt
8499 (org-prepare-agenda-buffers files)
8500 (while (setq file (pop files))
8501 (setq entries
8502 (delq nil
8503 (append entries
8504 (apply 'org-agenda-get-day-entries
8505 file today scope)))))
8506 ;; Map thru entries and find if we should filter them out
8507 (mapc
8508 (lambda(x)
8509 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
8510 (cat (get-text-property 1 'org-category x))
8511 (tod (get-text-property 1 'time-of-day x))
8512 (ok (or (null filter)
8513 (and (stringp filter) (string-match filter evt))
8514 (and (functionp filter) (funcall filter x))
8515 (and (listp filter)
8516 (let ((cat-filter (cadr (assoc 'category filter)))
8517 (evt-filter (cadr (assoc 'headline filter))))
8518 (or (and (stringp cat-filter)
8519 (string-match cat-filter cat))
8520 (and (stringp evt-filter)
8521 (string-match evt-filter evt))))))))
8522 ;; FIXME: Shall we remove text-properties for the appt text?
8523 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
8524 (when (and ok tod)
8525 (setq tod (concat "00" (number-to-string tod))
8526 tod (when (string-match
8527 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
8528 (concat (match-string 1 tod) ":"
8529 (match-string 2 tod))))
8530 (appt-add tod evt)
8531 (setq cnt (1+ cnt))))) entries)
8532 (org-release-buffers org-agenda-new-buffers)
8533 (if (eq cnt 0)
8534 (message "No event to add")
8535 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
8537 (defun org-agenda-todayp (date)
8538 "Does DATE mean today, when considering `org-extend-today-until'?"
8539 (let ((today (org-today))
8540 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
8541 date)))
8542 (eq date today)))
8544 (defun org-agenda-todo-yesterday (&optional arg)
8545 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday"
8546 (interactive "P")
8547 (let* ((hour (third (decode-time
8548 (org-current-time))))
8549 (org-extend-today-until (1+ hour)))
8550 (org-agenda-todo arg)))
8552 (provide 'org-agenda)
8554 ;;; org-agenda.el ends here