Merge branch 'maint'
[org-mode/org-tableheadings.git] / lisp / org-agenda.el
blob576d5b92ba469e632de3598110285d03499c9aaa
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 search agenda view."
654 :tag "Org Agenda Search 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 (setq indent-tabs-mode nil)
2149 (use-local-map org-agenda-mode-map)
2150 (easy-menu-add org-agenda-menu)
2151 (if org-startup-truncated (setq truncate-lines t))
2152 (org-set-local 'line-move-visual nil)
2153 (org-add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
2154 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
2155 ;; Make sure properties are removed when copying text
2156 (org-add-hook 'filter-buffer-substring-functions
2157 (lambda (fun start end delete)
2158 (substring-no-properties (funcall fun start end delete)))
2159 nil t)
2160 (unless org-agenda-keep-modes
2161 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
2162 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
2163 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
2164 org-agenda-show-log org-agenda-start-with-log-mode))
2166 (easy-menu-change
2167 '("Agenda") "Agenda Files"
2168 (append
2169 (list
2170 (vector
2171 (if (get 'org-agenda-files 'org-restrict)
2172 "Restricted to single file"
2173 "Edit File List")
2174 '(org-edit-agenda-file-list)
2175 (not (get 'org-agenda-files 'org-restrict)))
2176 "--")
2177 (mapcar 'org-file-menu-entry (org-agenda-files))))
2178 (org-agenda-set-mode-name)
2179 (apply
2180 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2181 (list 'org-agenda-mode-hook)))
2183 (substitute-key-definition 'undo 'org-agenda-undo
2184 org-agenda-mode-map global-map)
2185 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2186 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2187 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2188 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2189 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2190 (org-defkey org-agenda-mode-map [(meta down)] 'org-agenda-drag-line-forward)
2191 (org-defkey org-agenda-mode-map [(meta up)] 'org-agenda-drag-line-backward)
2192 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2193 (org-defkey org-agenda-mode-map "\M-m" 'org-agenda-bulk-toggle)
2194 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2195 (org-defkey org-agenda-mode-map "\M-*" 'org-agenda-bulk-toggle-all)
2196 (org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
2197 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2198 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2199 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2200 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2201 (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2202 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2203 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2204 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2205 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2206 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2207 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2208 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2209 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2210 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2211 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2212 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2213 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2214 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2215 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2216 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2217 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2218 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2219 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2220 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2221 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2222 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2223 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2224 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2225 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2226 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2227 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2228 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2229 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2230 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2231 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2232 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2233 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2234 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2236 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2237 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2238 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2239 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2240 (while l (org-defkey org-agenda-mode-map
2241 (int-to-string (pop l)) 'digit-argument)))
2243 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2244 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2245 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2246 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2247 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2248 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2249 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2250 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2251 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2252 (org-defkey org-agenda-mode-map "g" (lambda () (interactive) (org-agenda-redo t)))
2253 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2254 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2255 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2256 'org-clock-modify-effort-estimate)
2257 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2258 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2259 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2260 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2261 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2262 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2263 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2264 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2265 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2266 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2267 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2268 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2269 (substitute-key-definition 'next-line 'org-agenda-next-line
2270 org-agenda-mode-map global-map)
2271 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2272 org-agenda-mode-map global-map)
2273 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2274 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2275 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2276 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2277 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2278 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2279 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2280 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2281 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2282 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2283 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2284 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2285 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2286 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2287 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2288 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2289 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2290 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2291 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2292 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2293 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2294 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2295 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2296 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2297 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2298 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2299 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2300 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2301 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2302 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2304 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2305 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2306 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2307 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2308 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2309 (org-defkey org-agenda-mode-map "=" 'org-agenda-filter-by-regexp)
2310 (org-defkey org-agenda-mode-map "|" 'org-agenda-filter-remove-all)
2311 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
2312 (org-defkey org-agenda-mode-map "~" 'org-agenda-limit-interactively)
2313 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2314 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-headline)
2315 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2316 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2317 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2318 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2320 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2321 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2323 (define-key org-agenda-mode-map [remap forward-paragraph] 'org-agenda-forward-block)
2324 (define-key org-agenda-mode-map [remap backward-paragraph] 'org-agenda-backward-block)
2326 (when org-agenda-mouse-1-follows-link
2327 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2328 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2329 '("Agenda"
2330 ("Agenda Files")
2331 "--"
2332 ("Agenda Dates"
2333 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2334 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2335 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2336 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2337 "--"
2338 ("View"
2339 ["Day View" org-agenda-day-view
2340 :active (org-agenda-check-type nil 'agenda)
2341 :style radio :selected (eq org-agenda-current-span 'day)
2342 :keys "v d (or just d)"]
2343 ["Week View" org-agenda-week-view
2344 :active (org-agenda-check-type nil 'agenda)
2345 :style radio :selected (eq org-agenda-current-span 'week)
2346 :keys "v w"]
2347 ["Fortnight View" org-agenda-fortnight-view
2348 :active (org-agenda-check-type nil 'agenda)
2349 :style radio :selected (eq org-agenda-current-span 'fortnight)
2350 :keys "v f"]
2351 ["Month View" org-agenda-month-view
2352 :active (org-agenda-check-type nil 'agenda)
2353 :style radio :selected (eq org-agenda-current-span 'month)
2354 :keys "v m"]
2355 ["Year View" org-agenda-year-view
2356 :active (org-agenda-check-type nil 'agenda)
2357 :style radio :selected (eq org-agenda-current-span 'year)
2358 :keys "v y"]
2359 "--"
2360 ["Include Diary" org-agenda-toggle-diary
2361 :style toggle :selected org-agenda-include-diary
2362 :active (org-agenda-check-type nil 'agenda)]
2363 ["Include Deadlines" org-agenda-toggle-deadlines
2364 :style toggle :selected org-agenda-include-deadlines
2365 :active (org-agenda-check-type nil 'agenda)]
2366 ["Use Time Grid" org-agenda-toggle-time-grid
2367 :style toggle :selected org-agenda-use-time-grid
2368 :active (org-agenda-check-type nil 'agenda)]
2369 "--"
2370 ["Show clock report" org-agenda-clockreport-mode
2371 :style toggle :selected org-agenda-clockreport-mode
2372 :active (org-agenda-check-type nil 'agenda)]
2373 ["Show some entry text" org-agenda-entry-text-mode
2374 :style toggle :selected org-agenda-entry-text-mode
2375 :active t]
2376 "--"
2377 ["Show Logbook entries" org-agenda-log-mode
2378 :style toggle :selected org-agenda-show-log
2379 :active (org-agenda-check-type nil 'agenda 'timeline)
2380 :keys "v l (or just l)"]
2381 ["Include archived trees" org-agenda-archives-mode
2382 :style toggle :selected org-agenda-archives-mode :active t
2383 :keys "v a"]
2384 ["Include archive files" (org-agenda-archives-mode t)
2385 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2386 :keys "v A"]
2387 "--"
2388 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2389 ["Write view to file" org-agenda-write t]
2390 ["Rebuild buffer" org-agenda-redo t]
2391 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2392 "--"
2393 ["Show original entry" org-agenda-show t]
2394 ["Go To (other window)" org-agenda-goto t]
2395 ["Go To (this window)" org-agenda-switch-to t]
2396 ["Capture with cursor date" org-agenda-capture t]
2397 ["Follow Mode" org-agenda-follow-mode
2398 :style toggle :selected org-agenda-follow-mode :active t]
2399 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2400 "--"
2401 ("TODO"
2402 ["Cycle TODO" org-agenda-todo t]
2403 ["Next TODO set" org-agenda-todo-nextset t]
2404 ["Previous TODO set" org-agenda-todo-previousset t]
2405 ["Add note" org-agenda-add-note t])
2406 ("Archive/Refile/Delete"
2407 ["Archive default" org-agenda-archive-default t]
2408 ["Archive default" org-agenda-archive-default-with-confirmation t]
2409 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2410 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2411 ["Archive subtree" org-agenda-archive t]
2412 "--"
2413 ["Refile" org-agenda-refile t]
2414 "--"
2415 ["Delete subtree" org-agenda-kill t])
2416 ("Bulk action"
2417 ["Mark entry" org-agenda-bulk-mark t]
2418 ["Mark all" org-agenda-bulk-mark-all t]
2419 ["Unmark entry" org-agenda-bulk-unmark t]
2420 ["Unmark all" org-agenda-bulk-unmark-all :active t :keys "U"]
2421 ["Toggle mark" org-agenda-bulk-toggle t]
2422 ["Toggle all" org-agenda-bulk-toggle-all t]
2423 ["Mark regexp" org-agenda-bulk-mark-regexp t])
2424 ["Act on all marked" org-agenda-bulk-action t]
2425 "--"
2426 ("Tags and Properties"
2427 ["Show all Tags" org-agenda-show-tags t]
2428 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2429 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2430 "--"
2431 ["Column View" org-columns t])
2432 ("Deadline/Schedule"
2433 ["Schedule" org-agenda-schedule t]
2434 ["Set Deadline" org-agenda-deadline t]
2435 "--"
2436 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2437 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2438 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2439 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2440 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2441 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2442 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2443 ("Clock and Effort"
2444 ["Clock in" org-agenda-clock-in t]
2445 ["Clock out" org-agenda-clock-out t]
2446 ["Clock cancel" org-agenda-clock-cancel t]
2447 ["Goto running clock" org-clock-goto t]
2448 "--"
2449 ["Set Effort" org-agenda-set-effort t]
2450 ["Change clocked effort" org-clock-modify-effort-estimate
2451 (org-clock-is-active)])
2452 ("Priority"
2453 ["Set Priority" org-agenda-priority t]
2454 ["Increase Priority" org-agenda-priority-up t]
2455 ["Decrease Priority" org-agenda-priority-down t]
2456 ["Show Priority" org-show-priority t])
2457 ("Calendar/Diary"
2458 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2459 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2460 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2461 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2462 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2463 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2464 "--"
2465 ["Create iCalendar File" org-icalendar-combine-agenda-files t])
2466 "--"
2467 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2468 "--"
2469 ("MobileOrg"
2470 ["Push Files and Views" org-mobile-push t]
2471 ["Get Captured and Flagged" org-mobile-pull t]
2472 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2473 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2474 "--"
2475 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2476 "--"
2477 ["Quit" org-agenda-quit t]
2478 ["Exit and Release Buffers" org-agenda-exit t]
2481 ;;; Agenda undo
2483 (defvar org-agenda-allow-remote-undo t
2484 "Non-nil means allow remote undo from the agenda buffer.")
2485 (defvar org-agenda-undo-has-started-in nil
2486 "Buffers that have already seen `undo-start' in the current undo sequence.")
2488 (defun org-agenda-undo ()
2489 "Undo a remote editing step in the agenda.
2490 This undoes changes both in the agenda buffer and in the remote buffer
2491 that have been changed along."
2492 (interactive)
2493 (or org-agenda-allow-remote-undo
2494 (user-error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2495 (if (not (eq this-command last-command))
2496 (setq org-agenda-undo-has-started-in nil
2497 org-agenda-pending-undo-list org-agenda-undo-list))
2498 (if (not org-agenda-pending-undo-list)
2499 (user-error "No further undo information"))
2500 (let* ((entry (pop org-agenda-pending-undo-list))
2501 buf line cmd rembuf)
2502 (setq cmd (pop entry) line (pop entry))
2503 (setq rembuf (nth 2 entry))
2504 (org-with-remote-undo rembuf
2505 (while (bufferp (setq buf (pop entry)))
2506 (if (pop entry)
2507 (with-current-buffer buf
2508 (let ((last-undo-buffer buf)
2509 (inhibit-read-only t))
2510 (unless (memq buf org-agenda-undo-has-started-in)
2511 (push buf org-agenda-undo-has-started-in)
2512 (make-local-variable 'pending-undo-list)
2513 (undo-start))
2514 (while (and pending-undo-list
2515 (listp pending-undo-list)
2516 (not (car pending-undo-list)))
2517 (pop pending-undo-list))
2518 (undo-more 1))))))
2519 (org-goto-line line)
2520 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2522 (defun org-verify-change-for-undo (l1 l2)
2523 "Verify that a real change occurred between the undo lists L1 and L2."
2524 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2525 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2526 (not (eq l1 l2)))
2528 ;;; Agenda dispatch
2530 (defvar org-agenda-restrict-begin (make-marker))
2531 (defvar org-agenda-restrict-end (make-marker))
2532 (defvar org-agenda-last-dispatch-buffer nil)
2533 (defvar org-agenda-overriding-restriction nil)
2535 (defcustom org-agenda-custom-commands-contexts nil
2536 "Alist of custom agenda keys and contextual rules.
2538 For example, if you have a custom agenda command \"p\" and you
2539 want this command to be accessible only from plain text files,
2540 use this:
2542 '((\"p\" ((in-file . \"\\\\.txt\\\\'\"))))
2544 Here are the available contexts definitions:
2546 in-file: command displayed only in matching files
2547 in-mode: command displayed only in matching modes
2548 not-in-file: command not displayed in matching files
2549 not-in-mode: command not displayed in matching modes
2550 in-buffer: command displayed only in matching buffers
2551 not-in-buffer: command not displayed in matching buffers
2552 [function]: a custom function taking no argument
2554 If you define several checks, the agenda command will be
2555 accessible if there is at least one valid check.
2557 You can also bind a key to another agenda custom command
2558 depending on contextual rules.
2560 '((\"p\" \"q\" ((in-file . \"\\\\.txt\\\\'\"))))
2562 Here it means: in .txt files, use \"p\" as the key for the
2563 agenda command otherwise associated with \"q\". (The command
2564 originally associated with \"q\" is not displayed to avoid
2565 duplicates.)"
2566 :version "24.3"
2567 :group 'org-agenda-custom-commands
2568 :type '(repeat (list :tag "Rule"
2569 (string :tag " Agenda key")
2570 (string :tag "Replace by command")
2571 (repeat :tag "Available when"
2572 (choice
2573 (cons :tag "Condition"
2574 (choice
2575 (const :tag "In file" in-file)
2576 (const :tag "Not in file" not-in-file)
2577 (const :tag "In buffer" in-buffer)
2578 (const :tag "Not in buffer" not-in-buffer)
2579 (const :tag "In mode" in-mode)
2580 (const :tag "Not in mode" not-in-mode))
2581 (regexp))
2582 (function :tag "Custom function"))))))
2584 (defcustom org-agenda-max-entries nil
2585 "Maximum number of entries to display in an agenda.
2586 This can be nil (no limit) or an integer or an alist of agenda
2587 types with an associated number of entries to display in this
2588 type."
2589 :version "24.4"
2590 :package-version '(Org . "8.0")
2591 :group 'org-agenda-custom-commands
2592 :type '(choice (symbol :tag "No limit" nil)
2593 (integer :tag "Max number of entries")
2594 (repeat
2595 (cons (choice :tag "Agenda type"
2596 (const agenda)
2597 (const todo)
2598 (const tags)
2599 (const search)
2600 (const timeline))
2601 (integer :tag "Max number of entries")))))
2603 (defcustom org-agenda-max-todos nil
2604 "Maximum number of TODOs to display in an agenda.
2605 This can be nil (no limit) or an integer or an alist of agenda
2606 types with an associated number of entries to display in this
2607 type."
2608 :version "24.4"
2609 :package-version '(Org . "8.0")
2610 :group 'org-agenda-custom-commands
2611 :type '(choice (symbol :tag "No limit" nil)
2612 (integer :tag "Max number of TODOs")
2613 (repeat
2614 (cons (choice :tag "Agenda type"
2615 (const agenda)
2616 (const todo)
2617 (const tags)
2618 (const search)
2619 (const timeline))
2620 (integer :tag "Max number of TODOs")))))
2622 (defcustom org-agenda-max-tags nil
2623 "Maximum number of tagged entries to display in an agenda.
2624 This can be nil (no limit) or an integer or an alist of agenda
2625 types with an associated number of entries to display in this
2626 type."
2627 :version "24.4"
2628 :package-version '(Org . "8.0")
2629 :group 'org-agenda-custom-commands
2630 :type '(choice (symbol :tag "No limit" nil)
2631 (integer :tag "Max number of tagged entries")
2632 (repeat
2633 (cons (choice :tag "Agenda type"
2634 (const agenda)
2635 (const todo)
2636 (const tags)
2637 (const search)
2638 (const timeline))
2639 (integer :tag "Max number of tagged entries")))))
2641 (defcustom org-agenda-max-effort nil
2642 "Maximum cumulated effort duration for the agenda.
2643 This can be nil (no limit) or a number of minutes (as an integer)
2644 or an alist of agenda types with an associated number of minutes
2645 to limit entries to in this type."
2646 :version "24.4"
2647 :package-version '(Org . "8.0")
2648 :group 'org-agenda-custom-commands
2649 :type '(choice (symbol :tag "No limit" nil)
2650 (integer :tag "Max number of minutes")
2651 (repeat
2652 (cons (choice :tag "Agenda type"
2653 (const agenda)
2654 (const todo)
2655 (const tags)
2656 (const search)
2657 (const timeline))
2658 (integer :tag "Max number of minutes")))))
2660 (defvar org-keys nil)
2661 (defvar org-match nil)
2662 ;;;###autoload
2663 (defun org-agenda (&optional arg org-keys restriction)
2664 "Dispatch agenda commands to collect entries to the agenda buffer.
2665 Prompts for a command to execute. Any prefix arg will be passed
2666 on to the selected command. The default selections are:
2668 a Call `org-agenda-list' to display the agenda for current day or week.
2669 t Call `org-todo-list' to display the global todo list.
2670 T Call `org-todo-list' to display the global todo list, select only
2671 entries with a specific TODO keyword (the user gets a prompt).
2672 m Call `org-tags-view' to display headlines with tags matching
2673 a condition (the user is prompted for the condition).
2674 M Like `m', but select only TODO entries, no ordinary headlines.
2675 L Create a timeline for the current buffer.
2676 e Export views to associated files.
2677 s Search entries for keywords.
2678 S Search entries for keywords, only with TODO keywords.
2679 / Multi occur across all agenda files and also files listed
2680 in `org-agenda-text-search-extra-files'.
2681 < Restrict agenda commands to buffer, subtree, or region.
2682 Press several times to get the desired effect.
2683 > Remove a previous restriction.
2684 # List \"stuck\" projects.
2685 ! Configure what \"stuck\" means.
2686 C Configure custom agenda commands.
2688 More commands can be added by configuring the variable
2689 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2690 searches can be pre-defined in this way.
2692 If the current buffer is in Org-mode and visiting a file, you can also
2693 first press `<' once to indicate that the agenda should be temporarily
2694 \(until the next use of \\[org-agenda]) restricted to the current file.
2695 Pressing `<' twice means to restrict to the current subtree or region
2696 \(if active)."
2697 (interactive "P")
2698 (catch 'exit
2699 (let* ((prefix-descriptions nil)
2700 (org-agenda-buffer-name org-agenda-buffer-name)
2701 (org-agenda-window-setup (if (equal (buffer-name)
2702 org-agenda-buffer-name)
2703 'current-window
2704 org-agenda-window-setup))
2705 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2706 (org-agenda-custom-commands
2707 ;; normalize different versions
2708 (delq nil
2709 (mapcar
2710 (lambda (x)
2711 (cond ((stringp (cdr x))
2712 (push x prefix-descriptions)
2713 nil)
2714 ((stringp (nth 1 x)) x)
2715 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2716 (t (cons (car x) (cons "" (cdr x))))))
2717 org-agenda-custom-commands)))
2718 (org-agenda-custom-commands
2719 (org-contextualize-keys
2720 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2721 (buf (current-buffer))
2722 (bfn (buffer-file-name (buffer-base-buffer)))
2723 entry key type org-match lprops ans)
2724 ;; Turn off restriction unless there is an overriding one,
2725 (unless org-agenda-overriding-restriction
2726 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2727 ;; There is a request to keep the file list in place
2728 (put 'org-agenda-files 'org-restrict nil))
2729 (setq org-agenda-restrict nil)
2730 (move-marker org-agenda-restrict-begin nil)
2731 (move-marker org-agenda-restrict-end nil))
2732 ;; Delete old local properties
2733 (put 'org-agenda-redo-command 'org-lprops nil)
2734 ;; Delete previously set last-arguments
2735 (put 'org-agenda-redo-command 'last-args nil)
2736 ;; Remember where this call originated
2737 (setq org-agenda-last-dispatch-buffer (current-buffer))
2738 (unless org-keys
2739 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2740 org-keys (car ans)
2741 restriction (cdr ans)))
2742 ;; If we have sticky agenda buffers, set a name for the buffer,
2743 ;; depending on the invoking keys. The user may still set this
2744 ;; as a command option, which will overwrite what we do here.
2745 (if org-agenda-sticky
2746 (setq org-agenda-buffer-name
2747 (format "*Org Agenda(%s)*" org-keys)))
2748 ;; Establish the restriction, if any
2749 (when (and (not org-agenda-overriding-restriction) restriction)
2750 (put 'org-agenda-files 'org-restrict (list bfn))
2751 (cond
2752 ((eq restriction 'region)
2753 (setq org-agenda-restrict (current-buffer))
2754 (move-marker org-agenda-restrict-begin (region-beginning))
2755 (move-marker org-agenda-restrict-end (region-end)))
2756 ((eq restriction 'subtree)
2757 (save-excursion
2758 (setq org-agenda-restrict (current-buffer))
2759 (org-back-to-heading t)
2760 (move-marker org-agenda-restrict-begin (point))
2761 (move-marker org-agenda-restrict-end
2762 (progn (org-end-of-subtree t)))))))
2764 ;; For example the todo list should not need it (but does...)
2765 (cond
2766 ((setq entry (assoc org-keys org-agenda-custom-commands))
2767 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2768 (progn
2769 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
2770 lprops (nth 4 entry))
2771 (if org-agenda-sticky
2772 (setq org-agenda-buffer-name
2773 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2774 (format "*Org Agenda(%s)*" org-keys))))
2775 (put 'org-agenda-redo-command 'org-lprops lprops)
2776 (cond
2777 ((eq type 'agenda)
2778 (org-let lprops '(org-agenda-list current-prefix-arg)))
2779 ((eq type 'agenda*)
2780 (org-let lprops '(org-agenda-list current-prefix-arg nil nil t)))
2781 ((eq type 'alltodo)
2782 (org-let lprops '(org-todo-list current-prefix-arg)))
2783 ((eq type 'search)
2784 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
2785 ((eq type 'stuck)
2786 (org-let lprops '(org-agenda-list-stuck-projects
2787 current-prefix-arg)))
2788 ((eq type 'tags)
2789 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2790 ((eq type 'tags-todo)
2791 (org-let lprops '(org-tags-view '(4) org-match)))
2792 ((eq type 'todo)
2793 (org-let lprops '(org-todo-list org-match)))
2794 ((eq type 'tags-tree)
2795 (org-check-for-org-mode)
2796 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
2797 ((eq type 'todo-tree)
2798 (org-check-for-org-mode)
2799 (org-let lprops
2800 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2801 (regexp-quote org-match) "\\>"))))
2802 ((eq type 'occur-tree)
2803 (org-check-for-org-mode)
2804 (org-let lprops '(org-occur org-match)))
2805 ((functionp type)
2806 (org-let lprops '(funcall type org-match)))
2807 ((fboundp type)
2808 (org-let lprops '(funcall type org-match)))
2809 (t (user-error "Invalid custom agenda command type %s" type))))
2810 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2811 ((equal org-keys "C")
2812 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2813 (customize-variable 'org-agenda-custom-commands))
2814 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2815 ((equal org-keys "s") (call-interactively 'org-search-view))
2816 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2817 ((equal org-keys "t") (call-interactively 'org-todo-list))
2818 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2819 ((equal org-keys "m") (call-interactively 'org-tags-view))
2820 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2821 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2822 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
2823 (org-add-hook
2824 'post-command-hook
2825 (lambda ()
2826 (unless (current-message)
2827 (let* ((m (org-agenda-get-any-marker))
2828 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2829 (when note
2830 (message (concat
2831 "FLAGGING-NOTE ([?] for more info): "
2832 (org-add-props
2833 (replace-regexp-in-string
2834 "\\\\n" "//"
2835 (copy-sequence note))
2836 nil 'face 'org-warning)))))))
2837 t t))
2838 ((equal org-keys "L")
2839 (unless (derived-mode-p 'org-mode)
2840 (user-error "This is not an Org-mode file"))
2841 (unless restriction
2842 (put 'org-agenda-files 'org-restrict (list bfn))
2843 (org-call-with-arg 'org-timeline arg)))
2844 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2845 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2846 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
2847 (t (user-error "Invalid agenda key"))))))
2849 (defvar org-agenda-multi)
2851 (defun org-agenda-append-agenda ()
2852 "Append another agenda view to the current one.
2853 This function allows interactive building of block agendas.
2854 Agenda views are separated by `org-agenda-block-separator'."
2855 (interactive)
2856 (unless (derived-mode-p 'org-agenda-mode)
2857 (user-error "Can only append from within agenda buffer"))
2858 (let ((org-agenda-multi t))
2859 (org-agenda)
2860 (widen)
2861 (org-agenda-finalize)
2862 (setq buffer-read-only t)
2863 (org-agenda-fit-window-to-buffer)))
2865 (defun org-agenda-normalize-custom-commands (cmds)
2866 "Normalize custom commands CMDS."
2867 (delq nil
2868 (mapcar
2869 (lambda (x)
2870 (cond ((stringp (cdr x)) nil)
2871 ((stringp (nth 1 x)) x)
2872 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2873 (t (cons (car x) (cons "" (cdr x))))))
2874 cmds)))
2876 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2877 "The user interface for selecting an agenda command."
2878 (catch 'exit
2879 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2880 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2881 (region-p (org-region-active-p))
2882 (custom org-agenda-custom-commands)
2883 (selstring "")
2884 restriction second-time
2885 c entry key type match prefixes rmheader header-end custom1 desc
2886 line lines left right n n1)
2887 (save-window-excursion
2888 (delete-other-windows)
2889 (org-switch-to-buffer-other-window " *Agenda Commands*")
2890 (erase-buffer)
2891 (insert (eval-when-compile
2892 (let ((header
2893 "Press key for an agenda command: < Buffer, subtree/region restriction
2894 -------------------------------- > Remove restriction
2895 a Agenda for current week or day e Export agenda views
2896 t List of all TODO entries T Entries with special TODO kwd
2897 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2898 s Search for keywords S Like s, but only TODO entries
2899 L Timeline for current buffer # List stuck projects (!=configure)
2900 / Multi-occur C Configure custom agenda commands
2901 ? Find :FLAGGED: entries * Toggle sticky agenda views
2903 (start 0))
2904 (while (string-match
2905 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2906 header start)
2907 (setq start (match-end 0))
2908 (add-text-properties (match-beginning 2) (match-end 2)
2909 '(face bold) header))
2910 header)))
2911 (setq header-end (point-marker))
2912 (while t
2913 (setq custom1 custom)
2914 (when (eq rmheader t)
2915 (org-goto-line 1)
2916 (re-search-forward ":" nil t)
2917 (delete-region (match-end 0) (point-at-eol))
2918 (forward-char 1)
2919 (looking-at "-+")
2920 (delete-region (match-end 0) (point-at-eol))
2921 (move-marker header-end (match-end 0)))
2922 (goto-char header-end)
2923 (delete-region (point) (point-max))
2925 ;; Produce all the lines that describe custom commands and prefixes
2926 (setq lines nil)
2927 (while (setq entry (pop custom1))
2928 (setq key (car entry) desc (nth 1 entry)
2929 type (nth 2 entry)
2930 match (nth 3 entry))
2931 (if (> (length key) 1)
2932 (add-to-list 'prefixes (string-to-char key))
2933 (setq line
2934 (format
2935 "%-4s%-14s"
2936 (org-add-props (copy-sequence key)
2937 '(face bold))
2938 (cond
2939 ((string-match "\\S-" desc) desc)
2940 ((eq type 'agenda) "Agenda for current week or day")
2941 ((eq type 'agenda*) "Appointments for current week or day")
2942 ((eq type 'alltodo) "List of all TODO entries")
2943 ((eq type 'search) "Word search")
2944 ((eq type 'stuck) "List of stuck projects")
2945 ((eq type 'todo) "TODO keyword")
2946 ((eq type 'tags) "Tags query")
2947 ((eq type 'tags-todo) "Tags (TODO)")
2948 ((eq type 'tags-tree) "Tags tree")
2949 ((eq type 'todo-tree) "TODO kwd tree")
2950 ((eq type 'occur-tree) "Occur tree")
2951 ((functionp type) (if (symbolp type)
2952 (symbol-name type)
2953 "Lambda expression"))
2954 (t "???"))))
2955 (if org-agenda-menu-show-matcher
2956 (setq line
2957 (concat line ": "
2958 (cond
2959 ((stringp match)
2960 (setq match (copy-sequence match))
2961 (org-add-props match nil 'face 'org-warning))
2962 ((listp type)
2963 (format "set of %d commands" (length type))))))
2964 (if (org-string-nw-p match)
2965 (add-text-properties
2966 0 (length line) (list 'help-echo
2967 (concat "Matcher: " match)) line)))
2968 (push line lines)))
2969 (setq lines (nreverse lines))
2970 (when prefixes
2971 (mapc (lambda (x)
2972 (push
2973 (format "%s %s"
2974 (org-add-props (char-to-string x)
2975 nil 'face 'bold)
2976 (or (cdr (assoc (concat selstring
2977 (char-to-string x))
2978 prefix-descriptions))
2979 "Prefix key"))
2980 lines))
2981 prefixes))
2983 ;; Check if we should display in two columns
2984 (if org-agenda-menu-two-columns
2985 (progn
2986 (setq n (length lines)
2987 n1 (+ (/ n 2) (mod n 2))
2988 right (nthcdr n1 lines)
2989 left (copy-sequence lines))
2990 (setcdr (nthcdr (1- n1) left) nil))
2991 (setq left lines right nil))
2992 (while left
2993 (insert "\n" (pop left))
2994 (when right
2995 (if (< (current-column) 40)
2996 (move-to-column 40 t)
2997 (insert " "))
2998 (insert (pop right))))
3000 ;; Make the window the right size
3001 (goto-char (point-min))
3002 (if second-time
3003 (if (not (pos-visible-in-window-p (point-max)))
3004 (org-fit-window-to-buffer))
3005 (setq second-time t)
3006 (org-fit-window-to-buffer))
3008 ;; Ask for selection
3009 (message "Press key for agenda command%s:"
3010 (if (or restrict-ok org-agenda-overriding-restriction)
3011 (if org-agenda-overriding-restriction
3012 " (restriction lock active)"
3013 (if restriction
3014 (format " (restricted to %s)" restriction)
3015 " (unrestricted)"))
3016 ""))
3017 (setq c (read-char-exclusive))
3018 (message "")
3019 (cond
3020 ((assoc (char-to-string c) custom)
3021 (setq selstring (concat selstring (char-to-string c)))
3022 (throw 'exit (cons selstring restriction)))
3023 ((memq c prefixes)
3024 (setq selstring (concat selstring (char-to-string c))
3025 prefixes nil
3026 rmheader (or rmheader t)
3027 custom (delq nil (mapcar
3028 (lambda (x)
3029 (if (or (= (length (car x)) 1)
3030 (/= (string-to-char (car x)) c))
3032 (cons (substring (car x) 1) (cdr x))))
3033 custom))))
3034 ((eq c ?*)
3035 (call-interactively 'org-toggle-sticky-agenda)
3036 (sit-for 2))
3037 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
3038 (message "Restriction is only possible in Org-mode buffers")
3039 (ding) (sit-for 1))
3040 ((eq c ?1)
3041 (org-agenda-remove-restriction-lock 'noupdate)
3042 (setq restriction 'buffer))
3043 ((eq c ?0)
3044 (org-agenda-remove-restriction-lock 'noupdate)
3045 (setq restriction (if region-p 'region 'subtree)))
3046 ((eq c ?<)
3047 (org-agenda-remove-restriction-lock 'noupdate)
3048 (setq restriction
3049 (cond
3050 ((eq restriction 'buffer)
3051 (if region-p 'region 'subtree))
3052 ((memq restriction '(subtree region))
3053 nil)
3054 (t 'buffer))))
3055 ((eq c ?>)
3056 (org-agenda-remove-restriction-lock 'noupdate)
3057 (setq restriction nil))
3058 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
3059 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
3060 ((and (> (length selstring) 0) (eq c ?\d))
3061 (delete-window)
3062 (org-agenda-get-restriction-and-command prefix-descriptions))
3064 ((equal c ?q) (error "Abort"))
3065 (t (user-error "Invalid key %c" c))))))))
3067 (defun org-agenda-fit-window-to-buffer ()
3068 "Fit the window to the buffer size."
3069 (and (memq org-agenda-window-setup '(reorganize-frame))
3070 (fboundp 'fit-window-to-buffer)
3071 (if (and (= (cdr org-agenda-window-frame-fractions) 1.0)
3072 (= (car org-agenda-window-frame-fractions) 1.0))
3073 (delete-other-windows)
3074 (org-fit-window-to-buffer
3076 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3077 (floor (* (frame-height) (car org-agenda-window-frame-fractions)))))))
3079 (defvar org-cmd nil)
3080 (defvar org-agenda-overriding-cmd nil)
3081 (defvar org-agenda-overriding-arguments nil)
3082 (defvar org-agenda-overriding-cmd-arguments nil)
3083 (defun org-agenda-run-series (name series)
3084 "Run agenda NAME as a SERIES of agenda commands."
3085 (org-let (nth 1 series) '(org-agenda-prepare name))
3086 ;; We need to reset agenda markers here, because when constructing a
3087 ;; block agenda, the individual blocks do not do that.
3088 (org-agenda-reset-markers)
3089 (let* ((org-agenda-multi t)
3090 (redo (list 'org-agenda-run-series name (list 'quote series)))
3091 (cmds (car series))
3092 (gprops (nth 1 series))
3093 match ;; The byte compiler incorrectly complains about this. Keep it!
3094 org-cmd type lprops)
3095 (while (setq org-cmd (pop cmds))
3096 (setq type (car org-cmd)
3097 match (eval (nth 1 org-cmd))
3098 lprops (nth 2 org-cmd))
3099 (let ((org-agenda-overriding-arguments
3100 (if (eq org-agenda-overriding-cmd org-cmd)
3101 (or org-agenda-overriding-arguments
3102 org-agenda-overriding-cmd-arguments))))
3103 (cond
3104 ((eq type 'agenda)
3105 (org-let2 gprops lprops
3106 '(call-interactively 'org-agenda-list)))
3107 ((eq type 'agenda*)
3108 (org-let2 gprops lprops
3109 '(funcall 'org-agenda-list nil nil t)))
3110 ((eq type 'alltodo)
3111 (org-let2 gprops lprops
3112 '(call-interactively 'org-todo-list)))
3113 ((eq type 'search)
3114 (org-let2 gprops lprops
3115 '(org-search-view current-prefix-arg match nil)))
3116 ((eq type 'stuck)
3117 (org-let2 gprops lprops
3118 '(call-interactively 'org-agenda-list-stuck-projects)))
3119 ((eq type 'tags)
3120 (org-let2 gprops lprops
3121 '(org-tags-view current-prefix-arg match)))
3122 ((eq type 'tags-todo)
3123 (org-let2 gprops lprops
3124 '(org-tags-view '(4) match)))
3125 ((eq type 'todo)
3126 (org-let2 gprops lprops
3127 '(org-todo-list match)))
3128 ((fboundp type)
3129 (org-let2 gprops lprops
3130 '(funcall type match)))
3131 (t (error "Invalid type in command series")))))
3132 (widen)
3133 (let ((inhibit-read-only t))
3134 (add-text-properties (point-min) (point-max)
3135 `(org-series t org-series-redo-cmd ,redo)))
3136 (setq org-agenda-redo-command redo)
3137 (goto-char (point-min)))
3138 (org-agenda-fit-window-to-buffer)
3139 (org-let (nth 1 series) '(org-agenda-finalize)))
3141 ;;;###autoload
3142 (defmacro org-batch-agenda (cmd-key &rest parameters)
3143 "Run an agenda command in batch mode and send the result to STDOUT.
3144 If CMD-KEY is a string of length 1, it is used as a key in
3145 `org-agenda-custom-commands' and triggers this command. If it is a
3146 longer string it is used as a tags/todo match string.
3147 Parameters are alternating variable names and values that will be bound
3148 before running the agenda command."
3149 (org-eval-in-environment (org-make-parameter-alist parameters)
3150 (let (org-agenda-sticky)
3151 (if (> (length cmd-key) 2)
3152 (org-tags-view nil cmd-key)
3153 (org-agenda nil cmd-key))))
3154 (set-buffer org-agenda-buffer-name)
3155 (princ (buffer-string)))
3157 (defvar org-agenda-info nil)
3159 ;;;###autoload
3160 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
3161 "Run an agenda command in batch mode and send the result to STDOUT.
3162 If CMD-KEY is a string of length 1, it is used as a key in
3163 `org-agenda-custom-commands' and triggers this command. If it is a
3164 longer string it is used as a tags/todo match string.
3165 Parameters are alternating variable names and values that will be bound
3166 before running the agenda command.
3168 The output gives a line for each selected agenda item. Each
3169 item is a list of comma-separated values, like this:
3171 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3173 category The category of the item
3174 head The headline, without TODO kwd, TAGS and PRIORITY
3175 type The type of the agenda entry, can be
3176 todo selected in TODO match
3177 tagsmatch selected in tags match
3178 diary imported from diary
3179 deadline a deadline on given date
3180 scheduled scheduled on given date
3181 timestamp entry has timestamp on given date
3182 closed entry was closed on given date
3183 upcoming-deadline warning about deadline
3184 past-scheduled forwarded scheduled item
3185 block entry has date block including g. date
3186 todo The todo keyword, if any
3187 tags All tags including inherited ones, separated by colons
3188 date The relevant date, like 2007-2-14
3189 time The time, like 15:00-16:50
3190 extra Sting with extra planning info
3191 priority-l The priority letter if any was given
3192 priority-n The computed numerical priority
3193 agenda-day The day in the agenda where this is listed"
3194 (org-eval-in-environment (append '((org-agenda-remove-tags t))
3195 (org-make-parameter-alist parameters))
3196 (if (> (length cmd-key) 2)
3197 (org-tags-view nil cmd-key)
3198 (org-agenda nil cmd-key)))
3199 (set-buffer org-agenda-buffer-name)
3200 (let* ((lines (org-split-string (buffer-string) "\n"))
3201 line)
3202 (while (setq line (pop lines))
3203 (catch 'next
3204 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
3205 (setq org-agenda-info
3206 (org-fix-agenda-info (text-properties-at 0 line)))
3207 (princ
3208 (mapconcat 'org-agenda-export-csv-mapper
3209 '(org-category txt type todo tags date time extra
3210 priority-letter priority agenda-day)
3211 ","))
3212 (princ "\n")))))
3214 (defun org-fix-agenda-info (props)
3215 "Make sure all properties on an agenda item have a canonical form.
3216 This ensures the export commands can easily use it."
3217 (let (tmp re)
3218 (when (setq tmp (plist-get props 'tags))
3219 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3220 (when (setq tmp (plist-get props 'date))
3221 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3222 (let ((calendar-date-display-form '(year "-" month "-" day)))
3223 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3225 (setq tmp (calendar-date-string tmp)))
3226 (setq props (plist-put props 'date tmp)))
3227 (when (setq tmp (plist-get props 'day))
3228 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3229 (let ((calendar-date-display-form '(year "-" month "-" day)))
3230 (setq tmp (calendar-date-string tmp)))
3231 (setq props (plist-put props 'day tmp))
3232 (setq props (plist-put props 'agenda-day tmp)))
3233 (when (setq tmp (plist-get props 'txt))
3234 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3235 (plist-put props 'priority-letter (match-string 1 tmp))
3236 (setq tmp (replace-match "" t t tmp)))
3237 (when (and (setq re (plist-get props 'org-todo-regexp))
3238 (setq re (concat "\\`\\.*" re " ?"))
3239 (string-match re tmp))
3240 (plist-put props 'todo (match-string 1 tmp))
3241 (setq tmp (replace-match "" t t tmp)))
3242 (plist-put props 'txt tmp)))
3243 props)
3245 (defun org-agenda-export-csv-mapper (prop)
3246 (let ((res (plist-get org-agenda-info prop)))
3247 (setq res
3248 (cond
3249 ((not res) "")
3250 ((stringp res) res)
3251 (t (prin1-to-string res))))
3252 (while (string-match "," res)
3253 (setq res (replace-match ";" t t res)))
3254 (org-trim res)))
3256 ;;;###autoload
3257 (defun org-store-agenda-views (&rest parameters)
3258 "Store agenda views."
3259 (interactive)
3260 (eval (list 'org-batch-store-agenda-views)))
3262 ;;;###autoload
3263 (defmacro org-batch-store-agenda-views (&rest parameters)
3264 "Run all custom agenda commands that have a file argument."
3265 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3266 (pop-up-frames nil)
3267 (dir default-directory)
3268 (pars (org-make-parameter-alist parameters))
3269 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
3270 (save-window-excursion
3271 (while cmds
3272 (setq cmd (pop cmds)
3273 thiscmdkey (car cmd)
3274 thiscmdcmd (cdr cmd)
3275 match (nth 2 thiscmdcmd)
3276 bufname (if org-agenda-sticky
3277 (or (and (stringp match)
3278 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3279 (format "*Org Agenda(%s)*" thiscmdkey))
3280 org-agenda-buffer-name)
3281 cmd-or-set (nth 2 cmd)
3282 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3283 files (nth (if (listp cmd-or-set) 4 5) cmd))
3284 (if (stringp files) (setq files (list files)))
3285 (when files
3286 (org-eval-in-environment (append org-agenda-exporter-settings
3287 opts pars)
3288 (org-agenda nil thiscmdkey))
3289 (set-buffer bufname)
3290 (while files
3291 (org-eval-in-environment (append org-agenda-exporter-settings
3292 opts pars)
3293 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3294 (and (get-buffer bufname)
3295 (kill-buffer bufname)))))))
3297 (defvar org-agenda-current-span nil
3298 "The current span used in the agenda view.") ; local variable in the agenda buffer
3299 (defun org-agenda-mark-header-line (pos)
3300 "Mark the line at POS as an agenda structure header."
3301 (save-excursion
3302 (goto-char pos)
3303 (put-text-property (point-at-bol) (point-at-eol)
3304 'org-agenda-structural-header t)
3305 (when org-agenda-title-append
3306 (put-text-property (point-at-bol) (point-at-eol)
3307 'org-agenda-title-append org-agenda-title-append))))
3309 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3310 (defvar org-agenda-write-buffer-name "Agenda View")
3311 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3312 "Write the current buffer (an agenda view) as a file.
3313 Depending on the extension of the file name, plain text (.txt),
3314 HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
3315 If the extension is .ics, run icalendar export over all files used
3316 to construct the agenda and limit the export to entries listed in the
3317 agenda now.
3318 If the extension is .org, collect all subtrees corresponding to the
3319 agenda entries and add them in an .org file.
3320 With prefix argument OPEN, open the new file immediately.
3321 If NOSETTINGS is given, do not scope the settings of
3322 `org-agenda-exporter-settings' into the export commands. This is used when
3323 the settings have already been scoped and we do not wish to overrule other,
3324 higher priority settings.
3325 If AGENDA-BUFFER-NAME, use this as the buffer name for the agenda to write."
3326 (interactive "FWrite agenda to file: \nP")
3327 (if (or (not (file-writable-p file))
3328 (and (file-exists-p file)
3329 (if (org-called-interactively-p 'any)
3330 (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
3331 (user-error "Cannot write agenda to file %s" file))
3332 (org-let (if nosettings nil org-agenda-exporter-settings)
3333 '(save-excursion
3334 (save-window-excursion
3335 (org-agenda-mark-filtered-text)
3336 (let ((bs (copy-sequence (buffer-string))) beg content)
3337 (org-agenda-unmark-filtered-text)
3338 (with-temp-buffer
3339 (rename-buffer org-agenda-write-buffer-name t)
3340 (set-buffer-modified-p nil)
3341 (insert bs)
3342 (org-agenda-remove-marked-text 'org-filtered)
3343 (while (setq beg (text-property-any (point-min) (point-max)
3344 'org-filtered t))
3345 (delete-region
3346 beg (or (next-single-property-change beg 'org-filtered)
3347 (point-max))))
3348 (run-hooks 'org-agenda-before-write-hook)
3349 (cond
3350 ((org-bound-and-true-p org-mobile-creating-agendas)
3351 (org-mobile-write-agenda-for-mobile file))
3352 ((string-match "\\.org\\'" file)
3353 (let (content p m message-log-max)
3354 (goto-char (point-min))
3355 (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
3356 (goto-char p)
3357 (setq m (get-text-property (point) 'org-hd-marker))
3358 (when m
3359 (push (save-excursion
3360 (set-buffer (marker-buffer m))
3361 (goto-char m)
3362 (org-copy-subtree 1 nil t t)
3363 org-subtree-clip)
3364 content)))
3365 (find-file file)
3366 (erase-buffer)
3367 (mapcar (lambda (s) (org-paste-subtree 1 s)) (reverse content))
3368 (write-file file)
3369 (kill-buffer (current-buffer))
3370 (message "Org file written to %s" file)))
3371 ((string-match "\\.html?\\'" file)
3372 (require 'htmlize)
3373 (set-buffer (htmlize-buffer (current-buffer)))
3374 (when org-agenda-export-html-style
3375 ;; replace <style> section with org-agenda-export-html-style
3376 (goto-char (point-min))
3377 (kill-region (- (search-forward "<style") 6)
3378 (search-forward "</style>"))
3379 (insert org-agenda-export-html-style))
3380 (write-file file)
3381 (kill-buffer (current-buffer))
3382 (message "HTML written to %s" file))
3383 ((string-match "\\.ps\\'" file)
3384 (require 'ps-print)
3385 (ps-print-buffer-with-faces file)
3386 (message "Postscript written to %s" file))
3387 ((string-match "\\.pdf\\'" file)
3388 (require 'ps-print)
3389 (ps-print-buffer-with-faces
3390 (concat (file-name-sans-extension file) ".ps"))
3391 (call-process "ps2pdf" nil nil nil
3392 (expand-file-name
3393 (concat (file-name-sans-extension file) ".ps"))
3394 (expand-file-name file))
3395 (delete-file (concat (file-name-sans-extension file) ".ps"))
3396 (message "PDF written to %s" file))
3397 ((string-match "\\.ics\\'" file)
3398 (require 'ox-icalendar)
3399 (org-icalendar-export-current-agenda (expand-file-name file)))
3401 (let ((bs (buffer-string)))
3402 (find-file file)
3403 (erase-buffer)
3404 (insert bs)
3405 (save-buffer 0)
3406 (kill-buffer (current-buffer))
3407 (message "Plain text written to %s" file))))))))
3408 (set-buffer (or agenda-bufname
3409 (and (org-called-interactively-p 'any) (buffer-name))
3410 org-agenda-buffer-name)))
3411 (when open (org-open-file file)))
3413 (defvar org-agenda-tag-filter-overlays nil)
3414 (defvar org-agenda-cat-filter-overlays nil)
3415 (defvar org-agenda-re-filter-overlays nil)
3417 (defun org-agenda-mark-filtered-text ()
3418 "Mark all text hidden by filtering with a text property."
3419 (let ((inhibit-read-only t))
3420 (mapc
3421 (lambda (o)
3422 (when (equal (overlay-buffer o) (current-buffer))
3423 (put-text-property
3424 (overlay-start o) (overlay-end o)
3425 'org-filtered t)))
3426 (append org-agenda-tag-filter-overlays
3427 org-agenda-cat-filter-overlays
3428 org-agenda-re-filter-overlays))))
3430 (defun org-agenda-unmark-filtered-text ()
3431 "Remove the filtering text property."
3432 (let ((inhibit-read-only t))
3433 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
3435 (defun org-agenda-remove-marked-text (property &optional value)
3436 "Delete all text marked with VALUE of PROPERTY.
3437 VALUE defaults to t."
3438 (let (beg)
3439 (setq value (or value t))
3440 (while (setq beg (text-property-any (point-min) (point-max)
3441 property value))
3442 (delete-region
3443 beg (or (next-single-property-change beg 'org-filtered)
3444 (point-max))))))
3446 (defun org-agenda-add-entry-text ()
3447 "Add entry text to agenda lines.
3448 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3449 entry text following headings shown in the agenda.
3450 Drawers will be excluded, also the line with scheduling/deadline info."
3451 (when (and (> org-agenda-add-entry-text-maxlines 0)
3452 (not (org-bound-and-true-p org-mobile-creating-agendas)))
3453 (let (m txt)
3454 (goto-char (point-min))
3455 (while (not (eobp))
3456 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3457 (beginning-of-line 2)
3458 (setq txt (org-agenda-get-some-entry-text
3459 m org-agenda-add-entry-text-maxlines " > "))
3460 (end-of-line 1)
3461 (if (string-match "\\S-" txt)
3462 (insert "\n" txt)
3463 (or (eobp) (forward-char 1))))))))
3465 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3466 &rest keep)
3467 "Extract entry text from MARKER, at most N-LINES lines.
3468 This will ignore drawers etc, just get the text.
3469 If INDENT is given, prefix every line with this string. If KEEP is
3470 given, it is a list of symbols, defining stuff that should not be
3471 removed from the entry content. Currently only `planning' is allowed here."
3472 (let (txt drawer-re kwd-time-re ind)
3473 (save-excursion
3474 (with-current-buffer (marker-buffer marker)
3475 (if (not (derived-mode-p 'org-mode))
3476 (setq txt "")
3477 (save-excursion
3478 (save-restriction
3479 (widen)
3480 (goto-char marker)
3481 (end-of-line 1)
3482 (setq txt (buffer-substring
3483 (min (1+ (point)) (point-max))
3484 (progn (outline-next-heading) (point)))
3485 drawer-re org-drawer-regexp
3486 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3487 ".*\n?"))
3488 (with-temp-buffer
3489 (insert txt)
3490 (when org-agenda-add-entry-text-descriptive-links
3491 (goto-char (point-min))
3492 (while (org-activate-bracket-links (point-max))
3493 (add-text-properties (match-beginning 0) (match-end 0)
3494 '(face org-link))))
3495 (goto-char (point-min))
3496 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3497 (set-text-properties (match-beginning 0) (match-end 0)
3498 nil))
3499 (goto-char (point-min))
3500 (while (re-search-forward drawer-re nil t)
3501 (delete-region
3502 (match-beginning 0)
3503 (progn (re-search-forward
3504 "^[ \t]*:END:.*\n?" nil 'move)
3505 (point))))
3506 (unless (member 'planning keep)
3507 (goto-char (point-min))
3508 (while (re-search-forward kwd-time-re nil t)
3509 (replace-match "")))
3510 (goto-char (point-min))
3511 (when org-agenda-entry-text-exclude-regexps
3512 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3513 (while (setq re (pop re-list))
3514 (goto-char (point-min))
3515 (while (re-search-forward re nil t)
3516 (replace-match "")))))
3517 (goto-char (point-max))
3518 (skip-chars-backward " \t\n")
3519 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3521 ;; find and remove min common indentation
3522 (goto-char (point-min))
3523 (untabify (point-min) (point-max))
3524 (setq ind (org-get-indentation))
3525 (while (not (eobp))
3526 (unless (looking-at "[ \t]*$")
3527 (setq ind (min ind (org-get-indentation))))
3528 (beginning-of-line 2))
3529 (goto-char (point-min))
3530 (while (not (eobp))
3531 (unless (looking-at "[ \t]*$")
3532 (move-to-column ind)
3533 (delete-region (point-at-bol) (point)))
3534 (beginning-of-line 2))
3536 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3538 (goto-char (point-min))
3539 (when indent
3540 (while (and (not (eobp)) (re-search-forward "^" nil t))
3541 (replace-match indent t t)))
3542 (goto-char (point-min))
3543 (while (looking-at "[ \t]*\n") (replace-match ""))
3544 (goto-char (point-max))
3545 (when (> (org-current-line)
3546 n-lines)
3547 (org-goto-line (1+ n-lines))
3548 (backward-char 1))
3549 (setq txt (buffer-substring (point-min) (point)))))))))
3550 txt))
3552 (defun org-check-for-org-mode ()
3553 "Make sure current buffer is in org-mode. Error if not."
3554 (or (derived-mode-p 'org-mode)
3555 (error "Cannot execute org-mode agenda command on buffer in %s"
3556 major-mode)))
3558 ;;; Agenda prepare and finalize
3560 (defvar org-agenda-multi nil) ; dynamically scoped
3561 (defvar org-agenda-pre-window-conf nil)
3562 (defvar org-agenda-columns-active nil)
3563 (defvar org-agenda-name nil)
3564 (defvar org-agenda-tag-filter nil)
3565 (defvar org-agenda-category-filter nil)
3566 (defvar org-agenda-regexp-filter nil)
3567 (defvar org-agenda-top-headline-filter nil)
3568 (defvar org-agenda-tag-filter-while-redo nil)
3569 (defvar org-agenda-tag-filter-preset nil
3570 "A preset of the tags filter used for secondary agenda filtering.
3571 This must be a list of strings, each string must be a single tag preceded
3572 by \"+\" or \"-\".
3573 This variable should not be set directly, but agenda custom commands can
3574 bind it in the options section. The preset filter is a global property of
3575 the entire agenda view. In a block agenda, it will not work reliably to
3576 define a filter for one of the individual blocks. You need to set it in
3577 the global options and expect it to be applied to the entire view.")
3579 (defvar org-agenda-category-filter-preset nil
3580 "A preset of the category filter used for secondary agenda filtering.
3581 This must be a list of strings, each string must be a single category
3582 preceded by \"+\" or \"-\".
3583 This variable should not be set directly, but agenda custom commands can
3584 bind it in the options section. The preset filter is a global property of
3585 the entire agenda view. In a block agenda, it will not work reliably to
3586 define a filter for one of the individual blocks. You need to set it in
3587 the global options and expect it to be applied to the entire view.")
3589 (defvar org-agenda-regexp-filter-preset nil
3590 "A preset of the regexp filter used for secondary agenda filtering.
3591 This must be a list of strings, each string must be a single regexp
3592 preceded by \"+\" or \"-\".
3593 This variable should not be set directly, but agenda custom commands can
3594 bind it in the options section. The preset filter is a global property of
3595 the entire agenda view. In a block agenda, it will not work reliably to
3596 define a filter for one of the individual blocks. You need to set it in
3597 the global options and expect it to be applied to the entire view.")
3599 (defun org-agenda-use-sticky-p ()
3600 "Return non-nil if an agenda buffer named
3601 `org-agenda-buffer-name' exists and should be shown instead of
3602 generating a new one."
3603 (and
3604 ;; turned off by user
3605 org-agenda-sticky
3606 ;; For multi-agenda buffer already exists
3607 (not org-agenda-multi)
3608 ;; buffer found
3609 (get-buffer org-agenda-buffer-name)
3610 ;; C-u parameter is same as last call
3611 (with-current-buffer (get-buffer org-agenda-buffer-name)
3612 (and
3613 (equal current-prefix-arg
3614 org-agenda-last-prefix-arg)
3615 ;; In case user turned stickiness on, while having existing
3616 ;; Agenda buffer active, don't reuse that buffer, because it
3617 ;; does not have org variables local
3618 org-agenda-this-buffer-is-sticky))))
3620 (defun org-agenda-prepare-window (abuf)
3621 "Setup agenda buffer in the window."
3622 (let* ((awin (get-buffer-window abuf))
3623 wconf)
3624 (cond
3625 ((equal (current-buffer) abuf) nil)
3626 (awin (select-window awin))
3627 ((not (setq wconf (current-window-configuration))))
3628 ((equal org-agenda-window-setup 'current-window)
3629 (org-pop-to-buffer-same-window abuf))
3630 ((equal org-agenda-window-setup 'other-window)
3631 (org-switch-to-buffer-other-window abuf))
3632 ((equal org-agenda-window-setup 'other-frame)
3633 (switch-to-buffer-other-frame abuf))
3634 ((equal org-agenda-window-setup 'reorganize-frame)
3635 (delete-other-windows)
3636 (org-switch-to-buffer-other-window abuf)))
3637 ;; additional test in case agenda is invoked from within agenda
3638 ;; buffer via elisp link
3639 (unless (equal (current-buffer) abuf)
3640 (org-pop-to-buffer-same-window abuf))
3641 (setq org-agenda-pre-window-conf
3642 (or org-agenda-pre-window-conf wconf))))
3644 (defun org-agenda-prepare (&optional name)
3645 (if (org-agenda-use-sticky-p)
3646 (progn
3647 ;; Popup existing buffer
3648 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name))
3649 (message "Sticky Agenda buffer, use `r' to refresh")
3650 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3651 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3652 (setq org-todo-keywords-for-agenda nil)
3653 (unless org-agenda-persistent-filter
3654 (setq org-agenda-tag-filter nil
3655 org-agenda-category-filter nil
3656 org-agenda-regexp-filter nil))
3657 (put 'org-agenda-tag-filter :preset-filter
3658 org-agenda-tag-filter-preset)
3659 (put 'org-agenda-category-filter :preset-filter
3660 org-agenda-category-filter-preset)
3661 (put 'org-agenda-regexp-filter :preset-filter
3662 org-agenda-regexp-filter-preset)
3663 (if org-agenda-multi
3664 (progn
3665 (setq buffer-read-only nil)
3666 (goto-char (point-max))
3667 (unless (or (bobp) org-agenda-compact-blocks
3668 (not org-agenda-block-separator))
3669 (insert "\n"
3670 (if (stringp org-agenda-block-separator)
3671 org-agenda-block-separator
3672 (make-string (window-width) org-agenda-block-separator))
3673 "\n"))
3674 (narrow-to-region (point) (point-max)))
3675 (setq org-done-keywords-for-agenda nil)
3677 ;; Setting any org variables that are in org-agenda-local-vars
3678 ;; list need to be done after the prepare call
3679 (org-agenda-prepare-window (get-buffer-create org-agenda-buffer-name))
3680 (setq buffer-read-only nil)
3681 (org-agenda-reset-markers)
3682 (let ((inhibit-read-only t)) (erase-buffer))
3683 (org-agenda-mode)
3684 (setq org-agenda-buffer (current-buffer))
3685 (setq org-agenda-contributing-files nil)
3686 (setq org-agenda-columns-active nil)
3687 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3688 (setq org-todo-keywords-for-agenda
3689 (org-uniquify org-todo-keywords-for-agenda))
3690 (setq org-done-keywords-for-agenda
3691 (org-uniquify org-done-keywords-for-agenda))
3692 (setq org-agenda-last-prefix-arg current-prefix-arg)
3693 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3694 (and name (not org-agenda-name)
3695 (org-set-local 'org-agenda-name name)))
3696 (setq buffer-read-only nil)))
3698 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3699 (defun org-agenda-finalize ()
3700 "Finishing touch for the agenda buffer, called just before displaying it."
3701 (unless org-agenda-multi
3702 (save-excursion
3703 (let ((inhibit-read-only t))
3704 (goto-char (point-min))
3705 (save-excursion
3706 (while (org-activate-bracket-links (point-max))
3707 (add-text-properties (match-beginning 0) (match-end 0)
3708 '(face org-link))))
3709 (save-excursion
3710 (while (org-activate-plain-links (point-max))
3711 (add-text-properties (match-beginning 0) (match-end 0)
3712 '(face org-link))))
3713 (unless (eq org-agenda-remove-tags t)
3714 (org-agenda-align-tags))
3715 (unless org-agenda-with-colors
3716 (remove-text-properties (point-min) (point-max) '(face nil)))
3717 (if (and (boundp 'org-agenda-overriding-columns-format)
3718 org-agenda-overriding-columns-format)
3719 (org-set-local 'org-agenda-overriding-columns-format
3720 org-agenda-overriding-columns-format))
3721 (if (and (boundp 'org-agenda-view-columns-initially)
3722 org-agenda-view-columns-initially)
3723 (org-agenda-columns))
3724 (when org-agenda-fontify-priorities
3725 (org-agenda-fontify-priorities))
3726 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3727 (org-agenda-dim-blocked-tasks))
3728 (org-agenda-mark-clocking-task)
3729 (when org-agenda-entry-text-mode
3730 (org-agenda-entry-text-hide)
3731 (org-agenda-entry-text-show))
3732 (if (and (functionp 'org-habit-insert-consistency-graphs)
3733 (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
3734 (org-habit-insert-consistency-graphs))
3735 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3736 (unless (or (eq org-agenda-show-inherited-tags 'always)
3737 (and (listp org-agenda-show-inherited-tags)
3738 (memq org-agenda-type org-agenda-show-inherited-tags))
3739 (and (eq org-agenda-show-inherited-tags t)
3740 (or (eq org-agenda-use-tag-inheritance t)
3741 (and (listp org-agenda-use-tag-inheritance)
3742 (not (memq org-agenda-type
3743 org-agenda-use-tag-inheritance))))))
3744 (let (mrk)
3745 (save-excursion
3746 (goto-char (point-min))
3747 (while (equal (forward-line) 0)
3748 (when (setq mrk (or (get-text-property (point) 'org-hd-marker)
3749 (get-text-property (point) 'org-hd-marker)))
3750 (put-text-property (point-at-bol) (point-at-eol)
3751 'tags (org-with-point-at mrk
3752 (delete-dups
3753 (mapcar 'downcase (org-get-tags-at))))))))))
3754 (run-hooks 'org-agenda-finalize-hook)
3755 (when org-agenda-top-headline-filter
3756 (org-agenda-filter-top-headline-apply
3757 org-agenda-top-headline-filter))
3758 (when org-agenda-tag-filter
3759 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
3760 (when (get 'org-agenda-tag-filter :preset-filter)
3761 (org-agenda-filter-apply
3762 (get 'org-agenda-tag-filter :preset-filter) 'tag))
3763 (when org-agenda-category-filter
3764 (org-agenda-filter-apply org-agenda-category-filter 'category))
3765 (when (get 'org-agenda-category-filter :preset-filter)
3766 (org-agenda-filter-apply
3767 (get 'org-agenda-category-filter :preset-filter) 'category))
3768 (when org-agenda-regexp-filter
3769 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
3770 (when (get 'org-agenda-regexp-filter :preset-filter)
3771 (org-agenda-filter-apply
3772 (get 'org-agenda-regexp-filter :preset-filter) 'regexp))
3773 (org-add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)))))
3775 (defun org-agenda-mark-clocking-task ()
3776 "Mark the current clock entry in the agenda if it is present."
3777 ;; We need to widen when `org-agenda-finalize' is called from
3778 ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
3779 (when org-clock-current-task
3780 (save-restriction
3781 (widen)
3782 (org-agenda-unmark-clocking-task)
3783 (when (marker-buffer org-clock-hd-marker)
3784 (save-excursion
3785 (goto-char (point-min))
3786 (let (s ov)
3787 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3788 (goto-char s)
3789 (when (equal (org-get-at-bol 'org-hd-marker)
3790 org-clock-hd-marker)
3791 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3792 (overlay-put ov 'type 'org-agenda-clocking)
3793 (overlay-put ov 'face 'org-agenda-clocking)
3794 (overlay-put ov 'help-echo
3795 "The clock is running in this item")))))))))
3797 (defun org-agenda-unmark-clocking-task ()
3798 "Unmark the current clocking task."
3799 (mapc (lambda (o)
3800 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3801 (delete-overlay o)))
3802 (overlays-in (point-min) (point-max))))
3804 (defun org-agenda-fontify-priorities ()
3805 "Make highest priority lines bold, and lowest italic."
3806 (interactive)
3807 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3808 (delete-overlay o)))
3809 (overlays-in (point-min) (point-max)))
3810 (save-excursion
3811 (let (b e p ov h l)
3812 (goto-char (point-min))
3813 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3814 (setq h (or (get-char-property (point) 'org-highest-priority)
3815 org-highest-priority)
3816 l (or (get-char-property (point) 'org-lowest-priority)
3817 org-lowest-priority)
3818 p (string-to-char (match-string 1))
3819 b (match-beginning 0)
3820 e (if (eq org-agenda-fontify-priorities 'cookies)
3821 (match-end 0)
3822 (point-at-eol))
3823 ov (make-overlay b e))
3824 (overlay-put
3825 ov 'face
3826 (cons (cond ((org-face-from-face-or-color
3827 'priority nil
3828 (cdr (assoc p org-priority-faces))))
3829 ((and (listp org-agenda-fontify-priorities)
3830 (org-face-from-face-or-color
3831 'priority nil
3832 (cdr (assoc p org-agenda-fontify-priorities)))))
3833 ((equal p l) 'italic)
3834 ((equal p h) 'bold))
3835 'org-priority))
3836 (overlay-put ov 'org-type 'org-priority)))))
3838 (defvar org-depend-tag-blocked)
3840 (defun org-agenda-dim-blocked-tasks (&optional invisible)
3841 "Dim currently blocked TODO's in the agenda display.
3842 When INVISIBLE is non-nil, hide currently blocked TODO instead of
3843 dimming them."
3844 (interactive "P")
3845 (when (org-called-interactively-p 'interactive)
3846 (message "Dim or hide blocked tasks..."))
3847 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3848 (delete-overlay o)))
3849 (overlays-in (point-min) (point-max)))
3850 (save-excursion
3851 (let ((inhibit-read-only t)
3852 (org-depend-tag-blocked nil)
3853 (invis (or (not (null invisible))
3854 (eq org-agenda-dim-blocked-tasks 'invisible)))
3855 org-blocked-by-checkboxes
3856 invis1 b e p ov h l)
3857 (goto-char (point-min))
3858 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3859 (and pos (goto-char (1+ pos))))
3860 (setq org-blocked-by-checkboxes nil invis1 invis)
3861 (let ((marker (org-get-at-bol 'org-hd-marker)))
3862 (when (and marker
3863 (with-current-buffer (marker-buffer marker)
3864 (save-excursion (goto-char marker)
3865 (org-entry-blocked-p))))
3866 (if org-blocked-by-checkboxes (setq invis1 nil))
3867 (setq b (if invis1
3868 (max (point-min) (1- (point-at-bol)))
3869 (point-at-bol))
3870 e (point-at-eol)
3871 ov (make-overlay b e))
3872 (if invis1
3873 (progn (overlay-put ov 'invisible t)
3874 (overlay-put ov 'intangible t))
3875 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3876 (overlay-put ov 'org-type 'org-blocked-todo))))))
3877 (when (org-called-interactively-p 'interactive)
3878 (message "Dim or hide blocked tasks...done")))
3880 (defvar org-agenda-skip-function nil
3881 "Function to be called at each match during agenda construction.
3882 If this function returns nil, the current match should not be skipped.
3883 Otherwise, the function must return a position from where the search
3884 should be continued.
3885 This may also be a Lisp form, it will be evaluated.
3886 Never set this variable using `setq' or so, because then it will apply
3887 to all future agenda commands. If you do want a global skipping condition,
3888 use the option `org-agenda-skip-function-global' instead.
3889 The correct usage for `org-agenda-skip-function' is to bind it with
3890 `let' to scope it dynamically into the agenda-constructing command.
3891 A good way to set it is through options in `org-agenda-custom-commands'.")
3893 (defun org-agenda-skip ()
3894 "Throw to `:skip' in places that should be skipped.
3895 Also moves point to the end of the skipped region, so that search can
3896 continue from there."
3897 (let ((p (point-at-bol)) to)
3898 (when (or
3899 (save-excursion (goto-char p) (looking-at comment-start-skip))
3900 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3901 (get-text-property p :org-archived)
3902 (org-end-of-subtree t))
3903 (and org-agenda-skip-comment-trees
3904 (get-text-property p :org-comment)
3905 (org-end-of-subtree t))
3906 (and (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3907 (org-agenda-skip-eval org-agenda-skip-function)))
3908 (goto-char to))
3909 (org-in-src-block-p t))
3910 (throw :skip t))))
3912 (defun org-agenda-skip-eval (form)
3913 "If FORM is a function or a list, call (or eval) it and return the result.
3914 `save-excursion' and `save-match-data' are wrapped around the call, so point
3915 and match data are returned to the previous state no matter what these
3916 functions do."
3917 (let (fp)
3918 (and form
3919 (or (setq fp (functionp form))
3920 (consp form))
3921 (save-excursion
3922 (save-match-data
3923 (if fp
3924 (funcall form)
3925 (eval form)))))))
3927 (defvar org-agenda-markers nil
3928 "List of all currently active markers created by `org-agenda'.")
3929 (defvar org-agenda-last-marker-time (org-float-time)
3930 "Creation time of the last agenda marker.")
3932 (defun org-agenda-new-marker (&optional pos)
3933 "Return a new agenda marker.
3934 Org-mode keeps a list of these markers and resets them when they are
3935 no longer in use."
3936 (let ((m (copy-marker (or pos (point)))))
3937 (setq org-agenda-last-marker-time (org-float-time))
3938 (if org-agenda-buffer
3939 (with-current-buffer org-agenda-buffer
3940 (push m org-agenda-markers))
3941 (push m org-agenda-markers))
3944 (defun org-agenda-reset-markers ()
3945 "Reset markers created by `org-agenda'."
3946 (while org-agenda-markers
3947 (move-marker (pop org-agenda-markers) nil)))
3949 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3950 "Save relative positions of markers in region.
3951 This check for agenda markers in all agenda buffers currently active."
3952 (dolist (buf (buffer-list))
3953 (with-current-buffer buf
3954 (when (eq major-mode 'org-agenda-mode)
3955 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3956 org-agenda-markers)))))
3958 ;;; Entry text mode
3960 (defun org-agenda-entry-text-show-here ()
3961 "Add some text from the entry as context to the current line."
3962 (let (m txt o)
3963 (setq m (org-get-at-bol 'org-hd-marker))
3964 (unless (marker-buffer m)
3965 (error "No marker points to an entry here"))
3966 (setq txt (concat "\n" (org-no-properties
3967 (org-agenda-get-some-entry-text
3968 m org-agenda-entry-text-maxlines
3969 org-agenda-entry-text-leaders))))
3970 (when (string-match "\\S-" txt)
3971 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3972 (overlay-put o 'evaporate t)
3973 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3974 (overlay-put o 'after-string txt))))
3976 (defun org-agenda-entry-text-show ()
3977 "Add entry context for all agenda lines."
3978 (interactive)
3979 (save-excursion
3980 (goto-char (point-max))
3981 (beginning-of-line 1)
3982 (while (not (bobp))
3983 (when (org-get-at-bol 'org-hd-marker)
3984 (org-agenda-entry-text-show-here))
3985 (beginning-of-line 0))))
3987 (defun org-agenda-entry-text-hide ()
3988 "Remove any shown entry context."
3989 (delq nil
3990 (mapcar (lambda (o)
3991 (if (eq (overlay-get o 'org-overlay-type)
3992 'agenda-entry-content)
3993 (progn (delete-overlay o) t)))
3994 (overlays-in (point-min) (point-max)))))
3996 (defun org-agenda-get-day-face (date)
3997 "Return the face DATE should be displayed with."
3998 (or (and (functionp org-agenda-day-face-function)
3999 (funcall org-agenda-day-face-function date))
4000 (cond ((org-agenda-todayp date)
4001 'org-agenda-date-today)
4002 ((member (calendar-day-of-week date) org-agenda-weekend-days)
4003 'org-agenda-date-weekend)
4004 (t 'org-agenda-date))))
4006 ;;; Agenda timeline
4008 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
4009 (defvar org-agenda-show-log-scoped) ;; dynamically scope in `org-timeline' or `org-agenda-list'
4011 (defun org-timeline (&optional dotodo)
4012 "Show a time-sorted view of the entries in the current org file.
4013 Only entries with a time stamp of today or later will be listed. With
4014 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
4015 under the current date.
4016 If the buffer contains an active region, only check the region for
4017 dates."
4018 (interactive "P")
4019 (let* ((dopast t)
4020 (org-agenda-show-log-scoped org-agenda-show-log)
4021 (org-agenda-show-log org-agenda-show-log-scoped)
4022 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
4023 (current-buffer))))
4024 (date (calendar-current-date))
4025 (beg (if (org-region-active-p) (region-beginning) (point-min)))
4026 (end (if (org-region-active-p) (region-end) (point-max)))
4027 (day-numbers (org-get-all-dates
4028 beg end 'no-ranges
4029 t org-agenda-show-log-scoped ; always include today
4030 org-timeline-show-empty-dates))
4031 (org-deadline-warning-days 0)
4032 (org-agenda-only-exact-dates t)
4033 (today (org-today))
4034 (past t)
4035 args
4036 s e rtn d emptyp)
4037 (setq org-agenda-redo-command
4038 (list 'let
4039 (list (list 'org-agenda-show-log 'org-agenda-show-log))
4040 (list 'org-switch-to-buffer-other-window (current-buffer))
4041 (list 'org-timeline (list 'quote dotodo))))
4042 (put 'org-agenda-redo-command 'org-lprops nil)
4043 (if (not dopast)
4044 ;; Remove past dates from the list of dates.
4045 (setq day-numbers (delq nil (mapcar (lambda(x)
4046 (if (>= x today) x nil))
4047 day-numbers))))
4048 (org-agenda-prepare (concat "Timeline " (file-name-nondirectory entry)))
4049 (org-compile-prefix-format 'timeline)
4050 (org-set-sorting-strategy 'timeline)
4051 (if org-agenda-show-log-scoped (push :closed args))
4052 (push :timestamp args)
4053 (push :deadline args)
4054 (push :scheduled args)
4055 (push :sexp args)
4056 (if dotodo (push :todo args))
4057 (insert "Timeline of file " entry "\n")
4058 (add-text-properties (point-min) (point)
4059 (list 'face 'org-agenda-structure))
4060 (org-agenda-mark-header-line (point-min))
4061 (while (setq d (pop day-numbers))
4062 (if (and (listp d) (eq (car d) :omitted))
4063 (progn
4064 (setq s (point))
4065 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
4066 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
4067 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
4068 (if (and (>= d today)
4069 dopast
4070 past)
4071 (progn
4072 (setq past nil)
4073 (insert (make-string 79 ?-) "\n")))
4074 (setq date (calendar-gregorian-from-absolute d))
4075 (setq s (point))
4076 (setq rtn (and (not emptyp)
4077 (apply 'org-agenda-get-day-entries entry
4078 date args)))
4079 (if (or rtn (equal d today) org-timeline-show-empty-dates)
4080 (progn
4081 (insert
4082 (if (stringp org-agenda-format-date)
4083 (format-time-string org-agenda-format-date
4084 (org-time-from-absolute date))
4085 (funcall org-agenda-format-date date))
4086 "\n")
4087 (put-text-property s (1- (point)) 'face
4088 (org-agenda-get-day-face date))
4089 (put-text-property s (1- (point)) 'org-date-line t)
4090 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4091 (if (equal d today)
4092 (put-text-property s (1- (point)) 'org-today t))
4093 (and rtn (insert (org-agenda-finalize-entries rtn 'timeline) "\n"))
4094 (put-text-property s (1- (point)) 'day d)))))
4095 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
4096 (point-min)))
4097 (add-text-properties
4098 (point-min) (point-max)
4099 `(org-agenda-type timeline org-redo-cmd ,org-agenda-redo-command))
4100 (org-agenda-finalize)
4101 (setq buffer-read-only t)))
4103 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
4104 "Return a list of all relevant day numbers from BEG to END buffer positions.
4105 If NO-RANGES is non-nil, include only the start and end dates of a range,
4106 not every single day in the range. If FORCE-TODAY is non-nil, make
4107 sure that TODAY is included in the list. If INACTIVE is non-nil, also
4108 inactive time stamps (those in square brackets) are included.
4109 When EMPTY is non-nil, also include days without any entries."
4110 (let ((re (concat
4111 (if pre-re pre-re "")
4112 (if inactive org-ts-regexp-both org-ts-regexp)))
4113 dates dates1 date day day1 day2 ts1 ts2 pos)
4114 (if force-today
4115 (setq dates (list (org-today))))
4116 (save-excursion
4117 (goto-char beg)
4118 (while (re-search-forward re end t)
4119 (setq day (time-to-days (org-time-string-to-time
4120 (substring (match-string 1) 0 10)
4121 (current-buffer) (match-beginning 0))))
4122 (or (memq day dates) (push day dates)))
4123 (unless no-ranges
4124 (goto-char beg)
4125 (while (re-search-forward org-tr-regexp end t)
4126 (setq pos (match-beginning 0))
4127 (setq ts1 (substring (match-string 1) 0 10)
4128 ts2 (substring (match-string 2) 0 10)
4129 day1 (time-to-days (org-time-string-to-time
4130 ts1 (current-buffer) pos))
4131 day2 (time-to-days (org-time-string-to-time
4132 ts2 (current-buffer) pos)))
4133 (while (< (setq day1 (1+ day1)) day2)
4134 (or (memq day1 dates) (push day1 dates)))))
4135 (setq dates (sort dates '<))
4136 (when empty
4137 (while (setq day (pop dates))
4138 (setq day2 (car dates))
4139 (push day dates1)
4140 (when (and day2 empty)
4141 (if (or (eq empty t)
4142 (and (numberp empty) (<= (- day2 day) empty)))
4143 (while (< (setq day (1+ day)) day2)
4144 (push (list day) dates1))
4145 (push (cons :omitted (- day2 day)) dates1))))
4146 (setq dates (nreverse dates1)))
4147 dates)))
4149 ;;; Agenda Daily/Weekly
4151 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4152 "Start day for the agenda view.
4153 Custom commands can set this variable in the options section.
4154 This is usually a string like \"2007-11-01\", \"+2d\" or any other
4155 input allowed when reading a date through the Org calendar.
4156 See the docstring of `org-read-date' for details.")
4157 (defvar org-starting-day nil) ; local variable in the agenda buffer
4158 (defvar org-arg-loc nil) ; local variable
4160 (defvar org-agenda-buffer-tmp-name nil)
4161 ;;;###autoload
4162 (defun org-agenda-list (&optional arg start-day span with-hour)
4163 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4164 The view will be for the current day or week, but from the overview buffer
4165 you will be able to go to other days/weeks.
4167 With a numeric prefix argument in an interactive call, the agenda will
4168 span ARG days. Lisp programs should instead specify SPAN to change
4169 the number of days. SPAN defaults to `org-agenda-span'.
4171 START-DAY defaults to TODAY, or to the most recent match for the weekday
4172 given in `org-agenda-start-on-weekday'.
4174 When WITH-HOUR is non-nil, only include scheduled and deadline
4175 items if they have an hour specification like [h]h:mm."
4176 (interactive "P")
4177 (if org-agenda-overriding-arguments
4178 (setq arg (car org-agenda-overriding-arguments)
4179 start-day (nth 1 org-agenda-overriding-arguments)
4180 span (nth 2 org-agenda-overriding-arguments)))
4181 (if (and (integerp arg) (> arg 0))
4182 (setq span arg arg nil))
4183 (catch 'exit
4184 (setq org-agenda-buffer-name
4185 (or org-agenda-buffer-tmp-name
4186 (if org-agenda-sticky
4187 (cond ((and org-keys (stringp org-match))
4188 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4189 (org-keys
4190 (format "*Org Agenda(%s)*" org-keys))
4191 (t "*Org Agenda(a)*")))
4192 org-agenda-buffer-name))
4193 (org-agenda-prepare "Day/Week")
4194 (setq start-day (or start-day org-agenda-start-day))
4195 (if (stringp start-day)
4196 ;; Convert to an absolute day number
4197 (setq start-day (time-to-days (org-read-date nil t start-day))))
4198 (org-compile-prefix-format 'agenda)
4199 (org-set-sorting-strategy 'agenda)
4200 (let* ((span (org-agenda-ndays-to-span
4201 (or span org-agenda-ndays org-agenda-span)))
4202 (today (org-today))
4203 (sd (or start-day today))
4204 (ndays (org-agenda-span-to-ndays span sd))
4205 (org-agenda-start-on-weekday
4206 (if (or (eq ndays 7) (eq ndays 14))
4207 org-agenda-start-on-weekday))
4208 (thefiles (org-agenda-files nil 'ifmode))
4209 (files thefiles)
4210 (start (if (or (null org-agenda-start-on-weekday)
4211 (< ndays 7))
4213 (let* ((nt (calendar-day-of-week
4214 (calendar-gregorian-from-absolute sd)))
4215 (n1 org-agenda-start-on-weekday)
4216 (d (- nt n1)))
4217 (- sd (+ (if (< d 0) 7 0) d)))))
4218 (day-numbers (list start))
4219 (day-cnt 0)
4220 (inhibit-redisplay (not debug-on-error))
4221 (org-agenda-show-log-scoped org-agenda-show-log)
4222 s e rtn rtnall file date d start-pos end-pos todayp
4223 clocktable-start clocktable-end filter)
4224 (setq org-agenda-redo-command
4225 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4226 (dotimes (n (1- ndays))
4227 (push (1+ (car day-numbers)) day-numbers))
4228 (setq day-numbers (nreverse day-numbers))
4229 (setq clocktable-start (car day-numbers)
4230 clocktable-end (1+ (or (org-last day-numbers) 0)))
4231 (org-set-local 'org-starting-day (car day-numbers))
4232 (org-set-local 'org-arg-loc arg)
4233 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
4234 (unless org-agenda-compact-blocks
4235 (let* ((d1 (car day-numbers))
4236 (d2 (org-last day-numbers))
4237 (w1 (org-days-to-iso-week d1))
4238 (w2 (org-days-to-iso-week d2)))
4239 (setq s (point))
4240 (if org-agenda-overriding-header
4241 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4242 nil 'face 'org-agenda-structure) "\n")
4243 (insert (org-agenda-span-name span)
4244 "-agenda"
4245 (if (< (- d2 d1) 350)
4246 (if (= w1 w2)
4247 (format " (W%02d)" w1)
4248 (format " (W%02d-W%02d)" w1 w2))
4250 ":\n")))
4251 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4252 'org-date-line t))
4253 (org-agenda-mark-header-line s))
4254 (while (setq d (pop day-numbers))
4255 (setq date (calendar-gregorian-from-absolute d)
4256 s (point))
4257 (if (or (setq todayp (= d today))
4258 (and (not start-pos) (= d sd)))
4259 (setq start-pos (point))
4260 (if (and start-pos (not end-pos))
4261 (setq end-pos (point))))
4262 (setq files thefiles
4263 rtnall nil)
4264 (while (setq file (pop files))
4265 (catch 'nextfile
4266 (org-check-agenda-file file)
4267 (let ((org-agenda-entry-types org-agenda-entry-types))
4268 ;; Starred types override non-starred equivalents
4269 (when (member :deadline* org-agenda-entry-types)
4270 (setq org-agenda-entry-types
4271 (delq :deadline org-agenda-entry-types)))
4272 (when (member :scheduled* org-agenda-entry-types)
4273 (setq org-agenda-entry-types
4274 (delq :scheduled org-agenda-entry-types)))
4275 ;; Honor with-hour
4276 (when with-hour
4277 (when (member :deadline org-agenda-entry-types)
4278 (setq org-agenda-entry-types
4279 (delq :deadline org-agenda-entry-types))
4280 (push :deadline* org-agenda-entry-types))
4281 (when (member :scheduled org-agenda-entry-types)
4282 (setq org-agenda-entry-types
4283 (delq :scheduled org-agenda-entry-types))
4284 (push :scheduled* org-agenda-entry-types)))
4285 (unless org-agenda-include-deadlines
4286 (setq org-agenda-entry-types
4287 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4288 (cond
4289 ((memq org-agenda-show-log-scoped '(only clockcheck))
4290 (setq rtn (org-agenda-get-day-entries
4291 file date :closed)))
4292 (org-agenda-show-log-scoped
4293 (setq rtn (apply 'org-agenda-get-day-entries
4294 file date
4295 (append '(:closed) org-agenda-entry-types))))
4297 (setq rtn (apply 'org-agenda-get-day-entries
4298 file date
4299 org-agenda-entry-types)))))
4300 (setq rtnall (append rtnall rtn)))) ;; all entries
4301 (if org-agenda-include-diary
4302 (let ((org-agenda-search-headline-for-time t))
4303 (require 'diary-lib)
4304 (setq rtn (org-get-entries-from-diary date))
4305 (setq rtnall (append rtnall rtn))))
4306 (if (or rtnall org-agenda-show-all-dates)
4307 (progn
4308 (setq day-cnt (1+ day-cnt))
4309 (insert
4310 (if (stringp org-agenda-format-date)
4311 (format-time-string org-agenda-format-date
4312 (org-time-from-absolute date))
4313 (funcall org-agenda-format-date date))
4314 "\n")
4315 (put-text-property s (1- (point)) 'face
4316 (org-agenda-get-day-face date))
4317 (put-text-property s (1- (point)) 'org-date-line t)
4318 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4319 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4320 (when todayp
4321 (put-text-property s (1- (point)) 'org-today t))
4322 (setq rtnall
4323 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4324 (if rtnall (insert ;; all entries
4325 (org-agenda-finalize-entries rtnall 'agenda)
4326 "\n"))
4327 (put-text-property s (1- (point)) 'day d)
4328 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4329 (when (and org-agenda-clockreport-mode clocktable-start)
4330 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4331 ;; the above line is to ensure the restricted range!
4332 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4333 tbl)
4334 (setq p (org-plist-delete p :block))
4335 (setq p (plist-put p :tstart clocktable-start))
4336 (setq p (plist-put p :tend clocktable-end))
4337 (setq p (plist-put p :scope 'agenda))
4338 (when (and (eq org-agenda-clockreport-mode 'with-filter)
4339 (setq filter (or org-agenda-tag-filter-while-redo
4340 (get 'org-agenda-tag-filter :preset-filter))))
4341 (setq p (plist-put p :tags (mapconcat (lambda (x)
4342 (if (string-match "[<>=]" x)
4345 filter ""))))
4346 (setq tbl (apply 'org-clock-get-clocktable p))
4347 (insert tbl)))
4348 (goto-char (point-min))
4349 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4350 (unless (and (pos-visible-in-window-p (point-min))
4351 (pos-visible-in-window-p (point-max)))
4352 (goto-char (1- (point-max)))
4353 (recenter -1)
4354 (if (not (pos-visible-in-window-p (or start-pos 1)))
4355 (progn
4356 (goto-char (or start-pos 1))
4357 (recenter 1))))
4358 (goto-char (or start-pos 1))
4359 (add-text-properties (point-min) (point-max)
4360 `(org-agenda-type agenda
4361 org-last-args (,arg ,start-day ,span)
4362 org-redo-cmd ,org-agenda-redo-command
4363 org-series-cmd ,org-cmd))
4364 (if (eq org-agenda-show-log-scoped 'clockcheck)
4365 (org-agenda-show-clocking-issues))
4366 (org-agenda-finalize)
4367 (setq buffer-read-only t)
4368 (message ""))))
4370 (defun org-agenda-ndays-to-span (n)
4371 "Return a span symbol for a span of N days, or N if none matches."
4372 (cond ((symbolp n) n)
4373 ((= n 1) 'day)
4374 ((= n 7) 'week)
4375 ((= n 14) 'fortnight)
4376 (t n)))
4378 (defun org-agenda-span-to-ndays (span &optional start-day)
4379 "Return ndays from SPAN, possibly starting at START-DAY.
4380 START-DAY is an absolute time value."
4381 (cond ((numberp span) span)
4382 ((eq span 'day) 1)
4383 ((eq span 'week) 7)
4384 ((eq span 'fortnight) 14)
4385 ((eq span 'month)
4386 (let ((date (calendar-gregorian-from-absolute start-day)))
4387 (calendar-last-day-of-month (car date) (caddr date))))
4388 ((eq span 'year)
4389 (let ((date (calendar-gregorian-from-absolute start-day)))
4390 (if (calendar-leap-year-p (caddr date)) 366 365)))))
4392 (defun org-agenda-span-name (span)
4393 "Return a SPAN name."
4394 (if (null span)
4396 (if (symbolp span)
4397 (capitalize (symbol-name span))
4398 (format "%d days" span))))
4400 ;;; Agenda word search
4402 (defvar org-agenda-search-history nil)
4404 (defvar org-search-syntax-table nil
4405 "Special syntax table for org-mode search.
4406 In this table, we have single quotes not as word constituents, to
4407 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4409 (defvar org-mode-syntax-table) ; From org.el
4410 (defun org-search-syntax-table ()
4411 (unless org-search-syntax-table
4412 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4413 (modify-syntax-entry ?' "." org-search-syntax-table)
4414 (modify-syntax-entry ?` "." org-search-syntax-table))
4415 org-search-syntax-table)
4417 (defvar org-agenda-last-search-view-search-was-boolean nil)
4419 ;;;###autoload
4420 (defun org-search-view (&optional todo-only string edit-at)
4421 "Show all entries that contain a phrase or words or regular expressions.
4423 With optional prefix argument TODO-ONLY, only consider entries that are
4424 TODO entries. The argument STRING can be used to pass a default search
4425 string into this function. If EDIT-AT is non-nil, it means that the
4426 user should get a chance to edit this string, with cursor at position
4427 EDIT-AT.
4429 The search string can be viewed either as a phrase that should be found as
4430 is, or it can be broken into a number of snippets, each of which must match
4431 in a Boolean way to select an entry. The default depends on the variable
4432 `org-agenda-search-view-always-boolean'.
4433 Even if this is turned off (the default) you can always switch to
4434 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4436 The default is a direct search of the whole phrase, where each space in
4437 the search string can expand to an arbitrary amount of whitespace,
4438 including newlines.
4440 If using a Boolean search, the search string is split on whitespace and
4441 each snippet is searched separately, with logical AND to select an entry.
4442 Words prefixed with a minus must *not* occur in the entry. Words without
4443 a prefix or prefixed with a plus must occur in the entry. Matching is
4444 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4445 match whole words, not parts of a word) if
4446 `org-agenda-search-view-force-full-words' is set (default is nil).
4448 Boolean search snippets enclosed by curly braces are interpreted as
4449 regular expressions that must or (when preceded with \"-\") must not
4450 match in the entry. Snippets enclosed into double quotes will be taken
4451 as a whole, to include whitespace.
4453 - If the search string starts with an asterisk, search only in headlines.
4454 - If (possibly after the leading star) the search string starts with an
4455 exclamation mark, this also means to look at TODO entries only, an effect
4456 that can also be achieved with a prefix argument.
4457 - If (possibly after star and exclamation mark) the search string starts
4458 with a colon, this will mean that the (non-regexp) snippets of the
4459 Boolean search must match as full words.
4461 This command searches the agenda files, and in addition the files listed
4462 in `org-agenda-text-search-extra-files'."
4463 (interactive "P")
4464 (if org-agenda-overriding-arguments
4465 (setq todo-only (car org-agenda-overriding-arguments)
4466 string (nth 1 org-agenda-overriding-arguments)
4467 edit-at (nth 2 org-agenda-overriding-arguments)))
4468 (let* ((props (list 'face nil
4469 'done-face 'org-agenda-done
4470 'org-not-done-regexp org-not-done-regexp
4471 'org-todo-regexp org-todo-regexp
4472 'org-complex-heading-regexp org-complex-heading-regexp
4473 'mouse-face 'highlight
4474 'help-echo (format "mouse-2 or RET jump to location")))
4475 (full-words org-agenda-search-view-force-full-words)
4476 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4477 regexp rtn rtnall files file pos inherited-tags
4478 marker category category-pos level tags c neg re boolean
4479 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4480 (unless (and (not edit-at)
4481 (stringp string)
4482 (string-match "\\S-" string))
4483 (setq string (read-string
4484 (if org-agenda-search-view-always-boolean
4485 "[+-]Word/{Regexp} ...: "
4486 "Phrase or [+-]Word/{Regexp} ...: ")
4487 (cond
4488 ((integerp edit-at) (cons string edit-at))
4489 (edit-at string))
4490 'org-agenda-search-history)))
4491 (catch 'exit
4492 (if org-agenda-sticky
4493 (setq org-agenda-buffer-name
4494 (if (stringp string)
4495 (format "*Org Agenda(%s:%s)*"
4496 (or org-keys (or (and todo-only "S") "s")) string)
4497 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4498 (org-agenda-prepare "SEARCH")
4499 (org-compile-prefix-format 'search)
4500 (org-set-sorting-strategy 'search)
4501 (setq org-agenda-redo-command
4502 (list 'org-search-view (if todo-only t nil)
4503 (list 'if 'current-prefix-arg nil string)))
4504 (setq org-agenda-query-string string)
4505 (if (equal (string-to-char string) ?*)
4506 (setq hdl-only t
4507 words (substring string 1))
4508 (setq words string))
4509 (when (equal (string-to-char words) ?!)
4510 (setq todo-only t
4511 words (substring words 1)))
4512 (when (equal (string-to-char words) ?:)
4513 (setq full-words t
4514 words (substring words 1)))
4515 (if (or org-agenda-search-view-always-boolean
4516 (member (string-to-char words) '(?- ?+ ?\{)))
4517 (setq boolean t))
4518 (setq words (org-split-string words))
4519 (let (www w)
4520 (while (setq w (pop words))
4521 (while (and (string-match "\\\\\\'" w) words)
4522 (setq w (concat (substring w 0 -1) " " (pop words))))
4523 (push w www))
4524 (setq words (nreverse www) www nil)
4525 (while (setq w (pop words))
4526 (when (and (string-match "\\`[-+]?{" w)
4527 (not (string-match "}\\'" w)))
4528 (while (and words (not (string-match "}\\'" (car words))))
4529 (setq w (concat w " " (pop words))))
4530 (setq w (concat w " " (pop words))))
4531 (push w www))
4532 (setq words (nreverse www)))
4533 (setq org-agenda-last-search-view-search-was-boolean boolean)
4534 (when boolean
4535 (let (wds w)
4536 (while (setq w (pop words))
4537 (if (or (equal (substring w 0 1) "\"")
4538 (and (> (length w) 1)
4539 (member (substring w 0 1) '("+" "-"))
4540 (equal (substring w 1 2) "\"")))
4541 (while (and words (not (equal (substring w -1) "\"")))
4542 (setq w (concat w " " (pop words)))))
4543 (and (string-match "\\`\\([-+]?\\)\"" w)
4544 (setq w (replace-match "\\1" nil nil w)))
4545 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4546 (push w wds))
4547 (setq words (nreverse wds))))
4548 (if boolean
4549 (mapc (lambda (w)
4550 (setq c (string-to-char w))
4551 (if (equal c ?-)
4552 (setq neg t w (substring w 1))
4553 (if (equal c ?+)
4554 (setq neg nil w (substring w 1))
4555 (setq neg nil)))
4556 (if (string-match "\\`{.*}\\'" w)
4557 (setq re (substring w 1 -1))
4558 (if full-words
4559 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4560 (setq re (regexp-quote (downcase w)))))
4561 (if neg (push re regexps-) (push re regexps+)))
4562 words)
4563 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4564 regexps+))
4565 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4566 (if (not regexps+)
4567 (setq regexp org-outline-regexp-bol)
4568 (setq regexp (pop regexps+))
4569 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4570 regexp))))
4571 (setq files (org-agenda-files nil 'ifmode))
4572 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4573 (pop org-agenda-text-search-extra-files)
4574 (setq files (org-add-archive-files files)))
4575 (setq files (append files org-agenda-text-search-extra-files)
4576 rtnall nil)
4577 (while (setq file (pop files))
4578 (setq ee nil)
4579 (catch 'nextfile
4580 (org-check-agenda-file file)
4581 (setq buffer (if (file-exists-p file)
4582 (org-get-agenda-file-buffer file)
4583 (error "No such file %s" file)))
4584 (if (not buffer)
4585 ;; If file does not exist, make sure an error message is sent
4586 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4587 file))))
4588 (with-current-buffer buffer
4589 (with-syntax-table (org-search-syntax-table)
4590 (unless (derived-mode-p 'org-mode)
4591 (error "Agenda file %s is not in `org-mode'" file))
4592 (let ((case-fold-search t))
4593 (save-excursion
4594 (save-restriction
4595 (if (eq buffer org-agenda-restrict)
4596 (narrow-to-region org-agenda-restrict-begin
4597 org-agenda-restrict-end)
4598 (widen))
4599 (goto-char (point-min))
4600 (unless (or (org-at-heading-p)
4601 (outline-next-heading))
4602 (throw 'nextfile t))
4603 (goto-char (max (point-min) (1- (point))))
4604 (while (re-search-forward regexp nil t)
4605 (org-back-to-heading t)
4606 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4607 (> (org-reduced-level (org-outline-level))
4608 org-agenda-search-view-max-outline-level)
4609 (forward-line -1)
4610 (outline-back-to-heading t)))
4611 (skip-chars-forward "* ")
4612 (setq beg (point-at-bol)
4613 beg1 (point)
4614 end (progn
4615 (outline-next-heading)
4616 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4617 (> (org-reduced-level (org-outline-level))
4618 org-agenda-search-view-max-outline-level)
4619 (forward-line 1)
4620 (outline-next-heading)))
4621 (point)))
4623 (catch :skip
4624 (goto-char beg)
4625 (org-agenda-skip)
4626 (setq str (buffer-substring-no-properties
4627 (point-at-bol)
4628 (if hdl-only (point-at-eol) end)))
4629 (mapc (lambda (wr) (when (string-match wr str)
4630 (goto-char (1- end))
4631 (throw :skip t)))
4632 regexps-)
4633 (mapc (lambda (wr) (unless (string-match wr str)
4634 (goto-char (1- end))
4635 (throw :skip t)))
4636 (if todo-only
4637 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
4638 regexps+)
4639 regexps+))
4640 (goto-char beg)
4641 (setq marker (org-agenda-new-marker (point))
4642 category (org-get-category)
4643 level (make-string (org-reduced-level (org-outline-level)) ? )
4644 category-pos (get-text-property (point) 'org-category-position)
4645 inherited-tags
4646 (or (eq org-agenda-show-inherited-tags 'always)
4647 (and (listp org-agenda-show-inherited-tags)
4648 (memq 'todo org-agenda-show-inherited-tags))
4649 (and (eq org-agenda-show-inherited-tags t)
4650 (or (eq org-agenda-use-tag-inheritance t)
4651 (memq 'todo org-agenda-use-tag-inheritance))))
4652 tags (org-get-tags-at nil (not inherited-tags))
4653 txt (org-agenda-format-item
4655 (buffer-substring-no-properties
4656 beg1 (point-at-eol))
4657 level category tags t))
4658 (org-add-props txt props
4659 'org-marker marker 'org-hd-marker marker
4660 'org-todo-regexp org-todo-regexp
4661 'level level
4662 'org-complex-heading-regexp org-complex-heading-regexp
4663 'priority 1000 'org-category category
4664 'org-category-position category-pos
4665 'type "search")
4666 (push txt ee)
4667 (goto-char (1- end))))))))))
4668 (setq rtn (nreverse ee))
4669 (setq rtnall (append rtnall rtn)))
4670 (if org-agenda-overriding-header
4671 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4672 nil 'face 'org-agenda-structure) "\n")
4673 (insert "Search words: ")
4674 (add-text-properties (point-min) (1- (point))
4675 (list 'face 'org-agenda-structure))
4676 (setq pos (point))
4677 (insert string "\n")
4678 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4679 (setq pos (point))
4680 (unless org-agenda-multi
4681 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
4682 (add-text-properties pos (1- (point))
4683 (list 'face 'org-agenda-structure))))
4684 (org-agenda-mark-header-line (point-min))
4685 (when rtnall
4686 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4687 (goto-char (point-min))
4688 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4689 (add-text-properties (point-min) (point-max)
4690 `(org-agenda-type search
4691 org-last-args (,todo-only ,string ,edit-at)
4692 org-redo-cmd ,org-agenda-redo-command
4693 org-series-cmd ,org-cmd))
4694 (org-agenda-finalize)
4695 (setq buffer-read-only t))))
4697 ;;; Agenda TODO list
4699 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4700 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4701 (concat
4702 (if (or (equal keywords "ALL") (not keywords))
4703 (propertize "ALL" 'face 'warning)
4704 (mapconcat
4705 (lambda (kw)
4706 (propertize kw 'face (org-get-todo-face kw)))
4707 (org-split-string keywords "|")
4708 "|"))
4709 "\n"))
4711 (defvar org-select-this-todo-keyword nil)
4712 (defvar org-last-arg nil)
4714 ;;;###autoload
4715 (defun org-todo-list (&optional arg)
4716 "Show all (not done) TODO entries from all agenda file in a single list.
4717 The prefix arg can be used to select a specific TODO keyword and limit
4718 the list to these. When using \\[universal-argument], you will be prompted
4719 for a keyword. A numeric prefix directly selects the Nth keyword in
4720 `org-todo-keywords-1'."
4721 (interactive "P")
4722 (if org-agenda-overriding-arguments
4723 (setq arg org-agenda-overriding-arguments))
4724 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4725 (let* ((today (org-today))
4726 (date (calendar-gregorian-from-absolute today))
4727 (kwds org-todo-keywords-for-agenda)
4728 (completion-ignore-case t)
4729 (org-select-this-todo-keyword
4730 (if (stringp arg) arg
4731 (and arg (integerp arg) (> arg 0)
4732 (nth (1- arg) kwds))))
4733 rtn rtnall files file pos)
4734 (when (equal arg '(4))
4735 (setq org-select-this-todo-keyword
4736 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4737 (mapcar 'list kwds) nil nil)))
4738 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4739 (catch 'exit
4740 (if org-agenda-sticky
4741 (setq org-agenda-buffer-name
4742 (if (stringp org-select-this-todo-keyword)
4743 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4744 org-select-this-todo-keyword)
4745 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4746 (org-agenda-prepare "TODO")
4747 (org-compile-prefix-format 'todo)
4748 (org-set-sorting-strategy 'todo)
4749 (setq org-agenda-redo-command
4750 `(org-todo-list (or (and (numberp current-prefix-arg)
4751 current-prefix-arg)
4752 ,org-select-this-todo-keyword
4753 current-prefix-arg ,arg)))
4754 (setq files (org-agenda-files nil 'ifmode)
4755 rtnall nil)
4756 (while (setq file (pop files))
4757 (catch 'nextfile
4758 (org-check-agenda-file file)
4759 (setq rtn (org-agenda-get-day-entries file date :todo))
4760 (setq rtnall (append rtnall rtn))))
4761 (if org-agenda-overriding-header
4762 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4763 nil 'face 'org-agenda-structure) "\n")
4764 (insert "Global list of TODO items of type: ")
4765 (add-text-properties (point-min) (1- (point))
4766 (list 'face 'org-agenda-structure
4767 'short-heading
4768 (concat "ToDo: "
4769 (or org-select-this-todo-keyword "ALL"))))
4770 (org-agenda-mark-header-line (point-min))
4771 (insert (org-agenda-propertize-selected-todo-keywords
4772 org-select-this-todo-keyword))
4773 (setq pos (point))
4774 (unless org-agenda-multi
4775 (insert "Available with `N r': (0)[ALL]")
4776 (let ((n 0) s)
4777 (mapc (lambda (x)
4778 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4779 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4780 (insert "\n "))
4781 (insert " " s))
4782 kwds))
4783 (insert "\n"))
4784 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4785 (org-agenda-mark-header-line (point-min))
4786 (when rtnall
4787 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4788 (goto-char (point-min))
4789 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4790 (add-text-properties (point-min) (point-max)
4791 `(org-agenda-type todo
4792 org-last-args ,arg
4793 org-redo-cmd ,org-agenda-redo-command
4794 org-series-cmd ,org-cmd))
4795 (org-agenda-finalize)
4796 (setq buffer-read-only t))))
4798 ;;; Agenda tags match
4800 ;;;###autoload
4801 (defun org-tags-view (&optional todo-only match)
4802 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4803 The prefix arg TODO-ONLY limits the search to TODO entries."
4804 (interactive "P")
4805 (if org-agenda-overriding-arguments
4806 (setq todo-only (car org-agenda-overriding-arguments)
4807 match (nth 1 org-agenda-overriding-arguments)))
4808 (let* ((org-tags-match-list-sublevels
4809 org-tags-match-list-sublevels)
4810 (completion-ignore-case t)
4811 rtn rtnall files file pos matcher
4812 buffer)
4813 (when (and (stringp match) (not (string-match "\\S-" match)))
4814 (setq match nil))
4815 (catch 'exit
4816 (if org-agenda-sticky
4817 (setq org-agenda-buffer-name
4818 (if (stringp match)
4819 (format "*Org Agenda(%s:%s)*"
4820 (or org-keys (or (and todo-only "M") "m")) match)
4821 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4822 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4823 ;; expanding tags within `org-make-tags-matcher'
4824 (org-agenda-prepare (concat "TAGS " match))
4825 (setq matcher (org-make-tags-matcher match)
4826 match (car matcher) matcher (cdr matcher))
4827 (org-compile-prefix-format 'tags)
4828 (org-set-sorting-strategy 'tags)
4829 (setq org-agenda-query-string match)
4830 (setq org-agenda-redo-command
4831 (list 'org-tags-view `(quote ,todo-only)
4832 (list 'if 'current-prefix-arg nil `(quote ,org-agenda-query-string))))
4833 (setq files (org-agenda-files nil 'ifmode)
4834 rtnall nil)
4835 (while (setq file (pop files))
4836 (catch 'nextfile
4837 (org-check-agenda-file file)
4838 (setq buffer (if (file-exists-p file)
4839 (org-get-agenda-file-buffer file)
4840 (error "No such file %s" file)))
4841 (if (not buffer)
4842 ;; If file does not exist, error message to agenda
4843 (setq rtn (list
4844 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4845 rtnall (append rtnall rtn))
4846 (with-current-buffer buffer
4847 (unless (derived-mode-p 'org-mode)
4848 (error "Agenda file %s is not in `org-mode'" file))
4849 (save-excursion
4850 (save-restriction
4851 (if (eq buffer org-agenda-restrict)
4852 (narrow-to-region org-agenda-restrict-begin
4853 org-agenda-restrict-end)
4854 (widen))
4855 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4856 (setq rtnall (append rtnall rtn))))))))
4857 (if org-agenda-overriding-header
4858 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4859 nil 'face 'org-agenda-structure) "\n")
4860 (insert "Headlines with TAGS match: ")
4861 (add-text-properties (point-min) (1- (point))
4862 (list 'face 'org-agenda-structure
4863 'short-heading
4864 (concat "Match: " match)))
4865 (setq pos (point))
4866 (insert match "\n")
4867 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4868 (setq pos (point))
4869 (unless org-agenda-multi
4870 (insert "Press `C-u r' to search again with new search string\n"))
4871 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4872 (org-agenda-mark-header-line (point-min))
4873 (when rtnall
4874 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4875 (goto-char (point-min))
4876 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4877 (add-text-properties (point-min) (point-max)
4878 `(org-agenda-type tags
4879 org-last-args (,todo-only ,match)
4880 org-redo-cmd ,org-agenda-redo-command
4881 org-series-cmd ,org-cmd))
4882 (org-agenda-finalize)
4883 (setq buffer-read-only t))))
4885 ;;; Agenda Finding stuck projects
4887 (defvar org-agenda-skip-regexp nil
4888 "Regular expression used in skipping subtrees for the agenda.
4889 This is basically a temporary global variable that can be set and then
4890 used by user-defined selections using `org-agenda-skip-function'.")
4892 (defvar org-agenda-overriding-header nil
4893 "When set during agenda, todo and tags searches it replaces the header.
4894 This variable should not be set directly, but custom commands can bind it
4895 in the options section.")
4897 (defun org-agenda-skip-entry-when-regexp-matches ()
4898 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4899 If yes, it returns the end position of this entry, causing agenda commands
4900 to skip the entry but continuing the search in the subtree. This is a
4901 function that can be put into `org-agenda-skip-function' for the duration
4902 of a command."
4903 (let ((end (save-excursion (org-end-of-subtree t)))
4904 skip)
4905 (save-excursion
4906 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4907 (and skip end)))
4909 (defun org-agenda-skip-subtree-when-regexp-matches ()
4910 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4911 If yes, it returns the end position of this tree, causing agenda commands
4912 to skip this subtree. This is a function that can be put into
4913 `org-agenda-skip-function' for the duration of a command."
4914 (let ((end (save-excursion (org-end-of-subtree t)))
4915 skip)
4916 (save-excursion
4917 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4918 (and skip end)))
4920 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4921 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4922 If yes, it returns the end position of the current entry (NOT the tree),
4923 causing agenda commands to skip the entry but continuing the search in
4924 the subtree. This is a function that can be put into
4925 `org-agenda-skip-function' for the duration of a command. An important
4926 use of this function is for the stuck project list."
4927 (let ((end (save-excursion (org-end-of-subtree t)))
4928 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4929 skip)
4930 (save-excursion
4931 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4932 (and skip entry-end)))
4934 (defun org-agenda-skip-entry-if (&rest conditions)
4935 "Skip entry if any of CONDITIONS is true.
4936 See `org-agenda-skip-if' for details."
4937 (org-agenda-skip-if nil conditions))
4939 (defun org-agenda-skip-subtree-if (&rest conditions)
4940 "Skip subtree if any of CONDITIONS is true.
4941 See `org-agenda-skip-if' for details."
4942 (org-agenda-skip-if t conditions))
4944 (defun org-agenda-skip-if (subtree conditions)
4945 "Checks current entity for CONDITIONS.
4946 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4947 the entry (i.e. the text before the next heading) is checked.
4949 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4950 from different tests. Valid conditions are:
4952 scheduled Check if there is a scheduled cookie
4953 notscheduled Check if there is no scheduled cookie
4954 deadline Check if there is a deadline
4955 notdeadline Check if there is no deadline
4956 timestamp Check if there is a timestamp (also deadline or scheduled)
4957 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4958 regexp Check if regexp matches
4959 notregexp Check if regexp does not match.
4960 todo Check if TODO keyword matches
4961 nottodo Check if TODO keyword does not match
4963 The regexp is taken from the conditions list, it must come right after
4964 the `regexp' or `notregexp' element.
4966 `todo' and `nottodo' accept as an argument a list of todo
4967 keywords, which may include \"*\" to match any todo keyword.
4969 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4971 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4973 Instead of a list, a keyword class may be given. For example:
4975 (org-agenda-skip-entry-if 'nottodo 'done)
4977 would skip entries that haven't been marked with any of \"DONE\"
4978 keywords. Possible classes are: `todo', `done', `any'.
4980 If any of these conditions is met, this function returns the end point of
4981 the entity, causing the search to continue from there. This is a function
4982 that can be put into `org-agenda-skip-function' for the duration of a command."
4983 (let (beg end m)
4984 (org-back-to-heading t)
4985 (setq beg (point)
4986 end (if subtree
4987 (progn (org-end-of-subtree t) (point))
4988 (progn (outline-next-heading) (1- (point)))))
4989 (goto-char beg)
4990 (and
4992 (and (memq 'scheduled conditions)
4993 (re-search-forward org-scheduled-time-regexp end t))
4994 (and (memq 'notscheduled conditions)
4995 (not (re-search-forward org-scheduled-time-regexp end t)))
4996 (and (memq 'deadline conditions)
4997 (re-search-forward org-deadline-time-regexp end t))
4998 (and (memq 'notdeadline conditions)
4999 (not (re-search-forward org-deadline-time-regexp end t)))
5000 (and (memq 'timestamp conditions)
5001 (re-search-forward org-ts-regexp end t))
5002 (and (memq 'nottimestamp conditions)
5003 (not (re-search-forward org-ts-regexp end t)))
5004 (and (setq m (memq 'regexp conditions))
5005 (stringp (nth 1 m))
5006 (re-search-forward (nth 1 m) end t))
5007 (and (setq m (memq 'notregexp conditions))
5008 (stringp (nth 1 m))
5009 (not (re-search-forward (nth 1 m) end t)))
5010 (and (or
5011 (setq m (memq 'nottodo conditions))
5012 (setq m (memq 'todo-unblocked conditions))
5013 (setq m (memq 'nottodo-unblocked conditions))
5014 (setq m (memq 'todo conditions)))
5015 (org-agenda-skip-if-todo m end)))
5016 end)))
5018 (defun org-agenda-skip-if-todo (args end)
5019 "Helper function for `org-agenda-skip-if', do not use it directly.
5020 ARGS is a list with first element either `todo', `nottodo',
5021 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
5022 a list of TODO keywords, or a state symbol `todo' or `done' or
5023 `any'."
5024 (let ((kw (car args))
5025 (arg (cadr args))
5026 todo-wds todo-re)
5027 (setq todo-wds
5028 (org-uniquify
5029 (cond
5030 ((listp arg) ;; list of keywords
5031 (if (member "*" arg)
5032 (mapcar 'substring-no-properties org-todo-keywords-1)
5033 arg))
5034 ((symbolp arg) ;; keyword class name
5035 (cond
5036 ((eq arg 'todo)
5037 (org-delete-all org-done-keywords
5038 (mapcar 'substring-no-properties
5039 org-todo-keywords-1)))
5040 ((eq arg 'done) org-done-keywords)
5041 ((eq arg 'any)
5042 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
5043 (setq todo-re
5044 (concat "^\\*+[ \t]+\\<\\("
5045 (mapconcat 'identity todo-wds "\\|")
5046 "\\)\\>"))
5047 (cond
5048 ((eq kw 'todo) (re-search-forward todo-re end t))
5049 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
5050 ((eq kw 'todo-unblocked)
5051 (catch 'unblocked
5052 (while (re-search-forward todo-re end t)
5053 (or (org-entry-blocked-p) (throw 'unblocked t)))
5054 nil))
5055 ((eq kw 'nottodo-unblocked)
5056 (catch 'unblocked
5057 (while (re-search-forward todo-re end t)
5058 (or (org-entry-blocked-p) (throw 'unblocked nil)))
5062 ;;;###autoload
5063 (defun org-agenda-list-stuck-projects (&rest ignore)
5064 "Create agenda view for projects that are stuck.
5065 Stuck projects are project that have no next actions. For the definitions
5066 of what a project is and how to check if it stuck, customize the variable
5067 `org-stuck-projects'."
5068 (interactive)
5069 (let* ((org-agenda-skip-function
5070 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
5071 ;; We could have used org-agenda-skip-if here.
5072 (org-agenda-overriding-header
5073 (or org-agenda-overriding-header "List of stuck projects: "))
5074 (matcher (nth 0 org-stuck-projects))
5075 (todo (nth 1 org-stuck-projects))
5076 (todo-wds (if (member "*" todo)
5077 (progn
5078 (org-agenda-prepare-buffers (org-agenda-files
5079 nil 'ifmode))
5080 (org-delete-all
5081 org-done-keywords-for-agenda
5082 (copy-sequence org-todo-keywords-for-agenda)))
5083 todo))
5084 (todo-re (concat "^\\*+[ \t]+\\("
5085 (mapconcat 'identity todo-wds "\\|")
5086 "\\)\\>"))
5087 (tags (nth 2 org-stuck-projects))
5088 (tags-re (if (member "*" tags)
5089 (concat org-outline-regexp-bol
5090 (org-re ".*:[[:alnum:]_@#%]+:[ \t]*$"))
5091 (if tags
5092 (concat org-outline-regexp-bol
5093 ".*:\\("
5094 (mapconcat 'identity tags "\\|")
5095 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
5096 (gen-re (nth 3 org-stuck-projects))
5097 (re-list
5098 (delq nil
5099 (list
5100 (if todo todo-re)
5101 (if tags tags-re)
5102 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
5103 gen-re)))))
5104 (setq org-agenda-skip-regexp
5105 (if re-list
5106 (mapconcat 'identity re-list "\\|")
5107 (error "No information how to identify unstuck projects")))
5108 (org-tags-view nil matcher)
5109 (setq org-agenda-buffer-name (buffer-name))
5110 (with-current-buffer org-agenda-buffer-name
5111 (setq org-agenda-redo-command
5112 `(org-agenda-list-stuck-projects ,current-prefix-arg)))))
5114 ;;; Diary integration
5116 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
5117 (defvar diary-list-entries-hook)
5118 (defvar diary-time-regexp)
5119 (defun org-get-entries-from-diary (date)
5120 "Get the (Emacs Calendar) diary entries for DATE."
5121 (require 'diary-lib)
5122 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
5123 (diary-display-hook '(fancy-diary-display))
5124 (diary-display-function 'fancy-diary-display)
5125 (pop-up-frames nil)
5126 (diary-list-entries-hook
5127 (cons 'org-diary-default-entry diary-list-entries-hook))
5128 (diary-file-name-prefix nil) ; turn this feature off
5129 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
5130 entries
5131 (org-disable-agenda-to-diary t))
5132 (save-excursion
5133 (save-window-excursion
5134 (funcall (if (fboundp 'diary-list-entries)
5135 'diary-list-entries 'list-diary-entries)
5136 date 1)))
5137 (if (not (get-buffer diary-fancy-buffer))
5138 (setq entries nil)
5139 (with-current-buffer diary-fancy-buffer
5140 (setq buffer-read-only nil)
5141 (if (zerop (buffer-size))
5142 ;; No entries
5143 (setq entries nil)
5144 ;; Omit the date and other unnecessary stuff
5145 (org-agenda-cleanup-fancy-diary)
5146 ;; Add prefix to each line and extend the text properties
5147 (if (zerop (buffer-size))
5148 (setq entries nil)
5149 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
5150 (setq entries
5151 (with-temp-buffer
5152 (insert entries) (goto-char (point-min))
5153 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5154 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5155 (replace-match (concat "; " (match-string 1)))))
5156 (buffer-string)))))
5157 (set-buffer-modified-p nil)
5158 (kill-buffer diary-fancy-buffer)))
5159 (when entries
5160 (setq entries (org-split-string entries "\n"))
5161 (setq entries
5162 (mapcar
5163 (lambda (x)
5164 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5165 ;; Extend the text properties to the beginning of the line
5166 (org-add-props x (text-properties-at (1- (length x)) x)
5167 'type "diary" 'date date 'face 'org-agenda-diary))
5168 entries)))))
5170 (defvar org-agenda-cleanup-fancy-diary-hook nil
5171 "Hook run when the fancy diary buffer is cleaned up.")
5173 (defun org-agenda-cleanup-fancy-diary ()
5174 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5175 This gets rid of the date, the underline under the date, and
5176 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5177 date. It also removes lines that contain only whitespace."
5178 (goto-char (point-min))
5179 (if (looking-at ".*?:[ \t]*")
5180 (progn
5181 (replace-match "")
5182 (re-search-forward "\n=+$" nil t)
5183 (replace-match "")
5184 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5185 (re-search-forward "\n=+$" nil t)
5186 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5187 (goto-char (point-min))
5188 (while (re-search-forward "^ +\n" nil t)
5189 (replace-match ""))
5190 (goto-char (point-min))
5191 (if (re-search-forward "^Org-mode dummy\n?" nil t)
5192 (replace-match ""))
5193 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5195 ;; Make sure entries from the diary have the right text properties.
5196 (eval-after-load "diary-lib"
5197 '(if (boundp 'diary-modify-entry-list-string-function)
5198 ;; We can rely on the hook, nothing to do
5200 ;; Hook not available, must use advice to make this work
5201 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5202 "Make the position visible."
5203 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5204 (stringp string)
5205 buffer-file-name)
5206 (setq string (org-modify-diary-entry-string string))))))
5208 (defun org-modify-diary-entry-string (string)
5209 "Add text properties to string, allowing org-mode to act on it."
5210 (org-add-props string nil
5211 'mouse-face 'highlight
5212 'help-echo (if buffer-file-name
5213 (format "mouse-2 or RET jump to diary file %s"
5214 (abbreviate-file-name buffer-file-name))
5216 'org-agenda-diary-link t
5217 'org-marker (org-agenda-new-marker (point-at-bol))))
5219 (defun org-diary-default-entry ()
5220 "Add a dummy entry to the diary.
5221 Needed to avoid empty dates which mess up holiday display."
5222 ;; Catch the error if dealing with the new add-to-diary-alist
5223 (when org-disable-agenda-to-diary
5224 (condition-case nil
5225 (org-add-to-diary-list original-date "Org-mode dummy" "")
5226 (error
5227 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
5229 (defun org-add-to-diary-list (&rest args)
5230 (if (fboundp 'diary-add-to-list)
5231 (apply 'diary-add-to-list args)
5232 (apply 'add-to-diary-list args)))
5234 (defvar org-diary-last-run-time nil)
5236 ;;;###autoload
5237 (defun org-diary (&rest args)
5238 "Return diary information from org files.
5239 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5240 It accesses org files and extracts information from those files to be
5241 listed in the diary. The function accepts arguments specifying what
5242 items should be listed. For a list of arguments allowed here, see the
5243 variable `org-agenda-entry-types'.
5245 The call in the diary file should look like this:
5247 &%%(org-diary) ~/path/to/some/orgfile.org
5249 Use a separate line for each org file to check. Or, if you omit the file name,
5250 all files listed in `org-agenda-files' will be checked automatically:
5252 &%%(org-diary)
5254 If you don't give any arguments (as in the example above), the default value
5255 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5256 So the example above may also be written as
5258 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5260 The function expects the lisp variables `entry' and `date' to be provided
5261 by the caller, because this is how the calendar works. Don't use this
5262 function from a program - use `org-agenda-get-day-entries' instead."
5263 (when (> (- (org-float-time)
5264 org-agenda-last-marker-time)
5266 ;; I am not sure if this works with sticky agendas, because the marker
5267 ;; list is then no longer a global variable.
5268 (org-agenda-reset-markers))
5269 (org-compile-prefix-format 'agenda)
5270 (org-set-sorting-strategy 'agenda)
5271 (setq args (or args org-agenda-entry-types))
5272 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5273 (list entry)
5274 (org-agenda-files t)))
5275 (time (org-float-time))
5276 file rtn results)
5277 (when (or (not org-diary-last-run-time)
5278 (> (- time
5279 org-diary-last-run-time)
5281 (org-agenda-prepare-buffers files))
5282 (setq org-diary-last-run-time time)
5283 ;; If this is called during org-agenda, don't return any entries to
5284 ;; the calendar. Org Agenda will list these entries itself.
5285 (if org-disable-agenda-to-diary (setq files nil))
5286 (while (setq file (pop files))
5287 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5288 (setq results (append results rtn)))
5289 (when results
5290 (setq results
5291 (mapcar (lambda (i) (replace-regexp-in-string
5292 org-bracket-link-regexp "\\3" i)) results))
5293 (concat (org-agenda-finalize-entries results) "\n"))))
5295 ;;; Agenda entry finders
5297 (defun org-agenda-get-day-entries (file date &rest args)
5298 "Does the work for `org-diary' and `org-agenda'.
5299 FILE is the path to a file to be checked for entries. DATE is date like
5300 the one returned by `calendar-current-date'. ARGS are symbols indicating
5301 which kind of entries should be extracted. For details about these, see
5302 the documentation of `org-diary'."
5303 (setq args (or args org-agenda-entry-types))
5304 (let* ((org-startup-folded nil)
5305 (org-startup-align-all-tables nil)
5306 (buffer (if (file-exists-p file)
5307 (org-get-agenda-file-buffer file)
5308 (error "No such file %s" file)))
5309 arg results rtn deadline-results)
5310 (if (not buffer)
5311 ;; If file does not exist, make sure an error message ends up in diary
5312 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5313 (with-current-buffer buffer
5314 (unless (derived-mode-p 'org-mode)
5315 (error "Agenda file %s is not in `org-mode'" file))
5316 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5317 (let ((case-fold-search nil))
5318 (save-excursion
5319 (save-restriction
5320 (if (eq buffer org-agenda-restrict)
5321 (narrow-to-region org-agenda-restrict-begin
5322 org-agenda-restrict-end)
5323 (widen))
5324 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
5325 (while (setq arg (pop args))
5326 (cond
5327 ((and (eq arg :todo)
5328 (equal date (calendar-gregorian-from-absolute
5329 (org-today))))
5330 (setq rtn (org-agenda-get-todos))
5331 (setq results (append results rtn)))
5332 ((eq arg :timestamp)
5333 (setq rtn (org-agenda-get-blocks))
5334 (setq results (append results rtn))
5335 (setq rtn (org-agenda-get-timestamps deadline-results))
5336 (setq results (append results rtn)))
5337 ((eq arg :sexp)
5338 (setq rtn (org-agenda-get-sexps))
5339 (setq results (append results rtn)))
5340 ((eq arg :scheduled)
5341 (setq rtn (org-agenda-get-scheduled deadline-results))
5342 (setq results (append results rtn)))
5343 ((eq arg :scheduled*)
5344 (setq rtn (org-agenda-get-scheduled deadline-results t))
5345 (setq results (append results rtn)))
5346 ((eq arg :closed)
5347 (setq rtn (org-agenda-get-progress))
5348 (setq results (append results rtn)))
5349 ((eq arg :deadline)
5350 (setq rtn (org-agenda-get-deadlines))
5351 (setq deadline-results (copy-sequence rtn))
5352 (setq results (append results rtn)))
5353 ((eq arg :deadline*)
5354 (setq rtn (org-agenda-get-deadlines t))
5355 (setq deadline-results (copy-sequence rtn))
5356 (setq results (append results rtn))))))))
5357 results))))
5359 (defsubst org-em (x y list)
5360 "Is X or Y a member of LIST?"
5361 (or (memq x list) (memq y list)))
5363 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5364 (defvar org-agenda-sorting-strategy-selected nil)
5366 (defun org-agenda-get-todos ()
5367 "Return the TODO information for agenda display."
5368 (let* ((props (list 'face nil
5369 'done-face 'org-agenda-done
5370 'org-not-done-regexp org-not-done-regexp
5371 'org-todo-regexp org-todo-regexp
5372 'org-complex-heading-regexp org-complex-heading-regexp
5373 'mouse-face 'highlight
5374 'help-echo
5375 (format "mouse-2 or RET jump to org file %s"
5376 (abbreviate-file-name buffer-file-name))))
5377 (regexp (format org-heading-keyword-regexp-format
5378 (cond
5379 ((and org-select-this-todo-keyword
5380 (equal org-select-this-todo-keyword "*"))
5381 org-todo-regexp)
5382 (org-select-this-todo-keyword
5383 (concat "\\("
5384 (mapconcat 'identity
5385 (org-split-string
5386 org-select-this-todo-keyword
5387 "|")
5388 "\\|") "\\)"))
5389 (t org-not-done-regexp))))
5390 marker priority category category-pos level tags todo-state ts-date ts-date-type
5391 ee txt beg end inherited-tags todo-state-end-pos)
5392 (goto-char (point-min))
5393 (while (re-search-forward regexp nil t)
5394 (catch :skip
5395 (save-match-data
5396 (beginning-of-line)
5397 (org-agenda-skip)
5398 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5399 (unless (and (setq todo-state (org-get-todo-state))
5400 (setq todo-state-end-pos (match-end 2)))
5401 (goto-char end)
5402 (throw :skip nil))
5403 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5404 (goto-char (1+ beg))
5405 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5406 (throw :skip nil)))
5407 (goto-char (match-beginning 2))
5408 (setq marker (org-agenda-new-marker (match-beginning 0))
5409 category (org-get-category)
5410 ts-date (let (ts)
5411 (save-match-data
5412 (cond ((org-em 'scheduled-up 'scheduled-down
5413 org-agenda-sorting-strategy-selected)
5414 (setq ts (org-entry-get (point) "SCHEDULED")
5415 ts-date-type " scheduled"))
5416 ((org-em 'deadline-up 'deadline-down
5417 org-agenda-sorting-strategy-selected)
5418 (setq ts (org-entry-get (point) "DEADLINE")
5419 ts-date-type " deadline"))
5420 ((org-em 'ts-up 'ts-down
5421 org-agenda-sorting-strategy-selected)
5422 (setq ts (org-entry-get (point) "TIMESTAMP")
5423 ts-date-type " timestamp"))
5424 ((org-em 'tsia-up 'tsia-down
5425 org-agenda-sorting-strategy-selected)
5426 (setq ts (org-entry-get (point) "TIMESTAMP_IA")
5427 ts-date-type " timestamp_ia"))
5428 ((org-em 'timestamp-up 'timestamp-down
5429 org-agenda-sorting-strategy-selected)
5430 (setq ts (or (org-entry-get (point) "SCHEDULED")
5431 (org-entry-get (point) "DEADLINE")
5432 (org-entry-get (point) "TIMESTAMP")
5433 (org-entry-get (point) "TIMESTAMP_IA"))
5434 ts-date-type ""))
5435 (t (setq ts-date-type "")))
5436 (when ts (ignore-errors (org-time-string-to-absolute ts)))))
5437 category-pos (get-text-property (point) 'org-category-position)
5438 txt (org-trim
5439 (buffer-substring (match-beginning 2) (match-end 0)))
5440 inherited-tags
5441 (or (eq org-agenda-show-inherited-tags 'always)
5442 (and (listp org-agenda-show-inherited-tags)
5443 (memq 'todo org-agenda-show-inherited-tags))
5444 (and (eq org-agenda-show-inherited-tags t)
5445 (or (eq org-agenda-use-tag-inheritance t)
5446 (memq 'todo org-agenda-use-tag-inheritance))))
5447 tags (org-get-tags-at nil (not inherited-tags))
5448 level (make-string (org-reduced-level (org-outline-level)) ? )
5449 txt (org-agenda-format-item "" txt level category tags t)
5450 priority (1+ (org-get-priority txt)))
5451 (org-add-props txt props
5452 'org-marker marker 'org-hd-marker marker
5453 'priority priority 'org-category category
5454 'level level
5455 'ts-date ts-date
5456 'org-category-position category-pos
5457 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5458 (push txt ee)
5459 (if org-agenda-todo-list-sublevels
5460 (goto-char todo-state-end-pos)
5461 (org-end-of-subtree 'invisible))))
5462 (nreverse ee)))
5464 (defun org-agenda-todo-custom-ignore-p (time n)
5465 "Check whether timestamp is farther away than n number of days.
5466 This function is invoked if `org-agenda-todo-ignore-deadlines',
5467 `org-agenda-todo-ignore-scheduled' or
5468 `org-agenda-todo-ignore-timestamp' is set to an integer."
5469 (let ((days (org-time-stamp-to-now
5470 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5471 (if (>= n 0)
5472 (>= days n)
5473 (<= days n))))
5475 ;;;###autoload
5476 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5477 (&optional end)
5478 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5479 (when (or org-agenda-todo-ignore-with-date
5480 org-agenda-todo-ignore-scheduled
5481 org-agenda-todo-ignore-deadlines
5482 org-agenda-todo-ignore-timestamp)
5483 (setq end (or end (save-excursion (outline-next-heading) (point))))
5484 (save-excursion
5485 (or (and org-agenda-todo-ignore-with-date
5486 (re-search-forward org-ts-regexp end t))
5487 (and org-agenda-todo-ignore-scheduled
5488 (re-search-forward org-scheduled-time-regexp end t)
5489 (cond
5490 ((eq org-agenda-todo-ignore-scheduled 'future)
5491 (> (org-time-stamp-to-now
5492 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5493 ((eq org-agenda-todo-ignore-scheduled 'past)
5494 (<= (org-time-stamp-to-now
5495 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5496 ((numberp org-agenda-todo-ignore-scheduled)
5497 (org-agenda-todo-custom-ignore-p
5498 (match-string 1) org-agenda-todo-ignore-scheduled))
5499 (t)))
5500 (and org-agenda-todo-ignore-deadlines
5501 (re-search-forward org-deadline-time-regexp end t)
5502 (cond
5503 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5504 ((eq org-agenda-todo-ignore-deadlines 'far)
5505 (not (org-deadline-close (match-string 1))))
5506 ((eq org-agenda-todo-ignore-deadlines 'future)
5507 (> (org-time-stamp-to-now
5508 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5509 ((eq org-agenda-todo-ignore-deadlines 'past)
5510 (<= (org-time-stamp-to-now
5511 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5512 ((numberp org-agenda-todo-ignore-deadlines)
5513 (org-agenda-todo-custom-ignore-p
5514 (match-string 1) org-agenda-todo-ignore-deadlines))
5515 (t (org-deadline-close (match-string 1)))))
5516 (and org-agenda-todo-ignore-timestamp
5517 (let ((buffer (current-buffer))
5518 (regexp
5519 (concat
5520 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5521 (start (point)))
5522 ;; Copy current buffer into a temporary one
5523 (with-temp-buffer
5524 (insert-buffer-substring buffer start end)
5525 (goto-char (point-min))
5526 ;; Delete SCHEDULED and DEADLINE items
5527 (while (re-search-forward regexp end t)
5528 (delete-region (match-beginning 0) (match-end 0)))
5529 (goto-char (point-min))
5530 ;; No search for timestamp left
5531 (when (re-search-forward org-ts-regexp nil t)
5532 (cond
5533 ((eq org-agenda-todo-ignore-timestamp 'future)
5534 (> (org-time-stamp-to-now
5535 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5536 ((eq org-agenda-todo-ignore-timestamp 'past)
5537 (<= (org-time-stamp-to-now
5538 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5539 ((numberp org-agenda-todo-ignore-timestamp)
5540 (org-agenda-todo-custom-ignore-p
5541 (match-string 1) org-agenda-todo-ignore-timestamp))
5542 (t))))))))))
5544 (defun org-agenda-get-timestamps (&optional deadline-results)
5545 "Return the date stamp information for agenda display."
5546 (let* ((props (list 'face 'org-agenda-calendar-event
5547 'org-not-done-regexp org-not-done-regexp
5548 'org-todo-regexp org-todo-regexp
5549 'org-complex-heading-regexp org-complex-heading-regexp
5550 'mouse-face 'highlight
5551 'help-echo
5552 (format "mouse-2 or RET jump to org file %s"
5553 (abbreviate-file-name buffer-file-name))))
5554 (d1 (calendar-absolute-from-gregorian date))
5556 (deadline-position-alist
5557 (mapcar (lambda (a) (and (setq mm (get-text-property
5558 0 'org-hd-marker a))
5559 (cons (marker-position mm) a)))
5560 deadline-results))
5561 (remove-re org-ts-regexp)
5562 (regexp
5563 (concat
5564 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5565 (regexp-quote
5566 (substring
5567 (format-time-string
5568 (car org-time-stamp-formats)
5569 (apply 'encode-time ; DATE bound by calendar
5570 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5571 1 11))
5572 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5573 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5574 marker hdmarker deadlinep scheduledp clockp closedp inactivep
5575 donep tmp priority category category-pos level ee txt timestr tags
5576 b0 b3 e3 head todo-state end-of-match show-all warntime habitp
5577 inherited-tags ts-date)
5578 (goto-char (point-min))
5579 (while (setq end-of-match (re-search-forward regexp nil t))
5580 (setq b0 (match-beginning 0)
5581 b3 (match-beginning 3) e3 (match-end 3)
5582 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
5583 habitp (and (functionp 'org-is-habit-p) (save-match-data (org-is-habit-p)))
5584 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5585 (member todo-state
5586 org-agenda-repeating-timestamp-show-all)))
5587 (catch :skip
5588 (and (org-at-date-range-p) (throw :skip nil))
5589 (org-agenda-skip)
5590 (if (and (match-end 1)
5591 (not (= d1 (org-time-string-to-absolute
5592 (match-string 1) d1 nil show-all
5593 (current-buffer) b0))))
5594 (throw :skip nil))
5595 (if (and e3
5596 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
5597 (throw :skip nil))
5598 (setq tmp (buffer-substring (max (point-min)
5599 (- b0 org-ds-keyword-length))
5601 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
5602 inactivep (= (char-after b0) ?\[)
5603 deadlinep (string-match org-deadline-regexp tmp)
5604 scheduledp (string-match org-scheduled-regexp tmp)
5605 closedp (and org-agenda-include-inactive-timestamps
5606 (string-match org-closed-string tmp))
5607 clockp (and org-agenda-include-inactive-timestamps
5608 (or (string-match org-clock-string tmp)
5609 (string-match "]-+\\'" tmp)))
5610 warntime (get-text-property (point) 'org-appt-warntime)
5611 donep (member todo-state org-done-keywords))
5612 (if (or scheduledp deadlinep closedp clockp
5613 (and donep org-agenda-skip-timestamp-if-done))
5614 (throw :skip t))
5615 (if (string-match ">" timestr)
5616 ;; substring should only run to end of time stamp
5617 (setq timestr (substring timestr 0 (match-end 0))))
5618 (setq marker (org-agenda-new-marker b0)
5619 category (org-get-category b0)
5620 category-pos (get-text-property b0 'org-category-position))
5621 (save-excursion
5622 (if (not (re-search-backward org-outline-regexp-bol nil t))
5623 (throw :skip nil)
5624 (goto-char (match-beginning 0))
5625 (if (and (eq t org-agenda-skip-timestamp-if-deadline-is-shown)
5626 (assoc (point) deadline-position-alist))
5627 (throw :skip nil))
5628 (setq hdmarker (org-agenda-new-marker)
5629 inherited-tags
5630 (or (eq org-agenda-show-inherited-tags 'always)
5631 (and (listp org-agenda-show-inherited-tags)
5632 (memq 'agenda org-agenda-show-inherited-tags))
5633 (and (eq org-agenda-show-inherited-tags t)
5634 (or (eq org-agenda-use-tag-inheritance t)
5635 (memq 'agenda org-agenda-use-tag-inheritance))))
5636 tags (org-get-tags-at nil (not inherited-tags))
5637 level (make-string (org-reduced-level (org-outline-level)) ? ))
5638 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5639 (setq head (or (match-string 1) ""))
5640 (setq txt (org-agenda-format-item
5641 (if inactivep org-agenda-inactive-leader nil)
5642 head level category tags timestr
5643 remove-re habitp)))
5644 (setq priority (org-get-priority txt))
5645 (org-add-props txt props 'priority priority
5646 'org-marker marker 'org-hd-marker hdmarker
5647 'org-category category 'date date
5648 'level level
5649 'ts-date
5650 (ignore-errors (org-time-string-to-absolute timestr))
5651 'org-category-position category-pos
5652 'todo-state todo-state
5653 'warntime warntime
5654 'type "timestamp")
5655 (push txt ee))
5656 (if org-agenda-skip-additional-timestamps-same-entry
5657 (outline-next-heading)
5658 (goto-char end-of-match))))
5659 (nreverse ee)))
5661 (defun org-agenda-get-sexps ()
5662 "Return the sexp information for agenda display."
5663 (require 'diary-lib)
5664 (let* ((props (list 'face 'org-agenda-calendar-sexp
5665 'mouse-face 'highlight
5666 'help-echo
5667 (format "mouse-2 or RET jump to org file %s"
5668 (abbreviate-file-name buffer-file-name))))
5669 (regexp "^&?%%(")
5670 marker category extra category-pos level ee txt tags entry
5671 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5672 (goto-char (point-min))
5673 (while (re-search-forward regexp nil t)
5674 (catch :skip
5675 (org-agenda-skip)
5676 (setq beg (match-beginning 0))
5677 (goto-char (1- (match-end 0)))
5678 (setq b (point))
5679 (forward-sexp 1)
5680 (setq sexp (buffer-substring b (point)))
5681 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5682 (org-trim (match-string 1))
5683 ""))
5684 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5685 (when result
5686 (setq marker (org-agenda-new-marker beg)
5687 level (make-string (org-reduced-level (org-outline-level)) ? )
5688 category (org-get-category beg)
5689 category-pos (get-text-property beg 'org-category-position)
5690 inherited-tags
5691 (or (eq org-agenda-show-inherited-tags 'always)
5692 (and (listp org-agenda-show-inherited-tags)
5693 (memq 'agenda org-agenda-show-inherited-tags))
5694 (and (eq org-agenda-show-inherited-tags t)
5695 (or (eq org-agenda-use-tag-inheritance t)
5696 (memq 'agenda org-agenda-use-tag-inheritance))))
5697 tags (org-get-tags-at nil (not inherited-tags))
5698 todo-state (org-get-todo-state)
5699 warntime (get-text-property (point) 'org-appt-warntime)
5700 extra nil)
5702 (dolist (r (if (stringp result)
5703 (list result)
5704 result)) ;; we expect a list here
5705 (when (and org-agenda-diary-sexp-prefix
5706 (string-match org-agenda-diary-sexp-prefix r))
5707 (setq extra (match-string 0 r)
5708 r (replace-match "" nil nil r)))
5709 (if (string-match "\\S-" r)
5710 (setq txt r)
5711 (setq txt "SEXP entry returned empty string"))
5712 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5713 (org-add-props txt props 'org-marker marker
5714 'org-category category 'date date 'todo-state todo-state
5715 'org-category-position category-pos 'tags tags
5716 'level level
5717 'type "sexp" 'warntime warntime)
5718 (push txt ee)))))
5719 (nreverse ee)))
5721 ;; Calendar sanity: define some functions that are independent of
5722 ;; `calendar-date-style'.
5723 ;; Normally I would like to use ISO format when calling the diary functions,
5724 ;; but to make sure we still have Emacs 22 compatibility we bind
5725 ;; also `european-calendar-style' and use european format
5726 (defun org-anniversary (year month day &optional mark)
5727 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5728 (org-no-warnings
5729 (let ((calendar-date-style 'european) (european-calendar-style t))
5730 (diary-anniversary day month year mark))))
5731 (defun org-cyclic (N year month day &optional mark)
5732 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5733 (org-no-warnings
5734 (let ((calendar-date-style 'european) (european-calendar-style t))
5735 (diary-cyclic N day month year mark))))
5736 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5737 "Like `diary-block', but with fixed (ISO) order of arguments."
5738 (org-no-warnings
5739 (let ((calendar-date-style 'european) (european-calendar-style t))
5740 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
5741 (defun org-date (year month day &optional mark)
5742 "Like `diary-date', but with fixed (ISO) order of arguments."
5743 (org-no-warnings
5744 (let ((calendar-date-style 'european) (european-calendar-style t))
5745 (diary-date day month year mark))))
5747 ;; Define the` org-class' function
5748 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5749 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5750 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5751 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5752 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5753 `holidays', then any date that is known by the Emacs calendar to be a
5754 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5755 then those holidays will be skipped."
5756 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5757 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5758 (d (calendar-absolute-from-gregorian date))
5759 (h (when skip-weeks (calendar-check-holidays date))))
5760 (and
5761 (<= date1 d)
5762 (<= d date2)
5763 (= (calendar-day-of-week date) dayname)
5764 (or (not skip-weeks)
5765 (progn
5766 (require 'cal-iso)
5767 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5768 (not (or (and h (memq 'holidays skip-weeks))
5769 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5770 entry)))
5772 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
5773 "Like `org-class', but honor `calendar-date-style'.
5774 The order of the first 2 times 3 arguments depends on the variable
5775 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
5776 So for American calendars, give this as MONTH DAY YEAR, for European as
5777 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
5778 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
5779 is any number of ISO weeks in the block period for which the item should
5780 be skipped.
5782 This function is here only for backward compatibility and it is deprecated,
5783 please use `org-class' instead."
5784 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
5785 (date2 (org-order-calendar-date-args m2 d2 y2)))
5786 (org-class
5787 (nth 2 date1) (car date1) (nth 1 date1)
5788 (nth 2 date2) (car date2) (nth 1 date2)
5789 dayname skip-weeks)))
5790 (make-obsolete 'org-diary-class 'org-class "")
5792 (defalias 'org-get-closed 'org-agenda-get-progress)
5793 (defun org-agenda-get-progress ()
5794 "Return the logged TODO entries for agenda display."
5795 (let* ((props (list 'mouse-face 'highlight
5796 'org-not-done-regexp org-not-done-regexp
5797 'org-todo-regexp org-todo-regexp
5798 'org-complex-heading-regexp org-complex-heading-regexp
5799 'help-echo
5800 (format "mouse-2 or RET jump to org file %s"
5801 (abbreviate-file-name buffer-file-name))))
5802 (items (if (consp org-agenda-show-log-scoped)
5803 org-agenda-show-log-scoped
5804 (if (eq org-agenda-show-log-scoped 'clockcheck)
5805 '(clock)
5806 org-agenda-log-mode-items)))
5807 (parts
5808 (delq nil
5809 (list
5810 (if (memq 'closed items) (concat "\\<" org-closed-string))
5811 (if (memq 'clock items) (concat "\\<" org-clock-string))
5812 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5813 (parts-re (if parts (mapconcat 'identity parts "\\|")
5814 (error "`org-agenda-log-mode-items' is empty")))
5815 (regexp (concat
5816 "\\(" parts-re "\\)"
5817 " *\\["
5818 (regexp-quote
5819 (substring
5820 (format-time-string
5821 (car org-time-stamp-formats)
5822 (apply 'encode-time ; DATE bound by calendar
5823 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5824 1 11))))
5825 (org-agenda-search-headline-for-time nil)
5826 marker hdmarker priority category category-pos level tags closedp
5827 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5828 (goto-char (point-min))
5829 (while (re-search-forward regexp nil t)
5830 (catch :skip
5831 (org-agenda-skip)
5832 (setq marker (org-agenda-new-marker (match-beginning 0))
5833 closedp (equal (match-string 1) org-closed-string)
5834 statep (equal (string-to-char (match-string 1)) ?-)
5835 clockp (not (or closedp statep))
5836 state (and statep (match-string 2))
5837 category (org-get-category (match-beginning 0))
5838 category-pos (get-text-property (match-beginning 0) 'org-category-position)
5839 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5840 (when (string-match "\\]" timestr)
5841 ;; substring should only run to end of time stamp
5842 (setq rest (substring timestr (match-end 0))
5843 timestr (substring timestr 0 (match-end 0)))
5844 (if (and (not closedp) (not statep)
5845 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5846 rest))
5847 (progn (setq timestr (concat (substring timestr 0 -1)
5848 "-" (match-string 1 rest) "]"))
5849 (setq clocked (match-string 2 rest)))
5850 (setq clocked "-")))
5851 (save-excursion
5852 (setq extra
5853 (cond
5854 ((not org-agenda-log-mode-add-notes) nil)
5855 (statep
5856 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5857 (match-string 1)))
5858 (clockp
5859 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5860 (match-string 1)))))
5861 (if (not (re-search-backward org-outline-regexp-bol nil t))
5862 (throw :skip nil)
5863 (goto-char (match-beginning 0))
5864 (setq hdmarker (org-agenda-new-marker)
5865 inherited-tags
5866 (or (eq org-agenda-show-inherited-tags 'always)
5867 (and (listp org-agenda-show-inherited-tags)
5868 (memq 'todo org-agenda-show-inherited-tags))
5869 (and (eq org-agenda-show-inherited-tags t)
5870 (or (eq org-agenda-use-tag-inheritance t)
5871 (memq 'todo org-agenda-use-tag-inheritance))))
5872 tags (org-get-tags-at nil (not inherited-tags))
5873 level (make-string (org-reduced-level (org-outline-level)) ? ))
5874 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5875 (setq txt (match-string 1))
5876 (when extra
5877 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5878 (setq txt (concat (substring txt 0 (match-beginning 1))
5879 " - " extra " " (match-string 2 txt)))
5880 (setq txt (concat txt " - " extra))))
5881 (setq txt (org-agenda-format-item
5882 (cond
5883 (closedp "Closed: ")
5884 (statep (concat "State: (" state ")"))
5885 (t (concat "Clocked: (" clocked ")")))
5886 txt level category tags timestr)))
5887 (setq priority 100000)
5888 (org-add-props txt props
5889 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5890 'priority priority 'org-category category
5891 'org-category-position category-pos
5892 'level level
5893 'type "closed" 'date date
5894 'undone-face 'org-warning 'done-face 'org-agenda-done)
5895 (push txt ee))
5896 (goto-char (point-at-eol))))
5897 (nreverse ee)))
5899 (defun org-agenda-show-clocking-issues ()
5900 "Add overlays, showing issues with clocking.
5901 See also the user option `org-agenda-clock-consistency-checks'."
5902 (interactive)
5903 (let* ((org-time-clocksum-use-effort-durations nil)
5904 (pl org-agenda-clock-consistency-checks)
5905 (re (concat "^[ \t]*"
5906 org-clock-string
5907 "[ \t]+"
5908 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5909 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5910 (tlstart 0.)
5911 (tlend 0.)
5912 (maxtime (org-hh:mm-string-to-minutes
5913 (or (plist-get pl :max-duration) "24:00")))
5914 (mintime (org-hh:mm-string-to-minutes
5915 (or (plist-get pl :min-duration) 0)))
5916 (maxgap (org-hh:mm-string-to-minutes
5917 ;; default 30:00 means never complain
5918 (or (plist-get pl :max-gap) "30:00")))
5919 (gapok (mapcar 'org-hh:mm-string-to-minutes
5920 (plist-get pl :gap-ok-around)))
5921 (def-face (or (plist-get pl :default-face)
5922 '((:background "DarkRed") (:foreground "white"))))
5923 issue face m te ts dt ov)
5924 (goto-char (point-min))
5925 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5926 (setq issue nil face def-face)
5927 (catch 'next
5928 (setq m (org-get-at-bol 'org-marker)
5929 te nil ts nil)
5930 (unless (and m (markerp m))
5931 (setq issue "No valid clock line") (throw 'next t))
5932 (org-with-point-at m
5933 (save-excursion
5934 (goto-char (point-at-bol))
5935 (unless (looking-at re)
5936 (error "No valid Clock line")
5937 (throw 'next t))
5938 (unless (match-end 3)
5939 (setq issue "No end time"
5940 face (or (plist-get pl :no-end-time-face) face))
5941 (throw 'next t))
5942 (setq ts (match-string 1)
5943 te (match-string 3)
5944 ts (org-float-time
5945 (apply 'encode-time (org-parse-time-string ts)))
5946 te (org-float-time
5947 (apply 'encode-time (org-parse-time-string te)))
5948 dt (- te ts))))
5949 (cond
5950 ((> dt (* 60 maxtime))
5951 ;; a very long clocking chunk
5952 (setq issue (format "Clocking interval is very long: %s"
5953 (org-minutes-to-clocksum-string
5954 (floor (/ (float dt) 60.))))
5955 face (or (plist-get pl :long-face) face)))
5956 ((< dt (* 60 mintime))
5957 ;; a very short clocking chunk
5958 (setq issue (format "Clocking interval is very short: %s"
5959 (org-minutes-to-clocksum-string
5960 (floor (/ (float dt) 60.))))
5961 face (or (plist-get pl :short-face) face)))
5962 ((and (> tlend 0) (< ts tlend))
5963 ;; Two clock entries are overlapping
5964 (setq issue (format "Clocking overlap: %d minutes"
5965 (/ (- tlend ts) 60))
5966 face (or (plist-get pl :overlap-face) face)))
5967 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5968 ;; There is a gap, lets see if we need to report it
5969 (unless (org-agenda-check-clock-gap tlend ts gapok)
5970 (setq issue (format "Clocking gap: %d minutes"
5971 (/ (- ts tlend) 60))
5972 face (or (plist-get pl :gap-face) face))))
5973 (t nil)))
5974 (setq tlend (or te tlend) tlstart (or ts tlstart))
5975 (when issue
5976 ;; OK, there was some issue, add an overlay to show the issue
5977 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5978 (overlay-put ov 'before-string
5979 (concat
5980 (org-add-props
5981 (format "%-43s" (concat " " issue))
5983 'face face)
5984 "\n"))
5985 (overlay-put ov 'evaporate t)))))
5987 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5988 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5989 (catch 'exit
5990 (unless ok-list
5991 ;; there are no OK times for gaps...
5992 (throw 'exit nil))
5993 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5994 ;; This is more than 24 hours, so it is OK.
5995 ;; because we have at least one OK time, that must be in the
5996 ;; 24 hour interval.
5997 (throw 'exit t))
5998 ;; We have a shorter gap.
5999 ;; Now we have to get the minute of the day when these times are
6000 (let* ((t1dec (decode-time (seconds-to-time t1)))
6001 (t2dec (decode-time (seconds-to-time t2)))
6002 ;; compute the minute on the day
6003 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
6004 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
6005 (when (< min2 min1)
6006 ;; if min2 is smaller than min1, this means it is on the next day.
6007 ;; Wrap it to after midnight.
6008 (setq min2 (+ min2 1440)))
6009 ;; Now check if any of the OK times is in the gap
6010 (mapc (lambda (x)
6011 ;; Wrap the time to after midnight if necessary
6012 (if (< x min1) (setq x (+ x 1440)))
6013 ;; Check if in interval
6014 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
6015 ok-list)
6016 ;; Nope, this gap is not OK
6017 nil)))
6019 (defun org-agenda-get-deadlines (&optional with-hour)
6020 "Return the deadline information for agenda display.
6021 When WITH-HOUR is non-nil, only return deadlines with an hour
6022 specification like [h]h:mm."
6023 (let* ((props (list 'mouse-face 'highlight
6024 'org-not-done-regexp org-not-done-regexp
6025 'org-todo-regexp org-todo-regexp
6026 'org-complex-heading-regexp org-complex-heading-regexp
6027 'help-echo
6028 (format "mouse-2 or RET jump to org file %s"
6029 (abbreviate-file-name buffer-file-name))))
6030 (regexp (if with-hour
6031 org-deadline-time-hour-regexp
6032 org-deadline-time-regexp))
6033 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
6034 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
6035 (dl0 (car org-agenda-deadline-leaders))
6036 (dl1 (nth 1 org-agenda-deadline-leaders))
6037 (dl2 (or (nth 2 org-agenda-deadline-leaders) dl1))
6038 d2 diff dfrac wdays pos pos1 category category-pos level
6039 tags suppress-prewarning ee txt head face s todo-state
6040 show-all upcomingp donep timestr warntime inherited-tags ts-date)
6041 (goto-char (point-min))
6042 (while (re-search-forward regexp nil t)
6043 (catch :skip
6044 (org-agenda-skip)
6045 (setq s (match-string 1)
6046 txt nil
6047 pos (1- (match-beginning 1))
6048 todo-state (save-match-data (org-get-todo-state))
6049 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6050 (member todo-state
6051 org-agenda-repeating-timestamp-show-all))
6052 d2 (org-time-string-to-absolute
6053 s d1 'past show-all (current-buffer) pos)
6054 diff (- d2 d1))
6055 (setq suppress-prewarning
6056 (let ((ds (and org-agenda-skip-deadline-prewarning-if-scheduled
6057 (let ((item (buffer-substring (point-at-bol)
6058 (point-at-eol))))
6059 (save-match-data
6060 (and (string-match
6061 org-scheduled-time-regexp item)
6062 (match-string 1 item)))))))
6063 (cond
6064 ((not ds) nil)
6065 ;; The current item has a scheduled date (in ds), so
6066 ;; evaluate its prewarning lead time.
6067 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
6068 ;; Use global prewarning-restart lead time.
6069 org-agenda-skip-deadline-prewarning-if-scheduled)
6070 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
6071 'pre-scheduled)
6072 ;; Set prewarning to no earlier than scheduled.
6073 (min (- d2 (org-time-string-to-absolute
6074 ds d1 'past show-all (current-buffer) pos))
6075 org-deadline-warning-days))
6076 ;; Set prewarning to deadline.
6077 (t 0))))
6078 (setq wdays (if suppress-prewarning
6079 (let ((org-deadline-warning-days suppress-prewarning))
6080 (org-get-wdays s))
6081 (org-get-wdays s))
6082 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
6083 upcomingp (and todayp (> diff 0)))
6084 ;; When to show a deadline in the calendar:
6085 ;; If the expiration is within wdays warning time.
6086 ;; Past-due deadlines are only shown on the current date
6087 (if (and (or (and (<= diff wdays)
6088 (and todayp (not org-agenda-only-exact-dates)))
6089 (= diff 0)))
6090 (save-excursion
6091 ;; (setq todo-state (org-get-todo-state))
6092 (setq donep (member todo-state org-done-keywords))
6093 (if (and donep
6094 (or org-agenda-skip-deadline-if-done
6095 (not (= diff 0))))
6096 (setq txt nil)
6097 (setq category (org-get-category)
6098 warntime (get-text-property (point) 'org-appt-warntime)
6099 category-pos (get-text-property (point) 'org-category-position))
6100 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6101 (throw :skip nil)
6102 (goto-char (match-end 0))
6103 (setq pos1 (match-beginning 0))
6104 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6105 (setq inherited-tags
6106 (or (eq org-agenda-show-inherited-tags 'always)
6107 (and (listp org-agenda-show-inherited-tags)
6108 (memq 'agenda org-agenda-show-inherited-tags))
6109 (and (eq org-agenda-show-inherited-tags t)
6110 (or (eq org-agenda-use-tag-inheritance t)
6111 (memq 'agenda org-agenda-use-tag-inheritance))))
6112 tags (org-get-tags-at pos1 (not inherited-tags)))
6113 (setq head (buffer-substring
6114 (point)
6115 (progn (skip-chars-forward "^\r\n")
6116 (point))))
6117 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6118 (setq timestr
6119 (concat (substring s (match-beginning 1)) " "))
6120 (setq timestr 'time))
6121 (setq txt (org-agenda-format-item
6122 (cond ((= diff 0) dl0)
6123 ((> diff 0)
6124 (if (functionp dl1)
6125 (funcall dl1 diff date)
6126 (format dl1 diff)))
6128 (if (functionp dl2)
6129 (funcall dl2 diff date)
6130 (format dl2 (if (string= dl2 dl1)
6131 diff (abs diff))))))
6132 head level category tags
6133 (if (not (= diff 0)) nil timestr)))))
6134 (when txt
6135 (setq face (org-agenda-deadline-face dfrac))
6136 (org-add-props txt props
6137 'org-marker (org-agenda-new-marker pos)
6138 'warntime warntime
6139 'level level
6140 'ts-date d2
6141 'org-hd-marker (org-agenda-new-marker pos1)
6142 'priority (+ (- diff)
6143 (org-get-priority txt))
6144 'org-category category
6145 'org-category-position category-pos
6146 'todo-state todo-state
6147 'type (if upcomingp "upcoming-deadline" "deadline")
6148 'date (if upcomingp date d2)
6149 'face (if donep 'org-agenda-done face)
6150 'undone-face face 'done-face 'org-agenda-done)
6151 (push txt ee))))))
6152 (nreverse ee)))
6154 (defun org-agenda-deadline-face (fraction)
6155 "Return the face to displaying a deadline item.
6156 FRACTION is what fraction of the head-warning time has passed."
6157 (let ((faces org-agenda-deadline-faces) f)
6158 (catch 'exit
6159 (while (setq f (pop faces))
6160 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
6162 (defun org-agenda-get-scheduled (&optional deadline-results with-hour)
6163 "Return the scheduled information for agenda display.
6164 When WITH-HOUR is non-nil, only return scheduled items with
6165 an hour specification like [h]h:mm."
6166 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
6167 'org-todo-regexp org-todo-regexp
6168 'org-complex-heading-regexp org-complex-heading-regexp
6169 'done-face 'org-agenda-done
6170 'mouse-face 'highlight
6171 'help-echo
6172 (format "mouse-2 or RET jump to org file %s"
6173 (abbreviate-file-name buffer-file-name))))
6174 (regexp (if with-hour
6175 org-scheduled-time-hour-regexp
6176 org-scheduled-time-regexp))
6177 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
6178 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
6180 (deadline-position-alist
6181 (mapcar (lambda (a) (and (setq mm (get-text-property
6182 0 'org-hd-marker a))
6183 (cons (marker-position mm) a)))
6184 deadline-results))
6185 d2 diff pos pos1 category category-pos level tags donep
6186 ee txt head pastschedp todo-state face timestr s habitp show-all
6187 did-habit-check-p warntime inherited-tags ts-date suppress-delay
6188 ddays)
6189 (goto-char (point-min))
6190 (while (re-search-forward regexp nil t)
6191 (catch :skip
6192 (org-agenda-skip)
6193 (setq s (match-string 1)
6194 txt nil
6195 pos (1- (match-beginning 1))
6196 todo-state (save-match-data (org-get-todo-state))
6197 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6198 (member todo-state
6199 org-agenda-repeating-timestamp-show-all))
6200 d2 (org-time-string-to-absolute
6201 s d1 'past show-all (current-buffer) pos)
6202 diff (- d2 d1)
6203 warntime (get-text-property (point) 'org-appt-warntime))
6204 (setq pastschedp (and todayp (< diff 0)))
6205 (setq did-habit-check-p nil)
6206 (setq suppress-delay
6207 (let ((ds (and org-agenda-skip-scheduled-delay-if-deadline
6208 (let ((item (buffer-substring (point-at-bol) (point-at-eol))))
6209 (save-match-data
6210 (and (string-match
6211 org-deadline-time-regexp item)
6212 (match-string 1 item)))))))
6213 (cond
6214 ((not ds) nil)
6215 ;; The current item has a deadline date (in ds), so
6216 ;; evaluate its delay time.
6217 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6218 ;; Use global delay time.
6219 (- org-agenda-skip-scheduled-delay-if-deadline))
6220 ((eq org-agenda-skip-scheduled-delay-if-deadline
6221 'post-deadline)
6222 ;; Set delay to no later than deadline.
6223 (min (- d2 (org-time-string-to-absolute
6224 ds d1 'past show-all (current-buffer) pos))
6225 org-scheduled-delay-days))
6226 (t 0))))
6227 (setq ddays (if suppress-delay
6228 (let ((org-scheduled-delay-days suppress-delay))
6229 (org-get-wdays s t t))
6230 (org-get-wdays s t)))
6231 ;; Use a delay of 0 when there is a repeater and the delay is
6232 ;; of the form --3d
6233 (when (and (save-match-data (string-match "--[0-9]+[hdwmy]" s))
6234 (< (org-time-string-to-absolute s)
6235 (org-time-string-to-absolute
6236 s d2 'past nil (current-buffer) pos)))
6237 (setq ddays 0))
6238 ;; When to show a scheduled item in the calendar:
6239 ;; If it is on or past the date.
6240 (when (or (and (> ddays 0) (= diff (- ddays)))
6241 (and (zerop ddays) (= diff 0))
6242 (and (< (+ diff ddays) 0)
6243 (< (abs diff) org-scheduled-past-days)
6244 (and todayp (not org-agenda-only-exact-dates)))
6245 ;; org-is-habit-p uses org-entry-get, which is expansive
6246 ;; so we go extra mile to only call it once
6247 (and todayp
6248 (boundp 'org-habit-show-all-today)
6249 org-habit-show-all-today
6250 (setq did-habit-check-p t)
6251 (setq habitp (and (functionp 'org-is-habit-p)
6252 (org-is-habit-p)))))
6253 (save-excursion
6254 (setq donep (member todo-state org-done-keywords))
6255 (if (and donep
6256 (or org-agenda-skip-scheduled-if-done
6257 (not (= diff 0))
6258 (and (functionp 'org-is-habit-p)
6259 (org-is-habit-p))))
6260 (setq txt nil)
6261 (setq habitp (if did-habit-check-p habitp
6262 (and (functionp 'org-is-habit-p)
6263 (org-is-habit-p))))
6264 (setq category (org-get-category)
6265 category-pos (get-text-property (point) 'org-category-position))
6266 (if (and (eq org-agenda-skip-scheduled-if-deadline-is-shown
6267 'repeated-after-deadline)
6268 (org-get-deadline-time (point))
6269 (<= 0 (- d2 (time-to-days (org-get-deadline-time (point))))))
6270 (throw :skip nil))
6271 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6272 (throw :skip nil)
6273 (goto-char (match-end 0))
6274 (setq pos1 (match-beginning 0))
6275 (if habitp
6276 (if (or (not org-habit-show-habits)
6277 (and (not todayp)
6278 (boundp 'org-habit-show-habits-only-for-today)
6279 org-habit-show-habits-only-for-today))
6280 (throw :skip nil))
6281 (if (and
6282 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
6283 (and (eq org-agenda-skip-scheduled-if-deadline-is-shown 'not-today)
6284 pastschedp))
6285 (setq mm (assoc pos1 deadline-position-alist)))
6286 (throw :skip nil)))
6287 (setq inherited-tags
6288 (or (eq org-agenda-show-inherited-tags 'always)
6289 (and (listp org-agenda-show-inherited-tags)
6290 (memq 'agenda org-agenda-show-inherited-tags))
6291 (and (eq org-agenda-show-inherited-tags t)
6292 (or (eq org-agenda-use-tag-inheritance t)
6293 (memq 'agenda org-agenda-use-tag-inheritance))))
6295 tags (org-get-tags-at nil (not inherited-tags)))
6296 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6297 (setq head (buffer-substring
6298 (point)
6299 (progn (skip-chars-forward "^\r\n") (point))))
6300 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6301 (setq timestr
6302 (concat (substring s (match-beginning 1)) " "))
6303 (setq timestr 'time))
6304 (setq txt (org-agenda-format-item
6305 (if (= diff 0)
6306 (car org-agenda-scheduled-leaders)
6307 (format (nth 1 org-agenda-scheduled-leaders)
6308 (- 1 diff)))
6309 head level category tags
6310 (if (not (= diff 0)) nil timestr)
6311 nil habitp))))
6312 (when txt
6313 (setq face
6314 (cond
6315 ((and (not habitp) pastschedp)
6316 'org-scheduled-previously)
6317 (todayp 'org-scheduled-today)
6318 (t 'org-scheduled))
6319 habitp (and habitp (org-habit-parse-todo)))
6320 (org-add-props txt props
6321 'undone-face face
6322 'face (if donep 'org-agenda-done face)
6323 'org-marker (org-agenda-new-marker pos)
6324 'org-hd-marker (org-agenda-new-marker pos1)
6325 'type (if pastschedp "past-scheduled" "scheduled")
6326 'date (if pastschedp d2 date)
6327 'ts-date d2
6328 'warntime warntime
6329 'level level
6330 'priority (if habitp
6331 (org-habit-get-priority habitp)
6332 (+ 94 (- 5 diff) (org-get-priority txt)))
6333 'org-category category
6334 'category-position category-pos
6335 'org-habit-p habitp
6336 'todo-state todo-state)
6337 (push txt ee))))))
6338 (nreverse ee)))
6340 (defun org-agenda-get-blocks ()
6341 "Return the date-range information for agenda display."
6342 (let* ((props (list 'face nil
6343 'org-not-done-regexp org-not-done-regexp
6344 'org-todo-regexp org-todo-regexp
6345 'org-complex-heading-regexp org-complex-heading-regexp
6346 'mouse-face 'highlight
6347 'help-echo
6348 (format "mouse-2 or RET jump to org file %s"
6349 (abbreviate-file-name buffer-file-name))))
6350 (regexp org-tr-regexp)
6351 (d0 (calendar-absolute-from-gregorian date))
6352 marker hdmarker ee txt d1 d2 s1 s2 category category-pos
6353 level todo-state tags pos head donep inherited-tags)
6354 (goto-char (point-min))
6355 (while (re-search-forward regexp nil t)
6356 (catch :skip
6357 (org-agenda-skip)
6358 (setq pos (point))
6359 (let ((start-time (match-string 1))
6360 (end-time (match-string 2)))
6361 (setq s1 (match-string 1)
6362 s2 (match-string 2)
6363 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
6364 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
6365 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6366 ;; Only allow days between the limits, because the normal
6367 ;; date stamps will catch the limits.
6368 (save-excursion
6369 (setq todo-state (org-get-todo-state))
6370 (setq donep (member todo-state org-done-keywords))
6371 (if (and donep org-agenda-skip-timestamp-if-done)
6372 (throw :skip t))
6373 (setq marker (org-agenda-new-marker (point)))
6374 (setq category (org-get-category)
6375 category-pos (get-text-property (point) 'org-category-position))
6376 (if (not (re-search-backward org-outline-regexp-bol nil t))
6377 (throw :skip nil)
6378 (goto-char (match-beginning 0))
6379 (setq hdmarker (org-agenda-new-marker (point))
6380 inherited-tags
6381 (or (eq org-agenda-show-inherited-tags 'always)
6382 (and (listp org-agenda-show-inherited-tags)
6383 (memq 'agenda org-agenda-show-inherited-tags))
6384 (and (eq org-agenda-show-inherited-tags t)
6385 (or (eq org-agenda-use-tag-inheritance t)
6386 (memq 'agenda org-agenda-use-tag-inheritance))))
6388 tags (org-get-tags-at nil (not inherited-tags)))
6389 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6390 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
6391 (setq head (match-string 1))
6392 (let ((remove-re
6393 (if org-agenda-remove-timeranges-from-blocks
6394 (concat
6395 "<" (regexp-quote s1) ".*?>"
6396 "--"
6397 "<" (regexp-quote s2) ".*?>")
6398 nil)))
6399 (setq txt (org-agenda-format-item
6400 (format
6401 (nth (if (= d1 d2) 0 1)
6402 org-agenda-timerange-leaders)
6403 (1+ (- d0 d1)) (1+ (- d2 d1)))
6404 head level category tags
6405 (cond ((and (= d1 d0) (= d2 d0))
6406 (concat "<" start-time ">--<" end-time ">"))
6407 ((= d1 d0)
6408 (concat "<" start-time ">"))
6409 ((= d2 d0)
6410 (concat "<" end-time ">")))
6411 remove-re))))
6412 (org-add-props txt props
6413 'org-marker marker 'org-hd-marker hdmarker
6414 'type "block" 'date date
6415 'level level
6416 'todo-state todo-state
6417 'priority (org-get-priority txt) 'org-category category
6418 'org-category-position category-pos)
6419 (push txt ee))))
6420 (goto-char pos)))
6421 ;; Sort the entries by expiration date.
6422 (nreverse ee)))
6424 ;;; Agenda presentation and sorting
6426 (defvar org-prefix-has-time nil
6427 "A flag, set by `org-compile-prefix-format'.
6428 The flag is set if the currently compiled format contains a `%t'.")
6429 (defvar org-prefix-has-tag nil
6430 "A flag, set by `org-compile-prefix-format'.
6431 The flag is set if the currently compiled format contains a `%T'.")
6432 (defvar org-prefix-has-effort nil
6433 "A flag, set by `org-compile-prefix-format'.
6434 The flag is set if the currently compiled format contains a `%e'.")
6435 (defvar org-prefix-has-breadcrumbs nil
6436 "A flag, set by `org-compile-prefix-format'.
6437 The flag is set if the currently compiled format contains a `%b'.")
6438 (defvar org-prefix-category-length nil
6439 "Used by `org-compile-prefix-format' to remember the category field width.")
6440 (defvar org-prefix-category-max-length nil
6441 "Used by `org-compile-prefix-format' to remember the category field width.")
6443 (defun org-agenda-get-category-icon (category)
6444 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6445 (dolist (entry org-agenda-category-icon-alist)
6446 (when (org-string-match-p (car entry) category)
6447 (if (listp (cadr entry))
6448 (return (cadr entry))
6449 (return (apply 'create-image (cdr entry)))))))
6451 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6452 remove-re habitp)
6453 "Format TXT to be inserted into the agenda buffer.
6454 In particular, add the prefix and corresponding text properties.
6456 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6457 LEVEL may be a string to replace the `%l' specifier.
6458 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6459 category taken from local variable or file name. It will replace the `%c'
6460 specifier in the format.
6461 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6462 TXT for sorting of this entry, and for the `%t' specifier in the format.
6463 When DOTIME is a string, this string is searched for a time before TXT is.
6464 TAGS can be the tags of the headline.
6465 Any match of REMOVE-RE will be removed from TXT."
6466 ;; We keep the org-prefix-* variable values along with a compiled
6467 ;; formatter, so that multiple agendas existing at the same time do
6468 ;; not step on each other toes.
6470 ;; It was inconvenient to make these variables buffer local in
6471 ;; Agenda buffers, because this function expects to be called with
6472 ;; the buffer where item comes from being current, and not agenda
6473 ;; buffer
6474 (let* ((bindings (car org-prefix-format-compiled))
6475 (formatter (cadr org-prefix-format-compiled)))
6476 (loop for (var value) in bindings
6477 do (set var value))
6478 (save-match-data
6479 ;; Diary entries sometimes have extra whitespace at the beginning
6480 (setq txt (org-trim txt))
6482 ;; Fix the tags part in txt
6483 (setq txt (org-agenda-fix-displayed-tags
6484 txt tags
6485 org-agenda-show-inherited-tags
6486 org-agenda-hide-tags-regexp))
6488 (let* ((category (or category
6489 (if (stringp org-category)
6490 org-category
6491 (and org-category (symbol-name org-category)))
6492 (if buffer-file-name
6493 (file-name-sans-extension
6494 (file-name-nondirectory buffer-file-name))
6495 "")))
6496 (category-icon (org-agenda-get-category-icon category))
6497 (category-icon (if category-icon
6498 (propertize " " 'display category-icon)
6499 ""))
6500 ;; time, tag, effort are needed for the eval of the prefix format
6501 (tag (if tags (nth (1- (length tags)) tags) ""))
6502 time effort neffort
6503 (ts (if dotime (concat
6504 (if (stringp dotime) dotime "")
6505 (and org-agenda-search-headline-for-time txt))))
6506 (time-of-day (and dotime (org-get-time-of-day ts)))
6507 stamp plain s0 s1 s2 rtn srp l
6508 duration thecategory breadcrumbs)
6509 (and (derived-mode-p 'org-mode) buffer-file-name
6510 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6511 (when (and dotime time-of-day)
6512 ;; Extract starting and ending time and move them to prefix
6513 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6514 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6515 (setq s0 (match-string 0 ts)
6516 srp (and stamp (match-end 3))
6517 s1 (match-string (if plain 1 2) ts)
6518 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6520 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6521 ;; them, we might want to remove them there to avoid duplication.
6522 ;; The user can turn this off with a variable.
6523 (if (and org-prefix-has-time
6524 org-agenda-remove-times-when-in-prefix (or stamp plain)
6525 (string-match (concat (regexp-quote s0) " *") txt)
6526 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6527 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6528 (= (match-beginning 0) 0)
6530 (setq txt (replace-match "" nil nil txt))))
6531 ;; Normalize the time(s) to 24 hour
6532 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6533 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6535 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
6536 (let (org-time-clocksum-use-effort-durations)
6537 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6538 (setq s2
6539 (org-minutes-to-clocksum-string
6540 (+ (org-hh:mm-string-to-minutes s1)
6541 org-agenda-default-appointment-duration)))))
6543 ;; Compute the duration
6544 (when s2
6545 (setq duration (- (org-hh:mm-string-to-minutes s2)
6546 (org-hh:mm-string-to-minutes s1)))))
6548 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
6549 txt)
6550 ;; Tags are in the string
6551 (if (or (eq org-agenda-remove-tags t)
6552 (and org-agenda-remove-tags
6553 org-prefix-has-tag))
6554 (setq txt (replace-match "" t t txt))
6555 (setq txt (replace-match
6556 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6557 (match-string 2 txt))
6558 t t txt))))
6559 (when (derived-mode-p 'org-mode)
6560 (setq effort (ignore-errors (get-text-property 0 'org-effort txt))))
6562 ;; org-agenda-add-time-grid-maybe calls us with *Agenda* as
6563 ;; current buffer, so move this check outside of above
6564 (if effort
6565 (setq neffort (org-duration-string-to-minutes effort)
6566 effort (setq effort (concat "[" effort "]")))
6567 ;; prevent erroring out with %e format when there is no effort
6568 (setq effort ""))
6570 (when remove-re
6571 (while (string-match remove-re txt)
6572 (setq txt (replace-match "" t t txt))))
6574 ;; Set org-heading property on `txt' to mark the start of the
6575 ;; heading.
6576 (add-text-properties 0 (length txt) '(org-heading t) txt)
6578 ;; Prepare the variables needed in the eval of the compiled format
6579 (if org-prefix-has-breadcrumbs
6580 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6581 (let ((s (org-display-outline-path nil nil "->" t)))
6582 (if (eq "" s) "" (concat s "->"))))))
6583 (setq time (cond (s2 (concat
6584 (org-agenda-time-of-day-to-ampm-maybe s1)
6585 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6586 (if org-agenda-timegrid-use-ampm " ")))
6587 (s1 (concat
6588 (org-agenda-time-of-day-to-ampm-maybe s1)
6589 (if org-agenda-timegrid-use-ampm
6590 "........ "
6591 "......")))
6592 (t ""))
6593 extra (or (and (not habitp) extra) "")
6594 category (if (symbolp category) (symbol-name category) category)
6595 thecategory (copy-sequence category)
6596 level (or level ""))
6597 (if (string-match org-bracket-link-regexp category)
6598 (progn
6599 (setq l (if (match-end 3)
6600 (- (match-end 3) (match-beginning 3))
6601 (- (match-end 1) (match-beginning 1))))
6602 (when (< l (or org-prefix-category-length 0))
6603 (setq category (copy-sequence category))
6604 (org-add-props category nil
6605 'extra-space (make-string
6606 (- org-prefix-category-length l 1) ?\ ))))
6607 (if (and org-prefix-category-max-length
6608 (>= (length category) org-prefix-category-max-length))
6609 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6610 ;; Evaluate the compiled format
6611 (setq rtn (concat (eval formatter) txt))
6613 ;; And finally add the text properties
6614 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6615 (org-add-props rtn nil
6616 'org-category (if thecategory (downcase thecategory) category)
6617 'tags (mapcar 'org-downcase-keep-props tags)
6618 'org-highest-priority org-highest-priority
6619 'org-lowest-priority org-lowest-priority
6620 'time-of-day time-of-day
6621 'duration duration
6622 'effort effort
6623 'effort-minutes neffort
6624 'breadcrumbs breadcrumbs
6625 'txt txt
6626 'level level
6627 'time time
6628 'extra extra
6629 'format org-prefix-format-compiled
6630 'dotime dotime)))))
6632 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6633 "Remove tags string from TXT, and add a modified list of tags.
6634 The modified list may contain inherited tags, and tags matched by
6635 `org-agenda-hide-tags-regexp' will be removed."
6636 (when (or add-inherited hide-re)
6637 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
6638 (setq txt (substring txt 0 (match-beginning 0))))
6639 (setq tags
6640 (delq nil
6641 (mapcar (lambda (tg)
6642 (if (or (and hide-re (string-match hide-re tg))
6643 (and (not add-inherited)
6644 (get-text-property 0 'inherited tg)))
6646 tg))
6647 tags)))
6648 (when tags
6649 (let ((have-i (get-text-property 0 'inherited (car tags)))
6651 (setq txt (concat txt " :"
6652 (mapconcat
6653 (lambda (x)
6654 (setq i (get-text-property 0 'inherited x))
6655 (if (and have-i (not i))
6656 (progn
6657 (setq have-i nil)
6658 (concat ":" x))
6660 tags ":")
6661 (if have-i "::" ":"))))))
6662 txt)
6664 (defun org-downcase-keep-props (s)
6665 (let ((props (text-properties-at 0 s)))
6666 (setq s (downcase s))
6667 (add-text-properties 0 (length s) props s)
6670 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6672 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6673 "Add a time-grid for agenda items which need it.
6675 LIST is the list of agenda items formatted by `org-agenda-list'.
6676 NDAYS is the span of the current agenda view.
6677 TODAYP is `t' when the current agenda view is on today."
6678 (catch 'exit
6679 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6680 ((and todayp (member 'today (car org-agenda-time-grid))))
6681 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6682 ((member 'weekly (car org-agenda-time-grid)))
6683 (t (throw 'exit list)))
6684 (let* ((have (delq nil (mapcar
6685 (lambda (x) (get-text-property 1 'time-of-day x))
6686 list)))
6687 (string (nth 1 org-agenda-time-grid))
6688 (gridtimes (nth 2 org-agenda-time-grid))
6689 (req (car org-agenda-time-grid))
6690 (remove (member 'remove-match req))
6691 new time)
6692 (if (and (member 'require-timed req) (not have))
6693 ;; don't show empty grid
6694 (throw 'exit list))
6695 (while (setq time (pop gridtimes))
6696 (unless (and remove (member time have))
6697 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6698 (push (org-agenda-format-item
6699 nil string nil "" nil
6700 (concat (substring time 0 -2) ":" (substring time -2)))
6701 new)
6702 (put-text-property
6703 2 (length (car new)) 'face 'org-time-grid (car new))))
6704 (when (and todayp org-agenda-show-current-time-in-grid)
6705 (push (org-agenda-format-item
6706 nil org-agenda-current-time-string nil "" nil
6707 (format-time-string "%H:%M "))
6708 new)
6709 (put-text-property
6710 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6712 (if (member 'time-up org-agenda-sorting-strategy-selected)
6713 (append new list)
6714 (append list new)))))
6716 (defun org-compile-prefix-format (key)
6717 "Compile the prefix format into a Lisp form that can be evaluated.
6718 The resulting form and associated variable bindings is returned
6719 and stored in the variable `org-prefix-format-compiled'."
6720 (setq org-prefix-has-time nil
6721 org-prefix-has-tag nil
6722 org-prefix-category-length nil
6723 org-prefix-has-effort nil
6724 org-prefix-has-breadcrumbs nil)
6725 (let ((s (cond
6726 ((stringp org-agenda-prefix-format)
6727 org-agenda-prefix-format)
6728 ((assq key org-agenda-prefix-format)
6729 (cdr (assq key org-agenda-prefix-format)))
6730 (t " %-12:c%?-12t% s")))
6731 (start 0)
6732 varform vars var e c f opt)
6733 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6734 s start)
6735 (setq var (or (cdr (assoc (match-string 4 s)
6736 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6737 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6738 'eval)
6739 c (or (match-string 3 s) "")
6740 opt (match-beginning 1)
6741 start (1+ (match-beginning 0)))
6742 (if (equal var 'time) (setq org-prefix-has-time t))
6743 (if (equal var 'tag) (setq org-prefix-has-tag t))
6744 (if (equal var 'effort) (setq org-prefix-has-effort t))
6745 (if (equal var 'breadcrumbs) (setq org-prefix-has-breadcrumbs t))
6746 (setq f (concat "%" (match-string 2 s) "s"))
6747 (when (equal var 'category)
6748 (setq org-prefix-category-length
6749 (floor (abs (string-to-number (match-string 2 s)))))
6750 (setq org-prefix-category-max-length
6751 (let ((x (match-string 2 s)))
6752 (save-match-data
6753 (if (string-match "\\.[0-9]+" x)
6754 (string-to-number (substring (match-string 0 x) 1)))))))
6755 (if (eq var 'eval)
6756 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6757 (if opt
6758 (setq varform
6759 `(if (equal "" ,var)
6761 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
6762 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6763 (setq s (replace-match "%s" t nil s))
6764 (push varform vars))
6765 (setq vars (nreverse vars))
6766 (with-current-buffer (or org-agenda-buffer (current-buffer))
6767 (setq org-prefix-format-compiled
6768 (list
6769 `((org-prefix-has-time ,org-prefix-has-time)
6770 (org-prefix-has-tag ,org-prefix-has-tag)
6771 (org-prefix-category-length ,org-prefix-category-length)
6772 (org-prefix-has-effort ,org-prefix-has-effort)
6773 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6774 `(format ,s ,@vars))))))
6776 (defun org-set-sorting-strategy (key)
6777 (if (symbolp (car org-agenda-sorting-strategy))
6778 ;; the old format
6779 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6780 (setq org-agenda-sorting-strategy-selected
6781 (or (cdr (assq key org-agenda-sorting-strategy))
6782 (cdr (assq 'agenda org-agenda-sorting-strategy))
6783 '(time-up category-keep priority-down)))))
6785 (defun org-get-time-of-day (s &optional string mod24)
6786 "Check string S for a time of day.
6787 If found, return it as a military time number between 0 and 2400.
6788 If not found, return nil.
6789 The optional STRING argument forces conversion into a 5 character wide string
6790 HH:MM."
6791 (save-match-data
6792 (when
6793 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6794 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6795 (let* ((h (string-to-number (match-string 1 s)))
6796 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6797 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6798 (am-p (equal ampm "am"))
6799 (h1 (cond ((not ampm) h)
6800 ((= h 12) (if am-p 0 12))
6801 (t (+ h (if am-p 0 12)))))
6802 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6803 (mod h1 24) h1))
6804 (t0 (+ (* 100 h2) m))
6805 (t1 (concat (if (>= h1 24) "+" " ")
6806 (if (and org-agenda-time-leading-zero
6807 (< t0 1000)) "0" "")
6808 (if (< t0 100) "0" "")
6809 (if (< t0 10) "0" "")
6810 (int-to-string t0))))
6811 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6813 (defvar org-agenda-before-sorting-filter-function nil
6814 "Function to be applied to agenda items prior to sorting.
6815 Prior to sorting also means just before they are inserted into the agenda.
6817 To aid sorting, you may revisit the original entries and add more text
6818 properties which will later be used by the sorting functions.
6820 The function should take a string argument, an agenda line.
6821 It has access to the text properties in that line, which contain among
6822 other things, the property `org-hd-marker' that points to the entry
6823 where the line comes from. Note that not all lines going into the agenda
6824 have this property, only most.
6826 The function should return the modified string. It is probably best
6827 to ONLY change text properties.
6829 You can also use this function as a filter, by returning nil for lines
6830 you don't want to have in the agenda at all. For this application, you
6831 could bind the variable in the options section of a custom command.")
6833 (defun org-agenda-finalize-entries (list &optional type)
6834 "Sort, limit and concatenate the LIST of agenda items.
6835 The optional argument TYPE tells the agenda type."
6836 (let ((max-effort (cond ((listp org-agenda-max-effort)
6837 (cdr (assoc type org-agenda-max-effort)))
6838 (t org-agenda-max-effort)))
6839 (max-todo (cond ((listp org-agenda-max-todos)
6840 (cdr (assoc type org-agenda-max-todos)))
6841 (t org-agenda-max-todos)))
6842 (max-tags (cond ((listp org-agenda-max-tags)
6843 (cdr (assoc type org-agenda-max-tags)))
6844 (t org-agenda-max-tags)))
6845 (max-entries (cond ((listp org-agenda-max-entries)
6846 (cdr (assoc type org-agenda-max-entries)))
6847 (t org-agenda-max-entries))) l)
6848 (when org-agenda-before-sorting-filter-function
6849 (setq list
6850 (delq nil
6851 (mapcar
6852 org-agenda-before-sorting-filter-function list))))
6853 (setq list (mapcar 'org-agenda-highlight-todo list)
6854 list (mapcar 'identity (sort list 'org-entries-lessp)))
6855 (when max-effort
6856 (setq list (org-agenda-limit-entries
6857 list 'effort-minutes max-effort 'identity)))
6858 (when max-todo
6859 (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
6860 (when max-tags
6861 (setq list (org-agenda-limit-entries list 'tags max-tags)))
6862 (when max-entries
6863 (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
6864 (mapconcat 'identity list "\n")))
6866 (defun org-agenda-limit-entries (list prop limit &optional fn)
6867 "Limit the number of agenda entries."
6868 (let ((include (and limit (< limit 0))))
6869 (if limit
6870 (let ((fun (or fn (lambda (p) (if p 1))))
6871 (lim 0))
6872 (delq nil
6873 (mapcar
6874 (lambda (e)
6875 (let ((pval (funcall fun (get-text-property 1 prop e))))
6876 (if pval (setq lim (+ lim pval)))
6877 (cond ((and pval (<= lim (abs limit))) e)
6878 ((and include (not pval)) e))))
6879 list)))
6880 list)))
6882 (defun org-agenda-limit-interactively ()
6883 "In agenda, interactively limit entries to various maximums."
6884 (interactive)
6885 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6886 (num (string-to-number (read-from-minibuffer "How many? "))))
6887 (cond ((equal max ?e)
6888 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6889 ((equal max ?t)
6890 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6891 ((equal max ?T)
6892 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6893 ((equal max ?E)
6894 (let ((org-agenda-max-effort num)) (org-agenda-redo)))))
6895 (org-agenda-fit-window-to-buffer))
6897 (defun org-agenda-highlight-todo (x)
6898 (let ((org-done-keywords org-done-keywords-for-agenda)
6899 (case-fold-search nil)
6901 (if (eq x 'line)
6902 (save-excursion
6903 (beginning-of-line 1)
6904 (setq re (org-get-at-bol 'org-todo-regexp))
6905 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6906 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6907 (add-text-properties (match-beginning 0) (match-end 1)
6908 (list 'face (org-get-todo-face 1)))
6909 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6910 (delete-region (match-beginning 1) (1- (match-end 0)))
6911 (goto-char (match-beginning 1))
6912 (insert (format org-agenda-todo-keyword-format s)))))
6913 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6914 (setq re (get-text-property 0 'org-todo-regexp x))
6915 (when (and re
6916 ;; Test `pl' because if there's no heading content,
6917 ;; there's no point matching to highlight. Note
6918 ;; that if we didn't test `pl' first, and there
6919 ;; happened to be no keyword from `org-todo-regexp'
6920 ;; on this heading line, then the `equal' comparison
6921 ;; afterwards would spuriously succeed in the case
6922 ;; where `pl' is nil -- causing an args-out-of-range
6923 ;; error when we try to add text properties to text
6924 ;; that isn't there.
6926 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6927 x pl) pl))
6928 (add-text-properties
6929 (or (match-end 1) (match-end 0)) (match-end 0)
6930 (list 'face (org-get-todo-face (match-string 2 x)))
6932 (when (match-end 1)
6933 (setq x (concat (substring x 0 (match-end 1))
6934 (format org-agenda-todo-keyword-format
6935 (match-string 2 x))
6936 (org-add-props " " (text-properties-at 0 x))
6937 (substring x (match-end 3)))))))
6938 x)))
6940 (defsubst org-cmp-priority (a b)
6941 "Compare the priorities of string A and B."
6942 (let ((pa (or (get-text-property 1 'priority a) 0))
6943 (pb (or (get-text-property 1 'priority b) 0)))
6944 (cond ((> pa pb) +1)
6945 ((< pa pb) -1))))
6947 (defsubst org-cmp-effort (a b)
6948 "Compare the effort values of string A and B."
6949 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6950 (ea (or (get-text-property 1 'effort-minutes a) def))
6951 (eb (or (get-text-property 1 'effort-minutes b) def)))
6952 (cond ((> ea eb) +1)
6953 ((< ea eb) -1))))
6955 (defsubst org-cmp-category (a b)
6956 "Compare the string values of categories of strings A and B."
6957 (let ((ca (or (get-text-property 1 'org-category a) ""))
6958 (cb (or (get-text-property 1 'org-category b) "")))
6959 (cond ((string-lessp ca cb) -1)
6960 ((string-lessp cb ca) +1))))
6962 (defsubst org-cmp-todo-state (a b)
6963 "Compare the todo states of strings A and B."
6964 (let* ((ma (or (get-text-property 1 'org-marker a)
6965 (get-text-property 1 'org-hd-marker a)))
6966 (mb (or (get-text-property 1 'org-marker b)
6967 (get-text-property 1 'org-hd-marker b)))
6968 (fa (and ma (marker-buffer ma)))
6969 (fb (and mb (marker-buffer mb)))
6970 (todo-kwds
6971 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6972 (and fb (with-current-buffer fb org-todo-keywords-1))))
6973 (ta (or (get-text-property 1 'todo-state a) ""))
6974 (tb (or (get-text-property 1 'todo-state b) ""))
6975 (la (- (length (member ta todo-kwds))))
6976 (lb (- (length (member tb todo-kwds))))
6977 (donepa (member ta org-done-keywords-for-agenda))
6978 (donepb (member tb org-done-keywords-for-agenda)))
6979 (cond ((and donepa (not donepb)) -1)
6980 ((and (not donepa) donepb) +1)
6981 ((< la lb) -1)
6982 ((< lb la) +1))))
6984 (defsubst org-cmp-alpha (a b)
6985 "Compare the headlines, alphabetically."
6986 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6987 (plb (text-property-any 0 (length b) 'org-heading t b))
6988 (ta (and pla (substring a pla)))
6989 (tb (and plb (substring b plb))))
6990 (when pla
6991 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6992 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6993 (setq ta (substring ta (match-end 0))))
6994 (setq ta (downcase ta)))
6995 (when plb
6996 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6997 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6998 (setq tb (substring tb (match-end 0))))
6999 (setq tb (downcase tb)))
7000 (cond ((not ta) +1)
7001 ((not tb) -1)
7002 ((string-lessp ta tb) -1)
7003 ((string-lessp tb ta) +1))))
7005 (defsubst org-cmp-tag (a b)
7006 "Compare the string values of the first tags of A and B."
7007 (let ((ta (car (last (get-text-property 1 'tags a))))
7008 (tb (car (last (get-text-property 1 'tags b)))))
7009 (cond ((not ta) +1)
7010 ((not tb) -1)
7011 ((string-lessp ta tb) -1)
7012 ((string-lessp tb ta) +1))))
7014 (defsubst org-cmp-time (a b)
7015 "Compare the time-of-day values of strings A and B."
7016 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
7017 (ta (or (get-text-property 1 'time-of-day a) def))
7018 (tb (or (get-text-property 1 'time-of-day b) def)))
7019 (cond ((< ta tb) -1)
7020 ((< tb ta) +1))))
7022 (defsubst org-cmp-ts (a b &optional type)
7023 "Compare the timestamps values of entries A and B.
7024 When TYPE is \"scheduled\", \"deadline\", \"timestamp\" or
7025 \"timestamp_ia\", compare within each of these type. When TYPE
7026 is the empty string, compare all timestamps without respect of
7027 their type."
7028 (let* ((def (if org-sort-agenda-notime-is-late most-positive-fixnum -1))
7029 (ta (or (and (string-match type (or (get-text-property 1 'type a) ""))
7030 (get-text-property 1 'ts-date a)) def))
7031 (tb (or (and (string-match type (or (get-text-property 1 'type b) ""))
7032 (get-text-property 1 'ts-date b)) def)))
7033 (cond ((< ta tb) -1)
7034 ((< tb ta) +1))))
7036 (defsubst org-cmp-habit-p (a b)
7037 "Compare the todo states of strings A and B."
7038 (let ((ha (get-text-property 1 'org-habit-p a))
7039 (hb (get-text-property 1 'org-habit-p b)))
7040 (cond ((and ha (not hb)) -1)
7041 ((and (not ha) hb) +1))))
7043 (defun org-entries-lessp (a b)
7044 "Predicate for sorting agenda entries."
7045 ;; The following variables will be used when the form is evaluated.
7046 ;; So even though the compiler complains, keep them.
7047 (let* ((ss org-agenda-sorting-strategy-selected)
7048 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
7049 (org-cmp-ts a b "")))
7050 (timestamp-down (if timestamp-up (- timestamp-up) nil))
7051 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
7052 (org-cmp-ts a b "scheduled")))
7053 (scheduled-down (if scheduled-up (- scheduled-up) nil))
7054 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
7055 (org-cmp-ts a b "deadline")))
7056 (deadline-down (if deadline-up (- deadline-up) nil))
7057 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
7058 (org-cmp-ts a b "iatimestamp_ia")))
7059 (tsia-down (if tsia-up (- tsia-up) nil))
7060 (ts-up (and (org-em 'ts-up 'ts-down ss)
7061 (org-cmp-ts a b "timestamp")))
7062 (ts-down (if ts-up (- ts-up) nil))
7063 (time-up (and (org-em 'time-up 'time-down ss)
7064 (org-cmp-time a b)))
7065 (time-down (if time-up (- time-up) nil))
7066 (priority-up (and (org-em 'priority-up 'priority-down ss)
7067 (org-cmp-priority a b)))
7068 (priority-down (if priority-up (- priority-up) nil))
7069 (effort-up (and (org-em 'effort-up 'effort-down ss)
7070 (org-cmp-effort a b)))
7071 (effort-down (if effort-up (- effort-up) nil))
7072 (category-up (and (or (org-em 'category-up 'category-down ss)
7073 (memq 'category-keep ss))
7074 (org-cmp-category a b)))
7075 (category-down (if category-up (- category-up) nil))
7076 (category-keep (if category-up +1 nil))
7077 (tag-up (and (org-em 'tag-up 'tag-down ss)
7078 (org-cmp-tag a b)))
7079 (tag-down (if tag-up (- tag-up) nil))
7080 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
7081 (org-cmp-todo-state a b)))
7082 (todo-state-down (if todo-state-up (- todo-state-up) nil))
7083 (habit-up (and (org-em 'habit-up 'habit-down ss)
7084 (org-cmp-habit-p a b)))
7085 (habit-down (if habit-up (- habit-up) nil))
7086 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
7087 (org-cmp-alpha a b)))
7088 (alpha-down (if alpha-up (- alpha-up) nil))
7089 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
7090 user-defined-up user-defined-down)
7091 (if (and need-user-cmp org-agenda-cmp-user-defined
7092 (functionp org-agenda-cmp-user-defined))
7093 (setq user-defined-up
7094 (funcall org-agenda-cmp-user-defined a b)
7095 user-defined-down (if user-defined-up (- user-defined-up) nil)))
7096 (cdr (assoc
7097 (eval (cons 'or org-agenda-sorting-strategy-selected))
7098 '((-1 . t) (1 . nil) (nil . nil))))))
7100 ;;; Agenda restriction lock
7102 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
7103 "Overlay to mark the headline to which agenda commands are restricted.")
7104 (overlay-put org-agenda-restriction-lock-overlay
7105 'face 'org-agenda-restriction-lock)
7106 (overlay-put org-agenda-restriction-lock-overlay
7107 'help-echo "Agendas are currently limited to this subtree.")
7108 (org-detach-overlay org-agenda-restriction-lock-overlay)
7110 ;;;###autoload
7111 (defun org-agenda-set-restriction-lock (&optional type)
7112 "Set restriction lock for agenda, to current subtree or file.
7113 Restriction will be the file if TYPE is `file', or if type is the
7114 universal prefix '(4), or if the cursor is before the first headline
7115 in the file. Otherwise, restriction will be to the current subtree."
7116 (interactive "P")
7117 (and (equal type '(4)) (setq type 'file))
7118 (setq type (cond
7119 (type type)
7120 ((org-at-heading-p) 'subtree)
7121 ((condition-case nil (org-back-to-heading t) (error nil))
7122 'subtree)
7123 (t 'file)))
7124 (if (eq type 'subtree)
7125 (progn
7126 (setq org-agenda-restrict (current-buffer))
7127 (setq org-agenda-overriding-restriction 'subtree)
7128 (put 'org-agenda-files 'org-restrict
7129 (list (buffer-file-name (buffer-base-buffer))))
7130 (org-back-to-heading t)
7131 (move-overlay org-agenda-restriction-lock-overlay
7132 (point)
7133 (if org-agenda-restriction-lock-highlight-subtree
7134 (save-excursion (org-end-of-subtree t t) (point))
7135 (point-at-eol)))
7136 (move-marker org-agenda-restrict-begin (point))
7137 (move-marker org-agenda-restrict-end
7138 (save-excursion (org-end-of-subtree t t)))
7139 (message "Locking agenda restriction to subtree"))
7140 (put 'org-agenda-files 'org-restrict
7141 (list (buffer-file-name (buffer-base-buffer))))
7142 (setq org-agenda-restrict nil)
7143 (setq org-agenda-overriding-restriction 'file)
7144 (move-marker org-agenda-restrict-begin nil)
7145 (move-marker org-agenda-restrict-end nil)
7146 (message "Locking agenda restriction to file"))
7147 (setq current-prefix-arg nil)
7148 (org-agenda-maybe-redo))
7150 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7151 "Remove the agenda restriction lock."
7152 (interactive "P")
7153 (org-detach-overlay org-agenda-restriction-lock-overlay)
7154 (org-detach-overlay org-speedbar-restriction-lock-overlay)
7155 (setq org-agenda-overriding-restriction nil)
7156 (setq org-agenda-restrict nil)
7157 (put 'org-agenda-files 'org-restrict nil)
7158 (move-marker org-agenda-restrict-begin nil)
7159 (move-marker org-agenda-restrict-end nil)
7160 (setq current-prefix-arg nil)
7161 (message "Agenda restriction lock removed")
7162 (or noupdate (org-agenda-maybe-redo)))
7164 (defun org-agenda-maybe-redo ()
7165 "If there is any window showing the agenda view, update it."
7166 (let ((w (get-buffer-window org-agenda-buffer-name t))
7167 (w0 (selected-window)))
7168 (when w
7169 (select-window w)
7170 (org-agenda-redo)
7171 (select-window w0)
7172 (if org-agenda-overriding-restriction
7173 (message "Agenda view shifted to new %s restriction"
7174 org-agenda-overriding-restriction)
7175 (message "Agenda restriction lock removed")))))
7177 ;;; Agenda commands
7179 (defun org-agenda-check-type (error &rest types)
7180 "Check if agenda buffer is of allowed type.
7181 If ERROR is non-nil, throw an error, otherwise just return nil.
7182 Allowed types are 'agenda 'timeline 'todo 'tags 'search."
7183 (if (not org-agenda-type)
7184 (error "No Org agenda currently displayed")
7185 (if (memq org-agenda-type types)
7187 (if error
7188 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
7189 nil))))
7191 (defun org-agenda-Quit ()
7192 "Exit the agenda and kill buffers loaded by `org-agenda'.
7193 Also restore the window configuration."
7194 (interactive)
7195 (if org-agenda-columns-active
7196 (org-columns-quit)
7197 (let ((buf (current-buffer)))
7198 (if (eq org-agenda-window-setup 'other-frame)
7199 (progn
7200 (org-agenda-reset-markers)
7201 (kill-buffer buf)
7202 (org-columns-remove-overlays)
7203 (setq org-agenda-archives-mode nil)
7204 (delete-frame))
7205 (and (not (eq org-agenda-window-setup 'current-window))
7206 (not (one-window-p))
7207 (delete-window))
7208 (org-agenda-reset-markers)
7209 (kill-buffer buf)
7210 (org-columns-remove-overlays)
7211 (setq org-agenda-archives-mode nil)))
7212 (setq org-agenda-buffer nil)
7213 ;; Maybe restore the pre-agenda window configuration.
7214 (and org-agenda-restore-windows-after-quit
7215 (not (eq org-agenda-window-setup 'other-frame))
7216 org-agenda-pre-window-conf
7217 (set-window-configuration org-agenda-pre-window-conf)
7218 (setq org-agenda-pre-window-conf nil))))
7220 (defun org-agenda-quit ()
7221 "Exit the agenda and restore the window configuration.
7222 When `org-agenda-sticky' is non-nil, only bury the agenda."
7223 (interactive)
7224 (if (and (eq org-indirect-buffer-display 'other-window)
7225 org-last-indirect-buffer)
7226 (let ((org-last-indirect-window
7227 (get-buffer-window org-last-indirect-buffer)))
7228 (if org-last-indirect-window
7229 (delete-window org-last-indirect-window))))
7230 (if org-agenda-columns-active
7231 (org-columns-quit)
7232 (if org-agenda-sticky
7233 (let ((buf (current-buffer)))
7234 (if (eq org-agenda-window-setup 'other-frame)
7235 (progn
7236 (delete-frame))
7237 (and (not (eq org-agenda-window-setup 'current-window))
7238 (not (one-window-p))
7239 (delete-window)))
7240 (with-current-buffer buf
7241 (bury-buffer)
7242 ;; Maybe restore the pre-agenda window configuration.
7243 (and org-agenda-restore-windows-after-quit
7244 (not (eq org-agenda-window-setup 'other-frame))
7245 org-agenda-pre-window-conf
7246 (set-window-configuration org-agenda-pre-window-conf)
7247 (setq org-agenda-pre-window-conf nil))))
7248 (org-agenda-Quit))))
7250 (defun org-agenda-exit ()
7251 "Exit the agenda and restore the window configuration.
7252 Also kill Org-mode buffers loaded by `org-agenda'. Org-mode
7253 buffers visited directly by the user will not be touched."
7254 (interactive)
7255 (org-release-buffers org-agenda-new-buffers)
7256 (setq org-agenda-new-buffers nil)
7257 (org-agenda-Quit))
7259 (defun org-agenda-kill-all-agenda-buffers ()
7260 "Kill all buffers in `org-agenda-mode'.
7261 This is used when toggling sticky agendas.
7262 You can also explicitly invoke it with `C-c a C-k'."
7263 (interactive)
7264 (let (blist)
7265 (dolist (buf (buffer-list))
7266 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7267 (push buf blist)))
7268 (mapc 'kill-buffer blist)))
7270 (defun org-agenda-execute (arg)
7271 "Execute another agenda command, keeping same window.
7272 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7273 in the agenda."
7274 (interactive "P")
7275 (let ((org-agenda-window-setup 'current-window))
7276 (org-agenda arg)))
7278 (defun org-agenda-redo (&optional all)
7279 "Rebuild possibly ALL agenda view(s) in the current buffer."
7280 (interactive "P")
7281 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7282 (cpa (unless (eq all t) current-prefix-arg))
7283 (org-agenda-doing-sticky-redo org-agenda-sticky)
7284 (org-agenda-sticky nil)
7285 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7286 org-agenda-buffer-name))
7287 (org-agenda-keep-modes t)
7288 (tag-filter org-agenda-tag-filter)
7289 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7290 (top-hl-filter org-agenda-top-headline-filter)
7291 (cat-filter org-agenda-category-filter)
7292 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7293 (re-filter org-agenda-regexp-filter)
7294 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7295 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7296 (cols org-agenda-columns-active)
7297 (line (org-current-line))
7298 (window-line (- line (org-current-line (window-start))))
7299 (lprops (get 'org-agenda-redo-command 'org-lprops))
7300 (redo-cmd (get-text-property p 'org-redo-cmd))
7301 (last-args (get-text-property p 'org-last-args))
7302 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7303 (org-agenda-overriding-cmd-arguments
7304 (unless (eq all t)
7305 (cond ((listp last-args)
7306 (cons (or cpa (car last-args)) (cdr last-args)))
7307 ((stringp last-args)
7308 last-args))))
7309 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7310 (put 'org-agenda-tag-filter :preset-filter nil)
7311 (put 'org-agenda-category-filter :preset-filter nil)
7312 (put 'org-agenda-regexp-filter :preset-filter nil)
7313 (and cols (org-columns-quit))
7314 (message "Rebuilding agenda buffer...")
7315 (if series-redo-cmd
7316 (eval series-redo-cmd)
7317 (org-let lprops redo-cmd))
7318 (setq org-agenda-undo-list nil
7319 org-agenda-pending-undo-list nil
7320 org-agenda-tag-filter tag-filter
7321 org-agenda-category-filter cat-filter
7322 org-agenda-regexp-filter re-filter
7323 org-agenda-top-headline-filter top-hl-filter)
7324 (message "Rebuilding agenda buffer...done")
7325 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7326 (put 'org-agenda-category-filter :preset-filter cat-preset)
7327 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7328 (and (or tag-filter tag-preset) (org-agenda-filter-apply tag-filter 'tag))
7329 (and (or cat-filter cat-preset) (org-agenda-filter-apply cat-filter 'category))
7330 (and (or re-filter re-preset) (org-agenda-filter-apply re-filter 'regexp))
7331 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7332 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
7333 (org-goto-line line)
7334 (recenter window-line)))
7336 (defvar org-global-tags-completion-table nil)
7337 (defvar org-agenda-filter-form nil)
7338 (defvar org-agenda-filtered-by-category nil)
7340 (defun org-agenda-filter-by-category (strip)
7341 "Keep only those lines in the agenda buffer that have a specific category.
7342 The category is that of the current line."
7343 (interactive "P")
7344 (if (and org-agenda-filtered-by-category
7345 org-agenda-category-filter)
7346 (org-agenda-filter-show-all-cat)
7347 (let ((cat (org-no-properties (get-text-property (point) 'org-category))))
7348 (cond
7349 ((and cat strip)
7350 (org-agenda-filter-apply
7351 (push (concat "-" cat) org-agenda-category-filter) 'category))
7352 ((and cat)
7353 (org-agenda-filter-apply
7354 (setq org-agenda-category-filter
7355 (list (concat "+" cat))) 'category))
7356 ((error "No category at point"))))))
7358 (defun org-find-top-headline (&optional pos)
7359 "Find the topmost parent headline and return it."
7360 (save-excursion
7361 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
7362 (if pos (goto-char pos))
7363 ;; Skip up to the topmost parent
7364 (while (ignore-errors (outline-up-heading 1) t))
7365 (ignore-errors
7366 (nth 4 (org-heading-components))))))
7368 (defvar org-agenda-filtered-by-top-headline nil)
7369 (defun org-agenda-filter-by-top-headline (strip)
7370 "Keep only those lines that are descendants from the same top headline.
7371 The top headline is that of the current line."
7372 (interactive "P")
7373 (if org-agenda-filtered-by-top-headline
7374 (progn
7375 (setq org-agenda-filtered-by-top-headline nil
7376 org-agenda-top-headline-filter nil)
7377 (org-agenda-filter-show-all-cat))
7378 (let ((cat (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7379 (if cat (org-agenda-filter-top-headline-apply cat strip)
7380 (error "No top-level category at point")))))
7382 (defvar org-agenda-regexp-filter nil)
7383 (defun org-agenda-filter-by-regexp (strip)
7384 "Filter agenda entries by a regular expression.
7385 Regexp filters are cumulative.
7386 With no prefix argument, keep entries matching the regexp.
7387 With one prefix argument, filter out entries matching the regexp.
7388 With two prefix arguments, remove the regexp filters."
7389 (interactive "P")
7390 (if (not (equal strip '(16)))
7391 (let ((flt (concat (if (equal strip '(4)) "-" "+")
7392 (read-from-minibuffer
7393 (if (equal strip '(4))
7394 "Filter out entries matching regexp: "
7395 "Narrow to entries matching regexp: ")))))
7396 (push flt org-agenda-regexp-filter)
7397 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
7398 (org-agenda-filter-show-all-re)
7399 (message "Regexp filter removed")))
7401 (defun org-agenda-filter-remove-all ()
7402 "Remove all filters from the current agenda buffer."
7403 (interactive)
7404 (when org-agenda-tag-filter
7405 (org-agenda-filter-show-all-tag))
7406 (when org-agenda-category-filter
7407 (org-agenda-filter-show-all-cat))
7408 (when org-agenda-regexp-filter
7409 (org-agenda-filter-show-all-re)))
7411 (defun org-agenda-filter-by-tag (strip &optional char narrow)
7412 "Keep only those lines in the agenda buffer that have a specific tag.
7413 The tag is selected with its fast selection letter, as configured.
7414 With prefix argument STRIP, remove all lines that do have the tag.
7415 A lisp caller can specify CHAR. NARROW means that the new tag should be
7416 used to narrow the search - the interactive user can also press `-' or `+'
7417 to switch to narrowing."
7418 (interactive "P")
7419 (let* ((alist org-tag-alist-for-agenda)
7420 (tag-chars (mapconcat
7421 (lambda (x) (if (and (not (symbolp (car x)))
7422 (cdr x))
7423 (char-to-string (cdr x))
7424 ""))
7425 alist ""))
7426 (efforts (org-split-string
7427 (or (cdr (assoc (concat org-effort-property "_ALL")
7428 org-global-properties))
7429 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
7430 "")))
7431 (effort-op org-agenda-filter-effort-default-operator)
7432 (effort-prompt "")
7433 (inhibit-read-only t)
7434 (current org-agenda-tag-filter)
7435 maybe-refresh a n tag)
7436 (unless char
7437 (message
7438 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
7439 (if narrow "Narrow" "Filter") tag-chars
7440 (if org-agenda-auto-exclude-function "[RET], " ""))
7441 (setq char (read-char-exclusive)))
7442 (when (member char '(?+ ?-))
7443 ;; Narrowing down
7444 (cond ((equal char ?-) (setq strip t narrow t))
7445 ((equal char ?+) (setq strip nil narrow t)))
7446 (message
7447 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
7448 (setq char (read-char-exclusive)))
7449 (when (member char '(?< ?> ?= ??))
7450 ;; An effort operator
7451 (setq effort-op (char-to-string char))
7452 (setq alist nil) ; to make sure it will be interpreted as effort.
7453 (unless (equal char ??)
7454 (loop for i from 0 to 9 do
7455 (setq effort-prompt
7456 (concat
7457 effort-prompt " ["
7458 (if (= i 9) "0" (int-to-string (1+ i)))
7459 "]" (nth i efforts))))
7460 (message "Effort%s: %s " effort-op effort-prompt)
7461 (setq char (read-char-exclusive))
7462 (when (or (< char ?0) (> char ?9))
7463 (error "Need 1-9,0 to select effort"))))
7464 (when (equal char ?\t)
7465 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7466 (org-set-local 'org-global-tags-completion-table
7467 (org-global-tags-completion-table)))
7468 (let ((completion-ignore-case t))
7469 (setq tag (org-icompleting-read
7470 "Tag: " org-global-tags-completion-table))))
7471 (cond
7472 ((equal char ?\r)
7473 (org-agenda-filter-show-all-tag)
7474 (when org-agenda-auto-exclude-function
7475 (setq org-agenda-tag-filter nil)
7476 (dolist (tag (org-agenda-get-represented-tags))
7477 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7478 (if modifier
7479 (push modifier org-agenda-tag-filter))))
7480 (if (not (null org-agenda-tag-filter))
7481 (org-agenda-filter-apply org-agenda-tag-filter 'tag)))
7482 (setq maybe-refresh t))
7483 ((equal char ?/)
7484 (org-agenda-filter-show-all-tag)
7485 (when (get 'org-agenda-tag-filter :preset-filter)
7486 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
7487 (setq maybe-refresh t))
7488 ((equal char ?. )
7489 (setq org-agenda-tag-filter
7490 (mapcar (lambda(tag) (concat "+" tag))
7491 (org-get-at-bol 'tags)))
7492 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
7493 (setq maybe-refresh t))
7494 ((or (equal char ?\ )
7495 (setq a (rassoc char alist))
7496 (and (>= char ?0) (<= char ?9)
7497 (setq n (if (= char ?0) 9 (- char ?0 1))
7498 tag (concat effort-op (nth n efforts))
7499 a (cons tag nil)))
7500 (and (= char ??)
7501 (setq tag "?eff")
7502 a (cons tag nil))
7503 (and tag (setq a (cons tag nil))))
7504 (org-agenda-filter-show-all-tag)
7505 (setq tag (car a))
7506 (setq org-agenda-tag-filter
7507 (cons (concat (if strip "-" "+") tag)
7508 (if narrow current nil)))
7509 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
7510 (setq maybe-refresh t))
7511 (t (error "Invalid tag selection character %c" char)))
7512 (when (and maybe-refresh
7513 (eq org-agenda-clockreport-mode 'with-filter))
7514 (org-agenda-redo))))
7516 (defun org-agenda-get-represented-tags ()
7517 "Get a list of all tags currently represented in the agenda."
7518 (let (p tags)
7519 (save-excursion
7520 (goto-char (point-min))
7521 (while (setq p (next-single-property-change (point) 'tags))
7522 (goto-char p)
7523 (mapc (lambda (x) (add-to-list 'tags x))
7524 (get-text-property (point) 'tags))))
7525 tags))
7527 (defun org-agenda-filter-by-tag-refine (strip &optional char)
7528 "Refine the current filter. See `org-agenda-filter-by-tag'."
7529 (interactive "P")
7530 (org-agenda-filter-by-tag strip char 'refine))
7532 (defun org-agenda-filter-make-matcher (filter type)
7533 "Create the form that tests a line for agenda filter."
7534 (let (f f1)
7535 (cond
7536 ;; Tag filter
7537 ((eq type 'tag)
7538 (setq filter
7539 (delete-dups
7540 (append (get 'org-agenda-tag-filter :preset-filter)
7541 filter)))
7542 (dolist (x filter)
7543 (let ((nfilter (org-agenda-filter-expand-tags filter)) nf nf1
7544 (ffunc
7545 (lambda (nf0 nf01 fltr notgroup op)
7546 (dolist (x fltr)
7547 (if (member x '("-" "+"))
7548 (setq nf01 (if (equal x "-") 'tags '(not tags)))
7549 (if (string-match "[<=>?]" x)
7550 (setq nf01 (org-agenda-filter-effort-form x))
7551 (setq nf01 (list 'member (downcase (substring x 1))
7552 'tags)))
7553 (when (equal (string-to-char x) ?-)
7554 (setq nf01 (list 'not nf01))
7555 (when (not notgroup) (setq op 'and))))
7556 (push nf01 nf0))
7557 (if notgroup
7558 (push (cons 'and nf0) f)
7559 (push (cons (or op 'or) nf0) f)))))
7560 (cond ((equal filter '("+"))
7561 (setq f (list (list 'not 'tags))))
7562 ((equal nfilter filter)
7563 (funcall ffunc f1 f filter t nil))
7564 (t (funcall ffunc nf1 nf nfilter nil nil))))))
7565 ;; Category filter
7566 ((eq type 'category)
7567 (setq filter
7568 (delete-dups
7569 (append (get 'org-agenda-category-filter :preset-filter)
7570 filter)))
7571 (dolist (x filter)
7572 (if (equal "-" (substring x 0 1))
7573 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7574 (setq f1 (list 'equal (substring x 1) 'cat)))
7575 (push f1 f)))
7576 ;; Regexp filter
7577 ((eq type 'regexp)
7578 (setq filter
7579 (delete-dups
7580 (append (get 'org-agenda-regexp-filter :preset-filter)
7581 filter)))
7582 (dolist (x filter)
7583 (if (equal "-" (substring x 0 1))
7584 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7585 (setq f1 (list 'string-match (substring x 1) 'txt)))
7586 (push f1 f))))
7587 (cons 'and (nreverse f))))
7589 (defun org-agenda-filter-effort-form (e)
7590 "Return the form to compare the effort of the current line with what E says.
7591 E looks like \"+<2:25\"."
7592 (let (op)
7593 (setq e (substring e 1))
7594 (setq op (string-to-char e) e (substring e 1))
7595 (setq op (cond ((equal op ?<) '<=)
7596 ((equal op ?>) '>=)
7597 ((equal op ??) op)
7598 (t '=)))
7599 (list 'org-agenda-compare-effort (list 'quote op)
7600 (org-duration-string-to-minutes e))))
7602 (defun org-agenda-compare-effort (op value)
7603 "Compare the effort of the current line with VALUE, using OP.
7604 If the line does not have an effort defined, return nil."
7605 (let ((eff (org-get-at-bol 'effort-minutes)))
7606 (if (equal op ??)
7607 (not eff)
7608 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
7609 value))))
7611 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7612 "Expand group tags in FILTER for the agenda.
7613 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7614 (if org-group-tags
7615 (let ((case-fold-search t) rtn)
7616 (mapc
7617 (lambda (f)
7618 (let (f0 dir)
7619 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7620 (setq dir (match-string 1 f) f0 (match-string 2 f))
7621 (setq dir (if no-operator "" "+") f0 f))
7622 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7623 (org-tags-expand f0 t t))
7624 rtn))))
7625 filter)
7626 (reverse rtn))
7627 filter))
7629 (defun org-agenda-filter-apply (filter type)
7630 "Set FILTER as the new agenda filter and apply it."
7631 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7632 (if org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7633 (let (tags cat txt)
7634 (setq org-agenda-filter-form
7635 (org-agenda-filter-make-matcher filter type))
7636 (if (and (eq type 'category)
7637 (not (equal (substring (car filter) 0 1) "-")))
7638 ;; Only set `org-agenda-filtered-by-category' to t
7639 ;; when a unique category is used as the filter
7640 (setq org-agenda-filtered-by-category t))
7641 (org-agenda-set-mode-name)
7642 (save-excursion
7643 (goto-char (point-min))
7644 (while (not (eobp))
7645 (if (org-get-at-bol 'org-marker)
7646 (progn
7647 (setq tags ; used in eval
7648 (apply 'append
7649 (mapcar (lambda (f)
7650 (org-agenda-filter-expand-tags (list f) t))
7651 (org-get-at-bol 'tags)))
7652 cat (get-text-property (point) 'org-category)
7653 txt (get-text-property (point) 'txt))
7654 (if (not (eval org-agenda-filter-form))
7655 (org-agenda-filter-hide-line type))
7656 (beginning-of-line 2))
7657 (beginning-of-line 2))))
7658 (if (get-char-property (point) 'invisible)
7659 (ignore-errors (org-agenda-previous-line)))))
7661 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7662 "Filter by top headline HL."
7663 (org-agenda-set-mode-name)
7664 (save-excursion
7665 (goto-char (point-min))
7666 (while (not (eobp))
7667 (let* ((pos (org-get-at-bol 'org-hd-marker))
7668 (tophl (and pos (org-find-top-headline pos))))
7669 (if (and tophl (funcall (if negative 'identity 'not)
7670 (string= hl tophl)))
7671 (org-agenda-filter-hide-line 'category)))
7672 (beginning-of-line 2)))
7673 (if (get-char-property (point) 'invisible)
7674 (org-agenda-previous-line))
7675 (setq org-agenda-top-headline-filter hl
7676 org-agenda-filtered-by-top-headline t))
7678 (defun org-agenda-filter-hide-line (type)
7679 "Hide lines with TYPE in the agenda buffer."
7680 (let* ((b (max (point-min) (1- (point-at-bol))))
7681 (e (point-at-eol))
7682 (ov (make-overlay b e)))
7683 (overlay-put ov 'invisible t)
7684 (overlay-put ov 'intangible t)
7685 (overlay-put ov 'type type)
7686 (cond ((eq type 'tag) (push ov org-agenda-tag-filter-overlays))
7687 ((eq type 'category) (push ov org-agenda-cat-filter-overlays))
7688 ((eq type 'regexp) (push ov org-agenda-re-filter-overlays)))))
7690 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
7691 (setq pos (or pos (point)))
7692 (save-excursion
7693 (dolist (ov (overlays-at pos))
7694 (when (and (overlay-get ov 'invisible)
7695 (eq (overlay-get ov 'type) 'tag))
7696 (goto-char pos)
7697 (if (< (overlay-start ov) (point-at-eol))
7698 (move-overlay ov (point-at-eol)
7699 (overlay-end ov)))))))
7701 (defun org-agenda-filter-show-all-tag nil
7702 "Remove tag filter overlays from the agenda buffer."
7703 (mapc 'delete-overlay org-agenda-tag-filter-overlays)
7704 (setq org-agenda-tag-filter-overlays nil
7705 org-agenda-tag-filter nil
7706 org-agenda-filter-form nil)
7707 (org-agenda-set-mode-name))
7709 (defun org-agenda-filter-show-all-re nil
7710 "Remove regexp filter overlays from the agenda buffer."
7711 (mapc 'delete-overlay org-agenda-re-filter-overlays)
7712 (setq org-agenda-re-filter-overlays nil
7713 org-agenda-regexp-filter nil
7714 org-agenda-filter-form nil)
7715 (org-agenda-set-mode-name))
7717 (defun org-agenda-filter-show-all-cat nil
7718 "Remove category filter overlays from the agenda buffer."
7719 (mapc 'delete-overlay org-agenda-cat-filter-overlays)
7720 (setq org-agenda-cat-filter-overlays nil
7721 org-agenda-filtered-by-category nil
7722 org-agenda-category-filter nil
7723 org-agenda-filter-form nil)
7724 (org-agenda-set-mode-name))
7726 (defun org-agenda-manipulate-query-add ()
7727 "Manipulate the query by adding a search term with positive selection.
7728 Positive selection means the term must be matched for selection of an entry."
7729 (interactive)
7730 (org-agenda-manipulate-query ?\[))
7731 (defun org-agenda-manipulate-query-subtract ()
7732 "Manipulate the query by adding a search term with negative selection.
7733 Negative selection means term must not be matched for selection of an entry."
7734 (interactive)
7735 (org-agenda-manipulate-query ?\]))
7736 (defun org-agenda-manipulate-query-add-re ()
7737 "Manipulate the query by adding a search regexp with positive selection.
7738 Positive selection means the regexp must match for selection of an entry."
7739 (interactive)
7740 (org-agenda-manipulate-query ?\{))
7741 (defun org-agenda-manipulate-query-subtract-re ()
7742 "Manipulate the query by adding a search regexp with negative selection.
7743 Negative selection means regexp must not match for selection of an entry."
7744 (interactive)
7745 (org-agenda-manipulate-query ?\}))
7746 (defun org-agenda-manipulate-query (char)
7747 (cond
7748 ((memq org-agenda-type '(timeline agenda))
7749 (let ((org-agenda-include-inactive-timestamps t))
7750 (org-agenda-redo))
7751 (message "Display now includes inactive timestamps as well"))
7752 ((eq org-agenda-type 'search)
7753 (org-add-to-string
7754 'org-agenda-query-string
7755 (if org-agenda-last-search-view-search-was-boolean
7756 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7757 (?\{ . " +{}") (?\} . " -{}"))))
7758 " "))
7759 (setq org-agenda-redo-command
7760 (list 'org-search-view
7761 (car (get-text-property (min (1- (point-max)) (point))
7762 'org-last-args))
7763 org-agenda-query-string
7764 (+ (length org-agenda-query-string)
7765 (if (member char '(?\{ ?\})) 0 1))))
7766 (set-register org-agenda-query-register org-agenda-query-string)
7767 (let ((org-agenda-overriding-arguments
7768 (cdr org-agenda-redo-command)))
7769 (org-agenda-redo)))
7770 (t (error "Cannot manipulate query for %s-type agenda buffers"
7771 org-agenda-type))))
7773 (defun org-add-to-string (var string)
7774 (set var (concat (symbol-value var) string)))
7776 (defun org-agenda-goto-date (span)
7777 "Jump to DATE in agenda."
7778 (interactive "P")
7779 (let* ((org-read-date-prefer-future
7780 (eval org-agenda-jump-prefer-future))
7781 (date (org-read-date))
7782 (day (time-to-days (org-time-string-to-time date)))
7783 (org-agenda-sticky-orig org-agenda-sticky)
7784 (org-agenda-buffer-tmp-name (buffer-name))
7785 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7786 (0-arg (or current-prefix-arg (car args)))
7787 (2-arg (nth 2 args))
7788 (with-hour-p (nth 4 org-agenda-redo-command))
7789 (newcmd (list 'org-agenda-list 0-arg date
7790 (org-agenda-span-to-ndays
7791 2-arg (org-time-string-to-absolute date))
7792 with-hour-p))
7793 (newargs (cdr newcmd))
7794 (inhibit-read-only t)
7795 org-agenda-sticky)
7796 (if (not (org-agenda-check-type t 'agenda))
7797 (error "Not available in non-agenda views")
7798 (add-text-properties (point-min) (point-max)
7799 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7800 (org-agenda-redo)
7801 (goto-char (point-min))
7802 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7803 (save-excursion (move-beginning-of-line 2) (eobp))))
7804 (move-beginning-of-line 2))
7805 (setq org-agenda-sticky org-agenda-sticky-orig
7806 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7808 (defun org-agenda-goto-today ()
7809 "Go to today."
7810 (interactive)
7811 (org-agenda-check-type t 'timeline 'agenda)
7812 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7813 (curspan (nth 2 args))
7814 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7815 (cond
7816 (tdpos (goto-char tdpos))
7817 ((eq org-agenda-type 'agenda)
7818 (let* ((sd (org-agenda-compute-starting-span
7819 (org-today) (or curspan org-agenda-ndays org-agenda-span)))
7820 (org-agenda-overriding-arguments args))
7821 (setf (nth 1 org-agenda-overriding-arguments) sd)
7822 (org-agenda-redo)
7823 (org-agenda-find-same-or-today-or-agenda)))
7824 (t (error "Cannot find today")))))
7826 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7827 (goto-char
7828 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7829 (text-property-any (point-min) (point-max) 'org-today t)
7830 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7831 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7832 (org-agenda-backward-block))
7833 (point-min))))
7835 (defun org-agenda-backward-block ()
7836 "Move backward by one agenda block."
7837 (interactive)
7838 (org-agenda-forward-block 'backward))
7840 (defun org-agenda-forward-block (&optional backward)
7841 "Move forward by one agenda block.
7842 When optional argument BACKWARD is set, go backward"
7843 (interactive)
7844 (cond ((not (derived-mode-p 'org-agenda-mode))
7845 (user-error
7846 "Cannot execute this command outside of org-agenda-mode buffers"))
7847 ((looking-at (if backward "\\`" "\\'"))
7848 (message "Already at the %s block" (if backward "first" "last")))
7849 (t (let ((pos (prog1 (point)
7850 (ignore-errors (if backward (backward-char 1)
7851 (move-end-of-line 1)))))
7852 (f (if backward
7853 'previous-single-property-change
7854 'next-single-property-change))
7855 moved dest)
7856 (while (and (setq dest (funcall
7857 f (point) 'org-agenda-structural-header))
7858 (not (get-text-property
7859 (point) 'org-agenda-structural-header)))
7860 (setq moved t)
7861 (goto-char dest))
7862 (if moved (move-beginning-of-line 1)
7863 (goto-char (if backward (point-min) (point-max)))
7864 (move-beginning-of-line 1)
7865 (message "No %s block" (if backward "previous" "further")))))))
7867 (defun org-agenda-later (arg)
7868 "Go forward in time by the current span.
7869 With prefix ARG, go forward that many times the current span."
7870 (interactive "p")
7871 (org-agenda-check-type t 'agenda)
7872 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7873 (span (or (nth 2 args) org-agenda-current-span))
7874 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7875 (greg (calendar-gregorian-from-absolute sd))
7876 (cnt (org-get-at-bol 'org-day-cnt))
7877 greg2)
7878 (cond
7879 ((numberp span)
7880 (setq sd (+ (* span arg) sd)))
7881 ((eq span 'day)
7882 (setq sd (+ arg sd)))
7883 ((eq span 'week)
7884 (setq sd (+ (* 7 arg) sd)))
7885 ((eq span 'fortnight)
7886 (setq sd (+ (* 14 arg) sd)))
7887 ((eq span 'month)
7888 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7889 sd (calendar-absolute-from-gregorian greg2))
7890 (setcar greg2 (1+ (car greg2))))
7891 ((eq span 'year)
7892 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7893 sd (calendar-absolute-from-gregorian greg2))
7894 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7896 (setq sd (+ (* span arg) sd))))
7897 (let ((org-agenda-overriding-cmd
7898 ;; `cmd' may have been set by `org-agenda-run-series' which
7899 ;; uses `org-agenda-overriding-cmd' to decide whether
7900 ;; overriding is allowed for `cmd'
7901 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7902 (org-agenda-overriding-arguments
7903 (list (car args) sd span)))
7904 (org-agenda-redo)
7905 (org-agenda-find-same-or-today-or-agenda cnt))))
7907 (defun org-agenda-earlier (arg)
7908 "Go backward in time by the current span.
7909 With prefix ARG, go backward that many times the current span."
7910 (interactive "p")
7911 (org-agenda-later (- arg)))
7913 (defun org-agenda-view-mode-dispatch ()
7914 "Call one of the view mode commands."
7915 (interactive)
7916 (message "View: [d]ay [w]eek for[t]night [m]onth [y]ear [SPC]reset [q]uit/abort
7917 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7918 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7919 (let ((a (read-char-exclusive)))
7920 (case a
7921 (?\ (call-interactively 'org-agenda-reset-view))
7922 (?d (call-interactively 'org-agenda-day-view))
7923 (?w (call-interactively 'org-agenda-week-view))
7924 (?t (call-interactively 'org-agenda-fortnight-view))
7925 (?m (call-interactively 'org-agenda-month-view))
7926 (?y (call-interactively 'org-agenda-year-view))
7927 (?l (call-interactively 'org-agenda-log-mode))
7928 (?L (org-agenda-log-mode '(4)))
7929 (?c (org-agenda-log-mode 'clockcheck))
7930 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
7931 (?a (call-interactively 'org-agenda-archives-mode))
7932 (?A (org-agenda-archives-mode 'files))
7933 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7934 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7935 (?G (call-interactively 'org-agenda-toggle-time-grid))
7936 (?D (call-interactively 'org-agenda-toggle-diary))
7937 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7938 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7939 (org-agenda-check-type t 'timeline 'agenda)
7940 (org-agenda-redo))
7941 (message "Display now includes inactive timestamps as well"))
7942 (?q (message "Abort"))
7943 (otherwise (error "Invalid key" )))))
7945 (defun org-agenda-reset-view ()
7946 "Switch to default view for agenda."
7947 (interactive)
7948 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
7949 (defun org-agenda-day-view (&optional day-of-month)
7950 "Switch to daily view for agenda.
7951 With argument DAY-OF-MONTH, switch to that day of the month."
7952 (interactive "P")
7953 (org-agenda-change-time-span 'day day-of-month))
7954 (defun org-agenda-week-view (&optional iso-week)
7955 "Switch to daily view for agenda.
7956 With argument ISO-WEEK, switch to the corresponding ISO week.
7957 If ISO-WEEK has more then 2 digits, only the last two encode the
7958 week. Any digits before this encode a year. So 200712 means
7959 week 12 of year 2007. Years in the range 1938-2037 can also be
7960 written as 2-digit years."
7961 (interactive "P")
7962 (org-agenda-change-time-span 'week iso-week))
7963 (defun org-agenda-fortnight-view (&optional iso-week)
7964 "Switch to daily view for agenda.
7965 With argument ISO-WEEK, switch to the corresponding ISO week.
7966 If ISO-WEEK has more then 2 digits, only the last two encode the
7967 week. Any digits before this encode a year. So 200712 means
7968 week 12 of year 2007. Years in the range 1938-2037 can also be
7969 written as 2-digit years."
7970 (interactive "P")
7971 (org-agenda-change-time-span 'fortnight iso-week))
7972 (defun org-agenda-month-view (&optional month)
7973 "Switch to monthly view for agenda.
7974 With argument MONTH, switch to that month."
7975 (interactive "P")
7976 (org-agenda-change-time-span 'month month))
7977 (defun org-agenda-year-view (&optional year)
7978 "Switch to yearly view for agenda.
7979 With argument YEAR, switch to that year.
7980 If MONTH has more then 2 digits, only the last two encode the
7981 month. Any digits before this encode a year. So 200712 means
7982 December year 2007. Years in the range 1938-2037 can also be
7983 written as 2-digit years."
7984 (interactive "P")
7985 (when year
7986 (setq year (org-small-year-to-year year)))
7987 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
7988 (org-agenda-change-time-span 'year year)
7989 (error "Abort")))
7991 (defun org-agenda-change-time-span (span &optional n)
7992 "Change the agenda view to SPAN.
7993 SPAN may be `day', `week', `fortnight', `month', `year'."
7994 (org-agenda-check-type t 'agenda)
7995 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7996 (curspan (nth 2 args)))
7997 (if (and (not n) (equal curspan span))
7998 (error "Viewing span is already \"%s\"" span))
7999 (let* ((sd (or (org-get-at-bol 'day)
8000 (nth 1 args)
8001 org-starting-day))
8002 (sd (org-agenda-compute-starting-span sd span n))
8003 (org-agenda-overriding-cmd
8004 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
8005 (org-agenda-overriding-arguments
8006 (list (car args) sd span)))
8007 (org-agenda-redo)
8008 (org-agenda-find-same-or-today-or-agenda))
8009 (org-agenda-set-mode-name)
8010 (message "Switched to %s view" span)))
8012 (defun org-agenda-compute-starting-span (sd span &optional n)
8013 "Compute starting date for agenda.
8014 SPAN may be `day', `week', `fortnight', `month', `year'. The return value
8015 is a cons cell with the starting date and the number of days,
8016 so that the date SD will be in that range."
8017 (let* ((greg (calendar-gregorian-from-absolute sd))
8018 (dg (nth 1 greg))
8019 (mg (car greg))
8020 (yg (nth 2 greg)))
8021 (cond
8022 ((eq span 'day)
8023 (when n
8024 (setq sd (+ (calendar-absolute-from-gregorian
8025 (list mg 1 yg))
8026 n -1))))
8027 ((or (eq span 'week) (eq span 'fortnight))
8028 (let* ((nt (calendar-day-of-week
8029 (calendar-gregorian-from-absolute sd)))
8030 (d (if org-agenda-start-on-weekday
8031 (- nt org-agenda-start-on-weekday)
8034 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
8035 (when n
8036 (require 'cal-iso)
8037 (when (> n 99)
8038 (setq y1 (org-small-year-to-year (/ n 100))
8039 n (mod n 100)))
8040 (setq sd
8041 (calendar-absolute-from-iso
8042 (list n 1
8043 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
8044 ((eq span 'month)
8045 (let (y1)
8046 (when (and n (> n 99))
8047 (setq y1 (org-small-year-to-year (/ n 100))
8048 n (mod n 100)))
8049 (setq sd (calendar-absolute-from-gregorian
8050 (list (or n mg) 1 (or y1 yg))))))
8051 ((eq span 'year)
8052 (setq sd (calendar-absolute-from-gregorian
8053 (list 1 1 (or n yg))))))
8054 sd))
8056 (defun org-agenda-next-date-line (&optional arg)
8057 "Jump to the next line indicating a date in agenda buffer."
8058 (interactive "p")
8059 (org-agenda-check-type t 'agenda 'timeline)
8060 (beginning-of-line 1)
8061 ;; This does not work if user makes date format that starts with a blank
8062 (if (looking-at "^\\S-") (forward-char 1))
8063 (if (not (re-search-forward "^\\S-" nil t arg))
8064 (progn
8065 (backward-char 1)
8066 (error "No next date after this line in this buffer")))
8067 (goto-char (match-beginning 0)))
8069 (defun org-agenda-previous-date-line (&optional arg)
8070 "Jump to the previous line indicating a date in agenda buffer."
8071 (interactive "p")
8072 (org-agenda-check-type t 'agenda 'timeline)
8073 (beginning-of-line 1)
8074 (if (not (re-search-backward "^\\S-" nil t arg))
8075 (error "No previous date before this line in this buffer")))
8077 ;; Initialize the highlight
8078 (defvar org-hl (make-overlay 1 1))
8079 (overlay-put org-hl 'face 'highlight)
8081 (defun org-highlight (begin end &optional buffer)
8082 "Highlight a region with overlay."
8083 (move-overlay org-hl begin end (or buffer (current-buffer))))
8085 (defun org-unhighlight ()
8086 "Detach overlay INDEX."
8087 (org-detach-overlay org-hl))
8089 (defun org-unhighlight-once ()
8090 "Remove the highlight from its position, and this function from the hook."
8091 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8092 (org-unhighlight))
8094 (defvar org-agenda-pre-follow-window-conf nil)
8095 (defun org-agenda-follow-mode ()
8096 "Toggle follow mode in an agenda buffer."
8097 (interactive)
8098 (unless org-agenda-follow-mode
8099 (setq org-agenda-pre-follow-window-conf
8100 (current-window-configuration)))
8101 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8102 (unless org-agenda-follow-mode
8103 (set-window-configuration org-agenda-pre-follow-window-conf))
8104 (org-agenda-set-mode-name)
8105 (org-agenda-do-context-action)
8106 (message "Follow mode is %s"
8107 (if org-agenda-follow-mode "on" "off")))
8109 (defun org-agenda-entry-text-mode (&optional arg)
8110 "Toggle entry text mode in an agenda buffer."
8111 (interactive "P")
8112 (if (or org-agenda-tag-filter
8113 org-agenda-category-filter
8114 org-agenda-regexp-filter
8115 org-agenda-top-headline-filter)
8116 (user-error "Can't show entry text in filtered views")
8117 (setq org-agenda-entry-text-mode (or (integerp arg)
8118 (not org-agenda-entry-text-mode)))
8119 (org-agenda-entry-text-hide)
8120 (and org-agenda-entry-text-mode
8121 (let ((org-agenda-entry-text-maxlines
8122 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8123 (org-agenda-entry-text-show)))
8124 (org-agenda-set-mode-name)
8125 (message "Entry text mode is %s%s"
8126 (if org-agenda-entry-text-mode "on" "off")
8127 (if (not org-agenda-entry-text-mode) ""
8128 (format " (maximum number of lines is %d)"
8129 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8131 (defun org-agenda-clockreport-mode (&optional with-filter)
8132 "Toggle clocktable mode in an agenda buffer.
8133 With prefix arg WITH-FILTER, make the clocktable respect the current
8134 agenda filter."
8135 (interactive "P")
8136 (org-agenda-check-type t 'agenda)
8137 (if with-filter
8138 (setq org-agenda-clockreport-mode 'with-filter)
8139 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
8140 (org-agenda-set-mode-name)
8141 (org-agenda-redo)
8142 (message "Clocktable mode is %s"
8143 (if org-agenda-clockreport-mode "on" "off")))
8145 (defun org-agenda-log-mode (&optional special)
8146 "Toggle log mode in an agenda buffer.
8147 With argument SPECIAL, show all possible log items, not only the ones
8148 configured in `org-agenda-log-mode-items'.
8149 With a double `C-u' prefix arg, show *only* log items, nothing else."
8150 (interactive "P")
8151 (org-agenda-check-type t 'agenda 'timeline)
8152 (setq org-agenda-show-log
8153 (cond
8154 ((equal special '(16)) 'only)
8155 ((eq special 'clockcheck)
8156 (if (eq org-agenda-show-log 'clockcheck)
8157 nil 'clockcheck))
8158 (special '(closed clock state))
8159 (t (not org-agenda-show-log))))
8160 (org-agenda-set-mode-name)
8161 (org-agenda-redo)
8162 (message "Log mode is %s"
8163 (if org-agenda-show-log "on" "off")))
8165 (defun org-agenda-archives-mode (&optional with-files)
8166 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8167 When called with a prefix argument, include all archive files as well."
8168 (interactive "P")
8169 (setq org-agenda-archives-mode
8170 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8171 (org-agenda-set-mode-name)
8172 (org-agenda-redo)
8173 (message
8174 "%s"
8175 (cond
8176 ((eq org-agenda-archives-mode nil)
8177 "No archives are included")
8178 ((eq org-agenda-archives-mode 'trees)
8179 (format "Trees with :%s: tag are included" org-archive-tag))
8180 ((eq org-agenda-archives-mode t)
8181 (format "Trees with :%s: tag and all active archive files are included"
8182 org-archive-tag)))))
8184 (defun org-agenda-toggle-diary ()
8185 "Toggle diary inclusion in an agenda buffer."
8186 (interactive)
8187 (org-agenda-check-type t 'agenda)
8188 (setq org-agenda-include-diary (not org-agenda-include-diary))
8189 (org-agenda-redo)
8190 (org-agenda-set-mode-name)
8191 (message "Diary inclusion turned %s"
8192 (if org-agenda-include-diary "on" "off")))
8194 (defun org-agenda-toggle-deadlines ()
8195 "Toggle inclusion of entries with a deadline in an agenda buffer."
8196 (interactive)
8197 (org-agenda-check-type t 'agenda)
8198 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8199 (org-agenda-redo)
8200 (org-agenda-set-mode-name)
8201 (message "Deadlines inclusion turned %s"
8202 (if org-agenda-include-deadlines "on" "off")))
8204 (defun org-agenda-toggle-time-grid ()
8205 "Toggle time grid in an agenda buffer."
8206 (interactive)
8207 (org-agenda-check-type t 'agenda)
8208 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8209 (org-agenda-redo)
8210 (org-agenda-set-mode-name)
8211 (message "Time-grid turned %s"
8212 (if org-agenda-use-time-grid "on" "off")))
8214 (defun org-agenda-set-mode-name ()
8215 "Set the mode name to indicate all the small mode settings."
8216 (setq mode-name
8217 (list "Org-Agenda"
8218 (if (get 'org-agenda-files 'org-restrict) " []" "")
8220 '(:eval (org-agenda-span-name org-agenda-current-span))
8221 (if org-agenda-follow-mode " Follow" "")
8222 (if org-agenda-entry-text-mode " ETxt" "")
8223 (if org-agenda-include-diary " Diary" "")
8224 (if org-agenda-include-deadlines " Ddl" "")
8225 (if org-agenda-use-time-grid " Grid" "")
8226 (if (and (boundp 'org-habit-show-habits)
8227 org-habit-show-habits) " Habit" "")
8228 (cond
8229 ((consp org-agenda-show-log) " LogAll")
8230 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8231 (org-agenda-show-log " Log")
8232 (t ""))
8233 (if (or org-agenda-category-filter
8234 (get 'org-agenda-category-filter :preset-filter))
8235 '(:eval (org-propertize
8236 (concat " <"
8237 (mapconcat
8238 'identity
8239 (append
8240 (get 'org-agenda-category-filter :preset-filter)
8241 org-agenda-category-filter)
8243 ">")
8244 'face 'org-agenda-filter-category
8245 'help-echo "Category used in filtering")) "")
8246 (if (or org-agenda-tag-filter
8247 (get 'org-agenda-tag-filter :preset-filter))
8248 '(:eval (org-propertize
8249 (concat " {"
8250 (mapconcat
8251 'identity
8252 (append
8253 (get 'org-agenda-tag-filter :preset-filter)
8254 org-agenda-tag-filter)
8256 "}")
8257 'face 'org-agenda-filter-tags
8258 'help-echo "Tags used in filtering")) "")
8259 (if (or org-agenda-regexp-filter
8260 (get 'org-agenda-regexp-filter :preset-filter))
8261 '(:eval (org-propertize
8262 (concat " ["
8263 (mapconcat
8264 'identity
8265 (append
8266 (get 'org-agenda-regexp-filter :preset-filter)
8267 org-agenda-regexp-filter)
8269 "]")
8270 'face 'org-agenda-filter-regexp
8271 'help-echo "Regexp used in filtering")) "")
8272 (if org-agenda-archives-mode
8273 (if (eq org-agenda-archives-mode t)
8274 " Archives"
8275 (format " :%s:" org-archive-tag))
8277 (if org-agenda-clockreport-mode
8278 (if (eq org-agenda-clockreport-mode 'with-filter)
8279 " Clock{}" " Clock")
8280 "")))
8281 (force-mode-line-update))
8283 (define-obsolete-function-alias
8284 'org-agenda-post-command-hook 'org-agenda-update-agenda-type "24.3")
8286 (defun org-agenda-update-agenda-type ()
8287 "Update the agenda type after each command."
8288 (setq org-agenda-type
8289 (or (get-text-property (point) 'org-agenda-type)
8290 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8292 (defun org-agenda-next-line ()
8293 "Move cursor to the next line, and show if follow mode is active."
8294 (interactive)
8295 (call-interactively 'next-line)
8296 (org-agenda-do-context-action))
8298 (defun org-agenda-previous-line ()
8299 "Move cursor to the previous line, and show if follow-mode is active."
8300 (interactive)
8301 (call-interactively 'previous-line)
8302 (org-agenda-do-context-action))
8304 (defun org-agenda-next-item (n)
8305 "Move cursor to next agenda item."
8306 (interactive "p")
8307 (let ((col (current-column)))
8308 (dotimes (c n)
8309 (when (next-single-property-change (point-at-eol) 'org-marker)
8310 (move-end-of-line 1)
8311 (goto-char (next-single-property-change (point) 'org-marker))))
8312 (org-move-to-column col))
8313 (org-agenda-do-context-action))
8315 (defun org-agenda-previous-item (n)
8316 "Move cursor to next agenda item."
8317 (interactive "p")
8318 (dotimes (c n)
8319 (let ((col (current-column))
8320 (goto (save-excursion
8321 (move-end-of-line 0)
8322 (previous-single-property-change (point) 'org-marker))))
8323 (if goto (goto-char goto))
8324 (org-move-to-column col)))
8325 (org-agenda-do-context-action))
8327 (defun org-agenda-do-context-action ()
8328 "Show outline path and, maybe, follow mode window."
8329 (let ((m (org-get-at-bol 'org-marker)))
8330 (when (and (markerp m) (marker-buffer m))
8331 (and org-agenda-follow-mode
8332 (if org-agenda-follow-indirect
8333 (org-agenda-tree-to-indirect-buffer nil)
8334 (org-agenda-show)))
8335 (and org-agenda-show-outline-path
8336 (org-with-point-at m (org-display-outline-path t))))))
8338 (defun org-agenda-show-tags ()
8339 "Show the tags applicable to the current item."
8340 (interactive)
8341 (let* ((tags (org-get-at-bol 'tags)))
8342 (if tags
8343 (message "Tags are :%s:"
8344 (org-no-properties (mapconcat 'identity tags ":")))
8345 (message "No tags associated with this line"))))
8347 (defun org-agenda-goto (&optional highlight)
8348 "Go to the Org-mode file which contains the item at point."
8349 (interactive)
8350 (let* ((marker (or (org-get-at-bol 'org-marker)
8351 (org-agenda-error)))
8352 (buffer (marker-buffer marker))
8353 (pos (marker-position marker)))
8354 (switch-to-buffer-other-window buffer)
8355 (widen)
8356 (push-mark)
8357 (goto-char pos)
8358 (when (derived-mode-p 'org-mode)
8359 (org-show-context 'agenda)
8360 (save-excursion
8361 (and (outline-next-heading)
8362 (org-flag-heading nil)))) ; show the next heading
8363 (when (outline-invisible-p)
8364 (show-entry)) ; display invisible text
8365 (recenter (/ (window-height) 2))
8366 (run-hooks 'org-agenda-after-show-hook)
8367 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8369 (defvar org-agenda-after-show-hook nil
8370 "Normal hook run after an item has been shown from the agenda.
8371 Point is in the buffer where the item originated.")
8373 (defun org-agenda-kill ()
8374 "Kill the entry or subtree belonging to the current agenda entry."
8375 (interactive)
8376 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8377 (let* ((bufname-orig (buffer-name))
8378 (marker (or (org-get-at-bol 'org-marker)
8379 (org-agenda-error)))
8380 (buffer (marker-buffer marker))
8381 (pos (marker-position marker))
8382 (type (org-get-at-bol 'type))
8383 dbeg dend (n 0) conf)
8384 (org-with-remote-undo buffer
8385 (with-current-buffer buffer
8386 (save-excursion
8387 (goto-char pos)
8388 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8389 (setq dbeg (progn (org-back-to-heading t) (point))
8390 dend (org-end-of-subtree t t))
8391 (setq dbeg (point-at-bol)
8392 dend (min (point-max) (1+ (point-at-eol)))))
8393 (goto-char dbeg)
8394 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8395 (setq conf (or (eq t org-agenda-confirm-kill)
8396 (and (numberp org-agenda-confirm-kill)
8397 (> n org-agenda-confirm-kill))))
8398 (and conf
8399 (not (y-or-n-p
8400 (format "Delete entry with %d lines in buffer \"%s\"? "
8401 n (buffer-name buffer))))
8402 (error "Abort"))
8403 (let ((org-agenda-buffer-name bufname-orig))
8404 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8405 (with-current-buffer buffer (delete-region dbeg dend))
8406 (message "Agenda item and source killed"))))
8408 (defvar org-archive-default-command) ; defined in org-archive.el
8409 (defun org-agenda-archive-default ()
8410 "Archive the entry or subtree belonging to the current agenda entry."
8411 (interactive)
8412 (require 'org-archive)
8413 (org-agenda-archive-with org-archive-default-command))
8415 (defun org-agenda-archive-default-with-confirmation ()
8416 "Archive the entry or subtree belonging to the current agenda entry."
8417 (interactive)
8418 (require 'org-archive)
8419 (org-agenda-archive-with org-archive-default-command 'confirm))
8421 (defun org-agenda-archive ()
8422 "Archive the entry or subtree belonging to the current agenda entry."
8423 (interactive)
8424 (org-agenda-archive-with 'org-archive-subtree))
8426 (defun org-agenda-archive-to-archive-sibling ()
8427 "Move the entry to the archive sibling."
8428 (interactive)
8429 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8431 (defun org-agenda-archive-with (cmd &optional confirm)
8432 "Move the entry to the archive sibling."
8433 (interactive)
8434 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8435 (let* ((bufname-orig (buffer-name))
8436 (marker (or (org-get-at-bol 'org-marker)
8437 (org-agenda-error)))
8438 (buffer (marker-buffer marker))
8439 (pos (marker-position marker)))
8440 (org-with-remote-undo buffer
8441 (with-current-buffer buffer
8442 (if (derived-mode-p 'org-mode)
8443 (if (and confirm
8444 (not (y-or-n-p "Archive this subtree or entry? ")))
8445 (error "Abort")
8446 (save-window-excursion
8447 (goto-char pos)
8448 (let ((org-agenda-buffer-name bufname-orig))
8449 (org-remove-subtree-entries-from-agenda))
8450 (org-back-to-heading t)
8451 (funcall cmd)))
8452 (error "Archiving works only in Org-mode files"))))))
8454 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8455 "Remove all lines in the agenda that correspond to a given subtree.
8456 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8457 If this information is not given, the function uses the tree at point."
8458 (let ((buf (or buf (current-buffer))) m p)
8459 (save-excursion
8460 (unless (and beg end)
8461 (org-back-to-heading t)
8462 (setq beg (point))
8463 (org-end-of-subtree t)
8464 (setq end (point)))
8465 (set-buffer (get-buffer org-agenda-buffer-name))
8466 (save-excursion
8467 (goto-char (point-max))
8468 (beginning-of-line 1)
8469 (while (not (bobp))
8470 (when (and (setq m (org-get-at-bol 'org-marker))
8471 (equal buf (marker-buffer m))
8472 (setq p (marker-position m))
8473 (>= p beg)
8474 (< p end))
8475 (let ((inhibit-read-only t))
8476 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8477 (beginning-of-line 0))))))
8479 (defun org-agenda-refile (&optional goto rfloc no-update)
8480 "Refile the item at point.
8482 When GOTO is 0 or '(64), clear the refile cache.
8483 When GOTO is '(16), go to the location of the last refiled item.
8484 RFLOC can be a refile location obtained in a different way.
8485 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8486 (interactive "P")
8487 (cond
8488 ((member goto '(0 (64)))
8489 (org-refile-cache-clear))
8490 ((equal goto '(16))
8491 (org-refile-goto-last-stored))
8493 (let* ((buffer-orig (buffer-name))
8494 (marker (or (org-get-at-bol 'org-hd-marker)
8495 (org-agenda-error)))
8496 (buffer (marker-buffer marker))
8497 (pos (marker-position marker))
8498 (rfloc (or rfloc
8499 (org-refile-get-location
8500 (if goto "Goto" "Refile to") buffer
8501 org-refile-allow-creating-parent-nodes))))
8502 (with-current-buffer buffer
8503 (save-excursion
8504 (save-restriction
8505 (widen)
8506 (goto-char marker)
8507 (let ((org-agenda-buffer-name buffer-orig))
8508 (org-remove-subtree-entries-from-agenda))
8509 (org-refile goto buffer rfloc)))))
8510 (unless no-update (org-agenda-redo)))))
8512 (defun org-agenda-open-link (&optional arg)
8513 "Open the link(s) in the current entry, if any.
8514 This looks for a link in the displayed line in the agenda.
8515 It also looks at the text of the entry itself."
8516 (interactive "P")
8517 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8518 (org-get-at-bol 'org-marker)))
8519 (buffer (and marker (marker-buffer marker)))
8520 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8521 (lkall (and buffer (org-offer-links-in-entry
8522 buffer marker arg prefix)))
8523 (lk0 (car lkall))
8524 (lk (if (stringp lk0) (list lk0) lk0))
8525 (lkend (cdr lkall))
8526 trg)
8527 (cond
8528 ((and buffer lk)
8529 (mapcar (lambda(l)
8530 (with-current-buffer buffer
8531 (setq trg (and (string-match org-bracket-link-regexp l)
8532 (match-string 1 l)))
8533 (if (or (not trg) (string-match org-any-link-re trg))
8534 (save-excursion
8535 (save-restriction
8536 (widen)
8537 (goto-char marker)
8538 (when (search-forward l nil lkend)
8539 (goto-char (match-beginning 0))
8540 (org-open-at-point))))
8541 ;; This is an internal link, widen the buffer
8542 (switch-to-buffer-other-window buffer)
8543 (widen)
8544 (goto-char marker)
8545 (when (search-forward l nil lkend)
8546 (goto-char (match-beginning 0))
8547 (org-open-at-point)))))
8548 lk))
8549 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8550 (save-excursion
8551 (beginning-of-line 1)
8552 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8553 (org-open-link-from-string (match-string 1)))
8554 (t (message "No link to open here")))))
8556 (defun org-agenda-copy-local-variable (var)
8557 "Get a variable from a referenced buffer and install it here."
8558 (let ((m (org-get-at-bol 'org-marker)))
8559 (when (and m (buffer-live-p (marker-buffer m)))
8560 (org-set-local var (with-current-buffer (marker-buffer m)
8561 (symbol-value var))))))
8563 (defun org-agenda-switch-to (&optional delete-other-windows)
8564 "Go to the Org-mode file which contains the item at point."
8565 (interactive)
8566 (if (and org-return-follows-link
8567 (not (org-get-at-bol 'org-marker))
8568 (org-in-regexp org-bracket-link-regexp))
8569 (org-open-link-from-string (match-string 0))
8570 (let* ((marker (or (org-get-at-bol 'org-marker)
8571 (org-agenda-error)))
8572 (buffer (marker-buffer marker))
8573 (pos (marker-position marker)))
8574 (org-pop-to-buffer-same-window buffer)
8575 (and delete-other-windows (delete-other-windows))
8576 (widen)
8577 (goto-char pos)
8578 (org-back-to-heading t)
8579 (when (derived-mode-p 'org-mode)
8580 (org-show-context 'agenda)
8581 (save-excursion
8582 (and (outline-next-heading)
8583 (org-flag-heading nil))) ; show the next heading
8584 (when (outline-invisible-p)
8585 (show-entry)) ; display invisible text
8586 (run-hooks 'org-agenda-after-show-hook)))))
8588 (defun org-agenda-goto-mouse (ev)
8589 "Go to the Org-mode file which contains the item at the mouse click."
8590 (interactive "e")
8591 (mouse-set-point ev)
8592 (org-agenda-goto))
8594 (defun org-agenda-show (&optional full-entry)
8595 "Display the Org-mode file which contains the item at point.
8596 With prefix argument FULL-ENTRY, make the entire entry visible
8597 if it was hidden in the outline."
8598 (interactive "P")
8599 (let ((win (selected-window)))
8600 (if full-entry
8601 (let ((org-show-entry-below t))
8602 (org-agenda-goto t))
8603 (org-agenda-goto t))
8604 (select-window win)))
8606 (defvar org-agenda-show-window nil)
8607 (defun org-agenda-show-and-scroll-up (&optional arg)
8608 "Display the Org-mode file which contains the item at point.
8609 When called repeatedly, scroll the window that is displaying the buffer.
8610 With a \\[universal-argument] prefix, use `org-show-entry' instead of
8611 `show-subtree' to display the item, so that drawers and logbooks stay
8612 folded."
8613 (interactive "P")
8614 (let ((win (selected-window)))
8615 (if (and (window-live-p org-agenda-show-window)
8616 (eq this-command last-command))
8617 (progn
8618 (select-window org-agenda-show-window)
8619 (ignore-errors (scroll-up)))
8620 (org-agenda-goto t)
8621 (if arg (org-show-entry) (show-subtree))
8622 (setq org-agenda-show-window (selected-window)))
8623 (select-window win)))
8625 (defun org-agenda-show-scroll-down ()
8626 "Scroll down the window showing the agenda."
8627 (interactive)
8628 (let ((win (selected-window)))
8629 (when (window-live-p org-agenda-show-window)
8630 (select-window org-agenda-show-window)
8631 (ignore-errors (scroll-down))
8632 (select-window win))))
8634 (defun org-agenda-show-1 (&optional more)
8635 "Display the Org-mode file which contains the item at point.
8636 The prefix arg selects the amount of information to display:
8638 0 hide the subtree
8639 1 just show the entry according to defaults.
8640 2 show the children view
8641 3 show the subtree view
8642 4 show the entire subtree and any LOGBOOK drawers
8643 5 show the entire subtree and any drawers
8644 With prefix argument FULL-ENTRY, make the entire entry visible
8645 if it was hidden in the outline."
8646 (interactive "p")
8647 (let ((win (selected-window)))
8648 (org-agenda-goto t)
8649 (org-recenter-heading 1)
8650 (cond
8651 ((= more 0)
8652 (hide-subtree)
8653 (save-excursion
8654 (org-back-to-heading)
8655 (run-hook-with-args 'org-cycle-hook 'folded))
8656 (message "Remote: FOLDED"))
8657 ((and (org-called-interactively-p 'any) (= more 1))
8658 (message "Remote: show with default settings"))
8659 ((= more 2)
8660 (show-entry)
8661 (show-children)
8662 (save-excursion
8663 (org-back-to-heading)
8664 (run-hook-with-args 'org-cycle-hook 'children))
8665 (message "Remote: CHILDREN"))
8666 ((= more 3)
8667 (show-subtree)
8668 (save-excursion
8669 (org-back-to-heading)
8670 (run-hook-with-args 'org-cycle-hook 'subtree))
8671 (message "Remote: SUBTREE"))
8672 ((= more 4)
8673 (show-subtree)
8674 (save-excursion
8675 (org-back-to-heading)
8676 (org-cycle-hide-drawers 'subtree '("LOGBOOK")))
8677 (message "Remote: SUBTREE AND LOGBOOK"))
8678 ((> more 4)
8679 (show-subtree)
8680 (message "Remote: SUBTREE AND ALL DRAWERS")))
8681 (select-window win)))
8683 (defun org-recenter-heading (n)
8684 (save-excursion
8685 (org-back-to-heading)
8686 (recenter n)))
8688 (defvar org-agenda-cycle-counter nil)
8689 (defun org-agenda-cycle-show (&optional n)
8690 "Show the current entry in another window, with default settings.
8691 Default settings are taken from `org-show-hierarchy-above' and siblings.
8692 When use repeatedly in immediate succession, the remote entry will cycle
8693 through visibility
8695 children -> subtree -> folded
8697 When called with a numeric prefix arg, that arg will be passed through to
8698 `org-agenda-show-1'. For the interpretation of that argument, see the
8699 docstring of `org-agenda-show-1'."
8700 (interactive "P")
8701 (if (integerp n)
8702 (setq org-agenda-cycle-counter n)
8703 (if (not (eq last-command this-command))
8704 (setq org-agenda-cycle-counter 1)
8705 (if (equal org-agenda-cycle-counter 0)
8706 (setq org-agenda-cycle-counter 2)
8707 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8708 (if (> org-agenda-cycle-counter 3)
8709 (setq org-agenda-cycle-counter 0)))))
8710 (org-agenda-show-1 org-agenda-cycle-counter))
8712 (defun org-agenda-recenter (arg)
8713 "Display the Org-mode file which contains the item at point and recenter."
8714 (interactive "P")
8715 (let ((win (selected-window)))
8716 (org-agenda-goto t)
8717 (recenter arg)
8718 (select-window win)))
8720 (defun org-agenda-show-mouse (ev)
8721 "Display the Org-mode file which contains the item at the mouse click."
8722 (interactive "e")
8723 (mouse-set-point ev)
8724 (org-agenda-show))
8726 (defun org-agenda-check-no-diary ()
8727 "Check if the entry is a diary link and abort if yes."
8728 (if (org-get-at-bol 'org-agenda-diary-link)
8729 (org-agenda-error)))
8731 (defun org-agenda-error ()
8732 "Throw an error when a command is not allowed in the agenda."
8733 (user-error "Command not allowed in this line"))
8735 (defun org-agenda-tree-to-indirect-buffer (arg)
8736 "Show the subtree corresponding to the current entry in an indirect buffer.
8737 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8739 With a numerical prefix ARG, go up to this level and then take that tree.
8740 With a negative numeric ARG, go up by this number of levels.
8741 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
8742 use the dedicated frame)."
8743 (interactive "P")
8744 (if current-prefix-arg
8745 (org-agenda-do-tree-to-indirect-buffer arg)
8746 (let ((agenda-buffer (buffer-name))
8747 (agenda-window (selected-window))
8748 (indirect-window
8749 (and org-last-indirect-buffer
8750 (get-buffer-window org-last-indirect-buffer))))
8751 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8752 (unless (or (eq org-indirect-buffer-display 'new-frame)
8753 (eq org-indirect-buffer-display 'dedicated-frame))
8754 (unwind-protect
8755 (unless (and indirect-window (window-live-p indirect-window))
8756 (setq indirect-window (split-window agenda-window)))
8757 (and indirect-window (select-window indirect-window))
8758 (switch-to-buffer org-last-indirect-buffer :norecord)
8759 (fit-window-to-buffer indirect-window)))
8760 (select-window (get-buffer-window agenda-buffer)))))
8762 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8763 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8764 (org-agenda-check-no-diary)
8765 (let* ((marker (or (org-get-at-bol 'org-marker)
8766 (org-agenda-error)))
8767 (buffer (marker-buffer marker))
8768 (pos (marker-position marker)))
8769 (with-current-buffer buffer
8770 (save-excursion
8771 (goto-char pos)
8772 (funcall 'org-tree-to-indirect-buffer arg)))))
8774 (defvar org-last-heading-marker (make-marker)
8775 "Marker pointing to the headline that last changed its TODO state
8776 by a remote command from the agenda.")
8778 (defun org-agenda-todo-nextset ()
8779 "Switch TODO entry to next sequence."
8780 (interactive)
8781 (org-agenda-todo 'nextset))
8783 (defun org-agenda-todo-previousset ()
8784 "Switch TODO entry to previous sequence."
8785 (interactive)
8786 (org-agenda-todo 'previousset))
8788 (defun org-agenda-todo (&optional arg)
8789 "Cycle TODO state of line at point, also in Org-mode file.
8790 This changes the line at point, all other lines in the agenda referring to
8791 the same tree node, and the headline of the tree node in the Org-mode file."
8792 (interactive "P")
8793 (org-agenda-check-no-diary)
8794 (let* ((col (current-column))
8795 (marker (or (org-get-at-bol 'org-marker)
8796 (org-agenda-error)))
8797 (buffer (marker-buffer marker))
8798 (pos (marker-position marker))
8799 (hdmarker (org-get-at-bol 'org-hd-marker))
8800 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
8801 (inhibit-read-only t)
8802 org-agenda-headline-snapshot-before-repeat newhead just-one)
8803 (org-with-remote-undo buffer
8804 (with-current-buffer buffer
8805 (widen)
8806 (goto-char pos)
8807 (org-show-context 'agenda)
8808 (save-excursion
8809 (and (outline-next-heading)
8810 (org-flag-heading nil))) ; show the next heading
8811 (let ((current-prefix-arg arg))
8812 (call-interactively 'org-todo))
8813 (and (bolp) (forward-char 1))
8814 (setq newhead (org-get-heading))
8815 (when (and (org-bound-and-true-p
8816 org-agenda-headline-snapshot-before-repeat)
8817 (not (equal org-agenda-headline-snapshot-before-repeat
8818 newhead))
8819 todayp)
8820 (setq newhead org-agenda-headline-snapshot-before-repeat
8821 just-one t))
8822 (save-excursion
8823 (org-back-to-heading)
8824 (move-marker org-last-heading-marker (point))))
8825 (beginning-of-line 1)
8826 (save-window-excursion
8827 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8828 (when (org-bound-and-true-p org-clock-out-when-done)
8829 (string-match (concat "^" (regexp-opt org-done-keywords-for-agenda))
8830 newhead)
8831 (org-agenda-unmark-clocking-task))
8832 (org-move-to-column col))))
8834 (defun org-agenda-add-note (&optional arg)
8835 "Add a time-stamped note to the entry at point."
8836 (interactive "P")
8837 (org-agenda-check-no-diary)
8838 (let* ((marker (or (org-get-at-bol 'org-marker)
8839 (org-agenda-error)))
8840 (buffer (marker-buffer marker))
8841 (pos (marker-position marker))
8842 (hdmarker (org-get-at-bol 'org-hd-marker))
8843 (inhibit-read-only t))
8844 (with-current-buffer buffer
8845 (widen)
8846 (goto-char pos)
8847 (org-show-context 'agenda)
8848 (save-excursion
8849 (and (outline-next-heading)
8850 (org-flag-heading nil))) ; show the next heading
8851 (org-add-note))))
8853 (defun org-agenda-change-all-lines (newhead hdmarker
8854 &optional fixface just-this)
8855 "Change all lines in the agenda buffer which match HDMARKER.
8856 The new content of the line will be NEWHEAD (as modified by
8857 `org-agenda-format-item'). HDMARKER is checked with
8858 `equal' against all `org-hd-marker' text properties in the file.
8859 If FIXFACE is non-nil, the face of each item is modified according to
8860 the new TODO state.
8861 If JUST-THIS is non-nil, change just the current line, not all.
8862 If FORCE-TAGS is non nil, the car of it returns the new tags."
8863 (let* ((inhibit-read-only t)
8864 (line (org-current-line))
8865 (org-agenda-buffer (current-buffer))
8866 (thetags (with-current-buffer (marker-buffer hdmarker)
8867 (save-excursion (save-restriction (widen)
8868 (goto-char hdmarker)
8869 (org-get-tags-at)))))
8870 props m pl undone-face done-face finish new dotime level cat tags)
8871 (save-excursion
8872 (goto-char (point-max))
8873 (beginning-of-line 1)
8874 (while (not finish)
8875 (setq finish (bobp))
8876 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8877 (or (not just-this) (= (org-current-line) line))
8878 (equal m hdmarker))
8879 (setq props (text-properties-at (point))
8880 dotime (org-get-at-bol 'dotime)
8881 cat (org-get-at-bol 'org-category)
8882 level (org-get-at-bol 'level)
8883 tags thetags
8885 (let ((org-prefix-format-compiled
8886 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8887 org-prefix-format-compiled))
8888 (extra (org-get-at-bol 'extra)))
8889 (with-current-buffer (marker-buffer hdmarker)
8890 (save-excursion
8891 (save-restriction
8892 (widen)
8893 (org-agenda-format-item extra newhead level cat tags dotime)))))
8894 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8895 undone-face (org-get-at-bol 'undone-face)
8896 done-face (org-get-at-bol 'done-face))
8897 (beginning-of-line 1)
8898 (cond
8899 ((equal new "")
8900 (and (looking-at ".*\n?") (replace-match "")))
8901 ((looking-at ".*")
8902 (replace-match new t t)
8903 (beginning-of-line 1)
8904 (add-text-properties (point-at-bol) (point-at-eol) props)
8905 (when fixface
8906 (add-text-properties
8907 (point-at-bol) (point-at-eol)
8908 (list 'face
8909 (if org-last-todo-state-is-todo
8910 undone-face done-face))))
8911 (org-agenda-highlight-todo 'line)
8912 (beginning-of-line 1))
8913 (t (error "Line update did not work")))
8914 (save-restriction
8915 (narrow-to-region (point-at-bol) (point-at-eol))
8916 (org-agenda-finalize)))
8917 (beginning-of-line 0)))))
8919 (defun org-agenda-align-tags (&optional line)
8920 "Align all tags in agenda items to `org-agenda-tags-column'."
8921 (let ((inhibit-read-only t) l c)
8922 (save-excursion
8923 (goto-char (if line (point-at-bol) (point-min)))
8924 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
8925 (if line (point-at-eol) nil) t)
8926 (add-text-properties
8927 (match-beginning 2) (match-end 2)
8928 (list 'face (delq nil (let ((prop (get-text-property
8929 (match-beginning 2) 'face)))
8930 (or (listp prop) (setq prop (list prop)))
8931 (if (memq 'org-tag prop)
8932 prop
8933 (cons 'org-tag prop))))))
8934 (setq l (- (match-end 2) (match-beginning 2))
8935 c (if (< org-agenda-tags-column 0)
8936 (- (abs org-agenda-tags-column) l)
8937 org-agenda-tags-column))
8938 (delete-region (match-beginning 1) (match-end 1))
8939 (goto-char (match-beginning 1))
8940 (insert (org-add-props
8941 (make-string (max 1 (- c (current-column))) ?\ )
8942 (plist-put (copy-sequence (text-properties-at (point)))
8943 'face nil))))
8944 (goto-char (point-min))
8945 (org-font-lock-add-tag-faces (point-max)))))
8947 (defun org-agenda-priority-up ()
8948 "Increase the priority of line at point, also in Org-mode file."
8949 (interactive)
8950 (org-agenda-priority 'up))
8952 (defun org-agenda-priority-down ()
8953 "Decrease the priority of line at point, also in Org-mode file."
8954 (interactive)
8955 (org-agenda-priority 'down))
8957 (defun org-agenda-priority (&optional force-direction)
8958 "Set the priority of line at point, also in Org-mode file.
8959 This changes the line at point, all other lines in the agenda referring to
8960 the same tree node, and the headline of the tree node in the Org-mode file.
8961 Called with a universal prefix arg, show the priority instead of setting it."
8962 (interactive "P")
8963 (if (equal force-direction '(4))
8964 (org-show-priority)
8965 (unless org-enable-priority-commands
8966 (error "Priority commands are disabled"))
8967 (org-agenda-check-no-diary)
8968 (let* ((col (current-column))
8969 (marker (or (org-get-at-bol 'org-marker)
8970 (org-agenda-error)))
8971 (hdmarker (org-get-at-bol 'org-hd-marker))
8972 (buffer (marker-buffer hdmarker))
8973 (pos (marker-position hdmarker))
8974 (inhibit-read-only t)
8975 newhead)
8976 (org-with-remote-undo buffer
8977 (with-current-buffer buffer
8978 (widen)
8979 (goto-char pos)
8980 (org-show-context 'agenda)
8981 (save-excursion
8982 (and (outline-next-heading)
8983 (org-flag-heading nil))) ; show the next heading
8984 (funcall 'org-priority force-direction)
8985 (end-of-line 1)
8986 (setq newhead (org-get-heading)))
8987 (org-agenda-change-all-lines newhead hdmarker)
8988 (org-move-to-column col)))))
8990 ;; FIXME: should fix the tags property of the agenda line.
8991 (defun org-agenda-set-tags (&optional tag onoff)
8992 "Set tags for the current headline."
8993 (interactive)
8994 (org-agenda-check-no-diary)
8995 (if (and (org-region-active-p) (org-called-interactively-p 'any))
8996 (call-interactively 'org-change-tag-in-region)
8997 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8998 (org-agenda-error)))
8999 (buffer (marker-buffer hdmarker))
9000 (pos (marker-position hdmarker))
9001 (inhibit-read-only t)
9002 newhead)
9003 (org-with-remote-undo buffer
9004 (with-current-buffer buffer
9005 (widen)
9006 (goto-char pos)
9007 (save-excursion
9008 (org-show-context 'agenda))
9009 (save-excursion
9010 (and (outline-next-heading)
9011 (org-flag-heading nil))) ; show the next heading
9012 (goto-char pos)
9013 (if tag
9014 (org-toggle-tag tag onoff)
9015 (call-interactively 'org-set-tags))
9016 (end-of-line 1)
9017 (setq newhead (org-get-heading)))
9018 (org-agenda-change-all-lines newhead hdmarker)
9019 (beginning-of-line 1)))))
9021 (defun org-agenda-set-property ()
9022 "Set a property for the current headline."
9023 (interactive)
9024 (org-agenda-check-no-diary)
9025 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9026 (org-agenda-error)))
9027 (buffer (marker-buffer hdmarker))
9028 (pos (marker-position hdmarker))
9029 (inhibit-read-only t)
9030 newhead)
9031 (org-with-remote-undo buffer
9032 (with-current-buffer buffer
9033 (widen)
9034 (goto-char pos)
9035 (save-excursion
9036 (org-show-context 'agenda))
9037 (save-excursion
9038 (and (outline-next-heading)
9039 (org-flag-heading nil))) ; show the next heading
9040 (goto-char pos)
9041 (call-interactively 'org-set-property)))))
9043 (defun org-agenda-set-effort ()
9044 "Set the effort property for the current headline."
9045 (interactive)
9046 (org-agenda-check-no-diary)
9047 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9048 (org-agenda-error)))
9049 (buffer (marker-buffer hdmarker))
9050 (pos (marker-position hdmarker))
9051 (inhibit-read-only t)
9052 newhead)
9053 (org-with-remote-undo buffer
9054 (with-current-buffer buffer
9055 (widen)
9056 (goto-char pos)
9057 (save-excursion
9058 (org-show-context 'agenda))
9059 (save-excursion
9060 (and (outline-next-heading)
9061 (org-flag-heading nil))) ; show the next heading
9062 (goto-char pos)
9063 (call-interactively 'org-set-effort)
9064 (end-of-line 1)
9065 (setq newhead (org-get-heading)))
9066 (org-agenda-change-all-lines newhead hdmarker))))
9068 (defun org-agenda-toggle-archive-tag ()
9069 "Toggle the archive tag for the current entry."
9070 (interactive)
9071 (org-agenda-check-no-diary)
9072 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9073 (org-agenda-error)))
9074 (buffer (marker-buffer hdmarker))
9075 (pos (marker-position hdmarker))
9076 (inhibit-read-only t)
9077 newhead)
9078 (org-with-remote-undo buffer
9079 (with-current-buffer buffer
9080 (widen)
9081 (goto-char pos)
9082 (org-show-context 'agenda)
9083 (save-excursion
9084 (and (outline-next-heading)
9085 (org-flag-heading nil))) ; show the next heading
9086 (call-interactively 'org-toggle-archive-tag)
9087 (end-of-line 1)
9088 (setq newhead (org-get-heading)))
9089 (org-agenda-change-all-lines newhead hdmarker)
9090 (beginning-of-line 1))))
9092 (defun org-agenda-do-date-later (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-later-minutes)
9099 (org-agenda-date-later-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-later-hours)
9104 (org-agenda-date-later-hours 1))
9106 (org-agenda-date-later (prefix-numeric-value arg)))))
9108 (defun org-agenda-do-date-earlier (arg)
9109 (interactive "P")
9110 (cond
9111 ((or (equal arg '(16))
9112 (memq last-command
9113 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9114 (setq this-command 'org-agenda-date-earlier-minutes)
9115 (org-agenda-date-earlier-minutes 1))
9116 ((or (equal arg '(4))
9117 (memq last-command
9118 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9119 (setq this-command 'org-agenda-date-earlier-hours)
9120 (org-agenda-date-earlier-hours 1))
9122 (org-agenda-date-earlier (prefix-numeric-value arg)))))
9124 (defun org-agenda-date-later (arg &optional what)
9125 "Change the date of this item to ARG day(s) later."
9126 (interactive "p")
9127 (org-agenda-check-type t 'agenda 'timeline)
9128 (org-agenda-check-no-diary)
9129 (let* ((marker (or (org-get-at-bol 'org-marker)
9130 (org-agenda-error)))
9131 (buffer (marker-buffer marker))
9132 (pos (marker-position marker))
9133 cdate today)
9134 (org-with-remote-undo buffer
9135 (with-current-buffer buffer
9136 (widen)
9137 (goto-char pos)
9138 (if (not (org-at-timestamp-p))
9139 (error "Cannot find time stamp"))
9140 (when (and org-agenda-move-date-from-past-immediately-to-today
9141 (equal arg 1)
9142 (or (not what) (eq what 'day))
9143 (not (save-match-data (org-at-date-range-p))))
9144 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9145 cdate (calendar-absolute-from-gregorian
9146 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9147 today (org-today))
9148 (if (> today cdate)
9149 ;; immediately shift to today
9150 (setq arg (- today cdate))))
9151 (org-timestamp-change arg (or what 'day))
9152 (when (and (org-at-date-range-p)
9153 (re-search-backward org-tr-regexp-both (point-at-bol)))
9154 (let ((end org-last-changed-timestamp))
9155 (org-timestamp-change arg (or what 'day))
9156 (setq org-last-changed-timestamp
9157 (concat org-last-changed-timestamp "--" end)))))
9158 (org-agenda-show-new-time marker org-last-changed-timestamp))
9159 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9161 (defun org-agenda-date-earlier (arg &optional what)
9162 "Change the date of this item to ARG day(s) earlier."
9163 (interactive "p")
9164 (org-agenda-date-later (- arg) what))
9166 (defun org-agenda-date-later-minutes (arg)
9167 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9168 (interactive "p")
9169 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9170 (org-agenda-date-later arg 'minute))
9172 (defun org-agenda-date-earlier-minutes (arg)
9173 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9174 (interactive "p")
9175 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9176 (org-agenda-date-earlier arg 'minute))
9178 (defun org-agenda-date-later-hours (arg)
9179 "Change the time of this item, in hour steps."
9180 (interactive "p")
9181 (org-agenda-date-later arg 'hour))
9183 (defun org-agenda-date-earlier-hours (arg)
9184 "Change the time of this item, in hour steps."
9185 (interactive "p")
9186 (org-agenda-date-earlier arg 'hour))
9188 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9189 "Show new date stamp via text properties."
9190 ;; We use text properties to make this undoable
9191 (let ((inhibit-read-only t))
9192 (setq stamp (concat prefix " => " stamp " "))
9193 (save-excursion
9194 (goto-char (point-max))
9195 (while (not (bobp))
9196 (when (equal marker (org-get-at-bol 'org-marker))
9197 (remove-text-properties (point-at-bol) (point-at-eol) '(display))
9198 (org-move-to-column (- (window-width) (length stamp)) t)
9200 (org-agenda-fix-tags-filter-overlays-at (point))
9201 (if (featurep 'xemacs)
9202 ;; Use `duplicable' property to trigger undo recording
9203 (let ((ex (make-extent nil nil))
9204 (gl (make-glyph stamp)))
9205 (set-glyph-face gl 'secondary-selection)
9206 (set-extent-properties
9207 ex (list 'invisible t 'end-glyph gl 'duplicable t))
9208 (insert-extent ex (1- (point)) (point-at-eol)))
9209 (add-text-properties
9210 (1- (point)) (point-at-eol)
9211 (list 'display (org-add-props stamp nil
9212 'face 'secondary-selection))))
9213 (beginning-of-line 1))
9214 (beginning-of-line 0)))))
9216 (defun org-agenda-date-prompt (arg)
9217 "Change the date of this item. Date is prompted for, with default today.
9218 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9219 be used to request time specification in the time stamp."
9220 (interactive "P")
9221 (org-agenda-check-type t 'agenda 'timeline)
9222 (org-agenda-check-no-diary)
9223 (let* ((marker (or (org-get-at-bol 'org-marker)
9224 (org-agenda-error)))
9225 (buffer (marker-buffer marker))
9226 (pos (marker-position marker)))
9227 (org-with-remote-undo buffer
9228 (with-current-buffer buffer
9229 (widen)
9230 (goto-char pos)
9231 (if (not (org-at-timestamp-p t))
9232 (error "Cannot find time stamp"))
9233 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
9234 (org-agenda-show-new-time marker org-last-changed-timestamp))
9235 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9237 (defun org-agenda-schedule (arg &optional time)
9238 "Schedule the item at point.
9239 ARG is passed through to `org-schedule'."
9240 (interactive "P")
9241 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9242 (org-agenda-check-no-diary)
9243 (let* ((marker (or (org-get-at-bol 'org-marker)
9244 (org-agenda-error)))
9245 (type (marker-insertion-type marker))
9246 (buffer (marker-buffer marker))
9247 (pos (marker-position marker))
9248 (org-insert-labeled-timestamps-at-point nil)
9250 (set-marker-insertion-type marker t)
9251 (org-with-remote-undo buffer
9252 (with-current-buffer buffer
9253 (widen)
9254 (goto-char pos)
9255 (setq ts (org-schedule arg time)))
9256 (org-agenda-show-new-time marker ts " S"))
9257 (message "%s" ts)))
9259 (defun org-agenda-deadline (arg &optional time)
9260 "Schedule the item at point.
9261 ARG is passed through to `org-deadline'."
9262 (interactive "P")
9263 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9264 (org-agenda-check-no-diary)
9265 (let* ((marker (or (org-get-at-bol 'org-marker)
9266 (org-agenda-error)))
9267 (buffer (marker-buffer marker))
9268 (pos (marker-position marker))
9269 (org-insert-labeled-timestamps-at-point nil)
9271 (org-with-remote-undo buffer
9272 (with-current-buffer buffer
9273 (widen)
9274 (goto-char pos)
9275 (setq ts (org-deadline arg time)))
9276 (org-agenda-show-new-time marker ts " D"))
9277 (message "%s" ts)))
9279 (defun org-agenda-clock-in (&optional arg)
9280 "Start the clock on the currently selected item."
9281 (interactive "P")
9282 (org-agenda-check-no-diary)
9283 (if (equal arg '(4))
9284 (org-clock-in arg)
9285 (let* ((marker (or (org-get-at-bol 'org-marker)
9286 (org-agenda-error)))
9287 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9288 (pos (marker-position marker))
9289 (col (current-column))
9290 newhead)
9291 (org-with-remote-undo (marker-buffer marker)
9292 (with-current-buffer (marker-buffer marker)
9293 (widen)
9294 (goto-char pos)
9295 (org-show-context 'agenda)
9296 (org-show-entry)
9297 (org-cycle-hide-drawers 'children)
9298 (org-clock-in arg)
9299 (setq newhead (org-get-heading)))
9300 (org-agenda-change-all-lines newhead hdmarker))
9301 (org-move-to-column col))))
9303 (defun org-agenda-clock-out ()
9304 "Stop the currently running clock."
9305 (interactive)
9306 (unless (marker-buffer org-clock-marker)
9307 (error "No running clock"))
9308 (let ((marker (make-marker)) (col (current-column)) newhead)
9309 (org-with-remote-undo (marker-buffer org-clock-marker)
9310 (with-current-buffer (marker-buffer org-clock-marker)
9311 (save-excursion
9312 (save-restriction
9313 (widen)
9314 (goto-char org-clock-marker)
9315 (org-back-to-heading t)
9316 (move-marker marker (point))
9317 (org-clock-out)
9318 (setq newhead (org-get-heading))))))
9319 (org-agenda-change-all-lines newhead marker)
9320 (move-marker marker nil)
9321 (org-move-to-column col)
9322 (org-agenda-unmark-clocking-task)))
9324 (defun org-agenda-clock-cancel (&optional arg)
9325 "Cancel the currently running clock."
9326 (interactive "P")
9327 (unless (marker-buffer org-clock-marker)
9328 (user-error "No running clock"))
9329 (org-with-remote-undo (marker-buffer org-clock-marker)
9330 (org-clock-cancel)))
9332 (defun org-agenda-clock-goto ()
9333 "Jump to the currently clocked in task within the agenda.
9334 If the currently clocked in task is not listed in the agenda
9335 buffer, display it in another window."
9336 (interactive)
9337 (let (pos)
9338 (mapc (lambda (o)
9339 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
9340 (setq pos (overlay-start o))))
9341 (overlays-in (point-min) (point-max)))
9342 (cond (pos (goto-char pos))
9343 ;; If the currently clocked entry is not in the agenda
9344 ;; buffer, we visit it in another window:
9345 (org-clock-current-task
9346 (org-switch-to-buffer-other-window (org-clock-goto)))
9347 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9349 (defun org-agenda-diary-entry-in-org-file ()
9350 "Make a diary entry in the file `org-agenda-diary-file'."
9351 (let (d1 d2 char (text "") dp1 dp2)
9352 (if (equal (buffer-name) "*Calendar*")
9353 (setq d1 (calendar-cursor-to-date t)
9354 d2 (car calendar-mark-ring))
9355 (setq dp1 (get-text-property (point-at-bol) 'day))
9356 (unless dp1 (user-error "No date defined in current line"))
9357 (setq d1 (calendar-gregorian-from-absolute dp1)
9358 d2 (and (ignore-errors (mark))
9359 (save-excursion
9360 (goto-char (mark))
9361 (setq dp2 (get-text-property (point-at-bol) 'day)))
9362 (calendar-gregorian-from-absolute dp2))))
9363 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9364 (setq char (read-char-exclusive))
9365 (cond
9366 ((equal char ?d)
9367 (setq text (read-string "Day entry: "))
9368 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9369 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9370 ((equal char ?a)
9371 (setq d1 (list (car d1) (nth 1 d1)
9372 (read-number (format "Reference year [%d]: " (nth 2 d1))
9373 (nth 2 d1))))
9374 (setq text (read-string "Anniversary (use %d to show years): "))
9375 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9376 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9377 ((equal char ?b)
9378 (setq text (read-string "Block entry: "))
9379 (unless (and d1 d2 (not (equal d1 d2)))
9380 (user-error "No block of days selected"))
9381 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9382 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9383 ((equal char ?j)
9384 (org-switch-to-buffer-other-window
9385 (find-file-noselect org-agenda-diary-file))
9386 (require 'org-datetree)
9387 (org-datetree-find-date-create d1)
9388 (org-reveal t))
9389 (t (user-error "Invalid selection character `%c'" char)))))
9391 (defcustom org-agenda-insert-diary-strategy 'date-tree
9392 "Where in `org-agenda-diary-file' should new entries be added?
9393 Valid values:
9395 date-tree in the date tree, as child of the date
9396 top-level as top-level entries at the end of the file."
9397 :group 'org-agenda
9398 :type '(choice
9399 (const :tag "in a date tree" date-tree)
9400 (const :tag "as top level at end of file" top-level)))
9402 (defcustom org-agenda-insert-diary-extract-time nil
9403 "Non-nil means extract any time specification from the diary entry."
9404 :group 'org-agenda
9405 :version "24.1"
9406 :type 'boolean)
9408 (defcustom org-agenda-bulk-mark-char ">"
9409 "A single-character string to be used as the bulk mark."
9410 :group 'org-agenda
9411 :version "24.1"
9412 :type 'string)
9414 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9415 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9416 If TEXT is not empty, it will become the headline of the new entry, and
9417 the resulting entry will not be shown. When TEXT is empty, switch to
9418 `org-agenda-diary-file' and let the user finish the entry there."
9419 (let ((cw (current-window-configuration)))
9420 (org-switch-to-buffer-other-window
9421 (find-file-noselect org-agenda-diary-file))
9422 (widen)
9423 (goto-char (point-min))
9424 (cond
9425 ((eq type 'anniversary)
9426 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
9427 (progn
9428 (or (org-at-heading-p t)
9429 (progn
9430 (outline-next-heading)
9431 (insert "* Anniversaries\n\n")
9432 (beginning-of-line -1)))))
9433 (outline-next-heading)
9434 (org-back-over-empty-lines)
9435 (backward-char 1)
9436 (insert "\n")
9437 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9438 (nth 2 d1) (car d1) (nth 1 d1) text)))
9439 ((eq type 'day)
9440 (let ((org-prefix-has-time t)
9441 (org-agenda-time-leading-zero t)
9442 fmt time time2)
9443 (if org-agenda-insert-diary-extract-time
9444 ;; Use org-agenda-format-item to parse text for a time-range and
9445 ;; remove it. FIXME: This is a hack, we should refactor
9446 ;; that function to make time extraction available separately
9447 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9448 time (get-text-property 0 'time fmt)
9449 time2 (if (> (length time) 0)
9450 ;; split-string removes trailing ...... if
9451 ;; no end time given. First space
9452 ;; separates time from date.
9453 (concat " " (car (split-string time "\\.")))
9454 nil)
9455 text (get-text-property 0 'txt fmt)))
9456 (if (eq org-agenda-insert-diary-strategy 'top-level)
9457 (org-agenda-insert-diary-as-top-level text)
9458 (require 'org-datetree)
9459 (org-datetree-find-date-create d1)
9460 (org-agenda-insert-diary-make-new-entry text))
9461 (org-insert-time-stamp (org-time-from-absolute
9462 (calendar-absolute-from-gregorian d1))
9463 nil nil nil nil time2))
9464 (end-of-line 0))
9465 ((eq type 'block)
9466 (if (> (calendar-absolute-from-gregorian d1)
9467 (calendar-absolute-from-gregorian d2))
9468 (setq d1 (prog1 d2 (setq d2 d1))))
9469 (if (eq org-agenda-insert-diary-strategy 'top-level)
9470 (org-agenda-insert-diary-as-top-level text)
9471 (require 'org-datetree)
9472 (org-datetree-find-date-create d1)
9473 (org-agenda-insert-diary-make-new-entry text))
9474 (org-insert-time-stamp (org-time-from-absolute
9475 (calendar-absolute-from-gregorian d1)))
9476 (insert "--")
9477 (org-insert-time-stamp (org-time-from-absolute
9478 (calendar-absolute-from-gregorian d2)))
9479 (end-of-line 0)))
9480 (if (string-match "\\S-" text)
9481 (progn
9482 (set-window-configuration cw)
9483 (message "%s entry added to %s"
9484 (capitalize (symbol-name type))
9485 (abbreviate-file-name org-agenda-diary-file)))
9486 (org-reveal t)
9487 (message "Please finish entry here"))))
9489 (defun org-agenda-insert-diary-as-top-level (text)
9490 "Make new entry as a top-level entry at the end of the file.
9491 Add TEXT as headline, and position the cursor in the second line so that
9492 a timestamp can be added there."
9493 (widen)
9494 (goto-char (point-max))
9495 (or (bolp) (insert "\n"))
9496 (insert "* " text "\n")
9497 (if org-adapt-indentation (org-indent-to-column 2)))
9499 (defun org-agenda-insert-diary-make-new-entry (text)
9500 "Make new entry as last child of current entry.
9501 Add TEXT as headline, and position the cursor in the second line so that
9502 a timestamp can be added there."
9503 (let ((org-show-following-heading t)
9504 (org-show-siblings t)
9505 (org-show-hierarchy-above t)
9506 (org-show-entry-below t)
9507 col)
9508 (outline-next-heading)
9509 (org-back-over-empty-lines)
9510 (or (looking-at "[ \t]*$")
9511 (progn (insert "\n") (backward-char 1)))
9512 (org-insert-heading nil t)
9513 (org-do-demote)
9514 (setq col (current-column))
9515 (insert text "\n")
9516 (if org-adapt-indentation (org-indent-to-column col))
9517 (let ((org-show-following-heading t)
9518 (org-show-siblings t)
9519 (org-show-hierarchy-above t)
9520 (org-show-entry-below t))
9521 (org-show-context))))
9523 (defun org-agenda-diary-entry ()
9524 "Make a diary entry, like the `i' command from the calendar.
9525 All the standard commands work: block, weekly etc.
9526 When `org-agenda-diary-file' points to a file,
9527 `org-agenda-diary-entry-in-org-file' is called instead to create
9528 entries in that Org-mode file."
9529 (interactive)
9530 (if (not (eq org-agenda-diary-file 'diary-file))
9531 (org-agenda-diary-entry-in-org-file)
9532 (require 'diary-lib)
9533 (let* ((char (progn
9534 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
9535 (read-char-exclusive)))
9536 (cmd (cdr (assoc char
9537 '((?d . insert-diary-entry)
9538 (?w . insert-weekly-diary-entry)
9539 (?m . insert-monthly-diary-entry)
9540 (?y . insert-yearly-diary-entry)
9541 (?a . insert-anniversary-diary-entry)
9542 (?b . insert-block-diary-entry)
9543 (?c . insert-cyclic-diary-entry)))))
9544 (oldf (symbol-function 'calendar-cursor-to-date))
9545 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9546 (point (point))
9547 (mark (or (mark t) (point))))
9548 (unless cmd
9549 (user-error "No command associated with <%c>" char))
9550 (unless (and (get-text-property point 'day)
9551 (or (not (equal ?b char))
9552 (get-text-property mark 'day)))
9553 (user-error "Don't know which date to use for diary entry"))
9554 ;; We implement this by hacking the `calendar-cursor-to-date' function
9555 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9556 (let ((calendar-mark-ring
9557 (list (calendar-gregorian-from-absolute
9558 (or (get-text-property mark 'day)
9559 (get-text-property point 'day))))))
9560 (unwind-protect
9561 (progn
9562 (fset 'calendar-cursor-to-date
9563 (lambda (&optional error dummy)
9564 (calendar-gregorian-from-absolute
9565 (get-text-property point 'day))))
9566 (call-interactively cmd))
9567 (fset 'calendar-cursor-to-date oldf))))))
9569 (defun org-agenda-execute-calendar-command (cmd)
9570 "Execute a calendar command from the agenda with date from cursor."
9571 (org-agenda-check-type t 'agenda 'timeline)
9572 (require 'diary-lib)
9573 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9574 (user-error "Don't know which date to use for the calendar command"))
9575 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9576 (point (point))
9577 (date (calendar-gregorian-from-absolute
9578 (get-text-property point 'day)))
9579 ;; the following 2 vars are needed in the calendar
9580 (displayed-month (car date))
9581 (displayed-year (nth 2 date)))
9582 (unwind-protect
9583 (progn
9584 (fset 'calendar-cursor-to-date
9585 (lambda (&optional error dummy)
9586 (calendar-gregorian-from-absolute
9587 (get-text-property point 'day))))
9588 (call-interactively cmd))
9589 (fset 'calendar-cursor-to-date oldf))))
9591 (defun org-agenda-phases-of-moon ()
9592 "Display the phases of the moon for the 3 months around the cursor date."
9593 (interactive)
9594 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
9596 (defun org-agenda-holidays ()
9597 "Display the holidays for the 3 months around the cursor date."
9598 (interactive)
9599 (org-agenda-execute-calendar-command 'list-calendar-holidays))
9601 (defvar calendar-longitude) ; defined in calendar.el
9602 (defvar calendar-latitude) ; defined in calendar.el
9603 (defvar calendar-location-name) ; defined in calendar.el
9605 (defun org-agenda-sunrise-sunset (arg)
9606 "Display sunrise and sunset for the cursor date.
9607 Latitude and longitude can be specified with the variables
9608 `calendar-latitude' and `calendar-longitude'. When called with prefix
9609 argument, latitude and longitude will be prompted for."
9610 (interactive "P")
9611 (require 'solar)
9612 (let ((calendar-longitude (if arg nil calendar-longitude))
9613 (calendar-latitude (if arg nil calendar-latitude))
9614 (calendar-location-name
9615 (if arg "the given coordinates" calendar-location-name)))
9616 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9618 (defun org-agenda-goto-calendar ()
9619 "Open the Emacs calendar with the date at the cursor."
9620 (interactive)
9621 (org-agenda-check-type t 'agenda 'timeline)
9622 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9623 (user-error "Don't know which date to open in calendar")))
9624 (date (calendar-gregorian-from-absolute day))
9625 (calendar-move-hook nil)
9626 (calendar-view-holidays-initially-flag nil)
9627 (calendar-view-diary-initially-flag nil))
9628 (calendar)
9629 (calendar-goto-date date)))
9631 ;;;###autoload
9632 (defun org-calendar-goto-agenda ()
9633 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
9634 This is a command that has to be installed in `calendar-mode-map'."
9635 (interactive)
9636 (org-agenda-list nil (calendar-absolute-from-gregorian
9637 (calendar-cursor-to-date))
9638 nil))
9640 (defun org-agenda-convert-date ()
9641 (interactive)
9642 (org-agenda-check-type t 'agenda 'timeline)
9643 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9644 date s)
9645 (unless day
9646 (user-error "Don't know which date to convert"))
9647 (setq date (calendar-gregorian-from-absolute day))
9648 (setq s (concat
9649 "Gregorian: " (calendar-date-string date) "\n"
9650 "ISO: " (calendar-iso-date-string date) "\n"
9651 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9652 "Julian: " (calendar-julian-date-string date) "\n"
9653 "Astron. JD: " (calendar-astro-date-string date)
9654 " (Julian date number at noon UTC)\n"
9655 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9656 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9657 "French: " (calendar-french-date-string date) "\n"
9658 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
9659 "Mayan: " (calendar-mayan-date-string date) "\n"
9660 "Coptic: " (calendar-coptic-date-string date) "\n"
9661 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9662 "Persian: " (calendar-persian-date-string date) "\n"
9663 "Chinese: " (calendar-chinese-date-string date) "\n"))
9664 (with-output-to-temp-buffer "*Dates*"
9665 (princ s))
9666 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9668 ;;; Bulk commands
9670 (defun org-agenda-bulk-marked-p ()
9671 (eq (get-char-property (point-at-bol) 'type)
9672 'org-marked-entry-overlay))
9674 (defun org-agenda-bulk-mark (&optional arg)
9675 "Mark the entry at point for future bulk action."
9676 (interactive "p")
9677 (dotimes (i (or arg 1))
9678 (unless (org-get-at-bol 'org-agenda-diary-link)
9679 (let* ((m (org-get-at-bol 'org-hd-marker))
9681 (unless (org-agenda-bulk-marked-p)
9682 (unless m (user-error "Nothing to mark at point"))
9683 (push m org-agenda-bulk-marked-entries)
9684 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9685 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9686 (org-get-todo-face "TODO")
9687 'evaporate)
9688 (overlay-put ov 'type 'org-marked-entry-overlay))
9689 (end-of-line 1)
9690 (or (ignore-errors
9691 (goto-char (next-single-property-change (point) 'txt)))
9692 (beginning-of-line 2))
9693 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9694 (beginning-of-line 2))
9695 (message "%d entries marked for bulk action"
9696 (length org-agenda-bulk-marked-entries))))))
9698 (defun org-agenda-bulk-mark-all ()
9699 "Mark all entries for future agenda bulk action."
9700 (interactive)
9701 (org-agenda-bulk-mark-regexp "."))
9703 (defun org-agenda-bulk-mark-regexp (regexp)
9704 "Mark entries matching REGEXP for future agenda bulk action."
9705 (interactive "sMark entries matching regexp: ")
9706 (let ((entries-marked 0) txt-at-point)
9707 (save-excursion
9708 (goto-char (point-min))
9709 (goto-char (next-single-property-change (point) 'txt))
9710 (while (and (re-search-forward regexp nil t)
9711 (setq txt-at-point (get-text-property (point) 'txt)))
9712 (when (string-match regexp txt-at-point)
9713 (setq entries-marked (1+ entries-marked))
9714 (call-interactively 'org-agenda-bulk-mark))))
9715 (if (not entries-marked)
9716 (message "No entry matching this regexp."))))
9718 (defun org-agenda-bulk-unmark (&optional arg)
9719 "Unmark the entry at point for future bulk action."
9720 (interactive "P")
9721 (if arg
9722 (org-agenda-bulk-unmark-all)
9723 (cond ((org-agenda-bulk-marked-p)
9724 (org-agenda-bulk-remove-overlays
9725 (point-at-bol) (+ 2 (point-at-bol)))
9726 (setq org-agenda-bulk-marked-entries
9727 (delete (org-get-at-bol 'org-hd-marker)
9728 org-agenda-bulk-marked-entries))
9729 (end-of-line 1)
9730 (or (ignore-errors
9731 (goto-char (next-single-property-change (point) 'txt)))
9732 (beginning-of-line 2))
9733 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9734 (beginning-of-line 2))
9735 (message "%d entries left marked for bulk action"
9736 (length org-agenda-bulk-marked-entries)))
9737 (t (message "No entry to unmark here")))))
9739 (defun org-agenda-bulk-toggle-all ()
9740 "Toggle all marks for bulk action."
9741 (interactive)
9742 (save-excursion
9743 (goto-char (point-min))
9744 (while (ignore-errors
9745 (goto-char (next-single-property-change (point) 'txt)))
9746 (org-agenda-bulk-toggle))))
9748 (defun org-agenda-bulk-toggle ()
9749 "Toggle the mark at point for bulk action."
9750 (interactive)
9751 (if (org-agenda-bulk-marked-p)
9752 (org-agenda-bulk-unmark)
9753 (org-agenda-bulk-mark)))
9755 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9756 "Remove the mark overlays between BEG and END in the agenda buffer.
9757 BEG and END default to the buffer limits.
9759 This only removes the overlays, it does not remove the markers
9760 from the list in `org-agenda-bulk-marked-entries'."
9761 (interactive)
9762 (mapc (lambda (ov)
9763 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9764 (delete-overlay ov)))
9765 (overlays-in (or beg (point-min)) (or end (point-max)))))
9767 (defun org-agenda-bulk-unmark-all ()
9768 "Remove all marks in the agenda buffer.
9769 This will remove the markers and the overlays."
9770 (interactive)
9771 (if (null org-agenda-bulk-marked-entries)
9772 (message "No entry to unmark")
9773 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
9774 (setq org-agenda-bulk-marked-entries nil)
9775 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9777 (defcustom org-agenda-persistent-marks nil
9778 "Non-nil means marked items will stay marked after a bulk action.
9779 You can toggle this interactively by typing `p' when prompted for a
9780 bulk action."
9781 :group 'org-agenda
9782 :version "24.1"
9783 :type 'boolean)
9785 (defun org-agenda-bulk-action (&optional arg)
9786 "Execute an remote-editing action on all marked entries.
9787 The prefix arg is passed through to the command if possible."
9788 (interactive "P")
9789 ;; Make sure we have markers, and only valid ones
9790 (unless org-agenda-bulk-marked-entries (user-error "No entries are marked"))
9791 (mapc
9792 (lambda (m)
9793 (unless (and (markerp m)
9794 (marker-buffer m)
9795 (buffer-live-p (marker-buffer m))
9796 (marker-position m))
9797 (user-error "Marker %s for bulk command is invalid" m)))
9798 org-agenda-bulk-marked-entries)
9800 ;; Prompt for the bulk command
9801 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
9802 (message (concat msg "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9803 "[S]catter [f]unction "
9804 (when org-agenda-bulk-custom-functions
9805 (concat " Custom: ["
9806 (mapconcat (lambda(f) (char-to-string (car f)))
9807 org-agenda-bulk-custom-functions "")
9808 "]"))))
9809 (catch 'exit
9810 (let* ((action (read-char-exclusive))
9811 (org-log-refile (if org-log-refile 'time nil))
9812 (entries (reverse org-agenda-bulk-marked-entries))
9813 (org-overriding-default-time
9814 (if (get-text-property (point) 'org-agenda-date-header)
9815 (org-get-cursor-date)))
9816 redo-at-end
9817 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
9818 (cond
9819 ((equal action ?p)
9820 (let ((org-agenda-persistent-marks
9821 (not org-agenda-persistent-marks)))
9822 (org-agenda-bulk-action)
9823 (throw 'exit nil)))
9825 ((equal action ?$)
9826 (setq cmd '(org-agenda-archive)))
9828 ((equal action ?A)
9829 (setq cmd '(org-agenda-archive-to-archive-sibling)))
9831 ((member action '(?r ?w))
9832 (setq rfloc (org-refile-get-location
9833 "Refile to"
9834 (marker-buffer (car entries))
9835 org-refile-allow-creating-parent-nodes))
9836 (if (nth 3 rfloc)
9837 (setcar (nthcdr 3 rfloc)
9838 (move-marker (make-marker) (nth 3 rfloc)
9839 (or (get-file-buffer (nth 1 rfloc))
9840 (find-buffer-visiting (nth 1 rfloc))
9841 (error "This should not happen")))))
9843 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
9844 redo-at-end t))
9846 ((equal action ?t)
9847 (setq state (org-icompleting-read
9848 "Todo state: "
9849 (with-current-buffer (marker-buffer (car entries))
9850 (mapcar 'list org-todo-keywords-1))))
9851 (setq cmd `(let ((org-inhibit-blocking t)
9852 (org-inhibit-logging 'note))
9853 (org-agenda-todo ,state))))
9855 ((memq action '(?- ?+))
9856 (setq tag (org-icompleting-read
9857 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9858 (with-current-buffer (marker-buffer (car entries))
9859 (delq nil
9860 (mapcar (lambda (x)
9861 (if (stringp (car x)) x)) org-tag-alist)))))
9862 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
9864 ((memq action '(?s ?d))
9865 (let* ((time
9866 (unless arg
9867 (org-read-date
9868 nil nil nil
9869 (if (eq action ?s) "(Re)Schedule to" "(Re)Set Deadline to")
9870 org-overriding-default-time)))
9871 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
9872 (setq cmd `(eval '(,c1 arg ,time)))))
9874 ((equal action ?S)
9875 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
9876 (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
9877 (let ((days (read-number
9878 (format "Scatter tasks across how many %sdays: "
9879 (if arg "week" "")) 7)))
9880 (setq cmd
9881 `(let ((distance (1+ (random ,days))))
9882 (if arg
9883 (let ((dist distance)
9884 (day-of-week
9885 (calendar-day-of-week
9886 (calendar-gregorian-from-absolute (org-today)))))
9887 (dotimes (i (1+ dist))
9888 (while (member day-of-week org-agenda-weekend-days)
9889 (incf distance)
9890 (incf day-of-week)
9891 (if (= day-of-week 7)
9892 (setq day-of-week 0)))
9893 (incf day-of-week)
9894 (if (= day-of-week 7)
9895 (setq day-of-week 0)))))
9896 ;; silently fail when try to replan a sexp entry
9897 (condition-case nil
9898 (let* ((date (calendar-gregorian-from-absolute
9899 (+ (org-today) distance)))
9900 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9901 (nth 2 date))))
9902 (org-agenda-schedule nil time))
9903 (error nil)))))))
9905 ((assoc action org-agenda-bulk-custom-functions)
9906 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
9907 redo-at-end t))
9909 ((equal action ?f)
9910 (setq cmd (list (intern
9911 (org-icompleting-read "Function: "
9912 obarray 'fboundp t nil nil)))))
9914 (t (user-error "Invalid bulk action")))
9916 ;; Sort the markers, to make sure that parents are handled before children
9917 (setq entries (sort entries
9918 (lambda (a b)
9919 (cond
9920 ((equal (marker-buffer a) (marker-buffer b))
9921 (< (marker-position a) (marker-position b)))
9923 (string< (buffer-name (marker-buffer a))
9924 (buffer-name (marker-buffer b))))))))
9926 ;; Now loop over all markers and apply cmd
9927 (while (setq e (pop entries))
9928 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
9929 (if (not pos)
9930 (progn (message "Skipping removed entry at %s" e)
9931 (setq cntskip (1+ cntskip)))
9932 (goto-char pos)
9933 (let (org-loop-over-headlines-in-active-region)
9934 (eval cmd))
9935 (setq cnt (1+ cnt))))
9936 (when redo-at-end (org-agenda-redo))
9937 (unless org-agenda-persistent-marks
9938 (org-agenda-bulk-unmark-all))
9939 (message "Acted on %d entries%s%s"
9941 (if (= cntskip 0)
9943 (format ", skipped %d (disappeared before their turn)"
9944 cntskip))
9945 (if (not org-agenda-persistent-marks)
9946 "" " (kept marked)"))))))
9948 (defun org-agenda-capture (&optional with-time)
9949 "Call `org-capture' with the date at point.
9950 With a `C-1' prefix, use the HH:MM value at point (if any) or the
9951 current HH:MM time."
9952 (interactive "P")
9953 (if (not (eq major-mode 'org-agenda-mode))
9954 (user-error "You cannot do this outside of agenda buffers")
9955 (let ((org-overriding-default-time
9956 (org-get-cursor-date (equal with-time 1))))
9957 (call-interactively 'org-capture))))
9959 ;;; Dragging agenda lines forward/backward
9961 (defun org-agenda-reapply-filters ()
9962 "Re-apply all agenda filters."
9963 (mapcar
9964 (lambda(f) (when (car f) (org-agenda-filter-apply (car f) (cadr f))))
9965 `((,org-agenda-tag-filter tag)
9966 (,org-agenda-category-filter category)
9967 (,org-agenda-regexp-filter regexp)
9968 (,(get 'org-agenda-tag-filter :preset-filter) tag)
9969 (,(get 'org-agenda-category-filter :preset-filter) category)
9970 (,(get 'org-agenda-regexp-filter :preset-filter) regexp))))
9972 (defun org-agenda-drag-line-forward (arg &optional backward)
9973 "Drag an agenda line forward by ARG lines.
9974 When the optional argument `backward' is non-nil, move backward."
9975 (interactive "p")
9976 (let ((inhibit-read-only t) lst line)
9977 (if (or (not (get-text-property (point) 'txt))
9978 (save-excursion
9979 (dotimes (n arg)
9980 (move-beginning-of-line (if backward 0 2))
9981 (push (not (get-text-property (point) 'txt)) lst))
9982 (delq nil lst)))
9983 (message "Cannot move line forward")
9984 (let ((end (save-excursion (move-beginning-of-line 2) (point))))
9985 (move-beginning-of-line 1)
9986 (setq line (buffer-substring (point) end))
9987 (delete-region (point) end)
9988 (move-beginning-of-line (funcall (if backward '1- '1+) arg))
9989 (insert line)
9990 (org-agenda-reapply-filters)
9991 (org-agenda-mark-clocking-task)
9992 (move-beginning-of-line 0)))))
9994 (defun org-agenda-drag-line-backward (arg)
9995 "Drag an agenda line backward by ARG lines."
9996 (interactive "p")
9997 (org-agenda-drag-line-forward arg t))
9999 ;;; Flagging notes
10001 (defun org-agenda-show-the-flagging-note ()
10002 "Display the flagging note in the other window.
10003 When called a second time in direct sequence, offer to remove the FLAGGING
10004 tag and (if present) the flagging note."
10005 (interactive)
10006 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
10007 (win (selected-window))
10008 note heading newhead)
10009 (unless hdmarker
10010 (user-error "No linked entry at point"))
10011 (if (and (eq this-command last-command)
10012 (y-or-n-p "Unflag and remove any flagging note? "))
10013 (progn
10014 (org-agenda-remove-flag hdmarker)
10015 (let ((win (get-buffer-window "*Flagging Note*")))
10016 (and win (delete-window win)))
10017 (message "Entry unflagged"))
10018 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
10019 (unless note
10020 (user-error "No flagging note"))
10021 (org-kill-new note)
10022 (org-switch-to-buffer-other-window "*Flagging Note*")
10023 (erase-buffer)
10024 (insert note)
10025 (goto-char (point-min))
10026 (while (re-search-forward "\\\\n" nil t)
10027 (replace-match "\n" t t))
10028 (goto-char (point-min))
10029 (select-window win)
10030 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
10032 (defun org-agenda-remove-flag (marker)
10033 "Remove the FLAGGED tag and any flagging note in the entry."
10034 (let (newhead)
10035 (org-with-point-at marker
10036 (org-toggle-tag "FLAGGED" 'off)
10037 (org-entry-delete nil "THEFLAGGINGNOTE")
10038 (setq newhead (org-get-heading)))
10039 (org-agenda-change-all-lines newhead marker)
10040 (message "Entry unflagged")))
10042 (defun org-agenda-get-any-marker (&optional pos)
10043 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
10044 (get-text-property (or pos (point-at-bol)) 'org-marker)))
10046 ;;; Appointment reminders
10048 (defvar appt-time-msg-list) ; defined in appt.el
10050 ;;;###autoload
10051 (defun org-agenda-to-appt (&optional refresh filter &rest args)
10052 "Activate appointments found in `org-agenda-files'.
10053 With a \\[universal-argument] prefix, refresh the list of
10054 appointments.
10056 If FILTER is t, interactively prompt the user for a regular
10057 expression, and filter out entries that don't match it.
10059 If FILTER is a string, use this string as a regular expression
10060 for filtering entries out.
10062 If FILTER is a function, filter out entries against which
10063 calling the function returns nil. This function takes one
10064 argument: an entry from `org-agenda-get-day-entries'.
10066 FILTER can also be an alist with the car of each cell being
10067 either 'headline or 'category. For example:
10069 '((headline \"IMPORTANT\")
10070 (category \"Work\"))
10072 will only add headlines containing IMPORTANT or headlines
10073 belonging to the \"Work\" category.
10075 ARGS are symbols indicating what kind of entries to consider.
10076 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
10077 \(i.e., deadlines and scheduled items with a hh:mm specification)
10078 and :timestamp entries. See the docstring of `org-diary' for
10079 details and examples.
10081 If an entry has a APPT_WARNTIME property, its value will be used
10082 to override `appt-message-warning-time'."
10083 (interactive "P")
10084 (if refresh (setq appt-time-msg-list nil))
10085 (if (eq filter t)
10086 (setq filter (read-from-minibuffer "Regexp filter: ")))
10087 (let* ((cnt 0) ; count added events
10088 (scope (or args '(:deadline* :scheduled* :timestamp)))
10089 (org-agenda-new-buffers nil)
10090 (org-deadline-warning-days 0)
10091 ;; Do not use `org-today' here because appt only takes
10092 ;; time and without date as argument, so it may pass wrong
10093 ;; information otherwise
10094 (today (org-date-to-gregorian
10095 (time-to-days (current-time))))
10096 (org-agenda-restrict nil)
10097 (files (org-agenda-files 'unrestricted)) entries file
10098 (org-agenda-buffer nil))
10099 ;; Get all entries which may contain an appt
10100 (org-agenda-prepare-buffers files)
10101 (while (setq file (pop files))
10102 (setq entries
10103 (delq nil
10104 (append entries
10105 (apply 'org-agenda-get-day-entries
10106 file today scope)))))
10107 ;; Map thru entries and find if we should filter them out
10108 (mapc
10109 (lambda(x)
10110 (let* ((evt (org-trim
10111 (replace-regexp-in-string
10112 org-bracket-link-regexp "\\3"
10113 (or (get-text-property 1 'txt x) ""))))
10114 (cat (get-text-property 1 'org-category x))
10115 (tod (get-text-property 1 'time-of-day x))
10116 (ok (or (null filter)
10117 (and (stringp filter) (string-match filter evt))
10118 (and (functionp filter) (funcall filter x))
10119 (and (listp filter)
10120 (let ((cat-filter (cadr (assoc 'category filter)))
10121 (evt-filter (cadr (assoc 'headline filter))))
10122 (or (and (stringp cat-filter)
10123 (string-match cat-filter cat))
10124 (and (stringp evt-filter)
10125 (string-match evt-filter evt)))))))
10126 (wrn (get-text-property 1 'warntime x)))
10127 ;; FIXME: Shall we remove text-properties for the appt text?
10128 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
10129 (when (and ok tod)
10130 (setq tod (concat "00" (number-to-string tod))
10131 tod (when (string-match
10132 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
10133 (concat (match-string 1 tod) ":"
10134 (match-string 2 tod))))
10135 (if (version< emacs-version "23.3")
10136 (appt-add tod evt)
10137 (appt-add tod evt wrn))
10138 (setq cnt (1+ cnt))))) entries)
10139 (org-release-buffers org-agenda-new-buffers)
10140 (if (eq cnt 0)
10141 (message "No event to add")
10142 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
10144 (defun org-agenda-todayp (date)
10145 "Does DATE mean today, when considering `org-extend-today-until'?"
10146 (let ((today (org-today))
10147 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
10148 date)))
10149 (eq date today)))
10151 (defun org-agenda-todo-yesterday (&optional arg)
10152 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
10153 (interactive "P")
10154 (let* ((hour (third (decode-time
10155 (org-current-time))))
10156 (org-extend-today-until (1+ hour)))
10157 (org-agenda-todo arg)))
10159 (provide 'org-agenda)
10161 ;;; org-agenda.el ends here