ob-sh: add :hlines processing and :hline-string header arg
[org-mode/org-tableheadings.git] / lisp / org-agenda.el
blobeda5e692fc23b6f746f5ad18ad3932b2a843f155
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004-2014 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 convenient 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 (require 'org-macs)
50 (eval-when-compile
51 (require 'cl))
53 (declare-function diary-add-to-list "diary-lib"
54 (date string specifier &optional marker globcolor literal))
55 (declare-function calendar-absolute-from-iso "cal-iso" (date))
56 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
57 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
58 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
59 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
60 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
61 (declare-function calendar-french-date-string "cal-french" (&optional date))
62 (declare-function calendar-goto-date "cal-move" (date))
63 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
64 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
65 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
66 (declare-function calendar-iso-from-absolute "cal-iso" (date))
67 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
68 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
69 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
70 (declare-function calendar-check-holidays "holidays" (date))
72 (declare-function org-columns-remove-overlays "org-colview" ())
73 (declare-function org-datetree-find-date-create "org-datetree"
74 (date &optional keep-restriction))
75 (declare-function org-columns-quit "org-colview" ())
76 (declare-function diary-date-display-form "diary-lib" (&optional type))
77 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
78 (declare-function org-habit-insert-consistency-graphs
79 "org-habit" (&optional line))
80 (declare-function org-is-habit-p "org-habit" (&optional pom))
81 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
82 (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
83 (declare-function org-pop-to-buffer-same-window "org-compat"
84 (&optional buffer-or-name norecord label))
85 (declare-function org-agenda-columns "org-colview" ())
86 (declare-function org-add-archive-files "org-archive" (files))
87 (declare-function org-capture "org-capture" (&optional goto keys))
89 (defvar calendar-mode-map) ; defined in calendar.el
90 (defvar org-clock-current-task nil) ; defined in org-clock.el
91 (defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
92 (defvar org-habit-show-habits) ; defined in org-habit.el
93 (defvar org-habit-show-habits-only-for-today)
94 (defvar org-habit-show-all-today)
96 ;; Defined somewhere in this file, but used before definition.
97 (defvar org-agenda-buffer-name "*Org Agenda*")
98 (defvar org-agenda-overriding-header nil)
99 (defvar org-agenda-title-append nil)
100 (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
101 (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
102 (defvar original-date) ; dynamically scoped, calendar.el does scope this
104 (defvar org-agenda-undo-list nil
105 "List of undoable operations in the agenda since last refresh.")
106 (defvar org-agenda-pending-undo-list nil
107 "In a series of undo commands, this is the list of remaining undo items.")
109 (defcustom org-agenda-confirm-kill 1
110 "When set, remote killing from the agenda buffer needs confirmation.
111 When t, a confirmation is always needed. When a number N, confirmation is
112 only needed when the text to be killed contains more than N non-white lines."
113 :group 'org-agenda
114 :type '(choice
115 (const :tag "Never" nil)
116 (const :tag "Always" t)
117 (integer :tag "When more than N lines")))
119 (defcustom org-agenda-compact-blocks nil
120 "Non-nil means make the block agenda more compact.
121 This is done globally by leaving out lines like the agenda span
122 name and week number or the separator lines."
123 :group 'org-agenda
124 :type 'boolean)
126 (defcustom org-agenda-block-separator ?=
127 "The separator between blocks in the agenda.
128 If this is a string, it will be used as the separator, with a newline added.
129 If it is a character, it will be repeated to fill the window width.
130 If nil the separator is disabled. In `org-agenda-custom-commands' this
131 addresses the separator between the current and the previous block."
132 :group 'org-agenda
133 :type '(choice
134 (const :tag "Disabled" nil)
135 (character)
136 (string)))
138 (defgroup org-agenda-export nil
139 "Options concerning exporting agenda views in Org-mode."
140 :tag "Org Agenda Export"
141 :group 'org-agenda)
143 (defcustom org-agenda-with-colors t
144 "Non-nil means use colors in agenda views."
145 :group 'org-agenda-export
146 :type 'boolean)
148 (defcustom org-agenda-exporter-settings nil
149 "Alist of variable/value pairs that should be active during agenda export.
150 This is a good place to set options for ps-print and for htmlize.
151 Note that the way this is implemented, the values will be evaluated
152 before assigned to the variables. So make sure to quote values you do
153 *not* want evaluated, for example
155 (setq org-agenda-exporter-settings
156 '((ps-print-color-p 'black-white)))"
157 :group 'org-agenda-export
158 :type '(repeat
159 (list
160 (variable)
161 (sexp :tag "Value"))))
163 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
164 "Hook run in a temporary buffer before writing the agenda to an export file.
165 A useful function for this hook is `org-agenda-add-entry-text'."
166 :group 'org-agenda-export
167 :type 'hook
168 :options '(org-agenda-add-entry-text))
170 (defcustom org-agenda-add-entry-text-maxlines 0
171 "Maximum number of entry text lines to be added to agenda.
172 This is only relevant when `org-agenda-add-entry-text' is part of
173 `org-agenda-before-write-hook', which is the default.
174 When this is 0, nothing will happen. When it is greater than 0, it
175 specifies the maximum number of lines that will be added for each entry
176 that is listed in the agenda view.
178 Note that this variable is not used during display, only when exporting
179 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
180 and `org-agenda-entry-text-maxlines'."
181 :group 'org-agenda
182 :type 'integer)
184 (defcustom org-agenda-add-entry-text-descriptive-links t
185 "Non-nil means export org-links as descriptive links in agenda added text.
186 This variable applies to the text added to the agenda when
187 `org-agenda-add-entry-text-maxlines' is larger than 0.
188 When this variable nil, the URL will (also) be shown."
189 :group 'org-agenda
190 :type 'boolean)
192 (defcustom org-agenda-export-html-style nil
193 "The style specification for exported HTML Agenda files.
194 If this variable contains a string, it will replace the default <style>
195 section as produced by `htmlize'.
196 Since there are different ways of setting style information, this variable
197 needs to contain the full HTML structure to provide a style, including the
198 surrounding HTML tags. The style specifications should include definitions
199 the fonts used by the agenda, here is an example:
201 <style type=\"text/css\">
202 p { font-weight: normal; color: gray; }
203 .org-agenda-structure {
204 font-size: 110%;
205 color: #003399;
206 font-weight: 600;
208 .org-todo {
209 color: #cc6666;
210 font-weight: bold;
212 .org-agenda-done {
213 color: #339933;
215 .org-done {
216 color: #339933;
218 .title { text-align: center; }
219 .todo, .deadline { color: red; }
220 .done { color: green; }
221 </style>
223 or, if you want to keep the style in a file,
225 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
227 As the value of this option simply gets inserted into the HTML <head> header,
228 you can \"misuse\" it to also add other text to the header."
229 :group 'org-agenda-export
230 :group 'org-export-html
231 :type '(choice
232 (const nil)
233 (string)))
235 (defcustom org-agenda-persistent-filter nil
236 "When set, keep filters from one agenda view to the next."
237 :group 'org-agenda
238 :type 'boolean)
240 (defgroup org-agenda-custom-commands nil
241 "Options concerning agenda views in Org-mode."
242 :tag "Org Agenda Custom Commands"
243 :group 'org-agenda)
245 (defconst org-sorting-choice
246 '(choice
247 (const time-up) (const time-down)
248 (const timestamp-up) (const timestamp-down)
249 (const scheduled-up) (const scheduled-down)
250 (const deadline-up) (const deadline-down)
251 (const ts-up) (const ts-down)
252 (const tsia-up) (const tsia-down)
253 (const category-keep) (const category-up) (const category-down)
254 (const tag-down) (const tag-up)
255 (const priority-up) (const priority-down)
256 (const todo-state-up) (const todo-state-down)
257 (const effort-up) (const effort-down)
258 (const habit-up) (const habit-down)
259 (const alpha-up) (const alpha-down)
260 (const user-defined-up) (const user-defined-down))
261 "Sorting choices.")
263 ;; Keep custom values for `org-agenda-filter-preset' compatible with
264 ;; the new variable `org-agenda-tag-filter-preset'.
265 (org-defvaralias 'org-agenda-filter-preset 'org-agenda-tag-filter-preset)
266 (org-defvaralias 'org-agenda-filter 'org-agenda-tag-filter)
268 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
269 "List of types searched for when creating the daily/weekly agenda.
270 This variable is a list of symbols that controls the types of
271 items that appear in the daily/weekly agenda. Allowed symbols in this
272 list are are
274 :timestamp List items containing a date stamp or date range matching
275 the selected date. This includes sexp entries in angular
276 brackets.
278 :sexp List entries resulting from plain diary-like sexps.
280 :deadline List deadline due on that date. When the date is today,
281 also list any deadlines past due, or due within
282 `org-deadline-warning-days'. `:deadline' must appear before
283 `:scheduled' if the setting of
284 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
285 any effect.
287 :deadline* Same as above, but only include the deadline if it has an
288 hour specification as [h]h:mm.
290 :scheduled List all items which are scheduled for the given date.
291 The diary for *today* also contains items which were
292 scheduled earlier and are not yet marked DONE.
294 :scheduled* Same as above, but only include the scheduled item if it
295 has an hour specification as [h]h:mm.
297 By default, all four non-starred types are turned on.
299 When :scheduled* or :deadline* are included, :schedule or :deadline
300 will be ignored.
302 Never set this variable globally using `setq', because then it
303 will apply to all future agenda commands. Instead, bind it with
304 `let' to scope it dynamically into the agenda-constructing
305 command. A good way to set it is through options in
306 `org-agenda-custom-commands'. For a more flexible (though
307 somewhat less efficient) way of determining what is included in
308 the daily/weekly agenda, see `org-agenda-skip-function'.")
310 (defconst org-agenda-custom-commands-local-options
311 `(repeat :tag "Local settings for this command. Remember to quote values"
312 (choice :tag "Setting"
313 (list :tag "Heading for this block"
314 (const org-agenda-overriding-header)
315 (string :tag "Headline"))
316 (list :tag "Files to be searched"
317 (const org-agenda-files)
318 (list
319 (const :format "" quote)
320 (repeat (file))))
321 (list :tag "Sorting strategy"
322 (const org-agenda-sorting-strategy)
323 (list
324 (const :format "" quote)
325 (repeat
326 ,org-sorting-choice)))
327 (list :tag "Prefix format"
328 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
329 (string))
330 (list :tag "Number of days in agenda"
331 (const org-agenda-span)
332 (choice (const :tag "Day" day)
333 (const :tag "Week" week)
334 (const :tag "Fortnight" fortnight)
335 (const :tag "Month" month)
336 (const :tag "Year" year)
337 (integer :tag "Custom")))
338 (list :tag "Fixed starting date"
339 (const org-agenda-start-day)
340 (string :value "2007-11-01"))
341 (list :tag "Start on day of week"
342 (const org-agenda-start-on-weekday)
343 (choice :value 1
344 (const :tag "Today" nil)
345 (integer :tag "Weekday No.")))
346 (list :tag "Include data from diary"
347 (const org-agenda-include-diary)
348 (boolean))
349 (list :tag "Deadline Warning days"
350 (const org-deadline-warning-days)
351 (integer :value 1))
352 (list :tag "Category filter preset"
353 (const org-agenda-category-filter-preset)
354 (list
355 (const :format "" quote)
356 (repeat
357 (string :tag "+category or -category"))))
358 (list :tag "Tags filter preset"
359 (const org-agenda-tag-filter-preset)
360 (list
361 (const :format "" quote)
362 (repeat
363 (string :tag "+tag or -tag"))))
364 (list :tag "Regexp filter preset"
365 (const org-agenda-regexp-filter-preset)
366 (list
367 (const :format "" quote)
368 (repeat
369 (string :tag "+regexp or -regexp"))))
370 (list :tag "Set daily/weekly entry types"
371 (const org-agenda-entry-types)
372 (list
373 (const :format "" quote)
374 (set :greedy t :value ,org-agenda-entry-types
375 (const :deadline)
376 (const :scheduled)
377 (const :deadline*)
378 (const :scheduled*)
379 (const :timestamp)
380 (const :sexp))))
381 (list :tag "Standard skipping condition"
382 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
383 (const org-agenda-skip-function)
384 (list
385 (const :format "" quote)
386 (list
387 (choice
388 :tag "Skipping range"
389 (const :tag "Skip entry" org-agenda-skip-entry-if)
390 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
391 (repeat :inline t :tag "Conditions for skipping"
392 (choice
393 :tag "Condition type"
394 (list :tag "Regexp matches" :inline t (const :format "" regexp) (regexp))
395 (list :tag "Regexp does not match" :inline t (const :format "" notregexp) (regexp))
396 (list :tag "TODO state is" :inline t
397 (const todo)
398 (choice
399 (const :tag "any not-done state" todo)
400 (const :tag "any done state" done)
401 (const :tag "any state" any)
402 (list :tag "Keyword list"
403 (const :format "" quote)
404 (repeat (string :tag "Keyword")))))
405 (list :tag "TODO state is not" :inline t
406 (const nottodo)
407 (choice
408 (const :tag "any not-done state" todo)
409 (const :tag "any done state" done)
410 (const :tag "any state" any)
411 (list :tag "Keyword list"
412 (const :format "" quote)
413 (repeat (string :tag "Keyword")))))
414 (const :tag "scheduled" scheduled)
415 (const :tag "not scheduled" notscheduled)
416 (const :tag "deadline" deadline)
417 (const :tag "no deadline" notdeadline)
418 (const :tag "timestamp" timestamp)
419 (const :tag "no timestamp" nottimestamp))))))
420 (list :tag "Non-standard skipping condition"
421 :value (org-agenda-skip-function)
422 (const org-agenda-skip-function)
423 (sexp :tag "Function or form (quoted!)"))
424 (list :tag "Any variable"
425 (variable :tag "Variable")
426 (sexp :tag "Value (sexp)"))))
427 "Selection of examples for agenda command settings.
428 This will be spliced into the custom type of
429 `org-agenda-custom-commands'.")
432 (defcustom org-agenda-custom-commands
433 '(("n" "Agenda and all TODO's" ((agenda "") (alltodo ""))))
434 "Custom commands for the agenda.
435 These commands will be offered on the splash screen displayed by the
436 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
438 (key desc type match settings files)
440 key The key (one or more characters as a string) to be associated
441 with the command.
442 desc A description of the command, when omitted or nil, a default
443 description is built using MATCH.
444 type The command type, any of the following symbols:
445 agenda The daily/weekly agenda.
446 todo Entries with a specific TODO keyword, in all agenda files.
447 search Entries containing search words entry or headline.
448 tags Tags/Property/TODO match in all agenda files.
449 tags-todo Tags/P/T match in all agenda files, TODO entries only.
450 todo-tree Sparse tree of specific TODO keyword in *current* file.
451 tags-tree Sparse tree with all tags matches in *current* file.
452 occur-tree Occur sparse tree for *current* file.
453 ... A user-defined function.
454 match What to search for:
455 - a single keyword for TODO keyword searches
456 - a tags match expression for tags searches
457 - a word search expression for text searches.
458 - a regular expression for occur searches
459 For all other commands, this should be the empty string.
460 settings A list of option settings, similar to that in a let form, so like
461 this: ((opt1 val1) (opt2 val2) ...). The values will be
462 evaluated at the moment of execution, so quote them when needed.
463 files A list of files file to write the produced agenda buffer to
464 with the command `org-store-agenda-views'.
465 If a file name ends in \".html\", an HTML version of the buffer
466 is written out. If it ends in \".ps\", a postscript version is
467 produced. Otherwise, only the plain text is written to the file.
469 You can also define a set of commands, to create a composite agenda buffer.
470 In this case, an entry looks like this:
472 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
474 where
476 desc A description string to be displayed in the dispatcher menu.
477 cmd An agenda command, similar to the above. However, tree commands
478 are not allowed, but instead you can get agenda and global todo list.
479 So valid commands for a set are:
480 (agenda \"\" settings)
481 (alltodo \"\" settings)
482 (stuck \"\" settings)
483 (todo \"match\" settings files)
484 (search \"match\" settings files)
485 (tags \"match\" settings files)
486 (tags-todo \"match\" settings files)
488 Each command can carry a list of options, and another set of options can be
489 given for the whole set of commands. Individual command options take
490 precedence over the general options.
492 When using several characters as key to a command, the first characters
493 are prefix commands. For the dispatcher to display useful information, you
494 should provide a description for the prefix, like
496 (setq org-agenda-custom-commands
497 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
498 (\"hl\" tags \"+HOME+Lisa\")
499 (\"hp\" tags \"+HOME+Peter\")
500 (\"hk\" tags \"+HOME+Kim\")))"
501 :group 'org-agenda-custom-commands
502 :type `(repeat
503 (choice :value ("x" "Describe command here" tags "" nil)
504 (list :tag "Single command"
505 (string :tag "Access Key(s) ")
506 (option (string :tag "Description"))
507 (choice
508 (const :tag "Agenda" agenda)
509 (const :tag "TODO list" alltodo)
510 (const :tag "Search words" search)
511 (const :tag "Stuck projects" stuck)
512 (const :tag "Tags/Property match (all agenda files)" tags)
513 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
514 (const :tag "TODO keyword search (all agenda files)" todo)
515 (const :tag "Tags sparse tree (current buffer)" tags-tree)
516 (const :tag "TODO keyword tree (current buffer)" todo-tree)
517 (const :tag "Occur tree (current buffer)" occur-tree)
518 (sexp :tag "Other, user-defined function"))
519 (string :tag "Match (only for some commands)")
520 ,org-agenda-custom-commands-local-options
521 (option (repeat :tag "Export" (file :tag "Export to"))))
522 (list :tag "Command series, all agenda files"
523 (string :tag "Access Key(s)")
524 (string :tag "Description ")
525 (repeat :tag "Component"
526 (choice
527 (list :tag "Agenda"
528 (const :format "" agenda)
529 (const :tag "" :format "" "")
530 ,org-agenda-custom-commands-local-options)
531 (list :tag "TODO list (all keywords)"
532 (const :format "" alltodo)
533 (const :tag "" :format "" "")
534 ,org-agenda-custom-commands-local-options)
535 (list :tag "Search words"
536 (const :format "" search)
537 (string :tag "Match")
538 ,org-agenda-custom-commands-local-options)
539 (list :tag "Stuck projects"
540 (const :format "" stuck)
541 (const :tag "" :format "" "")
542 ,org-agenda-custom-commands-local-options)
543 (list :tag "Tags search"
544 (const :format "" tags)
545 (string :tag "Match")
546 ,org-agenda-custom-commands-local-options)
547 (list :tag "Tags search, TODO entries only"
548 (const :format "" tags-todo)
549 (string :tag "Match")
550 ,org-agenda-custom-commands-local-options)
551 (list :tag "TODO keyword search"
552 (const :format "" todo)
553 (string :tag "Match")
554 ,org-agenda-custom-commands-local-options)
555 (list :tag "Other, user-defined function"
556 (symbol :tag "function")
557 (string :tag "Match")
558 ,org-agenda-custom-commands-local-options)))
560 (repeat :tag "Settings for entire command set"
561 (list (variable :tag "Any variable")
562 (sexp :tag "Value")))
563 (option (repeat :tag "Export" (file :tag "Export to"))))
564 (cons :tag "Prefix key documentation"
565 (string :tag "Access Key(s)")
566 (string :tag "Description ")))))
568 (defcustom org-agenda-query-register ?o
569 "The register holding the current query string.
570 The purpose of this is that if you construct a query string interactively,
571 you can then use it to define a custom command."
572 :group 'org-agenda-custom-commands
573 :type 'character)
575 (defcustom org-stuck-projects
576 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
577 "How to identify stuck projects.
578 This is a list of four items:
579 1. A tags/todo/property matcher string that is used to identify a project.
580 See the manual for a description of tag and property searches.
581 The entire tree below a headline matched by this is considered one project.
582 2. A list of TODO keywords identifying non-stuck projects.
583 If the project subtree contains any headline with one of these todo
584 keywords, the project is considered to be not stuck. If you specify
585 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
586 3. A list of tags identifying non-stuck projects.
587 If the project subtree contains any headline with one of these tags,
588 the project is considered to be not stuck. If you specify \"*\" as
589 a tag, any tag will mark the project unstuck. Note that this is about
590 the explicit presence of a tag somewhere in the subtree, inherited
591 tags do not count here. If inherited tags make a project not stuck,
592 use \"-TAG\" in the tags part of the matcher under (1.) above.
593 4. An arbitrary regular expression matching non-stuck projects.
595 If the project turns out to be not stuck, search continues also in the
596 subtree to see if any of the subtasks have project status.
598 See also the variable `org-tags-match-list-sublevels' which applies
599 to projects matched by this search as well.
601 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
602 or `C-c a #' to produce the list."
603 :group 'org-agenda-custom-commands
604 :type '(list
605 (string :tag "Tags/TODO match to identify a project")
606 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
607 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
608 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
610 (defcustom org-agenda-filter-effort-default-operator "<"
611 "The default operator for effort estimate filtering.
612 If you select an effort estimate limit without first pressing an operator,
613 this one will be used."
614 :group 'org-agenda-custom-commands
615 :type '(choice (const :tag "less or equal" "<")
616 (const :tag "greater or equal"">")
617 (const :tag "equal" "=")))
619 (defgroup org-agenda-skip nil
620 "Options concerning skipping parts of agenda files."
621 :tag "Org Agenda Skip"
622 :group 'org-agenda)
624 (defcustom org-agenda-skip-function-global nil
625 "Function to be called at each match during agenda construction.
626 If this function returns nil, the current match should not be skipped.
627 If the function decided to skip an agenda match, is must return the
628 buffer position from which the search should be continued.
629 This may also be a Lisp form, which will be evaluated.
631 This variable will be applied to every agenda match, including
632 tags/property searches and TODO lists. So try to make the test function
633 do its checking as efficiently as possible. To implement a skipping
634 condition just for specific agenda commands, use the variable
635 `org-agenda-skip-function' which can be set in the options section
636 of custom agenda commands."
637 :group 'org-agenda-skip
638 :type 'sexp)
640 (defgroup org-agenda-daily/weekly nil
641 "Options concerning the daily/weekly agenda."
642 :tag "Org Agenda Daily/Weekly"
643 :group 'org-agenda)
644 (defgroup org-agenda-todo-list nil
645 "Options concerning the global todo list agenda view."
646 :tag "Org Agenda Todo List"
647 :group 'org-agenda)
648 (defgroup org-agenda-match-view nil
649 "Options concerning the general tags/property/todo match agenda view."
650 :tag "Org Agenda Match View"
651 :group 'org-agenda)
652 (defgroup org-agenda-search-view nil
653 "Options concerning the general tags/property/todo match agenda view."
654 :tag "Org Agenda Match View"
655 :group 'org-agenda)
657 (defvar org-agenda-archives-mode nil
658 "Non-nil means the agenda will include archived items.
659 If this is the symbol `trees', trees in the selected agenda scope
660 that are marked with the ARCHIVE tag will be included anyway. When this is
661 t, also all archive files associated with the current selection of agenda
662 files will be included.")
664 (defcustom org-agenda-restriction-lock-highlight-subtree t
665 "Non-nil means highlight the whole subtree when restriction is active.
666 Otherwise only highlight the headline. Highlighting the whole subtree is
667 useful to ensure no edits happen beyond the restricted region."
668 :group 'org-agenda
669 :type 'boolean)
671 (defcustom org-agenda-skip-comment-trees t
672 "Non-nil means skip trees that start with the COMMENT keyword.
673 When nil, these trees are also scanned by agenda commands."
674 :group 'org-agenda-skip
675 :type 'boolean)
677 (defcustom org-agenda-todo-list-sublevels t
678 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
679 When nil, the sublevels of a TODO entry are not checked, resulting in
680 potentially much shorter TODO lists."
681 :group 'org-agenda-skip
682 :group 'org-agenda-todo-list
683 :type 'boolean)
685 (defcustom org-agenda-todo-ignore-with-date nil
686 "Non-nil means don't show entries with a date in the global todo list.
687 You can use this if you prefer to mark mere appointments with a TODO keyword,
688 but don't want them to show up in the TODO list.
689 When this is set, it also covers deadlines and scheduled items, the settings
690 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
691 will be ignored.
692 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
693 :group 'org-agenda-skip
694 :group 'org-agenda-todo-list
695 :type 'boolean)
697 (defcustom org-agenda-todo-ignore-timestamp nil
698 "Non-nil means don't show entries with a timestamp.
699 This applies when creating the global todo list.
700 Valid values are:
702 past Don't show entries for today or in the past.
704 future Don't show entries with a timestamp in the future.
705 The idea behind this is that if it has a future
706 timestamp, you don't want to think about it until the
707 date.
709 all Don't show any entries with a timestamp in the global todo list.
710 The idea behind this is that by setting a timestamp, you
711 have already \"taken care\" of this item.
713 This variable can also have an integer as a value. If positive (N),
714 todos with a timestamp N or more days in the future will be ignored. If
715 negative (-N), todos with a timestamp N or more days in the past will be
716 ignored. If 0, todos with a timestamp either today or in the future will
717 be ignored. For example, a value of -1 will exclude todos with a
718 timestamp in the past (yesterday or earlier), while a value of 7 will
719 exclude todos with a timestamp a week or more in the future.
721 See also `org-agenda-todo-ignore-with-date'.
722 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
723 to make his option also apply to the tags-todo list."
724 :group 'org-agenda-skip
725 :group 'org-agenda-todo-list
726 :version "24.1"
727 :type '(choice
728 (const :tag "Ignore future timestamp todos" future)
729 (const :tag "Ignore past or present timestamp todos" past)
730 (const :tag "Ignore all timestamp todos" all)
731 (const :tag "Show timestamp todos" nil)
732 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
734 (defcustom org-agenda-todo-ignore-scheduled nil
735 "Non-nil means, ignore some scheduled TODO items when making TODO list.
736 This applies when creating the global todo list.
737 Valid values are:
739 past Don't show entries scheduled today or in the past.
741 future Don't show entries scheduled in the future.
742 The idea behind this is that by scheduling it, you don't want to
743 think about it until the scheduled date.
745 all Don't show any scheduled entries in the global todo list.
746 The idea behind this is that by scheduling it, you have already
747 \"taken care\" of this item.
749 t Same as `all', for backward compatibility.
751 This variable can also have an integer as a value. See
752 `org-agenda-todo-ignore-timestamp' for more details.
754 See also `org-agenda-todo-ignore-with-date'.
755 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
756 to make his option also apply to the tags-todo list."
757 :group 'org-agenda-skip
758 :group 'org-agenda-todo-list
759 :type '(choice
760 (const :tag "Ignore future-scheduled todos" future)
761 (const :tag "Ignore past- or present-scheduled todos" past)
762 (const :tag "Ignore all scheduled todos" all)
763 (const :tag "Ignore all scheduled todos (compatibility)" t)
764 (const :tag "Show scheduled todos" nil)
765 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
767 (defcustom org-agenda-todo-ignore-deadlines nil
768 "Non-nil means ignore some deadline TODO items when making TODO list.
769 There are different motivations for using different values, please think
770 carefully when configuring this variable.
772 This applies when creating the global todo list.
773 Valid values are:
775 near Don't show near deadline entries. A deadline is near when it is
776 closer than `org-deadline-warning-days' days. The idea behind this
777 is that such items will appear in the agenda anyway.
779 far Don't show TODO entries where a deadline has been defined, but
780 the deadline is not near. This is useful if you don't want to
781 use the todo list to figure out what to do now.
783 past Don't show entries with a deadline timestamp for today or in the past.
785 future Don't show entries with a deadline timestamp in the future, not even
786 when they become `near' ones. Use it with caution.
788 all Ignore all TODO entries that do have a deadline.
790 t Same as `near', for backward compatibility.
792 This variable can also have an integer as a value. See
793 `org-agenda-todo-ignore-timestamp' for more details.
795 See also `org-agenda-todo-ignore-with-date'.
796 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
797 to make his option also apply to the tags-todo list."
798 :group 'org-agenda-skip
799 :group 'org-agenda-todo-list
800 :type '(choice
801 (const :tag "Ignore near deadlines" near)
802 (const :tag "Ignore near deadlines (compatibility)" t)
803 (const :tag "Ignore far deadlines" far)
804 (const :tag "Ignore all TODOs with a deadlines" all)
805 (const :tag "Show all TODOs, even if they have a deadline" nil)
806 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
808 (defcustom org-agenda-todo-ignore-time-comparison-use-seconds nil
809 "Time unit to use when possibly ignoring an agenda item.
811 See the docstring of various `org-agenda-todo-ignore-*' options.
812 The default is to compare time stamps using days. An item is thus
813 considered to be in the future if it is at least one day after today.
814 Non-nil means to compare time stamps using seconds. An item is then
815 considered future if it has a time value later than current time."
816 :group 'org-agenda-skip
817 :group 'org-agenda-todo-list
818 :version "24.4"
819 :package-version '(Org . "8.0")
820 :type '(choice
821 (const :tag "Compare time with days" nil)
822 (const :tag "Compare time with seconds" t)))
824 (defcustom org-agenda-tags-todo-honor-ignore-options nil
825 "Non-nil means honor todo-list ignores options also in tags-todo search.
826 The variables
827 `org-agenda-todo-ignore-with-date',
828 `org-agenda-todo-ignore-timestamp',
829 `org-agenda-todo-ignore-scheduled',
830 `org-agenda-todo-ignore-deadlines'
831 make the global TODO list skip entries that have time stamps of certain
832 kinds. If this option is set, the same options will also apply for the
833 tags-todo search, which is the general tags/property matcher
834 restricted to unfinished TODO entries only."
835 :group 'org-agenda-skip
836 :group 'org-agenda-todo-list
837 :group 'org-agenda-match-view
838 :type 'boolean)
840 (defcustom org-agenda-skip-scheduled-if-done nil
841 "Non-nil means don't show scheduled items in agenda when they are done.
842 This is relevant for the daily/weekly agenda, not for the TODO list. And
843 it applies only to the actual date of the scheduling. Warnings about
844 an item with a past scheduling dates are always turned off when the item
845 is DONE."
846 :group 'org-agenda-skip
847 :group 'org-agenda-daily/weekly
848 :type 'boolean)
850 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
851 "Non-nil means skip scheduling line if same entry shows because of deadline.
853 In the agenda of today, an entry can show up multiple times
854 because it is both scheduled and has a nearby deadline, and maybe
855 a plain time stamp as well.
857 When this variable is nil, the entry will be shown several times.
859 When set to t, then only the deadline is shown and the fact that
860 the entry is scheduled today or was scheduled previously is not
861 shown.
863 When set to the symbol `not-today', skip scheduled previously,
864 but not scheduled today.
866 When set to the symbol `repeated-after-deadline', skip scheduled
867 items if they are repeated beyond the current deadline."
868 :group 'org-agenda-skip
869 :group 'org-agenda-daily/weekly
870 :type '(choice
871 (const :tag "Never" nil)
872 (const :tag "Always" t)
873 (const :tag "Not when scheduled today" not-today)
874 (const :tag "When repeated past deadline" repeated-after-deadline)))
876 (defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
877 "Non-nil means skip timestamp line if same entry shows because of deadline.
878 In the agenda of today, an entry can show up multiple times
879 because it has both a plain timestamp and has a nearby deadline.
880 When this variable is t, then only the deadline is shown and the
881 fact that the entry has a timestamp for or including today is not
882 shown. When this variable is nil, the entry will be shown
883 several times."
884 :group 'org-agenda-skip
885 :group 'org-agenda-daily/weekly
886 :version "24.1"
887 :type '(choice
888 (const :tag "Never" nil)
889 (const :tag "Always" t)))
891 (defcustom org-agenda-skip-deadline-if-done nil
892 "Non-nil means don't show deadlines when the corresponding item is done.
893 When nil, the deadline is still shown and should give you a happy feeling.
894 This is relevant for the daily/weekly agenda. And it applied only to the
895 actually date of the deadline. Warnings about approaching and past-due
896 deadlines are always turned off when the item is DONE."
897 :group 'org-agenda-skip
898 :group 'org-agenda-daily/weekly
899 :type 'boolean)
901 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
902 "Non-nil means skip deadline prewarning when entry is also scheduled.
903 This will apply on all days where a prewarning for the deadline would
904 be shown, but not at the day when the entry is actually due. On that day,
905 the deadline will be shown anyway.
906 This variable may be set to nil, t, the symbol `pre-scheduled',
907 or a number which will then give the number of days before the actual
908 deadline when the prewarnings should resume. The symbol `pre-scheduled'
909 eliminates the deadline prewarning only prior to the scheduled date.
910 This can be used in a workflow where the first showing of the deadline will
911 trigger you to schedule it, and then you don't want to be reminded of it
912 because you will take care of it on the day when scheduled."
913 :group 'org-agenda-skip
914 :group 'org-agenda-daily/weekly
915 :version "24.1"
916 :type '(choice
917 (const :tag "Always show prewarning" nil)
918 (const :tag "Remove prewarning prior to scheduled date" pre-scheduled)
919 (const :tag "Remove prewarning if entry is scheduled" t)
920 (integer :tag "Restart prewarning N days before deadline")))
922 (defcustom org-agenda-skip-scheduled-delay-if-deadline nil
923 "Non-nil means skip scheduled delay when entry also has a deadline.
924 This variable may be set to nil, t, the symbol `post-deadline',
925 or a number which will then give the number of days after the actual
926 scheduled date when the delay should expire. The symbol `post-deadline'
927 eliminates the schedule delay when the date is posterior to the deadline."
928 :group 'org-agenda-skip
929 :group 'org-agenda-daily/weekly
930 :version "24.4"
931 :package-version '(Org . "8.0")
932 :type '(choice
933 (const :tag "Always honor delay" nil)
934 (const :tag "Ignore delay if posterior to the deadline" post-deadline)
935 (const :tag "Ignore delay if entry has a deadline" t)
936 (integer :tag "Honor delay up until N days after the scheduled date")))
938 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
939 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
940 When non-nil, after the search for timestamps has matched once in an
941 entry, the rest of the entry will not be searched."
942 :group 'org-agenda-skip
943 :type 'boolean)
945 (defcustom org-agenda-skip-timestamp-if-done nil
946 "Non-nil means don't select item by timestamp or -range if it is DONE."
947 :group 'org-agenda-skip
948 :group 'org-agenda-daily/weekly
949 :type 'boolean)
951 (defcustom org-agenda-dim-blocked-tasks t
952 "Non-nil means dim blocked tasks in the agenda display.
953 This causes some overhead during agenda construction, but if you
954 have turned on `org-enforce-todo-dependencies',
955 `org-enforce-todo-checkbox-dependencies', or any other blocking
956 mechanism, this will create useful feedback in the agenda.
958 Instead of t, this variable can also have the value `invisible'.
959 Then blocked tasks will be invisible and only become visible when
960 they become unblocked. An exemption to this behavior is when a task is
961 blocked because of unchecked checkboxes below it. Since checkboxes do
962 not show up in the agenda views, making this task invisible you remove any
963 trace from agenda views that there is something to do. Therefore, a task
964 that is blocked because of checkboxes will never be made invisible, it
965 will only be dimmed."
966 :group 'org-agenda-daily/weekly
967 :group 'org-agenda-todo-list
968 :version "24.3"
969 :type '(choice
970 (const :tag "Do not dim" nil)
971 (const :tag "Dim to a gray face" t)
972 (const :tag "Make invisible" invisible)))
974 (defcustom org-timeline-show-empty-dates 3
975 "Non-nil means `org-timeline' also shows dates without an entry.
976 When nil, only the days which actually have entries are shown.
977 When t, all days between the first and the last date are shown.
978 When an integer, show also empty dates, but if there is a gap of more than
979 N days, just insert a special line indicating the size of the gap."
980 :group 'org-agenda-skip
981 :type '(choice
982 (const :tag "None" nil)
983 (const :tag "All" t)
984 (integer :tag "at most")))
986 (defgroup org-agenda-startup nil
987 "Options concerning initial settings in the Agenda in Org Mode."
988 :tag "Org Agenda Startup"
989 :group 'org-agenda)
991 (defcustom org-agenda-menu-show-matcher t
992 "Non-nil means show the match string in the agenda dispatcher menu.
993 When nil, the matcher string is not shown, but is put into the help-echo
994 property so than moving the mouse over the command shows it.
995 Setting it to nil is good if matcher strings are very long and/or if
996 you want to use two-columns display (see `org-agenda-menu-two-columns')."
997 :group 'org-agenda
998 :version "24.1"
999 :type 'boolean)
1001 (define-obsolete-variable-alias 'org-agenda-menu-two-column 'org-agenda-menu-two-columns "24.3")
1003 (defcustom org-agenda-menu-two-columns nil
1004 "Non-nil means, use two columns to show custom commands in the dispatcher.
1005 If you use this, you probably want to set `org-agenda-menu-show-matcher'
1006 to nil."
1007 :group 'org-agenda
1008 :version "24.1"
1009 :type 'boolean)
1011 (define-obsolete-variable-alias 'org-finalize-agenda-hook 'org-agenda-finalize-hook "24.3")
1012 (defcustom org-agenda-finalize-hook nil
1013 "Hook run just before displaying an agenda buffer.
1014 The buffer is still writable when the hook is called.
1016 You can modify some of the buffer substrings but you should be
1017 extra careful not to modify the text properties of the agenda
1018 headlines as the agenda display heavily relies on them."
1019 :group 'org-agenda-startup
1020 :type 'hook)
1022 (defcustom org-agenda-mouse-1-follows-link nil
1023 "Non-nil means mouse-1 on a link will follow the link in the agenda.
1024 A longer mouse click will still set point. Does not work on XEmacs.
1025 Needs to be set before org.el is loaded."
1026 :group 'org-agenda-startup
1027 :type 'boolean)
1029 (defcustom org-agenda-start-with-follow-mode nil
1030 "The initial value of follow mode in a newly created agenda window."
1031 :group 'org-agenda-startup
1032 :type 'boolean)
1034 (defcustom org-agenda-follow-indirect nil
1035 "Non-nil means `org-agenda-follow-mode' displays only the
1036 current item's tree, in an indirect buffer."
1037 :group 'org-agenda
1038 :version "24.1"
1039 :type 'boolean)
1041 (defcustom org-agenda-show-outline-path t
1042 "Non-nil means show outline path in echo area after line motion."
1043 :group 'org-agenda-startup
1044 :type 'boolean)
1046 (defcustom org-agenda-start-with-entry-text-mode nil
1047 "The initial value of entry-text-mode in a newly created agenda window."
1048 :group 'org-agenda-startup
1049 :type 'boolean)
1051 (defcustom org-agenda-entry-text-maxlines 5
1052 "Number of text lines to be added when `E' is pressed in the agenda.
1054 Note that this variable only used during agenda display. Add add entry text
1055 when exporting the agenda, configure the variable
1056 `org-agenda-add-entry-ext-maxlines'."
1057 :group 'org-agenda
1058 :type 'integer)
1060 (defcustom org-agenda-entry-text-exclude-regexps nil
1061 "List of regular expressions to clean up entry text.
1062 The complete matches of all regular expressions in this list will be
1063 removed from entry text before it is shown in the agenda."
1064 :group 'org-agenda
1065 :type '(repeat (regexp)))
1067 (defcustom org-agenda-entry-text-leaders " > "
1068 "Text prepended to the entry text in agenda buffers."
1069 :version "24.4"
1070 :package-version '(Org . "8.0")
1071 :group 'org-agenda
1072 :type 'string)
1074 (defvar org-agenda-entry-text-cleanup-hook nil
1075 "Hook that is run after basic cleanup of entry text to be shown in agenda.
1076 This cleanup is done in a temporary buffer, so the function may inspect and
1077 change the entire buffer.
1078 Some default stuff like drawers and scheduling/deadline dates will already
1079 have been removed when this is called, as will any matches for regular
1080 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
1082 (defvar org-agenda-include-inactive-timestamps nil
1083 "Non-nil means include inactive time stamps in agenda and timeline.
1084 Dynamically scoped.")
1086 (defgroup org-agenda-windows nil
1087 "Options concerning the windows used by the Agenda in Org Mode."
1088 :tag "Org Agenda Windows"
1089 :group 'org-agenda)
1091 (defcustom org-agenda-window-setup 'reorganize-frame
1092 "How the agenda buffer should be displayed.
1093 Possible values for this option are:
1095 current-window Show agenda in the current window, keeping all other windows.
1096 other-window Use `switch-to-buffer-other-window' to display agenda.
1097 reorganize-frame Show only two windows on the current frame, the current
1098 window and the agenda.
1099 other-frame Use `switch-to-buffer-other-frame' to display agenda.
1100 Also, when exiting the agenda, kill that frame.
1101 See also the variable `org-agenda-restore-windows-after-quit'."
1102 :group 'org-agenda-windows
1103 :type '(choice
1104 (const current-window)
1105 (const other-frame)
1106 (const other-window)
1107 (const reorganize-frame)))
1109 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
1110 "The min and max height of the agenda window as a fraction of frame height.
1111 The value of the variable is a cons cell with two numbers between 0 and 1.
1112 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
1113 :group 'org-agenda-windows
1114 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
1116 (defcustom org-agenda-restore-windows-after-quit nil
1117 "Non-nil means restore window configuration upon exiting agenda.
1118 Before the window configuration is changed for displaying the agenda,
1119 the current status is recorded. When the agenda is exited with
1120 `q' or `x' and this option is set, the old state is restored. If
1121 `org-agenda-window-setup' is `other-frame', the value of this
1122 option will be ignored."
1123 :group 'org-agenda-windows
1124 :type 'boolean)
1126 (defcustom org-agenda-ndays nil
1127 "Number of days to include in overview display.
1128 Should be 1 or 7.
1129 Obsolete, see `org-agenda-span'."
1130 :group 'org-agenda-daily/weekly
1131 :type '(choice (const nil)
1132 (integer)))
1134 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
1136 (defcustom org-agenda-span 'week
1137 "Number of days to include in overview display.
1138 Can be day, week, month, year, or any number of days.
1139 Custom commands can set this variable in the options section."
1140 :group 'org-agenda-daily/weekly
1141 :type '(choice (const :tag "Day" day)
1142 (const :tag "Week" week)
1143 (const :tag "Fortnight" fortnight)
1144 (const :tag "Month" month)
1145 (const :tag "Year" year)
1146 (integer :tag "Custom")))
1148 (defcustom org-agenda-start-on-weekday 1
1149 "Non-nil means start the overview always on the specified weekday.
1150 0 denotes Sunday, 1 denotes Monday, etc.
1151 When nil, always start on the current day.
1152 Custom commands can set this variable in the options section."
1153 :group 'org-agenda-daily/weekly
1154 :type '(choice (const :tag "Today" nil)
1155 (integer :tag "Weekday No.")))
1157 (defcustom org-agenda-show-all-dates t
1158 "Non-nil means `org-agenda' shows every day in the selected range.
1159 When nil, only the days which actually have entries are shown."
1160 :group 'org-agenda-daily/weekly
1161 :type 'boolean)
1163 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1164 "Format string for displaying dates in the agenda.
1165 Used by the daily/weekly agenda and by the timeline. This should be
1166 a format string understood by `format-time-string', or a function returning
1167 the formatted date as a string. The function must take a single argument,
1168 a calendar-style date list like (month day year)."
1169 :group 'org-agenda-daily/weekly
1170 :type '(choice
1171 (string :tag "Format string")
1172 (function :tag "Function")))
1174 (defun org-agenda-format-date-aligned (date)
1175 "Format a DATE string for display in the daily/weekly agenda, or timeline.
1176 This function makes sure that dates are aligned for easy reading."
1177 (require 'cal-iso)
1178 (let* ((dayname (calendar-day-name date))
1179 (day (cadr date))
1180 (day-of-week (calendar-day-of-week date))
1181 (month (car date))
1182 (monthname (calendar-month-name month))
1183 (year (nth 2 date))
1184 (iso-week (org-days-to-iso-week
1185 (calendar-absolute-from-gregorian date)))
1186 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1187 (1- year))
1188 ((and (= month 12) (<= iso-week 1))
1189 (1+ year))
1190 (t year)))
1191 (weekstring (if (= day-of-week 1)
1192 (format " W%02d" iso-week)
1193 "")))
1194 (format "%-10s %2d %s %4d%s"
1195 dayname day monthname year weekstring)))
1197 (defcustom org-agenda-time-leading-zero nil
1198 "Non-nil means use leading zero for military times in agenda.
1199 For example, 9:30am would become 09:30 rather than 9:30."
1200 :group 'org-agenda-daily/weekly
1201 :version "24.1"
1202 :type 'boolean)
1204 (defcustom org-agenda-timegrid-use-ampm nil
1205 "When set, show AM/PM style timestamps on the timegrid."
1206 :group 'org-agenda
1207 :version "24.1"
1208 :type 'boolean)
1210 (defun org-agenda-time-of-day-to-ampm (time)
1211 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1212 (let* ((hour-number (string-to-number (substring time 0 -3)))
1213 (minute (substring time -2))
1214 (ampm "am"))
1215 (cond
1216 ((equal hour-number 12)
1217 (setq ampm "pm"))
1218 ((> hour-number 12)
1219 (setq ampm "pm")
1220 (setq hour-number (- hour-number 12))))
1221 (concat
1222 (if org-agenda-time-leading-zero
1223 (format "%02d" hour-number)
1224 (format "%02s" (number-to-string hour-number)))
1225 ":" minute ampm)))
1227 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1228 "Conditionally convert TIME to AM/PM format based on `org-agenda-timegrid-use-ampm'."
1229 (if org-agenda-timegrid-use-ampm
1230 (org-agenda-time-of-day-to-ampm time)
1231 time))
1233 (defcustom org-agenda-weekend-days '(6 0)
1234 "Which days are weekend?
1235 These days get the special face `org-agenda-date-weekend' in the agenda
1236 and timeline buffers."
1237 :group 'org-agenda-daily/weekly
1238 :type '(set :greedy t
1239 (const :tag "Monday" 1)
1240 (const :tag "Tuesday" 2)
1241 (const :tag "Wednesday" 3)
1242 (const :tag "Thursday" 4)
1243 (const :tag "Friday" 5)
1244 (const :tag "Saturday" 6)
1245 (const :tag "Sunday" 0)))
1247 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1248 "Non-nil means jump to today when moving a past date forward in time.
1249 When using S-right in the agenda to move a a date forward, and the date
1250 stamp currently points to the past, the first key press will move it
1251 to today. WHen nil, just move one day forward even if the date stays
1252 in the past."
1253 :group 'org-agenda-daily/weekly
1254 :version "24.1"
1255 :type 'boolean)
1257 (defcustom org-agenda-include-diary nil
1258 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1259 Custom commands can set this variable in the options section."
1260 :group 'org-agenda-daily/weekly
1261 :type 'boolean)
1263 (defcustom org-agenda-include-deadlines t
1264 "If non-nil, include entries within their deadline warning period.
1265 Custom commands can set this variable in the options section."
1266 :group 'org-agenda-daily/weekly
1267 :version "24.1"
1268 :type 'boolean)
1270 (defcustom org-agenda-repeating-timestamp-show-all t
1271 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1272 When set to a list of strings, only show occurrences of repeating
1273 stamps for these TODO keywords. When nil, only one occurrence is
1274 shown, either today or the nearest into the future."
1275 :group 'org-agenda-daily/weekly
1276 :type '(choice
1277 (const :tag "Show repeating stamps" t)
1278 (repeat :tag "Show repeating stamps for these TODO keywords"
1279 (string :tag "TODO Keyword"))
1280 (const :tag "Don't show repeating stamps" nil)))
1282 (defcustom org-scheduled-past-days 10000
1283 "Number of days to continue listing scheduled items not marked DONE.
1284 When an item is scheduled on a date, it shows up in the agenda on this
1285 day and will be listed until it is marked done for the number of days
1286 given here."
1287 :group 'org-agenda-daily/weekly
1288 :type 'integer)
1290 (defcustom org-agenda-log-mode-items '(closed clock)
1291 "List of items that should be shown in agenda log mode.
1292 This list may contain the following symbols:
1294 closed Show entries that have been closed on that day.
1295 clock Show entries that have received clocked time on that day.
1296 state Show all logged state changes.
1297 Note that instead of changing this variable, you can also press `C-u l' in
1298 the agenda to display all available LOG items temporarily."
1299 :group 'org-agenda-daily/weekly
1300 :type '(set :greedy t (const closed) (const clock) (const state)))
1302 (defcustom org-agenda-clock-consistency-checks
1303 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1304 :gap-ok-around ("4:00")
1305 :default-face ((:background "DarkRed") (:foreground "white"))
1306 :overlap-face nil :gap-face nil :no-end-time-face nil
1307 :long-face nil :short-face nil)
1308 "This is a property list, with the following keys:
1310 :max-duration Mark clocking chunks that are longer than this time.
1311 This is a time string like \"HH:MM\", or the number
1312 of minutes as an integer.
1314 :min-duration Mark clocking chunks that are shorter that this.
1315 This is a time string like \"HH:MM\", or the number
1316 of minutes as an integer.
1318 :max-gap Mark gaps between clocking chunks that are longer than
1319 this duration. A number of minutes, or a string
1320 like \"HH:MM\".
1322 :gap-ok-around List of times during the day which are usually not working
1323 times. When a gap is detected, but the gap contains any
1324 of these times, the gap is *not* reported. For example,
1325 if this is (\"4:00\" \"13:00\") then gaps that contain
1326 4:00 in the morning (i.e. the night) and 13:00
1327 (i.e. a typical lunch time) do not cause a warning.
1328 You should have at least one time during the night in this
1329 list, or otherwise the first task each morning will trigger
1330 a warning because it follows a long gap.
1332 Furthermore, the following properties can be used to define faces for
1333 issue display.
1335 :default-face the default face, if the specific face is undefined
1336 :overlap-face face for overlapping clocks
1337 :gap-face face for gaps between clocks
1338 :no-end-time-face face for incomplete clocks
1339 :long-face face for clock intervals that are too long
1340 :short-face face for clock intervals that are too short"
1341 :group 'org-agenda-daily/weekly
1342 :group 'org-clock
1343 :version "24.1"
1344 :type 'plist)
1346 (defcustom org-agenda-log-mode-add-notes t
1347 "Non-nil means add first line of notes to log entries in agenda views.
1348 If a log item like a state change or a clock entry is associated with
1349 notes, the first line of these notes will be added to the entry in the
1350 agenda display."
1351 :group 'org-agenda-daily/weekly
1352 :type 'boolean)
1354 (defcustom org-agenda-start-with-log-mode nil
1355 "The initial value of log-mode in a newly created agenda window.
1356 See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
1357 explanations on the possible values."
1358 :group 'org-agenda-startup
1359 :group 'org-agenda-daily/weekly
1360 :type '(choice (const :tag "Don't show log items" nil)
1361 (const :tag "Show only log items" only)
1362 (const :tag "Show all possible log items" clockcheck)
1363 (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
1364 (choice (const :tag "Show closed log items" closed)
1365 (const :tag "Show clocked log items" clock)
1366 (const :tag "Show all logged state changes" state)))))
1368 (defcustom org-agenda-start-with-clockreport-mode nil
1369 "The initial value of clockreport-mode in a newly created agenda window."
1370 :group 'org-agenda-startup
1371 :group 'org-agenda-daily/weekly
1372 :type 'boolean)
1374 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1375 "Property list with parameters for the clocktable in clockreport mode.
1376 This is the display mode that shows a clock table in the daily/weekly
1377 agenda, the properties for this dynamic block can be set here.
1378 The usual clocktable parameters are allowed here, but you cannot set
1379 the properties :name, :tstart, :tend, :block, and :scope - these will
1380 be overwritten to make sure the content accurately reflects the
1381 current display in the agenda."
1382 :group 'org-agenda-daily/weekly
1383 :type 'plist)
1385 (defcustom org-agenda-search-view-always-boolean nil
1386 "Non-nil means the search string is interpreted as individual parts.
1388 The search string for search view can either be interpreted as a phrase,
1389 or as a list of snippets that define a boolean search for a number of
1390 strings.
1392 When this is non-nil, the string will be split on whitespace, and each
1393 snippet will be searched individually, and all must match in order to
1394 select an entry. A snippet is then a single string of non-white
1395 characters, or a string in double quotes, or a regexp in {} braces.
1396 If a snippet is preceded by \"-\", the snippet must *not* match.
1397 \"+\" is syntactic sugar for positive selection. Each snippet may
1398 be found as a full word or a partial word, but see the variable
1399 `org-agenda-search-view-force-full-words'.
1401 When this is nil, search will look for the entire search phrase as one,
1402 with each space character matching any amount of whitespace, including
1403 line breaks.
1405 Even when this is nil, you can still switch to Boolean search dynamically
1406 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1407 is a regexp marked with braces like \"{abc}\", this will also switch to
1408 boolean search."
1409 :group 'org-agenda-search-view
1410 :version "24.1"
1411 :type 'boolean)
1413 (org-defvaralias 'org-agenda-search-view-search-words-only
1414 'org-agenda-search-view-always-boolean)
1416 (defcustom org-agenda-search-view-force-full-words nil
1417 "Non-nil means, search words must be matches as complete words.
1418 When nil, they may also match part of a word."
1419 :group 'org-agenda-search-view
1420 :version "24.1"
1421 :type 'boolean)
1423 (defcustom org-agenda-search-view-max-outline-level 0
1424 "Maximum outline level to display in search view.
1425 E.g. when this is set to 1, the search view will only
1426 show headlines of level 1. When set to 0, the default
1427 value, don't limit agenda view by outline level."
1428 :group 'org-agenda-search-view
1429 :version "24.4"
1430 :package-version '(Org . "8.3")
1431 :type 'integer)
1433 (defgroup org-agenda-time-grid nil
1434 "Options concerning the time grid in the Org-mode Agenda."
1435 :tag "Org Agenda Time Grid"
1436 :group 'org-agenda)
1438 (defcustom org-agenda-search-headline-for-time t
1439 "Non-nil means search headline for a time-of-day.
1440 If the headline contains a time-of-day in one format or another, it will
1441 be used to sort the entry into the time sequence of items for a day.
1442 Some people have time stamps in the headline that refer to the creation
1443 time or so, and then this produces an unwanted side effect. If this is
1444 the case for your, use this variable to turn off searching the headline
1445 for a time."
1446 :group 'org-agenda-time-grid
1447 :type 'boolean)
1449 (defcustom org-agenda-use-time-grid t
1450 "Non-nil means show a time grid in the agenda schedule.
1451 A time grid is a set of lines for specific times (like every two hours between
1452 8:00 and 20:00). The items scheduled for a day at specific times are
1453 sorted in between these lines.
1454 For details about when the grid will be shown, and what it will look like, see
1455 the variable `org-agenda-time-grid'."
1456 :group 'org-agenda-time-grid
1457 :type 'boolean)
1459 (defcustom org-agenda-time-grid
1460 '((daily today require-timed)
1461 "----------------"
1462 (800 1000 1200 1400 1600 1800 2000))
1464 "The settings for time grid for agenda display.
1465 This is a list of three items. The first item is again a list. It contains
1466 symbols specifying conditions when the grid should be displayed:
1468 daily if the agenda shows a single day
1469 weekly if the agenda shows an entire week
1470 today show grid on current date, independent of daily/weekly display
1471 require-timed show grid only if at least one item has a time specification
1473 The second item is a string which will be placed behind the grid time.
1475 The third item is a list of integers, indicating the times that should have
1476 a grid line."
1477 :group 'org-agenda-time-grid
1478 :type
1479 '(list
1480 (set :greedy t :tag "Grid Display Options"
1481 (const :tag "Show grid in single day agenda display" daily)
1482 (const :tag "Show grid in weekly agenda display" weekly)
1483 (const :tag "Always show grid for today" today)
1484 (const :tag "Show grid only if any timed entries are present"
1485 require-timed)
1486 (const :tag "Skip grid times already present in an entry"
1487 remove-match))
1488 (string :tag "Grid String")
1489 (repeat :tag "Grid Times" (integer :tag "Time"))))
1491 (defcustom org-agenda-show-current-time-in-grid t
1492 "Non-nil means show the current time in the time grid."
1493 :group 'org-agenda-time-grid
1494 :version "24.1"
1495 :type 'boolean)
1497 (defcustom org-agenda-current-time-string
1498 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1499 "The string for the current time marker in the agenda."
1500 :group 'org-agenda-time-grid
1501 :version "24.1"
1502 :type 'string)
1504 (defgroup org-agenda-sorting nil
1505 "Options concerning sorting in the Org-mode Agenda."
1506 :tag "Org Agenda Sorting"
1507 :group 'org-agenda)
1509 (defcustom org-agenda-sorting-strategy
1510 '((agenda habit-down time-up priority-down category-keep)
1511 (todo priority-down category-keep)
1512 (tags priority-down category-keep)
1513 (search category-keep))
1514 "Sorting structure for the agenda items of a single day.
1515 This is a list of symbols which will be used in sequence to determine
1516 if an entry should be listed before another entry. The following
1517 symbols are recognized:
1519 time-up Put entries with time-of-day indications first, early first
1520 time-down Put entries with time-of-day indications first, late first
1521 timestamp-up Sort by any timestamp, early first
1522 timestamp-down Sort by any timestamp, late first
1523 scheduled-up Sort by scheduled timestamp, early first
1524 scheduled-down Sort by scheduled timestamp, late first
1525 deadline-up Sort by deadline timestamp, early first
1526 deadline-down Sort by deadline timestamp, late first
1527 ts-up Sort by active timestamp, early first
1528 ts-down Sort by active timestamp, late first
1529 tsia-up Sort by inactive timestamp, early first
1530 tsia-down Sort by inactive timestamp, late first
1531 category-keep Keep the default order of categories, corresponding to the
1532 sequence in `org-agenda-files'.
1533 category-up Sort alphabetically by category, A-Z.
1534 category-down Sort alphabetically by category, Z-A.
1535 tag-up Sort alphabetically by last tag, A-Z.
1536 tag-down Sort alphabetically by last tag, Z-A.
1537 priority-up Sort numerically by priority, high priority last.
1538 priority-down Sort numerically by priority, high priority first.
1539 todo-state-up Sort by todo state, tasks that are done last.
1540 todo-state-down Sort by todo state, tasks that are done first.
1541 effort-up Sort numerically by estimated effort, high effort last.
1542 effort-down Sort numerically by estimated effort, high effort first.
1543 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1544 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1545 habit-up Put entries that are habits first
1546 habit-down Put entries that are habits last
1547 alpha-up Sort headlines alphabetically
1548 alpha-down Sort headlines alphabetically, reversed
1550 The different possibilities will be tried in sequence, and testing stops
1551 if one comparison returns a \"not-equal\". For example, the default
1552 '(time-up category-keep priority-down)
1553 means: Pull out all entries having a specified time of day and sort them,
1554 in order to make a time schedule for the current day the first thing in the
1555 agenda listing for the day. Of the entries without a time indication, keep
1556 the grouped in categories, don't sort the categories, but keep them in
1557 the sequence given in `org-agenda-files'. Within each category sort by
1558 priority.
1560 Leaving out `category-keep' would mean that items will be sorted across
1561 categories by priority.
1563 Instead of a single list, this can also be a set of list for specific
1564 contents, with a context symbol in the car of the list, any of
1565 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1567 Custom commands can bind this variable in the options section."
1568 :group 'org-agenda-sorting
1569 :type `(choice
1570 (repeat :tag "General" ,org-sorting-choice)
1571 (list :tag "Individually"
1572 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1573 (repeat ,org-sorting-choice))
1574 (cons (const :tag "Strategy for TODO lists" todo)
1575 (repeat ,org-sorting-choice))
1576 (cons (const :tag "Strategy for Tags matches" tags)
1577 (repeat ,org-sorting-choice))
1578 (cons (const :tag "Strategy for search matches" search)
1579 (repeat ,org-sorting-choice)))))
1581 (defcustom org-agenda-cmp-user-defined nil
1582 "A function to define the comparison `user-defined'.
1583 This function must receive two arguments, agenda entry a and b.
1584 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1585 the user comparison, return nil.
1586 When this is defined, you can make `user-defined-up' and `user-defined-down'
1587 part of an agenda sorting strategy."
1588 :group 'org-agenda-sorting
1589 :type 'symbol)
1591 (defcustom org-sort-agenda-notime-is-late t
1592 "Non-nil means items without time are considered late.
1593 This is only relevant for sorting. When t, items which have no explicit
1594 time like 15:30 will be considered as 99:01, i.e. later than any items which
1595 do have a time. When nil, the default time is before 0:00. You can use this
1596 option to decide if the schedule for today should come before or after timeless
1597 agenda entries."
1598 :group 'org-agenda-sorting
1599 :type 'boolean)
1601 (defcustom org-sort-agenda-noeffort-is-high t
1602 "Non-nil means items without effort estimate are sorted as high effort.
1603 This also applies when filtering an agenda view with respect to the
1604 < or > effort operator. Then, tasks with no effort defined will be treated
1605 as tasks with high effort.
1606 When nil, such items are sorted as 0 minutes effort."
1607 :group 'org-agenda-sorting
1608 :type 'boolean)
1610 (defgroup org-agenda-line-format nil
1611 "Options concerning the entry prefix in the Org-mode agenda display."
1612 :tag "Org Agenda Line Format"
1613 :group 'org-agenda)
1615 (defcustom org-agenda-prefix-format
1616 '((agenda . " %i %-12:c%?-12t% s")
1617 (timeline . " % s")
1618 (todo . " %i %-12:c")
1619 (tags . " %i %-12:c")
1620 (search . " %i %-12:c"))
1621 "Format specifications for the prefix of items in the agenda views.
1622 An alist with five entries, each for the different agenda types. The
1623 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1624 The values are format strings.
1626 This format works similar to a printf format, with the following meaning:
1628 %c the category of the item, \"Diary\" for entries from the diary,
1629 or as given by the CATEGORY keyword or derived from the file name
1630 %e the effort required by the item
1631 %l the level of the item (insert X space(s) if item is of level X)
1632 %i the icon category of the item, see `org-agenda-category-icon-alist'
1633 %T the last tag of the item (ignore inherited tags, which come first)
1634 %t the HH:MM time-of-day specification if one applies to the entry
1635 %s Scheduling/Deadline information, a short string
1636 %b show breadcrumbs, i.e., the names of the higher levels
1637 %(expression) Eval EXPRESSION and replace the control string
1638 by the result
1640 All specifiers work basically like the standard `%s' of printf, but may
1641 contain two additional characters: a question mark just after the `%'
1642 and a whitespace/punctuation character just before the final letter.
1644 If the first character after `%' is a question mark, the entire field
1645 will only be included if the corresponding value applies to the current
1646 entry. This is useful for fields which should have fixed width when
1647 present, but zero width when absent. For example, \"%?-12t\" will
1648 result in a 12 character time field if a time of the day is specified,
1649 but will completely disappear in entries which do not contain a time.
1651 If there is punctuation or whitespace character just before the
1652 final format letter, this character will be appended to the field
1653 value if the value is not empty. For example, the format
1654 \"%-12:c\" leads to \"Diary: \" if the category is \"Diary\". If
1655 the category is empty, no additional colon is inserted.
1657 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1658 which means:
1660 - Indent the line with two space characters
1661 - Give the category a 12 chars wide field, padded with whitespace on
1662 the right (because of `-'). Append a colon if there is a category
1663 (because of `:').
1664 - If there is a time-of-day, put it into a 12 chars wide field. If no
1665 time, don't put in an empty field, just skip it (because of '?').
1666 - Finally, put the scheduling information.
1668 See also the variables `org-agenda-remove-times-when-in-prefix' and
1669 `org-agenda-remove-tags'.
1671 Custom commands can set this variable in the options section."
1672 :type '(choice
1673 (string :tag "General format")
1674 (list :greedy t :tag "View dependent"
1675 (cons (const agenda) (string :tag "Format"))
1676 (cons (const timeline) (string :tag "Format"))
1677 (cons (const todo) (string :tag "Format"))
1678 (cons (const tags) (string :tag "Format"))
1679 (cons (const search) (string :tag "Format"))))
1680 :group 'org-agenda-line-format)
1682 (defvar org-prefix-format-compiled nil
1683 "The compiled prefix format and associated variables.
1684 This is a list where first element is a list of variable bindings, and second
1685 element is the compiled format expression. See the variable
1686 `org-agenda-prefix-format'.")
1688 (defcustom org-agenda-todo-keyword-format "%-1s"
1689 "Format for the TODO keyword in agenda lines.
1690 Set this to something like \"%-12s\" if you want all TODO keywords
1691 to occupy a fixed space in the agenda display."
1692 :group 'org-agenda-line-format
1693 :type 'string)
1695 (defcustom org-agenda-diary-sexp-prefix nil
1696 "A regexp that matches part of a diary sexp entry
1697 which should be treated as scheduling/deadline information in
1698 `org-agenda'.
1700 For example, you can use this to extract the `diary-remind-message' from
1701 `diary-remind' entries."
1702 :group 'org-agenda-line-format
1703 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1705 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1706 "Text preceding timerange entries in the agenda view.
1707 This is a list with two strings. The first applies when the range
1708 is entirely on one day. The second applies if the range spans several days.
1709 The strings may have two \"%d\" format specifiers which will be filled
1710 with the sequence number of the days, and the total number of days in the
1711 range, respectively."
1712 :group 'org-agenda-line-format
1713 :type '(list
1714 (string :tag "Deadline today ")
1715 (choice :tag "Deadline relative"
1716 (string :tag "Format string")
1717 (function))))
1719 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1720 "Text preceding scheduled items in the agenda view.
1721 This is a list with two strings. The first applies when the item is
1722 scheduled on the current day. The second applies when it has been scheduled
1723 previously, it may contain a %d indicating that this is the nth time that
1724 this item is scheduled, due to automatic rescheduling of unfinished items
1725 for the following day. So this number is one larger than the number of days
1726 that passed since this item was scheduled first."
1727 :group 'org-agenda-line-format
1728 :version "24.4"
1729 :package-version '(Org . "8.0")
1730 :type '(list
1731 (string :tag "Scheduled today ")
1732 (string :tag "Scheduled previously")))
1734 (defcustom org-agenda-inactive-leader "["
1735 "Text preceding item pulled into the agenda by inactive time stamps.
1736 These entries are added to the agenda when pressing \"[\"."
1737 :group 'org-agenda-line-format
1738 :version "24.1"
1739 :type 'string)
1741 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: " "%2d d. ago: ")
1742 "Text preceding deadline items in the agenda view.
1743 This is a list with three strings. The first applies when the item has its
1744 deadline on the current day. The second applies when the deadline is in the
1745 future, the third one when it is in the past. The strings may contain %d
1746 to capture the number of days."
1747 :group 'org-agenda-line-format
1748 :version "24.4"
1749 :package-version '(Org . "8.0")
1750 :type '(list
1751 (string :tag "Deadline today ")
1752 (string :tag "Deadline in the future ")
1753 (string :tag "Deadline in the past ")))
1755 (defcustom org-agenda-remove-times-when-in-prefix t
1756 "Non-nil means remove duplicate time specifications in agenda items.
1757 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1758 time-of-day specification in a headline or diary entry is extracted and
1759 placed into the prefix. If this option is non-nil, the original specification
1760 \(a timestamp or -range, or just a plain time(range) specification like
1761 11:30-4pm) will be removed for agenda display. This makes the agenda less
1762 cluttered.
1763 The option can be t or nil. It may also be the symbol `beg', indicating
1764 that the time should only be removed when it is located at the beginning of
1765 the headline/diary entry."
1766 :group 'org-agenda-line-format
1767 :type '(choice
1768 (const :tag "Always" t)
1769 (const :tag "Never" nil)
1770 (const :tag "When at beginning of entry" beg)))
1772 (defcustom org-agenda-remove-timeranges-from-blocks nil
1773 "Non-nil means remove time ranges specifications in agenda
1774 items that span on several days."
1775 :group 'org-agenda-line-format
1776 :version "24.1"
1777 :type 'boolean)
1779 (defcustom org-agenda-default-appointment-duration nil
1780 "Default duration for appointments that only have a starting time.
1781 When nil, no duration is specified in such cases.
1782 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1783 :group 'org-agenda-line-format
1784 :type '(choice
1785 (integer :tag "Minutes")
1786 (const :tag "No default duration")))
1788 (defcustom org-agenda-show-inherited-tags t
1789 "Non-nil means show inherited tags in each agenda line.
1791 When this option is set to 'always, it take precedences over
1792 `org-agenda-use-tag-inheritance' and inherited tags are shown
1793 in every agenda.
1795 When this option is set to t (the default), inherited tags are
1796 shown when they are available, i.e. when the value of
1797 `org-agenda-use-tag-inheritance' has been taken into account.
1799 This can be set to a list of agenda types in which the agenda
1800 must display the inherited tags. Available types are 'todo,
1801 'agenda, 'search and 'timeline.
1803 When set to nil, never show inherited tags in agenda lines."
1804 :group 'org-agenda-line-format
1805 :group 'org-agenda
1806 :version "24.3"
1807 :type '(choice
1808 (const :tag "Show inherited tags when available" t)
1809 (const :tag "Always show inherited tags" always)
1810 (repeat :tag "Show inherited tags only in selected agenda types"
1811 (symbol :tag "Agenda type"))))
1813 (defcustom org-agenda-use-tag-inheritance '(todo search timeline agenda)
1814 "List of agenda view types where to use tag inheritance.
1816 In tags/tags-todo/tags-tree agenda views, tag inheritance is
1817 controlled by `org-use-tag-inheritance'. In other agenda types,
1818 `org-use-tag-inheritance' is not used for the selection of the
1819 agenda entries. Still, you may want the agenda to be aware of
1820 the inherited tags anyway, e.g. for later tag filtering.
1822 Allowed value are 'todo, 'search, 'timeline and 'agenda.
1824 This variable has no effect if `org-agenda-show-inherited-tags'
1825 is set to 'always. In that case, the agenda is aware of those
1826 tags.
1828 The default value sets tags in every agenda type. Setting this
1829 option to nil will speed up non-tags agenda view a lot."
1830 :group 'org-agenda
1831 :version "24.3"
1832 :type '(choice
1833 (const :tag "Use tag inheritance in all agenda types" t)
1834 (repeat :tag "Use tag inheritance in selected agenda types"
1835 (symbol :tag "Agenda type"))))
1837 (defcustom org-agenda-hide-tags-regexp nil
1838 "Regular expression used to filter away specific tags in agenda views.
1839 This means that these tags will be present, but not be shown in the agenda
1840 line. Secondary filtering will still work on the hidden tags.
1841 Nil means don't hide any tags."
1842 :group 'org-agenda-line-format
1843 :type '(choice
1844 (const :tag "Hide none" nil)
1845 (string :tag "Regexp ")))
1847 (defcustom org-agenda-remove-tags nil
1848 "Non-nil means remove the tags from the headline copy in the agenda.
1849 When this is the symbol `prefix', only remove tags when
1850 `org-agenda-prefix-format' contains a `%T' specifier."
1851 :group 'org-agenda-line-format
1852 :type '(choice
1853 (const :tag "Always" t)
1854 (const :tag "Never" nil)
1855 (const :tag "When prefix format contains %T" prefix)))
1857 (org-defvaralias 'org-agenda-remove-tags-when-in-prefix
1858 'org-agenda-remove-tags)
1860 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1861 "Shift tags in agenda items to this column.
1862 If this number is positive, it specifies the column. If it is negative,
1863 it means that the tags should be flushright to that column. For example,
1864 -80 works well for a normal 80 character screen."
1865 :group 'org-agenda-line-format
1866 :type 'integer)
1868 (org-defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column)
1870 (defcustom org-agenda-fontify-priorities 'cookies
1871 "Non-nil means highlight low and high priorities in agenda.
1872 When t, the highest priority entries are bold, lowest priority italic.
1873 However, settings in `org-priority-faces' will overrule these faces.
1874 When this variable is the symbol `cookies', only fontify the
1875 cookies, not the entire task.
1876 This may also be an association list of priority faces, whose
1877 keys are the character values of `org-highest-priority',
1878 `org-default-priority', and `org-lowest-priority' (the default values
1879 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1880 color as a string, or a list like `(:background \"Red\")'.
1881 If it is a color, the variable `org-faces-easy-properties'
1882 determines if it is a foreground or a background color."
1883 :group 'org-agenda-line-format
1884 :type '(choice
1885 (const :tag "Never" nil)
1886 (const :tag "Defaults" t)
1887 (const :tag "Cookies only" cookies)
1888 (repeat :tag "Specify"
1889 (list (character :tag "Priority" :value ?A)
1890 (choice :tag "Face "
1891 (string :tag "Color")
1892 (sexp :tag "Face"))))))
1894 (defcustom org-agenda-day-face-function nil
1895 "Function called to determine what face should be used to display a day.
1896 The only argument passed to that function is the day. It should
1897 returns a face, or nil if does not want to specify a face and let
1898 the normal rules apply."
1899 :group 'org-agenda-line-format
1900 :version "24.1"
1901 :type '(choice (const nil) (function)))
1903 (defcustom org-agenda-category-icon-alist nil
1904 "Alist of category icon to be displayed in agenda views.
1906 Each entry should have the following format:
1908 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1910 Where CATEGORY-REGEXP is a regexp matching the categories where
1911 the icon should be displayed.
1912 FILE-OR-DATA either a file path or a string containing image data.
1914 The other fields can be omitted safely if not needed:
1915 TYPE indicates the image type.
1916 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1917 image data.
1918 PROPS are additional image attributes to assign to the image,
1919 like, e.g. `:ascent center'.
1921 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1923 If you want to set the display properties yourself, just put a
1924 list as second element:
1926 (CATEGORY-REGEXP (MY PROPERTY LIST))
1928 For example, to display a 16px horizontal space for Emacs
1929 category, you can use:
1931 (\"Emacs\" '(space . (:width (16))))"
1932 :group 'org-agenda-line-format
1933 :version "24.1"
1934 :type '(alist :key-type (string :tag "Regexp matching category")
1935 :value-type (choice (list :tag "Icon"
1936 (string :tag "File or data")
1937 (symbol :tag "Type")
1938 (boolean :tag "Data?")
1939 (repeat :tag "Extra image properties" :inline t symbol))
1940 (list :tag "Display properties" sexp))))
1942 (defgroup org-agenda-column-view nil
1943 "Options concerning column view in the agenda."
1944 :tag "Org Agenda Column View"
1945 :group 'org-agenda)
1947 (defcustom org-agenda-columns-show-summaries t
1948 "Non-nil means show summaries for columns displayed in the agenda view."
1949 :group 'org-agenda-column-view
1950 :type 'boolean)
1952 (defcustom org-agenda-columns-compute-summary-properties t
1953 "Non-nil means recompute all summary properties before column view.
1954 When column view in the agenda is listing properties that have a summary
1955 operator, it can go to all relevant buffers and recompute the summaries
1956 there. This can mean overhead for the agenda column view, but is necessary
1957 to have thing up to date.
1958 As a special case, a CLOCKSUM property also makes sure that the clock
1959 computations are current."
1960 :group 'org-agenda-column-view
1961 :type 'boolean)
1963 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1964 "Non-nil means the duration of an appointment will add to day effort.
1965 The property to which appointment durations will be added is the one given
1966 in the option `org-effort-property'. If an appointment does not have
1967 an end time, `org-agenda-default-appointment-duration' will be used. If that
1968 is not set, an appointment without end time will not contribute to the time
1969 estimate."
1970 :group 'org-agenda-column-view
1971 :type 'boolean)
1973 (defcustom org-agenda-auto-exclude-function nil
1974 "A function called with a tag to decide if it is filtered on '/ RET'.
1975 The sole argument to the function, which is called once for each
1976 possible tag, is a string giving the name of the tag. The
1977 function should return either nil if the tag should be included
1978 as normal, or \"-<TAG>\" to exclude the tag.
1979 Note that for the purpose of tag filtering, only the lower-case version of
1980 all tags will be considered, so that this function will only ever see
1981 the lower-case version of all tags."
1982 :group 'org-agenda
1983 :type '(choice (const nil) (function)))
1985 (defcustom org-agenda-bulk-custom-functions nil
1986 "Alist of characters and custom functions for bulk actions.
1987 For example, this value makes those two functions available:
1989 '((?R set-category)
1990 (?C bulk-cut))
1992 With selected entries in an agenda buffer, `B R' will call
1993 the custom function `set-category' on the selected entries.
1994 Note that functions in this alist don't need to be quoted."
1995 :type 'alist
1996 :version "24.1"
1997 :group 'org-agenda)
1999 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
2000 "Execute BODY with point at location given by `org-hd-marker' property.
2001 If STRING is non-nil, the text property will be fetched from position 0
2002 in that string. If STRING is nil, it will be fetched from the beginning
2003 of the current line."
2004 (org-with-gensyms (marker)
2005 `(let ((,marker (get-text-property (if string 0 (point-at-bol))
2006 'org-hd-marker ,string)))
2007 (with-current-buffer (marker-buffer ,marker)
2008 (save-excursion
2009 (goto-char ,marker)
2010 ,@body)))))
2011 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
2013 (defun org-add-agenda-custom-command (entry)
2014 "Replace or add a command in `org-agenda-custom-commands'.
2015 This is mostly for hacking and trying a new command - once the command
2016 works you probably want to add it to `org-agenda-custom-commands' for good."
2017 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
2018 (if ass
2019 (setcdr ass (cdr entry))
2020 (push entry org-agenda-custom-commands))))
2022 ;;; Define the org-agenda-mode
2024 (defvar org-agenda-mode-map (make-sparse-keymap)
2025 "Keymap for `org-agenda-mode'.")
2026 (org-defvaralias 'org-agenda-keymap 'org-agenda-mode-map)
2028 (defvar org-agenda-menu) ; defined later in this file.
2029 (defvar org-agenda-restrict nil) ; defined later in this file.
2030 (defvar org-agenda-follow-mode nil)
2031 (defvar org-agenda-entry-text-mode nil)
2032 (defvar org-agenda-clockreport-mode nil)
2033 (defvar org-agenda-show-log nil)
2034 (defvar org-agenda-redo-command nil)
2035 (defvar org-agenda-query-string nil)
2036 (defvar org-agenda-mode-hook nil
2037 "Hook run after `org-agenda-mode' is turned on.
2038 The buffer is still writable when this hook is called.")
2039 (defvar org-agenda-type nil)
2040 (defvar org-agenda-force-single-file nil)
2041 (defvar org-agenda-bulk-marked-entries nil
2042 "List of markers that refer to marked entries in the agenda.")
2044 ;;; Multiple agenda buffers support
2046 (defcustom org-agenda-sticky nil
2047 "Non-nil means agenda q key will bury agenda buffers.
2048 Agenda commands will then show existing buffer instead of generating new ones.
2049 When nil, `q' will kill the single agenda buffer."
2050 :group 'org-agenda
2051 :version "24.3"
2052 :type 'boolean)
2055 ;;;###autoload
2056 (defun org-toggle-sticky-agenda (&optional arg)
2057 "Toggle `org-agenda-sticky'."
2058 (interactive "P")
2059 (let ((new-value (if arg
2060 (> (prefix-numeric-value arg) 0)
2061 (not org-agenda-sticky))))
2062 (if (equal new-value org-agenda-sticky)
2063 (and (org-called-interactively-p 'interactive)
2064 (message "Sticky agenda was already %s"
2065 (if org-agenda-sticky "enabled" "disabled")))
2066 (setq org-agenda-sticky new-value)
2067 (org-agenda-kill-all-agenda-buffers)
2068 (and (org-called-interactively-p 'interactive)
2069 (message "Sticky agenda was %s"
2070 (if org-agenda-sticky "enabled" "disabled"))))))
2072 (defvar org-agenda-buffer nil
2073 "Agenda buffer currently being generated.")
2075 (defvar org-agenda-last-prefix-arg nil)
2076 (defvar org-agenda-this-buffer-name nil)
2077 (defvar org-agenda-doing-sticky-redo nil)
2078 (defvar org-agenda-this-buffer-is-sticky nil)
2080 (defconst org-agenda-local-vars
2081 '(org-agenda-this-buffer-name
2082 org-agenda-undo-list
2083 org-agenda-pending-undo-list
2084 org-agenda-follow-mode
2085 org-agenda-entry-text-mode
2086 org-agenda-clockreport-mode
2087 org-agenda-show-log
2088 org-agenda-redo-command
2089 org-agenda-query-string
2090 org-agenda-type
2091 org-agenda-bulk-marked-entries
2092 org-agenda-undo-has-started-in
2093 org-agenda-info
2094 org-agenda-pre-window-conf
2095 org-agenda-columns-active
2096 org-agenda-tag-filter-overlays
2097 org-agenda-tag-filter
2098 org-agenda-cat-filter-overlays
2099 org-agenda-category-filter
2100 org-agenda-re-filter-overlays
2101 org-agenda-regexp-filter
2102 org-agenda-markers
2103 org-agenda-last-search-view-search-was-boolean
2104 org-agenda-filtered-by-category
2105 org-agenda-filter-form
2106 org-agenda-cycle-counter
2107 org-agenda-last-prefix-arg)
2108 "Variables that must be local in agenda buffers to allow multiple buffers.")
2110 (defun org-agenda-mode ()
2111 "Mode for time-sorted view on action items in Org-mode files.
2113 The following commands are available:
2115 \\{org-agenda-mode-map}"
2116 (interactive)
2117 (cond (org-agenda-doing-sticky-redo
2118 ;; Refreshing sticky agenda-buffer
2120 ;; Preserve the value of `org-agenda-local-vars' variables,
2121 ;; while letting `kill-all-local-variables' kill the rest
2122 (let ((save (buffer-local-variables)))
2123 (kill-all-local-variables)
2124 (mapc 'make-local-variable org-agenda-local-vars)
2125 (dolist (elem save)
2126 (let ((var (car elem))
2127 (val (cdr elem)))
2128 (when (and val
2129 (member var org-agenda-local-vars))
2130 (set var val)))))
2131 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
2132 (org-agenda-sticky
2133 ;; Creating a sticky Agenda buffer for the first time
2134 (kill-all-local-variables)
2135 (mapc 'make-local-variable org-agenda-local-vars)
2136 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
2138 ;; Creating a non-sticky agenda buffer
2139 (kill-all-local-variables)
2140 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) nil)))
2141 (setq org-agenda-undo-list nil
2142 org-agenda-pending-undo-list nil
2143 org-agenda-bulk-marked-entries nil)
2144 (setq major-mode 'org-agenda-mode)
2145 ;; Keep global-font-lock-mode from turning on font-lock-mode
2146 (org-set-local 'font-lock-global-modes (list 'not major-mode))
2147 (setq mode-name "Org-Agenda")
2148 (use-local-map org-agenda-mode-map)
2149 (easy-menu-add org-agenda-menu)
2150 (if org-startup-truncated (setq truncate-lines t))
2151 (org-set-local 'line-move-visual nil)
2152 (org-add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
2153 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
2154 ;; Make sure properties are removed when copying text
2155 (org-add-hook 'filter-buffer-substring-functions
2156 (lambda (fun start end delete)
2157 (substring-no-properties (funcall fun start end delete)))
2158 nil t)
2159 (unless org-agenda-keep-modes
2160 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
2161 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
2162 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
2163 org-agenda-show-log org-agenda-start-with-log-mode))
2165 (easy-menu-change
2166 '("Agenda") "Agenda Files"
2167 (append
2168 (list
2169 (vector
2170 (if (get 'org-agenda-files 'org-restrict)
2171 "Restricted to single file"
2172 "Edit File List")
2173 '(org-edit-agenda-file-list)
2174 (not (get 'org-agenda-files 'org-restrict)))
2175 "--")
2176 (mapcar 'org-file-menu-entry (org-agenda-files))))
2177 (org-agenda-set-mode-name)
2178 (apply
2179 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2180 (list 'org-agenda-mode-hook)))
2182 (substitute-key-definition 'undo 'org-agenda-undo
2183 org-agenda-mode-map global-map)
2184 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2185 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2186 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2187 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2188 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2189 (org-defkey org-agenda-mode-map [(meta down)] 'org-agenda-drag-line-forward)
2190 (org-defkey org-agenda-mode-map [(meta up)] 'org-agenda-drag-line-backward)
2191 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2192 (org-defkey org-agenda-mode-map "\M-m" 'org-agenda-bulk-toggle)
2193 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2194 (org-defkey org-agenda-mode-map "\M-*" 'org-agenda-bulk-toggle-all)
2195 (org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
2196 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2197 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2198 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2199 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2200 (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2201 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2202 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2203 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2204 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2205 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2206 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2207 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2208 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2209 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2210 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2211 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2212 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2213 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2214 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2215 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2216 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2217 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2218 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2219 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2220 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2221 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2222 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2223 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2224 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2225 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2226 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2227 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2228 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2229 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2230 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2231 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2232 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2233 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2235 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2236 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2237 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2238 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2239 (while l (org-defkey org-agenda-mode-map
2240 (int-to-string (pop l)) 'digit-argument)))
2242 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2243 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2244 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2245 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2246 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2247 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2248 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2249 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2250 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2251 (org-defkey org-agenda-mode-map "g" (lambda () (interactive) (org-agenda-redo t)))
2252 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2253 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2254 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2255 'org-clock-modify-effort-estimate)
2256 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2257 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2258 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2259 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2260 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2261 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2262 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2263 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2264 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2265 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2266 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2267 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2268 (substitute-key-definition 'next-line 'org-agenda-next-line
2269 org-agenda-mode-map global-map)
2270 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2271 org-agenda-mode-map global-map)
2272 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2273 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2274 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2275 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2276 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2277 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2278 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2279 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2280 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2281 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2282 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2283 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2284 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2285 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2286 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2287 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2288 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2289 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2290 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2291 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2292 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2293 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2294 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2295 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2296 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2297 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2298 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2299 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2300 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2301 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2303 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2304 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2305 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2306 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2307 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2308 (org-defkey org-agenda-mode-map "=" 'org-agenda-filter-by-regexp)
2309 (org-defkey org-agenda-mode-map "|" 'org-agenda-filter-remove-all)
2310 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
2311 (org-defkey org-agenda-mode-map "~" 'org-agenda-limit-interactively)
2312 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2313 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-headline)
2314 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2315 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2316 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2317 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2319 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2320 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2322 (define-key org-agenda-mode-map [remap forward-paragraph] 'org-agenda-forward-block)
2323 (define-key org-agenda-mode-map [remap backward-paragraph] 'org-agenda-backward-block)
2325 (when org-agenda-mouse-1-follows-link
2326 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2327 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2328 '("Agenda"
2329 ("Agenda Files")
2330 "--"
2331 ("Agenda Dates"
2332 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2333 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2334 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2335 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2336 "--"
2337 ("View"
2338 ["Day View" org-agenda-day-view
2339 :active (org-agenda-check-type nil 'agenda)
2340 :style radio :selected (eq org-agenda-current-span 'day)
2341 :keys "v d (or just d)"]
2342 ["Week View" org-agenda-week-view
2343 :active (org-agenda-check-type nil 'agenda)
2344 :style radio :selected (eq org-agenda-current-span 'week)
2345 :keys "v w"]
2346 ["Fortnight View" org-agenda-fortnight-view
2347 :active (org-agenda-check-type nil 'agenda)
2348 :style radio :selected (eq org-agenda-current-span 'fortnight)
2349 :keys "v f"]
2350 ["Month View" org-agenda-month-view
2351 :active (org-agenda-check-type nil 'agenda)
2352 :style radio :selected (eq org-agenda-current-span 'month)
2353 :keys "v m"]
2354 ["Year View" org-agenda-year-view
2355 :active (org-agenda-check-type nil 'agenda)
2356 :style radio :selected (eq org-agenda-current-span 'year)
2357 :keys "v y"]
2358 "--"
2359 ["Include Diary" org-agenda-toggle-diary
2360 :style toggle :selected org-agenda-include-diary
2361 :active (org-agenda-check-type nil 'agenda)]
2362 ["Include Deadlines" org-agenda-toggle-deadlines
2363 :style toggle :selected org-agenda-include-deadlines
2364 :active (org-agenda-check-type nil 'agenda)]
2365 ["Use Time Grid" org-agenda-toggle-time-grid
2366 :style toggle :selected org-agenda-use-time-grid
2367 :active (org-agenda-check-type nil 'agenda)]
2368 "--"
2369 ["Show clock report" org-agenda-clockreport-mode
2370 :style toggle :selected org-agenda-clockreport-mode
2371 :active (org-agenda-check-type nil 'agenda)]
2372 ["Show some entry text" org-agenda-entry-text-mode
2373 :style toggle :selected org-agenda-entry-text-mode
2374 :active t]
2375 "--"
2376 ["Show Logbook entries" org-agenda-log-mode
2377 :style toggle :selected org-agenda-show-log
2378 :active (org-agenda-check-type nil 'agenda 'timeline)
2379 :keys "v l (or just l)"]
2380 ["Include archived trees" org-agenda-archives-mode
2381 :style toggle :selected org-agenda-archives-mode :active t
2382 :keys "v a"]
2383 ["Include archive files" (org-agenda-archives-mode t)
2384 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2385 :keys "v A"]
2386 "--"
2387 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2388 ["Write view to file" org-agenda-write t]
2389 ["Rebuild buffer" org-agenda-redo t]
2390 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2391 "--"
2392 ["Show original entry" org-agenda-show t]
2393 ["Go To (other window)" org-agenda-goto t]
2394 ["Go To (this window)" org-agenda-switch-to t]
2395 ["Capture with cursor date" org-agenda-capture t]
2396 ["Follow Mode" org-agenda-follow-mode
2397 :style toggle :selected org-agenda-follow-mode :active t]
2398 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2399 "--"
2400 ("TODO"
2401 ["Cycle TODO" org-agenda-todo t]
2402 ["Next TODO set" org-agenda-todo-nextset t]
2403 ["Previous TODO set" org-agenda-todo-previousset t]
2404 ["Add note" org-agenda-add-note t])
2405 ("Archive/Refile/Delete"
2406 ["Archive default" org-agenda-archive-default t]
2407 ["Archive default" org-agenda-archive-default-with-confirmation t]
2408 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2409 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2410 ["Archive subtree" org-agenda-archive t]
2411 "--"
2412 ["Refile" org-agenda-refile t]
2413 "--"
2414 ["Delete subtree" org-agenda-kill t])
2415 ("Bulk action"
2416 ["Mark entry" org-agenda-bulk-mark t]
2417 ["Mark all" org-agenda-bulk-mark-all t]
2418 ["Unmark entry" org-agenda-bulk-unmark t]
2419 ["Unmark all" org-agenda-bulk-unmark-all :active t :keys "U"]
2420 ["Toggle mark" org-agenda-bulk-toggle t]
2421 ["Toggle all" org-agenda-bulk-toggle-all t]
2422 ["Mark regexp" org-agenda-bulk-mark-regexp t])
2423 ["Act on all marked" org-agenda-bulk-action t]
2424 "--"
2425 ("Tags and Properties"
2426 ["Show all Tags" org-agenda-show-tags t]
2427 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2428 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2429 "--"
2430 ["Column View" org-columns t])
2431 ("Deadline/Schedule"
2432 ["Schedule" org-agenda-schedule t]
2433 ["Set Deadline" org-agenda-deadline t]
2434 "--"
2435 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2436 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2437 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2438 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2439 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2440 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2441 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2442 ("Clock and Effort"
2443 ["Clock in" org-agenda-clock-in t]
2444 ["Clock out" org-agenda-clock-out t]
2445 ["Clock cancel" org-agenda-clock-cancel t]
2446 ["Goto running clock" org-clock-goto t]
2447 "--"
2448 ["Set Effort" org-agenda-set-effort t]
2449 ["Change clocked effort" org-clock-modify-effort-estimate
2450 (org-clock-is-active)])
2451 ("Priority"
2452 ["Set Priority" org-agenda-priority t]
2453 ["Increase Priority" org-agenda-priority-up t]
2454 ["Decrease Priority" org-agenda-priority-down t]
2455 ["Show Priority" org-show-priority t])
2456 ("Calendar/Diary"
2457 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2458 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2459 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2460 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2461 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2462 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2463 "--"
2464 ["Create iCalendar File" org-icalendar-combine-agenda-files t])
2465 "--"
2466 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2467 "--"
2468 ("MobileOrg"
2469 ["Push Files and Views" org-mobile-push t]
2470 ["Get Captured and Flagged" org-mobile-pull t]
2471 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2472 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2473 "--"
2474 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2475 "--"
2476 ["Quit" org-agenda-quit t]
2477 ["Exit and Release Buffers" org-agenda-exit t]
2480 ;;; Agenda undo
2482 (defvar org-agenda-allow-remote-undo t
2483 "Non-nil means allow remote undo from the agenda buffer.")
2484 (defvar org-agenda-undo-has-started-in nil
2485 "Buffers that have already seen `undo-start' in the current undo sequence.")
2487 (defun org-agenda-undo ()
2488 "Undo a remote editing step in the agenda.
2489 This undoes changes both in the agenda buffer and in the remote buffer
2490 that have been changed along."
2491 (interactive)
2492 (or org-agenda-allow-remote-undo
2493 (user-error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2494 (if (not (eq this-command last-command))
2495 (setq org-agenda-undo-has-started-in nil
2496 org-agenda-pending-undo-list org-agenda-undo-list))
2497 (if (not org-agenda-pending-undo-list)
2498 (user-error "No further undo information"))
2499 (let* ((entry (pop org-agenda-pending-undo-list))
2500 buf line cmd rembuf)
2501 (setq cmd (pop entry) line (pop entry))
2502 (setq rembuf (nth 2 entry))
2503 (org-with-remote-undo rembuf
2504 (while (bufferp (setq buf (pop entry)))
2505 (if (pop entry)
2506 (with-current-buffer buf
2507 (let ((last-undo-buffer buf)
2508 (inhibit-read-only t))
2509 (unless (memq buf org-agenda-undo-has-started-in)
2510 (push buf org-agenda-undo-has-started-in)
2511 (make-local-variable 'pending-undo-list)
2512 (undo-start))
2513 (while (and pending-undo-list
2514 (listp pending-undo-list)
2515 (not (car pending-undo-list)))
2516 (pop pending-undo-list))
2517 (undo-more 1))))))
2518 (org-goto-line line)
2519 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2521 (defun org-verify-change-for-undo (l1 l2)
2522 "Verify that a real change occurred between the undo lists L1 and L2."
2523 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2524 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2525 (not (eq l1 l2)))
2527 ;;; Agenda dispatch
2529 (defvar org-agenda-restrict-begin (make-marker))
2530 (defvar org-agenda-restrict-end (make-marker))
2531 (defvar org-agenda-last-dispatch-buffer nil)
2532 (defvar org-agenda-overriding-restriction nil)
2534 (defcustom org-agenda-custom-commands-contexts nil
2535 "Alist of custom agenda keys and contextual rules.
2537 For example, if you have a custom agenda command \"p\" and you
2538 want this command to be accessible only from plain text files,
2539 use this:
2541 '((\"p\" ((in-file . \"\\\\.txt\\\\'\"))))
2543 Here are the available contexts definitions:
2545 in-file: command displayed only in matching files
2546 in-mode: command displayed only in matching modes
2547 not-in-file: command not displayed in matching files
2548 not-in-mode: command not displayed in matching modes
2549 in-buffer: command displayed only in matching buffers
2550 not-in-buffer: command not displayed in matching buffers
2551 [function]: a custom function taking no argument
2553 If you define several checks, the agenda command will be
2554 accessible if there is at least one valid check.
2556 You can also bind a key to another agenda custom command
2557 depending on contextual rules.
2559 '((\"p\" \"q\" ((in-file . \"\\\\.txt\\\\'\"))))
2561 Here it means: in .txt files, use \"p\" as the key for the
2562 agenda command otherwise associated with \"q\". (The command
2563 originally associated with \"q\" is not displayed to avoid
2564 duplicates.)"
2565 :version "24.3"
2566 :group 'org-agenda-custom-commands
2567 :type '(repeat (list :tag "Rule"
2568 (string :tag " Agenda key")
2569 (string :tag "Replace by command")
2570 (repeat :tag "Available when"
2571 (choice
2572 (cons :tag "Condition"
2573 (choice
2574 (const :tag "In file" in-file)
2575 (const :tag "Not in file" not-in-file)
2576 (const :tag "In buffer" in-buffer)
2577 (const :tag "Not in buffer" not-in-buffer)
2578 (const :tag "In mode" in-mode)
2579 (const :tag "Not in mode" not-in-mode))
2580 (regexp))
2581 (function :tag "Custom function"))))))
2583 (defcustom org-agenda-max-entries nil
2584 "Maximum number of entries to display in an agenda.
2585 This can be nil (no limit) or an integer or an alist of agenda
2586 types with an associated number of entries to display in this
2587 type."
2588 :version "24.4"
2589 :package-version '(Org . "8.0")
2590 :group 'org-agenda-custom-commands
2591 :type '(choice (symbol :tag "No limit" nil)
2592 (integer :tag "Max number of entries")
2593 (repeat
2594 (cons (choice :tag "Agenda type"
2595 (const agenda)
2596 (const todo)
2597 (const tags)
2598 (const search)
2599 (const timeline))
2600 (integer :tag "Max number of entries")))))
2602 (defcustom org-agenda-max-todos nil
2603 "Maximum number of TODOs to display in an agenda.
2604 This can be nil (no limit) or an integer or an alist of agenda
2605 types with an associated number of entries to display in this
2606 type."
2607 :version "24.4"
2608 :package-version '(Org . "8.0")
2609 :group 'org-agenda-custom-commands
2610 :type '(choice (symbol :tag "No limit" nil)
2611 (integer :tag "Max number of entries")
2612 (repeat
2613 (cons (choice :tag "Agenda type"
2614 (const agenda)
2615 (const todo)
2616 (const tags)
2617 (const search)
2618 (const timeline))
2619 (integer :tag "Max number of entries")))))
2621 (defcustom org-agenda-max-tags nil
2622 "Maximum number of tagged entries to display in an agenda.
2623 This can be nil (no limit) or an integer or an alist of agenda
2624 types with an associated number of entries to display in this
2625 type."
2626 :version "24.4"
2627 :package-version '(Org . "8.0")
2628 :group 'org-agenda-custom-commands
2629 :type '(choice (symbol :tag "No limit" nil)
2630 (integer :tag "Max number of entries")
2631 (repeat
2632 (cons (choice :tag "Agenda type"
2633 (const agenda)
2634 (const todo)
2635 (const tags)
2636 (const search)
2637 (const timeline))
2638 (integer :tag "Max number of entries")))))
2640 (defcustom org-agenda-max-effort nil
2641 "Maximum cumulated effort duration for the agenda.
2642 This can be nil (no limit) or a number of minutes (as an integer)
2643 or an alist of agenda types with an associated number of minutes
2644 to limit entries to in this type."
2645 :version "24.4"
2646 :package-version '(Org . "8.0")
2647 :group 'org-agenda-custom-commands
2648 :type '(choice (symbol :tag "No limit" nil)
2649 (integer :tag "Max number of entries")
2650 (repeat
2651 (cons (choice :tag "Agenda type"
2652 (const agenda)
2653 (const todo)
2654 (const tags)
2655 (const search)
2656 (const timeline))
2657 (integer :tag "Max number of entries")))))
2659 (defvar org-keys nil)
2660 (defvar org-match nil)
2661 ;;;###autoload
2662 (defun org-agenda (&optional arg org-keys restriction)
2663 "Dispatch agenda commands to collect entries to the agenda buffer.
2664 Prompts for a command to execute. Any prefix arg will be passed
2665 on to the selected command. The default selections are:
2667 a Call `org-agenda-list' to display the agenda for current day or week.
2668 t Call `org-todo-list' to display the global todo list.
2669 T Call `org-todo-list' to display the global todo list, select only
2670 entries with a specific TODO keyword (the user gets a prompt).
2671 m Call `org-tags-view' to display headlines with tags matching
2672 a condition (the user is prompted for the condition).
2673 M Like `m', but select only TODO entries, no ordinary headlines.
2674 L Create a timeline for the current buffer.
2675 e Export views to associated files.
2676 s Search entries for keywords.
2677 S Search entries for keywords, only with TODO keywords.
2678 / Multi occur across all agenda files and also files listed
2679 in `org-agenda-text-search-extra-files'.
2680 < Restrict agenda commands to buffer, subtree, or region.
2681 Press several times to get the desired effect.
2682 > Remove a previous restriction.
2683 # List \"stuck\" projects.
2684 ! Configure what \"stuck\" means.
2685 C Configure custom agenda commands.
2687 More commands can be added by configuring the variable
2688 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2689 searches can be pre-defined in this way.
2691 If the current buffer is in Org-mode and visiting a file, you can also
2692 first press `<' once to indicate that the agenda should be temporarily
2693 \(until the next use of \\[org-agenda]) restricted to the current file.
2694 Pressing `<' twice means to restrict to the current subtree or region
2695 \(if active)."
2696 (interactive "P")
2697 (catch 'exit
2698 (let* ((prefix-descriptions nil)
2699 (org-agenda-buffer-name org-agenda-buffer-name)
2700 (org-agenda-window-setup (if (equal (buffer-name)
2701 org-agenda-buffer-name)
2702 'current-window
2703 org-agenda-window-setup))
2704 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2705 (org-agenda-custom-commands
2706 ;; normalize different versions
2707 (delq nil
2708 (mapcar
2709 (lambda (x)
2710 (cond ((stringp (cdr x))
2711 (push x prefix-descriptions)
2712 nil)
2713 ((stringp (nth 1 x)) x)
2714 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2715 (t (cons (car x) (cons "" (cdr x))))))
2716 org-agenda-custom-commands)))
2717 (org-agenda-custom-commands
2718 (org-contextualize-keys
2719 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2720 (buf (current-buffer))
2721 (bfn (buffer-file-name (buffer-base-buffer)))
2722 entry key type org-match lprops ans)
2723 ;; Turn off restriction unless there is an overriding one,
2724 (unless org-agenda-overriding-restriction
2725 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2726 ;; There is a request to keep the file list in place
2727 (put 'org-agenda-files 'org-restrict nil))
2728 (setq org-agenda-restrict nil)
2729 (move-marker org-agenda-restrict-begin nil)
2730 (move-marker org-agenda-restrict-end nil))
2731 ;; Delete old local properties
2732 (put 'org-agenda-redo-command 'org-lprops nil)
2733 ;; Delete previously set last-arguments
2734 (put 'org-agenda-redo-command 'last-args nil)
2735 ;; Remember where this call originated
2736 (setq org-agenda-last-dispatch-buffer (current-buffer))
2737 (unless org-keys
2738 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2739 org-keys (car ans)
2740 restriction (cdr ans)))
2741 ;; If we have sticky agenda buffers, set a name for the buffer,
2742 ;; depending on the invoking keys. The user may still set this
2743 ;; as a command option, which will overwrite what we do here.
2744 (if org-agenda-sticky
2745 (setq org-agenda-buffer-name
2746 (format "*Org Agenda(%s)*" org-keys)))
2747 ;; Establish the restriction, if any
2748 (when (and (not org-agenda-overriding-restriction) restriction)
2749 (put 'org-agenda-files 'org-restrict (list bfn))
2750 (cond
2751 ((eq restriction 'region)
2752 (setq org-agenda-restrict (current-buffer))
2753 (move-marker org-agenda-restrict-begin (region-beginning))
2754 (move-marker org-agenda-restrict-end (region-end)))
2755 ((eq restriction 'subtree)
2756 (save-excursion
2757 (setq org-agenda-restrict (current-buffer))
2758 (org-back-to-heading t)
2759 (move-marker org-agenda-restrict-begin (point))
2760 (move-marker org-agenda-restrict-end
2761 (progn (org-end-of-subtree t)))))))
2763 ;; For example the todo list should not need it (but does...)
2764 (cond
2765 ((setq entry (assoc org-keys org-agenda-custom-commands))
2766 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2767 (progn
2768 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
2769 lprops (nth 4 entry))
2770 (if org-agenda-sticky
2771 (setq org-agenda-buffer-name
2772 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2773 (format "*Org Agenda(%s)*" org-keys))))
2774 (put 'org-agenda-redo-command 'org-lprops lprops)
2775 (cond
2776 ((eq type 'agenda)
2777 (org-let lprops '(org-agenda-list current-prefix-arg)))
2778 ((eq type 'agenda*)
2779 (org-let lprops '(org-agenda-list current-prefix-arg nil nil t)))
2780 ((eq type 'alltodo)
2781 (org-let lprops '(org-todo-list current-prefix-arg)))
2782 ((eq type 'search)
2783 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
2784 ((eq type 'stuck)
2785 (org-let lprops '(org-agenda-list-stuck-projects
2786 current-prefix-arg)))
2787 ((eq type 'tags)
2788 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2789 ((eq type 'tags-todo)
2790 (org-let lprops '(org-tags-view '(4) org-match)))
2791 ((eq type 'todo)
2792 (org-let lprops '(org-todo-list org-match)))
2793 ((eq type 'tags-tree)
2794 (org-check-for-org-mode)
2795 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
2796 ((eq type 'todo-tree)
2797 (org-check-for-org-mode)
2798 (org-let lprops
2799 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2800 (regexp-quote org-match) "\\>"))))
2801 ((eq type 'occur-tree)
2802 (org-check-for-org-mode)
2803 (org-let lprops '(org-occur org-match)))
2804 ((functionp type)
2805 (org-let lprops '(funcall type org-match)))
2806 ((fboundp type)
2807 (org-let lprops '(funcall type org-match)))
2808 (t (user-error "Invalid custom agenda command type %s" type))))
2809 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2810 ((equal org-keys "C")
2811 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2812 (customize-variable 'org-agenda-custom-commands))
2813 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2814 ((equal org-keys "s") (call-interactively 'org-search-view))
2815 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2816 ((equal org-keys "t") (call-interactively 'org-todo-list))
2817 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2818 ((equal org-keys "m") (call-interactively 'org-tags-view))
2819 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2820 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2821 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
2822 (org-add-hook
2823 'post-command-hook
2824 (lambda ()
2825 (unless (current-message)
2826 (let* ((m (org-agenda-get-any-marker))
2827 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2828 (when note
2829 (message (concat
2830 "FLAGGING-NOTE ([?] for more info): "
2831 (org-add-props
2832 (replace-regexp-in-string
2833 "\\\\n" "//"
2834 (copy-sequence note))
2835 nil 'face 'org-warning)))))))
2836 t t))
2837 ((equal org-keys "L")
2838 (unless (derived-mode-p 'org-mode)
2839 (user-error "This is not an Org-mode file"))
2840 (unless restriction
2841 (put 'org-agenda-files 'org-restrict (list bfn))
2842 (org-call-with-arg 'org-timeline arg)))
2843 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2844 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2845 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
2846 (t (user-error "Invalid agenda key"))))))
2848 (defvar org-agenda-multi)
2850 (defun org-agenda-append-agenda ()
2851 "Append another agenda view to the current one.
2852 This function allows interactive building of block agendas.
2853 Agenda views are separated by `org-agenda-block-separator'."
2854 (interactive)
2855 (unless (derived-mode-p 'org-agenda-mode)
2856 (user-error "Can only append from within agenda buffer"))
2857 (let ((org-agenda-multi t))
2858 (org-agenda)
2859 (widen)
2860 (org-agenda-finalize)
2861 (setq buffer-read-only t)
2862 (org-agenda-fit-window-to-buffer)))
2864 (defun org-agenda-normalize-custom-commands (cmds)
2865 "Normalize custom commands CMDS."
2866 (delq nil
2867 (mapcar
2868 (lambda (x)
2869 (cond ((stringp (cdr x)) nil)
2870 ((stringp (nth 1 x)) x)
2871 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2872 (t (cons (car x) (cons "" (cdr x))))))
2873 cmds)))
2875 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2876 "The user interface for selecting an agenda command."
2877 (catch 'exit
2878 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2879 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2880 (region-p (org-region-active-p))
2881 (custom org-agenda-custom-commands)
2882 (selstring "")
2883 restriction second-time
2884 c entry key type match prefixes rmheader header-end custom1 desc
2885 line lines left right n n1)
2886 (save-window-excursion
2887 (delete-other-windows)
2888 (org-switch-to-buffer-other-window " *Agenda Commands*")
2889 (erase-buffer)
2890 (insert (eval-when-compile
2891 (let ((header
2892 "Press key for an agenda command: < Buffer, subtree/region restriction
2893 -------------------------------- > Remove restriction
2894 a Agenda for current week or day e Export agenda views
2895 t List of all TODO entries T Entries with special TODO kwd
2896 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2897 s Search for keywords S Like s, but only TODO entries
2898 L Timeline for current buffer # List stuck projects (!=configure)
2899 / Multi-occur C Configure custom agenda commands
2900 ? Find :FLAGGED: entries * Toggle sticky agenda views
2902 (start 0))
2903 (while (string-match
2904 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2905 header start)
2906 (setq start (match-end 0))
2907 (add-text-properties (match-beginning 2) (match-end 2)
2908 '(face bold) header))
2909 header)))
2910 (setq header-end (point-marker))
2911 (while t
2912 (setq custom1 custom)
2913 (when (eq rmheader t)
2914 (org-goto-line 1)
2915 (re-search-forward ":" nil t)
2916 (delete-region (match-end 0) (point-at-eol))
2917 (forward-char 1)
2918 (looking-at "-+")
2919 (delete-region (match-end 0) (point-at-eol))
2920 (move-marker header-end (match-end 0)))
2921 (goto-char header-end)
2922 (delete-region (point) (point-max))
2924 ;; Produce all the lines that describe custom commands and prefixes
2925 (setq lines nil)
2926 (while (setq entry (pop custom1))
2927 (setq key (car entry) desc (nth 1 entry)
2928 type (nth 2 entry)
2929 match (nth 3 entry))
2930 (if (> (length key) 1)
2931 (add-to-list 'prefixes (string-to-char key))
2932 (setq line
2933 (format
2934 "%-4s%-14s"
2935 (org-add-props (copy-sequence key)
2936 '(face bold))
2937 (cond
2938 ((string-match "\\S-" desc) desc)
2939 ((eq type 'agenda) "Agenda for current week or day")
2940 ((eq type 'agenda*) "Appointments for current week or day")
2941 ((eq type 'alltodo) "List of all TODO entries")
2942 ((eq type 'search) "Word search")
2943 ((eq type 'stuck) "List of stuck projects")
2944 ((eq type 'todo) "TODO keyword")
2945 ((eq type 'tags) "Tags query")
2946 ((eq type 'tags-todo) "Tags (TODO)")
2947 ((eq type 'tags-tree) "Tags tree")
2948 ((eq type 'todo-tree) "TODO kwd tree")
2949 ((eq type 'occur-tree) "Occur tree")
2950 ((functionp type) (if (symbolp type)
2951 (symbol-name type)
2952 "Lambda expression"))
2953 (t "???"))))
2954 (if org-agenda-menu-show-matcher
2955 (setq line
2956 (concat line ": "
2957 (cond
2958 ((stringp match)
2959 (setq match (copy-sequence match))
2960 (org-add-props match nil 'face 'org-warning))
2961 ((listp type)
2962 (format "set of %d commands" (length type))))))
2963 (if (org-string-nw-p match)
2964 (add-text-properties
2965 0 (length line) (list 'help-echo
2966 (concat "Matcher: " match)) line)))
2967 (push line lines)))
2968 (setq lines (nreverse lines))
2969 (when prefixes
2970 (mapc (lambda (x)
2971 (push
2972 (format "%s %s"
2973 (org-add-props (char-to-string x)
2974 nil 'face 'bold)
2975 (or (cdr (assoc (concat selstring
2976 (char-to-string x))
2977 prefix-descriptions))
2978 "Prefix key"))
2979 lines))
2980 prefixes))
2982 ;; Check if we should display in two columns
2983 (if org-agenda-menu-two-columns
2984 (progn
2985 (setq n (length lines)
2986 n1 (+ (/ n 2) (mod n 2))
2987 right (nthcdr n1 lines)
2988 left (copy-sequence lines))
2989 (setcdr (nthcdr (1- n1) left) nil))
2990 (setq left lines right nil))
2991 (while left
2992 (insert "\n" (pop left))
2993 (when right
2994 (if (< (current-column) 40)
2995 (move-to-column 40 t)
2996 (insert " "))
2997 (insert (pop right))))
2999 ;; Make the window the right size
3000 (goto-char (point-min))
3001 (if second-time
3002 (if (not (pos-visible-in-window-p (point-max)))
3003 (org-fit-window-to-buffer))
3004 (setq second-time t)
3005 (org-fit-window-to-buffer))
3007 ;; Ask for selection
3008 (message "Press key for agenda command%s:"
3009 (if (or restrict-ok org-agenda-overriding-restriction)
3010 (if org-agenda-overriding-restriction
3011 " (restriction lock active)"
3012 (if restriction
3013 (format " (restricted to %s)" restriction)
3014 " (unrestricted)"))
3015 ""))
3016 (setq c (read-char-exclusive))
3017 (message "")
3018 (cond
3019 ((assoc (char-to-string c) custom)
3020 (setq selstring (concat selstring (char-to-string c)))
3021 (throw 'exit (cons selstring restriction)))
3022 ((memq c prefixes)
3023 (setq selstring (concat selstring (char-to-string c))
3024 prefixes nil
3025 rmheader (or rmheader t)
3026 custom (delq nil (mapcar
3027 (lambda (x)
3028 (if (or (= (length (car x)) 1)
3029 (/= (string-to-char (car x)) c))
3031 (cons (substring (car x) 1) (cdr x))))
3032 custom))))
3033 ((eq c ?*)
3034 (call-interactively 'org-toggle-sticky-agenda)
3035 (sit-for 2))
3036 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
3037 (message "Restriction is only possible in Org-mode buffers")
3038 (ding) (sit-for 1))
3039 ((eq c ?1)
3040 (org-agenda-remove-restriction-lock 'noupdate)
3041 (setq restriction 'buffer))
3042 ((eq c ?0)
3043 (org-agenda-remove-restriction-lock 'noupdate)
3044 (setq restriction (if region-p 'region 'subtree)))
3045 ((eq c ?<)
3046 (org-agenda-remove-restriction-lock 'noupdate)
3047 (setq restriction
3048 (cond
3049 ((eq restriction 'buffer)
3050 (if region-p 'region 'subtree))
3051 ((memq restriction '(subtree region))
3052 nil)
3053 (t 'buffer))))
3054 ((eq c ?>)
3055 (org-agenda-remove-restriction-lock 'noupdate)
3056 (setq restriction nil))
3057 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
3058 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
3059 ((and (> (length selstring) 0) (eq c ?\d))
3060 (delete-window)
3061 (org-agenda-get-restriction-and-command prefix-descriptions))
3063 ((equal c ?q) (error "Abort"))
3064 (t (user-error "Invalid key %c" c))))))))
3066 (defun org-agenda-fit-window-to-buffer ()
3067 "Fit the window to the buffer size."
3068 (and (memq org-agenda-window-setup '(reorganize-frame))
3069 (fboundp 'fit-window-to-buffer)
3070 (if (and (= (cdr org-agenda-window-frame-fractions) 1.0)
3071 (= (car org-agenda-window-frame-fractions) 1.0))
3072 (delete-other-windows)
3073 (org-fit-window-to-buffer
3075 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3076 (floor (* (frame-height) (car org-agenda-window-frame-fractions)))))))
3078 (defvar org-cmd nil)
3079 (defvar org-agenda-overriding-cmd nil)
3080 (defvar org-agenda-overriding-arguments nil)
3081 (defvar org-agenda-overriding-cmd-arguments nil)
3082 (defun org-agenda-run-series (name series)
3083 "Run agenda NAME as a SERIES of agenda commands."
3084 (org-let (nth 1 series) '(org-agenda-prepare name))
3085 ;; We need to reset agenda markers here, because when constructing a
3086 ;; block agenda, the individual blocks do not do that.
3087 (org-agenda-reset-markers)
3088 (let* ((org-agenda-multi t)
3089 (redo (list 'org-agenda-run-series name (list 'quote series)))
3090 (cmds (car series))
3091 (gprops (nth 1 series))
3092 match ;; The byte compiler incorrectly complains about this. Keep it!
3093 org-cmd type lprops)
3094 (while (setq org-cmd (pop cmds))
3095 (setq type (car org-cmd)
3096 match (eval (nth 1 org-cmd))
3097 lprops (nth 2 org-cmd))
3098 (let ((org-agenda-overriding-arguments
3099 (if (eq org-agenda-overriding-cmd org-cmd)
3100 (or org-agenda-overriding-arguments
3101 org-agenda-overriding-cmd-arguments))))
3102 (cond
3103 ((eq type 'agenda)
3104 (org-let2 gprops lprops
3105 '(call-interactively 'org-agenda-list)))
3106 ((eq type 'agenda*)
3107 (org-let2 gprops lprops
3108 '(funcall 'org-agenda-list nil nil t)))
3109 ((eq type 'alltodo)
3110 (org-let2 gprops lprops
3111 '(call-interactively 'org-todo-list)))
3112 ((eq type 'search)
3113 (org-let2 gprops lprops
3114 '(org-search-view current-prefix-arg match nil)))
3115 ((eq type 'stuck)
3116 (org-let2 gprops lprops
3117 '(call-interactively 'org-agenda-list-stuck-projects)))
3118 ((eq type 'tags)
3119 (org-let2 gprops lprops
3120 '(org-tags-view current-prefix-arg match)))
3121 ((eq type 'tags-todo)
3122 (org-let2 gprops lprops
3123 '(org-tags-view '(4) match)))
3124 ((eq type 'todo)
3125 (org-let2 gprops lprops
3126 '(org-todo-list match)))
3127 ((fboundp type)
3128 (org-let2 gprops lprops
3129 '(funcall type match)))
3130 (t (error "Invalid type in command series")))))
3131 (widen)
3132 (let ((inhibit-read-only t))
3133 (add-text-properties (point-min) (point-max)
3134 `(org-series t org-series-redo-cmd ,redo)))
3135 (setq org-agenda-redo-command redo)
3136 (goto-char (point-min)))
3137 (org-agenda-fit-window-to-buffer)
3138 (org-let (nth 1 series) '(org-agenda-finalize)))
3140 ;;;###autoload
3141 (defmacro org-batch-agenda (cmd-key &rest parameters)
3142 "Run an agenda command in batch mode and send the result to STDOUT.
3143 If CMD-KEY is a string of length 1, it is used as a key in
3144 `org-agenda-custom-commands' and triggers this command. If it is a
3145 longer string it is used as a tags/todo match string.
3146 Parameters are alternating variable names and values that will be bound
3147 before running the agenda command."
3148 (org-eval-in-environment (org-make-parameter-alist parameters)
3149 (let (org-agenda-sticky)
3150 (if (> (length cmd-key) 2)
3151 (org-tags-view nil cmd-key)
3152 (org-agenda nil cmd-key))))
3153 (set-buffer org-agenda-buffer-name)
3154 (princ (buffer-string)))
3156 (defvar org-agenda-info nil)
3158 ;;;###autoload
3159 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
3160 "Run an agenda command in batch mode and send the result to STDOUT.
3161 If CMD-KEY is a string of length 1, it is used as a key in
3162 `org-agenda-custom-commands' and triggers this command. If it is a
3163 longer string it is used as a tags/todo match string.
3164 Parameters are alternating variable names and values that will be bound
3165 before running the agenda command.
3167 The output gives a line for each selected agenda item. Each
3168 item is a list of comma-separated values, like this:
3170 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3172 category The category of the item
3173 head The headline, without TODO kwd, TAGS and PRIORITY
3174 type The type of the agenda entry, can be
3175 todo selected in TODO match
3176 tagsmatch selected in tags match
3177 diary imported from diary
3178 deadline a deadline on given date
3179 scheduled scheduled on given date
3180 timestamp entry has timestamp on given date
3181 closed entry was closed on given date
3182 upcoming-deadline warning about deadline
3183 past-scheduled forwarded scheduled item
3184 block entry has date block including g. date
3185 todo The todo keyword, if any
3186 tags All tags including inherited ones, separated by colons
3187 date The relevant date, like 2007-2-14
3188 time The time, like 15:00-16:50
3189 extra Sting with extra planning info
3190 priority-l The priority letter if any was given
3191 priority-n The computed numerical priority
3192 agenda-day The day in the agenda where this is listed"
3193 (org-eval-in-environment (append '((org-agenda-remove-tags t))
3194 (org-make-parameter-alist parameters))
3195 (if (> (length cmd-key) 2)
3196 (org-tags-view nil cmd-key)
3197 (org-agenda nil cmd-key)))
3198 (set-buffer org-agenda-buffer-name)
3199 (let* ((lines (org-split-string (buffer-string) "\n"))
3200 line)
3201 (while (setq line (pop lines))
3202 (catch 'next
3203 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
3204 (setq org-agenda-info
3205 (org-fix-agenda-info (text-properties-at 0 line)))
3206 (princ
3207 (mapconcat 'org-agenda-export-csv-mapper
3208 '(org-category txt type todo tags date time extra
3209 priority-letter priority agenda-day)
3210 ","))
3211 (princ "\n")))))
3213 (defun org-fix-agenda-info (props)
3214 "Make sure all properties on an agenda item have a canonical form.
3215 This ensures the export commands can easily use it."
3216 (let (tmp re)
3217 (when (setq tmp (plist-get props 'tags))
3218 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3219 (when (setq tmp (plist-get props 'date))
3220 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3221 (let ((calendar-date-display-form '(year "-" month "-" day)))
3222 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3224 (setq tmp (calendar-date-string tmp)))
3225 (setq props (plist-put props 'date tmp)))
3226 (when (setq tmp (plist-get props 'day))
3227 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3228 (let ((calendar-date-display-form '(year "-" month "-" day)))
3229 (setq tmp (calendar-date-string tmp)))
3230 (setq props (plist-put props 'day tmp))
3231 (setq props (plist-put props 'agenda-day tmp)))
3232 (when (setq tmp (plist-get props 'txt))
3233 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3234 (plist-put props 'priority-letter (match-string 1 tmp))
3235 (setq tmp (replace-match "" t t tmp)))
3236 (when (and (setq re (plist-get props 'org-todo-regexp))
3237 (setq re (concat "\\`\\.*" re " ?"))
3238 (string-match re tmp))
3239 (plist-put props 'todo (match-string 1 tmp))
3240 (setq tmp (replace-match "" t t tmp)))
3241 (plist-put props 'txt tmp)))
3242 props)
3244 (defun org-agenda-export-csv-mapper (prop)
3245 (let ((res (plist-get org-agenda-info prop)))
3246 (setq res
3247 (cond
3248 ((not res) "")
3249 ((stringp res) res)
3250 (t (prin1-to-string res))))
3251 (while (string-match "," res)
3252 (setq res (replace-match ";" t t res)))
3253 (org-trim res)))
3255 ;;;###autoload
3256 (defun org-store-agenda-views (&rest parameters)
3257 "Store agenda views."
3258 (interactive)
3259 (eval (list 'org-batch-store-agenda-views)))
3261 ;;;###autoload
3262 (defmacro org-batch-store-agenda-views (&rest parameters)
3263 "Run all custom agenda commands that have a file argument."
3264 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3265 (pop-up-frames nil)
3266 (dir default-directory)
3267 (pars (org-make-parameter-alist parameters))
3268 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
3269 (save-window-excursion
3270 (while cmds
3271 (setq cmd (pop cmds)
3272 thiscmdkey (car cmd)
3273 thiscmdcmd (cdr cmd)
3274 match (nth 2 thiscmdcmd)
3275 bufname (if org-agenda-sticky
3276 (or (and (stringp match)
3277 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3278 (format "*Org Agenda(%s)*" thiscmdkey))
3279 org-agenda-buffer-name)
3280 cmd-or-set (nth 2 cmd)
3281 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3282 files (nth (if (listp cmd-or-set) 4 5) cmd))
3283 (if (stringp files) (setq files (list files)))
3284 (when files
3285 (org-eval-in-environment (append org-agenda-exporter-settings
3286 opts pars)
3287 (org-agenda nil thiscmdkey))
3288 (set-buffer bufname)
3289 (while files
3290 (org-eval-in-environment (append org-agenda-exporter-settings
3291 opts pars)
3292 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3293 (and (get-buffer bufname)
3294 (kill-buffer bufname)))))))
3296 (defvar org-agenda-current-span nil
3297 "The current span used in the agenda view.") ; local variable in the agenda buffer
3298 (defun org-agenda-mark-header-line (pos)
3299 "Mark the line at POS as an agenda structure header."
3300 (save-excursion
3301 (goto-char pos)
3302 (put-text-property (point-at-bol) (point-at-eol)
3303 'org-agenda-structural-header t)
3304 (when org-agenda-title-append
3305 (put-text-property (point-at-bol) (point-at-eol)
3306 'org-agenda-title-append org-agenda-title-append))))
3308 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3309 (defvar org-agenda-write-buffer-name "Agenda View")
3310 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3311 "Write the current buffer (an agenda view) as a file.
3312 Depending on the extension of the file name, plain text (.txt),
3313 HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
3314 If the extension is .ics, run icalendar export over all files used
3315 to construct the agenda and limit the export to entries listed in the
3316 agenda now.
3317 If the extension is .org, collect all subtrees corresponding to the
3318 agenda entries and add them in an .org file.
3319 With prefix argument OPEN, open the new file immediately.
3320 If NOSETTINGS is given, do not scope the settings of
3321 `org-agenda-exporter-settings' into the export commands. This is used when
3322 the settings have already been scoped and we do not wish to overrule other,
3323 higher priority settings.
3324 If AGENDA-BUFFER-NAME, use this as the buffer name for the agenda to write."
3325 (interactive "FWrite agenda to file: \nP")
3326 (if (or (not (file-writable-p file))
3327 (and (file-exists-p file)
3328 (if (org-called-interactively-p 'any)
3329 (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
3330 (user-error "Cannot write agenda to file %s" file))
3331 (org-let (if nosettings nil org-agenda-exporter-settings)
3332 '(save-excursion
3333 (save-window-excursion
3334 (org-agenda-mark-filtered-text)
3335 (let ((bs (copy-sequence (buffer-string))) beg content)
3336 (org-agenda-unmark-filtered-text)
3337 (with-temp-buffer
3338 (rename-buffer org-agenda-write-buffer-name t)
3339 (set-buffer-modified-p nil)
3340 (insert bs)
3341 (org-agenda-remove-marked-text 'org-filtered)
3342 (while (setq beg (text-property-any (point-min) (point-max)
3343 'org-filtered t))
3344 (delete-region
3345 beg (or (next-single-property-change beg 'org-filtered)
3346 (point-max))))
3347 (run-hooks 'org-agenda-before-write-hook)
3348 (cond
3349 ((org-bound-and-true-p org-mobile-creating-agendas)
3350 (org-mobile-write-agenda-for-mobile file))
3351 ((string-match "\\.org\\'" file)
3352 (let (content p m message-log-max)
3353 (goto-char (point-min))
3354 (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
3355 (goto-char p)
3356 (setq m (get-text-property (point) 'org-hd-marker))
3357 (when m
3358 (push (save-excursion
3359 (set-buffer (marker-buffer m))
3360 (goto-char m)
3361 (org-copy-subtree 1 nil t t)
3362 org-subtree-clip)
3363 content)))
3364 (find-file file)
3365 (erase-buffer)
3366 (mapcar (lambda (s) (org-paste-subtree 1 s)) (reverse content))
3367 (write-file file)
3368 (kill-buffer (current-buffer))
3369 (message "Org file written to %s" file)))
3370 ((string-match "\\.html?\\'" file)
3371 (require 'htmlize)
3372 (set-buffer (htmlize-buffer (current-buffer)))
3373 (when org-agenda-export-html-style
3374 ;; replace <style> section with org-agenda-export-html-style
3375 (goto-char (point-min))
3376 (kill-region (- (search-forward "<style") 6)
3377 (search-forward "</style>"))
3378 (insert org-agenda-export-html-style))
3379 (write-file file)
3380 (kill-buffer (current-buffer))
3381 (message "HTML written to %s" file))
3382 ((string-match "\\.ps\\'" file)
3383 (require 'ps-print)
3384 (ps-print-buffer-with-faces file)
3385 (message "Postscript written to %s" file))
3386 ((string-match "\\.pdf\\'" file)
3387 (require 'ps-print)
3388 (ps-print-buffer-with-faces
3389 (concat (file-name-sans-extension file) ".ps"))
3390 (call-process "ps2pdf" nil nil nil
3391 (expand-file-name
3392 (concat (file-name-sans-extension file) ".ps"))
3393 (expand-file-name file))
3394 (delete-file (concat (file-name-sans-extension file) ".ps"))
3395 (message "PDF written to %s" file))
3396 ((string-match "\\.ics\\'" file)
3397 (require 'ox-icalendar)
3398 (org-icalendar-export-current-agenda (expand-file-name file)))
3400 (let ((bs (buffer-string)))
3401 (find-file file)
3402 (erase-buffer)
3403 (insert bs)
3404 (save-buffer 0)
3405 (kill-buffer (current-buffer))
3406 (message "Plain text written to %s" file))))))))
3407 (set-buffer (or agenda-bufname
3408 (and (org-called-interactively-p 'any) (buffer-name))
3409 org-agenda-buffer-name)))
3410 (when open (org-open-file file)))
3412 (defvar org-agenda-tag-filter-overlays nil)
3413 (defvar org-agenda-cat-filter-overlays nil)
3414 (defvar org-agenda-re-filter-overlays nil)
3416 (defun org-agenda-mark-filtered-text ()
3417 "Mark all text hidden by filtering with a text property."
3418 (let ((inhibit-read-only t))
3419 (mapc
3420 (lambda (o)
3421 (when (equal (overlay-buffer o) (current-buffer))
3422 (put-text-property
3423 (overlay-start o) (overlay-end o)
3424 'org-filtered t)))
3425 (append org-agenda-tag-filter-overlays
3426 org-agenda-cat-filter-overlays
3427 org-agenda-re-filter-overlays))))
3429 (defun org-agenda-unmark-filtered-text ()
3430 "Remove the filtering text property."
3431 (let ((inhibit-read-only t))
3432 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
3434 (defun org-agenda-remove-marked-text (property &optional value)
3435 "Delete all text marked with VALUE of PROPERTY.
3436 VALUE defaults to t."
3437 (let (beg)
3438 (setq value (or value t))
3439 (while (setq beg (text-property-any (point-min) (point-max)
3440 property value))
3441 (delete-region
3442 beg (or (next-single-property-change beg 'org-filtered)
3443 (point-max))))))
3445 (defun org-agenda-add-entry-text ()
3446 "Add entry text to agenda lines.
3447 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3448 entry text following headings shown in the agenda.
3449 Drawers will be excluded, also the line with scheduling/deadline info."
3450 (when (and (> org-agenda-add-entry-text-maxlines 0)
3451 (not (org-bound-and-true-p org-mobile-creating-agendas)))
3452 (let (m txt)
3453 (goto-char (point-min))
3454 (while (not (eobp))
3455 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3456 (beginning-of-line 2)
3457 (setq txt (org-agenda-get-some-entry-text
3458 m org-agenda-add-entry-text-maxlines " > "))
3459 (end-of-line 1)
3460 (if (string-match "\\S-" txt)
3461 (insert "\n" txt)
3462 (or (eobp) (forward-char 1))))))))
3464 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3465 &rest keep)
3466 "Extract entry text from MARKER, at most N-LINES lines.
3467 This will ignore drawers etc, just get the text.
3468 If INDENT is given, prefix every line with this string. If KEEP is
3469 given, it is a list of symbols, defining stuff that should not be
3470 removed from the entry content. Currently only `planning' is allowed here."
3471 (let (txt drawer-re kwd-time-re ind)
3472 (save-excursion
3473 (with-current-buffer (marker-buffer marker)
3474 (if (not (derived-mode-p 'org-mode))
3475 (setq txt "")
3476 (save-excursion
3477 (save-restriction
3478 (widen)
3479 (goto-char marker)
3480 (end-of-line 1)
3481 (setq txt (buffer-substring
3482 (min (1+ (point)) (point-max))
3483 (progn (outline-next-heading) (point)))
3484 drawer-re org-drawer-regexp
3485 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3486 ".*\n?"))
3487 (with-temp-buffer
3488 (insert txt)
3489 (when org-agenda-add-entry-text-descriptive-links
3490 (goto-char (point-min))
3491 (while (org-activate-bracket-links (point-max))
3492 (add-text-properties (match-beginning 0) (match-end 0)
3493 '(face org-link))))
3494 (goto-char (point-min))
3495 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3496 (set-text-properties (match-beginning 0) (match-end 0)
3497 nil))
3498 (goto-char (point-min))
3499 (while (re-search-forward drawer-re nil t)
3500 (delete-region
3501 (match-beginning 0)
3502 (progn (re-search-forward
3503 "^[ \t]*:END:.*\n?" nil 'move)
3504 (point))))
3505 (unless (member 'planning keep)
3506 (goto-char (point-min))
3507 (while (re-search-forward kwd-time-re nil t)
3508 (replace-match "")))
3509 (goto-char (point-min))
3510 (when org-agenda-entry-text-exclude-regexps
3511 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3512 (while (setq re (pop re-list))
3513 (goto-char (point-min))
3514 (while (re-search-forward re nil t)
3515 (replace-match "")))))
3516 (goto-char (point-max))
3517 (skip-chars-backward " \t\n")
3518 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3520 ;; find and remove min common indentation
3521 (goto-char (point-min))
3522 (untabify (point-min) (point-max))
3523 (setq ind (org-get-indentation))
3524 (while (not (eobp))
3525 (unless (looking-at "[ \t]*$")
3526 (setq ind (min ind (org-get-indentation))))
3527 (beginning-of-line 2))
3528 (goto-char (point-min))
3529 (while (not (eobp))
3530 (unless (looking-at "[ \t]*$")
3531 (move-to-column ind)
3532 (delete-region (point-at-bol) (point)))
3533 (beginning-of-line 2))
3535 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3537 (goto-char (point-min))
3538 (when indent
3539 (while (and (not (eobp)) (re-search-forward "^" nil t))
3540 (replace-match indent t t)))
3541 (goto-char (point-min))
3542 (while (looking-at "[ \t]*\n") (replace-match ""))
3543 (goto-char (point-max))
3544 (when (> (org-current-line)
3545 n-lines)
3546 (org-goto-line (1+ n-lines))
3547 (backward-char 1))
3548 (setq txt (buffer-substring (point-min) (point)))))))))
3549 txt))
3551 (defun org-check-for-org-mode ()
3552 "Make sure current buffer is in org-mode. Error if not."
3553 (or (derived-mode-p 'org-mode)
3554 (error "Cannot execute org-mode agenda command on buffer in %s"
3555 major-mode)))
3557 ;;; Agenda prepare and finalize
3559 (defvar org-agenda-multi nil) ; dynamically scoped
3560 (defvar org-agenda-pre-window-conf nil)
3561 (defvar org-agenda-columns-active nil)
3562 (defvar org-agenda-name nil)
3563 (defvar org-agenda-tag-filter nil)
3564 (defvar org-agenda-category-filter nil)
3565 (defvar org-agenda-regexp-filter nil)
3566 (defvar org-agenda-top-headline-filter nil)
3567 (defvar org-agenda-tag-filter-while-redo nil)
3568 (defvar org-agenda-tag-filter-preset nil
3569 "A preset of the tags filter used for secondary agenda filtering.
3570 This must be a list of strings, each string must be a single tag preceded
3571 by \"+\" or \"-\".
3572 This variable should not be set directly, but agenda custom commands can
3573 bind it in the options section. The preset filter is a global property of
3574 the entire agenda view. In a block agenda, it will not work reliably to
3575 define a filter for one of the individual blocks. You need to set it in
3576 the global options and expect it to be applied to the entire view.")
3578 (defvar org-agenda-category-filter-preset nil
3579 "A preset of the category filter used for secondary agenda filtering.
3580 This must be a list of strings, each string must be a single category
3581 preceded by \"+\" or \"-\".
3582 This variable should not be set directly, but agenda custom commands can
3583 bind it in the options section. The preset filter is a global property of
3584 the entire agenda view. In a block agenda, it will not work reliably to
3585 define a filter for one of the individual blocks. You need to set it in
3586 the global options and expect it to be applied to the entire view.")
3588 (defvar org-agenda-regexp-filter-preset nil
3589 "A preset of the regexp filter used for secondary agenda filtering.
3590 This must be a list of strings, each string must be a single regexp
3591 preceded by \"+\" or \"-\".
3592 This variable should not be set directly, but agenda custom commands can
3593 bind it in the options section. The preset filter is a global property of
3594 the entire agenda view. In a block agenda, it will not work reliably to
3595 define a filter for one of the individual blocks. You need to set it in
3596 the global options and expect it to be applied to the entire view.")
3598 (defun org-agenda-use-sticky-p ()
3599 "Return non-nil if an agenda buffer named
3600 `org-agenda-buffer-name' exists and should be shown instead of
3601 generating a new one."
3602 (and
3603 ;; turned off by user
3604 org-agenda-sticky
3605 ;; For multi-agenda buffer already exists
3606 (not org-agenda-multi)
3607 ;; buffer found
3608 (get-buffer org-agenda-buffer-name)
3609 ;; C-u parameter is same as last call
3610 (with-current-buffer (get-buffer org-agenda-buffer-name)
3611 (and
3612 (equal current-prefix-arg
3613 org-agenda-last-prefix-arg)
3614 ;; In case user turned stickiness on, while having existing
3615 ;; Agenda buffer active, don't reuse that buffer, because it
3616 ;; does not have org variables local
3617 org-agenda-this-buffer-is-sticky))))
3619 (defun org-agenda-prepare-window (abuf)
3620 "Setup agenda buffer in the window."
3621 (let* ((awin (get-buffer-window abuf))
3622 wconf)
3623 (cond
3624 ((equal (current-buffer) abuf) nil)
3625 (awin (select-window awin))
3626 ((not (setq wconf (current-window-configuration))))
3627 ((equal org-agenda-window-setup 'current-window)
3628 (org-pop-to-buffer-same-window abuf))
3629 ((equal org-agenda-window-setup 'other-window)
3630 (org-switch-to-buffer-other-window abuf))
3631 ((equal org-agenda-window-setup 'other-frame)
3632 (switch-to-buffer-other-frame abuf))
3633 ((equal org-agenda-window-setup 'reorganize-frame)
3634 (delete-other-windows)
3635 (org-switch-to-buffer-other-window abuf)))
3636 ;; additional test in case agenda is invoked from within agenda
3637 ;; buffer via elisp link
3638 (unless (equal (current-buffer) abuf)
3639 (org-pop-to-buffer-same-window abuf))
3640 (setq org-agenda-pre-window-conf
3641 (or org-agenda-pre-window-conf wconf))))
3643 (defun org-agenda-prepare (&optional name)
3644 (if (org-agenda-use-sticky-p)
3645 (progn
3646 ;; Popup existing buffer
3647 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name))
3648 (message "Sticky Agenda buffer, use `r' to refresh")
3649 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3650 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3651 (setq org-todo-keywords-for-agenda nil)
3652 (unless org-agenda-persistent-filter
3653 (setq org-agenda-tag-filter nil
3654 org-agenda-category-filter nil
3655 org-agenda-regexp-filter nil))
3656 (put 'org-agenda-tag-filter :preset-filter
3657 org-agenda-tag-filter-preset)
3658 (put 'org-agenda-category-filter :preset-filter
3659 org-agenda-category-filter-preset)
3660 (put 'org-agenda-regexp-filter :preset-filter
3661 org-agenda-regexp-filter-preset)
3662 (if org-agenda-multi
3663 (progn
3664 (setq buffer-read-only nil)
3665 (goto-char (point-max))
3666 (unless (or (bobp) org-agenda-compact-blocks
3667 (not org-agenda-block-separator))
3668 (insert "\n"
3669 (if (stringp org-agenda-block-separator)
3670 org-agenda-block-separator
3671 (make-string (window-width) org-agenda-block-separator))
3672 "\n"))
3673 (narrow-to-region (point) (point-max)))
3674 (setq org-done-keywords-for-agenda nil)
3676 ;; Setting any org variables that are in org-agenda-local-vars
3677 ;; list need to be done after the prepare call
3678 (org-agenda-prepare-window (get-buffer-create org-agenda-buffer-name))
3679 (setq buffer-read-only nil)
3680 (org-agenda-reset-markers)
3681 (let ((inhibit-read-only t)) (erase-buffer))
3682 (org-agenda-mode)
3683 (setq org-agenda-buffer (current-buffer))
3684 (setq org-agenda-contributing-files nil)
3685 (setq org-agenda-columns-active nil)
3686 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3687 (setq org-todo-keywords-for-agenda
3688 (org-uniquify org-todo-keywords-for-agenda))
3689 (setq org-done-keywords-for-agenda
3690 (org-uniquify org-done-keywords-for-agenda))
3691 (setq org-agenda-last-prefix-arg current-prefix-arg)
3692 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3693 (and name (not org-agenda-name)
3694 (org-set-local 'org-agenda-name name)))
3695 (setq buffer-read-only nil)))
3697 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3698 (defun org-agenda-finalize ()
3699 "Finishing touch for the agenda buffer, called just before displaying it."
3700 (unless org-agenda-multi
3701 (save-excursion
3702 (let ((inhibit-read-only t))
3703 (goto-char (point-min))
3704 (save-excursion
3705 (while (org-activate-bracket-links (point-max))
3706 (add-text-properties (match-beginning 0) (match-end 0)
3707 '(face org-link))))
3708 (save-excursion
3709 (while (org-activate-plain-links (point-max))
3710 (add-text-properties (match-beginning 0) (match-end 0)
3711 '(face org-link))))
3712 (unless (eq org-agenda-remove-tags t)
3713 (org-agenda-align-tags))
3714 (unless org-agenda-with-colors
3715 (remove-text-properties (point-min) (point-max) '(face nil)))
3716 (if (and (boundp 'org-agenda-overriding-columns-format)
3717 org-agenda-overriding-columns-format)
3718 (org-set-local 'org-agenda-overriding-columns-format
3719 org-agenda-overriding-columns-format))
3720 (if (and (boundp 'org-agenda-view-columns-initially)
3721 org-agenda-view-columns-initially)
3722 (org-agenda-columns))
3723 (when org-agenda-fontify-priorities
3724 (org-agenda-fontify-priorities))
3725 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3726 (org-agenda-dim-blocked-tasks))
3727 (org-agenda-mark-clocking-task)
3728 (when org-agenda-entry-text-mode
3729 (org-agenda-entry-text-hide)
3730 (org-agenda-entry-text-show))
3731 (if (and (functionp 'org-habit-insert-consistency-graphs)
3732 (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
3733 (org-habit-insert-consistency-graphs))
3734 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3735 (unless (or (eq org-agenda-show-inherited-tags 'always)
3736 (and (listp org-agenda-show-inherited-tags)
3737 (memq org-agenda-type org-agenda-show-inherited-tags))
3738 (and (eq org-agenda-show-inherited-tags t)
3739 (or (eq org-agenda-use-tag-inheritance t)
3740 (and (listp org-agenda-use-tag-inheritance)
3741 (not (memq org-agenda-type
3742 org-agenda-use-tag-inheritance))))))
3743 (let (mrk)
3744 (save-excursion
3745 (goto-char (point-min))
3746 (while (equal (forward-line) 0)
3747 (when (setq mrk (or (get-text-property (point) 'org-hd-marker)
3748 (get-text-property (point) 'org-hd-marker)))
3749 (put-text-property (point-at-bol) (point-at-eol)
3750 'tags (org-with-point-at mrk
3751 (delete-dups
3752 (mapcar 'downcase (org-get-tags-at))))))))))
3753 (run-hooks 'org-agenda-finalize-hook)
3754 (when (or org-agenda-tag-filter (get 'org-agenda-tag-filter :preset-filter))
3755 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
3756 (when (or org-agenda-category-filter (get 'org-agenda-category-filter :preset-filter))
3757 (org-agenda-filter-apply org-agenda-category-filter 'category))
3758 (when (or org-agenda-regexp-filter (get 'org-agenda-regexp-filter :preset-filter))
3759 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
3760 (org-add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)))))
3762 (defun org-agenda-mark-clocking-task ()
3763 "Mark the current clock entry in the agenda if it is present."
3764 ;; We need to widen when `org-agenda-finalize' is called from
3765 ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
3766 (when org-clock-current-task
3767 (save-restriction
3768 (widen)
3769 (org-agenda-unmark-clocking-task)
3770 (when (marker-buffer org-clock-hd-marker)
3771 (save-excursion
3772 (goto-char (point-min))
3773 (let (s ov)
3774 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3775 (goto-char s)
3776 (when (equal (org-get-at-bol 'org-hd-marker)
3777 org-clock-hd-marker)
3778 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3779 (overlay-put ov 'type 'org-agenda-clocking)
3780 (overlay-put ov 'face 'org-agenda-clocking)
3781 (overlay-put ov 'help-echo
3782 "The clock is running in this item")))))))))
3784 (defun org-agenda-unmark-clocking-task ()
3785 "Unmark the current clocking task."
3786 (mapc (lambda (o)
3787 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3788 (delete-overlay o)))
3789 (overlays-in (point-min) (point-max))))
3791 (defun org-agenda-fontify-priorities ()
3792 "Make highest priority lines bold, and lowest italic."
3793 (interactive)
3794 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3795 (delete-overlay o)))
3796 (overlays-in (point-min) (point-max)))
3797 (save-excursion
3798 (let (b e p ov h l)
3799 (goto-char (point-min))
3800 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3801 (setq h (or (get-char-property (point) 'org-highest-priority)
3802 org-highest-priority)
3803 l (or (get-char-property (point) 'org-lowest-priority)
3804 org-lowest-priority)
3805 p (string-to-char (match-string 1))
3806 b (match-beginning 0)
3807 e (if (eq org-agenda-fontify-priorities 'cookies)
3808 (match-end 0)
3809 (point-at-eol))
3810 ov (make-overlay b e))
3811 (overlay-put
3812 ov 'face
3813 (cons (cond ((org-face-from-face-or-color
3814 'priority nil
3815 (cdr (assoc p org-priority-faces))))
3816 ((and (listp org-agenda-fontify-priorities)
3817 (org-face-from-face-or-color
3818 'priority nil
3819 (cdr (assoc p org-agenda-fontify-priorities)))))
3820 ((equal p l) 'italic)
3821 ((equal p h) 'bold))
3822 'org-priority))
3823 (overlay-put ov 'org-type 'org-priority)))))
3825 (defvar org-depend-tag-blocked)
3827 (defun org-agenda-dim-blocked-tasks (&optional invisible)
3828 "Dim currently blocked TODO's in the agenda display.
3829 When INVISIBLE is non-nil, hide currently blocked TODO instead of
3830 dimming them."
3831 (interactive "P")
3832 (when (org-called-interactively-p 'interactive)
3833 (message "Dim or hide blocked tasks..."))
3834 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3835 (delete-overlay o)))
3836 (overlays-in (point-min) (point-max)))
3837 (save-excursion
3838 (let ((inhibit-read-only t)
3839 (org-depend-tag-blocked nil)
3840 (invis (or (not (null invisible))
3841 (eq org-agenda-dim-blocked-tasks 'invisible)))
3842 org-blocked-by-checkboxes
3843 invis1 b e p ov h l)
3844 (goto-char (point-min))
3845 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3846 (and pos (goto-char (1+ pos))))
3847 (setq org-blocked-by-checkboxes nil invis1 invis)
3848 (let ((marker (org-get-at-bol 'org-hd-marker)))
3849 (when (and marker
3850 (with-current-buffer (marker-buffer marker)
3851 (save-excursion (goto-char marker)
3852 (org-entry-blocked-p))))
3853 (if org-blocked-by-checkboxes (setq invis1 nil))
3854 (setq b (if invis1
3855 (max (point-min) (1- (point-at-bol)))
3856 (point-at-bol))
3857 e (point-at-eol)
3858 ov (make-overlay b e))
3859 (if invis1
3860 (overlay-put ov 'invisible t)
3861 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3862 (overlay-put ov 'org-type 'org-blocked-todo))))))
3863 (when (org-called-interactively-p 'interactive)
3864 (message "Dim or hide blocked tasks...done")))
3866 (defvar org-agenda-skip-function nil
3867 "Function to be called at each match during agenda construction.
3868 If this function returns nil, the current match should not be skipped.
3869 Otherwise, the function must return a position from where the search
3870 should be continued.
3871 This may also be a Lisp form, it will be evaluated.
3872 Never set this variable using `setq' or so, because then it will apply
3873 to all future agenda commands. If you do want a global skipping condition,
3874 use the option `org-agenda-skip-function-global' instead.
3875 The correct usage for `org-agenda-skip-function' is to bind it with
3876 `let' to scope it dynamically into the agenda-constructing command.
3877 A good way to set it is through options in `org-agenda-custom-commands'.")
3879 (defun org-agenda-skip ()
3880 "Throw to `:skip' in places that should be skipped.
3881 Also moves point to the end of the skipped region, so that search can
3882 continue from there."
3883 (let ((p (point-at-bol)) to)
3884 (when (or
3885 (save-excursion (goto-char p) (looking-at comment-start-skip))
3886 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3887 (get-text-property p :org-archived)
3888 (org-end-of-subtree t))
3889 (and org-agenda-skip-comment-trees
3890 (get-text-property p :org-comment)
3891 (org-end-of-subtree t))
3892 (and (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3893 (org-agenda-skip-eval org-agenda-skip-function)))
3894 (goto-char to))
3895 (org-in-src-block-p t))
3896 (throw :skip t))))
3898 (defun org-agenda-skip-eval (form)
3899 "If FORM is a function or a list, call (or eval) it and return the result.
3900 `save-excursion' and `save-match-data' are wrapped around the call, so point
3901 and match data are returned to the previous state no matter what these
3902 functions do."
3903 (let (fp)
3904 (and form
3905 (or (setq fp (functionp form))
3906 (consp form))
3907 (save-excursion
3908 (save-match-data
3909 (if fp
3910 (funcall form)
3911 (eval form)))))))
3913 (defvar org-agenda-markers nil
3914 "List of all currently active markers created by `org-agenda'.")
3915 (defvar org-agenda-last-marker-time (org-float-time)
3916 "Creation time of the last agenda marker.")
3918 (defun org-agenda-new-marker (&optional pos)
3919 "Return a new agenda marker.
3920 Org-mode keeps a list of these markers and resets them when they are
3921 no longer in use."
3922 (let ((m (copy-marker (or pos (point)))))
3923 (setq org-agenda-last-marker-time (org-float-time))
3924 (if org-agenda-buffer
3925 (with-current-buffer org-agenda-buffer
3926 (push m org-agenda-markers))
3927 (push m org-agenda-markers))
3930 (defun org-agenda-reset-markers ()
3931 "Reset markers created by `org-agenda'."
3932 (while org-agenda-markers
3933 (move-marker (pop org-agenda-markers) nil)))
3935 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3936 "Save relative positions of markers in region.
3937 This check for agenda markers in all agenda buffers currently active."
3938 (dolist (buf (buffer-list))
3939 (with-current-buffer buf
3940 (when (eq major-mode 'org-agenda-mode)
3941 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3942 org-agenda-markers)))))
3944 ;;; Entry text mode
3946 (defun org-agenda-entry-text-show-here ()
3947 "Add some text from the entry as context to the current line."
3948 (let (m txt o)
3949 (setq m (org-get-at-bol 'org-hd-marker))
3950 (unless (marker-buffer m)
3951 (error "No marker points to an entry here"))
3952 (setq txt (concat "\n" (org-no-properties
3953 (org-agenda-get-some-entry-text
3954 m org-agenda-entry-text-maxlines
3955 org-agenda-entry-text-leaders))))
3956 (when (string-match "\\S-" txt)
3957 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3958 (overlay-put o 'evaporate t)
3959 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3960 (overlay-put o 'after-string txt))))
3962 (defun org-agenda-entry-text-show ()
3963 "Add entry context for all agenda lines."
3964 (interactive)
3965 (save-excursion
3966 (goto-char (point-max))
3967 (beginning-of-line 1)
3968 (while (not (bobp))
3969 (when (org-get-at-bol 'org-hd-marker)
3970 (org-agenda-entry-text-show-here))
3971 (beginning-of-line 0))))
3973 (defun org-agenda-entry-text-hide ()
3974 "Remove any shown entry context."
3975 (delq nil
3976 (mapcar (lambda (o)
3977 (if (eq (overlay-get o 'org-overlay-type)
3978 'agenda-entry-content)
3979 (progn (delete-overlay o) t)))
3980 (overlays-in (point-min) (point-max)))))
3982 (defun org-agenda-get-day-face (date)
3983 "Return the face DATE should be displayed with."
3984 (or (and (functionp org-agenda-day-face-function)
3985 (funcall org-agenda-day-face-function date))
3986 (cond ((org-agenda-todayp date)
3987 'org-agenda-date-today)
3988 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3989 'org-agenda-date-weekend)
3990 (t 'org-agenda-date))))
3992 ;;; Agenda timeline
3994 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3995 (defvar org-agenda-show-log-scoped) ;; dynamically scope in `org-timeline' or `org-agenda-list'
3997 (defun org-timeline (&optional dotodo)
3998 "Show a time-sorted view of the entries in the current org file.
3999 Only entries with a time stamp of today or later will be listed. With
4000 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
4001 under the current date.
4002 If the buffer contains an active region, only check the region for
4003 dates."
4004 (interactive "P")
4005 (let* ((dopast t)
4006 (org-agenda-show-log-scoped org-agenda-show-log)
4007 (org-agenda-show-log org-agenda-show-log-scoped)
4008 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
4009 (current-buffer))))
4010 (date (calendar-current-date))
4011 (beg (if (org-region-active-p) (region-beginning) (point-min)))
4012 (end (if (org-region-active-p) (region-end) (point-max)))
4013 (day-numbers (org-get-all-dates
4014 beg end 'no-ranges
4015 t org-agenda-show-log-scoped ; always include today
4016 org-timeline-show-empty-dates))
4017 (org-deadline-warning-days 0)
4018 (org-agenda-only-exact-dates t)
4019 (today (org-today))
4020 (past t)
4021 args
4022 s e rtn d emptyp)
4023 (setq org-agenda-redo-command
4024 (list 'let
4025 (list (list 'org-agenda-show-log 'org-agenda-show-log))
4026 (list 'org-switch-to-buffer-other-window (current-buffer))
4027 (list 'org-timeline (list 'quote dotodo))))
4028 (put 'org-agenda-redo-command 'org-lprops nil)
4029 (if (not dopast)
4030 ;; Remove past dates from the list of dates.
4031 (setq day-numbers (delq nil (mapcar (lambda(x)
4032 (if (>= x today) x nil))
4033 day-numbers))))
4034 (org-agenda-prepare (concat "Timeline " (file-name-nondirectory entry)))
4035 (org-compile-prefix-format 'timeline)
4036 (org-set-sorting-strategy 'timeline)
4037 (if org-agenda-show-log-scoped (push :closed args))
4038 (push :timestamp args)
4039 (push :deadline args)
4040 (push :scheduled args)
4041 (push :sexp args)
4042 (if dotodo (push :todo args))
4043 (insert "Timeline of file " entry "\n")
4044 (add-text-properties (point-min) (point)
4045 (list 'face 'org-agenda-structure))
4046 (org-agenda-mark-header-line (point-min))
4047 (while (setq d (pop day-numbers))
4048 (if (and (listp d) (eq (car d) :omitted))
4049 (progn
4050 (setq s (point))
4051 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
4052 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
4053 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
4054 (if (and (>= d today)
4055 dopast
4056 past)
4057 (progn
4058 (setq past nil)
4059 (insert (make-string 79 ?-) "\n")))
4060 (setq date (calendar-gregorian-from-absolute d))
4061 (setq s (point))
4062 (setq rtn (and (not emptyp)
4063 (apply 'org-agenda-get-day-entries entry
4064 date args)))
4065 (if (or rtn (equal d today) org-timeline-show-empty-dates)
4066 (progn
4067 (insert
4068 (if (stringp org-agenda-format-date)
4069 (format-time-string org-agenda-format-date
4070 (org-time-from-absolute date))
4071 (funcall org-agenda-format-date date))
4072 "\n")
4073 (put-text-property s (1- (point)) 'face
4074 (org-agenda-get-day-face date))
4075 (put-text-property s (1- (point)) 'org-date-line t)
4076 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4077 (if (equal d today)
4078 (put-text-property s (1- (point)) 'org-today t))
4079 (and rtn (insert (org-agenda-finalize-entries rtn 'timeline) "\n"))
4080 (put-text-property s (1- (point)) 'day d)))))
4081 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
4082 (point-min)))
4083 (add-text-properties
4084 (point-min) (point-max)
4085 `(org-agenda-type timeline org-redo-cmd ,org-agenda-redo-command))
4086 (org-agenda-finalize)
4087 (setq buffer-read-only t)))
4089 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
4090 "Return a list of all relevant day numbers from BEG to END buffer positions.
4091 If NO-RANGES is non-nil, include only the start and end dates of a range,
4092 not every single day in the range. If FORCE-TODAY is non-nil, make
4093 sure that TODAY is included in the list. If INACTIVE is non-nil, also
4094 inactive time stamps (those in square brackets) are included.
4095 When EMPTY is non-nil, also include days without any entries."
4096 (let ((re (concat
4097 (if pre-re pre-re "")
4098 (if inactive org-ts-regexp-both org-ts-regexp)))
4099 dates dates1 date day day1 day2 ts1 ts2 pos)
4100 (if force-today
4101 (setq dates (list (org-today))))
4102 (save-excursion
4103 (goto-char beg)
4104 (while (re-search-forward re end t)
4105 (setq day (time-to-days (org-time-string-to-time
4106 (substring (match-string 1) 0 10)
4107 (current-buffer) (match-beginning 0))))
4108 (or (memq day dates) (push day dates)))
4109 (unless no-ranges
4110 (goto-char beg)
4111 (while (re-search-forward org-tr-regexp end t)
4112 (setq pos (match-beginning 0))
4113 (setq ts1 (substring (match-string 1) 0 10)
4114 ts2 (substring (match-string 2) 0 10)
4115 day1 (time-to-days (org-time-string-to-time
4116 ts1 (current-buffer) pos))
4117 day2 (time-to-days (org-time-string-to-time
4118 ts2 (current-buffer) pos)))
4119 (while (< (setq day1 (1+ day1)) day2)
4120 (or (memq day1 dates) (push day1 dates)))))
4121 (setq dates (sort dates '<))
4122 (when empty
4123 (while (setq day (pop dates))
4124 (setq day2 (car dates))
4125 (push day dates1)
4126 (when (and day2 empty)
4127 (if (or (eq empty t)
4128 (and (numberp empty) (<= (- day2 day) empty)))
4129 (while (< (setq day (1+ day)) day2)
4130 (push (list day) dates1))
4131 (push (cons :omitted (- day2 day)) dates1))))
4132 (setq dates (nreverse dates1)))
4133 dates)))
4135 ;;; Agenda Daily/Weekly
4137 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4138 "Start day for the agenda view.
4139 Custom commands can set this variable in the options section.
4140 This is usually a string like \"2007-11-01\", \"+2d\" or any other
4141 input allowed when reading a date through the Org calendar.
4142 See the docstring of `org-read-date' for details.")
4143 (defvar org-starting-day nil) ; local variable in the agenda buffer
4144 (defvar org-arg-loc nil) ; local variable
4146 (defvar org-agenda-buffer-tmp-name nil)
4147 ;;;###autoload
4148 (defun org-agenda-list (&optional arg start-day span with-hour)
4149 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4150 The view will be for the current day or week, but from the overview buffer
4151 you will be able to go to other days/weeks.
4153 With a numeric prefix argument in an interactive call, the agenda will
4154 span ARG days. Lisp programs should instead specify SPAN to change
4155 the number of days. SPAN defaults to `org-agenda-span'.
4157 START-DAY defaults to TODAY, or to the most recent match for the weekday
4158 given in `org-agenda-start-on-weekday'.
4160 When WITH-HOUR is non-nil, only include scheduled and deadline
4161 items if they have an hour specification like [h]h:mm."
4162 (interactive "P")
4163 (if org-agenda-overriding-arguments
4164 (setq arg (car org-agenda-overriding-arguments)
4165 start-day (nth 1 org-agenda-overriding-arguments)
4166 span (nth 2 org-agenda-overriding-arguments)))
4167 (if (and (integerp arg) (> arg 0))
4168 (setq span arg arg nil))
4169 (catch 'exit
4170 (setq org-agenda-buffer-name
4171 (or org-agenda-buffer-tmp-name
4172 (if org-agenda-sticky
4173 (cond ((and org-keys (stringp org-match))
4174 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4175 (org-keys
4176 (format "*Org Agenda(%s)*" org-keys))
4177 (t "*Org Agenda(a)*")))
4178 org-agenda-buffer-name))
4179 (org-agenda-prepare "Day/Week")
4180 (setq start-day (or start-day org-agenda-start-day))
4181 (if (stringp start-day)
4182 ;; Convert to an absolute day number
4183 (setq start-day (time-to-days (org-read-date nil t start-day))))
4184 (org-compile-prefix-format 'agenda)
4185 (org-set-sorting-strategy 'agenda)
4186 (let* ((span (org-agenda-ndays-to-span
4187 (or span org-agenda-ndays org-agenda-span)))
4188 (today (org-today))
4189 (sd (or start-day today))
4190 (ndays (org-agenda-span-to-ndays span sd))
4191 (org-agenda-start-on-weekday
4192 (if (or (eq ndays 7) (eq ndays 14))
4193 org-agenda-start-on-weekday))
4194 (thefiles (org-agenda-files nil 'ifmode))
4195 (files thefiles)
4196 (start (if (or (null org-agenda-start-on-weekday)
4197 (< ndays 7))
4199 (let* ((nt (calendar-day-of-week
4200 (calendar-gregorian-from-absolute sd)))
4201 (n1 org-agenda-start-on-weekday)
4202 (d (- nt n1)))
4203 (- sd (+ (if (< d 0) 7 0) d)))))
4204 (day-numbers (list start))
4205 (day-cnt 0)
4206 (inhibit-redisplay (not debug-on-error))
4207 (org-agenda-show-log-scoped org-agenda-show-log)
4208 s e rtn rtnall file date d start-pos end-pos todayp
4209 clocktable-start clocktable-end filter)
4210 (setq org-agenda-redo-command
4211 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4212 (dotimes (n (1- ndays))
4213 (push (1+ (car day-numbers)) day-numbers))
4214 (setq day-numbers (nreverse day-numbers))
4215 (setq clocktable-start (car day-numbers)
4216 clocktable-end (1+ (or (org-last day-numbers) 0)))
4217 (org-set-local 'org-starting-day (car day-numbers))
4218 (org-set-local 'org-arg-loc arg)
4219 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
4220 (unless org-agenda-compact-blocks
4221 (let* ((d1 (car day-numbers))
4222 (d2 (org-last day-numbers))
4223 (w1 (org-days-to-iso-week d1))
4224 (w2 (org-days-to-iso-week d2)))
4225 (setq s (point))
4226 (if org-agenda-overriding-header
4227 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4228 nil 'face 'org-agenda-structure) "\n")
4229 (insert (org-agenda-span-name span)
4230 "-agenda"
4231 (if (< (- d2 d1) 350)
4232 (if (= w1 w2)
4233 (format " (W%02d)" w1)
4234 (format " (W%02d-W%02d)" w1 w2))
4236 ":\n")))
4237 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4238 'org-date-line t))
4239 (org-agenda-mark-header-line s))
4240 (while (setq d (pop day-numbers))
4241 (setq date (calendar-gregorian-from-absolute d)
4242 s (point))
4243 (if (or (setq todayp (= d today))
4244 (and (not start-pos) (= d sd)))
4245 (setq start-pos (point))
4246 (if (and start-pos (not end-pos))
4247 (setq end-pos (point))))
4248 (setq files thefiles
4249 rtnall nil)
4250 (while (setq file (pop files))
4251 (catch 'nextfile
4252 (org-check-agenda-file file)
4253 (let ((org-agenda-entry-types org-agenda-entry-types))
4254 ;; Starred types override non-starred equivalents
4255 (when (member :deadline* org-agenda-entry-types)
4256 (setq org-agenda-entry-types
4257 (delq :deadline org-agenda-entry-types)))
4258 (when (member :scheduled* org-agenda-entry-types)
4259 (setq org-agenda-entry-types
4260 (delq :scheduled org-agenda-entry-types)))
4261 ;; Honor with-hour
4262 (when with-hour
4263 (when (member :deadline org-agenda-entry-types)
4264 (setq org-agenda-entry-types
4265 (delq :deadline org-agenda-entry-types))
4266 (push :deadline* org-agenda-entry-types))
4267 (when (member :scheduled org-agenda-entry-types)
4268 (setq org-agenda-entry-types
4269 (delq :scheduled org-agenda-entry-types))
4270 (push :scheduled* org-agenda-entry-types)))
4271 (unless org-agenda-include-deadlines
4272 (setq org-agenda-entry-types
4273 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4274 (cond
4275 ((memq org-agenda-show-log-scoped '(only clockcheck))
4276 (setq rtn (org-agenda-get-day-entries
4277 file date :closed)))
4278 (org-agenda-show-log-scoped
4279 (setq rtn (apply 'org-agenda-get-day-entries
4280 file date
4281 (append '(:closed) org-agenda-entry-types))))
4283 (setq rtn (apply 'org-agenda-get-day-entries
4284 file date
4285 org-agenda-entry-types)))))
4286 (setq rtnall (append rtnall rtn)))) ;; all entries
4287 (if org-agenda-include-diary
4288 (let ((org-agenda-search-headline-for-time t))
4289 (require 'diary-lib)
4290 (setq rtn (org-get-entries-from-diary date))
4291 (setq rtnall (append rtnall rtn))))
4292 (if (or rtnall org-agenda-show-all-dates)
4293 (progn
4294 (setq day-cnt (1+ day-cnt))
4295 (insert
4296 (if (stringp org-agenda-format-date)
4297 (format-time-string org-agenda-format-date
4298 (org-time-from-absolute date))
4299 (funcall org-agenda-format-date date))
4300 "\n")
4301 (put-text-property s (1- (point)) 'face
4302 (org-agenda-get-day-face date))
4303 (put-text-property s (1- (point)) 'org-date-line t)
4304 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4305 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4306 (when todayp
4307 (put-text-property s (1- (point)) 'org-today t))
4308 (setq rtnall
4309 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4310 (if rtnall (insert ;; all entries
4311 (org-agenda-finalize-entries rtnall 'agenda)
4312 "\n"))
4313 (put-text-property s (1- (point)) 'day d)
4314 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4315 (when (and org-agenda-clockreport-mode clocktable-start)
4316 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4317 ;; the above line is to ensure the restricted range!
4318 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4319 tbl)
4320 (setq p (org-plist-delete p :block))
4321 (setq p (plist-put p :tstart clocktable-start))
4322 (setq p (plist-put p :tend clocktable-end))
4323 (setq p (plist-put p :scope 'agenda))
4324 (when (and (eq org-agenda-clockreport-mode 'with-filter)
4325 (setq filter (or org-agenda-tag-filter-while-redo
4326 (get 'org-agenda-tag-filter :preset-filter))))
4327 (setq p (plist-put p :tags (mapconcat (lambda (x)
4328 (if (string-match "[<>=]" x)
4331 filter ""))))
4332 (setq tbl (apply 'org-clock-get-clocktable p))
4333 (insert tbl)))
4334 (goto-char (point-min))
4335 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4336 (unless (and (pos-visible-in-window-p (point-min))
4337 (pos-visible-in-window-p (point-max)))
4338 (goto-char (1- (point-max)))
4339 (recenter -1)
4340 (if (not (pos-visible-in-window-p (or start-pos 1)))
4341 (progn
4342 (goto-char (or start-pos 1))
4343 (recenter 1))))
4344 (goto-char (or start-pos 1))
4345 (add-text-properties (point-min) (point-max)
4346 `(org-agenda-type agenda
4347 org-last-args (,arg ,start-day ,span)
4348 org-redo-cmd ,org-agenda-redo-command
4349 org-series-cmd ,org-cmd))
4350 (if (eq org-agenda-show-log-scoped 'clockcheck)
4351 (org-agenda-show-clocking-issues))
4352 (org-agenda-finalize)
4353 (setq buffer-read-only t)
4354 (message ""))))
4356 (defun org-agenda-ndays-to-span (n)
4357 "Return a span symbol for a span of N days, or N if none matches."
4358 (cond ((symbolp n) n)
4359 ((= n 1) 'day)
4360 ((= n 7) 'week)
4361 ((= n 14) 'fortnight)
4362 (t n)))
4364 (defun org-agenda-span-to-ndays (span &optional start-day)
4365 "Return ndays from SPAN, possibly starting at START-DAY.
4366 START-DAY is an absolute time value."
4367 (cond ((numberp span) span)
4368 ((eq span 'day) 1)
4369 ((eq span 'week) 7)
4370 ((eq span 'fortnight) 14)
4371 ((eq span 'month)
4372 (let ((date (calendar-gregorian-from-absolute start-day)))
4373 (calendar-last-day-of-month (car date) (caddr date))))
4374 ((eq span 'year)
4375 (let ((date (calendar-gregorian-from-absolute start-day)))
4376 (if (calendar-leap-year-p (caddr date)) 366 365)))))
4378 (defun org-agenda-span-name (span)
4379 "Return a SPAN name."
4380 (if (null span)
4382 (if (symbolp span)
4383 (capitalize (symbol-name span))
4384 (format "%d days" span))))
4386 ;;; Agenda word search
4388 (defvar org-agenda-search-history nil)
4390 (defvar org-search-syntax-table nil
4391 "Special syntax table for org-mode search.
4392 In this table, we have single quotes not as word constituents, to
4393 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4395 (defvar org-mode-syntax-table) ; From org.el
4396 (defun org-search-syntax-table ()
4397 (unless org-search-syntax-table
4398 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4399 (modify-syntax-entry ?' "." org-search-syntax-table)
4400 (modify-syntax-entry ?` "." org-search-syntax-table))
4401 org-search-syntax-table)
4403 (defvar org-agenda-last-search-view-search-was-boolean nil)
4405 ;;;###autoload
4406 (defun org-search-view (&optional todo-only string edit-at)
4407 "Show all entries that contain a phrase or words or regular expressions.
4409 With optional prefix argument TODO-ONLY, only consider entries that are
4410 TODO entries. The argument STRING can be used to pass a default search
4411 string into this function. If EDIT-AT is non-nil, it means that the
4412 user should get a chance to edit this string, with cursor at position
4413 EDIT-AT.
4415 The search string can be viewed either as a phrase that should be found as
4416 is, or it can be broken into a number of snippets, each of which must match
4417 in a Boolean way to select an entry. The default depends on the variable
4418 `org-agenda-search-view-always-boolean'.
4419 Even if this is turned off (the default) you can always switch to
4420 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4422 The default is a direct search of the whole phrase, where each space in
4423 the search string can expand to an arbitrary amount of whitespace,
4424 including newlines.
4426 If using a Boolean search, the search string is split on whitespace and
4427 each snippet is searched separately, with logical AND to select an entry.
4428 Words prefixed with a minus must *not* occur in the entry. Words without
4429 a prefix or prefixed with a plus must occur in the entry. Matching is
4430 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4431 match whole words, not parts of a word) if
4432 `org-agenda-search-view-force-full-words' is set (default is nil).
4434 Boolean search snippets enclosed by curly braces are interpreted as
4435 regular expressions that must or (when preceded with \"-\") must not
4436 match in the entry. Snippets enclosed into double quotes will be taken
4437 as a whole, to include whitespace.
4439 - If the search string starts with an asterisk, search only in headlines.
4440 - If (possibly after the leading star) the search string starts with an
4441 exclamation mark, this also means to look at TODO entries only, an effect
4442 that can also be achieved with a prefix argument.
4443 - If (possibly after star and exclamation mark) the search string starts
4444 with a colon, this will mean that the (non-regexp) snippets of the
4445 Boolean search must match as full words.
4447 This command searches the agenda files, and in addition the files listed
4448 in `org-agenda-text-search-extra-files'."
4449 (interactive "P")
4450 (if org-agenda-overriding-arguments
4451 (setq todo-only (car org-agenda-overriding-arguments)
4452 string (nth 1 org-agenda-overriding-arguments)
4453 edit-at (nth 2 org-agenda-overriding-arguments)))
4454 (let* ((props (list 'face nil
4455 'done-face 'org-agenda-done
4456 'org-not-done-regexp org-not-done-regexp
4457 'org-todo-regexp org-todo-regexp
4458 'org-complex-heading-regexp org-complex-heading-regexp
4459 'mouse-face 'highlight
4460 'help-echo (format "mouse-2 or RET jump to location")))
4461 (full-words org-agenda-search-view-force-full-words)
4462 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4463 regexp rtn rtnall files file pos inherited-tags
4464 marker category category-pos level tags c neg re boolean
4465 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4466 (unless (and (not edit-at)
4467 (stringp string)
4468 (string-match "\\S-" string))
4469 (setq string (read-string
4470 (if org-agenda-search-view-always-boolean
4471 "[+-]Word/{Regexp} ...: "
4472 "Phrase or [+-]Word/{Regexp} ...: ")
4473 (cond
4474 ((integerp edit-at) (cons string edit-at))
4475 (edit-at string))
4476 'org-agenda-search-history)))
4477 (catch 'exit
4478 (if org-agenda-sticky
4479 (setq org-agenda-buffer-name
4480 (if (stringp string)
4481 (format "*Org Agenda(%s:%s)*"
4482 (or org-keys (or (and todo-only "S") "s")) string)
4483 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4484 (org-agenda-prepare "SEARCH")
4485 (org-compile-prefix-format 'search)
4486 (org-set-sorting-strategy 'search)
4487 (setq org-agenda-redo-command
4488 (list 'org-search-view (if todo-only t nil)
4489 (list 'if 'current-prefix-arg nil string)))
4490 (setq org-agenda-query-string string)
4491 (if (equal (string-to-char string) ?*)
4492 (setq hdl-only t
4493 words (substring string 1))
4494 (setq words string))
4495 (when (equal (string-to-char words) ?!)
4496 (setq todo-only t
4497 words (substring words 1)))
4498 (when (equal (string-to-char words) ?:)
4499 (setq full-words t
4500 words (substring words 1)))
4501 (if (or org-agenda-search-view-always-boolean
4502 (member (string-to-char words) '(?- ?+ ?\{)))
4503 (setq boolean t))
4504 (setq words (org-split-string words))
4505 (let (www w)
4506 (while (setq w (pop words))
4507 (while (and (string-match "\\\\\\'" w) words)
4508 (setq w (concat (substring w 0 -1) " " (pop words))))
4509 (push w www))
4510 (setq words (nreverse www) www nil)
4511 (while (setq w (pop words))
4512 (when (and (string-match "\\`[-+]?{" w)
4513 (not (string-match "}\\'" w)))
4514 (while (and words (not (string-match "}\\'" (car words))))
4515 (setq w (concat w " " (pop words))))
4516 (setq w (concat w " " (pop words))))
4517 (push w www))
4518 (setq words (nreverse www)))
4519 (setq org-agenda-last-search-view-search-was-boolean boolean)
4520 (when boolean
4521 (let (wds w)
4522 (while (setq w (pop words))
4523 (if (or (equal (substring w 0 1) "\"")
4524 (and (> (length w) 1)
4525 (member (substring w 0 1) '("+" "-"))
4526 (equal (substring w 1 2) "\"")))
4527 (while (and words (not (equal (substring w -1) "\"")))
4528 (setq w (concat w " " (pop words)))))
4529 (and (string-match "\\`\\([-+]?\\)\"" w)
4530 (setq w (replace-match "\\1" nil nil w)))
4531 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4532 (push w wds))
4533 (setq words (nreverse wds))))
4534 (if boolean
4535 (mapc (lambda (w)
4536 (setq c (string-to-char w))
4537 (if (equal c ?-)
4538 (setq neg t w (substring w 1))
4539 (if (equal c ?+)
4540 (setq neg nil w (substring w 1))
4541 (setq neg nil)))
4542 (if (string-match "\\`{.*}\\'" w)
4543 (setq re (substring w 1 -1))
4544 (if full-words
4545 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4546 (setq re (regexp-quote (downcase w)))))
4547 (if neg (push re regexps-) (push re regexps+)))
4548 words)
4549 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4550 regexps+))
4551 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4552 (if (not regexps+)
4553 (setq regexp org-outline-regexp-bol)
4554 (setq regexp (pop regexps+))
4555 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4556 regexp))))
4557 (setq files (org-agenda-files nil 'ifmode))
4558 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4559 (pop org-agenda-text-search-extra-files)
4560 (setq files (org-add-archive-files files)))
4561 (setq files (append files org-agenda-text-search-extra-files)
4562 rtnall nil)
4563 (while (setq file (pop files))
4564 (setq ee nil)
4565 (catch 'nextfile
4566 (org-check-agenda-file file)
4567 (setq buffer (if (file-exists-p file)
4568 (org-get-agenda-file-buffer file)
4569 (error "No such file %s" file)))
4570 (if (not buffer)
4571 ;; If file does not exist, make sure an error message is sent
4572 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4573 file))))
4574 (with-current-buffer buffer
4575 (with-syntax-table (org-search-syntax-table)
4576 (unless (derived-mode-p 'org-mode)
4577 (error "Agenda file %s is not in `org-mode'" file))
4578 (let ((case-fold-search t))
4579 (save-excursion
4580 (save-restriction
4581 (if (eq buffer org-agenda-restrict)
4582 (narrow-to-region org-agenda-restrict-begin
4583 org-agenda-restrict-end)
4584 (widen))
4585 (goto-char (point-min))
4586 (unless (or (org-at-heading-p)
4587 (outline-next-heading))
4588 (throw 'nextfile t))
4589 (goto-char (max (point-min) (1- (point))))
4590 (while (re-search-forward regexp nil t)
4591 (org-back-to-heading t)
4592 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4593 (> (org-reduced-level (org-outline-level))
4594 org-agenda-search-view-max-outline-level)
4595 (forward-line -1)
4596 (outline-back-to-heading t)))
4597 (skip-chars-forward "* ")
4598 (setq beg (point-at-bol)
4599 beg1 (point)
4600 end (progn
4601 (outline-next-heading)
4602 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4603 (> (org-reduced-level (org-outline-level))
4604 org-agenda-search-view-max-outline-level)
4605 (forward-line 1)
4606 (outline-next-heading)))
4607 (point)))
4609 (catch :skip
4610 (goto-char beg)
4611 (org-agenda-skip)
4612 (setq str (buffer-substring-no-properties
4613 (point-at-bol)
4614 (if hdl-only (point-at-eol) end)))
4615 (mapc (lambda (wr) (when (string-match wr str)
4616 (goto-char (1- end))
4617 (throw :skip t)))
4618 regexps-)
4619 (mapc (lambda (wr) (unless (string-match wr str)
4620 (goto-char (1- end))
4621 (throw :skip t)))
4622 (if todo-only
4623 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
4624 regexps+)
4625 regexps+))
4626 (goto-char beg)
4627 (setq marker (org-agenda-new-marker (point))
4628 category (org-get-category)
4629 level (make-string (org-reduced-level (org-outline-level)) ? )
4630 category-pos (get-text-property (point) 'org-category-position)
4631 inherited-tags
4632 (or (eq org-agenda-show-inherited-tags 'always)
4633 (and (listp org-agenda-show-inherited-tags)
4634 (memq 'todo org-agenda-show-inherited-tags))
4635 (and (eq org-agenda-show-inherited-tags t)
4636 (or (eq org-agenda-use-tag-inheritance t)
4637 (memq 'todo org-agenda-use-tag-inheritance))))
4638 tags (org-get-tags-at nil (not inherited-tags))
4639 txt (org-agenda-format-item
4641 (buffer-substring-no-properties
4642 beg1 (point-at-eol))
4643 level category tags t))
4644 (org-add-props txt props
4645 'org-marker marker 'org-hd-marker marker
4646 'org-todo-regexp org-todo-regexp
4647 'level level
4648 'org-complex-heading-regexp org-complex-heading-regexp
4649 'priority 1000 'org-category category
4650 'org-category-position category-pos
4651 'type "search")
4652 (push txt ee)
4653 (goto-char (1- end))))))))))
4654 (setq rtn (nreverse ee))
4655 (setq rtnall (append rtnall rtn)))
4656 (if org-agenda-overriding-header
4657 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4658 nil 'face 'org-agenda-structure) "\n")
4659 (insert "Search words: ")
4660 (add-text-properties (point-min) (1- (point))
4661 (list 'face 'org-agenda-structure))
4662 (setq pos (point))
4663 (insert string "\n")
4664 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4665 (setq pos (point))
4666 (unless org-agenda-multi
4667 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
4668 (add-text-properties pos (1- (point))
4669 (list 'face 'org-agenda-structure))))
4670 (org-agenda-mark-header-line (point-min))
4671 (when rtnall
4672 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4673 (goto-char (point-min))
4674 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4675 (add-text-properties (point-min) (point-max)
4676 `(org-agenda-type search
4677 org-last-args (,todo-only ,string ,edit-at)
4678 org-redo-cmd ,org-agenda-redo-command
4679 org-series-cmd ,org-cmd))
4680 (org-agenda-finalize)
4681 (setq buffer-read-only t))))
4683 ;;; Agenda TODO list
4685 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4686 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4687 (concat
4688 (if (or (equal keywords "ALL") (not keywords))
4689 (propertize "ALL" 'face 'warning)
4690 (mapconcat
4691 (lambda (kw)
4692 (propertize kw 'face (org-get-todo-face kw)))
4693 (org-split-string keywords "|")
4694 "|"))
4695 "\n"))
4697 (defvar org-select-this-todo-keyword nil)
4698 (defvar org-last-arg nil)
4700 ;;;###autoload
4701 (defun org-todo-list (&optional arg)
4702 "Show all (not done) TODO entries from all agenda file in a single list.
4703 The prefix arg can be used to select a specific TODO keyword and limit
4704 the list to these. When using \\[universal-argument], you will be prompted
4705 for a keyword. A numeric prefix directly selects the Nth keyword in
4706 `org-todo-keywords-1'."
4707 (interactive "P")
4708 (if org-agenda-overriding-arguments
4709 (setq arg org-agenda-overriding-arguments))
4710 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4711 (let* ((today (org-today))
4712 (date (calendar-gregorian-from-absolute today))
4713 (kwds org-todo-keywords-for-agenda)
4714 (completion-ignore-case t)
4715 (org-select-this-todo-keyword
4716 (if (stringp arg) arg
4717 (and arg (integerp arg) (> arg 0)
4718 (nth (1- arg) kwds))))
4719 rtn rtnall files file pos)
4720 (when (equal arg '(4))
4721 (setq org-select-this-todo-keyword
4722 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4723 (mapcar 'list kwds) nil nil)))
4724 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4725 (catch 'exit
4726 (if org-agenda-sticky
4727 (setq org-agenda-buffer-name
4728 (if (stringp org-select-this-todo-keyword)
4729 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4730 org-select-this-todo-keyword)
4731 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4732 (org-agenda-prepare "TODO")
4733 (org-compile-prefix-format 'todo)
4734 (org-set-sorting-strategy 'todo)
4735 (setq org-agenda-redo-command
4736 `(org-todo-list (or (and (numberp current-prefix-arg)
4737 current-prefix-arg)
4738 ,org-select-this-todo-keyword
4739 current-prefix-arg ,arg)))
4740 (setq files (org-agenda-files nil 'ifmode)
4741 rtnall nil)
4742 (while (setq file (pop files))
4743 (catch 'nextfile
4744 (org-check-agenda-file file)
4745 (setq rtn (org-agenda-get-day-entries file date :todo))
4746 (setq rtnall (append rtnall rtn))))
4747 (if org-agenda-overriding-header
4748 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4749 nil 'face 'org-agenda-structure) "\n")
4750 (insert "Global list of TODO items of type: ")
4751 (add-text-properties (point-min) (1- (point))
4752 (list 'face 'org-agenda-structure
4753 'short-heading
4754 (concat "ToDo: "
4755 (or org-select-this-todo-keyword "ALL"))))
4756 (org-agenda-mark-header-line (point-min))
4757 (insert (org-agenda-propertize-selected-todo-keywords
4758 org-select-this-todo-keyword))
4759 (setq pos (point))
4760 (unless org-agenda-multi
4761 (insert "Available with `N r': (0)[ALL]")
4762 (let ((n 0) s)
4763 (mapc (lambda (x)
4764 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4765 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4766 (insert "\n "))
4767 (insert " " s))
4768 kwds))
4769 (insert "\n"))
4770 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4771 (org-agenda-mark-header-line (point-min))
4772 (when rtnall
4773 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4774 (goto-char (point-min))
4775 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4776 (add-text-properties (point-min) (point-max)
4777 `(org-agenda-type todo
4778 org-last-args ,arg
4779 org-redo-cmd ,org-agenda-redo-command
4780 org-series-cmd ,org-cmd))
4781 (org-agenda-finalize)
4782 (setq buffer-read-only t))))
4784 ;;; Agenda tags match
4786 ;;;###autoload
4787 (defun org-tags-view (&optional todo-only match)
4788 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4789 The prefix arg TODO-ONLY limits the search to TODO entries."
4790 (interactive "P")
4791 (if org-agenda-overriding-arguments
4792 (setq todo-only (car org-agenda-overriding-arguments)
4793 match (nth 1 org-agenda-overriding-arguments)))
4794 (let* ((org-tags-match-list-sublevels
4795 org-tags-match-list-sublevels)
4796 (completion-ignore-case t)
4797 rtn rtnall files file pos matcher
4798 buffer)
4799 (when (and (stringp match) (not (string-match "\\S-" match)))
4800 (setq match nil))
4801 (catch 'exit
4802 (if org-agenda-sticky
4803 (setq org-agenda-buffer-name
4804 (if (stringp match)
4805 (format "*Org Agenda(%s:%s)*"
4806 (or org-keys (or (and todo-only "M") "m")) match)
4807 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4808 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4809 ;; expanding tags within `org-make-tags-matcher'
4810 (org-agenda-prepare (concat "TAGS " match))
4811 (setq matcher (org-make-tags-matcher match)
4812 match (car matcher) matcher (cdr matcher))
4813 (org-compile-prefix-format 'tags)
4814 (org-set-sorting-strategy 'tags)
4815 (setq org-agenda-query-string match)
4816 (setq org-agenda-redo-command
4817 (list 'org-tags-view `(quote ,todo-only)
4818 (list 'if 'current-prefix-arg nil `(quote ,org-agenda-query-string))))
4819 (setq files (org-agenda-files nil 'ifmode)
4820 rtnall nil)
4821 (while (setq file (pop files))
4822 (catch 'nextfile
4823 (org-check-agenda-file file)
4824 (setq buffer (if (file-exists-p file)
4825 (org-get-agenda-file-buffer file)
4826 (error "No such file %s" file)))
4827 (if (not buffer)
4828 ;; If file does not exist, error message to agenda
4829 (setq rtn (list
4830 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4831 rtnall (append rtnall rtn))
4832 (with-current-buffer buffer
4833 (unless (derived-mode-p 'org-mode)
4834 (error "Agenda file %s is not in `org-mode'" file))
4835 (save-excursion
4836 (save-restriction
4837 (if (eq buffer org-agenda-restrict)
4838 (narrow-to-region org-agenda-restrict-begin
4839 org-agenda-restrict-end)
4840 (widen))
4841 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4842 (setq rtnall (append rtnall rtn))))))))
4843 (if org-agenda-overriding-header
4844 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4845 nil 'face 'org-agenda-structure) "\n")
4846 (insert "Headlines with TAGS match: ")
4847 (add-text-properties (point-min) (1- (point))
4848 (list 'face 'org-agenda-structure
4849 'short-heading
4850 (concat "Match: " match)))
4851 (setq pos (point))
4852 (insert match "\n")
4853 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4854 (setq pos (point))
4855 (unless org-agenda-multi
4856 (insert "Press `C-u r' to search again with new search string\n"))
4857 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4858 (org-agenda-mark-header-line (point-min))
4859 (when rtnall
4860 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4861 (goto-char (point-min))
4862 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4863 (add-text-properties (point-min) (point-max)
4864 `(org-agenda-type tags
4865 org-last-args (,todo-only ,match)
4866 org-redo-cmd ,org-agenda-redo-command
4867 org-series-cmd ,org-cmd))
4868 (org-agenda-finalize)
4869 (setq buffer-read-only t))))
4871 ;;; Agenda Finding stuck projects
4873 (defvar org-agenda-skip-regexp nil
4874 "Regular expression used in skipping subtrees for the agenda.
4875 This is basically a temporary global variable that can be set and then
4876 used by user-defined selections using `org-agenda-skip-function'.")
4878 (defvar org-agenda-overriding-header nil
4879 "When set during agenda, todo and tags searches it replaces the header.
4880 This variable should not be set directly, but custom commands can bind it
4881 in the options section.")
4883 (defun org-agenda-skip-entry-when-regexp-matches ()
4884 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4885 If yes, it returns the end position of this entry, causing agenda commands
4886 to skip the entry but continuing the search in the subtree. This is a
4887 function that can be put into `org-agenda-skip-function' for the duration
4888 of a command."
4889 (let ((end (save-excursion (org-end-of-subtree t)))
4890 skip)
4891 (save-excursion
4892 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4893 (and skip end)))
4895 (defun org-agenda-skip-subtree-when-regexp-matches ()
4896 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4897 If yes, it returns the end position of this tree, causing agenda commands
4898 to skip this subtree. This is a function that can be put into
4899 `org-agenda-skip-function' for the duration of a command."
4900 (let ((end (save-excursion (org-end-of-subtree t)))
4901 skip)
4902 (save-excursion
4903 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4904 (and skip end)))
4906 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4907 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4908 If yes, it returns the end position of the current entry (NOT the tree),
4909 causing agenda commands to skip the entry but continuing the search in
4910 the subtree. This is a function that can be put into
4911 `org-agenda-skip-function' for the duration of a command. An important
4912 use of this function is for the stuck project list."
4913 (let ((end (save-excursion (org-end-of-subtree t)))
4914 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4915 skip)
4916 (save-excursion
4917 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4918 (and skip entry-end)))
4920 (defun org-agenda-skip-entry-if (&rest conditions)
4921 "Skip entry if any of CONDITIONS is true.
4922 See `org-agenda-skip-if' for details."
4923 (org-agenda-skip-if nil conditions))
4925 (defun org-agenda-skip-subtree-if (&rest conditions)
4926 "Skip subtree if any of CONDITIONS is true.
4927 See `org-agenda-skip-if' for details."
4928 (org-agenda-skip-if t conditions))
4930 (defun org-agenda-skip-if (subtree conditions)
4931 "Checks current entity for CONDITIONS.
4932 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4933 the entry (i.e. the text before the next heading) is checked.
4935 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4936 from different tests. Valid conditions are:
4938 scheduled Check if there is a scheduled cookie
4939 notscheduled Check if there is no scheduled cookie
4940 deadline Check if there is a deadline
4941 notdeadline Check if there is no deadline
4942 timestamp Check if there is a timestamp (also deadline or scheduled)
4943 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4944 regexp Check if regexp matches
4945 notregexp Check if regexp does not match.
4946 todo Check if TODO keyword matches
4947 nottodo Check if TODO keyword does not match
4949 The regexp is taken from the conditions list, it must come right after
4950 the `regexp' or `notregexp' element.
4952 `todo' and `nottodo' accept as an argument a list of todo
4953 keywords, which may include \"*\" to match any todo keyword.
4955 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4957 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4959 Instead of a list, a keyword class may be given. For example:
4961 (org-agenda-skip-entry-if 'nottodo 'done)
4963 would skip entries that haven't been marked with any of \"DONE\"
4964 keywords. Possible classes are: `todo', `done', `any'.
4966 If any of these conditions is met, this function returns the end point of
4967 the entity, causing the search to continue from there. This is a function
4968 that can be put into `org-agenda-skip-function' for the duration of a command."
4969 (let (beg end m)
4970 (org-back-to-heading t)
4971 (setq beg (point)
4972 end (if subtree
4973 (progn (org-end-of-subtree t) (point))
4974 (progn (outline-next-heading) (1- (point)))))
4975 (goto-char beg)
4976 (and
4978 (and (memq 'scheduled conditions)
4979 (re-search-forward org-scheduled-time-regexp end t))
4980 (and (memq 'notscheduled conditions)
4981 (not (re-search-forward org-scheduled-time-regexp end t)))
4982 (and (memq 'deadline conditions)
4983 (re-search-forward org-deadline-time-regexp end t))
4984 (and (memq 'notdeadline conditions)
4985 (not (re-search-forward org-deadline-time-regexp end t)))
4986 (and (memq 'timestamp conditions)
4987 (re-search-forward org-ts-regexp end t))
4988 (and (memq 'nottimestamp conditions)
4989 (not (re-search-forward org-ts-regexp end t)))
4990 (and (setq m (memq 'regexp conditions))
4991 (stringp (nth 1 m))
4992 (re-search-forward (nth 1 m) end t))
4993 (and (setq m (memq 'notregexp conditions))
4994 (stringp (nth 1 m))
4995 (not (re-search-forward (nth 1 m) end t)))
4996 (and (or
4997 (setq m (memq 'nottodo conditions))
4998 (setq m (memq 'todo-unblocked conditions))
4999 (setq m (memq 'nottodo-unblocked conditions))
5000 (setq m (memq 'todo conditions)))
5001 (org-agenda-skip-if-todo m end)))
5002 end)))
5004 (defun org-agenda-skip-if-todo (args end)
5005 "Helper function for `org-agenda-skip-if', do not use it directly.
5006 ARGS is a list with first element either `todo', `nottodo',
5007 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
5008 a list of TODO keywords, or a state symbol `todo' or `done' or
5009 `any'."
5010 (let ((kw (car args))
5011 (arg (cadr args))
5012 todo-wds todo-re)
5013 (setq todo-wds
5014 (org-uniquify
5015 (cond
5016 ((listp arg) ;; list of keywords
5017 (if (member "*" arg)
5018 (mapcar 'substring-no-properties org-todo-keywords-1)
5019 arg))
5020 ((symbolp arg) ;; keyword class name
5021 (cond
5022 ((eq arg 'todo)
5023 (org-delete-all org-done-keywords
5024 (mapcar 'substring-no-properties
5025 org-todo-keywords-1)))
5026 ((eq arg 'done) org-done-keywords)
5027 ((eq arg 'any)
5028 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
5029 (setq todo-re
5030 (concat "^\\*+[ \t]+\\<\\("
5031 (mapconcat 'identity todo-wds "\\|")
5032 "\\)\\>"))
5033 (cond
5034 ((eq kw 'todo) (re-search-forward todo-re end t))
5035 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
5036 ((eq kw 'todo-unblocked)
5037 (catch 'unblocked
5038 (while (re-search-forward todo-re end t)
5039 (or (org-entry-blocked-p) (throw 'unblocked t)))
5040 nil))
5041 ((eq kw 'nottodo-unblocked)
5042 (catch 'unblocked
5043 (while (re-search-forward todo-re end t)
5044 (or (org-entry-blocked-p) (throw 'unblocked nil)))
5048 ;;;###autoload
5049 (defun org-agenda-list-stuck-projects (&rest ignore)
5050 "Create agenda view for projects that are stuck.
5051 Stuck projects are project that have no next actions. For the definitions
5052 of what a project is and how to check if it stuck, customize the variable
5053 `org-stuck-projects'."
5054 (interactive)
5055 (let* ((org-agenda-skip-function
5056 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
5057 ;; We could have used org-agenda-skip-if here.
5058 (org-agenda-overriding-header
5059 (or org-agenda-overriding-header "List of stuck projects: "))
5060 (matcher (nth 0 org-stuck-projects))
5061 (todo (nth 1 org-stuck-projects))
5062 (todo-wds (if (member "*" todo)
5063 (progn
5064 (org-agenda-prepare-buffers (org-agenda-files
5065 nil 'ifmode))
5066 (org-delete-all
5067 org-done-keywords-for-agenda
5068 (copy-sequence org-todo-keywords-for-agenda)))
5069 todo))
5070 (todo-re (concat "^\\*+[ \t]+\\("
5071 (mapconcat 'identity todo-wds "\\|")
5072 "\\)\\>"))
5073 (tags (nth 2 org-stuck-projects))
5074 (tags-re (if (member "*" tags)
5075 (concat org-outline-regexp-bol
5076 (org-re ".*:[[:alnum:]_@#%]+:[ \t]*$"))
5077 (if tags
5078 (concat org-outline-regexp-bol
5079 ".*:\\("
5080 (mapconcat 'identity tags "\\|")
5081 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
5082 (gen-re (nth 3 org-stuck-projects))
5083 (re-list
5084 (delq nil
5085 (list
5086 (if todo todo-re)
5087 (if tags tags-re)
5088 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
5089 gen-re)))))
5090 (setq org-agenda-skip-regexp
5091 (if re-list
5092 (mapconcat 'identity re-list "\\|")
5093 (error "No information how to identify unstuck projects")))
5094 (org-tags-view nil matcher)
5095 (setq org-agenda-buffer-name (buffer-name))
5096 (with-current-buffer org-agenda-buffer-name
5097 (setq org-agenda-redo-command
5098 `(org-agenda-list-stuck-projects ,current-prefix-arg)))))
5100 ;;; Diary integration
5102 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
5103 (defvar diary-list-entries-hook)
5104 (defvar diary-time-regexp)
5105 (defun org-get-entries-from-diary (date)
5106 "Get the (Emacs Calendar) diary entries for DATE."
5107 (require 'diary-lib)
5108 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
5109 (diary-display-hook '(fancy-diary-display))
5110 (diary-display-function 'fancy-diary-display)
5111 (pop-up-frames nil)
5112 (diary-list-entries-hook
5113 (cons 'org-diary-default-entry diary-list-entries-hook))
5114 (diary-file-name-prefix nil) ; turn this feature off
5115 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
5116 entries
5117 (org-disable-agenda-to-diary t))
5118 (save-excursion
5119 (save-window-excursion
5120 (funcall (if (fboundp 'diary-list-entries)
5121 'diary-list-entries 'list-diary-entries)
5122 date 1)))
5123 (if (not (get-buffer diary-fancy-buffer))
5124 (setq entries nil)
5125 (with-current-buffer diary-fancy-buffer
5126 (setq buffer-read-only nil)
5127 (if (zerop (buffer-size))
5128 ;; No entries
5129 (setq entries nil)
5130 ;; Omit the date and other unnecessary stuff
5131 (org-agenda-cleanup-fancy-diary)
5132 ;; Add prefix to each line and extend the text properties
5133 (if (zerop (buffer-size))
5134 (setq entries nil)
5135 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
5136 (setq entries
5137 (with-temp-buffer
5138 (insert entries) (goto-char (point-min))
5139 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5140 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5141 (replace-match (concat "; " (match-string 1)))))
5142 (buffer-string)))))
5143 (set-buffer-modified-p nil)
5144 (kill-buffer diary-fancy-buffer)))
5145 (when entries
5146 (setq entries (org-split-string entries "\n"))
5147 (setq entries
5148 (mapcar
5149 (lambda (x)
5150 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5151 ;; Extend the text properties to the beginning of the line
5152 (org-add-props x (text-properties-at (1- (length x)) x)
5153 'type "diary" 'date date 'face 'org-agenda-diary))
5154 entries)))))
5156 (defvar org-agenda-cleanup-fancy-diary-hook nil
5157 "Hook run when the fancy diary buffer is cleaned up.")
5159 (defun org-agenda-cleanup-fancy-diary ()
5160 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5161 This gets rid of the date, the underline under the date, and
5162 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5163 date. It also removes lines that contain only whitespace."
5164 (goto-char (point-min))
5165 (if (looking-at ".*?:[ \t]*")
5166 (progn
5167 (replace-match "")
5168 (re-search-forward "\n=+$" nil t)
5169 (replace-match "")
5170 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5171 (re-search-forward "\n=+$" nil t)
5172 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5173 (goto-char (point-min))
5174 (while (re-search-forward "^ +\n" nil t)
5175 (replace-match ""))
5176 (goto-char (point-min))
5177 (if (re-search-forward "^Org-mode dummy\n?" nil t)
5178 (replace-match ""))
5179 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5181 ;; Make sure entries from the diary have the right text properties.
5182 (eval-after-load "diary-lib"
5183 '(if (boundp 'diary-modify-entry-list-string-function)
5184 ;; We can rely on the hook, nothing to do
5186 ;; Hook not available, must use advice to make this work
5187 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5188 "Make the position visible."
5189 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5190 (stringp string)
5191 buffer-file-name)
5192 (setq string (org-modify-diary-entry-string string))))))
5194 (defun org-modify-diary-entry-string (string)
5195 "Add text properties to string, allowing org-mode to act on it."
5196 (org-add-props string nil
5197 'mouse-face 'highlight
5198 'help-echo (if buffer-file-name
5199 (format "mouse-2 or RET jump to diary file %s"
5200 (abbreviate-file-name buffer-file-name))
5202 'org-agenda-diary-link t
5203 'org-marker (org-agenda-new-marker (point-at-bol))))
5205 (defun org-diary-default-entry ()
5206 "Add a dummy entry to the diary.
5207 Needed to avoid empty dates which mess up holiday display."
5208 ;; Catch the error if dealing with the new add-to-diary-alist
5209 (when org-disable-agenda-to-diary
5210 (condition-case nil
5211 (org-add-to-diary-list original-date "Org-mode dummy" "")
5212 (error
5213 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
5215 (defun org-add-to-diary-list (&rest args)
5216 (if (fboundp 'diary-add-to-list)
5217 (apply 'diary-add-to-list args)
5218 (apply 'add-to-diary-list args)))
5220 (defvar org-diary-last-run-time nil)
5222 ;;;###autoload
5223 (defun org-diary (&rest args)
5224 "Return diary information from org files.
5225 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5226 It accesses org files and extracts information from those files to be
5227 listed in the diary. The function accepts arguments specifying what
5228 items should be listed. For a list of arguments allowed here, see the
5229 variable `org-agenda-entry-types'.
5231 The call in the diary file should look like this:
5233 &%%(org-diary) ~/path/to/some/orgfile.org
5235 Use a separate line for each org file to check. Or, if you omit the file name,
5236 all files listed in `org-agenda-files' will be checked automatically:
5238 &%%(org-diary)
5240 If you don't give any arguments (as in the example above), the default value
5241 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5242 So the example above may also be written as
5244 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5246 The function expects the lisp variables `entry' and `date' to be provided
5247 by the caller, because this is how the calendar works. Don't use this
5248 function from a program - use `org-agenda-get-day-entries' instead."
5249 (when (> (- (org-float-time)
5250 org-agenda-last-marker-time)
5252 ;; I am not sure if this works with sticky agendas, because the marker
5253 ;; list is then no longer a global variable.
5254 (org-agenda-reset-markers))
5255 (org-compile-prefix-format 'agenda)
5256 (org-set-sorting-strategy 'agenda)
5257 (setq args (or args org-agenda-entry-types))
5258 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5259 (list entry)
5260 (org-agenda-files t)))
5261 (time (org-float-time))
5262 file rtn results)
5263 (when (or (not org-diary-last-run-time)
5264 (> (- time
5265 org-diary-last-run-time)
5267 (org-agenda-prepare-buffers files))
5268 (setq org-diary-last-run-time time)
5269 ;; If this is called during org-agenda, don't return any entries to
5270 ;; the calendar. Org Agenda will list these entries itself.
5271 (if org-disable-agenda-to-diary (setq files nil))
5272 (while (setq file (pop files))
5273 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5274 (setq results (append results rtn)))
5275 (when results
5276 (setq results
5277 (mapcar (lambda (i) (replace-regexp-in-string
5278 org-bracket-link-regexp "\\3" i)) results))
5279 (concat (org-agenda-finalize-entries results) "\n"))))
5281 ;;; Agenda entry finders
5283 (defun org-agenda-get-day-entries (file date &rest args)
5284 "Does the work for `org-diary' and `org-agenda'.
5285 FILE is the path to a file to be checked for entries. DATE is date like
5286 the one returned by `calendar-current-date'. ARGS are symbols indicating
5287 which kind of entries should be extracted. For details about these, see
5288 the documentation of `org-diary'."
5289 (setq args (or args org-agenda-entry-types))
5290 (let* ((org-startup-folded nil)
5291 (org-startup-align-all-tables nil)
5292 (buffer (if (file-exists-p file)
5293 (org-get-agenda-file-buffer file)
5294 (error "No such file %s" file)))
5295 arg results rtn deadline-results)
5296 (if (not buffer)
5297 ;; If file does not exist, make sure an error message ends up in diary
5298 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5299 (with-current-buffer buffer
5300 (unless (derived-mode-p 'org-mode)
5301 (error "Agenda file %s is not in `org-mode'" file))
5302 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5303 (let ((case-fold-search nil))
5304 (save-excursion
5305 (save-restriction
5306 (if (eq buffer org-agenda-restrict)
5307 (narrow-to-region org-agenda-restrict-begin
5308 org-agenda-restrict-end)
5309 (widen))
5310 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
5311 (while (setq arg (pop args))
5312 (cond
5313 ((and (eq arg :todo)
5314 (equal date (calendar-gregorian-from-absolute
5315 (org-today))))
5316 (setq rtn (org-agenda-get-todos))
5317 (setq results (append results rtn)))
5318 ((eq arg :timestamp)
5319 (setq rtn (org-agenda-get-blocks))
5320 (setq results (append results rtn))
5321 (setq rtn (org-agenda-get-timestamps deadline-results))
5322 (setq results (append results rtn)))
5323 ((eq arg :sexp)
5324 (setq rtn (org-agenda-get-sexps))
5325 (setq results (append results rtn)))
5326 ((eq arg :scheduled)
5327 (setq rtn (org-agenda-get-scheduled deadline-results))
5328 (setq results (append results rtn)))
5329 ((eq arg :scheduled*)
5330 (setq rtn (org-agenda-get-scheduled deadline-results t))
5331 (setq results (append results rtn)))
5332 ((eq arg :closed)
5333 (setq rtn (org-agenda-get-progress))
5334 (setq results (append results rtn)))
5335 ((eq arg :deadline)
5336 (setq rtn (org-agenda-get-deadlines))
5337 (setq deadline-results (copy-sequence rtn))
5338 (setq results (append results rtn)))
5339 ((eq arg :deadline*)
5340 (setq rtn (org-agenda-get-deadlines t))
5341 (setq deadline-results (copy-sequence rtn))
5342 (setq results (append results rtn))))))))
5343 results))))
5345 (defsubst org-em (x y list)
5346 "Is X or Y a member of LIST?"
5347 (or (memq x list) (memq y list)))
5349 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5350 (defvar org-agenda-sorting-strategy-selected nil)
5352 (defun org-agenda-get-todos ()
5353 "Return the TODO information for agenda display."
5354 (let* ((props (list 'face nil
5355 'done-face 'org-agenda-done
5356 'org-not-done-regexp org-not-done-regexp
5357 'org-todo-regexp org-todo-regexp
5358 'org-complex-heading-regexp org-complex-heading-regexp
5359 'mouse-face 'highlight
5360 'help-echo
5361 (format "mouse-2 or RET jump to org file %s"
5362 (abbreviate-file-name buffer-file-name))))
5363 (regexp (format org-heading-keyword-regexp-format
5364 (cond
5365 ((and org-select-this-todo-keyword
5366 (equal org-select-this-todo-keyword "*"))
5367 org-todo-regexp)
5368 (org-select-this-todo-keyword
5369 (concat "\\("
5370 (mapconcat 'identity
5371 (org-split-string
5372 org-select-this-todo-keyword
5373 "|")
5374 "\\|") "\\)"))
5375 (t org-not-done-regexp))))
5376 marker priority category category-pos level tags todo-state ts-date ts-date-type
5377 ee txt beg end inherited-tags todo-state-end-pos)
5378 (goto-char (point-min))
5379 (while (re-search-forward regexp nil t)
5380 (catch :skip
5381 (save-match-data
5382 (beginning-of-line)
5383 (org-agenda-skip)
5384 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5385 (unless (and (setq todo-state (org-get-todo-state))
5386 (setq todo-state-end-pos (match-end 2)))
5387 (goto-char end)
5388 (throw :skip nil))
5389 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5390 (goto-char (1+ beg))
5391 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5392 (throw :skip nil)))
5393 (goto-char (match-beginning 2))
5394 (setq marker (org-agenda-new-marker (match-beginning 0))
5395 category (org-get-category)
5396 ts-date (let (ts)
5397 (save-match-data
5398 (cond ((org-em 'scheduled-up 'scheduled-down
5399 org-agenda-sorting-strategy-selected)
5400 (setq ts (org-entry-get (point) "SCHEDULED")
5401 ts-date-type " scheduled"))
5402 ((org-em 'deadline-up 'deadline-down
5403 org-agenda-sorting-strategy-selected)
5404 (setq ts (org-entry-get (point) "DEADLINE")
5405 ts-date-type " deadline"))
5406 ((org-em 'ts-up 'ts-down
5407 org-agenda-sorting-strategy-selected)
5408 (setq ts (org-entry-get (point) "TIMESTAMP")
5409 ts-date-type " timestamp"))
5410 ((org-em 'tsia-up 'tsia-down
5411 org-agenda-sorting-strategy-selected)
5412 (setq ts (org-entry-get (point) "TIMESTAMP_IA")
5413 ts-date-type " timestamp_ia"))
5414 ((org-em 'timestamp-up 'timestamp-down
5415 org-agenda-sorting-strategy-selected)
5416 (setq ts (or (org-entry-get (point) "SCHEDULED")
5417 (org-entry-get (point) "DEADLINE")
5418 (org-entry-get (point) "TIMESTAMP")
5419 (org-entry-get (point) "TIMESTAMP_IA"))
5420 ts-date-type ""))
5421 (t (setq ts-date-type "")))
5422 (when ts (ignore-errors (org-time-string-to-absolute ts)))))
5423 category-pos (get-text-property (point) 'org-category-position)
5424 txt (org-trim
5425 (buffer-substring (match-beginning 2) (match-end 0)))
5426 inherited-tags
5427 (or (eq org-agenda-show-inherited-tags 'always)
5428 (and (listp org-agenda-show-inherited-tags)
5429 (memq 'todo org-agenda-show-inherited-tags))
5430 (and (eq org-agenda-show-inherited-tags t)
5431 (or (eq org-agenda-use-tag-inheritance t)
5432 (memq 'todo org-agenda-use-tag-inheritance))))
5433 tags (org-get-tags-at nil (not inherited-tags))
5434 level (make-string (org-reduced-level (org-outline-level)) ? )
5435 txt (org-agenda-format-item "" txt level category tags t)
5436 priority (1+ (org-get-priority txt)))
5437 (org-add-props txt props
5438 'org-marker marker 'org-hd-marker marker
5439 'priority priority 'org-category category
5440 'level level
5441 'ts-date ts-date
5442 'org-category-position category-pos
5443 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5444 (push txt ee)
5445 (if org-agenda-todo-list-sublevels
5446 (goto-char todo-state-end-pos)
5447 (org-end-of-subtree 'invisible))))
5448 (nreverse ee)))
5450 (defun org-agenda-todo-custom-ignore-p (time n)
5451 "Check whether timestamp is farther away than n number of days.
5452 This function is invoked if `org-agenda-todo-ignore-deadlines',
5453 `org-agenda-todo-ignore-scheduled' or
5454 `org-agenda-todo-ignore-timestamp' is set to an integer."
5455 (let ((days (org-time-stamp-to-now
5456 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5457 (if (>= n 0)
5458 (>= days n)
5459 (<= days n))))
5461 ;;;###autoload
5462 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5463 (&optional end)
5464 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5465 (when (or org-agenda-todo-ignore-with-date
5466 org-agenda-todo-ignore-scheduled
5467 org-agenda-todo-ignore-deadlines
5468 org-agenda-todo-ignore-timestamp)
5469 (setq end (or end (save-excursion (outline-next-heading) (point))))
5470 (save-excursion
5471 (or (and org-agenda-todo-ignore-with-date
5472 (re-search-forward org-ts-regexp end t))
5473 (and org-agenda-todo-ignore-scheduled
5474 (re-search-forward org-scheduled-time-regexp end t)
5475 (cond
5476 ((eq org-agenda-todo-ignore-scheduled 'future)
5477 (> (org-time-stamp-to-now
5478 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5479 ((eq org-agenda-todo-ignore-scheduled 'past)
5480 (<= (org-time-stamp-to-now
5481 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5482 ((numberp org-agenda-todo-ignore-scheduled)
5483 (org-agenda-todo-custom-ignore-p
5484 (match-string 1) org-agenda-todo-ignore-scheduled))
5485 (t)))
5486 (and org-agenda-todo-ignore-deadlines
5487 (re-search-forward org-deadline-time-regexp end t)
5488 (cond
5489 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5490 ((eq org-agenda-todo-ignore-deadlines 'far)
5491 (not (org-deadline-close (match-string 1))))
5492 ((eq org-agenda-todo-ignore-deadlines 'future)
5493 (> (org-time-stamp-to-now
5494 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5495 ((eq org-agenda-todo-ignore-deadlines 'past)
5496 (<= (org-time-stamp-to-now
5497 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5498 ((numberp org-agenda-todo-ignore-deadlines)
5499 (org-agenda-todo-custom-ignore-p
5500 (match-string 1) org-agenda-todo-ignore-deadlines))
5501 (t (org-deadline-close (match-string 1)))))
5502 (and org-agenda-todo-ignore-timestamp
5503 (let ((buffer (current-buffer))
5504 (regexp
5505 (concat
5506 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5507 (start (point)))
5508 ;; Copy current buffer into a temporary one
5509 (with-temp-buffer
5510 (insert-buffer-substring buffer start end)
5511 (goto-char (point-min))
5512 ;; Delete SCHEDULED and DEADLINE items
5513 (while (re-search-forward regexp end t)
5514 (delete-region (match-beginning 0) (match-end 0)))
5515 (goto-char (point-min))
5516 ;; No search for timestamp left
5517 (when (re-search-forward org-ts-regexp nil t)
5518 (cond
5519 ((eq org-agenda-todo-ignore-timestamp 'future)
5520 (> (org-time-stamp-to-now
5521 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5522 ((eq org-agenda-todo-ignore-timestamp 'past)
5523 (<= (org-time-stamp-to-now
5524 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5525 ((numberp org-agenda-todo-ignore-timestamp)
5526 (org-agenda-todo-custom-ignore-p
5527 (match-string 1) org-agenda-todo-ignore-timestamp))
5528 (t))))))))))
5530 (defun org-agenda-get-timestamps (&optional deadline-results)
5531 "Return the date stamp information for agenda display."
5532 (let* ((props (list 'face 'org-agenda-calendar-event
5533 'org-not-done-regexp org-not-done-regexp
5534 'org-todo-regexp org-todo-regexp
5535 'org-complex-heading-regexp org-complex-heading-regexp
5536 'mouse-face 'highlight
5537 'help-echo
5538 (format "mouse-2 or RET jump to org file %s"
5539 (abbreviate-file-name buffer-file-name))))
5540 (d1 (calendar-absolute-from-gregorian date))
5542 (deadline-position-alist
5543 (mapcar (lambda (a) (and (setq mm (get-text-property
5544 0 'org-hd-marker a))
5545 (cons (marker-position mm) a)))
5546 deadline-results))
5547 (remove-re org-ts-regexp)
5548 (regexp
5549 (concat
5550 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5551 (regexp-quote
5552 (substring
5553 (format-time-string
5554 (car org-time-stamp-formats)
5555 (apply 'encode-time ; DATE bound by calendar
5556 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5557 1 11))
5558 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5559 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5560 marker hdmarker deadlinep scheduledp clockp closedp inactivep
5561 donep tmp priority category category-pos level ee txt timestr tags
5562 b0 b3 e3 head todo-state end-of-match show-all warntime habitp
5563 inherited-tags ts-date)
5564 (goto-char (point-min))
5565 (while (setq end-of-match (re-search-forward regexp nil t))
5566 (setq b0 (match-beginning 0)
5567 b3 (match-beginning 3) e3 (match-end 3)
5568 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
5569 habitp (and (functionp 'org-is-habit-p) (save-match-data (org-is-habit-p)))
5570 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5571 (member todo-state
5572 org-agenda-repeating-timestamp-show-all)))
5573 (catch :skip
5574 (and (org-at-date-range-p) (throw :skip nil))
5575 (org-agenda-skip)
5576 (if (and (match-end 1)
5577 (not (= d1 (org-time-string-to-absolute
5578 (match-string 1) d1 nil show-all
5579 (current-buffer) b0))))
5580 (throw :skip nil))
5581 (if (and e3
5582 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
5583 (throw :skip nil))
5584 (setq tmp (buffer-substring (max (point-min)
5585 (- b0 org-ds-keyword-length))
5587 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
5588 inactivep (= (char-after b0) ?\[)
5589 deadlinep (string-match org-deadline-regexp tmp)
5590 scheduledp (string-match org-scheduled-regexp tmp)
5591 closedp (and org-agenda-include-inactive-timestamps
5592 (string-match org-closed-string tmp))
5593 clockp (and org-agenda-include-inactive-timestamps
5594 (or (string-match org-clock-string tmp)
5595 (string-match "]-+\\'" tmp)))
5596 warntime (get-text-property (point) 'org-appt-warntime)
5597 donep (member todo-state org-done-keywords))
5598 (if (or scheduledp deadlinep closedp clockp
5599 (and donep org-agenda-skip-timestamp-if-done))
5600 (throw :skip t))
5601 (if (string-match ">" timestr)
5602 ;; substring should only run to end of time stamp
5603 (setq timestr (substring timestr 0 (match-end 0))))
5604 (setq marker (org-agenda-new-marker b0)
5605 category (org-get-category b0)
5606 category-pos (get-text-property b0 'org-category-position))
5607 (save-excursion
5608 (if (not (re-search-backward org-outline-regexp-bol nil t))
5609 (throw :skip nil)
5610 (goto-char (match-beginning 0))
5611 (if (and (eq t org-agenda-skip-timestamp-if-deadline-is-shown)
5612 (assoc (point) deadline-position-alist))
5613 (throw :skip nil))
5614 (setq hdmarker (org-agenda-new-marker)
5615 inherited-tags
5616 (or (eq org-agenda-show-inherited-tags 'always)
5617 (and (listp org-agenda-show-inherited-tags)
5618 (memq 'agenda org-agenda-show-inherited-tags))
5619 (and (eq org-agenda-show-inherited-tags t)
5620 (or (eq org-agenda-use-tag-inheritance t)
5621 (memq 'agenda org-agenda-use-tag-inheritance))))
5622 tags (org-get-tags-at nil (not inherited-tags))
5623 level (make-string (org-reduced-level (org-outline-level)) ? ))
5624 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5625 (setq head (or (match-string 1) ""))
5626 (setq txt (org-agenda-format-item
5627 (if inactivep org-agenda-inactive-leader nil)
5628 head level category tags timestr
5629 remove-re habitp)))
5630 (setq priority (org-get-priority txt))
5631 (org-add-props txt props 'priority priority
5632 'org-marker marker 'org-hd-marker hdmarker
5633 'org-category category 'date date
5634 'level level
5635 'ts-date
5636 (ignore-errors (org-time-string-to-absolute timestr))
5637 'org-category-position category-pos
5638 'todo-state todo-state
5639 'warntime warntime
5640 'type "timestamp")
5641 (push txt ee))
5642 (if org-agenda-skip-additional-timestamps-same-entry
5643 (outline-next-heading)
5644 (goto-char end-of-match))))
5645 (nreverse ee)))
5647 (defun org-agenda-get-sexps ()
5648 "Return the sexp information for agenda display."
5649 (require 'diary-lib)
5650 (let* ((props (list 'face 'org-agenda-calendar-sexp
5651 'mouse-face 'highlight
5652 'help-echo
5653 (format "mouse-2 or RET jump to org file %s"
5654 (abbreviate-file-name buffer-file-name))))
5655 (regexp "^&?%%(")
5656 marker category extra category-pos level ee txt tags entry
5657 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5658 (goto-char (point-min))
5659 (while (re-search-forward regexp nil t)
5660 (catch :skip
5661 (org-agenda-skip)
5662 (setq beg (match-beginning 0))
5663 (goto-char (1- (match-end 0)))
5664 (setq b (point))
5665 (forward-sexp 1)
5666 (setq sexp (buffer-substring b (point)))
5667 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5668 (org-trim (match-string 1))
5669 ""))
5670 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5671 (when result
5672 (setq marker (org-agenda-new-marker beg)
5673 level (make-string (org-reduced-level (org-outline-level)) ? )
5674 category (org-get-category beg)
5675 category-pos (get-text-property beg 'org-category-position)
5676 inherited-tags
5677 (or (eq org-agenda-show-inherited-tags 'always)
5678 (and (listp org-agenda-show-inherited-tags)
5679 (memq 'agenda org-agenda-show-inherited-tags))
5680 (and (eq org-agenda-show-inherited-tags t)
5681 (or (eq org-agenda-use-tag-inheritance t)
5682 (memq 'agenda org-agenda-use-tag-inheritance))))
5683 tags (org-get-tags-at nil (not inherited-tags))
5684 todo-state (org-get-todo-state)
5685 warntime (get-text-property (point) 'org-appt-warntime)
5686 extra nil)
5688 (dolist (r (if (stringp result)
5689 (list result)
5690 result)) ;; we expect a list here
5691 (when (and org-agenda-diary-sexp-prefix
5692 (string-match org-agenda-diary-sexp-prefix r))
5693 (setq extra (match-string 0 r)
5694 r (replace-match "" nil nil r)))
5695 (if (string-match "\\S-" r)
5696 (setq txt r)
5697 (setq txt "SEXP entry returned empty string"))
5698 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5699 (org-add-props txt props 'org-marker marker
5700 'org-category category 'date date 'todo-state todo-state
5701 'org-category-position category-pos 'tags tags
5702 'level level
5703 'type "sexp" 'warntime warntime)
5704 (push txt ee)))))
5705 (nreverse ee)))
5707 ;; Calendar sanity: define some functions that are independent of
5708 ;; `calendar-date-style'.
5709 ;; Normally I would like to use ISO format when calling the diary functions,
5710 ;; but to make sure we still have Emacs 22 compatibility we bind
5711 ;; also `european-calendar-style' and use european format
5712 (defun org-anniversary (year month day &optional mark)
5713 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5714 (org-no-warnings
5715 (let ((calendar-date-style 'european) (european-calendar-style t))
5716 (diary-anniversary day month year mark))))
5717 (defun org-cyclic (N year month day &optional mark)
5718 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5719 (org-no-warnings
5720 (let ((calendar-date-style 'european) (european-calendar-style t))
5721 (diary-cyclic N day month year mark))))
5722 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5723 "Like `diary-block', but with fixed (ISO) order of arguments."
5724 (org-no-warnings
5725 (let ((calendar-date-style 'european) (european-calendar-style t))
5726 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
5727 (defun org-date (year month day &optional mark)
5728 "Like `diary-date', but with fixed (ISO) order of arguments."
5729 (org-no-warnings
5730 (let ((calendar-date-style 'european) (european-calendar-style t))
5731 (diary-date day month year mark))))
5733 ;; Define the` org-class' function
5734 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5735 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5736 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5737 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5738 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5739 `holidays', then any date that is known by the Emacs calendar to be a
5740 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5741 then those holidays will be skipped."
5742 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5743 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5744 (d (calendar-absolute-from-gregorian date))
5745 (h (when skip-weeks (calendar-check-holidays date))))
5746 (and
5747 (<= date1 d)
5748 (<= d date2)
5749 (= (calendar-day-of-week date) dayname)
5750 (or (not skip-weeks)
5751 (progn
5752 (require 'cal-iso)
5753 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5754 (not (or (and h (memq 'holidays skip-weeks))
5755 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5756 entry)))
5758 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
5759 "Like `org-class', but honor `calendar-date-style'.
5760 The order of the first 2 times 3 arguments depends on the variable
5761 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
5762 So for American calendars, give this as MONTH DAY YEAR, for European as
5763 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
5764 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
5765 is any number of ISO weeks in the block period for which the item should
5766 be skipped.
5768 This function is here only for backward compatibility and it is deprecated,
5769 please use `org-class' instead."
5770 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
5771 (date2 (org-order-calendar-date-args m2 d2 y2)))
5772 (org-class
5773 (nth 2 date1) (car date1) (nth 1 date1)
5774 (nth 2 date2) (car date2) (nth 1 date2)
5775 dayname skip-weeks)))
5776 (make-obsolete 'org-diary-class 'org-class "")
5778 (defalias 'org-get-closed 'org-agenda-get-progress)
5779 (defun org-agenda-get-progress ()
5780 "Return the logged TODO entries for agenda display."
5781 (let* ((props (list 'mouse-face 'highlight
5782 'org-not-done-regexp org-not-done-regexp
5783 'org-todo-regexp org-todo-regexp
5784 'org-complex-heading-regexp org-complex-heading-regexp
5785 'help-echo
5786 (format "mouse-2 or RET jump to org file %s"
5787 (abbreviate-file-name buffer-file-name))))
5788 (items (if (consp org-agenda-show-log-scoped)
5789 org-agenda-show-log-scoped
5790 (if (eq org-agenda-show-log-scoped 'clockcheck)
5791 '(clock)
5792 org-agenda-log-mode-items)))
5793 (parts
5794 (delq nil
5795 (list
5796 (if (memq 'closed items) (concat "\\<" org-closed-string))
5797 (if (memq 'clock items) (concat "\\<" org-clock-string))
5798 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5799 (parts-re (if parts (mapconcat 'identity parts "\\|")
5800 (error "`org-agenda-log-mode-items' is empty")))
5801 (regexp (concat
5802 "\\(" parts-re "\\)"
5803 " *\\["
5804 (regexp-quote
5805 (substring
5806 (format-time-string
5807 (car org-time-stamp-formats)
5808 (apply 'encode-time ; DATE bound by calendar
5809 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5810 1 11))))
5811 (org-agenda-search-headline-for-time nil)
5812 marker hdmarker priority category category-pos level tags closedp
5813 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5814 (goto-char (point-min))
5815 (while (re-search-forward regexp nil t)
5816 (catch :skip
5817 (org-agenda-skip)
5818 (setq marker (org-agenda-new-marker (match-beginning 0))
5819 closedp (equal (match-string 1) org-closed-string)
5820 statep (equal (string-to-char (match-string 1)) ?-)
5821 clockp (not (or closedp statep))
5822 state (and statep (match-string 2))
5823 category (org-get-category (match-beginning 0))
5824 category-pos (get-text-property (match-beginning 0) 'org-category-position)
5825 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5826 (when (string-match "\\]" timestr)
5827 ;; substring should only run to end of time stamp
5828 (setq rest (substring timestr (match-end 0))
5829 timestr (substring timestr 0 (match-end 0)))
5830 (if (and (not closedp) (not statep)
5831 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5832 rest))
5833 (progn (setq timestr (concat (substring timestr 0 -1)
5834 "-" (match-string 1 rest) "]"))
5835 (setq clocked (match-string 2 rest)))
5836 (setq clocked "-")))
5837 (save-excursion
5838 (setq extra
5839 (cond
5840 ((not org-agenda-log-mode-add-notes) nil)
5841 (statep
5842 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5843 (match-string 1)))
5844 (clockp
5845 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5846 (match-string 1)))))
5847 (if (not (re-search-backward org-outline-regexp-bol nil t))
5848 (throw :skip nil)
5849 (goto-char (match-beginning 0))
5850 (setq hdmarker (org-agenda-new-marker)
5851 inherited-tags
5852 (or (eq org-agenda-show-inherited-tags 'always)
5853 (and (listp org-agenda-show-inherited-tags)
5854 (memq 'todo org-agenda-show-inherited-tags))
5855 (and (eq org-agenda-show-inherited-tags t)
5856 (or (eq org-agenda-use-tag-inheritance t)
5857 (memq 'todo org-agenda-use-tag-inheritance))))
5858 tags (org-get-tags-at nil (not inherited-tags))
5859 level (make-string (org-reduced-level (org-outline-level)) ? ))
5860 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5861 (setq txt (match-string 1))
5862 (when extra
5863 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5864 (setq txt (concat (substring txt 0 (match-beginning 1))
5865 " - " extra " " (match-string 2 txt)))
5866 (setq txt (concat txt " - " extra))))
5867 (setq txt (org-agenda-format-item
5868 (cond
5869 (closedp "Closed: ")
5870 (statep (concat "State: (" state ")"))
5871 (t (concat "Clocked: (" clocked ")")))
5872 txt level category tags timestr)))
5873 (setq priority 100000)
5874 (org-add-props txt props
5875 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5876 'priority priority 'org-category category
5877 'org-category-position category-pos
5878 'level level
5879 'type "closed" 'date date
5880 'undone-face 'org-warning 'done-face 'org-agenda-done)
5881 (push txt ee))
5882 (goto-char (point-at-eol))))
5883 (nreverse ee)))
5885 (defun org-agenda-show-clocking-issues ()
5886 "Add overlays, showing issues with clocking.
5887 See also the user option `org-agenda-clock-consistency-checks'."
5888 (interactive)
5889 (let* ((org-time-clocksum-use-effort-durations nil)
5890 (pl org-agenda-clock-consistency-checks)
5891 (re (concat "^[ \t]*"
5892 org-clock-string
5893 "[ \t]+"
5894 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5895 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5896 (tlstart 0.)
5897 (tlend 0.)
5898 (maxtime (org-hh:mm-string-to-minutes
5899 (or (plist-get pl :max-duration) "24:00")))
5900 (mintime (org-hh:mm-string-to-minutes
5901 (or (plist-get pl :min-duration) 0)))
5902 (maxgap (org-hh:mm-string-to-minutes
5903 ;; default 30:00 means never complain
5904 (or (plist-get pl :max-gap) "30:00")))
5905 (gapok (mapcar 'org-hh:mm-string-to-minutes
5906 (plist-get pl :gap-ok-around)))
5907 (def-face (or (plist-get pl :default-face)
5908 '((:background "DarkRed") (:foreground "white"))))
5909 issue face m te ts dt ov)
5910 (goto-char (point-min))
5911 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5912 (setq issue nil face def-face)
5913 (catch 'next
5914 (setq m (org-get-at-bol 'org-marker)
5915 te nil ts nil)
5916 (unless (and m (markerp m))
5917 (setq issue "No valid clock line") (throw 'next t))
5918 (org-with-point-at m
5919 (save-excursion
5920 (goto-char (point-at-bol))
5921 (unless (looking-at re)
5922 (error "No valid Clock line")
5923 (throw 'next t))
5924 (unless (match-end 3)
5925 (setq issue "No end time"
5926 face (or (plist-get pl :no-end-time-face) face))
5927 (throw 'next t))
5928 (setq ts (match-string 1)
5929 te (match-string 3)
5930 ts (org-float-time
5931 (apply 'encode-time (org-parse-time-string ts)))
5932 te (org-float-time
5933 (apply 'encode-time (org-parse-time-string te)))
5934 dt (- te ts))))
5935 (cond
5936 ((> dt (* 60 maxtime))
5937 ;; a very long clocking chunk
5938 (setq issue (format "Clocking interval is very long: %s"
5939 (org-minutes-to-clocksum-string
5940 (floor (/ (float dt) 60.))))
5941 face (or (plist-get pl :long-face) face)))
5942 ((< dt (* 60 mintime))
5943 ;; a very short clocking chunk
5944 (setq issue (format "Clocking interval is very short: %s"
5945 (org-minutes-to-clocksum-string
5946 (floor (/ (float dt) 60.))))
5947 face (or (plist-get pl :short-face) face)))
5948 ((and (> tlend 0) (< ts tlend))
5949 ;; Two clock entries are overlapping
5950 (setq issue (format "Clocking overlap: %d minutes"
5951 (/ (- tlend ts) 60))
5952 face (or (plist-get pl :overlap-face) face)))
5953 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5954 ;; There is a gap, lets see if we need to report it
5955 (unless (org-agenda-check-clock-gap tlend ts gapok)
5956 (setq issue (format "Clocking gap: %d minutes"
5957 (/ (- ts tlend) 60))
5958 face (or (plist-get pl :gap-face) face))))
5959 (t nil)))
5960 (setq tlend (or te tlend) tlstart (or ts tlstart))
5961 (when issue
5962 ;; OK, there was some issue, add an overlay to show the issue
5963 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5964 (overlay-put ov 'before-string
5965 (concat
5966 (org-add-props
5967 (format "%-43s" (concat " " issue))
5969 'face face)
5970 "\n"))
5971 (overlay-put ov 'evaporate t)))))
5973 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5974 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5975 (catch 'exit
5976 (unless ok-list
5977 ;; there are no OK times for gaps...
5978 (throw 'exit nil))
5979 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5980 ;; This is more than 24 hours, so it is OK.
5981 ;; because we have at least one OK time, that must be in the
5982 ;; 24 hour interval.
5983 (throw 'exit t))
5984 ;; We have a shorter gap.
5985 ;; Now we have to get the minute of the day when these times are
5986 (let* ((t1dec (decode-time (seconds-to-time t1)))
5987 (t2dec (decode-time (seconds-to-time t2)))
5988 ;; compute the minute on the day
5989 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5990 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5991 (when (< min2 min1)
5992 ;; if min2 is smaller than min1, this means it is on the next day.
5993 ;; Wrap it to after midnight.
5994 (setq min2 (+ min2 1440)))
5995 ;; Now check if any of the OK times is in the gap
5996 (mapc (lambda (x)
5997 ;; Wrap the time to after midnight if necessary
5998 (if (< x min1) (setq x (+ x 1440)))
5999 ;; Check if in interval
6000 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
6001 ok-list)
6002 ;; Nope, this gap is not OK
6003 nil)))
6005 (defun org-agenda-get-deadlines (&optional with-hour)
6006 "Return the deadline information for agenda display.
6007 When WITH-HOUR is non-nil, only return deadlines with an hour
6008 specification like [h]h:mm."
6009 (let* ((props (list 'mouse-face 'highlight
6010 'org-not-done-regexp org-not-done-regexp
6011 'org-todo-regexp org-todo-regexp
6012 'org-complex-heading-regexp org-complex-heading-regexp
6013 'help-echo
6014 (format "mouse-2 or RET jump to org file %s"
6015 (abbreviate-file-name buffer-file-name))))
6016 (regexp (if with-hour
6017 org-deadline-time-hour-regexp
6018 org-deadline-time-regexp))
6019 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
6020 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
6021 (dl0 (car org-agenda-deadline-leaders))
6022 (dl1 (nth 1 org-agenda-deadline-leaders))
6023 (dl2 (or (nth 2 org-agenda-deadline-leaders) dl1))
6024 d2 diff dfrac wdays pos pos1 category category-pos level
6025 tags suppress-prewarning ee txt head face s todo-state
6026 show-all upcomingp donep timestr warntime inherited-tags ts-date)
6027 (goto-char (point-min))
6028 (while (re-search-forward regexp nil t)
6029 (catch :skip
6030 (org-agenda-skip)
6031 (setq s (match-string 1)
6032 txt nil
6033 pos (1- (match-beginning 1))
6034 todo-state (save-match-data (org-get-todo-state))
6035 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6036 (member todo-state
6037 org-agenda-repeating-timestamp-show-all))
6038 d2 (org-time-string-to-absolute
6039 s d1 'past show-all (current-buffer) pos)
6040 diff (- d2 d1))
6041 (setq suppress-prewarning
6042 (let ((ds (and org-agenda-skip-deadline-prewarning-if-scheduled
6043 (let ((item (buffer-substring (point-at-bol)
6044 (point-at-eol))))
6045 (save-match-data
6046 (and (string-match
6047 org-scheduled-time-regexp item)
6048 (match-string 1 item)))))))
6049 (cond
6050 ((not ds) nil)
6051 ;; The current item has a scheduled date (in ds), so
6052 ;; evaluate its prewarning lead time.
6053 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
6054 ;; Use global prewarning-restart lead time.
6055 org-agenda-skip-deadline-prewarning-if-scheduled)
6056 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
6057 'pre-scheduled)
6058 ;; Set prewarning to no earlier than scheduled.
6059 (min (- d2 (org-time-string-to-absolute
6060 ds d1 'past show-all (current-buffer) pos))
6061 org-deadline-warning-days))
6062 ;; Set prewarning to deadline.
6063 (t 0))))
6064 (setq wdays (if suppress-prewarning
6065 (let ((org-deadline-warning-days suppress-prewarning))
6066 (org-get-wdays s))
6067 (org-get-wdays s))
6068 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
6069 upcomingp (and todayp (> diff 0)))
6070 ;; When to show a deadline in the calendar:
6071 ;; If the expiration is within wdays warning time.
6072 ;; Past-due deadlines are only shown on the current date
6073 (if (and (or (and (<= diff wdays)
6074 (and todayp (not org-agenda-only-exact-dates)))
6075 (= diff 0)))
6076 (save-excursion
6077 ;; (setq todo-state (org-get-todo-state))
6078 (setq donep (member todo-state org-done-keywords))
6079 (if (and donep
6080 (or org-agenda-skip-deadline-if-done
6081 (not (= diff 0))))
6082 (setq txt nil)
6083 (setq category (org-get-category)
6084 warntime (get-text-property (point) 'org-appt-warntime)
6085 category-pos (get-text-property (point) 'org-category-position))
6086 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6087 (throw :skip nil)
6088 (goto-char (match-end 0))
6089 (setq pos1 (match-beginning 0))
6090 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6091 (setq inherited-tags
6092 (or (eq org-agenda-show-inherited-tags 'always)
6093 (and (listp org-agenda-show-inherited-tags)
6094 (memq 'agenda org-agenda-show-inherited-tags))
6095 (and (eq org-agenda-show-inherited-tags t)
6096 (or (eq org-agenda-use-tag-inheritance t)
6097 (memq 'agenda org-agenda-use-tag-inheritance))))
6098 tags (org-get-tags-at pos1 (not inherited-tags)))
6099 (setq head (buffer-substring
6100 (point)
6101 (progn (skip-chars-forward "^\r\n")
6102 (point))))
6103 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6104 (setq timestr
6105 (concat (substring s (match-beginning 1)) " "))
6106 (setq timestr 'time))
6107 (setq txt (org-agenda-format-item
6108 (cond ((= diff 0) dl0)
6109 ((> diff 0)
6110 (if (functionp dl1)
6111 (funcall dl1 diff date)
6112 (format dl1 diff)))
6114 (if (functionp dl2)
6115 (funcall dl2 diff date)
6116 (format dl2 (if (string= dl2 dl1)
6117 diff (abs diff))))))
6118 head level category tags
6119 (if (not (= diff 0)) nil timestr)))))
6120 (when txt
6121 (setq face (org-agenda-deadline-face dfrac))
6122 (org-add-props txt props
6123 'org-marker (org-agenda-new-marker pos)
6124 'warntime warntime
6125 'level level
6126 'ts-date d2
6127 'org-hd-marker (org-agenda-new-marker pos1)
6128 'priority (+ (- diff)
6129 (org-get-priority txt))
6130 'org-category category
6131 'org-category-position category-pos
6132 'todo-state todo-state
6133 'type (if upcomingp "upcoming-deadline" "deadline")
6134 'date (if upcomingp date d2)
6135 'face (if donep 'org-agenda-done face)
6136 'undone-face face 'done-face 'org-agenda-done)
6137 (push txt ee))))))
6138 (nreverse ee)))
6140 (defun org-agenda-deadline-face (fraction)
6141 "Return the face to displaying a deadline item.
6142 FRACTION is what fraction of the head-warning time has passed."
6143 (let ((faces org-agenda-deadline-faces) f)
6144 (catch 'exit
6145 (while (setq f (pop faces))
6146 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
6148 (defun org-agenda-get-scheduled (&optional deadline-results with-hour)
6149 "Return the scheduled information for agenda display.
6150 When WITH-HOUR is non-nil, only return scheduled items with
6151 an hour specification like [h]h:mm."
6152 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
6153 'org-todo-regexp org-todo-regexp
6154 'org-complex-heading-regexp org-complex-heading-regexp
6155 'done-face 'org-agenda-done
6156 'mouse-face 'highlight
6157 'help-echo
6158 (format "mouse-2 or RET jump to org file %s"
6159 (abbreviate-file-name buffer-file-name))))
6160 (regexp (if with-hour
6161 org-scheduled-time-hour-regexp
6162 org-scheduled-time-regexp))
6163 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
6164 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
6166 (deadline-position-alist
6167 (mapcar (lambda (a) (and (setq mm (get-text-property
6168 0 'org-hd-marker a))
6169 (cons (marker-position mm) a)))
6170 deadline-results))
6171 d2 diff pos pos1 category category-pos level tags donep
6172 ee txt head pastschedp todo-state face timestr s habitp show-all
6173 did-habit-check-p warntime inherited-tags ts-date suppress-delay
6174 ddays)
6175 (goto-char (point-min))
6176 (while (re-search-forward regexp nil t)
6177 (catch :skip
6178 (org-agenda-skip)
6179 (setq s (match-string 1)
6180 txt nil
6181 pos (1- (match-beginning 1))
6182 todo-state (save-match-data (org-get-todo-state))
6183 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6184 (member todo-state
6185 org-agenda-repeating-timestamp-show-all))
6186 d2 (org-time-string-to-absolute
6187 s d1 'past show-all (current-buffer) pos)
6188 diff (- d2 d1)
6189 warntime (get-text-property (point) 'org-appt-warntime))
6190 (setq pastschedp (and todayp (< diff 0)))
6191 (setq did-habit-check-p nil)
6192 (setq suppress-delay
6193 (let ((ds (and org-agenda-skip-scheduled-delay-if-deadline
6194 (let ((item (buffer-substring (point-at-bol) (point-at-eol))))
6195 (save-match-data
6196 (and (string-match
6197 org-deadline-time-regexp item)
6198 (match-string 1 item)))))))
6199 (cond
6200 ((not ds) nil)
6201 ;; The current item has a deadline date (in ds), so
6202 ;; evaluate its delay time.
6203 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6204 ;; Use global delay time.
6205 (- org-agenda-skip-scheduled-delay-if-deadline))
6206 ((eq org-agenda-skip-scheduled-delay-if-deadline
6207 'post-deadline)
6208 ;; Set delay to no later than deadline.
6209 (min (- d2 (org-time-string-to-absolute
6210 ds d1 'past show-all (current-buffer) pos))
6211 org-scheduled-delay-days))
6212 (t 0))))
6213 (setq ddays (if suppress-delay
6214 (let ((org-scheduled-delay-days suppress-delay))
6215 (org-get-wdays s t t))
6216 (org-get-wdays s t)))
6217 ;; Use a delay of 0 when there is a repeater and the delay is
6218 ;; of the form --3d
6219 (when (and (save-match-data (string-match "--[0-9]+[hdwmy]" s))
6220 (< (org-time-string-to-absolute s)
6221 (org-time-string-to-absolute
6222 s d2 'past nil (current-buffer) pos)))
6223 (setq ddays 0))
6224 ;; When to show a scheduled item in the calendar:
6225 ;; If it is on or past the date.
6226 (when (or (and (> ddays 0) (= diff (- ddays)))
6227 (and (zerop ddays) (= diff 0))
6228 (and (< (+ diff ddays) 0)
6229 (< (abs diff) org-scheduled-past-days)
6230 (and todayp (not org-agenda-only-exact-dates)))
6231 ;; org-is-habit-p uses org-entry-get, which is expansive
6232 ;; so we go extra mile to only call it once
6233 (and todayp
6234 (boundp 'org-habit-show-all-today)
6235 org-habit-show-all-today
6236 (setq did-habit-check-p t)
6237 (setq habitp (and (functionp 'org-is-habit-p)
6238 (org-is-habit-p)))))
6239 (save-excursion
6240 (setq donep (member todo-state org-done-keywords))
6241 (if (and donep
6242 (or org-agenda-skip-scheduled-if-done
6243 (not (= diff 0))
6244 (and (functionp 'org-is-habit-p)
6245 (org-is-habit-p))))
6246 (setq txt nil)
6247 (setq habitp (if did-habit-check-p habitp
6248 (and (functionp 'org-is-habit-p)
6249 (org-is-habit-p))))
6250 (setq category (org-get-category)
6251 category-pos (get-text-property (point) 'org-category-position))
6252 (if (and (eq org-agenda-skip-scheduled-if-deadline-is-shown
6253 'repeated-after-deadline)
6254 (org-get-deadline-time (point))
6255 (<= 0 (- d2 (time-to-days (org-get-deadline-time (point))))))
6256 (throw :skip nil))
6257 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6258 (throw :skip nil)
6259 (goto-char (match-end 0))
6260 (setq pos1 (match-beginning 0))
6261 (if habitp
6262 (if (or (not org-habit-show-habits)
6263 (and (not todayp)
6264 (boundp 'org-habit-show-habits-only-for-today)
6265 org-habit-show-habits-only-for-today))
6266 (throw :skip nil))
6267 (if (and
6268 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
6269 (and (eq org-agenda-skip-scheduled-if-deadline-is-shown 'not-today)
6270 pastschedp))
6271 (setq mm (assoc pos1 deadline-position-alist)))
6272 (throw :skip nil)))
6273 (setq inherited-tags
6274 (or (eq org-agenda-show-inherited-tags 'always)
6275 (and (listp org-agenda-show-inherited-tags)
6276 (memq 'agenda org-agenda-show-inherited-tags))
6277 (and (eq org-agenda-show-inherited-tags t)
6278 (or (eq org-agenda-use-tag-inheritance t)
6279 (memq 'agenda org-agenda-use-tag-inheritance))))
6281 tags (org-get-tags-at nil (not inherited-tags)))
6282 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6283 (setq head (buffer-substring
6284 (point)
6285 (progn (skip-chars-forward "^\r\n") (point))))
6286 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6287 (setq timestr
6288 (concat (substring s (match-beginning 1)) " "))
6289 (setq timestr 'time))
6290 (setq txt (org-agenda-format-item
6291 (if (= diff 0)
6292 (car org-agenda-scheduled-leaders)
6293 (format (nth 1 org-agenda-scheduled-leaders)
6294 (- 1 diff)))
6295 head level category tags
6296 (if (not (= diff 0)) nil timestr)
6297 nil habitp))))
6298 (when txt
6299 (setq face
6300 (cond
6301 ((and (not habitp) pastschedp)
6302 'org-scheduled-previously)
6303 (todayp 'org-scheduled-today)
6304 (t 'org-scheduled))
6305 habitp (and habitp (org-habit-parse-todo)))
6306 (org-add-props txt props
6307 'undone-face face
6308 'face (if donep 'org-agenda-done face)
6309 'org-marker (org-agenda-new-marker pos)
6310 'org-hd-marker (org-agenda-new-marker pos1)
6311 'type (if pastschedp "past-scheduled" "scheduled")
6312 'date (if pastschedp d2 date)
6313 'ts-date d2
6314 'warntime warntime
6315 'level level
6316 'priority (if habitp
6317 (org-habit-get-priority habitp)
6318 (+ 94 (- 5 diff) (org-get-priority txt)))
6319 'org-category category
6320 'category-position category-pos
6321 'org-habit-p habitp
6322 'todo-state todo-state)
6323 (push txt ee))))))
6324 (nreverse ee)))
6326 (defun org-agenda-get-blocks ()
6327 "Return the date-range information for agenda display."
6328 (let* ((props (list 'face nil
6329 'org-not-done-regexp org-not-done-regexp
6330 'org-todo-regexp org-todo-regexp
6331 'org-complex-heading-regexp org-complex-heading-regexp
6332 'mouse-face 'highlight
6333 'help-echo
6334 (format "mouse-2 or RET jump to org file %s"
6335 (abbreviate-file-name buffer-file-name))))
6336 (regexp org-tr-regexp)
6337 (d0 (calendar-absolute-from-gregorian date))
6338 marker hdmarker ee txt d1 d2 s1 s2 category category-pos
6339 level todo-state tags pos head donep inherited-tags)
6340 (goto-char (point-min))
6341 (while (re-search-forward regexp nil t)
6342 (catch :skip
6343 (org-agenda-skip)
6344 (setq pos (point))
6345 (let ((start-time (match-string 1))
6346 (end-time (match-string 2)))
6347 (setq s1 (match-string 1)
6348 s2 (match-string 2)
6349 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
6350 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
6351 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6352 ;; Only allow days between the limits, because the normal
6353 ;; date stamps will catch the limits.
6354 (save-excursion
6355 (setq todo-state (org-get-todo-state))
6356 (setq donep (member todo-state org-done-keywords))
6357 (if (and donep org-agenda-skip-timestamp-if-done)
6358 (throw :skip t))
6359 (setq marker (org-agenda-new-marker (point)))
6360 (setq category (org-get-category)
6361 category-pos (get-text-property (point) 'org-category-position))
6362 (if (not (re-search-backward org-outline-regexp-bol nil t))
6363 (throw :skip nil)
6364 (goto-char (match-beginning 0))
6365 (setq hdmarker (org-agenda-new-marker (point))
6366 inherited-tags
6367 (or (eq org-agenda-show-inherited-tags 'always)
6368 (and (listp org-agenda-show-inherited-tags)
6369 (memq 'agenda org-agenda-show-inherited-tags))
6370 (and (eq org-agenda-show-inherited-tags t)
6371 (or (eq org-agenda-use-tag-inheritance t)
6372 (memq 'agenda org-agenda-use-tag-inheritance))))
6374 tags (org-get-tags-at nil (not inherited-tags)))
6375 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6376 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
6377 (setq head (match-string 1))
6378 (let ((remove-re
6379 (if org-agenda-remove-timeranges-from-blocks
6380 (concat
6381 "<" (regexp-quote s1) ".*?>"
6382 "--"
6383 "<" (regexp-quote s2) ".*?>")
6384 nil)))
6385 (setq txt (org-agenda-format-item
6386 (format
6387 (nth (if (= d1 d2) 0 1)
6388 org-agenda-timerange-leaders)
6389 (1+ (- d0 d1)) (1+ (- d2 d1)))
6390 head level category tags
6391 (cond ((and (= d1 d0) (= d2 d0))
6392 (concat "<" start-time ">--<" end-time ">"))
6393 ((= d1 d0)
6394 (concat "<" start-time ">"))
6395 ((= d2 d0)
6396 (concat "<" end-time ">")))
6397 remove-re))))
6398 (org-add-props txt props
6399 'org-marker marker 'org-hd-marker hdmarker
6400 'type "block" 'date date
6401 'level level
6402 'todo-state todo-state
6403 'priority (org-get-priority txt) 'org-category category
6404 'org-category-position category-pos)
6405 (push txt ee))))
6406 (goto-char pos)))
6407 ;; Sort the entries by expiration date.
6408 (nreverse ee)))
6410 ;;; Agenda presentation and sorting
6412 (defvar org-prefix-has-time nil
6413 "A flag, set by `org-compile-prefix-format'.
6414 The flag is set if the currently compiled format contains a `%t'.")
6415 (defvar org-prefix-has-tag nil
6416 "A flag, set by `org-compile-prefix-format'.
6417 The flag is set if the currently compiled format contains a `%T'.")
6418 (defvar org-prefix-has-effort nil
6419 "A flag, set by `org-compile-prefix-format'.
6420 The flag is set if the currently compiled format contains a `%e'.")
6421 (defvar org-prefix-has-breadcrumbs nil
6422 "A flag, set by `org-compile-prefix-format'.
6423 The flag is set if the currently compiled format contains a `%b'.")
6424 (defvar org-prefix-category-length nil
6425 "Used by `org-compile-prefix-format' to remember the category field width.")
6426 (defvar org-prefix-category-max-length nil
6427 "Used by `org-compile-prefix-format' to remember the category field width.")
6429 (defun org-agenda-get-category-icon (category)
6430 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6431 (dolist (entry org-agenda-category-icon-alist)
6432 (when (org-string-match-p (car entry) category)
6433 (if (listp (cadr entry))
6434 (return (cadr entry))
6435 (return (apply 'create-image (cdr entry)))))))
6437 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6438 remove-re habitp)
6439 "Format TXT to be inserted into the agenda buffer.
6440 In particular, add the prefix and corresponding text properties.
6442 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6443 LEVEL may be a string to replace the `%l' specifier.
6444 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6445 category taken from local variable or file name. It will replace the `%c'
6446 specifier in the format.
6447 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6448 TXT for sorting of this entry, and for the `%t' specifier in the format.
6449 When DOTIME is a string, this string is searched for a time before TXT is.
6450 TAGS can be the tags of the headline.
6451 Any match of REMOVE-RE will be removed from TXT."
6452 ;; We keep the org-prefix-* variable values along with a compiled
6453 ;; formatter, so that multiple agendas existing at the same time do
6454 ;; not step on each other toes.
6456 ;; It was inconvenient to make these variables buffer local in
6457 ;; Agenda buffers, because this function expects to be called with
6458 ;; the buffer where item comes from being current, and not agenda
6459 ;; buffer
6460 (let* ((bindings (car org-prefix-format-compiled))
6461 (formatter (cadr org-prefix-format-compiled)))
6462 (loop for (var value) in bindings
6463 do (set var value))
6464 (save-match-data
6465 ;; Diary entries sometimes have extra whitespace at the beginning
6466 (setq txt (org-trim txt))
6468 ;; Fix the tags part in txt
6469 (setq txt (org-agenda-fix-displayed-tags
6470 txt tags
6471 org-agenda-show-inherited-tags
6472 org-agenda-hide-tags-regexp))
6474 (let* ((category (or category
6475 (if (stringp org-category)
6476 org-category
6477 (and org-category (symbol-name org-category)))
6478 (if buffer-file-name
6479 (file-name-sans-extension
6480 (file-name-nondirectory buffer-file-name))
6481 "")))
6482 (category-icon (org-agenda-get-category-icon category))
6483 (category-icon (if category-icon
6484 (propertize " " 'display category-icon)
6485 ""))
6486 ;; time, tag, effort are needed for the eval of the prefix format
6487 (tag (if tags (nth (1- (length tags)) tags) ""))
6488 time effort neffort
6489 (ts (if dotime (concat
6490 (if (stringp dotime) dotime "")
6491 (and org-agenda-search-headline-for-time txt))))
6492 (time-of-day (and dotime (org-get-time-of-day ts)))
6493 stamp plain s0 s1 s2 rtn srp l
6494 duration thecategory breadcrumbs)
6495 (and (derived-mode-p 'org-mode) buffer-file-name
6496 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6497 (when (and dotime time-of-day)
6498 ;; Extract starting and ending time and move them to prefix
6499 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6500 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6501 (setq s0 (match-string 0 ts)
6502 srp (and stamp (match-end 3))
6503 s1 (match-string (if plain 1 2) ts)
6504 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6506 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6507 ;; them, we might want to remove them there to avoid duplication.
6508 ;; The user can turn this off with a variable.
6509 (if (and org-prefix-has-time
6510 org-agenda-remove-times-when-in-prefix (or stamp plain)
6511 (string-match (concat (regexp-quote s0) " *") txt)
6512 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6513 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6514 (= (match-beginning 0) 0)
6516 (setq txt (replace-match "" nil nil txt))))
6517 ;; Normalize the time(s) to 24 hour
6518 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6519 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6521 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
6522 (let (org-time-clocksum-use-effort-durations)
6523 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6524 (setq s2
6525 (org-minutes-to-clocksum-string
6526 (+ (org-hh:mm-string-to-minutes s1)
6527 org-agenda-default-appointment-duration)))))
6529 ;; Compute the duration
6530 (when s2
6531 (setq duration (- (org-hh:mm-string-to-minutes s2)
6532 (org-hh:mm-string-to-minutes s1)))))
6534 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
6535 txt)
6536 ;; Tags are in the string
6537 (if (or (eq org-agenda-remove-tags t)
6538 (and org-agenda-remove-tags
6539 org-prefix-has-tag))
6540 (setq txt (replace-match "" t t txt))
6541 (setq txt (replace-match
6542 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6543 (match-string 2 txt))
6544 t t txt))))
6545 (when (derived-mode-p 'org-mode)
6546 (setq effort (ignore-errors (get-text-property 0 'org-effort txt))))
6548 ;; org-agenda-add-time-grid-maybe calls us with *Agenda* as
6549 ;; current buffer, so move this check outside of above
6550 (if effort
6551 (setq neffort (org-duration-string-to-minutes effort)
6552 effort (setq effort (concat "[" effort "]")))
6553 ;; prevent erroring out with %e format when there is no effort
6554 (setq effort ""))
6556 (when remove-re
6557 (while (string-match remove-re txt)
6558 (setq txt (replace-match "" t t txt))))
6560 ;; Set org-heading property on `txt' to mark the start of the
6561 ;; heading.
6562 (add-text-properties 0 (length txt) '(org-heading t) txt)
6564 ;; Prepare the variables needed in the eval of the compiled format
6565 (if org-prefix-has-breadcrumbs
6566 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6567 (let ((s (org-display-outline-path nil nil "->" t)))
6568 (if (eq "" s) "" (concat s "->"))))))
6569 (setq time (cond (s2 (concat
6570 (org-agenda-time-of-day-to-ampm-maybe s1)
6571 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6572 (if org-agenda-timegrid-use-ampm " ")))
6573 (s1 (concat
6574 (org-agenda-time-of-day-to-ampm-maybe s1)
6575 (if org-agenda-timegrid-use-ampm
6576 "........ "
6577 "......")))
6578 (t ""))
6579 extra (or (and (not habitp) extra) "")
6580 category (if (symbolp category) (symbol-name category) category)
6581 thecategory (copy-sequence category)
6582 level (or level ""))
6583 (if (string-match org-bracket-link-regexp category)
6584 (progn
6585 (setq l (if (match-end 3)
6586 (- (match-end 3) (match-beginning 3))
6587 (- (match-end 1) (match-beginning 1))))
6588 (when (< l (or org-prefix-category-length 0))
6589 (setq category (copy-sequence category))
6590 (org-add-props category nil
6591 'extra-space (make-string
6592 (- org-prefix-category-length l 1) ?\ ))))
6593 (if (and org-prefix-category-max-length
6594 (>= (length category) org-prefix-category-max-length))
6595 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6596 ;; Evaluate the compiled format
6597 (setq rtn (concat (eval formatter) txt))
6599 ;; And finally add the text properties
6600 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6601 (org-add-props rtn nil
6602 'org-category (if thecategory (downcase thecategory) category)
6603 'tags (mapcar 'org-downcase-keep-props tags)
6604 'org-highest-priority org-highest-priority
6605 'org-lowest-priority org-lowest-priority
6606 'time-of-day time-of-day
6607 'duration duration
6608 'effort effort
6609 'effort-minutes neffort
6610 'breadcrumbs breadcrumbs
6611 'txt txt
6612 'level level
6613 'time time
6614 'extra extra
6615 'format org-prefix-format-compiled
6616 'dotime dotime)))))
6618 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6619 "Remove tags string from TXT, and add a modified list of tags.
6620 The modified list may contain inherited tags, and tags matched by
6621 `org-agenda-hide-tags-regexp' will be removed."
6622 (when (or add-inherited hide-re)
6623 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
6624 (setq txt (substring txt 0 (match-beginning 0))))
6625 (setq tags
6626 (delq nil
6627 (mapcar (lambda (tg)
6628 (if (or (and hide-re (string-match hide-re tg))
6629 (and (not add-inherited)
6630 (get-text-property 0 'inherited tg)))
6632 tg))
6633 tags)))
6634 (when tags
6635 (let ((have-i (get-text-property 0 'inherited (car tags)))
6637 (setq txt (concat txt " :"
6638 (mapconcat
6639 (lambda (x)
6640 (setq i (get-text-property 0 'inherited x))
6641 (if (and have-i (not i))
6642 (progn
6643 (setq have-i nil)
6644 (concat ":" x))
6646 tags ":")
6647 (if have-i "::" ":"))))))
6648 txt)
6650 (defun org-downcase-keep-props (s)
6651 (let ((props (text-properties-at 0 s)))
6652 (setq s (downcase s))
6653 (add-text-properties 0 (length s) props s)
6656 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6658 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6659 "Add a time-grid for agenda items which need it.
6661 LIST is the list of agenda items formatted by `org-agenda-list'.
6662 NDAYS is the span of the current agenda view.
6663 TODAYP is `t' when the current agenda view is on today."
6664 (catch 'exit
6665 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6666 ((and todayp (member 'today (car org-agenda-time-grid))))
6667 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6668 ((member 'weekly (car org-agenda-time-grid)))
6669 (t (throw 'exit list)))
6670 (let* ((have (delq nil (mapcar
6671 (lambda (x) (get-text-property 1 'time-of-day x))
6672 list)))
6673 (string (nth 1 org-agenda-time-grid))
6674 (gridtimes (nth 2 org-agenda-time-grid))
6675 (req (car org-agenda-time-grid))
6676 (remove (member 'remove-match req))
6677 new time)
6678 (if (and (member 'require-timed req) (not have))
6679 ;; don't show empty grid
6680 (throw 'exit list))
6681 (while (setq time (pop gridtimes))
6682 (unless (and remove (member time have))
6683 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6684 (push (org-agenda-format-item
6685 nil string nil "" nil
6686 (concat (substring time 0 -2) ":" (substring time -2)))
6687 new)
6688 (put-text-property
6689 2 (length (car new)) 'face 'org-time-grid (car new))))
6690 (when (and todayp org-agenda-show-current-time-in-grid)
6691 (push (org-agenda-format-item
6692 nil org-agenda-current-time-string nil "" nil
6693 (format-time-string "%H:%M "))
6694 new)
6695 (put-text-property
6696 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6698 (if (member 'time-up org-agenda-sorting-strategy-selected)
6699 (append new list)
6700 (append list new)))))
6702 (defun org-compile-prefix-format (key)
6703 "Compile the prefix format into a Lisp form that can be evaluated.
6704 The resulting form and associated variable bindings is returned
6705 and stored in the variable `org-prefix-format-compiled'."
6706 (setq org-prefix-has-time nil
6707 org-prefix-has-tag nil
6708 org-prefix-category-length nil
6709 org-prefix-has-effort nil
6710 org-prefix-has-breadcrumbs nil)
6711 (let ((s (cond
6712 ((stringp org-agenda-prefix-format)
6713 org-agenda-prefix-format)
6714 ((assq key org-agenda-prefix-format)
6715 (cdr (assq key org-agenda-prefix-format)))
6716 (t " %-12:c%?-12t% s")))
6717 (start 0)
6718 varform vars var e c f opt)
6719 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6720 s start)
6721 (setq var (or (cdr (assoc (match-string 4 s)
6722 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6723 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6724 'eval)
6725 c (or (match-string 3 s) "")
6726 opt (match-beginning 1)
6727 start (1+ (match-beginning 0)))
6728 (if (equal var 'time) (setq org-prefix-has-time t))
6729 (if (equal var 'tag) (setq org-prefix-has-tag t))
6730 (if (equal var 'effort) (setq org-prefix-has-effort t))
6731 (if (equal var 'breadcrumbs) (setq org-prefix-has-breadcrumbs t))
6732 (setq f (concat "%" (match-string 2 s) "s"))
6733 (when (equal var 'category)
6734 (setq org-prefix-category-length
6735 (floor (abs (string-to-number (match-string 2 s)))))
6736 (setq org-prefix-category-max-length
6737 (let ((x (match-string 2 s)))
6738 (save-match-data
6739 (if (string-match "\\.[0-9]+" x)
6740 (string-to-number (substring (match-string 0 x) 1)))))))
6741 (if (eq var 'eval)
6742 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6743 (if opt
6744 (setq varform
6745 `(if (equal "" ,var)
6747 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
6748 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6749 (setq s (replace-match "%s" t nil s))
6750 (push varform vars))
6751 (setq vars (nreverse vars))
6752 (with-current-buffer (or org-agenda-buffer (current-buffer))
6753 (setq org-prefix-format-compiled
6754 (list
6755 `((org-prefix-has-time ,org-prefix-has-time)
6756 (org-prefix-has-tag ,org-prefix-has-tag)
6757 (org-prefix-category-length ,org-prefix-category-length)
6758 (org-prefix-has-effort ,org-prefix-has-effort)
6759 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6760 `(format ,s ,@vars))))))
6762 (defun org-set-sorting-strategy (key)
6763 (if (symbolp (car org-agenda-sorting-strategy))
6764 ;; the old format
6765 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6766 (setq org-agenda-sorting-strategy-selected
6767 (or (cdr (assq key org-agenda-sorting-strategy))
6768 (cdr (assq 'agenda org-agenda-sorting-strategy))
6769 '(time-up category-keep priority-down)))))
6771 (defun org-get-time-of-day (s &optional string mod24)
6772 "Check string S for a time of day.
6773 If found, return it as a military time number between 0 and 2400.
6774 If not found, return nil.
6775 The optional STRING argument forces conversion into a 5 character wide string
6776 HH:MM."
6777 (save-match-data
6778 (when
6779 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6780 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6781 (let* ((h (string-to-number (match-string 1 s)))
6782 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6783 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6784 (am-p (equal ampm "am"))
6785 (h1 (cond ((not ampm) h)
6786 ((= h 12) (if am-p 0 12))
6787 (t (+ h (if am-p 0 12)))))
6788 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6789 (mod h1 24) h1))
6790 (t0 (+ (* 100 h2) m))
6791 (t1 (concat (if (>= h1 24) "+" " ")
6792 (if (and org-agenda-time-leading-zero
6793 (< t0 1000)) "0" "")
6794 (if (< t0 100) "0" "")
6795 (if (< t0 10) "0" "")
6796 (int-to-string t0))))
6797 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6799 (defvar org-agenda-before-sorting-filter-function nil
6800 "Function to be applied to agenda items prior to sorting.
6801 Prior to sorting also means just before they are inserted into the agenda.
6803 To aid sorting, you may revisit the original entries and add more text
6804 properties which will later be used by the sorting functions.
6806 The function should take a string argument, an agenda line.
6807 It has access to the text properties in that line, which contain among
6808 other things, the property `org-hd-marker' that points to the entry
6809 where the line comes from. Note that not all lines going into the agenda
6810 have this property, only most.
6812 The function should return the modified string. It is probably best
6813 to ONLY change text properties.
6815 You can also use this function as a filter, by returning nil for lines
6816 you don't want to have in the agenda at all. For this application, you
6817 could bind the variable in the options section of a custom command.")
6819 (defun org-agenda-finalize-entries (list &optional type)
6820 "Sort, limit and concatenate the LIST of agenda items.
6821 The optional argument TYPE tells the agenda type."
6822 (let ((max-effort (cond ((listp org-agenda-max-effort)
6823 (cdr (assoc type org-agenda-max-effort)))
6824 (t org-agenda-max-effort)))
6825 (max-todo (cond ((listp org-agenda-max-todos)
6826 (cdr (assoc type org-agenda-max-todos)))
6827 (t org-agenda-max-todos)))
6828 (max-tags (cond ((listp org-agenda-max-tags)
6829 (cdr (assoc type org-agenda-max-tags)))
6830 (t org-agenda-max-tags)))
6831 (max-entries (cond ((listp org-agenda-max-entries)
6832 (cdr (assoc type org-agenda-max-entries)))
6833 (t org-agenda-max-entries))) l)
6834 (when org-agenda-before-sorting-filter-function
6835 (setq list
6836 (delq nil
6837 (mapcar
6838 org-agenda-before-sorting-filter-function list))))
6839 (setq list (mapcar 'org-agenda-highlight-todo list)
6840 list (mapcar 'identity (sort list 'org-entries-lessp)))
6841 (when max-effort
6842 (setq list (org-agenda-limit-entries
6843 list 'effort-minutes max-effort 'identity)))
6844 (when max-todo
6845 (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
6846 (when max-tags
6847 (setq list (org-agenda-limit-entries list 'tags max-tags)))
6848 (when max-entries
6849 (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
6850 (mapconcat 'identity list "\n")))
6852 (defun org-agenda-limit-entries (list prop limit &optional fn)
6853 "Limit the number of agenda entries."
6854 (let ((include (and limit (< limit 0))))
6855 (if limit
6856 (let ((fun (or fn (lambda (p) (if p 1))))
6857 (lim 0))
6858 (delq nil
6859 (mapcar
6860 (lambda (e)
6861 (let ((pval (funcall fun (get-text-property 1 prop e))))
6862 (if pval (setq lim (+ lim pval)))
6863 (cond ((and pval (<= lim (abs limit))) e)
6864 ((and include (not pval)) e))))
6865 list)))
6866 list)))
6868 (defun org-agenda-limit-interactively ()
6869 "In agenda, interactively limit entries to various maximums."
6870 (interactive)
6871 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6872 (num (string-to-number (read-from-minibuffer "How many? "))))
6873 (cond ((equal max ?e)
6874 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6875 ((equal max ?t)
6876 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6877 ((equal max ?T)
6878 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6879 ((equal max ?E)
6880 (let ((org-agenda-max-effort num)) (org-agenda-redo)))))
6881 (org-agenda-fit-window-to-buffer))
6883 (defun org-agenda-highlight-todo (x)
6884 (let ((org-done-keywords org-done-keywords-for-agenda)
6885 (case-fold-search nil)
6887 (if (eq x 'line)
6888 (save-excursion
6889 (beginning-of-line 1)
6890 (setq re (org-get-at-bol 'org-todo-regexp))
6891 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6892 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6893 (add-text-properties (match-beginning 0) (match-end 1)
6894 (list 'face (org-get-todo-face 1)))
6895 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6896 (delete-region (match-beginning 1) (1- (match-end 0)))
6897 (goto-char (match-beginning 1))
6898 (insert (format org-agenda-todo-keyword-format s)))))
6899 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6900 (setq re (get-text-property 0 'org-todo-regexp x))
6901 (when (and re
6902 ;; Test `pl' because if there's no heading content,
6903 ;; there's no point matching to highlight. Note
6904 ;; that if we didn't test `pl' first, and there
6905 ;; happened to be no keyword from `org-todo-regexp'
6906 ;; on this heading line, then the `equal' comparison
6907 ;; afterwards would spuriously succeed in the case
6908 ;; where `pl' is nil -- causing an args-out-of-range
6909 ;; error when we try to add text properties to text
6910 ;; that isn't there.
6912 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6913 x pl) pl))
6914 (add-text-properties
6915 (or (match-end 1) (match-end 0)) (match-end 0)
6916 (list 'face (org-get-todo-face (match-string 2 x)))
6918 (when (match-end 1)
6919 (setq x (concat (substring x 0 (match-end 1))
6920 (format org-agenda-todo-keyword-format
6921 (match-string 2 x))
6922 (org-add-props " " (text-properties-at 0 x))
6923 (substring x (match-end 3)))))))
6924 x)))
6926 (defsubst org-cmp-priority (a b)
6927 "Compare the priorities of string A and B."
6928 (let ((pa (or (get-text-property 1 'priority a) 0))
6929 (pb (or (get-text-property 1 'priority b) 0)))
6930 (cond ((> pa pb) +1)
6931 ((< pa pb) -1))))
6933 (defsubst org-cmp-effort (a b)
6934 "Compare the effort values of string A and B."
6935 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6936 (ea (or (get-text-property 1 'effort-minutes a) def))
6937 (eb (or (get-text-property 1 'effort-minutes b) def)))
6938 (cond ((> ea eb) +1)
6939 ((< ea eb) -1))))
6941 (defsubst org-cmp-category (a b)
6942 "Compare the string values of categories of strings A and B."
6943 (let ((ca (or (get-text-property 1 'org-category a) ""))
6944 (cb (or (get-text-property 1 'org-category b) "")))
6945 (cond ((string-lessp ca cb) -1)
6946 ((string-lessp cb ca) +1))))
6948 (defsubst org-cmp-todo-state (a b)
6949 "Compare the todo states of strings A and B."
6950 (let* ((ma (or (get-text-property 1 'org-marker a)
6951 (get-text-property 1 'org-hd-marker a)))
6952 (mb (or (get-text-property 1 'org-marker b)
6953 (get-text-property 1 'org-hd-marker b)))
6954 (fa (and ma (marker-buffer ma)))
6955 (fb (and mb (marker-buffer mb)))
6956 (todo-kwds
6957 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6958 (and fb (with-current-buffer fb org-todo-keywords-1))))
6959 (ta (or (get-text-property 1 'todo-state a) ""))
6960 (tb (or (get-text-property 1 'todo-state b) ""))
6961 (la (- (length (member ta todo-kwds))))
6962 (lb (- (length (member tb todo-kwds))))
6963 (donepa (member ta org-done-keywords-for-agenda))
6964 (donepb (member tb org-done-keywords-for-agenda)))
6965 (cond ((and donepa (not donepb)) -1)
6966 ((and (not donepa) donepb) +1)
6967 ((< la lb) -1)
6968 ((< lb la) +1))))
6970 (defsubst org-cmp-alpha (a b)
6971 "Compare the headlines, alphabetically."
6972 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6973 (plb (text-property-any 0 (length b) 'org-heading t b))
6974 (ta (and pla (substring a pla)))
6975 (tb (and plb (substring b plb))))
6976 (when pla
6977 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6978 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6979 (setq ta (substring ta (match-end 0))))
6980 (setq ta (downcase ta)))
6981 (when plb
6982 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6983 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6984 (setq tb (substring tb (match-end 0))))
6985 (setq tb (downcase tb)))
6986 (cond ((not ta) +1)
6987 ((not tb) -1)
6988 ((string-lessp ta tb) -1)
6989 ((string-lessp tb ta) +1))))
6991 (defsubst org-cmp-tag (a b)
6992 "Compare the string values of the first tags of A and B."
6993 (let ((ta (car (last (get-text-property 1 'tags a))))
6994 (tb (car (last (get-text-property 1 'tags b)))))
6995 (cond ((not ta) +1)
6996 ((not tb) -1)
6997 ((string-lessp ta tb) -1)
6998 ((string-lessp tb ta) +1))))
7000 (defsubst org-cmp-time (a b)
7001 "Compare the time-of-day values of strings A and B."
7002 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
7003 (ta (or (get-text-property 1 'time-of-day a) def))
7004 (tb (or (get-text-property 1 'time-of-day b) def)))
7005 (cond ((< ta tb) -1)
7006 ((< tb ta) +1))))
7008 (defsubst org-cmp-ts (a b &optional type)
7009 "Compare the timestamps values of entries A and B.
7010 When TYPE is \"scheduled\", \"deadline\", \"timestamp\" or
7011 \"timestamp_ia\", compare within each of these type. When TYPE
7012 is the empty string, compare all timestamps without respect of
7013 their type."
7014 (let* ((def (if org-sort-agenda-notime-is-late most-positive-fixnum -1))
7015 (ta (or (and (string-match type (or (get-text-property 1 'type a) ""))
7016 (get-text-property 1 'ts-date a)) def))
7017 (tb (or (and (string-match type (or (get-text-property 1 'type b) ""))
7018 (get-text-property 1 'ts-date b)) def)))
7019 (cond ((< ta tb) -1)
7020 ((< tb ta) +1))))
7022 (defsubst org-cmp-habit-p (a b)
7023 "Compare the todo states of strings A and B."
7024 (let ((ha (get-text-property 1 'org-habit-p a))
7025 (hb (get-text-property 1 'org-habit-p b)))
7026 (cond ((and ha (not hb)) -1)
7027 ((and (not ha) hb) +1))))
7029 (defun org-entries-lessp (a b)
7030 "Predicate for sorting agenda entries."
7031 ;; The following variables will be used when the form is evaluated.
7032 ;; So even though the compiler complains, keep them.
7033 (let* ((ss org-agenda-sorting-strategy-selected)
7034 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
7035 (org-cmp-ts a b "")))
7036 (timestamp-down (if timestamp-up (- timestamp-up) nil))
7037 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
7038 (org-cmp-ts a b "scheduled")))
7039 (scheduled-down (if scheduled-up (- scheduled-up) nil))
7040 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
7041 (org-cmp-ts a b "deadline")))
7042 (deadline-down (if deadline-up (- deadline-up) nil))
7043 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
7044 (org-cmp-ts a b "iatimestamp_ia")))
7045 (tsia-down (if tsia-up (- tsia-up) nil))
7046 (ts-up (and (org-em 'ts-up 'ts-down ss)
7047 (org-cmp-ts a b "timestamp")))
7048 (ts-down (if ts-up (- ts-up) nil))
7049 (time-up (and (org-em 'time-up 'time-down ss)
7050 (org-cmp-time a b)))
7051 (time-down (if time-up (- time-up) nil))
7052 (priority-up (and (org-em 'priority-up 'priority-down ss)
7053 (org-cmp-priority a b)))
7054 (priority-down (if priority-up (- priority-up) nil))
7055 (effort-up (and (org-em 'effort-up 'effort-down ss)
7056 (org-cmp-effort a b)))
7057 (effort-down (if effort-up (- effort-up) nil))
7058 (category-up (and (or (org-em 'category-up 'category-down ss)
7059 (memq 'category-keep ss))
7060 (org-cmp-category a b)))
7061 (category-down (if category-up (- category-up) nil))
7062 (category-keep (if category-up +1 nil))
7063 (tag-up (and (org-em 'tag-up 'tag-down ss)
7064 (org-cmp-tag a b)))
7065 (tag-down (if tag-up (- tag-up) nil))
7066 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
7067 (org-cmp-todo-state a b)))
7068 (todo-state-down (if todo-state-up (- todo-state-up) nil))
7069 (habit-up (and (org-em 'habit-up 'habit-down ss)
7070 (org-cmp-habit-p a b)))
7071 (habit-down (if habit-up (- habit-up) nil))
7072 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
7073 (org-cmp-alpha a b)))
7074 (alpha-down (if alpha-up (- alpha-up) nil))
7075 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
7076 user-defined-up user-defined-down)
7077 (if (and need-user-cmp org-agenda-cmp-user-defined
7078 (functionp org-agenda-cmp-user-defined))
7079 (setq user-defined-up
7080 (funcall org-agenda-cmp-user-defined a b)
7081 user-defined-down (if user-defined-up (- user-defined-up) nil)))
7082 (cdr (assoc
7083 (eval (cons 'or org-agenda-sorting-strategy-selected))
7084 '((-1 . t) (1 . nil) (nil . nil))))))
7086 ;;; Agenda restriction lock
7088 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
7089 "Overlay to mark the headline to which agenda commands are restricted.")
7090 (overlay-put org-agenda-restriction-lock-overlay
7091 'face 'org-agenda-restriction-lock)
7092 (overlay-put org-agenda-restriction-lock-overlay
7093 'help-echo "Agendas are currently limited to this subtree.")
7094 (org-detach-overlay org-agenda-restriction-lock-overlay)
7096 ;;;###autoload
7097 (defun org-agenda-set-restriction-lock (&optional type)
7098 "Set restriction lock for agenda, to current subtree or file.
7099 Restriction will be the file if TYPE is `file', or if type is the
7100 universal prefix '(4), or if the cursor is before the first headline
7101 in the file. Otherwise, restriction will be to the current subtree."
7102 (interactive "P")
7103 (and (equal type '(4)) (setq type 'file))
7104 (setq type (cond
7105 (type type)
7106 ((org-at-heading-p) 'subtree)
7107 ((condition-case nil (org-back-to-heading t) (error nil))
7108 'subtree)
7109 (t 'file)))
7110 (if (eq type 'subtree)
7111 (progn
7112 (setq org-agenda-restrict (current-buffer))
7113 (setq org-agenda-overriding-restriction 'subtree)
7114 (put 'org-agenda-files 'org-restrict
7115 (list (buffer-file-name (buffer-base-buffer))))
7116 (org-back-to-heading t)
7117 (move-overlay org-agenda-restriction-lock-overlay
7118 (point)
7119 (if org-agenda-restriction-lock-highlight-subtree
7120 (save-excursion (org-end-of-subtree t t) (point))
7121 (point-at-eol)))
7122 (move-marker org-agenda-restrict-begin (point))
7123 (move-marker org-agenda-restrict-end
7124 (save-excursion (org-end-of-subtree t t)))
7125 (message "Locking agenda restriction to subtree"))
7126 (put 'org-agenda-files 'org-restrict
7127 (list (buffer-file-name (buffer-base-buffer))))
7128 (setq org-agenda-restrict nil)
7129 (setq org-agenda-overriding-restriction 'file)
7130 (move-marker org-agenda-restrict-begin nil)
7131 (move-marker org-agenda-restrict-end nil)
7132 (message "Locking agenda restriction to file"))
7133 (setq current-prefix-arg nil)
7134 (org-agenda-maybe-redo))
7136 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7137 "Remove the agenda restriction lock."
7138 (interactive "P")
7139 (org-detach-overlay org-agenda-restriction-lock-overlay)
7140 (org-detach-overlay org-speedbar-restriction-lock-overlay)
7141 (setq org-agenda-overriding-restriction nil)
7142 (setq org-agenda-restrict nil)
7143 (put 'org-agenda-files 'org-restrict nil)
7144 (move-marker org-agenda-restrict-begin nil)
7145 (move-marker org-agenda-restrict-end nil)
7146 (setq current-prefix-arg nil)
7147 (message "Agenda restriction lock removed")
7148 (or noupdate (org-agenda-maybe-redo)))
7150 (defun org-agenda-maybe-redo ()
7151 "If there is any window showing the agenda view, update it."
7152 (let ((w (get-buffer-window org-agenda-buffer-name t))
7153 (w0 (selected-window)))
7154 (when w
7155 (select-window w)
7156 (org-agenda-redo)
7157 (select-window w0)
7158 (if org-agenda-overriding-restriction
7159 (message "Agenda view shifted to new %s restriction"
7160 org-agenda-overriding-restriction)
7161 (message "Agenda restriction lock removed")))))
7163 ;;; Agenda commands
7165 (defun org-agenda-check-type (error &rest types)
7166 "Check if agenda buffer is of allowed type.
7167 If ERROR is non-nil, throw an error, otherwise just return nil.
7168 Allowed types are 'agenda 'timeline 'todo 'tags 'search."
7169 (if (not org-agenda-type)
7170 (error "No Org agenda currently displayed")
7171 (if (memq org-agenda-type types)
7173 (if error
7174 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
7175 nil))))
7177 (defun org-agenda-Quit ()
7178 "Exit the agenda and kill buffers loaded by `org-agenda'.
7179 Also restore the window configuration."
7180 (interactive)
7181 (if org-agenda-columns-active
7182 (org-columns-quit)
7183 (let ((buf (current-buffer)))
7184 (if (eq org-agenda-window-setup 'other-frame)
7185 (progn
7186 (org-agenda-reset-markers)
7187 (kill-buffer buf)
7188 (org-columns-remove-overlays)
7189 (setq org-agenda-archives-mode nil)
7190 (delete-frame))
7191 (and (not (eq org-agenda-window-setup 'current-window))
7192 (not (one-window-p))
7193 (delete-window))
7194 (org-agenda-reset-markers)
7195 (kill-buffer buf)
7196 (org-columns-remove-overlays)
7197 (setq org-agenda-archives-mode nil)))
7198 (setq org-agenda-buffer nil)
7199 ;; Maybe restore the pre-agenda window configuration.
7200 (and org-agenda-restore-windows-after-quit
7201 (not (eq org-agenda-window-setup 'other-frame))
7202 org-agenda-pre-window-conf
7203 (set-window-configuration org-agenda-pre-window-conf)
7204 (setq org-agenda-pre-window-conf nil))))
7206 (defun org-agenda-quit ()
7207 "Exit the agenda and restore the window configuration.
7208 When `org-agenda-sticky' is non-nil, only bury the agenda."
7209 (interactive)
7210 (if (and (eq org-indirect-buffer-display 'other-window)
7211 org-last-indirect-buffer)
7212 (let ((org-last-indirect-window
7213 (get-buffer-window org-last-indirect-buffer)))
7214 (if org-last-indirect-window
7215 (delete-window org-last-indirect-window))))
7216 (if org-agenda-columns-active
7217 (org-columns-quit)
7218 (if org-agenda-sticky
7219 (let ((buf (current-buffer)))
7220 (if (eq org-agenda-window-setup 'other-frame)
7221 (progn
7222 (delete-frame))
7223 (and (not (eq org-agenda-window-setup 'current-window))
7224 (not (one-window-p))
7225 (delete-window)))
7226 (with-current-buffer buf
7227 (bury-buffer)
7228 ;; Maybe restore the pre-agenda window configuration.
7229 (and org-agenda-restore-windows-after-quit
7230 (not (eq org-agenda-window-setup 'other-frame))
7231 org-agenda-pre-window-conf
7232 (set-window-configuration org-agenda-pre-window-conf)
7233 (setq org-agenda-pre-window-conf nil))))
7234 (org-agenda-Quit))))
7236 (defun org-agenda-exit ()
7237 "Exit the agenda and restore the window configuration.
7238 Also kill Org-mode buffers loaded by `org-agenda'. Org-mode
7239 buffers visited directly by the user will not be touched."
7240 (interactive)
7241 (org-release-buffers org-agenda-new-buffers)
7242 (setq org-agenda-new-buffers nil)
7243 (org-agenda-Quit))
7245 (defun org-agenda-kill-all-agenda-buffers ()
7246 "Kill all buffers in `org-agenda-mode'.
7247 This is used when toggling sticky agendas.
7248 You can also explicitly invoke it with `C-c a C-k'."
7249 (interactive)
7250 (let (blist)
7251 (dolist (buf (buffer-list))
7252 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7253 (push buf blist)))
7254 (mapc 'kill-buffer blist)))
7256 (defun org-agenda-execute (arg)
7257 "Execute another agenda command, keeping same window.
7258 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7259 in the agenda."
7260 (interactive "P")
7261 (let ((org-agenda-window-setup 'current-window))
7262 (org-agenda arg)))
7264 (defun org-agenda-redo (&optional all)
7265 "Rebuild possibly ALL agenda view(s) in the current buffer."
7266 (interactive "P")
7267 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7268 (cpa (unless (eq all t) current-prefix-arg))
7269 (org-agenda-doing-sticky-redo org-agenda-sticky)
7270 (org-agenda-sticky nil)
7271 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7272 org-agenda-buffer-name))
7273 (org-agenda-keep-modes t)
7274 (tag-filter org-agenda-tag-filter)
7275 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7276 (top-hl-filter org-agenda-top-headline-filter)
7277 (cat-filter org-agenda-category-filter)
7278 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7279 (re-filter org-agenda-regexp-filter)
7280 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7281 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7282 (cols org-agenda-columns-active)
7283 (line (org-current-line))
7284 (window-line (- line (org-current-line (window-start))))
7285 (lprops (get 'org-agenda-redo-command 'org-lprops))
7286 (redo-cmd (get-text-property p 'org-redo-cmd))
7287 (last-args (get-text-property p 'org-last-args))
7288 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7289 (org-agenda-overriding-cmd-arguments
7290 (unless (eq all t)
7291 (cond ((listp last-args)
7292 (cons (or cpa (car last-args)) (cdr last-args)))
7293 ((stringp last-args)
7294 last-args))))
7295 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7296 (put 'org-agenda-tag-filter :preset-filter nil)
7297 (put 'org-agenda-category-filter :preset-filter nil)
7298 (put 'org-agenda-regexp-filter :preset-filter nil)
7299 (and cols (org-columns-quit))
7300 (message "Rebuilding agenda buffer...")
7301 (if series-redo-cmd
7302 (eval series-redo-cmd)
7303 (org-let lprops redo-cmd))
7304 (setq org-agenda-undo-list nil
7305 org-agenda-pending-undo-list nil
7306 org-agenda-tag-filter tag-filter
7307 org-agenda-category-filter cat-filter
7308 org-agenda-regexp-filter re-filter
7309 org-agenda-top-headline-filter top-hl-filter)
7310 (message "Rebuilding agenda buffer...done")
7311 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7312 (put 'org-agenda-category-filter :preset-filter cat-preset)
7313 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7314 (and (or tag-filter tag-preset) (org-agenda-filter-apply tag-filter 'tag))
7315 (and (or cat-filter cat-preset) (org-agenda-filter-apply cat-filter 'category))
7316 (and (or re-filter re-preset) (org-agenda-filter-apply re-filter 'regexp))
7317 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7318 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
7319 (org-goto-line line)
7320 (recenter window-line)))
7322 (defvar org-global-tags-completion-table nil)
7323 (defvar org-agenda-filter-form nil)
7324 (defvar org-agenda-filtered-by-category nil)
7326 (defun org-agenda-filter-by-category (strip)
7327 "Keep only those lines in the agenda buffer that have a specific category.
7328 The category is that of the current line."
7329 (interactive "P")
7330 (if (and org-agenda-filtered-by-category
7331 org-agenda-category-filter)
7332 (org-agenda-filter-show-all-cat)
7333 (let ((cat (org-no-properties (get-text-property (point) 'org-category))))
7334 (cond
7335 ((and cat strip)
7336 (org-agenda-filter-apply
7337 (push (concat "-" cat) org-agenda-category-filter) 'category))
7338 ((and cat)
7339 (org-agenda-filter-apply
7340 (setq org-agenda-category-filter
7341 (list (concat "+" cat))) 'category))
7342 ((error "No category at point"))))))
7344 (defun org-find-top-headline (&optional pos)
7345 "Find the topmost parent headline and return it."
7346 (save-excursion
7347 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
7348 (if pos (goto-char pos))
7349 ;; Skip up to the topmost parent
7350 (while (ignore-errors (outline-up-heading 1) t))
7351 (ignore-errors
7352 (nth 4 (org-heading-components))))))
7354 (defvar org-agenda-filtered-by-top-headline nil)
7355 (defun org-agenda-filter-by-top-headline (strip)
7356 "Keep only those lines that are descendants from the same top headline.
7357 The top headline is that of the current line."
7358 (interactive "P")
7359 (if org-agenda-filtered-by-top-headline
7360 (progn
7361 (setq org-agenda-filtered-by-top-headline nil
7362 org-agenda-top-headline-filter nil)
7363 (org-agenda-filter-show-all-cat))
7364 (let ((cat (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7365 (if cat (org-agenda-filter-top-headline-apply cat strip)
7366 (error "No top-level category at point")))))
7368 (defvar org-agenda-regexp-filter nil)
7369 (defun org-agenda-filter-by-regexp (strip)
7370 "Filter agenda entries by a regular expression.
7371 Regexp filters are cumulative.
7372 With no prefix argument, keep entries matching the regexp.
7373 With one prefix argument, filter out entries matching the regexp.
7374 With two prefix arguments, remove the regexp filters."
7375 (interactive "P")
7376 (if (not (equal strip '(16)))
7377 (let ((flt (concat (if (equal strip '(4)) "-" "+")
7378 (read-from-minibuffer
7379 (if (equal strip '(4))
7380 "Filter out entries matching regexp: "
7381 "Narrow to entries matching regexp: ")))))
7382 (push flt org-agenda-regexp-filter)
7383 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
7384 (org-agenda-filter-show-all-re)
7385 (message "Regexp filter removed")))
7387 (defun org-agenda-filter-remove-all ()
7388 "Remove all filters from the current agenda buffer."
7389 (interactive)
7390 (when org-agenda-tag-filter
7391 (org-agenda-filter-show-all-tag))
7392 (when org-agenda-category-filter
7393 (org-agenda-filter-show-all-cat))
7394 (when org-agenda-regexp-filter
7395 (org-agenda-filter-show-all-re)))
7397 (defun org-agenda-filter-by-tag (strip &optional char narrow)
7398 "Keep only those lines in the agenda buffer that have a specific tag.
7399 The tag is selected with its fast selection letter, as configured.
7400 With prefix argument STRIP, remove all lines that do have the tag.
7401 A lisp caller can specify CHAR. NARROW means that the new tag should be
7402 used to narrow the search - the interactive user can also press `-' or `+'
7403 to switch to narrowing."
7404 (interactive "P")
7405 (let* ((alist org-tag-alist-for-agenda)
7406 (tag-chars (mapconcat
7407 (lambda (x) (if (and (not (symbolp (car x)))
7408 (cdr x))
7409 (char-to-string (cdr x))
7410 ""))
7411 alist ""))
7412 (efforts (org-split-string
7413 (or (cdr (assoc (concat org-effort-property "_ALL")
7414 org-global-properties))
7415 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
7416 "")))
7417 (effort-op org-agenda-filter-effort-default-operator)
7418 (effort-prompt "")
7419 (inhibit-read-only t)
7420 (current org-agenda-tag-filter)
7421 maybe-refresh a n tag)
7422 (unless char
7423 (message
7424 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
7425 (if narrow "Narrow" "Filter") tag-chars
7426 (if org-agenda-auto-exclude-function "[RET], " ""))
7427 (setq char (read-char-exclusive)))
7428 (when (member char '(?+ ?-))
7429 ;; Narrowing down
7430 (cond ((equal char ?-) (setq strip t narrow t))
7431 ((equal char ?+) (setq strip nil narrow t)))
7432 (message
7433 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
7434 (setq char (read-char-exclusive)))
7435 (when (member char '(?< ?> ?= ??))
7436 ;; An effort operator
7437 (setq effort-op (char-to-string char))
7438 (setq alist nil) ; to make sure it will be interpreted as effort.
7439 (unless (equal char ??)
7440 (loop for i from 0 to 9 do
7441 (setq effort-prompt
7442 (concat
7443 effort-prompt " ["
7444 (if (= i 9) "0" (int-to-string (1+ i)))
7445 "]" (nth i efforts))))
7446 (message "Effort%s: %s " effort-op effort-prompt)
7447 (setq char (read-char-exclusive))
7448 (when (or (< char ?0) (> char ?9))
7449 (error "Need 1-9,0 to select effort"))))
7450 (when (equal char ?\t)
7451 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7452 (org-set-local 'org-global-tags-completion-table
7453 (org-global-tags-completion-table)))
7454 (let ((completion-ignore-case t))
7455 (setq tag (org-icompleting-read
7456 "Tag: " org-global-tags-completion-table))))
7457 (cond
7458 ((equal char ?\r)
7459 (org-agenda-filter-show-all-tag)
7460 (when org-agenda-auto-exclude-function
7461 (setq org-agenda-tag-filter nil)
7462 (dolist (tag (org-agenda-get-represented-tags))
7463 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7464 (if modifier
7465 (push modifier org-agenda-tag-filter))))
7466 (if (not (null org-agenda-tag-filter))
7467 (org-agenda-filter-apply org-agenda-tag-filter 'tag)))
7468 (setq maybe-refresh t))
7469 ((equal char ?/)
7470 (org-agenda-filter-show-all-tag)
7471 (when (get 'org-agenda-tag-filter :preset-filter)
7472 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
7473 (setq maybe-refresh t))
7474 ((equal char ?. )
7475 (setq org-agenda-tag-filter
7476 (mapcar (lambda(tag) (concat "+" tag))
7477 (org-get-at-bol 'tags)))
7478 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
7479 (setq maybe-refresh t))
7480 ((or (equal char ?\ )
7481 (setq a (rassoc char alist))
7482 (and (>= char ?0) (<= char ?9)
7483 (setq n (if (= char ?0) 9 (- char ?0 1))
7484 tag (concat effort-op (nth n efforts))
7485 a (cons tag nil)))
7486 (and (= char ??)
7487 (setq tag "?eff")
7488 a (cons tag nil))
7489 (and tag (setq a (cons tag nil))))
7490 (org-agenda-filter-show-all-tag)
7491 (setq tag (car a))
7492 (setq org-agenda-tag-filter
7493 (cons (concat (if strip "-" "+") tag)
7494 (if narrow current nil)))
7495 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
7496 (setq maybe-refresh t))
7497 (t (error "Invalid tag selection character %c" char)))
7498 (when (and maybe-refresh
7499 (eq org-agenda-clockreport-mode 'with-filter))
7500 (org-agenda-redo))))
7502 (defun org-agenda-get-represented-tags ()
7503 "Get a list of all tags currently represented in the agenda."
7504 (let (p tags)
7505 (save-excursion
7506 (goto-char (point-min))
7507 (while (setq p (next-single-property-change (point) 'tags))
7508 (goto-char p)
7509 (mapc (lambda (x) (add-to-list 'tags x))
7510 (get-text-property (point) 'tags))))
7511 tags))
7513 (defun org-agenda-filter-by-tag-refine (strip &optional char)
7514 "Refine the current filter. See `org-agenda-filter-by-tag'."
7515 (interactive "P")
7516 (org-agenda-filter-by-tag strip char 'refine))
7518 (defun org-agenda-filter-make-matcher (filter type)
7519 "Create the form that tests a line for agenda filter."
7520 (let (f f1)
7521 (cond
7522 ;; Tag filter
7523 ((eq type 'tag)
7524 (setq filter
7525 (delete-dups
7526 (append (get 'org-agenda-tag-filter :preset-filter)
7527 filter)))
7528 (dolist (x filter)
7529 (let ((nfilter (org-agenda-filter-expand-tags filter)) nf nf1
7530 (ffunc
7531 (lambda (nf0 nf01 fltr notgroup op)
7532 (dolist (x fltr)
7533 (if (member x '("-" "+"))
7534 (setq nf01 (if (equal x "-") 'tags '(not tags)))
7535 (if (string-match "[<=>?]" x)
7536 (setq nf01 (org-agenda-filter-effort-form x))
7537 (setq nf01 (list 'member (downcase (substring x 1))
7538 'tags)))
7539 (when (equal (string-to-char x) ?-)
7540 (setq nf01 (list 'not nf01))
7541 (when (not notgroup) (setq op 'and))))
7542 (push nf01 nf0))
7543 (if notgroup
7544 (push (cons 'and nf0) f)
7545 (push (cons (or op 'or) nf0) f)))))
7546 (cond ((equal filter '("+"))
7547 (setq f (list (list 'not 'tags))))
7548 ((equal nfilter filter)
7549 (funcall ffunc f1 f filter t nil))
7550 (t (funcall ffunc nf1 nf nfilter nil nil))))))
7551 ;; Category filter
7552 ((eq type 'category)
7553 (setq filter
7554 (delete-dups
7555 (append (get 'org-agenda-category-filter :preset-filter)
7556 filter)))
7557 (dolist (x filter)
7558 (if (equal "-" (substring x 0 1))
7559 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7560 (setq f1 (list 'equal (substring x 1) 'cat)))
7561 (push f1 f)))
7562 ;; Regexp filter
7563 ((eq type 'regexp)
7564 (setq filter
7565 (delete-dups
7566 (append (get 'org-agenda-regexp-filter :preset-filter)
7567 filter)))
7568 (dolist (x filter)
7569 (if (equal "-" (substring x 0 1))
7570 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7571 (setq f1 (list 'string-match (substring x 1) 'txt)))
7572 (push f1 f))))
7573 (cons 'and (nreverse f))))
7575 (defun org-agenda-filter-effort-form (e)
7576 "Return the form to compare the effort of the current line with what E says.
7577 E looks like \"+<2:25\"."
7578 (let (op)
7579 (setq e (substring e 1))
7580 (setq op (string-to-char e) e (substring e 1))
7581 (setq op (cond ((equal op ?<) '<=)
7582 ((equal op ?>) '>=)
7583 ((equal op ??) op)
7584 (t '=)))
7585 (list 'org-agenda-compare-effort (list 'quote op)
7586 (org-duration-string-to-minutes e))))
7588 (defun org-agenda-compare-effort (op value)
7589 "Compare the effort of the current line with VALUE, using OP.
7590 If the line does not have an effort defined, return nil."
7591 (let ((eff (org-get-at-bol 'effort-minutes)))
7592 (if (equal op ??)
7593 (not eff)
7594 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
7595 value))))
7597 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7598 "Expand group tags in FILTER for the agenda.
7599 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7600 (if org-group-tags
7601 (let ((case-fold-search t) rtn)
7602 (mapc
7603 (lambda (f)
7604 (let (f0 dir)
7605 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7606 (setq dir (match-string 1 f) f0 (match-string 2 f))
7607 (setq dir (if no-operator "" "+") f0 f))
7608 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7609 (org-tags-expand f0 t t))
7610 rtn))))
7611 filter)
7612 (reverse rtn))
7613 filter))
7615 (defun org-agenda-filter-apply (filter type)
7616 "Set FILTER as the new agenda filter and apply it."
7617 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7618 (if org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7619 (let (tags cat txt)
7620 (setq org-agenda-filter-form
7621 (org-agenda-filter-make-matcher filter type))
7622 (if (and (eq type 'category)
7623 (not (equal (substring (car filter) 0 1) "-")))
7624 ;; Only set `org-agenda-filtered-by-category' to t
7625 ;; when a unique category is used as the filter
7626 (setq org-agenda-filtered-by-category t))
7627 (org-agenda-set-mode-name)
7628 (save-excursion
7629 (goto-char (point-min))
7630 (while (not (eobp))
7631 (if (org-get-at-bol 'org-marker)
7632 (progn
7633 (setq tags ; used in eval
7634 (apply 'append
7635 (mapcar (lambda (f)
7636 (org-agenda-filter-expand-tags (list f) t))
7637 (org-get-at-bol 'tags)))
7638 cat (get-text-property (point) 'org-category)
7639 txt (get-text-property (point) 'txt))
7640 (if (not (eval org-agenda-filter-form))
7641 (org-agenda-filter-hide-line type))
7642 (beginning-of-line 2))
7643 (beginning-of-line 2))))
7644 (if (get-char-property (point) 'invisible)
7645 (ignore-errors (org-agenda-previous-line)))))
7647 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7648 "Filter by top headline HL."
7649 (org-agenda-set-mode-name)
7650 (save-excursion
7651 (goto-char (point-min))
7652 (while (not (eobp))
7653 (let* ((pos (org-get-at-bol 'org-hd-marker))
7654 (tophl (and pos (org-find-top-headline pos))))
7655 (if (and tophl (funcall (if negative 'identity 'not)
7656 (string= hl tophl)))
7657 (org-agenda-filter-hide-line 'category)))
7658 (beginning-of-line 2)))
7659 (if (get-char-property (point) 'invisible)
7660 (org-agenda-previous-line))
7661 (setq org-agenda-top-headline-filter hl
7662 org-agenda-filtered-by-top-headline t))
7664 (defun org-agenda-filter-hide-line (type)
7665 "Hide lines with TYPE in the agenda buffer."
7666 (let (ov)
7667 (setq ov (make-overlay (max (point-min) (point-at-bol))
7668 (1+ (point-at-eol))))
7669 (overlay-put ov 'invisible t)
7670 (overlay-put ov 'type type)
7671 (cond ((eq type 'tag) (push ov org-agenda-tag-filter-overlays))
7672 ((eq type 'category) (push ov org-agenda-cat-filter-overlays))
7673 ((eq type 'regexp) (push ov org-agenda-re-filter-overlays)))))
7675 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
7676 (setq pos (or pos (point)))
7677 (save-excursion
7678 (dolist (ov (overlays-at pos))
7679 (when (and (overlay-get ov 'invisible)
7680 (eq (overlay-get ov 'type) 'tag))
7681 (goto-char pos)
7682 (if (< (overlay-start ov) (point-at-eol))
7683 (move-overlay ov (point-at-eol)
7684 (overlay-end ov)))))))
7686 (defun org-agenda-filter-show-all-tag nil
7687 "Remove tag filter overlays from the agenda buffer."
7688 (mapc 'delete-overlay org-agenda-tag-filter-overlays)
7689 (setq org-agenda-tag-filter-overlays nil
7690 org-agenda-tag-filter nil
7691 org-agenda-filter-form nil)
7692 (org-agenda-set-mode-name))
7694 (defun org-agenda-filter-show-all-re nil
7695 "Remove regexp filter overlays from the agenda buffer."
7696 (mapc 'delete-overlay org-agenda-re-filter-overlays)
7697 (setq org-agenda-re-filter-overlays nil
7698 org-agenda-regexp-filter nil
7699 org-agenda-filter-form nil)
7700 (org-agenda-set-mode-name))
7702 (defun org-agenda-filter-show-all-cat nil
7703 "Remove category filter overlays from the agenda buffer."
7704 (mapc 'delete-overlay org-agenda-cat-filter-overlays)
7705 (setq org-agenda-cat-filter-overlays nil
7706 org-agenda-filtered-by-category nil
7707 org-agenda-category-filter nil
7708 org-agenda-filter-form nil)
7709 (org-agenda-set-mode-name))
7711 (defun org-agenda-manipulate-query-add ()
7712 "Manipulate the query by adding a search term with positive selection.
7713 Positive selection means the term must be matched for selection of an entry."
7714 (interactive)
7715 (org-agenda-manipulate-query ?\[))
7716 (defun org-agenda-manipulate-query-subtract ()
7717 "Manipulate the query by adding a search term with negative selection.
7718 Negative selection means term must not be matched for selection of an entry."
7719 (interactive)
7720 (org-agenda-manipulate-query ?\]))
7721 (defun org-agenda-manipulate-query-add-re ()
7722 "Manipulate the query by adding a search regexp with positive selection.
7723 Positive selection means the regexp must match for selection of an entry."
7724 (interactive)
7725 (org-agenda-manipulate-query ?\{))
7726 (defun org-agenda-manipulate-query-subtract-re ()
7727 "Manipulate the query by adding a search regexp with negative selection.
7728 Negative selection means regexp must not match for selection of an entry."
7729 (interactive)
7730 (org-agenda-manipulate-query ?\}))
7731 (defun org-agenda-manipulate-query (char)
7732 (cond
7733 ((memq org-agenda-type '(timeline agenda))
7734 (let ((org-agenda-include-inactive-timestamps t))
7735 (org-agenda-redo))
7736 (message "Display now includes inactive timestamps as well"))
7737 ((eq org-agenda-type 'search)
7738 (org-add-to-string
7739 'org-agenda-query-string
7740 (if org-agenda-last-search-view-search-was-boolean
7741 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7742 (?\{ . " +{}") (?\} . " -{}"))))
7743 " "))
7744 (setq org-agenda-redo-command
7745 (list 'org-search-view
7746 (car (get-text-property (min (1- (point-max)) (point))
7747 'org-last-args))
7748 org-agenda-query-string
7749 (+ (length org-agenda-query-string)
7750 (if (member char '(?\{ ?\})) 0 1))))
7751 (set-register org-agenda-query-register org-agenda-query-string)
7752 (let ((org-agenda-overriding-arguments
7753 (cdr org-agenda-redo-command)))
7754 (org-agenda-redo)))
7755 (t (error "Cannot manipulate query for %s-type agenda buffers"
7756 org-agenda-type))))
7758 (defun org-add-to-string (var string)
7759 (set var (concat (symbol-value var) string)))
7761 (defun org-agenda-goto-date (span)
7762 "Jump to DATE in agenda."
7763 (interactive "P")
7764 (let* ((org-read-date-prefer-future
7765 (eval org-agenda-jump-prefer-future))
7766 (date (org-read-date))
7767 (day (time-to-days (org-time-string-to-time date)))
7768 (org-agenda-sticky-orig org-agenda-sticky)
7769 (org-agenda-buffer-tmp-name (buffer-name))
7770 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7771 (0-arg (or current-prefix-arg (car args)))
7772 (2-arg (nth 2 args))
7773 (with-hour-p (nth 4 org-agenda-redo-command))
7774 (newcmd (list 'org-agenda-list 0-arg date
7775 (org-agenda-span-to-ndays
7776 2-arg (org-time-string-to-absolute date))
7777 with-hour-p))
7778 (newargs (cdr newcmd))
7779 (inhibit-read-only t)
7780 org-agenda-sticky)
7781 (if (not (org-agenda-check-type t 'agenda))
7782 (error "Not available in non-agenda views")
7783 (add-text-properties (point-min) (point-max)
7784 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7785 (org-agenda-redo)
7786 (goto-char (point-min))
7787 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7788 (save-excursion (move-beginning-of-line 2) (eobp))))
7789 (move-beginning-of-line 2))
7790 (setq org-agenda-sticky org-agenda-sticky-orig
7791 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7793 (defun org-agenda-goto-today ()
7794 "Go to today."
7795 (interactive)
7796 (org-agenda-check-type t 'timeline 'agenda)
7797 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7798 (curspan (nth 2 args))
7799 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7800 (cond
7801 (tdpos (goto-char tdpos))
7802 ((eq org-agenda-type 'agenda)
7803 (let* ((sd (org-agenda-compute-starting-span
7804 (org-today) (or curspan org-agenda-ndays org-agenda-span)))
7805 (org-agenda-overriding-arguments args))
7806 (setf (nth 1 org-agenda-overriding-arguments) sd)
7807 (org-agenda-redo)
7808 (org-agenda-find-same-or-today-or-agenda)))
7809 (t (error "Cannot find today")))))
7811 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7812 (goto-char
7813 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7814 (text-property-any (point-min) (point-max) 'org-today t)
7815 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7816 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7817 (org-agenda-backward-block))
7818 (point-min))))
7820 (defun org-agenda-backward-block ()
7821 "Move backward by one agenda block."
7822 (interactive)
7823 (org-agenda-forward-block 'backward))
7825 (defun org-agenda-forward-block (&optional backward)
7826 "Move forward by one agenda block.
7827 When optional argument BACKWARD is set, go backward"
7828 (interactive)
7829 (cond ((not (derived-mode-p 'org-agenda-mode))
7830 (user-error
7831 "Cannot execute this command outside of org-agenda-mode buffers"))
7832 ((looking-at (if backward "\\`" "\\'"))
7833 (message "Already at the %s block" (if backward "first" "last")))
7834 (t (let ((pos (prog1 (point)
7835 (ignore-errors (if backward (backward-char 1)
7836 (move-end-of-line 1)))))
7837 (f (if backward
7838 'previous-single-property-change
7839 'next-single-property-change))
7840 moved dest)
7841 (while (and (setq dest (funcall
7842 f (point) 'org-agenda-structural-header))
7843 (not (get-text-property
7844 (point) 'org-agenda-structural-header)))
7845 (setq moved t)
7846 (goto-char dest))
7847 (if moved (move-beginning-of-line 1)
7848 (goto-char (if backward (point-min) (point-max)))
7849 (move-beginning-of-line 1)
7850 (message "No %s block" (if backward "previous" "further")))))))
7852 (defun org-agenda-later (arg)
7853 "Go forward in time by the current span.
7854 With prefix ARG, go forward that many times the current span."
7855 (interactive "p")
7856 (org-agenda-check-type t 'agenda)
7857 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7858 (span (or (nth 2 args) org-agenda-current-span))
7859 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7860 (greg (calendar-gregorian-from-absolute sd))
7861 (cnt (org-get-at-bol 'org-day-cnt))
7862 greg2)
7863 (cond
7864 ((numberp span)
7865 (setq sd (+ (* span arg) sd)))
7866 ((eq span 'day)
7867 (setq sd (+ arg sd)))
7868 ((eq span 'week)
7869 (setq sd (+ (* 7 arg) sd)))
7870 ((eq span 'fortnight)
7871 (setq sd (+ (* 14 arg) sd)))
7872 ((eq span 'month)
7873 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7874 sd (calendar-absolute-from-gregorian greg2))
7875 (setcar greg2 (1+ (car greg2))))
7876 ((eq span 'year)
7877 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7878 sd (calendar-absolute-from-gregorian greg2))
7879 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7881 (setq sd (+ (* span arg) sd))))
7882 (let ((org-agenda-overriding-cmd
7883 ;; `cmd' may have been set by `org-agenda-run-series' which
7884 ;; uses `org-agenda-overriding-cmd' to decide whether
7885 ;; overriding is allowed for `cmd'
7886 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7887 (org-agenda-overriding-arguments
7888 (list (car args) sd span)))
7889 (org-agenda-redo)
7890 (org-agenda-find-same-or-today-or-agenda cnt))))
7892 (defun org-agenda-earlier (arg)
7893 "Go backward in time by the current span.
7894 With prefix ARG, go backward that many times the current span."
7895 (interactive "p")
7896 (org-agenda-later (- arg)))
7898 (defun org-agenda-view-mode-dispatch ()
7899 "Call one of the view mode commands."
7900 (interactive)
7901 (message "View: [d]ay [w]eek for[t]night [m]onth [y]ear [SPC]reset [q]uit/abort
7902 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7903 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7904 (let ((a (read-char-exclusive)))
7905 (case a
7906 (?\ (call-interactively 'org-agenda-reset-view))
7907 (?d (call-interactively 'org-agenda-day-view))
7908 (?w (call-interactively 'org-agenda-week-view))
7909 (?t (call-interactively 'org-agenda-fortnight-view))
7910 (?m (call-interactively 'org-agenda-month-view))
7911 (?y (call-interactively 'org-agenda-year-view))
7912 (?l (call-interactively 'org-agenda-log-mode))
7913 (?L (org-agenda-log-mode '(4)))
7914 (?c (org-agenda-log-mode 'clockcheck))
7915 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
7916 (?a (call-interactively 'org-agenda-archives-mode))
7917 (?A (org-agenda-archives-mode 'files))
7918 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7919 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7920 (?G (call-interactively 'org-agenda-toggle-time-grid))
7921 (?D (call-interactively 'org-agenda-toggle-diary))
7922 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7923 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7924 (org-agenda-check-type t 'timeline 'agenda)
7925 (org-agenda-redo))
7926 (message "Display now includes inactive timestamps as well"))
7927 (?q (message "Abort"))
7928 (otherwise (error "Invalid key" )))))
7930 (defun org-agenda-reset-view ()
7931 "Switch to default view for agenda."
7932 (interactive)
7933 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
7934 (defun org-agenda-day-view (&optional day-of-month)
7935 "Switch to daily view for agenda.
7936 With argument DAY-OF-MONTH, switch to that day of the month."
7937 (interactive "P")
7938 (org-agenda-change-time-span 'day day-of-month))
7939 (defun org-agenda-week-view (&optional iso-week)
7940 "Switch to daily view for agenda.
7941 With argument ISO-WEEK, switch to the corresponding ISO week.
7942 If ISO-WEEK has more then 2 digits, only the last two encode the
7943 week. Any digits before this encode a year. So 200712 means
7944 week 12 of year 2007. Years in the range 1938-2037 can also be
7945 written as 2-digit years."
7946 (interactive "P")
7947 (org-agenda-change-time-span 'week iso-week))
7948 (defun org-agenda-fortnight-view (&optional iso-week)
7949 "Switch to daily view for agenda.
7950 With argument ISO-WEEK, switch to the corresponding ISO week.
7951 If ISO-WEEK has more then 2 digits, only the last two encode the
7952 week. Any digits before this encode a year. So 200712 means
7953 week 12 of year 2007. Years in the range 1938-2037 can also be
7954 written as 2-digit years."
7955 (interactive "P")
7956 (org-agenda-change-time-span 'fortnight iso-week))
7957 (defun org-agenda-month-view (&optional month)
7958 "Switch to monthly view for agenda.
7959 With argument MONTH, switch to that month."
7960 (interactive "P")
7961 (org-agenda-change-time-span 'month month))
7962 (defun org-agenda-year-view (&optional year)
7963 "Switch to yearly view for agenda.
7964 With argument YEAR, switch to that year.
7965 If MONTH has more then 2 digits, only the last two encode the
7966 month. Any digits before this encode a year. So 200712 means
7967 December year 2007. Years in the range 1938-2037 can also be
7968 written as 2-digit years."
7969 (interactive "P")
7970 (when year
7971 (setq year (org-small-year-to-year year)))
7972 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
7973 (org-agenda-change-time-span 'year year)
7974 (error "Abort")))
7976 (defun org-agenda-change-time-span (span &optional n)
7977 "Change the agenda view to SPAN.
7978 SPAN may be `day', `week', `fortnight', `month', `year'."
7979 (org-agenda-check-type t 'agenda)
7980 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7981 (curspan (nth 2 args)))
7982 (if (and (not n) (equal curspan span))
7983 (error "Viewing span is already \"%s\"" span))
7984 (let* ((sd (or (org-get-at-bol 'day)
7985 (nth 1 args)
7986 org-starting-day))
7987 (sd (org-agenda-compute-starting-span sd span n))
7988 (org-agenda-overriding-cmd
7989 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7990 (org-agenda-overriding-arguments
7991 (list (car args) sd span)))
7992 (org-agenda-redo)
7993 (org-agenda-find-same-or-today-or-agenda))
7994 (org-agenda-set-mode-name)
7995 (message "Switched to %s view" span)))
7997 (defun org-agenda-compute-starting-span (sd span &optional n)
7998 "Compute starting date for agenda.
7999 SPAN may be `day', `week', `fortnight', `month', `year'. The return value
8000 is a cons cell with the starting date and the number of days,
8001 so that the date SD will be in that range."
8002 (let* ((greg (calendar-gregorian-from-absolute sd))
8003 (dg (nth 1 greg))
8004 (mg (car greg))
8005 (yg (nth 2 greg)))
8006 (cond
8007 ((eq span 'day)
8008 (when n
8009 (setq sd (+ (calendar-absolute-from-gregorian
8010 (list mg 1 yg))
8011 n -1))))
8012 ((or (eq span 'week) (eq span 'fortnight))
8013 (let* ((nt (calendar-day-of-week
8014 (calendar-gregorian-from-absolute sd)))
8015 (d (if org-agenda-start-on-weekday
8016 (- nt org-agenda-start-on-weekday)
8019 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
8020 (when n
8021 (require 'cal-iso)
8022 (when (> n 99)
8023 (setq y1 (org-small-year-to-year (/ n 100))
8024 n (mod n 100)))
8025 (setq sd
8026 (calendar-absolute-from-iso
8027 (list n 1
8028 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
8029 ((eq span 'month)
8030 (let (y1)
8031 (when (and n (> n 99))
8032 (setq y1 (org-small-year-to-year (/ n 100))
8033 n (mod n 100)))
8034 (setq sd (calendar-absolute-from-gregorian
8035 (list (or n mg) 1 (or y1 yg))))))
8036 ((eq span 'year)
8037 (setq sd (calendar-absolute-from-gregorian
8038 (list 1 1 (or n yg))))))
8039 sd))
8041 (defun org-agenda-next-date-line (&optional arg)
8042 "Jump to the next line indicating a date in agenda buffer."
8043 (interactive "p")
8044 (org-agenda-check-type t 'agenda 'timeline)
8045 (beginning-of-line 1)
8046 ;; This does not work if user makes date format that starts with a blank
8047 (if (looking-at "^\\S-") (forward-char 1))
8048 (if (not (re-search-forward "^\\S-" nil t arg))
8049 (progn
8050 (backward-char 1)
8051 (error "No next date after this line in this buffer")))
8052 (goto-char (match-beginning 0)))
8054 (defun org-agenda-previous-date-line (&optional arg)
8055 "Jump to the previous line indicating a date in agenda buffer."
8056 (interactive "p")
8057 (org-agenda-check-type t 'agenda 'timeline)
8058 (beginning-of-line 1)
8059 (if (not (re-search-backward "^\\S-" nil t arg))
8060 (error "No previous date before this line in this buffer")))
8062 ;; Initialize the highlight
8063 (defvar org-hl (make-overlay 1 1))
8064 (overlay-put org-hl 'face 'highlight)
8066 (defun org-highlight (begin end &optional buffer)
8067 "Highlight a region with overlay."
8068 (move-overlay org-hl begin end (or buffer (current-buffer))))
8070 (defun org-unhighlight ()
8071 "Detach overlay INDEX."
8072 (org-detach-overlay org-hl))
8074 (defun org-unhighlight-once ()
8075 "Remove the highlight from its position, and this function from the hook."
8076 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8077 (org-unhighlight))
8079 (defvar org-agenda-pre-follow-window-conf nil)
8080 (defun org-agenda-follow-mode ()
8081 "Toggle follow mode in an agenda buffer."
8082 (interactive)
8083 (unless org-agenda-follow-mode
8084 (setq org-agenda-pre-follow-window-conf
8085 (current-window-configuration)))
8086 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8087 (unless org-agenda-follow-mode
8088 (set-window-configuration org-agenda-pre-follow-window-conf))
8089 (org-agenda-set-mode-name)
8090 (org-agenda-do-context-action)
8091 (message "Follow mode is %s"
8092 (if org-agenda-follow-mode "on" "off")))
8094 (defun org-agenda-entry-text-mode (&optional arg)
8095 "Toggle entry text mode in an agenda buffer."
8096 (interactive "P")
8097 (if (or org-agenda-tag-filter
8098 org-agenda-category-filter
8099 org-agenda-regexp-filter
8100 org-agenda-top-headline-filter)
8101 (user-error "Can't show entry text in filtered views")
8102 (setq org-agenda-entry-text-mode (or (integerp arg)
8103 (not org-agenda-entry-text-mode)))
8104 (org-agenda-entry-text-hide)
8105 (and org-agenda-entry-text-mode
8106 (let ((org-agenda-entry-text-maxlines
8107 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8108 (org-agenda-entry-text-show)))
8109 (org-agenda-set-mode-name)
8110 (message "Entry text mode is %s%s"
8111 (if org-agenda-entry-text-mode "on" "off")
8112 (if (not org-agenda-entry-text-mode) ""
8113 (format " (maximum number of lines is %d)"
8114 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8116 (defun org-agenda-clockreport-mode (&optional with-filter)
8117 "Toggle clocktable mode in an agenda buffer.
8118 With prefix arg WITH-FILTER, make the clocktable respect the current
8119 agenda filter."
8120 (interactive "P")
8121 (org-agenda-check-type t 'agenda)
8122 (if with-filter
8123 (setq org-agenda-clockreport-mode 'with-filter)
8124 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
8125 (org-agenda-set-mode-name)
8126 (org-agenda-redo)
8127 (message "Clocktable mode is %s"
8128 (if org-agenda-clockreport-mode "on" "off")))
8130 (defun org-agenda-log-mode (&optional special)
8131 "Toggle log mode in an agenda buffer.
8132 With argument SPECIAL, show all possible log items, not only the ones
8133 configured in `org-agenda-log-mode-items'.
8134 With a double `C-u' prefix arg, show *only* log items, nothing else."
8135 (interactive "P")
8136 (org-agenda-check-type t 'agenda 'timeline)
8137 (setq org-agenda-show-log
8138 (cond
8139 ((equal special '(16)) 'only)
8140 ((eq special 'clockcheck)
8141 (if (eq org-agenda-show-log 'clockcheck)
8142 nil 'clockcheck))
8143 (special '(closed clock state))
8144 (t (not org-agenda-show-log))))
8145 (org-agenda-set-mode-name)
8146 (org-agenda-redo)
8147 (message "Log mode is %s"
8148 (if org-agenda-show-log "on" "off")))
8150 (defun org-agenda-archives-mode (&optional with-files)
8151 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8152 When called with a prefix argument, include all archive files as well."
8153 (interactive "P")
8154 (setq org-agenda-archives-mode
8155 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8156 (org-agenda-set-mode-name)
8157 (org-agenda-redo)
8158 (message
8159 "%s"
8160 (cond
8161 ((eq org-agenda-archives-mode nil)
8162 "No archives are included")
8163 ((eq org-agenda-archives-mode 'trees)
8164 (format "Trees with :%s: tag are included" org-archive-tag))
8165 ((eq org-agenda-archives-mode t)
8166 (format "Trees with :%s: tag and all active archive files are included"
8167 org-archive-tag)))))
8169 (defun org-agenda-toggle-diary ()
8170 "Toggle diary inclusion in an agenda buffer."
8171 (interactive)
8172 (org-agenda-check-type t 'agenda)
8173 (setq org-agenda-include-diary (not org-agenda-include-diary))
8174 (org-agenda-redo)
8175 (org-agenda-set-mode-name)
8176 (message "Diary inclusion turned %s"
8177 (if org-agenda-include-diary "on" "off")))
8179 (defun org-agenda-toggle-deadlines ()
8180 "Toggle inclusion of entries with a deadline in an agenda buffer."
8181 (interactive)
8182 (org-agenda-check-type t 'agenda)
8183 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8184 (org-agenda-redo)
8185 (org-agenda-set-mode-name)
8186 (message "Deadlines inclusion turned %s"
8187 (if org-agenda-include-deadlines "on" "off")))
8189 (defun org-agenda-toggle-time-grid ()
8190 "Toggle time grid in an agenda buffer."
8191 (interactive)
8192 (org-agenda-check-type t 'agenda)
8193 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8194 (org-agenda-redo)
8195 (org-agenda-set-mode-name)
8196 (message "Time-grid turned %s"
8197 (if org-agenda-use-time-grid "on" "off")))
8199 (defun org-agenda-set-mode-name ()
8200 "Set the mode name to indicate all the small mode settings."
8201 (setq mode-name
8202 (list "Org-Agenda"
8203 (if (get 'org-agenda-files 'org-restrict) " []" "")
8205 '(:eval (org-agenda-span-name org-agenda-current-span))
8206 (if org-agenda-follow-mode " Follow" "")
8207 (if org-agenda-entry-text-mode " ETxt" "")
8208 (if org-agenda-include-diary " Diary" "")
8209 (if org-agenda-include-deadlines " Ddl" "")
8210 (if org-agenda-use-time-grid " Grid" "")
8211 (if (and (boundp 'org-habit-show-habits)
8212 org-habit-show-habits) " Habit" "")
8213 (cond
8214 ((consp org-agenda-show-log) " LogAll")
8215 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8216 (org-agenda-show-log " Log")
8217 (t ""))
8218 (if (or org-agenda-category-filter
8219 (get 'org-agenda-category-filter :preset-filter))
8220 '(:eval (org-propertize
8221 (concat " <"
8222 (mapconcat
8223 'identity
8224 (append
8225 (get 'org-agenda-category-filter :preset-filter)
8226 org-agenda-category-filter)
8228 ">")
8229 'face 'org-agenda-filter-category
8230 'help-echo "Category used in filtering")) "")
8231 (if (or org-agenda-tag-filter
8232 (get 'org-agenda-tag-filter :preset-filter))
8233 '(:eval (org-propertize
8234 (concat " {"
8235 (mapconcat
8236 'identity
8237 (append
8238 (get 'org-agenda-tag-filter :preset-filter)
8239 org-agenda-tag-filter)
8241 "}")
8242 'face 'org-agenda-filter-tags
8243 'help-echo "Tags used in filtering")) "")
8244 (if (or org-agenda-regexp-filter
8245 (get 'org-agenda-regexp-filter :preset-filter))
8246 '(:eval (org-propertize
8247 (concat " ["
8248 (mapconcat
8249 'identity
8250 (append
8251 (get 'org-agenda-regexp-filter :preset-filter)
8252 org-agenda-regexp-filter)
8254 "]")
8255 'face 'org-agenda-filter-regexp
8256 'help-echo "Regexp used in filtering")) "")
8257 (if org-agenda-archives-mode
8258 (if (eq org-agenda-archives-mode t)
8259 " Archives"
8260 (format " :%s:" org-archive-tag))
8262 (if org-agenda-clockreport-mode
8263 (if (eq org-agenda-clockreport-mode 'with-filter)
8264 " Clock{}" " Clock")
8265 "")))
8266 (force-mode-line-update))
8268 (define-obsolete-function-alias
8269 'org-agenda-post-command-hook 'org-agenda-update-agenda-type "24.3")
8271 (defun org-agenda-update-agenda-type ()
8272 "Update the agenda type after each command."
8273 (setq org-agenda-type
8274 (or (get-text-property (point) 'org-agenda-type)
8275 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8277 (defun org-agenda-next-line ()
8278 "Move cursor to the next line, and show if follow mode is active."
8279 (interactive)
8280 (call-interactively 'next-line)
8281 (org-agenda-do-context-action))
8283 (defun org-agenda-previous-line ()
8284 "Move cursor to the previous line, and show if follow-mode is active."
8285 (interactive)
8286 (call-interactively 'previous-line)
8287 (org-agenda-do-context-action))
8289 (defun org-agenda-next-item (n)
8290 "Move cursor to next agenda item."
8291 (interactive "p")
8292 (let ((col (current-column)))
8293 (dotimes (c n)
8294 (when (next-single-property-change (point-at-eol) 'org-marker)
8295 (move-end-of-line 1)
8296 (goto-char (next-single-property-change (point) 'org-marker))))
8297 (org-move-to-column col))
8298 (org-agenda-do-context-action))
8300 (defun org-agenda-previous-item (n)
8301 "Move cursor to next agenda item."
8302 (interactive "p")
8303 (dotimes (c n)
8304 (let ((col (current-column))
8305 (goto (save-excursion
8306 (move-end-of-line 0)
8307 (previous-single-property-change (point) 'org-marker))))
8308 (if goto (goto-char goto))
8309 (org-move-to-column col)))
8310 (org-agenda-do-context-action))
8312 (defun org-agenda-do-context-action ()
8313 "Show outline path and, maybe, follow mode window."
8314 (let ((m (org-get-at-bol 'org-marker)))
8315 (when (and (markerp m) (marker-buffer m))
8316 (and org-agenda-follow-mode
8317 (if org-agenda-follow-indirect
8318 (org-agenda-tree-to-indirect-buffer nil)
8319 (org-agenda-show)))
8320 (and org-agenda-show-outline-path
8321 (org-with-point-at m (org-display-outline-path t))))))
8323 (defun org-agenda-show-tags ()
8324 "Show the tags applicable to the current item."
8325 (interactive)
8326 (let* ((tags (org-get-at-bol 'tags)))
8327 (if tags
8328 (message "Tags are :%s:"
8329 (org-no-properties (mapconcat 'identity tags ":")))
8330 (message "No tags associated with this line"))))
8332 (defun org-agenda-goto (&optional highlight)
8333 "Go to the Org-mode file which contains the item at point."
8334 (interactive)
8335 (let* ((marker (or (org-get-at-bol 'org-marker)
8336 (org-agenda-error)))
8337 (buffer (marker-buffer marker))
8338 (pos (marker-position marker)))
8339 (switch-to-buffer-other-window buffer)
8340 (widen)
8341 (push-mark)
8342 (goto-char pos)
8343 (when (derived-mode-p 'org-mode)
8344 (org-show-context 'agenda)
8345 (save-excursion
8346 (and (outline-next-heading)
8347 (org-flag-heading nil)))) ; show the next heading
8348 (when (outline-invisible-p)
8349 (show-entry)) ; display invisible text
8350 (recenter (/ (window-height) 2))
8351 (run-hooks 'org-agenda-after-show-hook)
8352 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8354 (defvar org-agenda-after-show-hook nil
8355 "Normal hook run after an item has been shown from the agenda.
8356 Point is in the buffer where the item originated.")
8358 (defun org-agenda-kill ()
8359 "Kill the entry or subtree belonging to the current agenda entry."
8360 (interactive)
8361 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8362 (let* ((bufname-orig (buffer-name))
8363 (marker (or (org-get-at-bol 'org-marker)
8364 (org-agenda-error)))
8365 (buffer (marker-buffer marker))
8366 (pos (marker-position marker))
8367 (type (org-get-at-bol 'type))
8368 dbeg dend (n 0) conf)
8369 (org-with-remote-undo buffer
8370 (with-current-buffer buffer
8371 (save-excursion
8372 (goto-char pos)
8373 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8374 (setq dbeg (progn (org-back-to-heading t) (point))
8375 dend (org-end-of-subtree t t))
8376 (setq dbeg (point-at-bol)
8377 dend (min (point-max) (1+ (point-at-eol)))))
8378 (goto-char dbeg)
8379 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8380 (setq conf (or (eq t org-agenda-confirm-kill)
8381 (and (numberp org-agenda-confirm-kill)
8382 (> n org-agenda-confirm-kill))))
8383 (and conf
8384 (not (y-or-n-p
8385 (format "Delete entry with %d lines in buffer \"%s\"? "
8386 n (buffer-name buffer))))
8387 (error "Abort"))
8388 (let ((org-agenda-buffer-name bufname-orig))
8389 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8390 (with-current-buffer buffer (delete-region dbeg dend))
8391 (message "Agenda item and source killed"))))
8393 (defvar org-archive-default-command) ; defined in org-archive.el
8394 (defun org-agenda-archive-default ()
8395 "Archive the entry or subtree belonging to the current agenda entry."
8396 (interactive)
8397 (require 'org-archive)
8398 (org-agenda-archive-with org-archive-default-command))
8400 (defun org-agenda-archive-default-with-confirmation ()
8401 "Archive the entry or subtree belonging to the current agenda entry."
8402 (interactive)
8403 (require 'org-archive)
8404 (org-agenda-archive-with org-archive-default-command 'confirm))
8406 (defun org-agenda-archive ()
8407 "Archive the entry or subtree belonging to the current agenda entry."
8408 (interactive)
8409 (org-agenda-archive-with 'org-archive-subtree))
8411 (defun org-agenda-archive-to-archive-sibling ()
8412 "Move the entry to the archive sibling."
8413 (interactive)
8414 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8416 (defun org-agenda-archive-with (cmd &optional confirm)
8417 "Move the entry to the archive sibling."
8418 (interactive)
8419 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8420 (let* ((bufname-orig (buffer-name))
8421 (marker (or (org-get-at-bol 'org-marker)
8422 (org-agenda-error)))
8423 (buffer (marker-buffer marker))
8424 (pos (marker-position marker)))
8425 (org-with-remote-undo buffer
8426 (with-current-buffer buffer
8427 (if (derived-mode-p 'org-mode)
8428 (if (and confirm
8429 (not (y-or-n-p "Archive this subtree or entry? ")))
8430 (error "Abort")
8431 (save-window-excursion
8432 (goto-char pos)
8433 (let ((org-agenda-buffer-name bufname-orig))
8434 (org-remove-subtree-entries-from-agenda))
8435 (org-back-to-heading t)
8436 (funcall cmd)))
8437 (error "Archiving works only in Org-mode files"))))))
8439 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8440 "Remove all lines in the agenda that correspond to a given subtree.
8441 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8442 If this information is not given, the function uses the tree at point."
8443 (let ((buf (or buf (current-buffer))) m p)
8444 (save-excursion
8445 (unless (and beg end)
8446 (org-back-to-heading t)
8447 (setq beg (point))
8448 (org-end-of-subtree t)
8449 (setq end (point)))
8450 (set-buffer (get-buffer org-agenda-buffer-name))
8451 (save-excursion
8452 (goto-char (point-max))
8453 (beginning-of-line 1)
8454 (while (not (bobp))
8455 (when (and (setq m (org-get-at-bol 'org-marker))
8456 (equal buf (marker-buffer m))
8457 (setq p (marker-position m))
8458 (>= p beg)
8459 (< p end))
8460 (let ((inhibit-read-only t))
8461 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8462 (beginning-of-line 0))))))
8464 (defun org-agenda-refile (&optional goto rfloc no-update)
8465 "Refile the item at point.
8467 When GOTO is 0 or '(64), clear the refile cache.
8468 When GOTO is '(16), go to the location of the last refiled item.
8469 RFLOC can be a refile location obtained in a different way.
8470 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8471 (interactive "P")
8472 (cond
8473 ((member goto '(0 (64)))
8474 (org-refile-cache-clear))
8475 ((equal goto '(16))
8476 (org-refile-goto-last-stored))
8478 (let* ((buffer-orig (buffer-name))
8479 (marker (or (org-get-at-bol 'org-hd-marker)
8480 (org-agenda-error)))
8481 (buffer (marker-buffer marker))
8482 (pos (marker-position marker))
8483 (rfloc (or rfloc
8484 (org-refile-get-location
8485 (if goto "Goto" "Refile to") buffer
8486 org-refile-allow-creating-parent-nodes))))
8487 (with-current-buffer buffer
8488 (save-excursion
8489 (save-restriction
8490 (widen)
8491 (goto-char marker)
8492 (let ((org-agenda-buffer-name buffer-orig))
8493 (org-remove-subtree-entries-from-agenda))
8494 (org-refile goto buffer rfloc)))))
8495 (unless no-update (org-agenda-redo)))))
8497 (defun org-agenda-open-link (&optional arg)
8498 "Open the link(s) in the current entry, if any.
8499 This looks for a link in the displayed line in the agenda.
8500 It also looks at the text of the entry itself."
8501 (interactive "P")
8502 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8503 (org-get-at-bol 'org-marker)))
8504 (buffer (and marker (marker-buffer marker)))
8505 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8506 (lkall (and buffer (org-offer-links-in-entry
8507 buffer marker arg prefix)))
8508 (lk0 (car lkall))
8509 (lk (if (stringp lk0) (list lk0) lk0))
8510 (lkend (cdr lkall))
8511 trg)
8512 (cond
8513 ((and buffer lk)
8514 (mapcar (lambda(l)
8515 (with-current-buffer buffer
8516 (setq trg (and (string-match org-bracket-link-regexp l)
8517 (match-string 1 l)))
8518 (if (or (not trg) (string-match org-any-link-re trg))
8519 (save-excursion
8520 (save-restriction
8521 (widen)
8522 (goto-char marker)
8523 (when (search-forward l nil lkend)
8524 (goto-char (match-beginning 0))
8525 (org-open-at-point))))
8526 ;; This is an internal link, widen the buffer
8527 (switch-to-buffer-other-window buffer)
8528 (widen)
8529 (goto-char marker)
8530 (when (search-forward l nil lkend)
8531 (goto-char (match-beginning 0))
8532 (org-open-at-point)))))
8533 lk))
8534 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8535 (save-excursion
8536 (beginning-of-line 1)
8537 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8538 (org-open-link-from-string (match-string 1)))
8539 (t (message "No link to open here")))))
8541 (defun org-agenda-copy-local-variable (var)
8542 "Get a variable from a referenced buffer and install it here."
8543 (let ((m (org-get-at-bol 'org-marker)))
8544 (when (and m (buffer-live-p (marker-buffer m)))
8545 (org-set-local var (with-current-buffer (marker-buffer m)
8546 (symbol-value var))))))
8548 (defun org-agenda-switch-to (&optional delete-other-windows)
8549 "Go to the Org-mode file which contains the item at point."
8550 (interactive)
8551 (if (and org-return-follows-link
8552 (not (org-get-at-bol 'org-marker))
8553 (org-in-regexp org-bracket-link-regexp))
8554 (org-open-link-from-string (match-string 0))
8555 (let* ((marker (or (org-get-at-bol 'org-marker)
8556 (org-agenda-error)))
8557 (buffer (marker-buffer marker))
8558 (pos (marker-position marker)))
8559 (org-pop-to-buffer-same-window buffer)
8560 (and delete-other-windows (delete-other-windows))
8561 (widen)
8562 (goto-char pos)
8563 (org-back-to-heading t)
8564 (when (derived-mode-p 'org-mode)
8565 (org-show-context 'agenda)
8566 (save-excursion
8567 (and (outline-next-heading)
8568 (org-flag-heading nil))) ; show the next heading
8569 (when (outline-invisible-p)
8570 (show-entry)) ; display invisible text
8571 (run-hooks 'org-agenda-after-show-hook)))))
8573 (defun org-agenda-goto-mouse (ev)
8574 "Go to the Org-mode file which contains the item at the mouse click."
8575 (interactive "e")
8576 (mouse-set-point ev)
8577 (org-agenda-goto))
8579 (defun org-agenda-show (&optional full-entry)
8580 "Display the Org-mode file which contains the item at point.
8581 With prefix argument FULL-ENTRY, make the entire entry visible
8582 if it was hidden in the outline."
8583 (interactive "P")
8584 (let ((win (selected-window)))
8585 (if full-entry
8586 (let ((org-show-entry-below t))
8587 (org-agenda-goto t))
8588 (org-agenda-goto t))
8589 (select-window win)))
8591 (defvar org-agenda-show-window nil)
8592 (defun org-agenda-show-and-scroll-up (&optional arg)
8593 "Display the Org-mode file which contains the item at point.
8594 When called repeatedly, scroll the window that is displaying the buffer.
8595 With a \\[universal-argument] prefix, use `org-show-entry' instead of
8596 `show-subtree' to display the item, so that drawers and logbooks stay
8597 folded."
8598 (interactive "P")
8599 (let ((win (selected-window)))
8600 (if (and (window-live-p org-agenda-show-window)
8601 (eq this-command last-command))
8602 (progn
8603 (select-window org-agenda-show-window)
8604 (ignore-errors (scroll-up)))
8605 (org-agenda-goto t)
8606 (if arg (org-show-entry) (show-subtree))
8607 (setq org-agenda-show-window (selected-window)))
8608 (select-window win)))
8610 (defun org-agenda-show-scroll-down ()
8611 "Scroll down the window showing the agenda."
8612 (interactive)
8613 (let ((win (selected-window)))
8614 (when (window-live-p org-agenda-show-window)
8615 (select-window org-agenda-show-window)
8616 (ignore-errors (scroll-down))
8617 (select-window win))))
8619 (defun org-agenda-show-1 (&optional more)
8620 "Display the Org-mode file which contains the item at point.
8621 The prefix arg selects the amount of information to display:
8623 0 hide the subtree
8624 1 just show the entry according to defaults.
8625 2 show the children view
8626 3 show the subtree view
8627 4 show the entire subtree and any LOGBOOK drawers
8628 5 show the entire subtree and any drawers
8629 With prefix argument FULL-ENTRY, make the entire entry visible
8630 if it was hidden in the outline."
8631 (interactive "p")
8632 (let ((win (selected-window)))
8633 (org-agenda-goto t)
8634 (org-recenter-heading 1)
8635 (cond
8636 ((= more 0)
8637 (hide-subtree)
8638 (save-excursion
8639 (org-back-to-heading)
8640 (run-hook-with-args 'org-cycle-hook 'folded))
8641 (message "Remote: FOLDED"))
8642 ((and (org-called-interactively-p 'any) (= more 1))
8643 (message "Remote: show with default settings"))
8644 ((= more 2)
8645 (show-entry)
8646 (show-children)
8647 (save-excursion
8648 (org-back-to-heading)
8649 (run-hook-with-args 'org-cycle-hook 'children))
8650 (message "Remote: CHILDREN"))
8651 ((= more 3)
8652 (show-subtree)
8653 (save-excursion
8654 (org-back-to-heading)
8655 (run-hook-with-args 'org-cycle-hook 'subtree))
8656 (message "Remote: SUBTREE"))
8657 ((= more 4)
8658 (show-subtree)
8659 (save-excursion
8660 (org-back-to-heading)
8661 (org-cycle-hide-drawers 'subtree '("LOGBOOK")))
8662 (message "Remote: SUBTREE AND LOGBOOK"))
8663 ((> more 4)
8664 (show-subtree)
8665 (message "Remote: SUBTREE AND ALL DRAWERS")))
8666 (select-window win)))
8668 (defun org-recenter-heading (n)
8669 (save-excursion
8670 (org-back-to-heading)
8671 (recenter n)))
8673 (defvar org-agenda-cycle-counter nil)
8674 (defun org-agenda-cycle-show (&optional n)
8675 "Show the current entry in another window, with default settings.
8676 Default settings are taken from `org-show-hierarchy-above' and siblings.
8677 When use repeatedly in immediate succession, the remote entry will cycle
8678 through visibility
8680 children -> subtree -> folded
8682 When called with a numeric prefix arg, that arg will be passed through to
8683 `org-agenda-show-1'. For the interpretation of that argument, see the
8684 docstring of `org-agenda-show-1'."
8685 (interactive "P")
8686 (if (integerp n)
8687 (setq org-agenda-cycle-counter n)
8688 (if (not (eq last-command this-command))
8689 (setq org-agenda-cycle-counter 1)
8690 (if (equal org-agenda-cycle-counter 0)
8691 (setq org-agenda-cycle-counter 2)
8692 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8693 (if (> org-agenda-cycle-counter 3)
8694 (setq org-agenda-cycle-counter 0)))))
8695 (org-agenda-show-1 org-agenda-cycle-counter))
8697 (defun org-agenda-recenter (arg)
8698 "Display the Org-mode file which contains the item at point and recenter."
8699 (interactive "P")
8700 (let ((win (selected-window)))
8701 (org-agenda-goto t)
8702 (recenter arg)
8703 (select-window win)))
8705 (defun org-agenda-show-mouse (ev)
8706 "Display the Org-mode file which contains the item at the mouse click."
8707 (interactive "e")
8708 (mouse-set-point ev)
8709 (org-agenda-show))
8711 (defun org-agenda-check-no-diary ()
8712 "Check if the entry is a diary link and abort if yes."
8713 (if (org-get-at-bol 'org-agenda-diary-link)
8714 (org-agenda-error)))
8716 (defun org-agenda-error ()
8717 (error "Command not allowed in this line"))
8719 (defun org-agenda-tree-to-indirect-buffer (arg)
8720 "Show the subtree corresponding to the current entry in an indirect buffer.
8721 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8723 With a numerical prefix ARG, go up to this level and then take that tree.
8724 With a negative numeric ARG, go up by this number of levels.
8725 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
8726 use the dedicated frame)."
8727 (interactive "P")
8728 (if current-prefix-arg
8729 (org-agenda-do-tree-to-indirect-buffer arg)
8730 (let ((agenda-buffer (buffer-name))
8731 (agenda-window (selected-window))
8732 (indirect-window
8733 (and org-last-indirect-buffer
8734 (get-buffer-window org-last-indirect-buffer))))
8735 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8736 (unless (or (eq org-indirect-buffer-display 'new-frame)
8737 (eq org-indirect-buffer-display 'dedicated-frame))
8738 (unwind-protect
8739 (unless (and indirect-window (window-live-p indirect-window))
8740 (setq indirect-window (split-window agenda-window)))
8741 (and indirect-window (select-window indirect-window))
8742 (switch-to-buffer org-last-indirect-buffer :norecord)
8743 (fit-window-to-buffer indirect-window)))
8744 (select-window (get-buffer-window agenda-buffer)))))
8746 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8747 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8748 (org-agenda-check-no-diary)
8749 (let* ((marker (or (org-get-at-bol 'org-marker)
8750 (org-agenda-error)))
8751 (buffer (marker-buffer marker))
8752 (pos (marker-position marker)))
8753 (with-current-buffer buffer
8754 (save-excursion
8755 (goto-char pos)
8756 (funcall 'org-tree-to-indirect-buffer arg)))))
8758 (defvar org-last-heading-marker (make-marker)
8759 "Marker pointing to the headline that last changed its TODO state
8760 by a remote command from the agenda.")
8762 (defun org-agenda-todo-nextset ()
8763 "Switch TODO entry to next sequence."
8764 (interactive)
8765 (org-agenda-todo 'nextset))
8767 (defun org-agenda-todo-previousset ()
8768 "Switch TODO entry to previous sequence."
8769 (interactive)
8770 (org-agenda-todo 'previousset))
8772 (defun org-agenda-todo (&optional arg)
8773 "Cycle TODO state of line at point, also in Org-mode file.
8774 This changes the line at point, all other lines in the agenda referring to
8775 the same tree node, and the headline of the tree node in the Org-mode file."
8776 (interactive "P")
8777 (org-agenda-check-no-diary)
8778 (let* ((col (current-column))
8779 (marker (or (org-get-at-bol 'org-marker)
8780 (org-agenda-error)))
8781 (buffer (marker-buffer marker))
8782 (pos (marker-position marker))
8783 (hdmarker (org-get-at-bol 'org-hd-marker))
8784 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
8785 (inhibit-read-only t)
8786 org-agenda-headline-snapshot-before-repeat newhead just-one)
8787 (org-with-remote-undo buffer
8788 (with-current-buffer buffer
8789 (widen)
8790 (goto-char pos)
8791 (org-show-context 'agenda)
8792 (save-excursion
8793 (and (outline-next-heading)
8794 (org-flag-heading nil))) ; show the next heading
8795 (let ((current-prefix-arg arg))
8796 (call-interactively 'org-todo))
8797 (and (bolp) (forward-char 1))
8798 (setq newhead (org-get-heading))
8799 (when (and (org-bound-and-true-p
8800 org-agenda-headline-snapshot-before-repeat)
8801 (not (equal org-agenda-headline-snapshot-before-repeat
8802 newhead))
8803 todayp)
8804 (setq newhead org-agenda-headline-snapshot-before-repeat
8805 just-one t))
8806 (save-excursion
8807 (org-back-to-heading)
8808 (move-marker org-last-heading-marker (point))))
8809 (beginning-of-line 1)
8810 (save-window-excursion
8811 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8812 (when (org-bound-and-true-p org-clock-out-when-done)
8813 (string-match (concat "^" (regexp-opt org-done-keywords-for-agenda))
8814 newhead)
8815 (org-agenda-unmark-clocking-task))
8816 (org-move-to-column col))))
8818 (defun org-agenda-add-note (&optional arg)
8819 "Add a time-stamped note to the entry at point."
8820 (interactive "P")
8821 (org-agenda-check-no-diary)
8822 (let* ((marker (or (org-get-at-bol 'org-marker)
8823 (org-agenda-error)))
8824 (buffer (marker-buffer marker))
8825 (pos (marker-position marker))
8826 (hdmarker (org-get-at-bol 'org-hd-marker))
8827 (inhibit-read-only t))
8828 (with-current-buffer buffer
8829 (widen)
8830 (goto-char pos)
8831 (org-show-context 'agenda)
8832 (save-excursion
8833 (and (outline-next-heading)
8834 (org-flag-heading nil))) ; show the next heading
8835 (org-add-note))))
8837 (defun org-agenda-change-all-lines (newhead hdmarker
8838 &optional fixface just-this)
8839 "Change all lines in the agenda buffer which match HDMARKER.
8840 The new content of the line will be NEWHEAD (as modified by
8841 `org-agenda-format-item'). HDMARKER is checked with
8842 `equal' against all `org-hd-marker' text properties in the file.
8843 If FIXFACE is non-nil, the face of each item is modified according to
8844 the new TODO state.
8845 If JUST-THIS is non-nil, change just the current line, not all.
8846 If FORCE-TAGS is non nil, the car of it returns the new tags."
8847 (let* ((inhibit-read-only t)
8848 (line (org-current-line))
8849 (org-agenda-buffer (current-buffer))
8850 (thetags (with-current-buffer (marker-buffer hdmarker)
8851 (save-excursion (save-restriction (widen)
8852 (goto-char hdmarker)
8853 (org-get-tags-at)))))
8854 props m pl undone-face done-face finish new dotime level cat tags)
8855 (save-excursion
8856 (goto-char (point-max))
8857 (beginning-of-line 1)
8858 (while (not finish)
8859 (setq finish (bobp))
8860 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8861 (or (not just-this) (= (org-current-line) line))
8862 (equal m hdmarker))
8863 (setq props (text-properties-at (point))
8864 dotime (org-get-at-bol 'dotime)
8865 cat (org-get-at-bol 'org-category)
8866 level (org-get-at-bol 'level)
8867 tags thetags
8869 (let ((org-prefix-format-compiled
8870 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8871 org-prefix-format-compiled))
8872 (extra (org-get-at-bol 'extra)))
8873 (with-current-buffer (marker-buffer hdmarker)
8874 (save-excursion
8875 (save-restriction
8876 (widen)
8877 (org-agenda-format-item extra newhead level cat tags dotime)))))
8878 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8879 undone-face (org-get-at-bol 'undone-face)
8880 done-face (org-get-at-bol 'done-face))
8881 (beginning-of-line 1)
8882 (cond
8883 ((equal new "")
8884 (and (looking-at ".*\n?") (replace-match "")))
8885 ((looking-at ".*")
8886 (replace-match new t t)
8887 (beginning-of-line 1)
8888 (add-text-properties (point-at-bol) (point-at-eol) props)
8889 (when fixface
8890 (add-text-properties
8891 (point-at-bol) (point-at-eol)
8892 (list 'face
8893 (if org-last-todo-state-is-todo
8894 undone-face done-face))))
8895 (org-agenda-highlight-todo 'line)
8896 (beginning-of-line 1))
8897 (t (error "Line update did not work")))
8898 (save-restriction
8899 (narrow-to-region (point-at-bol) (point-at-eol))
8900 (org-agenda-finalize)))
8901 (beginning-of-line 0)))))
8903 (defun org-agenda-align-tags (&optional line)
8904 "Align all tags in agenda items to `org-agenda-tags-column'."
8905 (let ((inhibit-read-only t) l c)
8906 (save-excursion
8907 (goto-char (if line (point-at-bol) (point-min)))
8908 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
8909 (if line (point-at-eol) nil) t)
8910 (add-text-properties
8911 (match-beginning 2) (match-end 2)
8912 (list 'face (delq nil (let ((prop (get-text-property
8913 (match-beginning 2) 'face)))
8914 (or (listp prop) (setq prop (list prop)))
8915 (if (memq 'org-tag prop)
8916 prop
8917 (cons 'org-tag prop))))))
8918 (setq l (- (match-end 2) (match-beginning 2))
8919 c (if (< org-agenda-tags-column 0)
8920 (- (abs org-agenda-tags-column) l)
8921 org-agenda-tags-column))
8922 (delete-region (match-beginning 1) (match-end 1))
8923 (goto-char (match-beginning 1))
8924 (insert (org-add-props
8925 (make-string (max 1 (- c (current-column))) ?\ )
8926 (plist-put (copy-sequence (text-properties-at (point)))
8927 'face nil))))
8928 (goto-char (point-min))
8929 (org-font-lock-add-tag-faces (point-max)))))
8931 (defun org-agenda-priority-up ()
8932 "Increase the priority of line at point, also in Org-mode file."
8933 (interactive)
8934 (org-agenda-priority 'up))
8936 (defun org-agenda-priority-down ()
8937 "Decrease the priority of line at point, also in Org-mode file."
8938 (interactive)
8939 (org-agenda-priority 'down))
8941 (defun org-agenda-priority (&optional force-direction)
8942 "Set the priority of line at point, also in Org-mode file.
8943 This changes the line at point, all other lines in the agenda referring to
8944 the same tree node, and the headline of the tree node in the Org-mode file.
8945 Called with a universal prefix arg, show the priority instead of setting it."
8946 (interactive "P")
8947 (if (equal force-direction '(4))
8948 (org-show-priority)
8949 (unless org-enable-priority-commands
8950 (error "Priority commands are disabled"))
8951 (org-agenda-check-no-diary)
8952 (let* ((col (current-column))
8953 (marker (or (org-get-at-bol 'org-marker)
8954 (org-agenda-error)))
8955 (hdmarker (org-get-at-bol 'org-hd-marker))
8956 (buffer (marker-buffer hdmarker))
8957 (pos (marker-position hdmarker))
8958 (inhibit-read-only t)
8959 newhead)
8960 (org-with-remote-undo buffer
8961 (with-current-buffer buffer
8962 (widen)
8963 (goto-char pos)
8964 (org-show-context 'agenda)
8965 (save-excursion
8966 (and (outline-next-heading)
8967 (org-flag-heading nil))) ; show the next heading
8968 (funcall 'org-priority force-direction)
8969 (end-of-line 1)
8970 (setq newhead (org-get-heading)))
8971 (org-agenda-change-all-lines newhead hdmarker)
8972 (org-move-to-column col)))))
8974 ;; FIXME: should fix the tags property of the agenda line.
8975 (defun org-agenda-set-tags (&optional tag onoff)
8976 "Set tags for the current headline."
8977 (interactive)
8978 (org-agenda-check-no-diary)
8979 (if (and (org-region-active-p) (org-called-interactively-p 'any))
8980 (call-interactively 'org-change-tag-in-region)
8981 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8982 (org-agenda-error)))
8983 (buffer (marker-buffer hdmarker))
8984 (pos (marker-position hdmarker))
8985 (inhibit-read-only t)
8986 newhead)
8987 (org-with-remote-undo buffer
8988 (with-current-buffer buffer
8989 (widen)
8990 (goto-char pos)
8991 (save-excursion
8992 (org-show-context 'agenda))
8993 (save-excursion
8994 (and (outline-next-heading)
8995 (org-flag-heading nil))) ; show the next heading
8996 (goto-char pos)
8997 (if tag
8998 (org-toggle-tag tag onoff)
8999 (call-interactively 'org-set-tags))
9000 (end-of-line 1)
9001 (setq newhead (org-get-heading)))
9002 (org-agenda-change-all-lines newhead hdmarker)
9003 (beginning-of-line 1)))))
9005 (defun org-agenda-set-property ()
9006 "Set a property for the current headline."
9007 (interactive)
9008 (org-agenda-check-no-diary)
9009 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9010 (org-agenda-error)))
9011 (buffer (marker-buffer hdmarker))
9012 (pos (marker-position hdmarker))
9013 (inhibit-read-only t)
9014 newhead)
9015 (org-with-remote-undo buffer
9016 (with-current-buffer buffer
9017 (widen)
9018 (goto-char pos)
9019 (save-excursion
9020 (org-show-context 'agenda))
9021 (save-excursion
9022 (and (outline-next-heading)
9023 (org-flag-heading nil))) ; show the next heading
9024 (goto-char pos)
9025 (call-interactively 'org-set-property)))))
9027 (defun org-agenda-set-effort ()
9028 "Set the effort property for the current headline."
9029 (interactive)
9030 (org-agenda-check-no-diary)
9031 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9032 (org-agenda-error)))
9033 (buffer (marker-buffer hdmarker))
9034 (pos (marker-position hdmarker))
9035 (inhibit-read-only t)
9036 newhead)
9037 (org-with-remote-undo buffer
9038 (with-current-buffer buffer
9039 (widen)
9040 (goto-char pos)
9041 (save-excursion
9042 (org-show-context 'agenda))
9043 (save-excursion
9044 (and (outline-next-heading)
9045 (org-flag-heading nil))) ; show the next heading
9046 (goto-char pos)
9047 (call-interactively 'org-set-effort)
9048 (end-of-line 1)
9049 (setq newhead (org-get-heading)))
9050 (org-agenda-change-all-lines newhead hdmarker))))
9052 (defun org-agenda-toggle-archive-tag ()
9053 "Toggle the archive tag for the current entry."
9054 (interactive)
9055 (org-agenda-check-no-diary)
9056 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9057 (org-agenda-error)))
9058 (buffer (marker-buffer hdmarker))
9059 (pos (marker-position hdmarker))
9060 (inhibit-read-only t)
9061 newhead)
9062 (org-with-remote-undo buffer
9063 (with-current-buffer buffer
9064 (widen)
9065 (goto-char pos)
9066 (org-show-context 'agenda)
9067 (save-excursion
9068 (and (outline-next-heading)
9069 (org-flag-heading nil))) ; show the next heading
9070 (call-interactively 'org-toggle-archive-tag)
9071 (end-of-line 1)
9072 (setq newhead (org-get-heading)))
9073 (org-agenda-change-all-lines newhead hdmarker)
9074 (beginning-of-line 1))))
9076 (defun org-agenda-do-date-later (arg)
9077 (interactive "P")
9078 (cond
9079 ((or (equal arg '(16))
9080 (memq last-command
9081 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9082 (setq this-command 'org-agenda-date-later-minutes)
9083 (org-agenda-date-later-minutes 1))
9084 ((or (equal arg '(4))
9085 (memq last-command
9086 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9087 (setq this-command 'org-agenda-date-later-hours)
9088 (org-agenda-date-later-hours 1))
9090 (org-agenda-date-later (prefix-numeric-value arg)))))
9092 (defun org-agenda-do-date-earlier (arg)
9093 (interactive "P")
9094 (cond
9095 ((or (equal arg '(16))
9096 (memq last-command
9097 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9098 (setq this-command 'org-agenda-date-earlier-minutes)
9099 (org-agenda-date-earlier-minutes 1))
9100 ((or (equal arg '(4))
9101 (memq last-command
9102 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9103 (setq this-command 'org-agenda-date-earlier-hours)
9104 (org-agenda-date-earlier-hours 1))
9106 (org-agenda-date-earlier (prefix-numeric-value arg)))))
9108 (defun org-agenda-date-later (arg &optional what)
9109 "Change the date of this item to ARG day(s) later."
9110 (interactive "p")
9111 (org-agenda-check-type t 'agenda 'timeline)
9112 (org-agenda-check-no-diary)
9113 (let* ((marker (or (org-get-at-bol 'org-marker)
9114 (org-agenda-error)))
9115 (buffer (marker-buffer marker))
9116 (pos (marker-position marker))
9117 cdate today)
9118 (org-with-remote-undo buffer
9119 (with-current-buffer buffer
9120 (widen)
9121 (goto-char pos)
9122 (if (not (org-at-timestamp-p))
9123 (error "Cannot find time stamp"))
9124 (when (and org-agenda-move-date-from-past-immediately-to-today
9125 (equal arg 1)
9126 (or (not what) (eq what 'day))
9127 (not (save-match-data (org-at-date-range-p))))
9128 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9129 cdate (calendar-absolute-from-gregorian
9130 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9131 today (org-today))
9132 (if (> today cdate)
9133 ;; immediately shift to today
9134 (setq arg (- today cdate))))
9135 (org-timestamp-change arg (or what 'day))
9136 (when (and (org-at-date-range-p)
9137 (re-search-backward org-tr-regexp-both (point-at-bol)))
9138 (let ((end org-last-changed-timestamp))
9139 (org-timestamp-change arg (or what 'day))
9140 (setq org-last-changed-timestamp
9141 (concat org-last-changed-timestamp "--" end)))))
9142 (org-agenda-show-new-time marker org-last-changed-timestamp))
9143 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9145 (defun org-agenda-date-earlier (arg &optional what)
9146 "Change the date of this item to ARG day(s) earlier."
9147 (interactive "p")
9148 (org-agenda-date-later (- arg) what))
9150 (defun org-agenda-date-later-minutes (arg)
9151 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9152 (interactive "p")
9153 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9154 (org-agenda-date-later arg 'minute))
9156 (defun org-agenda-date-earlier-minutes (arg)
9157 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9158 (interactive "p")
9159 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9160 (org-agenda-date-earlier arg 'minute))
9162 (defun org-agenda-date-later-hours (arg)
9163 "Change the time of this item, in hour steps."
9164 (interactive "p")
9165 (org-agenda-date-later arg 'hour))
9167 (defun org-agenda-date-earlier-hours (arg)
9168 "Change the time of this item, in hour steps."
9169 (interactive "p")
9170 (org-agenda-date-earlier arg 'hour))
9172 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9173 "Show new date stamp via text properties."
9174 ;; We use text properties to make this undoable
9175 (let ((inhibit-read-only t))
9176 (setq stamp (concat prefix " => " stamp " "))
9177 (save-excursion
9178 (goto-char (point-max))
9179 (while (not (bobp))
9180 (when (equal marker (org-get-at-bol 'org-marker))
9181 (org-move-to-column (- (window-width) (length stamp)) t)
9182 (org-agenda-fix-tags-filter-overlays-at (point))
9183 (if (featurep 'xemacs)
9184 ;; Use `duplicable' property to trigger undo recording
9185 (let ((ex (make-extent nil nil))
9186 (gl (make-glyph stamp)))
9187 (set-glyph-face gl 'secondary-selection)
9188 (set-extent-properties
9189 ex (list 'invisible t 'end-glyph gl 'duplicable t))
9190 (insert-extent ex (1- (point)) (point-at-eol)))
9191 (add-text-properties
9192 (1- (point)) (point-at-eol)
9193 (list 'display (org-add-props stamp nil
9194 'face 'secondary-selection))))
9195 (move-beginning-of-line 1))
9196 (move-beginning-of-line 0)))))
9198 (defun org-agenda-date-prompt (arg)
9199 "Change the date of this item. Date is prompted for, with default today.
9200 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9201 be used to request time specification in the time stamp."
9202 (interactive "P")
9203 (org-agenda-check-type t 'agenda 'timeline)
9204 (org-agenda-check-no-diary)
9205 (let* ((marker (or (org-get-at-bol 'org-marker)
9206 (org-agenda-error)))
9207 (buffer (marker-buffer marker))
9208 (pos (marker-position marker)))
9209 (org-with-remote-undo buffer
9210 (with-current-buffer buffer
9211 (widen)
9212 (goto-char pos)
9213 (if (not (org-at-timestamp-p t))
9214 (error "Cannot find time stamp"))
9215 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
9216 (org-agenda-show-new-time marker org-last-changed-timestamp))
9217 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9219 (defun org-agenda-schedule (arg &optional time)
9220 "Schedule the item at point.
9221 ARG is passed through to `org-schedule'."
9222 (interactive "P")
9223 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9224 (org-agenda-check-no-diary)
9225 (let* ((marker (or (org-get-at-bol 'org-marker)
9226 (org-agenda-error)))
9227 (type (marker-insertion-type marker))
9228 (buffer (marker-buffer marker))
9229 (pos (marker-position marker))
9230 (org-insert-labeled-timestamps-at-point nil)
9232 (set-marker-insertion-type marker t)
9233 (org-with-remote-undo buffer
9234 (with-current-buffer buffer
9235 (widen)
9236 (goto-char pos)
9237 (setq ts (org-schedule arg time)))
9238 (org-agenda-show-new-time marker ts " S"))
9239 (message "%s" ts)))
9241 (defun org-agenda-deadline (arg &optional time)
9242 "Schedule the item at point.
9243 ARG is passed through to `org-deadline'."
9244 (interactive "P")
9245 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9246 (org-agenda-check-no-diary)
9247 (let* ((marker (or (org-get-at-bol 'org-marker)
9248 (org-agenda-error)))
9249 (buffer (marker-buffer marker))
9250 (pos (marker-position marker))
9251 (org-insert-labeled-timestamps-at-point nil)
9253 (org-with-remote-undo buffer
9254 (with-current-buffer buffer
9255 (widen)
9256 (goto-char pos)
9257 (setq ts (org-deadline arg time)))
9258 (org-agenda-show-new-time marker ts " D"))
9259 (message "%s" ts)))
9261 (defun org-agenda-clock-in (&optional arg)
9262 "Start the clock on the currently selected item."
9263 (interactive "P")
9264 (org-agenda-check-no-diary)
9265 (if (equal arg '(4))
9266 (org-clock-in arg)
9267 (let* ((marker (or (org-get-at-bol 'org-marker)
9268 (org-agenda-error)))
9269 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9270 (pos (marker-position marker))
9271 (col (current-column))
9272 newhead)
9273 (org-with-remote-undo (marker-buffer marker)
9274 (with-current-buffer (marker-buffer marker)
9275 (widen)
9276 (goto-char pos)
9277 (org-show-context 'agenda)
9278 (org-show-entry)
9279 (org-cycle-hide-drawers 'children)
9280 (org-clock-in arg)
9281 (setq newhead (org-get-heading)))
9282 (org-agenda-change-all-lines newhead hdmarker))
9283 (org-move-to-column col))))
9285 (defun org-agenda-clock-out ()
9286 "Stop the currently running clock."
9287 (interactive)
9288 (unless (marker-buffer org-clock-marker)
9289 (error "No running clock"))
9290 (let ((marker (make-marker)) (col (current-column)) newhead)
9291 (org-with-remote-undo (marker-buffer org-clock-marker)
9292 (with-current-buffer (marker-buffer org-clock-marker)
9293 (save-excursion
9294 (save-restriction
9295 (widen)
9296 (goto-char org-clock-marker)
9297 (org-back-to-heading t)
9298 (move-marker marker (point))
9299 (org-clock-out)
9300 (setq newhead (org-get-heading))))))
9301 (org-agenda-change-all-lines newhead marker)
9302 (move-marker marker nil)
9303 (org-move-to-column col)
9304 (org-agenda-unmark-clocking-task)))
9306 (defun org-agenda-clock-cancel (&optional arg)
9307 "Cancel the currently running clock."
9308 (interactive "P")
9309 (unless (marker-buffer org-clock-marker)
9310 (user-error "No running clock"))
9311 (org-with-remote-undo (marker-buffer org-clock-marker)
9312 (org-clock-cancel)))
9314 (defun org-agenda-clock-goto ()
9315 "Jump to the currently clocked in task within the agenda.
9316 If the currently clocked in task is not listed in the agenda
9317 buffer, display it in another window."
9318 (interactive)
9319 (let (pos)
9320 (mapc (lambda (o)
9321 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
9322 (setq pos (overlay-start o))))
9323 (overlays-in (point-min) (point-max)))
9324 (cond (pos (goto-char pos))
9325 ;; If the currently clocked entry is not in the agenda
9326 ;; buffer, we visit it in another window:
9327 (org-clock-current-task
9328 (org-switch-to-buffer-other-window (org-clock-goto)))
9329 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9331 (defun org-agenda-diary-entry-in-org-file ()
9332 "Make a diary entry in the file `org-agenda-diary-file'."
9333 (let (d1 d2 char (text "") dp1 dp2)
9334 (if (equal (buffer-name) "*Calendar*")
9335 (setq d1 (calendar-cursor-to-date t)
9336 d2 (car calendar-mark-ring))
9337 (setq dp1 (get-text-property (point-at-bol) 'day))
9338 (unless dp1 (user-error "No date defined in current line"))
9339 (setq d1 (calendar-gregorian-from-absolute dp1)
9340 d2 (and (ignore-errors (mark))
9341 (save-excursion
9342 (goto-char (mark))
9343 (setq dp2 (get-text-property (point-at-bol) 'day)))
9344 (calendar-gregorian-from-absolute dp2))))
9345 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9346 (setq char (read-char-exclusive))
9347 (cond
9348 ((equal char ?d)
9349 (setq text (read-string "Day entry: "))
9350 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9351 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9352 ((equal char ?a)
9353 (setq d1 (list (car d1) (nth 1 d1)
9354 (read-number (format "Reference year [%d]: " (nth 2 d1))
9355 (nth 2 d1))))
9356 (setq text (read-string "Anniversary (use %d to show years): "))
9357 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9358 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9359 ((equal char ?b)
9360 (setq text (read-string "Block entry: "))
9361 (unless (and d1 d2 (not (equal d1 d2)))
9362 (user-error "No block of days selected"))
9363 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9364 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9365 ((equal char ?j)
9366 (org-switch-to-buffer-other-window
9367 (find-file-noselect org-agenda-diary-file))
9368 (require 'org-datetree)
9369 (org-datetree-find-date-create d1)
9370 (org-reveal t))
9371 (t (user-error "Invalid selection character `%c'" char)))))
9373 (defcustom org-agenda-insert-diary-strategy 'date-tree
9374 "Where in `org-agenda-diary-file' should new entries be added?
9375 Valid values:
9377 date-tree in the date tree, as child of the date
9378 top-level as top-level entries at the end of the file."
9379 :group 'org-agenda
9380 :type '(choice
9381 (const :tag "in a date tree" date-tree)
9382 (const :tag "as top level at end of file" top-level)))
9384 (defcustom org-agenda-insert-diary-extract-time nil
9385 "Non-nil means extract any time specification from the diary entry."
9386 :group 'org-agenda
9387 :version "24.1"
9388 :type 'boolean)
9390 (defcustom org-agenda-bulk-mark-char ">"
9391 "A single-character string to be used as the bulk mark."
9392 :group 'org-agenda
9393 :version "24.1"
9394 :type 'string)
9396 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9397 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9398 If TEXT is not empty, it will become the headline of the new entry, and
9399 the resulting entry will not be shown. When TEXT is empty, switch to
9400 `org-agenda-diary-file' and let the user finish the entry there."
9401 (let ((cw (current-window-configuration)))
9402 (org-switch-to-buffer-other-window
9403 (find-file-noselect org-agenda-diary-file))
9404 (widen)
9405 (goto-char (point-min))
9406 (cond
9407 ((eq type 'anniversary)
9408 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
9409 (progn
9410 (or (org-at-heading-p t)
9411 (progn
9412 (outline-next-heading)
9413 (insert "* Anniversaries\n\n")
9414 (beginning-of-line -1)))))
9415 (outline-next-heading)
9416 (org-back-over-empty-lines)
9417 (backward-char 1)
9418 (insert "\n")
9419 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9420 (nth 2 d1) (car d1) (nth 1 d1) text)))
9421 ((eq type 'day)
9422 (let ((org-prefix-has-time t)
9423 (org-agenda-time-leading-zero t)
9424 fmt time time2)
9425 (if org-agenda-insert-diary-extract-time
9426 ;; Use org-agenda-format-item to parse text for a time-range and
9427 ;; remove it. FIXME: This is a hack, we should refactor
9428 ;; that function to make time extraction available separately
9429 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9430 time (get-text-property 0 'time fmt)
9431 time2 (if (> (length time) 0)
9432 ;; split-string removes trailing ...... if
9433 ;; no end time given. First space
9434 ;; separates time from date.
9435 (concat " " (car (split-string time "\\.")))
9436 nil)
9437 text (get-text-property 0 'txt fmt)))
9438 (if (eq org-agenda-insert-diary-strategy 'top-level)
9439 (org-agenda-insert-diary-as-top-level text)
9440 (require 'org-datetree)
9441 (org-datetree-find-date-create d1)
9442 (org-agenda-insert-diary-make-new-entry text))
9443 (org-insert-time-stamp (org-time-from-absolute
9444 (calendar-absolute-from-gregorian d1))
9445 nil nil nil nil time2))
9446 (end-of-line 0))
9447 ((eq type 'block)
9448 (if (> (calendar-absolute-from-gregorian d1)
9449 (calendar-absolute-from-gregorian d2))
9450 (setq d1 (prog1 d2 (setq d2 d1))))
9451 (if (eq org-agenda-insert-diary-strategy 'top-level)
9452 (org-agenda-insert-diary-as-top-level text)
9453 (require 'org-datetree)
9454 (org-datetree-find-date-create d1)
9455 (org-agenda-insert-diary-make-new-entry text))
9456 (org-insert-time-stamp (org-time-from-absolute
9457 (calendar-absolute-from-gregorian d1)))
9458 (insert "--")
9459 (org-insert-time-stamp (org-time-from-absolute
9460 (calendar-absolute-from-gregorian d2)))
9461 (end-of-line 0)))
9462 (if (string-match "\\S-" text)
9463 (progn
9464 (set-window-configuration cw)
9465 (message "%s entry added to %s"
9466 (capitalize (symbol-name type))
9467 (abbreviate-file-name org-agenda-diary-file)))
9468 (org-reveal t)
9469 (message "Please finish entry here"))))
9471 (defun org-agenda-insert-diary-as-top-level (text)
9472 "Make new entry as a top-level entry at the end of the file.
9473 Add TEXT as headline, and position the cursor in the second line so that
9474 a timestamp can be added there."
9475 (widen)
9476 (goto-char (point-max))
9477 (or (bolp) (insert "\n"))
9478 (insert "* " text "\n")
9479 (if org-adapt-indentation (org-indent-to-column 2)))
9481 (defun org-agenda-insert-diary-make-new-entry (text)
9482 "Make new entry as last child of current entry.
9483 Add TEXT as headline, and position the cursor in the second line so that
9484 a timestamp can be added there."
9485 (let ((org-show-following-heading t)
9486 (org-show-siblings t)
9487 (org-show-hierarchy-above t)
9488 (org-show-entry-below t)
9489 col)
9490 (outline-next-heading)
9491 (org-back-over-empty-lines)
9492 (or (looking-at "[ \t]*$")
9493 (progn (insert "\n") (backward-char 1)))
9494 (org-insert-heading nil t)
9495 (org-do-demote)
9496 (setq col (current-column))
9497 (insert text "\n")
9498 (if org-adapt-indentation (org-indent-to-column col))
9499 (let ((org-show-following-heading t)
9500 (org-show-siblings t)
9501 (org-show-hierarchy-above t)
9502 (org-show-entry-below t))
9503 (org-show-context))))
9505 (defun org-agenda-diary-entry ()
9506 "Make a diary entry, like the `i' command from the calendar.
9507 All the standard commands work: block, weekly etc.
9508 When `org-agenda-diary-file' points to a file,
9509 `org-agenda-diary-entry-in-org-file' is called instead to create
9510 entries in that Org-mode file."
9511 (interactive)
9512 (if (not (eq org-agenda-diary-file 'diary-file))
9513 (org-agenda-diary-entry-in-org-file)
9514 (require 'diary-lib)
9515 (let* ((char (progn
9516 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
9517 (read-char-exclusive)))
9518 (cmd (cdr (assoc char
9519 '((?d . insert-diary-entry)
9520 (?w . insert-weekly-diary-entry)
9521 (?m . insert-monthly-diary-entry)
9522 (?y . insert-yearly-diary-entry)
9523 (?a . insert-anniversary-diary-entry)
9524 (?b . insert-block-diary-entry)
9525 (?c . insert-cyclic-diary-entry)))))
9526 (oldf (symbol-function 'calendar-cursor-to-date))
9527 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9528 (point (point))
9529 (mark (or (mark t) (point))))
9530 (unless cmd
9531 (user-error "No command associated with <%c>" char))
9532 (unless (and (get-text-property point 'day)
9533 (or (not (equal ?b char))
9534 (get-text-property mark 'day)))
9535 (user-error "Don't know which date to use for diary entry"))
9536 ;; We implement this by hacking the `calendar-cursor-to-date' function
9537 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9538 (let ((calendar-mark-ring
9539 (list (calendar-gregorian-from-absolute
9540 (or (get-text-property mark 'day)
9541 (get-text-property point 'day))))))
9542 (unwind-protect
9543 (progn
9544 (fset 'calendar-cursor-to-date
9545 (lambda (&optional error dummy)
9546 (calendar-gregorian-from-absolute
9547 (get-text-property point 'day))))
9548 (call-interactively cmd))
9549 (fset 'calendar-cursor-to-date oldf))))))
9551 (defun org-agenda-execute-calendar-command (cmd)
9552 "Execute a calendar command from the agenda with date from cursor."
9553 (org-agenda-check-type t 'agenda 'timeline)
9554 (require 'diary-lib)
9555 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9556 (user-error "Don't know which date to use for the calendar command"))
9557 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9558 (point (point))
9559 (date (calendar-gregorian-from-absolute
9560 (get-text-property point 'day)))
9561 ;; the following 2 vars are needed in the calendar
9562 (displayed-month (car date))
9563 (displayed-year (nth 2 date)))
9564 (unwind-protect
9565 (progn
9566 (fset 'calendar-cursor-to-date
9567 (lambda (&optional error dummy)
9568 (calendar-gregorian-from-absolute
9569 (get-text-property point 'day))))
9570 (call-interactively cmd))
9571 (fset 'calendar-cursor-to-date oldf))))
9573 (defun org-agenda-phases-of-moon ()
9574 "Display the phases of the moon for the 3 months around the cursor date."
9575 (interactive)
9576 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
9578 (defun org-agenda-holidays ()
9579 "Display the holidays for the 3 months around the cursor date."
9580 (interactive)
9581 (org-agenda-execute-calendar-command 'list-calendar-holidays))
9583 (defvar calendar-longitude) ; defined in calendar.el
9584 (defvar calendar-latitude) ; defined in calendar.el
9585 (defvar calendar-location-name) ; defined in calendar.el
9587 (defun org-agenda-sunrise-sunset (arg)
9588 "Display sunrise and sunset for the cursor date.
9589 Latitude and longitude can be specified with the variables
9590 `calendar-latitude' and `calendar-longitude'. When called with prefix
9591 argument, latitude and longitude will be prompted for."
9592 (interactive "P")
9593 (require 'solar)
9594 (let ((calendar-longitude (if arg nil calendar-longitude))
9595 (calendar-latitude (if arg nil calendar-latitude))
9596 (calendar-location-name
9597 (if arg "the given coordinates" calendar-location-name)))
9598 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9600 (defun org-agenda-goto-calendar ()
9601 "Open the Emacs calendar with the date at the cursor."
9602 (interactive)
9603 (org-agenda-check-type t 'agenda 'timeline)
9604 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9605 (user-error "Don't know which date to open in calendar")))
9606 (date (calendar-gregorian-from-absolute day))
9607 (calendar-move-hook nil)
9608 (calendar-view-holidays-initially-flag nil)
9609 (calendar-view-diary-initially-flag nil))
9610 (calendar)
9611 (calendar-goto-date date)))
9613 ;;;###autoload
9614 (defun org-calendar-goto-agenda ()
9615 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
9616 This is a command that has to be installed in `calendar-mode-map'."
9617 (interactive)
9618 (org-agenda-list nil (calendar-absolute-from-gregorian
9619 (calendar-cursor-to-date))
9620 nil))
9622 (defun org-agenda-convert-date ()
9623 (interactive)
9624 (org-agenda-check-type t 'agenda 'timeline)
9625 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9626 date s)
9627 (unless day
9628 (user-error "Don't know which date to convert"))
9629 (setq date (calendar-gregorian-from-absolute day))
9630 (setq s (concat
9631 "Gregorian: " (calendar-date-string date) "\n"
9632 "ISO: " (calendar-iso-date-string date) "\n"
9633 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9634 "Julian: " (calendar-julian-date-string date) "\n"
9635 "Astron. JD: " (calendar-astro-date-string date)
9636 " (Julian date number at noon UTC)\n"
9637 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9638 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9639 "French: " (calendar-french-date-string date) "\n"
9640 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
9641 "Mayan: " (calendar-mayan-date-string date) "\n"
9642 "Coptic: " (calendar-coptic-date-string date) "\n"
9643 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9644 "Persian: " (calendar-persian-date-string date) "\n"
9645 "Chinese: " (calendar-chinese-date-string date) "\n"))
9646 (with-output-to-temp-buffer "*Dates*"
9647 (princ s))
9648 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9650 ;;; Bulk commands
9652 (defun org-agenda-bulk-marked-p ()
9653 (eq (get-char-property (point-at-bol) 'type)
9654 'org-marked-entry-overlay))
9656 (defun org-agenda-bulk-mark (&optional arg)
9657 "Mark the entry at point for future bulk action."
9658 (interactive "p")
9659 (dotimes (i (or arg 1))
9660 (unless (org-get-at-bol 'org-agenda-diary-link)
9661 (let* ((m (org-get-at-bol 'org-hd-marker))
9663 (unless (org-agenda-bulk-marked-p)
9664 (unless m (user-error "Nothing to mark at point"))
9665 (push m org-agenda-bulk-marked-entries)
9666 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9667 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9668 (org-get-todo-face "TODO")
9669 'evaporate)
9670 (overlay-put ov 'type 'org-marked-entry-overlay))
9671 (end-of-line 1)
9672 (or (ignore-errors
9673 (goto-char (next-single-property-change (point) 'txt)))
9674 (beginning-of-line 2))
9675 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9676 (beginning-of-line 2))
9677 (message "%d entries marked for bulk action"
9678 (length org-agenda-bulk-marked-entries))))))
9680 (defun org-agenda-bulk-mark-all ()
9681 "Mark all entries for future agenda bulk action."
9682 (interactive)
9683 (org-agenda-bulk-mark-regexp "."))
9685 (defun org-agenda-bulk-mark-regexp (regexp)
9686 "Mark entries matching REGEXP for future agenda bulk action."
9687 (interactive "sMark entries matching regexp: ")
9688 (let ((entries-marked 0) txt-at-point)
9689 (save-excursion
9690 (goto-char (point-min))
9691 (goto-char (next-single-property-change (point) 'txt))
9692 (while (and (re-search-forward regexp nil t)
9693 (setq txt-at-point (get-text-property (point) 'txt)))
9694 (when (string-match regexp txt-at-point)
9695 (setq entries-marked (1+ entries-marked))
9696 (call-interactively 'org-agenda-bulk-mark))))
9697 (if (not entries-marked)
9698 (message "No entry matching this regexp."))))
9700 (defun org-agenda-bulk-unmark (&optional arg)
9701 "Unmark the entry at point for future bulk action."
9702 (interactive "P")
9703 (if arg
9704 (org-agenda-bulk-unmark-all)
9705 (cond ((org-agenda-bulk-marked-p)
9706 (org-agenda-bulk-remove-overlays
9707 (point-at-bol) (+ 2 (point-at-bol)))
9708 (setq org-agenda-bulk-marked-entries
9709 (delete (org-get-at-bol 'org-hd-marker)
9710 org-agenda-bulk-marked-entries))
9711 (end-of-line 1)
9712 (or (ignore-errors
9713 (goto-char (next-single-property-change (point) 'txt)))
9714 (beginning-of-line 2))
9715 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9716 (beginning-of-line 2))
9717 (message "%d entries left marked for bulk action"
9718 (length org-agenda-bulk-marked-entries)))
9719 (t (message "No entry to unmark here")))))
9721 (defun org-agenda-bulk-toggle-all ()
9722 "Toggle all marks for bulk action."
9723 (interactive)
9724 (save-excursion
9725 (goto-char (point-min))
9726 (while (ignore-errors
9727 (goto-char (next-single-property-change (point) 'txt)))
9728 (org-agenda-bulk-toggle))))
9730 (defun org-agenda-bulk-toggle ()
9731 "Toggle the mark at point for bulk action."
9732 (interactive)
9733 (if (org-agenda-bulk-marked-p)
9734 (org-agenda-bulk-unmark)
9735 (org-agenda-bulk-mark)))
9737 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9738 "Remove the mark overlays between BEG and END in the agenda buffer.
9739 BEG and END default to the buffer limits.
9741 This only removes the overlays, it does not remove the markers
9742 from the list in `org-agenda-bulk-marked-entries'."
9743 (interactive)
9744 (mapc (lambda (ov)
9745 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9746 (delete-overlay ov)))
9747 (overlays-in (or beg (point-min)) (or end (point-max)))))
9749 (defun org-agenda-bulk-unmark-all ()
9750 "Remove all marks in the agenda buffer.
9751 This will remove the markers and the overlays."
9752 (interactive)
9753 (if (null org-agenda-bulk-marked-entries)
9754 (message "No entry to unmark")
9755 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
9756 (setq org-agenda-bulk-marked-entries nil)
9757 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9759 (defcustom org-agenda-persistent-marks nil
9760 "Non-nil means marked items will stay marked after a bulk action.
9761 You can toggle this interactively by typing `p' when prompted for a
9762 bulk action."
9763 :group 'org-agenda
9764 :version "24.1"
9765 :type 'boolean)
9767 (defun org-agenda-bulk-action (&optional arg)
9768 "Execute an remote-editing action on all marked entries.
9769 The prefix arg is passed through to the command if possible."
9770 (interactive "P")
9771 ;; Make sure we have markers, and only valid ones
9772 (unless org-agenda-bulk-marked-entries (user-error "No entries are marked"))
9773 (mapc
9774 (lambda (m)
9775 (unless (and (markerp m)
9776 (marker-buffer m)
9777 (buffer-live-p (marker-buffer m))
9778 (marker-position m))
9779 (user-error "Marker %s for bulk command is invalid" m)))
9780 org-agenda-bulk-marked-entries)
9782 ;; Prompt for the bulk command
9783 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
9784 (message (concat msg "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9785 "[S]catter [f]unction "
9786 (when org-agenda-bulk-custom-functions
9787 (concat " Custom: ["
9788 (mapconcat (lambda(f) (char-to-string (car f)))
9789 org-agenda-bulk-custom-functions "")
9790 "]"))))
9791 (catch 'exit
9792 (let* ((action (read-char-exclusive))
9793 (org-log-refile (if org-log-refile 'time nil))
9794 (entries (reverse org-agenda-bulk-marked-entries))
9795 (org-overriding-default-time
9796 (if (get-text-property (point) 'org-agenda-date-header)
9797 (org-get-cursor-date)))
9798 redo-at-end
9799 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
9800 (cond
9801 ((equal action ?p)
9802 (let ((org-agenda-persistent-marks
9803 (not org-agenda-persistent-marks)))
9804 (org-agenda-bulk-action)
9805 (throw 'exit nil)))
9807 ((equal action ?$)
9808 (setq cmd '(org-agenda-archive)))
9810 ((equal action ?A)
9811 (setq cmd '(org-agenda-archive-to-archive-sibling)))
9813 ((member action '(?r ?w))
9814 (setq rfloc (org-refile-get-location
9815 "Refile to"
9816 (marker-buffer (car entries))
9817 org-refile-allow-creating-parent-nodes))
9818 (if (nth 3 rfloc)
9819 (setcar (nthcdr 3 rfloc)
9820 (move-marker (make-marker) (nth 3 rfloc)
9821 (or (get-file-buffer (nth 1 rfloc))
9822 (find-buffer-visiting (nth 1 rfloc))
9823 (error "This should not happen")))))
9825 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
9826 redo-at-end t))
9828 ((equal action ?t)
9829 (setq state (org-icompleting-read
9830 "Todo state: "
9831 (with-current-buffer (marker-buffer (car entries))
9832 (mapcar 'list org-todo-keywords-1))))
9833 (setq cmd `(let ((org-inhibit-blocking t)
9834 (org-inhibit-logging 'note))
9835 (org-agenda-todo ,state))))
9837 ((memq action '(?- ?+))
9838 (setq tag (org-icompleting-read
9839 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9840 (with-current-buffer (marker-buffer (car entries))
9841 (delq nil
9842 (mapcar (lambda (x)
9843 (if (stringp (car x)) x)) org-tag-alist)))))
9844 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
9846 ((memq action '(?s ?d))
9847 (let* ((time
9848 (unless arg
9849 (org-read-date
9850 nil nil nil
9851 (if (eq action ?s) "(Re)Schedule to" "(Re)Set Deadline to")
9852 org-overriding-default-time)))
9853 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
9854 (setq cmd `(eval '(,c1 arg ,time)))))
9856 ((equal action ?S)
9857 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
9858 (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
9859 (let ((days (read-number
9860 (format "Scatter tasks across how many %sdays: "
9861 (if arg "week" "")) 7)))
9862 (setq cmd
9863 `(let ((distance (1+ (random ,days))))
9864 (if arg
9865 (let ((dist distance)
9866 (day-of-week
9867 (calendar-day-of-week
9868 (calendar-gregorian-from-absolute (org-today)))))
9869 (dotimes (i (1+ dist))
9870 (while (member day-of-week org-agenda-weekend-days)
9871 (incf distance)
9872 (incf day-of-week)
9873 (if (= day-of-week 7)
9874 (setq day-of-week 0)))
9875 (incf day-of-week)
9876 (if (= day-of-week 7)
9877 (setq day-of-week 0)))))
9878 ;; silently fail when try to replan a sexp entry
9879 (condition-case nil
9880 (let* ((date (calendar-gregorian-from-absolute
9881 (+ (org-today) distance)))
9882 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9883 (nth 2 date))))
9884 (org-agenda-schedule nil time))
9885 (error nil)))))))
9887 ((assoc action org-agenda-bulk-custom-functions)
9888 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
9889 redo-at-end t))
9891 ((equal action ?f)
9892 (setq cmd (list (intern
9893 (org-icompleting-read "Function: "
9894 obarray 'fboundp t nil nil)))))
9896 (t (user-error "Invalid bulk action")))
9898 ;; Sort the markers, to make sure that parents are handled before children
9899 (setq entries (sort entries
9900 (lambda (a b)
9901 (cond
9902 ((equal (marker-buffer a) (marker-buffer b))
9903 (< (marker-position a) (marker-position b)))
9905 (string< (buffer-name (marker-buffer a))
9906 (buffer-name (marker-buffer b))))))))
9908 ;; Now loop over all markers and apply cmd
9909 (while (setq e (pop entries))
9910 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
9911 (if (not pos)
9912 (progn (message "Skipping removed entry at %s" e)
9913 (setq cntskip (1+ cntskip)))
9914 (goto-char pos)
9915 (let (org-loop-over-headlines-in-active-region)
9916 (eval cmd))
9917 (setq cnt (1+ cnt))))
9918 (when redo-at-end (org-agenda-redo))
9919 (unless org-agenda-persistent-marks
9920 (org-agenda-bulk-unmark-all))
9921 (message "Acted on %d entries%s%s"
9923 (if (= cntskip 0)
9925 (format ", skipped %d (disappeared before their turn)"
9926 cntskip))
9927 (if (not org-agenda-persistent-marks)
9928 "" " (kept marked)"))))))
9930 (defun org-agenda-capture (&optional with-time)
9931 "Call `org-capture' with the date at point.
9932 With a `C-1' prefix, use the HH:MM value at point (if any) or the
9933 current HH:MM time."
9934 (interactive "P")
9935 (if (not (eq major-mode 'org-agenda-mode))
9936 (user-error "You cannot do this outside of agenda buffers")
9937 (let ((org-overriding-default-time
9938 (org-get-cursor-date (equal with-time 1))))
9939 (call-interactively 'org-capture))))
9941 ;;; Dragging agenda lines forward/backward
9943 (defun org-agenda-reapply-filters ()
9944 "Re-apply all agenda filters."
9945 (mapcar
9946 (lambda(f) (when (car f) (org-agenda-filter-apply (car f) (cadr f))))
9947 `((,org-agenda-tag-filter tag)
9948 (,org-agenda-category-filter category)
9949 (,org-agenda-regexp-filter regexp)
9950 (,(get 'org-agenda-tag-filter :preset-filter) tag)
9951 (,(get 'org-agenda-category-filter :preset-filter) category)
9952 (,(get 'org-agenda-regexp-filter :preset-filter) regexp))))
9954 (defun org-agenda-drag-line-forward (arg &optional backward)
9955 "Drag an agenda line forward by ARG lines.
9956 When the optional argument `backward' is non-nil, move backward."
9957 (interactive "p")
9958 (let ((inhibit-read-only t) lst)
9959 (if (or (not (get-text-property (point) 'txt))
9960 (save-excursion
9961 (dotimes (n arg)
9962 (move-beginning-of-line (if backward 0 2))
9963 (forward-char 1)
9964 (push (not (get-text-property (point) 'txt)) lst))
9965 (delq nil lst)))
9966 (message "Cannot move line %s" (if backward "backward" "forward"))
9967 (let ((end (save-excursion (move-end-of-line 1) (point)))
9968 (col (current-column)) line)
9969 (move-beginning-of-line 1)
9970 (setq line (buffer-substring (point) end))
9971 (delete-region (point) (1+ end))
9972 (move-end-of-line (funcall (if backward '1- '1+) (1- arg)))
9973 (insert "\n" line)
9974 (org-move-to-column col)
9975 (org-agenda-reapply-filters)
9976 (org-agenda-mark-clocking-task)))))
9978 (defun org-agenda-drag-line-backward (arg)
9979 "Drag an agenda line backward by ARG lines."
9980 (interactive "p")
9981 (org-agenda-drag-line-forward arg t))
9983 ;;; Flagging notes
9985 (defun org-agenda-show-the-flagging-note ()
9986 "Display the flagging note in the other window.
9987 When called a second time in direct sequence, offer to remove the FLAGGING
9988 tag and (if present) the flagging note."
9989 (interactive)
9990 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
9991 (win (selected-window))
9992 note heading newhead)
9993 (unless hdmarker
9994 (user-error "No linked entry at point"))
9995 (if (and (eq this-command last-command)
9996 (y-or-n-p "Unflag and remove any flagging note? "))
9997 (progn
9998 (org-agenda-remove-flag hdmarker)
9999 (let ((win (get-buffer-window "*Flagging Note*")))
10000 (and win (delete-window win)))
10001 (message "Entry unflagged"))
10002 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
10003 (unless note
10004 (user-error "No flagging note"))
10005 (org-kill-new note)
10006 (org-switch-to-buffer-other-window "*Flagging Note*")
10007 (erase-buffer)
10008 (insert note)
10009 (goto-char (point-min))
10010 (while (re-search-forward "\\\\n" nil t)
10011 (replace-match "\n" t t))
10012 (goto-char (point-min))
10013 (select-window win)
10014 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
10016 (defun org-agenda-remove-flag (marker)
10017 "Remove the FLAGGED tag and any flagging note in the entry."
10018 (let (newhead)
10019 (org-with-point-at marker
10020 (org-toggle-tag "FLAGGED" 'off)
10021 (org-entry-delete nil "THEFLAGGINGNOTE")
10022 (setq newhead (org-get-heading)))
10023 (org-agenda-change-all-lines newhead marker)
10024 (message "Entry unflagged")))
10026 (defun org-agenda-get-any-marker (&optional pos)
10027 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
10028 (get-text-property (or pos (point-at-bol)) 'org-marker)))
10030 ;;; Appointment reminders
10032 (defvar appt-time-msg-list) ; defined in appt.el
10034 ;;;###autoload
10035 (defun org-agenda-to-appt (&optional refresh filter &rest args)
10036 "Activate appointments found in `org-agenda-files'.
10037 With a \\[universal-argument] prefix, refresh the list of
10038 appointments.
10040 If FILTER is t, interactively prompt the user for a regular
10041 expression, and filter out entries that don't match it.
10043 If FILTER is a string, use this string as a regular expression
10044 for filtering entries out.
10046 If FILTER is a function, filter out entries against which
10047 calling the function returns nil. This function takes one
10048 argument: an entry from `org-agenda-get-day-entries'.
10050 FILTER can also be an alist with the car of each cell being
10051 either 'headline or 'category. For example:
10053 '((headline \"IMPORTANT\")
10054 (category \"Work\"))
10056 will only add headlines containing IMPORTANT or headlines
10057 belonging to the \"Work\" category.
10059 ARGS are symbols indicating what kind of entries to consider.
10060 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
10061 \(i.e., deadlines and scheduled items with a hh:mm specification)
10062 and :timestamp entries. See the docstring of `org-diary' for
10063 details and examples.
10065 If an entry has a APPT_WARNTIME property, its value will be used
10066 to override `appt-message-warning-time'."
10067 (interactive "P")
10068 (if refresh (setq appt-time-msg-list nil))
10069 (if (eq filter t)
10070 (setq filter (read-from-minibuffer "Regexp filter: ")))
10071 (let* ((cnt 0) ; count added events
10072 (scope (or args '(:deadline* :scheduled* :timestamp)))
10073 (org-agenda-new-buffers nil)
10074 (org-deadline-warning-days 0)
10075 ;; Do not use `org-today' here because appt only takes
10076 ;; time and without date as argument, so it may pass wrong
10077 ;; information otherwise
10078 (today (org-date-to-gregorian
10079 (time-to-days (current-time))))
10080 (org-agenda-restrict nil)
10081 (files (org-agenda-files 'unrestricted)) entries file
10082 (org-agenda-buffer nil))
10083 ;; Get all entries which may contain an appt
10084 (org-agenda-prepare-buffers files)
10085 (while (setq file (pop files))
10086 (setq entries
10087 (delq nil
10088 (append entries
10089 (apply 'org-agenda-get-day-entries
10090 file today scope)))))
10091 ;; Map thru entries and find if we should filter them out
10092 (mapc
10093 (lambda(x)
10094 (let* ((evt (org-trim
10095 (replace-regexp-in-string
10096 org-bracket-link-regexp "\\3"
10097 (or (get-text-property 1 'txt x) ""))))
10098 (cat (get-text-property 1 'org-category x))
10099 (tod (get-text-property 1 'time-of-day x))
10100 (ok (or (null filter)
10101 (and (stringp filter) (string-match filter evt))
10102 (and (functionp filter) (funcall filter x))
10103 (and (listp filter)
10104 (let ((cat-filter (cadr (assoc 'category filter)))
10105 (evt-filter (cadr (assoc 'headline filter))))
10106 (or (and (stringp cat-filter)
10107 (string-match cat-filter cat))
10108 (and (stringp evt-filter)
10109 (string-match evt-filter evt)))))))
10110 (wrn (get-text-property 1 'warntime x)))
10111 ;; FIXME: Shall we remove text-properties for the appt text?
10112 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
10113 (when (and ok tod)
10114 (setq tod (concat "00" (number-to-string tod))
10115 tod (when (string-match
10116 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
10117 (concat (match-string 1 tod) ":"
10118 (match-string 2 tod))))
10119 (if (version< emacs-version "23.3")
10120 (appt-add tod evt)
10121 (appt-add tod evt wrn))
10122 (setq cnt (1+ cnt))))) entries)
10123 (org-release-buffers org-agenda-new-buffers)
10124 (if (eq cnt 0)
10125 (message "No event to add")
10126 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
10128 (defun org-agenda-todayp (date)
10129 "Does DATE mean today, when considering `org-extend-today-until'?"
10130 (let ((today (org-today))
10131 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
10132 date)))
10133 (eq date today)))
10135 (defun org-agenda-todo-yesterday (&optional arg)
10136 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
10137 (interactive "P")
10138 (let* ((hour (third (decode-time
10139 (org-current-time))))
10140 (org-extend-today-until (1+ hour)))
10141 (org-agenda-todo arg)))
10143 (provide 'org-agenda)
10145 ;;; org-agenda.el ends here