Fix aa86e4bc
[org-mode.git] / lisp / org-agenda.el
blob864cfdd0cb1faecc3be00bd4b0477e9acd57238e
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 "Effort filter preset"
365 (const org-agenda-effort-filter-preset)
366 (list
367 (const :format "" quote)
368 (repeat
369 (string :tag "+=10 or -=10 or +<10 or ->10"))))
370 (list :tag "Regexp filter preset"
371 (const org-agenda-regexp-filter-preset)
372 (list
373 (const :format "" quote)
374 (repeat
375 (string :tag "+regexp or -regexp"))))
376 (list :tag "Set daily/weekly entry types"
377 (const org-agenda-entry-types)
378 (list
379 (const :format "" quote)
380 (set :greedy t :value ,org-agenda-entry-types
381 (const :deadline)
382 (const :scheduled)
383 (const :deadline*)
384 (const :scheduled*)
385 (const :timestamp)
386 (const :sexp))))
387 (list :tag "Standard skipping condition"
388 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
389 (const org-agenda-skip-function)
390 (list
391 (const :format "" quote)
392 (list
393 (choice
394 :tag "Skipping range"
395 (const :tag "Skip entry" org-agenda-skip-entry-if)
396 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
397 (repeat :inline t :tag "Conditions for skipping"
398 (choice
399 :tag "Condition type"
400 (list :tag "Regexp matches" :inline t (const :format "" regexp) (regexp))
401 (list :tag "Regexp does not match" :inline t (const :format "" notregexp) (regexp))
402 (list :tag "TODO state is" :inline t
403 (const todo)
404 (choice
405 (const :tag "any not-done state" todo)
406 (const :tag "any done state" done)
407 (const :tag "any state" any)
408 (list :tag "Keyword list"
409 (const :format "" quote)
410 (repeat (string :tag "Keyword")))))
411 (list :tag "TODO state is not" :inline t
412 (const nottodo)
413 (choice
414 (const :tag "any not-done state" todo)
415 (const :tag "any done state" done)
416 (const :tag "any state" any)
417 (list :tag "Keyword list"
418 (const :format "" quote)
419 (repeat (string :tag "Keyword")))))
420 (const :tag "scheduled" scheduled)
421 (const :tag "not scheduled" notscheduled)
422 (const :tag "deadline" deadline)
423 (const :tag "no deadline" notdeadline)
424 (const :tag "timestamp" timestamp)
425 (const :tag "no timestamp" nottimestamp))))))
426 (list :tag "Non-standard skipping condition"
427 :value (org-agenda-skip-function)
428 (const org-agenda-skip-function)
429 (sexp :tag "Function or form (quoted!)"))
430 (list :tag "Any variable"
431 (variable :tag "Variable")
432 (sexp :tag "Value (sexp)"))))
433 "Selection of examples for agenda command settings.
434 This will be spliced into the custom type of
435 `org-agenda-custom-commands'.")
438 (defcustom org-agenda-custom-commands
439 '(("n" "Agenda and all TODO's" ((agenda "") (alltodo ""))))
440 "Custom commands for the agenda.
441 These commands will be offered on the splash screen displayed by the
442 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
444 (key desc type match settings files)
446 key The key (one or more characters as a string) to be associated
447 with the command.
448 desc A description of the command, when omitted or nil, a default
449 description is built using MATCH.
450 type The command type, any of the following symbols:
451 agenda The daily/weekly agenda.
452 todo Entries with a specific TODO keyword, in all agenda files.
453 search Entries containing search words entry or headline.
454 tags Tags/Property/TODO match in all agenda files.
455 tags-todo Tags/P/T match in all agenda files, TODO entries only.
456 todo-tree Sparse tree of specific TODO keyword in *current* file.
457 tags-tree Sparse tree with all tags matches in *current* file.
458 occur-tree Occur sparse tree for *current* file.
459 ... A user-defined function.
460 match What to search for:
461 - a single keyword for TODO keyword searches
462 - a tags match expression for tags searches
463 - a word search expression for text searches.
464 - a regular expression for occur searches
465 For all other commands, this should be the empty string.
466 settings A list of option settings, similar to that in a let form, so like
467 this: ((opt1 val1) (opt2 val2) ...). The values will be
468 evaluated at the moment of execution, so quote them when needed.
469 files A list of files file to write the produced agenda buffer to
470 with the command `org-store-agenda-views'.
471 If a file name ends in \".html\", an HTML version of the buffer
472 is written out. If it ends in \".ps\", a postscript version is
473 produced. Otherwise, only the plain text is written to the file.
475 You can also define a set of commands, to create a composite agenda buffer.
476 In this case, an entry looks like this:
478 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
480 where
482 desc A description string to be displayed in the dispatcher menu.
483 cmd An agenda command, similar to the above. However, tree commands
484 are not allowed, but instead you can get agenda and global todo list.
485 So valid commands for a set are:
486 (agenda \"\" settings)
487 (alltodo \"\" settings)
488 (stuck \"\" settings)
489 (todo \"match\" settings files)
490 (search \"match\" settings files)
491 (tags \"match\" settings files)
492 (tags-todo \"match\" settings files)
494 Each command can carry a list of options, and another set of options can be
495 given for the whole set of commands. Individual command options take
496 precedence over the general options.
498 When using several characters as key to a command, the first characters
499 are prefix commands. For the dispatcher to display useful information, you
500 should provide a description for the prefix, like
502 (setq org-agenda-custom-commands
503 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
504 (\"hl\" tags \"+HOME+Lisa\")
505 (\"hp\" tags \"+HOME+Peter\")
506 (\"hk\" tags \"+HOME+Kim\")))"
507 :group 'org-agenda-custom-commands
508 :type `(repeat
509 (choice :value ("x" "Describe command here" tags "" nil)
510 (list :tag "Single command"
511 (string :tag "Access Key(s) ")
512 (option (string :tag "Description"))
513 (choice
514 (const :tag "Agenda" agenda)
515 (const :tag "TODO list" alltodo)
516 (const :tag "Search words" search)
517 (const :tag "Stuck projects" stuck)
518 (const :tag "Tags/Property match (all agenda files)" tags)
519 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
520 (const :tag "TODO keyword search (all agenda files)" todo)
521 (const :tag "Tags sparse tree (current buffer)" tags-tree)
522 (const :tag "TODO keyword tree (current buffer)" todo-tree)
523 (const :tag "Occur tree (current buffer)" occur-tree)
524 (sexp :tag "Other, user-defined function"))
525 (string :tag "Match (only for some commands)")
526 ,org-agenda-custom-commands-local-options
527 (option (repeat :tag "Export" (file :tag "Export to"))))
528 (list :tag "Command series, all agenda files"
529 (string :tag "Access Key(s)")
530 (string :tag "Description ")
531 (repeat :tag "Component"
532 (choice
533 (list :tag "Agenda"
534 (const :format "" agenda)
535 (const :tag "" :format "" "")
536 ,org-agenda-custom-commands-local-options)
537 (list :tag "TODO list (all keywords)"
538 (const :format "" alltodo)
539 (const :tag "" :format "" "")
540 ,org-agenda-custom-commands-local-options)
541 (list :tag "Search words"
542 (const :format "" search)
543 (string :tag "Match")
544 ,org-agenda-custom-commands-local-options)
545 (list :tag "Stuck projects"
546 (const :format "" stuck)
547 (const :tag "" :format "" "")
548 ,org-agenda-custom-commands-local-options)
549 (list :tag "Tags search"
550 (const :format "" tags)
551 (string :tag "Match")
552 ,org-agenda-custom-commands-local-options)
553 (list :tag "Tags search, TODO entries only"
554 (const :format "" tags-todo)
555 (string :tag "Match")
556 ,org-agenda-custom-commands-local-options)
557 (list :tag "TODO keyword search"
558 (const :format "" todo)
559 (string :tag "Match")
560 ,org-agenda-custom-commands-local-options)
561 (list :tag "Other, user-defined function"
562 (symbol :tag "function")
563 (string :tag "Match")
564 ,org-agenda-custom-commands-local-options)))
566 (repeat :tag "Settings for entire command set"
567 (list (variable :tag "Any variable")
568 (sexp :tag "Value")))
569 (option (repeat :tag "Export" (file :tag "Export to"))))
570 (cons :tag "Prefix key documentation"
571 (string :tag "Access Key(s)")
572 (string :tag "Description ")))))
574 (defcustom org-agenda-query-register ?o
575 "The register holding the current query string.
576 The purpose of this is that if you construct a query string interactively,
577 you can then use it to define a custom command."
578 :group 'org-agenda-custom-commands
579 :type 'character)
581 (defcustom org-stuck-projects
582 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
583 "How to identify stuck projects.
584 This is a list of four items:
585 1. A tags/todo/property matcher string that is used to identify a project.
586 See the manual for a description of tag and property searches.
587 The entire tree below a headline matched by this is considered one project.
588 2. A list of TODO keywords identifying non-stuck projects.
589 If the project subtree contains any headline with one of these todo
590 keywords, the project is considered to be not stuck. If you specify
591 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
592 3. A list of tags identifying non-stuck projects.
593 If the project subtree contains any headline with one of these tags,
594 the project is considered to be not stuck. If you specify \"*\" as
595 a tag, any tag will mark the project unstuck. Note that this is about
596 the explicit presence of a tag somewhere in the subtree, inherited
597 tags do not count here. If inherited tags make a project not stuck,
598 use \"-TAG\" in the tags part of the matcher under (1.) above.
599 4. An arbitrary regular expression matching non-stuck projects.
601 If the project turns out to be not stuck, search continues also in the
602 subtree to see if any of the subtasks have project status.
604 See also the variable `org-tags-match-list-sublevels' which applies
605 to projects matched by this search as well.
607 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
608 or `C-c a #' to produce the list."
609 :group 'org-agenda-custom-commands
610 :type '(list
611 (string :tag "Tags/TODO match to identify a project")
612 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
613 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
614 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
616 (defgroup org-agenda-skip nil
617 "Options concerning skipping parts of agenda files."
618 :tag "Org Agenda Skip"
619 :group 'org-agenda)
621 (defcustom org-agenda-skip-function-global nil
622 "Function to be called at each match during agenda construction.
623 If this function returns nil, the current match should not be skipped.
624 If the function decided to skip an agenda match, is must return the
625 buffer position from which the search should be continued.
626 This may also be a Lisp form, which will be evaluated.
628 This variable will be applied to every agenda match, including
629 tags/property searches and TODO lists. So try to make the test function
630 do its checking as efficiently as possible. To implement a skipping
631 condition just for specific agenda commands, use the variable
632 `org-agenda-skip-function' which can be set in the options section
633 of custom agenda commands."
634 :group 'org-agenda-skip
635 :type 'sexp)
637 (defgroup org-agenda-daily/weekly nil
638 "Options concerning the daily/weekly agenda."
639 :tag "Org Agenda Daily/Weekly"
640 :group 'org-agenda)
641 (defgroup org-agenda-todo-list nil
642 "Options concerning the global todo list agenda view."
643 :tag "Org Agenda Todo List"
644 :group 'org-agenda)
645 (defgroup org-agenda-match-view nil
646 "Options concerning the general tags/property/todo match agenda view."
647 :tag "Org Agenda Match View"
648 :group 'org-agenda)
649 (defgroup org-agenda-search-view nil
650 "Options concerning the search agenda view."
651 :tag "Org Agenda Search View"
652 :group 'org-agenda)
654 (defvar org-agenda-archives-mode nil
655 "Non-nil means the agenda will include archived items.
656 If this is the symbol `trees', trees in the selected agenda scope
657 that are marked with the ARCHIVE tag will be included anyway. When this is
658 t, also all archive files associated with the current selection of agenda
659 files will be included.")
661 (defcustom org-agenda-restriction-lock-highlight-subtree t
662 "Non-nil means highlight the whole subtree when restriction is active.
663 Otherwise only highlight the headline. Highlighting the whole subtree is
664 useful to ensure no edits happen beyond the restricted region."
665 :group 'org-agenda
666 :type 'boolean)
668 (defcustom org-agenda-skip-comment-trees t
669 "Non-nil means skip trees that start with the COMMENT keyword.
670 When nil, these trees are also scanned by agenda commands."
671 :group 'org-agenda-skip
672 :type 'boolean)
674 (defcustom org-agenda-todo-list-sublevels t
675 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
676 When nil, the sublevels of a TODO entry are not checked, resulting in
677 potentially much shorter TODO lists."
678 :group 'org-agenda-skip
679 :group 'org-agenda-todo-list
680 :type 'boolean)
682 (defcustom org-agenda-todo-ignore-with-date nil
683 "Non-nil means don't show entries with a date in the global todo list.
684 You can use this if you prefer to mark mere appointments with a TODO keyword,
685 but don't want them to show up in the TODO list.
686 When this is set, it also covers deadlines and scheduled items, the settings
687 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
688 will be ignored.
689 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
690 :group 'org-agenda-skip
691 :group 'org-agenda-todo-list
692 :type 'boolean)
694 (defcustom org-agenda-todo-ignore-timestamp nil
695 "Non-nil means don't show entries with a timestamp.
696 This applies when creating the global todo list.
697 Valid values are:
699 past Don't show entries for today or in the past.
701 future Don't show entries with a timestamp in the future.
702 The idea behind this is that if it has a future
703 timestamp, you don't want to think about it until the
704 date.
706 all Don't show any entries with a timestamp in the global todo list.
707 The idea behind this is that by setting a timestamp, you
708 have already \"taken care\" of this item.
710 This variable can also have an integer as a value. If positive (N),
711 todos with a timestamp N or more days in the future will be ignored. If
712 negative (-N), todos with a timestamp N or more days in the past will be
713 ignored. If 0, todos with a timestamp either today or in the future will
714 be ignored. For example, a value of -1 will exclude todos with a
715 timestamp in the past (yesterday or earlier), while a value of 7 will
716 exclude todos with a timestamp a week or more in the future.
718 See also `org-agenda-todo-ignore-with-date'.
719 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
720 to make his option also apply to the tags-todo list."
721 :group 'org-agenda-skip
722 :group 'org-agenda-todo-list
723 :version "24.1"
724 :type '(choice
725 (const :tag "Ignore future timestamp todos" future)
726 (const :tag "Ignore past or present timestamp todos" past)
727 (const :tag "Ignore all timestamp todos" all)
728 (const :tag "Show timestamp todos" nil)
729 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
731 (defcustom org-agenda-todo-ignore-scheduled nil
732 "Non-nil means, ignore some scheduled TODO items when making TODO list.
733 This applies when creating the global todo list.
734 Valid values are:
736 past Don't show entries scheduled today or in the past.
738 future Don't show entries scheduled in the future.
739 The idea behind this is that by scheduling it, you don't want to
740 think about it until the scheduled date.
742 all Don't show any scheduled entries in the global todo list.
743 The idea behind this is that by scheduling it, you have already
744 \"taken care\" of this item.
746 t Same as `all', for backward compatibility.
748 This variable can also have an integer as a value. See
749 `org-agenda-todo-ignore-timestamp' for more details.
751 See also `org-agenda-todo-ignore-with-date'.
752 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
753 to make his option also apply to the tags-todo list."
754 :group 'org-agenda-skip
755 :group 'org-agenda-todo-list
756 :type '(choice
757 (const :tag "Ignore future-scheduled todos" future)
758 (const :tag "Ignore past- or present-scheduled todos" past)
759 (const :tag "Ignore all scheduled todos" all)
760 (const :tag "Ignore all scheduled todos (compatibility)" t)
761 (const :tag "Show scheduled todos" nil)
762 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
764 (defcustom org-agenda-todo-ignore-deadlines nil
765 "Non-nil means ignore some deadline TODO items when making TODO list.
766 There are different motivations for using different values, please think
767 carefully when configuring this variable.
769 This applies when creating the global todo list.
770 Valid values are:
772 near Don't show near deadline entries. A deadline is near when it is
773 closer than `org-deadline-warning-days' days. The idea behind this
774 is that such items will appear in the agenda anyway.
776 far Don't show TODO entries where a deadline has been defined, but
777 the deadline is not near. This is useful if you don't want to
778 use the todo list to figure out what to do now.
780 past Don't show entries with a deadline timestamp for today or in the past.
782 future Don't show entries with a deadline timestamp in the future, not even
783 when they become `near' ones. Use it with caution.
785 all Ignore all TODO entries that do have a deadline.
787 t Same as `near', for backward compatibility.
789 This variable can also have an integer as a value. See
790 `org-agenda-todo-ignore-timestamp' for more details.
792 See also `org-agenda-todo-ignore-with-date'.
793 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
794 to make his option also apply to the tags-todo list."
795 :group 'org-agenda-skip
796 :group 'org-agenda-todo-list
797 :type '(choice
798 (const :tag "Ignore near deadlines" near)
799 (const :tag "Ignore near deadlines (compatibility)" t)
800 (const :tag "Ignore far deadlines" far)
801 (const :tag "Ignore all TODOs with a deadlines" all)
802 (const :tag "Show all TODOs, even if they have a deadline" nil)
803 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
805 (defcustom org-agenda-todo-ignore-time-comparison-use-seconds nil
806 "Time unit to use when possibly ignoring an agenda item.
808 See the docstring of various `org-agenda-todo-ignore-*' options.
809 The default is to compare time stamps using days. An item is thus
810 considered to be in the future if it is at least one day after today.
811 Non-nil means to compare time stamps using seconds. An item is then
812 considered future if it has a time value later than current time."
813 :group 'org-agenda-skip
814 :group 'org-agenda-todo-list
815 :version "24.4"
816 :package-version '(Org . "8.0")
817 :type '(choice
818 (const :tag "Compare time with days" nil)
819 (const :tag "Compare time with seconds" t)))
821 (defcustom org-agenda-tags-todo-honor-ignore-options nil
822 "Non-nil means honor todo-list ignores options also in tags-todo search.
823 The variables
824 `org-agenda-todo-ignore-with-date',
825 `org-agenda-todo-ignore-timestamp',
826 `org-agenda-todo-ignore-scheduled',
827 `org-agenda-todo-ignore-deadlines'
828 make the global TODO list skip entries that have time stamps of certain
829 kinds. If this option is set, the same options will also apply for the
830 tags-todo search, which is the general tags/property matcher
831 restricted to unfinished TODO entries only."
832 :group 'org-agenda-skip
833 :group 'org-agenda-todo-list
834 :group 'org-agenda-match-view
835 :type 'boolean)
837 (defcustom org-agenda-skip-scheduled-if-done nil
838 "Non-nil means don't show scheduled items in agenda when they are done.
839 This is relevant for the daily/weekly agenda, not for the TODO list. And
840 it applies only to the actual date of the scheduling. Warnings about
841 an item with a past scheduling dates are always turned off when the item
842 is DONE."
843 :group 'org-agenda-skip
844 :group 'org-agenda-daily/weekly
845 :type 'boolean)
847 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
848 "Non-nil means skip scheduling line if same entry shows because of deadline.
850 In the agenda of today, an entry can show up multiple times
851 because it is both scheduled and has a nearby deadline, and maybe
852 a plain time stamp as well.
854 When this variable is nil, the entry will be shown several times.
856 When set to t, then only the deadline is shown and the fact that
857 the entry is scheduled today or was scheduled previously is not
858 shown.
860 When set to the symbol `not-today', skip scheduled previously,
861 but not scheduled today.
863 When set to the symbol `repeated-after-deadline', skip scheduled
864 items if they are repeated beyond the current deadline."
865 :group 'org-agenda-skip
866 :group 'org-agenda-daily/weekly
867 :type '(choice
868 (const :tag "Never" nil)
869 (const :tag "Always" t)
870 (const :tag "Not when scheduled today" not-today)
871 (const :tag "When repeated past deadline" repeated-after-deadline)))
873 (defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
874 "Non-nil means skip timestamp line if same entry shows because of deadline.
875 In the agenda of today, an entry can show up multiple times
876 because it has both a plain timestamp and has a nearby deadline.
877 When this variable is t, then only the deadline is shown and the
878 fact that the entry has a timestamp for or including today is not
879 shown. When this variable is nil, the entry will be shown
880 several times."
881 :group 'org-agenda-skip
882 :group 'org-agenda-daily/weekly
883 :version "24.1"
884 :type '(choice
885 (const :tag "Never" nil)
886 (const :tag "Always" t)))
888 (defcustom org-agenda-skip-deadline-if-done nil
889 "Non-nil means don't show deadlines when the corresponding item is done.
890 When nil, the deadline is still shown and should give you a happy feeling.
891 This is relevant for the daily/weekly agenda. And it applied only to the
892 actually date of the deadline. Warnings about approaching and past-due
893 deadlines are always turned off when the item is DONE."
894 :group 'org-agenda-skip
895 :group 'org-agenda-daily/weekly
896 :type 'boolean)
898 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
899 "Non-nil means skip deadline prewarning when entry is also scheduled.
900 This will apply on all days where a prewarning for the deadline would
901 be shown, but not at the day when the entry is actually due. On that day,
902 the deadline will be shown anyway.
903 This variable may be set to nil, t, the symbol `pre-scheduled',
904 or a number which will then give the number of days before the actual
905 deadline when the prewarnings should resume. The symbol `pre-scheduled'
906 eliminates the deadline prewarning only prior to the scheduled date.
907 This can be used in a workflow where the first showing of the deadline will
908 trigger you to schedule it, and then you don't want to be reminded of it
909 because you will take care of it on the day when scheduled."
910 :group 'org-agenda-skip
911 :group 'org-agenda-daily/weekly
912 :version "24.1"
913 :type '(choice
914 (const :tag "Always show prewarning" nil)
915 (const :tag "Remove prewarning prior to scheduled date" pre-scheduled)
916 (const :tag "Remove prewarning if entry is scheduled" t)
917 (integer :tag "Restart prewarning N days before deadline")))
919 (defcustom org-agenda-skip-scheduled-delay-if-deadline nil
920 "Non-nil means skip scheduled delay when entry also has a deadline.
921 This variable may be set to nil, t, the symbol `post-deadline',
922 or a number which will then give the number of days after the actual
923 scheduled date when the delay should expire. The symbol `post-deadline'
924 eliminates the schedule delay when the date is posterior to the deadline."
925 :group 'org-agenda-skip
926 :group 'org-agenda-daily/weekly
927 :version "24.4"
928 :package-version '(Org . "8.0")
929 :type '(choice
930 (const :tag "Always honor delay" nil)
931 (const :tag "Ignore delay if posterior to the deadline" post-deadline)
932 (const :tag "Ignore delay if entry has a deadline" t)
933 (integer :tag "Honor delay up until N days after the scheduled date")))
935 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
936 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
937 When non-nil, after the search for timestamps has matched once in an
938 entry, the rest of the entry will not be searched."
939 :group 'org-agenda-skip
940 :type 'boolean)
942 (defcustom org-agenda-skip-timestamp-if-done nil
943 "Non-nil means don't select item by timestamp or -range if it is DONE."
944 :group 'org-agenda-skip
945 :group 'org-agenda-daily/weekly
946 :type 'boolean)
948 (defcustom org-agenda-dim-blocked-tasks t
949 "Non-nil means dim blocked tasks in the agenda display.
950 This causes some overhead during agenda construction, but if you
951 have turned on `org-enforce-todo-dependencies',
952 `org-enforce-todo-checkbox-dependencies', or any other blocking
953 mechanism, this will create useful feedback in the agenda.
955 Instead of t, this variable can also have the value `invisible'.
956 Then blocked tasks will be invisible and only become visible when
957 they become unblocked. An exemption to this behavior is when a task is
958 blocked because of unchecked checkboxes below it. Since checkboxes do
959 not show up in the agenda views, making this task invisible you remove any
960 trace from agenda views that there is something to do. Therefore, a task
961 that is blocked because of checkboxes will never be made invisible, it
962 will only be dimmed."
963 :group 'org-agenda-daily/weekly
964 :group 'org-agenda-todo-list
965 :version "24.3"
966 :type '(choice
967 (const :tag "Do not dim" nil)
968 (const :tag "Dim to a gray face" t)
969 (const :tag "Make invisible" invisible)))
971 (defcustom org-timeline-show-empty-dates 3
972 "Non-nil means `org-timeline' also shows dates without an entry.
973 When nil, only the days which actually have entries are shown.
974 When t, all days between the first and the last date are shown.
975 When an integer, show also empty dates, but if there is a gap of more than
976 N days, just insert a special line indicating the size of the gap."
977 :group 'org-agenda-skip
978 :type '(choice
979 (const :tag "None" nil)
980 (const :tag "All" t)
981 (integer :tag "at most")))
983 (defgroup org-agenda-startup nil
984 "Options concerning initial settings in the Agenda in Org Mode."
985 :tag "Org Agenda Startup"
986 :group 'org-agenda)
988 (defcustom org-agenda-menu-show-matcher t
989 "Non-nil means show the match string in the agenda dispatcher menu.
990 When nil, the matcher string is not shown, but is put into the help-echo
991 property so than moving the mouse over the command shows it.
992 Setting it to nil is good if matcher strings are very long and/or if
993 you want to use two-columns display (see `org-agenda-menu-two-columns')."
994 :group 'org-agenda
995 :version "24.1"
996 :type 'boolean)
998 (define-obsolete-variable-alias 'org-agenda-menu-two-column 'org-agenda-menu-two-columns "24.3")
1000 (defcustom org-agenda-menu-two-columns nil
1001 "Non-nil means, use two columns to show custom commands in the dispatcher.
1002 If you use this, you probably want to set `org-agenda-menu-show-matcher'
1003 to nil."
1004 :group 'org-agenda
1005 :version "24.1"
1006 :type 'boolean)
1008 (define-obsolete-variable-alias 'org-finalize-agenda-hook 'org-agenda-finalize-hook "24.3")
1009 (defcustom org-agenda-finalize-hook nil
1010 "Hook run just before displaying an agenda buffer.
1011 The buffer is still writable when the hook is called.
1013 You can modify some of the buffer substrings but you should be
1014 extra careful not to modify the text properties of the agenda
1015 headlines as the agenda display heavily relies on them."
1016 :group 'org-agenda-startup
1017 :type 'hook)
1019 (defcustom org-agenda-mouse-1-follows-link nil
1020 "Non-nil means mouse-1 on a link will follow the link in the agenda.
1021 A longer mouse click will still set point. Does not work on XEmacs.
1022 Needs to be set before org.el is loaded."
1023 :group 'org-agenda-startup
1024 :type 'boolean)
1026 (defcustom org-agenda-start-with-follow-mode nil
1027 "The initial value of follow mode in a newly created agenda window."
1028 :group 'org-agenda-startup
1029 :type 'boolean)
1031 (defcustom org-agenda-follow-indirect nil
1032 "Non-nil means `org-agenda-follow-mode' displays only the
1033 current item's tree, in an indirect buffer."
1034 :group 'org-agenda
1035 :version "24.1"
1036 :type 'boolean)
1038 (defcustom org-agenda-show-outline-path t
1039 "Non-nil means show outline path in echo area after line motion."
1040 :group 'org-agenda-startup
1041 :type 'boolean)
1043 (defcustom org-agenda-start-with-entry-text-mode nil
1044 "The initial value of entry-text-mode in a newly created agenda window."
1045 :group 'org-agenda-startup
1046 :type 'boolean)
1048 (defcustom org-agenda-entry-text-maxlines 5
1049 "Number of text lines to be added when `E' is pressed in the agenda.
1051 Note that this variable only used during agenda display. Add add entry text
1052 when exporting the agenda, configure the variable
1053 `org-agenda-add-entry-ext-maxlines'."
1054 :group 'org-agenda
1055 :type 'integer)
1057 (defcustom org-agenda-entry-text-exclude-regexps nil
1058 "List of regular expressions to clean up entry text.
1059 The complete matches of all regular expressions in this list will be
1060 removed from entry text before it is shown in the agenda."
1061 :group 'org-agenda
1062 :type '(repeat (regexp)))
1064 (defcustom org-agenda-entry-text-leaders " > "
1065 "Text prepended to the entry text in agenda buffers."
1066 :version "24.4"
1067 :package-version '(Org . "8.0")
1068 :group 'org-agenda
1069 :type 'string)
1071 (defvar org-agenda-entry-text-cleanup-hook nil
1072 "Hook that is run after basic cleanup of entry text to be shown in agenda.
1073 This cleanup is done in a temporary buffer, so the function may inspect and
1074 change the entire buffer.
1075 Some default stuff like drawers and scheduling/deadline dates will already
1076 have been removed when this is called, as will any matches for regular
1077 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
1079 (defvar org-agenda-include-inactive-timestamps nil
1080 "Non-nil means include inactive time stamps in agenda and timeline.
1081 Dynamically scoped.")
1083 (defgroup org-agenda-windows nil
1084 "Options concerning the windows used by the Agenda in Org Mode."
1085 :tag "Org Agenda Windows"
1086 :group 'org-agenda)
1088 (defcustom org-agenda-window-setup 'reorganize-frame
1089 "How the agenda buffer should be displayed.
1090 Possible values for this option are:
1092 current-window Show agenda in the current window, keeping all other windows.
1093 other-window Use `switch-to-buffer-other-window' to display agenda.
1094 reorganize-frame Show only two windows on the current frame, the current
1095 window and the agenda.
1096 other-frame Use `switch-to-buffer-other-frame' to display agenda.
1097 Also, when exiting the agenda, kill that frame.
1098 See also the variable `org-agenda-restore-windows-after-quit'."
1099 :group 'org-agenda-windows
1100 :type '(choice
1101 (const current-window)
1102 (const other-frame)
1103 (const other-window)
1104 (const reorganize-frame)))
1106 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
1107 "The min and max height of the agenda window as a fraction of frame height.
1108 The value of the variable is a cons cell with two numbers between 0 and 1.
1109 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
1110 :group 'org-agenda-windows
1111 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
1113 (defcustom org-agenda-restore-windows-after-quit nil
1114 "Non-nil means restore window configuration upon exiting agenda.
1115 Before the window configuration is changed for displaying the agenda,
1116 the current status is recorded. When the agenda is exited with
1117 `q' or `x' and this option is set, the old state is restored. If
1118 `org-agenda-window-setup' is `other-frame', the value of this
1119 option will be ignored."
1120 :group 'org-agenda-windows
1121 :type 'boolean)
1123 (defcustom org-agenda-ndays nil
1124 "Number of days to include in overview display.
1125 Should be 1 or 7.
1126 Obsolete, see `org-agenda-span'."
1127 :group 'org-agenda-daily/weekly
1128 :type '(choice (const nil)
1129 (integer)))
1131 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
1133 (defcustom org-agenda-span 'week
1134 "Number of days to include in overview display.
1135 Can be day, week, month, year, or any number of days.
1136 Custom commands can set this variable in the options section."
1137 :group 'org-agenda-daily/weekly
1138 :type '(choice (const :tag "Day" day)
1139 (const :tag "Week" week)
1140 (const :tag "Fortnight" fortnight)
1141 (const :tag "Month" month)
1142 (const :tag "Year" year)
1143 (integer :tag "Custom")))
1145 (defcustom org-agenda-start-on-weekday 1
1146 "Non-nil means start the overview always on the specified weekday.
1147 0 denotes Sunday, 1 denotes Monday, etc.
1148 When nil, always start on the current day.
1149 Custom commands can set this variable in the options section."
1150 :group 'org-agenda-daily/weekly
1151 :type '(choice (const :tag "Today" nil)
1152 (integer :tag "Weekday No.")))
1154 (defcustom org-agenda-show-all-dates t
1155 "Non-nil means `org-agenda' shows every day in the selected range.
1156 When nil, only the days which actually have entries are shown."
1157 :group 'org-agenda-daily/weekly
1158 :type 'boolean)
1160 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1161 "Format string for displaying dates in the agenda.
1162 Used by the daily/weekly agenda and by the timeline. This should be
1163 a format string understood by `format-time-string', or a function returning
1164 the formatted date as a string. The function must take a single argument,
1165 a calendar-style date list like (month day year)."
1166 :group 'org-agenda-daily/weekly
1167 :type '(choice
1168 (string :tag "Format string")
1169 (function :tag "Function")))
1171 (defun org-agenda-format-date-aligned (date)
1172 "Format a DATE string for display in the daily/weekly agenda, or timeline.
1173 This function makes sure that dates are aligned for easy reading."
1174 (require 'cal-iso)
1175 (let* ((dayname (calendar-day-name date))
1176 (day (cadr date))
1177 (day-of-week (calendar-day-of-week date))
1178 (month (car date))
1179 (monthname (calendar-month-name month))
1180 (year (nth 2 date))
1181 (iso-week (org-days-to-iso-week
1182 (calendar-absolute-from-gregorian date)))
1183 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1184 (1- year))
1185 ((and (= month 12) (<= iso-week 1))
1186 (1+ year))
1187 (t year)))
1188 (weekstring (if (= day-of-week 1)
1189 (format " W%02d" iso-week)
1190 "")))
1191 (format "%-10s %2d %s %4d%s"
1192 dayname day monthname year weekstring)))
1194 (defcustom org-agenda-time-leading-zero nil
1195 "Non-nil means use leading zero for military times in agenda.
1196 For example, 9:30am would become 09:30 rather than 9:30."
1197 :group 'org-agenda-daily/weekly
1198 :version "24.1"
1199 :type 'boolean)
1201 (defcustom org-agenda-timegrid-use-ampm nil
1202 "When set, show AM/PM style timestamps on the timegrid."
1203 :group 'org-agenda
1204 :version "24.1"
1205 :type 'boolean)
1207 (defun org-agenda-time-of-day-to-ampm (time)
1208 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1209 (let* ((hour-number (string-to-number (substring time 0 -3)))
1210 (minute (substring time -2))
1211 (ampm "am"))
1212 (cond
1213 ((equal hour-number 12)
1214 (setq ampm "pm"))
1215 ((> hour-number 12)
1216 (setq ampm "pm")
1217 (setq hour-number (- hour-number 12))))
1218 (concat
1219 (if org-agenda-time-leading-zero
1220 (format "%02d" hour-number)
1221 (format "%02s" (number-to-string hour-number)))
1222 ":" minute ampm)))
1224 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1225 "Conditionally convert TIME to AM/PM format based on `org-agenda-timegrid-use-ampm'."
1226 (if org-agenda-timegrid-use-ampm
1227 (org-agenda-time-of-day-to-ampm time)
1228 time))
1230 (defcustom org-agenda-weekend-days '(6 0)
1231 "Which days are weekend?
1232 These days get the special face `org-agenda-date-weekend' in the agenda
1233 and timeline buffers."
1234 :group 'org-agenda-daily/weekly
1235 :type '(set :greedy t
1236 (const :tag "Monday" 1)
1237 (const :tag "Tuesday" 2)
1238 (const :tag "Wednesday" 3)
1239 (const :tag "Thursday" 4)
1240 (const :tag "Friday" 5)
1241 (const :tag "Saturday" 6)
1242 (const :tag "Sunday" 0)))
1244 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1245 "Non-nil means jump to today when moving a past date forward in time.
1246 When using S-right in the agenda to move a a date forward, and the date
1247 stamp currently points to the past, the first key press will move it
1248 to today. WHen nil, just move one day forward even if the date stays
1249 in the past."
1250 :group 'org-agenda-daily/weekly
1251 :version "24.1"
1252 :type 'boolean)
1254 (defcustom org-agenda-include-diary nil
1255 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1256 Custom commands can set this variable in the options section."
1257 :group 'org-agenda-daily/weekly
1258 :type 'boolean)
1260 (defcustom org-agenda-include-deadlines t
1261 "If non-nil, include entries within their deadline warning period.
1262 Custom commands can set this variable in the options section."
1263 :group 'org-agenda-daily/weekly
1264 :version "24.1"
1265 :type 'boolean)
1267 (defcustom org-agenda-repeating-timestamp-show-all t
1268 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1269 When set to a list of strings, only show occurrences of repeating
1270 stamps for these TODO keywords. When nil, only one occurrence is
1271 shown, either today or the nearest into the future."
1272 :group 'org-agenda-daily/weekly
1273 :type '(choice
1274 (const :tag "Show repeating stamps" t)
1275 (repeat :tag "Show repeating stamps for these TODO keywords"
1276 (string :tag "TODO Keyword"))
1277 (const :tag "Don't show repeating stamps" nil)))
1279 (defcustom org-scheduled-past-days 10000
1280 "Number of days to continue listing scheduled items not marked DONE.
1281 When an item is scheduled on a date, it shows up in the agenda on this
1282 day and will be listed until it is marked done for the number of days
1283 given here."
1284 :group 'org-agenda-daily/weekly
1285 :type 'integer)
1287 (defcustom org-agenda-log-mode-items '(closed clock)
1288 "List of items that should be shown in agenda log mode.
1289 This list may contain the following symbols:
1291 closed Show entries that have been closed on that day.
1292 clock Show entries that have received clocked time on that day.
1293 state Show all logged state changes.
1294 Note that instead of changing this variable, you can also press `C-u l' in
1295 the agenda to display all available LOG items temporarily."
1296 :group 'org-agenda-daily/weekly
1297 :type '(set :greedy t (const closed) (const clock) (const state)))
1299 (defcustom org-agenda-clock-consistency-checks
1300 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1301 :gap-ok-around ("4:00")
1302 :default-face ((:background "DarkRed") (:foreground "white"))
1303 :overlap-face nil :gap-face nil :no-end-time-face nil
1304 :long-face nil :short-face nil)
1305 "This is a property list, with the following keys:
1307 :max-duration Mark clocking chunks that are longer than this time.
1308 This is a time string like \"HH:MM\", or the number
1309 of minutes as an integer.
1311 :min-duration Mark clocking chunks that are shorter that this.
1312 This is a time string like \"HH:MM\", or the number
1313 of minutes as an integer.
1315 :max-gap Mark gaps between clocking chunks that are longer than
1316 this duration. A number of minutes, or a string
1317 like \"HH:MM\".
1319 :gap-ok-around List of times during the day which are usually not working
1320 times. When a gap is detected, but the gap contains any
1321 of these times, the gap is *not* reported. For example,
1322 if this is (\"4:00\" \"13:00\") then gaps that contain
1323 4:00 in the morning (i.e. the night) and 13:00
1324 (i.e. a typical lunch time) do not cause a warning.
1325 You should have at least one time during the night in this
1326 list, or otherwise the first task each morning will trigger
1327 a warning because it follows a long gap.
1329 Furthermore, the following properties can be used to define faces for
1330 issue display.
1332 :default-face the default face, if the specific face is undefined
1333 :overlap-face face for overlapping clocks
1334 :gap-face face for gaps between clocks
1335 :no-end-time-face face for incomplete clocks
1336 :long-face face for clock intervals that are too long
1337 :short-face face for clock intervals that are too short"
1338 :group 'org-agenda-daily/weekly
1339 :group 'org-clock
1340 :version "24.1"
1341 :type 'plist)
1343 (defcustom org-agenda-log-mode-add-notes t
1344 "Non-nil means add first line of notes to log entries in agenda views.
1345 If a log item like a state change or a clock entry is associated with
1346 notes, the first line of these notes will be added to the entry in the
1347 agenda display."
1348 :group 'org-agenda-daily/weekly
1349 :type 'boolean)
1351 (defcustom org-agenda-start-with-log-mode nil
1352 "The initial value of log-mode in a newly created agenda window.
1353 See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
1354 explanations on the possible values."
1355 :group 'org-agenda-startup
1356 :group 'org-agenda-daily/weekly
1357 :type '(choice (const :tag "Don't show log items" nil)
1358 (const :tag "Show only log items" only)
1359 (const :tag "Show all possible log items" clockcheck)
1360 (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
1361 (choice (const :tag "Show closed log items" closed)
1362 (const :tag "Show clocked log items" clock)
1363 (const :tag "Show all logged state changes" state)))))
1365 (defcustom org-agenda-start-with-clockreport-mode nil
1366 "The initial value of clockreport-mode in a newly created agenda window."
1367 :group 'org-agenda-startup
1368 :group 'org-agenda-daily/weekly
1369 :type 'boolean)
1371 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1372 "Property list with parameters for the clocktable in clockreport mode.
1373 This is the display mode that shows a clock table in the daily/weekly
1374 agenda, the properties for this dynamic block can be set here.
1375 The usual clocktable parameters are allowed here, but you cannot set
1376 the properties :name, :tstart, :tend, :block, and :scope - these will
1377 be overwritten to make sure the content accurately reflects the
1378 current display in the agenda."
1379 :group 'org-agenda-daily/weekly
1380 :type 'plist)
1382 (defcustom org-agenda-search-view-always-boolean nil
1383 "Non-nil means the search string is interpreted as individual parts.
1385 The search string for search view can either be interpreted as a phrase,
1386 or as a list of snippets that define a boolean search for a number of
1387 strings.
1389 When this is non-nil, the string will be split on whitespace, and each
1390 snippet will be searched individually, and all must match in order to
1391 select an entry. A snippet is then a single string of non-white
1392 characters, or a string in double quotes, or a regexp in {} braces.
1393 If a snippet is preceded by \"-\", the snippet must *not* match.
1394 \"+\" is syntactic sugar for positive selection. Each snippet may
1395 be found as a full word or a partial word, but see the variable
1396 `org-agenda-search-view-force-full-words'.
1398 When this is nil, search will look for the entire search phrase as one,
1399 with each space character matching any amount of whitespace, including
1400 line breaks.
1402 Even when this is nil, you can still switch to Boolean search dynamically
1403 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1404 is a regexp marked with braces like \"{abc}\", this will also switch to
1405 boolean search."
1406 :group 'org-agenda-search-view
1407 :version "24.1"
1408 :type 'boolean)
1410 (org-defvaralias 'org-agenda-search-view-search-words-only
1411 'org-agenda-search-view-always-boolean)
1413 (defcustom org-agenda-search-view-force-full-words nil
1414 "Non-nil means, search words must be matches as complete words.
1415 When nil, they may also match part of a word."
1416 :group 'org-agenda-search-view
1417 :version "24.1"
1418 :type 'boolean)
1420 (defcustom org-agenda-search-view-max-outline-level 0
1421 "Maximum outline level to display in search view.
1422 E.g. when this is set to 1, the search view will only
1423 show headlines of level 1. When set to 0, the default
1424 value, don't limit agenda view by outline level."
1425 :group 'org-agenda-search-view
1426 :version "24.4"
1427 :package-version '(Org . "8.3")
1428 :type 'integer)
1430 (defgroup org-agenda-time-grid nil
1431 "Options concerning the time grid in the Org-mode Agenda."
1432 :tag "Org Agenda Time Grid"
1433 :group 'org-agenda)
1435 (defcustom org-agenda-search-headline-for-time t
1436 "Non-nil means search headline for a time-of-day.
1437 If the headline contains a time-of-day in one format or another, it will
1438 be used to sort the entry into the time sequence of items for a day.
1439 Some people have time stamps in the headline that refer to the creation
1440 time or so, and then this produces an unwanted side effect. If this is
1441 the case for your, use this variable to turn off searching the headline
1442 for a time."
1443 :group 'org-agenda-time-grid
1444 :type 'boolean)
1446 (defcustom org-agenda-use-time-grid t
1447 "Non-nil means show a time grid in the agenda schedule.
1448 A time grid is a set of lines for specific times (like every two hours between
1449 8:00 and 20:00). The items scheduled for a day at specific times are
1450 sorted in between these lines.
1451 For details about when the grid will be shown, and what it will look like, see
1452 the variable `org-agenda-time-grid'."
1453 :group 'org-agenda-time-grid
1454 :type 'boolean)
1456 (defcustom org-agenda-time-grid
1457 '((daily today require-timed)
1458 "----------------"
1459 (800 1000 1200 1400 1600 1800 2000))
1461 "The settings for time grid for agenda display.
1462 This is a list of three items. The first item is again a list. It contains
1463 symbols specifying conditions when the grid should be displayed:
1465 daily if the agenda shows a single day
1466 weekly if the agenda shows an entire week
1467 today show grid on current date, independent of daily/weekly display
1468 require-timed show grid only if at least one item has a time specification
1470 The second item is a string which will be placed behind the grid time.
1472 The third item is a list of integers, indicating the times that should have
1473 a grid line."
1474 :group 'org-agenda-time-grid
1475 :type
1476 '(list
1477 (set :greedy t :tag "Grid Display Options"
1478 (const :tag "Show grid in single day agenda display" daily)
1479 (const :tag "Show grid in weekly agenda display" weekly)
1480 (const :tag "Always show grid for today" today)
1481 (const :tag "Show grid only if any timed entries are present"
1482 require-timed)
1483 (const :tag "Skip grid times already present in an entry"
1484 remove-match))
1485 (string :tag "Grid String")
1486 (repeat :tag "Grid Times" (integer :tag "Time"))))
1488 (defcustom org-agenda-show-current-time-in-grid t
1489 "Non-nil means show the current time in the time grid."
1490 :group 'org-agenda-time-grid
1491 :version "24.1"
1492 :type 'boolean)
1494 (defcustom org-agenda-current-time-string
1495 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1496 "The string for the current time marker in the agenda."
1497 :group 'org-agenda-time-grid
1498 :version "24.1"
1499 :type 'string)
1501 (defgroup org-agenda-sorting nil
1502 "Options concerning sorting in the Org-mode Agenda."
1503 :tag "Org Agenda Sorting"
1504 :group 'org-agenda)
1506 (defcustom org-agenda-sorting-strategy
1507 '((agenda habit-down time-up priority-down category-keep)
1508 (todo priority-down category-keep)
1509 (tags priority-down category-keep)
1510 (search category-keep))
1511 "Sorting structure for the agenda items of a single day.
1512 This is a list of symbols which will be used in sequence to determine
1513 if an entry should be listed before another entry. The following
1514 symbols are recognized:
1516 time-up Put entries with time-of-day indications first, early first
1517 time-down Put entries with time-of-day indications first, late first
1518 timestamp-up Sort by any timestamp, early first
1519 timestamp-down Sort by any timestamp, late first
1520 scheduled-up Sort by scheduled timestamp, early first
1521 scheduled-down Sort by scheduled timestamp, late first
1522 deadline-up Sort by deadline timestamp, early first
1523 deadline-down Sort by deadline timestamp, late first
1524 ts-up Sort by active timestamp, early first
1525 ts-down Sort by active timestamp, late first
1526 tsia-up Sort by inactive timestamp, early first
1527 tsia-down Sort by inactive timestamp, late first
1528 category-keep Keep the default order of categories, corresponding to the
1529 sequence in `org-agenda-files'.
1530 category-up Sort alphabetically by category, A-Z.
1531 category-down Sort alphabetically by category, Z-A.
1532 tag-up Sort alphabetically by last tag, A-Z.
1533 tag-down Sort alphabetically by last tag, Z-A.
1534 priority-up Sort numerically by priority, high priority last.
1535 priority-down Sort numerically by priority, high priority first.
1536 todo-state-up Sort by todo state, tasks that are done last.
1537 todo-state-down Sort by todo state, tasks that are done first.
1538 effort-up Sort numerically by estimated effort, high effort last.
1539 effort-down Sort numerically by estimated effort, high effort first.
1540 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1541 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1542 habit-up Put entries that are habits first
1543 habit-down Put entries that are habits last
1544 alpha-up Sort headlines alphabetically
1545 alpha-down Sort headlines alphabetically, reversed
1547 The different possibilities will be tried in sequence, and testing stops
1548 if one comparison returns a \"not-equal\". For example, the default
1549 '(time-up category-keep priority-down)
1550 means: Pull out all entries having a specified time of day and sort them,
1551 in order to make a time schedule for the current day the first thing in the
1552 agenda listing for the day. Of the entries without a time indication, keep
1553 the grouped in categories, don't sort the categories, but keep them in
1554 the sequence given in `org-agenda-files'. Within each category sort by
1555 priority.
1557 Leaving out `category-keep' would mean that items will be sorted across
1558 categories by priority.
1560 Instead of a single list, this can also be a set of list for specific
1561 contents, with a context symbol in the car of the list, any of
1562 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1564 Custom commands can bind this variable in the options section."
1565 :group 'org-agenda-sorting
1566 :type `(choice
1567 (repeat :tag "General" ,org-sorting-choice)
1568 (list :tag "Individually"
1569 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1570 (repeat ,org-sorting-choice))
1571 (cons (const :tag "Strategy for TODO lists" todo)
1572 (repeat ,org-sorting-choice))
1573 (cons (const :tag "Strategy for Tags matches" tags)
1574 (repeat ,org-sorting-choice))
1575 (cons (const :tag "Strategy for search matches" search)
1576 (repeat ,org-sorting-choice)))))
1578 (defcustom org-agenda-cmp-user-defined nil
1579 "A function to define the comparison `user-defined'.
1580 This function must receive two arguments, agenda entry a and b.
1581 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1582 the user comparison, return nil.
1583 When this is defined, you can make `user-defined-up' and `user-defined-down'
1584 part of an agenda sorting strategy."
1585 :group 'org-agenda-sorting
1586 :type 'symbol)
1588 (defcustom org-sort-agenda-notime-is-late t
1589 "Non-nil means items without time are considered late.
1590 This is only relevant for sorting. When t, items which have no explicit
1591 time like 15:30 will be considered as 99:01, i.e. later than any items which
1592 do have a time. When nil, the default time is before 0:00. You can use this
1593 option to decide if the schedule for today should come before or after timeless
1594 agenda entries."
1595 :group 'org-agenda-sorting
1596 :type 'boolean)
1598 (defcustom org-sort-agenda-noeffort-is-high t
1599 "Non-nil means items without effort estimate are sorted as high effort.
1600 This also applies when filtering an agenda view with respect to the
1601 < or > effort operator. Then, tasks with no effort defined will be treated
1602 as tasks with high effort.
1603 When nil, such items are sorted as 0 minutes effort."
1604 :group 'org-agenda-sorting
1605 :type 'boolean)
1607 (defgroup org-agenda-line-format nil
1608 "Options concerning the entry prefix in the Org-mode agenda display."
1609 :tag "Org Agenda Line Format"
1610 :group 'org-agenda)
1612 (defcustom org-agenda-prefix-format
1613 '((agenda . " %i %-12:c%?-12t% s")
1614 (timeline . " % s")
1615 (todo . " %i %-12:c")
1616 (tags . " %i %-12:c")
1617 (search . " %i %-12:c"))
1618 "Format specifications for the prefix of items in the agenda views.
1619 An alist with five entries, each for the different agenda types. The
1620 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1621 The values are format strings.
1623 This format works similar to a printf format, with the following meaning:
1625 %c the category of the item, \"Diary\" for entries from the diary,
1626 or as given by the CATEGORY keyword or derived from the file name
1627 %e the effort required by the item
1628 %l the level of the item (insert X space(s) if item is of level X)
1629 %i the icon category of the item, see `org-agenda-category-icon-alist'
1630 %T the last tag of the item (ignore inherited tags, which come first)
1631 %t the HH:MM time-of-day specification if one applies to the entry
1632 %s Scheduling/Deadline information, a short string
1633 %b show breadcrumbs, i.e., the names of the higher levels
1634 %(expression) Eval EXPRESSION and replace the control string
1635 by the result
1637 All specifiers work basically like the standard `%s' of printf, but may
1638 contain two additional characters: a question mark just after the `%'
1639 and a whitespace/punctuation character just before the final letter.
1641 If the first character after `%' is a question mark, the entire field
1642 will only be included if the corresponding value applies to the current
1643 entry. This is useful for fields which should have fixed width when
1644 present, but zero width when absent. For example, \"%?-12t\" will
1645 result in a 12 character time field if a time of the day is specified,
1646 but will completely disappear in entries which do not contain a time.
1648 If there is punctuation or whitespace character just before the
1649 final format letter, this character will be appended to the field
1650 value if the value is not empty. For example, the format
1651 \"%-12:c\" leads to \"Diary: \" if the category is \"Diary\". If
1652 the category is empty, no additional colon is inserted.
1654 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1655 which means:
1657 - Indent the line with two space characters
1658 - Give the category a 12 chars wide field, padded with whitespace on
1659 the right (because of `-'). Append a colon if there is a category
1660 (because of `:').
1661 - If there is a time-of-day, put it into a 12 chars wide field. If no
1662 time, don't put in an empty field, just skip it (because of '?').
1663 - Finally, put the scheduling information.
1665 See also the variables `org-agenda-remove-times-when-in-prefix' and
1666 `org-agenda-remove-tags'.
1668 Custom commands can set this variable in the options section."
1669 :type '(choice
1670 (string :tag "General format")
1671 (list :greedy t :tag "View dependent"
1672 (cons (const agenda) (string :tag "Format"))
1673 (cons (const timeline) (string :tag "Format"))
1674 (cons (const todo) (string :tag "Format"))
1675 (cons (const tags) (string :tag "Format"))
1676 (cons (const search) (string :tag "Format"))))
1677 :group 'org-agenda-line-format)
1679 (defvar org-prefix-format-compiled nil
1680 "The compiled prefix format and associated variables.
1681 This is a list where first element is a list of variable bindings, and second
1682 element is the compiled format expression. See the variable
1683 `org-agenda-prefix-format'.")
1685 (defcustom org-agenda-todo-keyword-format "%-1s"
1686 "Format for the TODO keyword in agenda lines.
1687 Set this to something like \"%-12s\" if you want all TODO keywords
1688 to occupy a fixed space in the agenda display."
1689 :group 'org-agenda-line-format
1690 :type 'string)
1692 (defcustom org-agenda-diary-sexp-prefix nil
1693 "A regexp that matches part of a diary sexp entry
1694 which should be treated as scheduling/deadline information in
1695 `org-agenda'.
1697 For example, you can use this to extract the `diary-remind-message' from
1698 `diary-remind' entries."
1699 :group 'org-agenda-line-format
1700 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1702 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1703 "Text preceding timerange entries in the agenda view.
1704 This is a list with two strings. The first applies when the range
1705 is entirely on one day. The second applies if the range spans several days.
1706 The strings may have two \"%d\" format specifiers which will be filled
1707 with the sequence number of the days, and the total number of days in the
1708 range, respectively."
1709 :group 'org-agenda-line-format
1710 :type '(list
1711 (string :tag "Deadline today ")
1712 (choice :tag "Deadline relative"
1713 (string :tag "Format string")
1714 (function))))
1716 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1717 "Text preceding scheduled items in the agenda view.
1718 This is a list with two strings. The first applies when the item is
1719 scheduled on the current day. The second applies when it has been scheduled
1720 previously, it may contain a %d indicating that this is the nth time that
1721 this item is scheduled, due to automatic rescheduling of unfinished items
1722 for the following day. So this number is one larger than the number of days
1723 that passed since this item was scheduled first."
1724 :group 'org-agenda-line-format
1725 :version "24.4"
1726 :package-version '(Org . "8.0")
1727 :type '(list
1728 (string :tag "Scheduled today ")
1729 (string :tag "Scheduled previously")))
1731 (defcustom org-agenda-inactive-leader "["
1732 "Text preceding item pulled into the agenda by inactive time stamps.
1733 These entries are added to the agenda when pressing \"[\"."
1734 :group 'org-agenda-line-format
1735 :version "24.1"
1736 :type 'string)
1738 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: " "%2d d. ago: ")
1739 "Text preceding deadline items in the agenda view.
1740 This is a list with three strings. The first applies when the item has its
1741 deadline on the current day. The second applies when the deadline is in the
1742 future, the third one when it is in the past. The strings may contain %d
1743 to capture the number of days."
1744 :group 'org-agenda-line-format
1745 :version "24.4"
1746 :package-version '(Org . "8.0")
1747 :type '(list
1748 (string :tag "Deadline today ")
1749 (string :tag "Deadline in the future ")
1750 (string :tag "Deadline in the past ")))
1752 (defcustom org-agenda-remove-times-when-in-prefix t
1753 "Non-nil means remove duplicate time specifications in agenda items.
1754 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1755 time-of-day specification in a headline or diary entry is extracted and
1756 placed into the prefix. If this option is non-nil, the original specification
1757 \(a timestamp or -range, or just a plain time(range) specification like
1758 11:30-4pm) will be removed for agenda display. This makes the agenda less
1759 cluttered.
1760 The option can be t or nil. It may also be the symbol `beg', indicating
1761 that the time should only be removed when it is located at the beginning of
1762 the headline/diary entry."
1763 :group 'org-agenda-line-format
1764 :type '(choice
1765 (const :tag "Always" t)
1766 (const :tag "Never" nil)
1767 (const :tag "When at beginning of entry" beg)))
1769 (defcustom org-agenda-remove-timeranges-from-blocks nil
1770 "Non-nil means remove time ranges specifications in agenda
1771 items that span on several days."
1772 :group 'org-agenda-line-format
1773 :version "24.1"
1774 :type 'boolean)
1776 (defcustom org-agenda-default-appointment-duration nil
1777 "Default duration for appointments that only have a starting time.
1778 When nil, no duration is specified in such cases.
1779 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1780 :group 'org-agenda-line-format
1781 :type '(choice
1782 (integer :tag "Minutes")
1783 (const :tag "No default duration")))
1785 (defcustom org-agenda-show-inherited-tags t
1786 "Non-nil means show inherited tags in each agenda line.
1788 When this option is set to 'always, it take precedences over
1789 `org-agenda-use-tag-inheritance' and inherited tags are shown
1790 in every agenda.
1792 When this option is set to t (the default), inherited tags are
1793 shown when they are available, i.e. when the value of
1794 `org-agenda-use-tag-inheritance' has been taken into account.
1796 This can be set to a list of agenda types in which the agenda
1797 must display the inherited tags. Available types are 'todo,
1798 'agenda, 'search and 'timeline.
1800 When set to nil, never show inherited tags in agenda lines."
1801 :group 'org-agenda-line-format
1802 :group 'org-agenda
1803 :version "24.3"
1804 :type '(choice
1805 (const :tag "Show inherited tags when available" t)
1806 (const :tag "Always show inherited tags" always)
1807 (repeat :tag "Show inherited tags only in selected agenda types"
1808 (symbol :tag "Agenda type"))))
1810 (defcustom org-agenda-use-tag-inheritance '(todo search timeline agenda)
1811 "List of agenda view types where to use tag inheritance.
1813 In tags/tags-todo/tags-tree agenda views, tag inheritance is
1814 controlled by `org-use-tag-inheritance'. In other agenda types,
1815 `org-use-tag-inheritance' is not used for the selection of the
1816 agenda entries. Still, you may want the agenda to be aware of
1817 the inherited tags anyway, e.g. for later tag filtering.
1819 Allowed value are 'todo, 'search, 'timeline and 'agenda.
1821 This variable has no effect if `org-agenda-show-inherited-tags'
1822 is set to 'always. In that case, the agenda is aware of those
1823 tags.
1825 The default value sets tags in every agenda type. Setting this
1826 option to nil will speed up non-tags agenda view a lot."
1827 :group 'org-agenda
1828 :version "24.3"
1829 :type '(choice
1830 (const :tag "Use tag inheritance in all agenda types" t)
1831 (repeat :tag "Use tag inheritance in selected agenda types"
1832 (symbol :tag "Agenda type"))))
1834 (defcustom org-agenda-hide-tags-regexp nil
1835 "Regular expression used to filter away specific tags in agenda views.
1836 This means that these tags will be present, but not be shown in the agenda
1837 line. Secondary filtering will still work on the hidden tags.
1838 Nil means don't hide any tags."
1839 :group 'org-agenda-line-format
1840 :type '(choice
1841 (const :tag "Hide none" nil)
1842 (string :tag "Regexp ")))
1844 (defcustom org-agenda-remove-tags nil
1845 "Non-nil means remove the tags from the headline copy in the agenda.
1846 When this is the symbol `prefix', only remove tags when
1847 `org-agenda-prefix-format' contains a `%T' specifier."
1848 :group 'org-agenda-line-format
1849 :type '(choice
1850 (const :tag "Always" t)
1851 (const :tag "Never" nil)
1852 (const :tag "When prefix format contains %T" prefix)))
1854 (org-defvaralias 'org-agenda-remove-tags-when-in-prefix
1855 'org-agenda-remove-tags)
1857 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1858 "Shift tags in agenda items to this column.
1859 If this number is positive, it specifies the column. If it is negative,
1860 it means that the tags should be flushright to that column. For example,
1861 -80 works well for a normal 80 character screen."
1862 :group 'org-agenda-line-format
1863 :type 'integer)
1865 (org-defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column)
1867 (defcustom org-agenda-fontify-priorities 'cookies
1868 "Non-nil means highlight low and high priorities in agenda.
1869 When t, the highest priority entries are bold, lowest priority italic.
1870 However, settings in `org-priority-faces' will overrule these faces.
1871 When this variable is the symbol `cookies', only fontify the
1872 cookies, not the entire task.
1873 This may also be an association list of priority faces, whose
1874 keys are the character values of `org-highest-priority',
1875 `org-default-priority', and `org-lowest-priority' (the default values
1876 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1877 color as a string, or a list like `(:background \"Red\")'.
1878 If it is a color, the variable `org-faces-easy-properties'
1879 determines if it is a foreground or a background color."
1880 :group 'org-agenda-line-format
1881 :type '(choice
1882 (const :tag "Never" nil)
1883 (const :tag "Defaults" t)
1884 (const :tag "Cookies only" cookies)
1885 (repeat :tag "Specify"
1886 (list (character :tag "Priority" :value ?A)
1887 (choice :tag "Face "
1888 (string :tag "Color")
1889 (sexp :tag "Face"))))))
1891 (defcustom org-agenda-day-face-function nil
1892 "Function called to determine what face should be used to display a day.
1893 The only argument passed to that function is the day. It should
1894 returns a face, or nil if does not want to specify a face and let
1895 the normal rules apply."
1896 :group 'org-agenda-line-format
1897 :version "24.1"
1898 :type '(choice (const nil) (function)))
1900 (defcustom org-agenda-category-icon-alist nil
1901 "Alist of category icon to be displayed in agenda views.
1903 Each entry should have the following format:
1905 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1907 Where CATEGORY-REGEXP is a regexp matching the categories where
1908 the icon should be displayed.
1909 FILE-OR-DATA either a file path or a string containing image data.
1911 The other fields can be omitted safely if not needed:
1912 TYPE indicates the image type.
1913 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1914 image data.
1915 PROPS are additional image attributes to assign to the image,
1916 like, e.g. `:ascent center'.
1918 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1920 If you want to set the display properties yourself, just put a
1921 list as second element:
1923 (CATEGORY-REGEXP (MY PROPERTY LIST))
1925 For example, to display a 16px horizontal space for Emacs
1926 category, you can use:
1928 (\"Emacs\" '(space . (:width (16))))"
1929 :group 'org-agenda-line-format
1930 :version "24.1"
1931 :type '(alist :key-type (string :tag "Regexp matching category")
1932 :value-type (choice (list :tag "Icon"
1933 (string :tag "File or data")
1934 (symbol :tag "Type")
1935 (boolean :tag "Data?")
1936 (repeat :tag "Extra image properties" :inline t symbol))
1937 (list :tag "Display properties" sexp))))
1939 (defgroup org-agenda-column-view nil
1940 "Options concerning column view in the agenda."
1941 :tag "Org Agenda Column View"
1942 :group 'org-agenda)
1944 (defcustom org-agenda-columns-show-summaries t
1945 "Non-nil means show summaries for columns displayed in the agenda view."
1946 :group 'org-agenda-column-view
1947 :type 'boolean)
1949 (defcustom org-agenda-columns-compute-summary-properties t
1950 "Non-nil means recompute all summary properties before column view.
1951 When column view in the agenda is listing properties that have a summary
1952 operator, it can go to all relevant buffers and recompute the summaries
1953 there. This can mean overhead for the agenda column view, but is necessary
1954 to have thing up to date.
1955 As a special case, a CLOCKSUM property also makes sure that the clock
1956 computations are current."
1957 :group 'org-agenda-column-view
1958 :type 'boolean)
1960 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1961 "Non-nil means the duration of an appointment will add to day effort.
1962 The property to which appointment durations will be added is the one given
1963 in the option `org-effort-property'. If an appointment does not have
1964 an end time, `org-agenda-default-appointment-duration' will be used. If that
1965 is not set, an appointment without end time will not contribute to the time
1966 estimate."
1967 :group 'org-agenda-column-view
1968 :type 'boolean)
1970 (defcustom org-agenda-auto-exclude-function nil
1971 "A function called with a tag to decide if it is filtered on '/ RET'.
1972 The sole argument to the function, which is called once for each
1973 possible tag, is a string giving the name of the tag. The
1974 function should return either nil if the tag should be included
1975 as normal, or \"-<TAG>\" to exclude the tag.
1976 Note that for the purpose of tag filtering, only the lower-case version of
1977 all tags will be considered, so that this function will only ever see
1978 the lower-case version of all tags."
1979 :group 'org-agenda
1980 :type '(choice (const nil) (function)))
1982 (defcustom org-agenda-bulk-custom-functions nil
1983 "Alist of characters and custom functions for bulk actions.
1984 For example, this value makes those two functions available:
1986 '((?R set-category)
1987 (?C bulk-cut))
1989 With selected entries in an agenda buffer, `B R' will call
1990 the custom function `set-category' on the selected entries.
1991 Note that functions in this alist don't need to be quoted."
1992 :type 'alist
1993 :version "24.1"
1994 :group 'org-agenda)
1996 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
1997 "Execute BODY with point at location given by `org-hd-marker' property.
1998 If STRING is non-nil, the text property will be fetched from position 0
1999 in that string. If STRING is nil, it will be fetched from the beginning
2000 of the current line."
2001 (org-with-gensyms (marker)
2002 `(let ((,marker (get-text-property (if string 0 (point-at-bol))
2003 'org-hd-marker ,string)))
2004 (with-current-buffer (marker-buffer ,marker)
2005 (save-excursion
2006 (goto-char ,marker)
2007 ,@body)))))
2008 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
2010 (defun org-add-agenda-custom-command (entry)
2011 "Replace or add a command in `org-agenda-custom-commands'.
2012 This is mostly for hacking and trying a new command - once the command
2013 works you probably want to add it to `org-agenda-custom-commands' for good."
2014 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
2015 (if ass
2016 (setcdr ass (cdr entry))
2017 (push entry org-agenda-custom-commands))))
2019 ;;; Define the org-agenda-mode
2021 (defvar org-agenda-mode-map (make-sparse-keymap)
2022 "Keymap for `org-agenda-mode'.")
2023 (org-defvaralias 'org-agenda-keymap 'org-agenda-mode-map)
2025 (defvar org-agenda-menu) ; defined later in this file.
2026 (defvar org-agenda-restrict nil) ; defined later in this file.
2027 (defvar org-agenda-follow-mode nil)
2028 (defvar org-agenda-entry-text-mode nil)
2029 (defvar org-agenda-clockreport-mode nil)
2030 (defvar org-agenda-show-log nil)
2031 (defvar org-agenda-redo-command nil)
2032 (defvar org-agenda-query-string nil)
2033 (defvar org-agenda-mode-hook nil
2034 "Hook run after `org-agenda-mode' is turned on.
2035 The buffer is still writable when this hook is called.")
2036 (defvar org-agenda-type nil)
2037 (defvar org-agenda-force-single-file nil)
2038 (defvar org-agenda-bulk-marked-entries nil
2039 "List of markers that refer to marked entries in the agenda.")
2041 ;;; Multiple agenda buffers support
2043 (defcustom org-agenda-sticky nil
2044 "Non-nil means agenda q key will bury agenda buffers.
2045 Agenda commands will then show existing buffer instead of generating new ones.
2046 When nil, `q' will kill the single agenda buffer."
2047 :group 'org-agenda
2048 :version "24.3"
2049 :type 'boolean)
2052 ;;;###autoload
2053 (defun org-toggle-sticky-agenda (&optional arg)
2054 "Toggle `org-agenda-sticky'."
2055 (interactive "P")
2056 (let ((new-value (if arg
2057 (> (prefix-numeric-value arg) 0)
2058 (not org-agenda-sticky))))
2059 (if (equal new-value org-agenda-sticky)
2060 (and (org-called-interactively-p 'interactive)
2061 (message "Sticky agenda was already %s"
2062 (if org-agenda-sticky "enabled" "disabled")))
2063 (setq org-agenda-sticky new-value)
2064 (org-agenda-kill-all-agenda-buffers)
2065 (and (org-called-interactively-p 'interactive)
2066 (message "Sticky agenda was %s"
2067 (if org-agenda-sticky "enabled" "disabled"))))))
2069 (defvar org-agenda-buffer nil
2070 "Agenda buffer currently being generated.")
2072 (defvar org-agenda-last-prefix-arg nil)
2073 (defvar org-agenda-this-buffer-name nil)
2074 (defvar org-agenda-doing-sticky-redo nil)
2075 (defvar org-agenda-this-buffer-is-sticky nil)
2077 (defconst org-agenda-local-vars
2078 '(org-agenda-this-buffer-name
2079 org-agenda-undo-list
2080 org-agenda-pending-undo-list
2081 org-agenda-follow-mode
2082 org-agenda-entry-text-mode
2083 org-agenda-clockreport-mode
2084 org-agenda-show-log
2085 org-agenda-redo-command
2086 org-agenda-query-string
2087 org-agenda-type
2088 org-agenda-bulk-marked-entries
2089 org-agenda-undo-has-started-in
2090 org-agenda-info
2091 org-agenda-pre-window-conf
2092 org-agenda-columns-active
2093 org-agenda-tag-filter
2094 org-agenda-category-filter
2095 org-agenda-top-headline-filter
2096 org-agenda-regexp-filter
2097 org-agenda-effort-filter
2098 org-agenda-markers
2099 org-agenda-last-search-view-search-was-boolean
2100 org-agenda-filtered-by-category
2101 org-agenda-filter-form
2102 org-agenda-cycle-counter
2103 org-agenda-last-prefix-arg)
2104 "Variables that must be local in agenda buffers to allow multiple buffers.")
2106 (defun org-agenda-mode ()
2107 "Mode for time-sorted view on action items in Org-mode files.
2109 The following commands are available:
2111 \\{org-agenda-mode-map}"
2112 (interactive)
2113 (cond (org-agenda-doing-sticky-redo
2114 ;; Refreshing sticky agenda-buffer
2116 ;; Preserve the value of `org-agenda-local-vars' variables,
2117 ;; while letting `kill-all-local-variables' kill the rest
2118 (let ((save (buffer-local-variables)))
2119 (kill-all-local-variables)
2120 (mapc 'make-local-variable org-agenda-local-vars)
2121 (dolist (elem save)
2122 (let ((var (car elem))
2123 (val (cdr elem)))
2124 (when (and val
2125 (member var org-agenda-local-vars))
2126 (set var val)))))
2127 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
2128 (org-agenda-sticky
2129 ;; Creating a sticky Agenda buffer for the first time
2130 (kill-all-local-variables)
2131 (mapc 'make-local-variable org-agenda-local-vars)
2132 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
2134 ;; Creating a non-sticky agenda buffer
2135 (kill-all-local-variables)
2136 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) nil)))
2137 (setq org-agenda-undo-list nil
2138 org-agenda-pending-undo-list nil
2139 org-agenda-bulk-marked-entries nil)
2140 (setq major-mode 'org-agenda-mode)
2141 ;; Keep global-font-lock-mode from turning on font-lock-mode
2142 (org-set-local 'font-lock-global-modes (list 'not major-mode))
2143 (setq mode-name "Org-Agenda")
2144 (setq indent-tabs-mode nil)
2145 (use-local-map org-agenda-mode-map)
2146 (easy-menu-add org-agenda-menu)
2147 (if org-startup-truncated (setq truncate-lines t))
2148 (org-set-local 'line-move-visual nil)
2149 (org-add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
2150 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
2151 ;; Make sure properties are removed when copying text
2152 (org-add-hook 'filter-buffer-substring-functions
2153 (lambda (fun start end delete)
2154 (substring-no-properties (funcall fun start end delete)))
2155 nil t)
2156 (unless org-agenda-keep-modes
2157 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
2158 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
2159 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
2160 org-agenda-show-log org-agenda-start-with-log-mode))
2161 (add-to-invisibility-spec '(org-filtered))
2162 (add-to-invisibility-spec '(org-link))
2163 (easy-menu-change
2164 '("Agenda") "Agenda Files"
2165 (append
2166 (list
2167 (vector
2168 (if (get 'org-agenda-files 'org-restrict)
2169 "Restricted to single file"
2170 "Edit File List")
2171 '(org-edit-agenda-file-list)
2172 (not (get 'org-agenda-files 'org-restrict)))
2173 "--")
2174 (mapcar 'org-file-menu-entry (org-agenda-files))))
2175 (org-agenda-set-mode-name)
2176 (apply
2177 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2178 (list 'org-agenda-mode-hook)))
2180 (substitute-key-definition 'undo 'org-agenda-undo
2181 org-agenda-mode-map global-map)
2182 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2183 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2184 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2185 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2186 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2187 (org-defkey org-agenda-mode-map [(meta down)] 'org-agenda-drag-line-forward)
2188 (org-defkey org-agenda-mode-map [(meta up)] 'org-agenda-drag-line-backward)
2189 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2190 (org-defkey org-agenda-mode-map "\M-m" 'org-agenda-bulk-toggle)
2191 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2192 (org-defkey org-agenda-mode-map "\M-*" 'org-agenda-bulk-toggle-all)
2193 (org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
2194 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2195 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2196 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2197 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2198 (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2199 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2200 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2201 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2202 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2203 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2204 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2205 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2206 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2207 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2208 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2209 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2210 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2211 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2212 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2213 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2214 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2215 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2216 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2217 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2218 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2219 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2220 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2221 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2222 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2223 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2224 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2225 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2226 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2227 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2228 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2229 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2230 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2231 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2233 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2234 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2235 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2236 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2237 (while l (org-defkey org-agenda-mode-map
2238 (int-to-string (pop l)) 'digit-argument)))
2240 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2241 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2242 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2243 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2244 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2245 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2246 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2247 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2248 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2249 (org-defkey org-agenda-mode-map "g" (lambda () (interactive) (org-agenda-redo t)))
2250 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2251 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2252 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2253 'org-clock-modify-effort-estimate)
2254 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2255 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2256 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2257 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2258 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2259 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2260 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2261 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2262 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2263 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2264 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2265 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2266 (substitute-key-definition 'next-line 'org-agenda-next-line
2267 org-agenda-mode-map global-map)
2268 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2269 org-agenda-mode-map global-map)
2270 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2271 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2272 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2273 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2274 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2275 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2276 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2277 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2278 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2279 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2280 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2281 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2282 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2283 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2284 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2285 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2286 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2287 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2288 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2289 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2290 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2291 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2292 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2293 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2294 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2295 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2296 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2297 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2298 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2299 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2301 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2302 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2303 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2304 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2305 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2306 (org-defkey org-agenda-mode-map "_" 'org-agenda-filter-by-effort)
2307 (org-defkey org-agenda-mode-map "=" 'org-agenda-filter-by-regexp)
2308 (org-defkey org-agenda-mode-map "|" 'org-agenda-filter-remove-all)
2309 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
2310 (org-defkey org-agenda-mode-map "~" 'org-agenda-limit-interactively)
2311 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2312 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-headline)
2313 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2314 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2315 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2316 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2318 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2319 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2321 (define-key org-agenda-mode-map [remap forward-paragraph] 'org-agenda-forward-block)
2322 (define-key org-agenda-mode-map [remap backward-paragraph] 'org-agenda-backward-block)
2324 (when org-agenda-mouse-1-follows-link
2325 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2326 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2327 '("Agenda"
2328 ("Agenda Files")
2329 "--"
2330 ("Agenda Dates"
2331 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2332 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2333 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2334 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2335 "--"
2336 ("View"
2337 ["Day View" org-agenda-day-view
2338 :active (org-agenda-check-type nil 'agenda)
2339 :style radio :selected (eq org-agenda-current-span 'day)
2340 :keys "v d (or just d)"]
2341 ["Week View" org-agenda-week-view
2342 :active (org-agenda-check-type nil 'agenda)
2343 :style radio :selected (eq org-agenda-current-span 'week)
2344 :keys "v w"]
2345 ["Fortnight View" org-agenda-fortnight-view
2346 :active (org-agenda-check-type nil 'agenda)
2347 :style radio :selected (eq org-agenda-current-span 'fortnight)
2348 :keys "v f"]
2349 ["Month View" org-agenda-month-view
2350 :active (org-agenda-check-type nil 'agenda)
2351 :style radio :selected (eq org-agenda-current-span 'month)
2352 :keys "v m"]
2353 ["Year View" org-agenda-year-view
2354 :active (org-agenda-check-type nil 'agenda)
2355 :style radio :selected (eq org-agenda-current-span 'year)
2356 :keys "v y"]
2357 "--"
2358 ["Include Diary" org-agenda-toggle-diary
2359 :style toggle :selected org-agenda-include-diary
2360 :active (org-agenda-check-type nil 'agenda)]
2361 ["Include Deadlines" org-agenda-toggle-deadlines
2362 :style toggle :selected org-agenda-include-deadlines
2363 :active (org-agenda-check-type nil 'agenda)]
2364 ["Use Time Grid" org-agenda-toggle-time-grid
2365 :style toggle :selected org-agenda-use-time-grid
2366 :active (org-agenda-check-type nil 'agenda)]
2367 "--"
2368 ["Show clock report" org-agenda-clockreport-mode
2369 :style toggle :selected org-agenda-clockreport-mode
2370 :active (org-agenda-check-type nil 'agenda)]
2371 ["Show some entry text" org-agenda-entry-text-mode
2372 :style toggle :selected org-agenda-entry-text-mode
2373 :active t]
2374 "--"
2375 ["Show Logbook entries" org-agenda-log-mode
2376 :style toggle :selected org-agenda-show-log
2377 :active (org-agenda-check-type nil 'agenda 'timeline)
2378 :keys "v l (or just l)"]
2379 ["Include archived trees" org-agenda-archives-mode
2380 :style toggle :selected org-agenda-archives-mode :active t
2381 :keys "v a"]
2382 ["Include archive files" (org-agenda-archives-mode t)
2383 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2384 :keys "v A"]
2385 "--"
2386 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2387 ["Write view to file" org-agenda-write t]
2388 ["Rebuild buffer" org-agenda-redo t]
2389 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2390 "--"
2391 ["Show original entry" org-agenda-show t]
2392 ["Go To (other window)" org-agenda-goto t]
2393 ["Go To (this window)" org-agenda-switch-to t]
2394 ["Capture with cursor date" org-agenda-capture t]
2395 ["Follow Mode" org-agenda-follow-mode
2396 :style toggle :selected org-agenda-follow-mode :active t]
2397 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2398 "--"
2399 ("TODO"
2400 ["Cycle TODO" org-agenda-todo t]
2401 ["Next TODO set" org-agenda-todo-nextset t]
2402 ["Previous TODO set" org-agenda-todo-previousset t]
2403 ["Add note" org-agenda-add-note t])
2404 ("Archive/Refile/Delete"
2405 ["Archive default" org-agenda-archive-default t]
2406 ["Archive default" org-agenda-archive-default-with-confirmation t]
2407 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2408 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2409 ["Archive subtree" org-agenda-archive t]
2410 "--"
2411 ["Refile" org-agenda-refile t]
2412 "--"
2413 ["Delete subtree" org-agenda-kill t])
2414 ("Bulk action"
2415 ["Mark entry" org-agenda-bulk-mark t]
2416 ["Mark all" org-agenda-bulk-mark-all t]
2417 ["Unmark entry" org-agenda-bulk-unmark t]
2418 ["Unmark all" org-agenda-bulk-unmark-all :active t :keys "U"]
2419 ["Toggle mark" org-agenda-bulk-toggle t]
2420 ["Toggle all" org-agenda-bulk-toggle-all t]
2421 ["Mark regexp" org-agenda-bulk-mark-regexp t])
2422 ["Act on all marked" org-agenda-bulk-action t]
2423 "--"
2424 ("Tags and Properties"
2425 ["Show all Tags" org-agenda-show-tags t]
2426 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2427 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2428 "--"
2429 ["Column View" org-columns t])
2430 ("Deadline/Schedule"
2431 ["Schedule" org-agenda-schedule t]
2432 ["Set Deadline" org-agenda-deadline t]
2433 "--"
2434 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2435 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2436 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2437 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2438 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2439 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2440 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2441 ("Clock and Effort"
2442 ["Clock in" org-agenda-clock-in t]
2443 ["Clock out" org-agenda-clock-out t]
2444 ["Clock cancel" org-agenda-clock-cancel t]
2445 ["Goto running clock" org-clock-goto t]
2446 "--"
2447 ["Set Effort" org-agenda-set-effort t]
2448 ["Change clocked effort" org-clock-modify-effort-estimate
2449 (org-clock-is-active)])
2450 ("Priority"
2451 ["Set Priority" org-agenda-priority t]
2452 ["Increase Priority" org-agenda-priority-up t]
2453 ["Decrease Priority" org-agenda-priority-down t]
2454 ["Show Priority" org-show-priority t])
2455 ("Calendar/Diary"
2456 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2457 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2458 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2459 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2460 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2461 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2462 "--"
2463 ["Create iCalendar File" org-icalendar-combine-agenda-files t])
2464 "--"
2465 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2466 "--"
2467 ("MobileOrg"
2468 ["Push Files and Views" org-mobile-push t]
2469 ["Get Captured and Flagged" org-mobile-pull t]
2470 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2471 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2472 "--"
2473 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2474 "--"
2475 ["Quit" org-agenda-quit t]
2476 ["Exit and Release Buffers" org-agenda-exit t]
2479 ;;; Agenda undo
2481 (defvar org-agenda-allow-remote-undo t
2482 "Non-nil means allow remote undo from the agenda buffer.")
2483 (defvar org-agenda-undo-has-started-in nil
2484 "Buffers that have already seen `undo-start' in the current undo sequence.")
2486 (defun org-agenda-undo ()
2487 "Undo a remote editing step in the agenda.
2488 This undoes changes both in the agenda buffer and in the remote buffer
2489 that have been changed along."
2490 (interactive)
2491 (or org-agenda-allow-remote-undo
2492 (user-error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2493 (if (not (eq this-command last-command))
2494 (setq org-agenda-undo-has-started-in nil
2495 org-agenda-pending-undo-list org-agenda-undo-list))
2496 (if (not org-agenda-pending-undo-list)
2497 (user-error "No further undo information"))
2498 (let* ((entry (pop org-agenda-pending-undo-list))
2499 buf line cmd rembuf)
2500 (setq cmd (pop entry) line (pop entry))
2501 (setq rembuf (nth 2 entry))
2502 (org-with-remote-undo rembuf
2503 (while (bufferp (setq buf (pop entry)))
2504 (if (pop entry)
2505 (with-current-buffer buf
2506 (let ((last-undo-buffer buf)
2507 (inhibit-read-only t))
2508 (unless (memq buf org-agenda-undo-has-started-in)
2509 (push buf org-agenda-undo-has-started-in)
2510 (make-local-variable 'pending-undo-list)
2511 (undo-start))
2512 (while (and pending-undo-list
2513 (listp pending-undo-list)
2514 (not (car pending-undo-list)))
2515 (pop pending-undo-list))
2516 (undo-more 1))))))
2517 (org-goto-line line)
2518 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2520 (defun org-verify-change-for-undo (l1 l2)
2521 "Verify that a real change occurred between the undo lists L1 and L2."
2522 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2523 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2524 (not (eq l1 l2)))
2526 ;;; Agenda dispatch
2528 (defvar org-agenda-restrict-begin (make-marker))
2529 (defvar org-agenda-restrict-end (make-marker))
2530 (defvar org-agenda-last-dispatch-buffer nil)
2531 (defvar org-agenda-overriding-restriction nil)
2533 (defcustom org-agenda-custom-commands-contexts nil
2534 "Alist of custom agenda keys and contextual rules.
2536 For example, if you have a custom agenda command \"p\" and you
2537 want this command to be accessible only from plain text files,
2538 use this:
2540 '((\"p\" ((in-file . \"\\\\.txt\\\\'\"))))
2542 Here are the available contexts definitions:
2544 in-file: command displayed only in matching files
2545 in-mode: command displayed only in matching modes
2546 not-in-file: command not displayed in matching files
2547 not-in-mode: command not displayed in matching modes
2548 in-buffer: command displayed only in matching buffers
2549 not-in-buffer: command not displayed in matching buffers
2550 [function]: a custom function taking no argument
2552 If you define several checks, the agenda command will be
2553 accessible if there is at least one valid check.
2555 You can also bind a key to another agenda custom command
2556 depending on contextual rules.
2558 '((\"p\" \"q\" ((in-file . \"\\\\.txt\\\\'\"))))
2560 Here it means: in .txt files, use \"p\" as the key for the
2561 agenda command otherwise associated with \"q\". (The command
2562 originally associated with \"q\" is not displayed to avoid
2563 duplicates.)"
2564 :version "24.3"
2565 :group 'org-agenda-custom-commands
2566 :type '(repeat (list :tag "Rule"
2567 (string :tag " Agenda key")
2568 (string :tag "Replace by command")
2569 (repeat :tag "Available when"
2570 (choice
2571 (cons :tag "Condition"
2572 (choice
2573 (const :tag "In file" in-file)
2574 (const :tag "Not in file" not-in-file)
2575 (const :tag "In buffer" in-buffer)
2576 (const :tag "Not in buffer" not-in-buffer)
2577 (const :tag "In mode" in-mode)
2578 (const :tag "Not in mode" not-in-mode))
2579 (regexp))
2580 (function :tag "Custom function"))))))
2582 (defcustom org-agenda-max-entries nil
2583 "Maximum number of entries to display in an agenda.
2584 This can be nil (no limit) or an integer or an alist of agenda
2585 types with an associated number of entries to display in this
2586 type."
2587 :version "24.4"
2588 :package-version '(Org . "8.0")
2589 :group 'org-agenda-custom-commands
2590 :type '(choice (symbol :tag "No limit" nil)
2591 (integer :tag "Max number of entries")
2592 (repeat
2593 (cons (choice :tag "Agenda type"
2594 (const agenda)
2595 (const todo)
2596 (const tags)
2597 (const search)
2598 (const timeline))
2599 (integer :tag "Max number of entries")))))
2601 (defcustom org-agenda-max-todos nil
2602 "Maximum number of TODOs to display in an agenda.
2603 This can be nil (no limit) or an integer or an alist of agenda
2604 types with an associated number of entries to display in this
2605 type."
2606 :version "24.4"
2607 :package-version '(Org . "8.0")
2608 :group 'org-agenda-custom-commands
2609 :type '(choice (symbol :tag "No limit" nil)
2610 (integer :tag "Max number of TODOs")
2611 (repeat
2612 (cons (choice :tag "Agenda type"
2613 (const agenda)
2614 (const todo)
2615 (const tags)
2616 (const search)
2617 (const timeline))
2618 (integer :tag "Max number of TODOs")))))
2620 (defcustom org-agenda-max-tags nil
2621 "Maximum number of tagged entries to display in an agenda.
2622 This can be nil (no limit) or an integer or an alist of agenda
2623 types with an associated number of entries to display in this
2624 type."
2625 :version "24.4"
2626 :package-version '(Org . "8.0")
2627 :group 'org-agenda-custom-commands
2628 :type '(choice (symbol :tag "No limit" nil)
2629 (integer :tag "Max number of tagged entries")
2630 (repeat
2631 (cons (choice :tag "Agenda type"
2632 (const agenda)
2633 (const todo)
2634 (const tags)
2635 (const search)
2636 (const timeline))
2637 (integer :tag "Max number of tagged entries")))))
2639 (defcustom org-agenda-max-effort nil
2640 "Maximum cumulated effort duration for the agenda.
2641 This can be nil (no limit) or a number of minutes (as an integer)
2642 or an alist of agenda types with an associated number of minutes
2643 to limit entries to in this type."
2644 :version "24.4"
2645 :package-version '(Org . "8.0")
2646 :group 'org-agenda-custom-commands
2647 :type '(choice (symbol :tag "No limit" nil)
2648 (integer :tag "Max number of minutes")
2649 (repeat
2650 (cons (choice :tag "Agenda type"
2651 (const agenda)
2652 (const todo)
2653 (const tags)
2654 (const search)
2655 (const timeline))
2656 (integer :tag "Max number of minutes")))))
2658 (defvar org-keys nil)
2659 (defvar org-match nil)
2660 ;;;###autoload
2661 (defun org-agenda (&optional arg org-keys restriction)
2662 "Dispatch agenda commands to collect entries to the agenda buffer.
2663 Prompts for a command to execute. Any prefix arg will be passed
2664 on to the selected command. The default selections are:
2666 a Call `org-agenda-list' to display the agenda for current day or week.
2667 t Call `org-todo-list' to display the global todo list.
2668 T Call `org-todo-list' to display the global todo list, select only
2669 entries with a specific TODO keyword (the user gets a prompt).
2670 m Call `org-tags-view' to display headlines with tags matching
2671 a condition (the user is prompted for the condition).
2672 M Like `m', but select only TODO entries, no ordinary headlines.
2673 L Create a timeline for the current buffer.
2674 e Export views to associated files.
2675 s Search entries for keywords.
2676 S Search entries for keywords, only with TODO keywords.
2677 / Multi occur across all agenda files and also files listed
2678 in `org-agenda-text-search-extra-files'.
2679 < Restrict agenda commands to buffer, subtree, or region.
2680 Press several times to get the desired effect.
2681 > Remove a previous restriction.
2682 # List \"stuck\" projects.
2683 ! Configure what \"stuck\" means.
2684 C Configure custom agenda commands.
2686 More commands can be added by configuring the variable
2687 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2688 searches can be pre-defined in this way.
2690 If the current buffer is in Org-mode and visiting a file, you can also
2691 first press `<' once to indicate that the agenda should be temporarily
2692 \(until the next use of \\[org-agenda]) restricted to the current file.
2693 Pressing `<' twice means to restrict to the current subtree or region
2694 \(if active)."
2695 (interactive "P")
2696 (catch 'exit
2697 (let* ((prefix-descriptions nil)
2698 (org-agenda-buffer-name org-agenda-buffer-name)
2699 (org-agenda-window-setup (if (equal (buffer-name)
2700 org-agenda-buffer-name)
2701 'current-window
2702 org-agenda-window-setup))
2703 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2704 (org-agenda-custom-commands
2705 ;; normalize different versions
2706 (delq nil
2707 (mapcar
2708 (lambda (x)
2709 (cond ((stringp (cdr x))
2710 (push x prefix-descriptions)
2711 nil)
2712 ((stringp (nth 1 x)) x)
2713 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2714 (t (cons (car x) (cons "" (cdr x))))))
2715 org-agenda-custom-commands)))
2716 (org-agenda-custom-commands
2717 (org-contextualize-keys
2718 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2719 (buf (current-buffer))
2720 (bfn (buffer-file-name (buffer-base-buffer)))
2721 entry key type org-match lprops ans)
2722 ;; Turn off restriction unless there is an overriding one,
2723 (unless org-agenda-overriding-restriction
2724 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2725 ;; There is a request to keep the file list in place
2726 (put 'org-agenda-files 'org-restrict nil))
2727 (setq org-agenda-restrict nil)
2728 (move-marker org-agenda-restrict-begin nil)
2729 (move-marker org-agenda-restrict-end nil))
2730 ;; Delete old local properties
2731 (put 'org-agenda-redo-command 'org-lprops nil)
2732 ;; Delete previously set last-arguments
2733 (put 'org-agenda-redo-command 'last-args nil)
2734 ;; Remember where this call originated
2735 (setq org-agenda-last-dispatch-buffer (current-buffer))
2736 (unless org-keys
2737 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2738 org-keys (car ans)
2739 restriction (cdr ans)))
2740 ;; If we have sticky agenda buffers, set a name for the buffer,
2741 ;; depending on the invoking keys. The user may still set this
2742 ;; as a command option, which will overwrite what we do here.
2743 (if org-agenda-sticky
2744 (setq org-agenda-buffer-name
2745 (format "*Org Agenda(%s)*" org-keys)))
2746 ;; Establish the restriction, if any
2747 (when (and (not org-agenda-overriding-restriction) restriction)
2748 (put 'org-agenda-files 'org-restrict (list bfn))
2749 (cond
2750 ((eq restriction 'region)
2751 (setq org-agenda-restrict (current-buffer))
2752 (move-marker org-agenda-restrict-begin (region-beginning))
2753 (move-marker org-agenda-restrict-end (region-end)))
2754 ((eq restriction 'subtree)
2755 (save-excursion
2756 (setq org-agenda-restrict (current-buffer))
2757 (org-back-to-heading t)
2758 (move-marker org-agenda-restrict-begin (point))
2759 (move-marker org-agenda-restrict-end
2760 (progn (org-end-of-subtree t)))))))
2762 ;; For example the todo list should not need it (but does...)
2763 (cond
2764 ((setq entry (assoc org-keys org-agenda-custom-commands))
2765 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2766 (progn
2767 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
2768 lprops (nth 4 entry))
2769 (if org-agenda-sticky
2770 (setq org-agenda-buffer-name
2771 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2772 (format "*Org Agenda(%s)*" org-keys))))
2773 (put 'org-agenda-redo-command 'org-lprops lprops)
2774 (cond
2775 ((eq type 'agenda)
2776 (org-let lprops '(org-agenda-list current-prefix-arg)))
2777 ((eq type 'agenda*)
2778 (org-let lprops '(org-agenda-list current-prefix-arg nil nil t)))
2779 ((eq type 'alltodo)
2780 (org-let lprops '(org-todo-list current-prefix-arg)))
2781 ((eq type 'search)
2782 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
2783 ((eq type 'stuck)
2784 (org-let lprops '(org-agenda-list-stuck-projects
2785 current-prefix-arg)))
2786 ((eq type 'tags)
2787 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2788 ((eq type 'tags-todo)
2789 (org-let lprops '(org-tags-view '(4) org-match)))
2790 ((eq type 'todo)
2791 (org-let lprops '(org-todo-list org-match)))
2792 ((eq type 'tags-tree)
2793 (org-check-for-org-mode)
2794 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
2795 ((eq type 'todo-tree)
2796 (org-check-for-org-mode)
2797 (org-let lprops
2798 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2799 (regexp-quote org-match) "\\>"))))
2800 ((eq type 'occur-tree)
2801 (org-check-for-org-mode)
2802 (org-let lprops '(org-occur org-match)))
2803 ((functionp type)
2804 (org-let lprops '(funcall type org-match)))
2805 ((fboundp type)
2806 (org-let lprops '(funcall type org-match)))
2807 (t (user-error "Invalid custom agenda command type %s" type))))
2808 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2809 ((equal org-keys "C")
2810 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2811 (customize-variable 'org-agenda-custom-commands))
2812 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2813 ((equal org-keys "s") (call-interactively 'org-search-view))
2814 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2815 ((equal org-keys "t") (call-interactively 'org-todo-list))
2816 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2817 ((equal org-keys "m") (call-interactively 'org-tags-view))
2818 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2819 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2820 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
2821 (org-add-hook
2822 'post-command-hook
2823 (lambda ()
2824 (unless (current-message)
2825 (let* ((m (org-agenda-get-any-marker))
2826 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2827 (when note
2828 (message (concat
2829 "FLAGGING-NOTE ([?] for more info): "
2830 (org-add-props
2831 (replace-regexp-in-string
2832 "\\\\n" "//"
2833 (copy-sequence note))
2834 nil 'face 'org-warning)))))))
2835 t t))
2836 ((equal org-keys "L")
2837 (unless (derived-mode-p 'org-mode)
2838 (user-error "This is not an Org-mode file"))
2839 (unless restriction
2840 (put 'org-agenda-files 'org-restrict (list bfn))
2841 (org-call-with-arg 'org-timeline arg)))
2842 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2843 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2844 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
2845 (t (user-error "Invalid agenda key"))))))
2847 (defvar org-agenda-multi)
2849 (defun org-agenda-append-agenda ()
2850 "Append another agenda view to the current one.
2851 This function allows interactive building of block agendas.
2852 Agenda views are separated by `org-agenda-block-separator'."
2853 (interactive)
2854 (unless (derived-mode-p 'org-agenda-mode)
2855 (user-error "Can only append from within agenda buffer"))
2856 (let ((org-agenda-multi t))
2857 (org-agenda)
2858 (widen)
2859 (org-agenda-finalize)
2860 (setq buffer-read-only t)
2861 (org-agenda-fit-window-to-buffer)))
2863 (defun org-agenda-normalize-custom-commands (cmds)
2864 "Normalize custom commands CMDS."
2865 (delq nil
2866 (mapcar
2867 (lambda (x)
2868 (cond ((stringp (cdr x)) nil)
2869 ((stringp (nth 1 x)) x)
2870 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2871 (t (cons (car x) (cons "" (cdr x))))))
2872 cmds)))
2874 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2875 "The user interface for selecting an agenda command."
2876 (catch 'exit
2877 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2878 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2879 (region-p (org-region-active-p))
2880 (custom org-agenda-custom-commands)
2881 (selstring "")
2882 restriction second-time
2883 c entry key type match prefixes rmheader header-end custom1 desc
2884 line lines left right n n1)
2885 (save-window-excursion
2886 (delete-other-windows)
2887 (org-switch-to-buffer-other-window " *Agenda Commands*")
2888 (erase-buffer)
2889 (insert (eval-when-compile
2890 (let ((header
2891 "Press key for an agenda command: < Buffer, subtree/region restriction
2892 -------------------------------- > Remove restriction
2893 a Agenda for current week or day e Export agenda views
2894 t List of all TODO entries T Entries with special TODO kwd
2895 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2896 s Search for keywords S Like s, but only TODO entries
2897 L Timeline for current buffer # List stuck projects (!=configure)
2898 / Multi-occur C Configure custom agenda commands
2899 ? Find :FLAGGED: entries * Toggle sticky agenda views
2901 (start 0))
2902 (while (string-match
2903 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2904 header start)
2905 (setq start (match-end 0))
2906 (add-text-properties (match-beginning 2) (match-end 2)
2907 '(face bold) header))
2908 header)))
2909 (setq header-end (point-marker))
2910 (while t
2911 (setq custom1 custom)
2912 (when (eq rmheader t)
2913 (org-goto-line 1)
2914 (re-search-forward ":" nil t)
2915 (delete-region (match-end 0) (point-at-eol))
2916 (forward-char 1)
2917 (looking-at "-+")
2918 (delete-region (match-end 0) (point-at-eol))
2919 (move-marker header-end (match-end 0)))
2920 (goto-char header-end)
2921 (delete-region (point) (point-max))
2923 ;; Produce all the lines that describe custom commands and prefixes
2924 (setq lines nil)
2925 (while (setq entry (pop custom1))
2926 (setq key (car entry) desc (nth 1 entry)
2927 type (nth 2 entry)
2928 match (nth 3 entry))
2929 (if (> (length key) 1)
2930 (add-to-list 'prefixes (string-to-char key))
2931 (setq line
2932 (format
2933 "%-4s%-14s"
2934 (org-add-props (copy-sequence key)
2935 '(face bold))
2936 (cond
2937 ((string-match "\\S-" desc) desc)
2938 ((eq type 'agenda) "Agenda for current week or day")
2939 ((eq type 'agenda*) "Appointments for current week or day")
2940 ((eq type 'alltodo) "List of all TODO entries")
2941 ((eq type 'search) "Word search")
2942 ((eq type 'stuck) "List of stuck projects")
2943 ((eq type 'todo) "TODO keyword")
2944 ((eq type 'tags) "Tags query")
2945 ((eq type 'tags-todo) "Tags (TODO)")
2946 ((eq type 'tags-tree) "Tags tree")
2947 ((eq type 'todo-tree) "TODO kwd tree")
2948 ((eq type 'occur-tree) "Occur tree")
2949 ((functionp type) (if (symbolp type)
2950 (symbol-name type)
2951 "Lambda expression"))
2952 (t "???"))))
2953 (if org-agenda-menu-show-matcher
2954 (setq line
2955 (concat line ": "
2956 (cond
2957 ((stringp match)
2958 (setq match (copy-sequence match))
2959 (org-add-props match nil 'face 'org-warning))
2960 ((listp type)
2961 (format "set of %d commands" (length type))))))
2962 (if (org-string-nw-p match)
2963 (add-text-properties
2964 0 (length line) (list 'help-echo
2965 (concat "Matcher: " match)) line)))
2966 (push line lines)))
2967 (setq lines (nreverse lines))
2968 (when prefixes
2969 (mapc (lambda (x)
2970 (push
2971 (format "%s %s"
2972 (org-add-props (char-to-string x)
2973 nil 'face 'bold)
2974 (or (cdr (assoc (concat selstring
2975 (char-to-string x))
2976 prefix-descriptions))
2977 "Prefix key"))
2978 lines))
2979 prefixes))
2981 ;; Check if we should display in two columns
2982 (if org-agenda-menu-two-columns
2983 (progn
2984 (setq n (length lines)
2985 n1 (+ (/ n 2) (mod n 2))
2986 right (nthcdr n1 lines)
2987 left (copy-sequence lines))
2988 (setcdr (nthcdr (1- n1) left) nil))
2989 (setq left lines right nil))
2990 (while left
2991 (insert "\n" (pop left))
2992 (when right
2993 (if (< (current-column) 40)
2994 (move-to-column 40 t)
2995 (insert " "))
2996 (insert (pop right))))
2998 ;; Make the window the right size
2999 (goto-char (point-min))
3000 (if second-time
3001 (if (not (pos-visible-in-window-p (point-max)))
3002 (org-fit-window-to-buffer))
3003 (setq second-time t)
3004 (org-fit-window-to-buffer))
3006 ;; Ask for selection
3007 (message "Press key for agenda command%s:"
3008 (if (or restrict-ok org-agenda-overriding-restriction)
3009 (if org-agenda-overriding-restriction
3010 " (restriction lock active)"
3011 (if restriction
3012 (format " (restricted to %s)" restriction)
3013 " (unrestricted)"))
3014 ""))
3015 (setq c (read-char-exclusive))
3016 (message "")
3017 (cond
3018 ((assoc (char-to-string c) custom)
3019 (setq selstring (concat selstring (char-to-string c)))
3020 (throw 'exit (cons selstring restriction)))
3021 ((memq c prefixes)
3022 (setq selstring (concat selstring (char-to-string c))
3023 prefixes nil
3024 rmheader (or rmheader t)
3025 custom (delq nil (mapcar
3026 (lambda (x)
3027 (if (or (= (length (car x)) 1)
3028 (/= (string-to-char (car x)) c))
3030 (cons (substring (car x) 1) (cdr x))))
3031 custom))))
3032 ((eq c ?*)
3033 (call-interactively 'org-toggle-sticky-agenda)
3034 (sit-for 2))
3035 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
3036 (message "Restriction is only possible in Org-mode buffers")
3037 (ding) (sit-for 1))
3038 ((eq c ?1)
3039 (org-agenda-remove-restriction-lock 'noupdate)
3040 (setq restriction 'buffer))
3041 ((eq c ?0)
3042 (org-agenda-remove-restriction-lock 'noupdate)
3043 (setq restriction (if region-p 'region 'subtree)))
3044 ((eq c ?<)
3045 (org-agenda-remove-restriction-lock 'noupdate)
3046 (setq restriction
3047 (cond
3048 ((eq restriction 'buffer)
3049 (if region-p 'region 'subtree))
3050 ((memq restriction '(subtree region))
3051 nil)
3052 (t 'buffer))))
3053 ((eq c ?>)
3054 (org-agenda-remove-restriction-lock 'noupdate)
3055 (setq restriction nil))
3056 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
3057 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
3058 ((and (> (length selstring) 0) (eq c ?\d))
3059 (delete-window)
3060 (org-agenda-get-restriction-and-command prefix-descriptions))
3062 ((equal c ?q) (error "Abort"))
3063 (t (user-error "Invalid key %c" c))))))))
3065 (defun org-agenda-fit-window-to-buffer ()
3066 "Fit the window to the buffer size."
3067 (and (memq org-agenda-window-setup '(reorganize-frame))
3068 (fboundp 'fit-window-to-buffer)
3069 (if (and (= (cdr org-agenda-window-frame-fractions) 1.0)
3070 (= (car org-agenda-window-frame-fractions) 1.0))
3071 (delete-other-windows)
3072 (org-fit-window-to-buffer
3074 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3075 (floor (* (frame-height) (car org-agenda-window-frame-fractions)))))))
3077 (defvar org-cmd nil)
3078 (defvar org-agenda-overriding-cmd nil)
3079 (defvar org-agenda-overriding-arguments nil)
3080 (defvar org-agenda-overriding-cmd-arguments nil)
3081 (defun org-agenda-run-series (name series)
3082 "Run agenda NAME as a SERIES of agenda commands."
3083 (org-let (nth 1 series) '(org-agenda-prepare name))
3084 ;; We need to reset agenda markers here, because when constructing a
3085 ;; block agenda, the individual blocks do not do that.
3086 (org-agenda-reset-markers)
3087 (let* ((org-agenda-multi t)
3088 (redo (list 'org-agenda-run-series name (list 'quote series)))
3089 (cmds (car series))
3090 (gprops (nth 1 series))
3091 match ;; The byte compiler incorrectly complains about this. Keep it!
3092 org-cmd type lprops)
3093 (while (setq org-cmd (pop cmds))
3094 (setq type (car org-cmd)
3095 match (eval (nth 1 org-cmd))
3096 lprops (nth 2 org-cmd))
3097 (let ((org-agenda-overriding-arguments
3098 (if (eq org-agenda-overriding-cmd org-cmd)
3099 (or org-agenda-overriding-arguments
3100 org-agenda-overriding-cmd-arguments))))
3101 (cond
3102 ((eq type 'agenda)
3103 (org-let2 gprops lprops
3104 '(call-interactively 'org-agenda-list)))
3105 ((eq type 'agenda*)
3106 (org-let2 gprops lprops
3107 '(funcall 'org-agenda-list nil nil t)))
3108 ((eq type 'alltodo)
3109 (org-let2 gprops lprops
3110 '(call-interactively 'org-todo-list)))
3111 ((eq type 'search)
3112 (org-let2 gprops lprops
3113 '(org-search-view current-prefix-arg match nil)))
3114 ((eq type 'stuck)
3115 (org-let2 gprops lprops
3116 '(call-interactively 'org-agenda-list-stuck-projects)))
3117 ((eq type 'tags)
3118 (org-let2 gprops lprops
3119 '(org-tags-view current-prefix-arg match)))
3120 ((eq type 'tags-todo)
3121 (org-let2 gprops lprops
3122 '(org-tags-view '(4) match)))
3123 ((eq type 'todo)
3124 (org-let2 gprops lprops
3125 '(org-todo-list match)))
3126 ((fboundp type)
3127 (org-let2 gprops lprops
3128 '(funcall type match)))
3129 (t (error "Invalid type in command series")))))
3130 (widen)
3131 (let ((inhibit-read-only t))
3132 (add-text-properties (point-min) (point-max)
3133 `(org-series t org-series-redo-cmd ,redo)))
3134 (setq org-agenda-redo-command redo)
3135 (goto-char (point-min)))
3136 (org-agenda-fit-window-to-buffer)
3137 (org-let (nth 1 series) '(org-agenda-finalize)))
3139 ;;;###autoload
3140 (defmacro org-batch-agenda (cmd-key &rest parameters)
3141 "Run an agenda command in batch mode and send the result to STDOUT.
3142 If CMD-KEY is a string of length 1, it is used as a key in
3143 `org-agenda-custom-commands' and triggers this command. If it is a
3144 longer string it is used as a tags/todo match string.
3145 Parameters are alternating variable names and values that will be bound
3146 before running the agenda command."
3147 (org-eval-in-environment (org-make-parameter-alist parameters)
3148 (let (org-agenda-sticky)
3149 (if (> (length cmd-key) 2)
3150 (org-tags-view nil cmd-key)
3151 (org-agenda nil cmd-key))))
3152 (set-buffer org-agenda-buffer-name)
3153 (princ (buffer-string)))
3155 (defvar org-agenda-info nil)
3157 ;;;###autoload
3158 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
3159 "Run an agenda command in batch mode and send the result to STDOUT.
3160 If CMD-KEY is a string of length 1, it is used as a key in
3161 `org-agenda-custom-commands' and triggers this command. If it is a
3162 longer string it is used as a tags/todo match string.
3163 Parameters are alternating variable names and values that will be bound
3164 before running the agenda command.
3166 The output gives a line for each selected agenda item. Each
3167 item is a list of comma-separated values, like this:
3169 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3171 category The category of the item
3172 head The headline, without TODO kwd, TAGS and PRIORITY
3173 type The type of the agenda entry, can be
3174 todo selected in TODO match
3175 tagsmatch selected in tags match
3176 diary imported from diary
3177 deadline a deadline on given date
3178 scheduled scheduled on given date
3179 timestamp entry has timestamp on given date
3180 closed entry was closed on given date
3181 upcoming-deadline warning about deadline
3182 past-scheduled forwarded scheduled item
3183 block entry has date block including g. date
3184 todo The todo keyword, if any
3185 tags All tags including inherited ones, separated by colons
3186 date The relevant date, like 2007-2-14
3187 time The time, like 15:00-16:50
3188 extra Sting with extra planning info
3189 priority-l The priority letter if any was given
3190 priority-n The computed numerical priority
3191 agenda-day The day in the agenda where this is listed"
3192 (org-eval-in-environment (append '((org-agenda-remove-tags t))
3193 (org-make-parameter-alist parameters))
3194 (if (> (length cmd-key) 2)
3195 (org-tags-view nil cmd-key)
3196 (org-agenda nil cmd-key)))
3197 (set-buffer org-agenda-buffer-name)
3198 (let* ((lines (org-split-string (buffer-string) "\n"))
3199 line)
3200 (while (setq line (pop lines))
3201 (catch 'next
3202 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
3203 (setq org-agenda-info
3204 (org-fix-agenda-info (text-properties-at 0 line)))
3205 (princ
3206 (mapconcat 'org-agenda-export-csv-mapper
3207 '(org-category txt type todo tags date time extra
3208 priority-letter priority agenda-day)
3209 ","))
3210 (princ "\n")))))
3212 (defun org-fix-agenda-info (props)
3213 "Make sure all properties on an agenda item have a canonical form.
3214 This ensures the export commands can easily use it."
3215 (let (tmp re)
3216 (when (setq tmp (plist-get props 'tags))
3217 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3218 (when (setq tmp (plist-get props 'date))
3219 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3220 (let ((calendar-date-display-form '(year "-" month "-" day)))
3221 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3223 (setq tmp (calendar-date-string tmp)))
3224 (setq props (plist-put props 'date tmp)))
3225 (when (setq tmp (plist-get props 'day))
3226 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3227 (let ((calendar-date-display-form '(year "-" month "-" day)))
3228 (setq tmp (calendar-date-string tmp)))
3229 (setq props (plist-put props 'day tmp))
3230 (setq props (plist-put props 'agenda-day tmp)))
3231 (when (setq tmp (plist-get props 'txt))
3232 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3233 (plist-put props 'priority-letter (match-string 1 tmp))
3234 (setq tmp (replace-match "" t t tmp)))
3235 (when (and (setq re (plist-get props 'org-todo-regexp))
3236 (setq re (concat "\\`\\.*" re " ?"))
3237 (string-match re tmp))
3238 (plist-put props 'todo (match-string 1 tmp))
3239 (setq tmp (replace-match "" t t tmp)))
3240 (plist-put props 'txt tmp)))
3241 props)
3243 (defun org-agenda-export-csv-mapper (prop)
3244 (let ((res (plist-get org-agenda-info prop)))
3245 (setq res
3246 (cond
3247 ((not res) "")
3248 ((stringp res) res)
3249 (t (prin1-to-string res))))
3250 (while (string-match "," res)
3251 (setq res (replace-match ";" t t res)))
3252 (org-trim res)))
3254 ;;;###autoload
3255 (defun org-store-agenda-views (&rest parameters)
3256 "Store agenda views."
3257 (interactive)
3258 (eval (list 'org-batch-store-agenda-views)))
3260 ;;;###autoload
3261 (defmacro org-batch-store-agenda-views (&rest parameters)
3262 "Run all custom agenda commands that have a file argument."
3263 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3264 (pop-up-frames nil)
3265 (dir default-directory)
3266 (pars (org-make-parameter-alist parameters))
3267 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
3268 (save-window-excursion
3269 (while cmds
3270 (setq cmd (pop cmds)
3271 thiscmdkey (car cmd)
3272 thiscmdcmd (cdr cmd)
3273 match (nth 2 thiscmdcmd)
3274 bufname (if org-agenda-sticky
3275 (or (and (stringp match)
3276 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3277 (format "*Org Agenda(%s)*" thiscmdkey))
3278 org-agenda-buffer-name)
3279 cmd-or-set (nth 2 cmd)
3280 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3281 files (nth (if (listp cmd-or-set) 4 5) cmd))
3282 (if (stringp files) (setq files (list files)))
3283 (when files
3284 (org-eval-in-environment (append org-agenda-exporter-settings
3285 opts pars)
3286 (org-agenda nil thiscmdkey))
3287 (set-buffer bufname)
3288 (while files
3289 (org-eval-in-environment (append org-agenda-exporter-settings
3290 opts pars)
3291 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3292 (and (get-buffer bufname)
3293 (kill-buffer bufname)))))))
3295 (defvar org-agenda-current-span nil
3296 "The current span used in the agenda view.") ; local variable in the agenda buffer
3297 (defun org-agenda-mark-header-line (pos)
3298 "Mark the line at POS as an agenda structure header."
3299 (save-excursion
3300 (goto-char pos)
3301 (put-text-property (point-at-bol) (point-at-eol)
3302 'org-agenda-structural-header t)
3303 (when org-agenda-title-append
3304 (put-text-property (point-at-bol) (point-at-eol)
3305 'org-agenda-title-append org-agenda-title-append))))
3307 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3308 (defvar org-agenda-write-buffer-name "Agenda View")
3309 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3310 "Write the current buffer (an agenda view) as a file.
3311 Depending on the extension of the file name, plain text (.txt),
3312 HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
3313 If the extension is .ics, run icalendar export over all files used
3314 to construct the agenda and limit the export to entries listed in the
3315 agenda now.
3316 If the extension is .org, collect all subtrees corresponding to the
3317 agenda entries and add them in an .org file.
3318 With prefix argument OPEN, open the new file immediately.
3319 If NOSETTINGS is given, do not scope the settings of
3320 `org-agenda-exporter-settings' into the export commands. This is used when
3321 the settings have already been scoped and we do not wish to overrule other,
3322 higher priority settings.
3323 If AGENDA-BUFFER-NAME, use this as the buffer name for the agenda to write."
3324 (interactive "FWrite agenda to file: \nP")
3325 (if (or (not (file-writable-p file))
3326 (and (file-exists-p file)
3327 (if (org-called-interactively-p 'any)
3328 (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
3329 (user-error "Cannot write agenda to file %s" file))
3330 (org-let (if nosettings nil org-agenda-exporter-settings)
3331 '(save-excursion
3332 (save-window-excursion
3333 (let ((bs (copy-sequence (buffer-string))) beg content)
3334 (with-temp-buffer
3335 (rename-buffer org-agenda-write-buffer-name t)
3336 (set-buffer-modified-p nil)
3337 (insert bs)
3338 (org-agenda-remove-marked-text 'org-filtered)
3339 (run-hooks 'org-agenda-before-write-hook)
3340 (cond
3341 ((org-bound-and-true-p org-mobile-creating-agendas)
3342 (org-mobile-write-agenda-for-mobile file))
3343 ((string-match "\\.org\\'" file)
3344 (let (content p m message-log-max)
3345 (goto-char (point-min))
3346 (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
3347 (goto-char p)
3348 (setq m (get-text-property (point) 'org-hd-marker))
3349 (when m
3350 (push (save-excursion
3351 (set-buffer (marker-buffer m))
3352 (goto-char m)
3353 (org-copy-subtree 1 nil t t)
3354 org-subtree-clip)
3355 content)))
3356 (find-file file)
3357 (erase-buffer)
3358 (mapcar (lambda (s) (org-paste-subtree 1 s)) (reverse content))
3359 (write-file file)
3360 (kill-buffer (current-buffer))
3361 (message "Org file written to %s" file)))
3362 ((string-match "\\.html?\\'" file)
3363 (require 'htmlize)
3364 (set-buffer (htmlize-buffer (current-buffer)))
3365 (when org-agenda-export-html-style
3366 ;; replace <style> section with org-agenda-export-html-style
3367 (goto-char (point-min))
3368 (kill-region (- (search-forward "<style") 6)
3369 (search-forward "</style>"))
3370 (insert org-agenda-export-html-style))
3371 (write-file file)
3372 (kill-buffer (current-buffer))
3373 (message "HTML written to %s" file))
3374 ((string-match "\\.ps\\'" file)
3375 (require 'ps-print)
3376 (ps-print-buffer-with-faces file)
3377 (message "Postscript written to %s" file))
3378 ((string-match "\\.pdf\\'" file)
3379 (require 'ps-print)
3380 (ps-print-buffer-with-faces
3381 (concat (file-name-sans-extension file) ".ps"))
3382 (call-process "ps2pdf" nil nil nil
3383 (expand-file-name
3384 (concat (file-name-sans-extension file) ".ps"))
3385 (expand-file-name file))
3386 (delete-file (concat (file-name-sans-extension file) ".ps"))
3387 (message "PDF written to %s" file))
3388 ((string-match "\\.ics\\'" file)
3389 (require 'ox-icalendar)
3390 (org-icalendar-export-current-agenda (expand-file-name file)))
3392 (let ((bs (buffer-string)))
3393 (find-file file)
3394 (erase-buffer)
3395 (insert bs)
3396 (save-buffer 0)
3397 (kill-buffer (current-buffer))
3398 (message "Plain text written to %s" file))))))))
3399 (set-buffer (or agenda-bufname
3400 (and (org-called-interactively-p 'any) (buffer-name))
3401 org-agenda-buffer-name)))
3402 (when open (org-open-file file)))
3404 (defun org-agenda-remove-marked-text (property &optional value)
3405 "Delete all text marked with VALUE of PROPERTY.
3406 VALUE defaults to t."
3407 (let (beg)
3408 (setq value (or value t))
3409 (while (setq beg (text-property-any (point-min) (point-max)
3410 property value))
3411 (delete-region
3412 beg (or (next-single-property-change beg property)
3413 (point-max))))))
3415 (defun org-agenda-add-entry-text ()
3416 "Add entry text to agenda lines.
3417 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3418 entry text following headings shown in the agenda.
3419 Drawers will be excluded, also the line with scheduling/deadline info."
3420 (when (and (> org-agenda-add-entry-text-maxlines 0)
3421 (not (org-bound-and-true-p org-mobile-creating-agendas)))
3422 (let (m txt)
3423 (goto-char (point-min))
3424 (while (not (eobp))
3425 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3426 (beginning-of-line 2)
3427 (setq txt (org-agenda-get-some-entry-text
3428 m org-agenda-add-entry-text-maxlines " > "))
3429 (end-of-line 1)
3430 (if (string-match "\\S-" txt)
3431 (insert "\n" txt)
3432 (or (eobp) (forward-char 1))))))))
3434 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3435 &rest keep)
3436 "Extract entry text from MARKER, at most N-LINES lines.
3437 This will ignore drawers etc, just get the text.
3438 If INDENT is given, prefix every line with this string. If KEEP is
3439 given, it is a list of symbols, defining stuff that should not be
3440 removed from the entry content. Currently only `planning' is allowed here."
3441 (let (txt drawer-re kwd-time-re ind)
3442 (save-excursion
3443 (with-current-buffer (marker-buffer marker)
3444 (if (not (derived-mode-p 'org-mode))
3445 (setq txt "")
3446 (save-excursion
3447 (save-restriction
3448 (widen)
3449 (goto-char marker)
3450 (end-of-line 1)
3451 (setq txt (buffer-substring
3452 (min (1+ (point)) (point-max))
3453 (progn (outline-next-heading) (point)))
3454 drawer-re org-drawer-regexp
3455 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3456 ".*\n?"))
3457 (with-temp-buffer
3458 (insert txt)
3459 (when org-agenda-add-entry-text-descriptive-links
3460 (goto-char (point-min))
3461 (while (org-activate-bracket-links (point-max))
3462 (add-text-properties (match-beginning 0) (match-end 0)
3463 '(face org-link))))
3464 (goto-char (point-min))
3465 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3466 (set-text-properties (match-beginning 0) (match-end 0)
3467 nil))
3468 (goto-char (point-min))
3469 (while (re-search-forward drawer-re nil t)
3470 (delete-region
3471 (match-beginning 0)
3472 (progn (re-search-forward
3473 "^[ \t]*:END:.*\n?" nil 'move)
3474 (point))))
3475 (unless (member 'planning keep)
3476 (goto-char (point-min))
3477 (while (re-search-forward kwd-time-re nil t)
3478 (replace-match "")))
3479 (goto-char (point-min))
3480 (when org-agenda-entry-text-exclude-regexps
3481 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3482 (while (setq re (pop re-list))
3483 (goto-char (point-min))
3484 (while (re-search-forward re nil t)
3485 (replace-match "")))))
3486 (goto-char (point-max))
3487 (skip-chars-backward " \t\n")
3488 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3490 ;; find and remove min common indentation
3491 (goto-char (point-min))
3492 (untabify (point-min) (point-max))
3493 (setq ind (org-get-indentation))
3494 (while (not (eobp))
3495 (unless (looking-at "[ \t]*$")
3496 (setq ind (min ind (org-get-indentation))))
3497 (beginning-of-line 2))
3498 (goto-char (point-min))
3499 (while (not (eobp))
3500 (unless (looking-at "[ \t]*$")
3501 (move-to-column ind)
3502 (delete-region (point-at-bol) (point)))
3503 (beginning-of-line 2))
3505 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3507 (goto-char (point-min))
3508 (when indent
3509 (while (and (not (eobp)) (re-search-forward "^" nil t))
3510 (replace-match indent t t)))
3511 (goto-char (point-min))
3512 (while (looking-at "[ \t]*\n") (replace-match ""))
3513 (goto-char (point-max))
3514 (when (> (org-current-line)
3515 n-lines)
3516 (org-goto-line (1+ n-lines))
3517 (backward-char 1))
3518 (setq txt (buffer-substring (point-min) (point)))))))))
3519 txt))
3521 (defun org-check-for-org-mode ()
3522 "Make sure current buffer is in org-mode. Error if not."
3523 (or (derived-mode-p 'org-mode)
3524 (error "Cannot execute org-mode agenda command on buffer in %s"
3525 major-mode)))
3527 ;;; Agenda prepare and finalize
3529 (defvar org-agenda-multi nil) ; dynamically scoped
3530 (defvar org-agenda-pre-window-conf nil)
3531 (defvar org-agenda-columns-active nil)
3532 (defvar org-agenda-name nil)
3533 (defvar org-agenda-tag-filter nil)
3534 (defvar org-agenda-category-filter nil)
3535 (defvar org-agenda-regexp-filter nil)
3536 (defvar org-agenda-effort-filter nil)
3537 (defvar org-agenda-top-headline-filter nil)
3538 (defvar org-agenda-tag-filter-while-redo nil)
3539 (defvar org-agenda-tag-filter-preset nil
3540 "A preset of the tags filter used for secondary agenda filtering.
3541 This must be a list of strings, each string must be a single tag preceded
3542 by \"+\" or \"-\".
3543 This variable should not be set directly, but agenda custom commands can
3544 bind it in the options section. The preset filter is a global property of
3545 the entire agenda view. In a block agenda, it will not work reliably to
3546 define a filter for one of the individual blocks. You need to set it in
3547 the global options and expect it to be applied to the entire view.")
3549 (defvar org-agenda-category-filter-preset nil
3550 "A preset of the category filter used for secondary agenda filtering.
3551 This must be a list of strings, each string must be a single category
3552 preceded by \"+\" or \"-\".
3553 This variable should not be set directly, but agenda custom commands can
3554 bind it in the options section. The preset filter is a global property of
3555 the entire agenda view. In a block agenda, it will not work reliably to
3556 define a filter for one of the individual blocks. You need to set it in
3557 the global options and expect it to be applied to the entire view.")
3559 (defvar org-agenda-regexp-filter-preset nil
3560 "A preset of the regexp filter used for secondary agenda filtering.
3561 This must be a list of strings, each string must be a single regexp
3562 preceded by \"+\" or \"-\".
3563 This variable should not be set directly, but agenda custom commands can
3564 bind it in the options section. The preset filter is a global property of
3565 the entire agenda view. In a block agenda, it will not work reliably to
3566 define a filter for one of the individual blocks. You need to set it in
3567 the global options and expect it to be applied to the entire view.")
3569 (defvar org-agenda-effort-filter-preset nil
3570 "A preset of the effort condition used for secondary agenda filtering.
3571 This must be a list of strings, each string must be a single regexp
3572 preceded 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 (defun org-agenda-use-sticky-p ()
3580 "Return non-nil if an agenda buffer named
3581 `org-agenda-buffer-name' exists and should be shown instead of
3582 generating a new one."
3583 (and
3584 ;; turned off by user
3585 org-agenda-sticky
3586 ;; For multi-agenda buffer already exists
3587 (not org-agenda-multi)
3588 ;; buffer found
3589 (get-buffer org-agenda-buffer-name)
3590 ;; C-u parameter is same as last call
3591 (with-current-buffer (get-buffer org-agenda-buffer-name)
3592 (and
3593 (equal current-prefix-arg
3594 org-agenda-last-prefix-arg)
3595 ;; In case user turned stickiness on, while having existing
3596 ;; Agenda buffer active, don't reuse that buffer, because it
3597 ;; does not have org variables local
3598 org-agenda-this-buffer-is-sticky))))
3600 (defun org-agenda-prepare-window (abuf filter-alist)
3601 "Setup agenda buffer in the window.
3602 ABUF is the buffer for the agenda window.
3603 FILTER-ALIST is an alist of filters we need to apply when
3604 `org-agenda-persistent-filter' is non-nil."
3605 (let* ((awin (get-buffer-window abuf)) wconf)
3606 (cond
3607 ((equal (current-buffer) abuf) nil)
3608 (awin (select-window awin))
3609 ((not (setq wconf (current-window-configuration))))
3610 ((equal org-agenda-window-setup 'current-window)
3611 (org-pop-to-buffer-same-window abuf))
3612 ((equal org-agenda-window-setup 'other-window)
3613 (org-switch-to-buffer-other-window abuf))
3614 ((equal org-agenda-window-setup 'other-frame)
3615 (switch-to-buffer-other-frame abuf))
3616 ((equal org-agenda-window-setup 'reorganize-frame)
3617 (delete-other-windows)
3618 (org-switch-to-buffer-other-window abuf)))
3619 (setq org-agenda-tag-filter (cdr (assoc 'tag filter-alist)))
3620 (setq org-agenda-category-filter (cdr (assoc 'cat filter-alist)))
3621 (setq org-agenda-effort-filter (cdr (assoc 'effort filter-alist)))
3622 (setq org-agenda-regexp-filter (cdr (assoc 're filter-alist)))
3623 ;; Additional test in case agenda is invoked from within agenda
3624 ;; buffer via elisp link.
3625 (unless (equal (current-buffer) abuf)
3626 (org-pop-to-buffer-same-window abuf))
3627 (setq org-agenda-pre-window-conf
3628 (or org-agenda-pre-window-conf wconf))))
3630 (defun org-agenda-prepare (&optional name)
3631 (let ((filter-alist (if org-agenda-persistent-filter
3632 (list `(tag . ,org-agenda-tag-filter)
3633 `(re . ,org-agenda-regexp-filter)
3634 `(effort . ,org-agenda-effort-filter)
3635 `(car . ,org-agenda-category-filter)))))
3636 (if (org-agenda-use-sticky-p)
3637 (progn
3638 ;; Popup existing buffer
3639 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name)
3640 filter-alist)
3641 (message "Sticky Agenda buffer, use `r' to refresh")
3642 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3643 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3644 (setq org-todo-keywords-for-agenda nil)
3645 (put 'org-agenda-tag-filter :preset-filter
3646 org-agenda-tag-filter-preset)
3647 (put 'org-agenda-category-filter :preset-filter
3648 org-agenda-category-filter-preset)
3649 (put 'org-agenda-regexp-filter :preset-filter
3650 org-agenda-regexp-filter-preset)
3651 (put 'org-agenda-effort-filter :preset-filter
3652 org-agenda-effort-filter-preset)
3653 (if org-agenda-multi
3654 (progn
3655 (setq buffer-read-only nil)
3656 (goto-char (point-max))
3657 (unless (or (bobp) org-agenda-compact-blocks
3658 (not org-agenda-block-separator))
3659 (insert "\n"
3660 (if (stringp org-agenda-block-separator)
3661 org-agenda-block-separator
3662 (make-string (window-width) org-agenda-block-separator))
3663 "\n"))
3664 (narrow-to-region (point) (point-max)))
3665 (setq org-done-keywords-for-agenda nil)
3666 ;; Setting any org variables that are in org-agenda-local-vars
3667 ;; list need to be done after the prepare call
3668 (org-agenda-prepare-window
3669 (get-buffer-create org-agenda-buffer-name) filter-alist)
3670 (setq buffer-read-only nil)
3671 (org-agenda-reset-markers)
3672 (let ((inhibit-read-only t)) (erase-buffer))
3673 (org-agenda-mode)
3674 (setq org-agenda-buffer (current-buffer))
3675 (setq org-agenda-contributing-files nil)
3676 (setq org-agenda-columns-active nil)
3677 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3678 (setq org-todo-keywords-for-agenda
3679 (org-uniquify org-todo-keywords-for-agenda))
3680 (setq org-done-keywords-for-agenda
3681 (org-uniquify org-done-keywords-for-agenda))
3682 (setq org-agenda-last-prefix-arg current-prefix-arg)
3683 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3684 (and name (not org-agenda-name)
3685 (org-set-local 'org-agenda-name name)))
3686 (setq buffer-read-only nil))))
3688 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3689 (defun org-agenda-finalize ()
3690 "Finishing touch for the agenda buffer, called just before displaying it."
3691 (unless org-agenda-multi
3692 (save-excursion
3693 (let ((inhibit-read-only t))
3694 (goto-char (point-min))
3695 (save-excursion
3696 (while (org-activate-bracket-links (point-max))
3697 (add-text-properties (match-beginning 0) (match-end 0)
3698 '(face org-link))))
3699 (save-excursion
3700 (while (org-activate-plain-links (point-max))
3701 (add-text-properties (match-beginning 0) (match-end 0)
3702 '(face org-link))))
3703 (unless (eq org-agenda-remove-tags t)
3704 (org-agenda-align-tags))
3705 (unless org-agenda-with-colors
3706 (remove-text-properties (point-min) (point-max) '(face nil)))
3707 (if (and (boundp 'org-agenda-overriding-columns-format)
3708 org-agenda-overriding-columns-format)
3709 (org-set-local 'org-agenda-overriding-columns-format
3710 org-agenda-overriding-columns-format))
3711 (if (and (boundp 'org-agenda-view-columns-initially)
3712 org-agenda-view-columns-initially)
3713 (org-agenda-columns))
3714 (when org-agenda-fontify-priorities
3715 (org-agenda-fontify-priorities))
3716 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3717 (org-agenda-dim-blocked-tasks))
3718 (org-agenda-mark-clocking-task)
3719 (when org-agenda-entry-text-mode
3720 (org-agenda-entry-text-hide)
3721 (org-agenda-entry-text-show))
3722 (if (and (functionp 'org-habit-insert-consistency-graphs)
3723 (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
3724 (org-habit-insert-consistency-graphs))
3725 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3726 (unless (or (eq org-agenda-show-inherited-tags 'always)
3727 (and (listp org-agenda-show-inherited-tags)
3728 (memq org-agenda-type org-agenda-show-inherited-tags))
3729 (and (eq org-agenda-show-inherited-tags t)
3730 (or (eq org-agenda-use-tag-inheritance t)
3731 (and (listp org-agenda-use-tag-inheritance)
3732 (not (memq org-agenda-type
3733 org-agenda-use-tag-inheritance))))))
3734 (let (mrk)
3735 (save-excursion
3736 (goto-char (point-min))
3737 (while (equal (forward-line) 0)
3738 (when (setq mrk (or (get-text-property (point) 'org-hd-marker)
3739 (get-text-property (point) 'org-hd-marker)))
3740 (put-text-property (point-at-bol) (point-at-eol)
3741 'tags (org-with-point-at mrk
3742 (delete-dups
3743 (mapcar 'downcase (org-get-tags-at))))))))))
3744 (run-hooks 'org-agenda-finalize-hook)
3745 (when org-agenda-top-headline-filter
3746 (org-agenda-filter-top-headline-apply
3747 org-agenda-top-headline-filter))
3748 (when org-agenda-tag-filter
3749 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
3750 (when (get 'org-agenda-tag-filter :preset-filter)
3751 (org-agenda-filter-apply
3752 (get 'org-agenda-tag-filter :preset-filter) 'tag))
3753 (when org-agenda-category-filter
3754 (org-agenda-filter-apply org-agenda-category-filter 'category))
3755 (when (get 'org-agenda-category-filter :preset-filter)
3756 (org-agenda-filter-apply
3757 (get 'org-agenda-category-filter :preset-filter) 'category))
3758 (when org-agenda-regexp-filter
3759 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
3760 (when (get 'org-agenda-regexp-filter :preset-filter)
3761 (org-agenda-filter-apply
3762 (get 'org-agenda-regexp-filter :preset-filter) 'regexp))
3763 (when org-agenda-effort-filter
3764 (org-agenda-filter-apply org-agenda-effort-filter 'effort))
3765 (when (get 'org-agenda-effort-filter :preset-filter)
3766 (org-agenda-filter-apply
3767 (get 'org-agenda-effort-filter :preset-filter) 'effort))
3768 (org-add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)))))
3770 (defun org-agenda-mark-clocking-task ()
3771 "Mark the current clock entry in the agenda if it is present."
3772 ;; We need to widen when `org-agenda-finalize' is called from
3773 ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
3774 (when org-clock-current-task
3775 (save-restriction
3776 (widen)
3777 (org-agenda-unmark-clocking-task)
3778 (when (marker-buffer org-clock-hd-marker)
3779 (save-excursion
3780 (goto-char (point-min))
3781 (let (s ov)
3782 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3783 (goto-char s)
3784 (when (equal (org-get-at-bol 'org-hd-marker)
3785 org-clock-hd-marker)
3786 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3787 (overlay-put ov 'type 'org-agenda-clocking)
3788 (overlay-put ov 'face 'org-agenda-clocking)
3789 (overlay-put ov 'help-echo
3790 "The clock is running in this item")))))))))
3792 (defun org-agenda-unmark-clocking-task ()
3793 "Unmark the current clocking task."
3794 (mapc (lambda (o)
3795 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3796 (delete-overlay o)))
3797 (overlays-in (point-min) (point-max))))
3799 (defun org-agenda-fontify-priorities ()
3800 "Make highest priority lines bold, and lowest italic."
3801 (interactive)
3802 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3803 (delete-overlay o)))
3804 (overlays-in (point-min) (point-max)))
3805 (save-excursion
3806 (let (b e p ov h l)
3807 (goto-char (point-min))
3808 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3809 (setq h (or (get-char-property (point) 'org-highest-priority)
3810 org-highest-priority)
3811 l (or (get-char-property (point) 'org-lowest-priority)
3812 org-lowest-priority)
3813 p (string-to-char (match-string 1))
3814 b (match-beginning 0)
3815 e (if (eq org-agenda-fontify-priorities 'cookies)
3816 (match-end 0)
3817 (point-at-eol))
3818 ov (make-overlay b e))
3819 (overlay-put
3820 ov 'face
3821 (cons (cond ((org-face-from-face-or-color
3822 'priority nil
3823 (cdr (assoc p org-priority-faces))))
3824 ((and (listp org-agenda-fontify-priorities)
3825 (org-face-from-face-or-color
3826 'priority nil
3827 (cdr (assoc p org-agenda-fontify-priorities)))))
3828 ((equal p l) 'italic)
3829 ((equal p h) 'bold))
3830 'org-priority))
3831 (overlay-put ov 'org-type 'org-priority)))))
3833 (defvar org-depend-tag-blocked)
3835 (defun org-agenda-dim-blocked-tasks (&optional invisible)
3836 "Dim currently blocked TODO's in the agenda display.
3837 When INVISIBLE is non-nil, hide currently blocked TODO instead of
3838 dimming them."
3839 (interactive "P")
3840 (when (org-called-interactively-p 'interactive)
3841 (message "Dim or hide blocked tasks..."))
3842 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3843 (delete-overlay o)))
3844 (overlays-in (point-min) (point-max)))
3845 (save-excursion
3846 (let ((inhibit-read-only t)
3847 (org-depend-tag-blocked nil)
3848 (invis (or (not (null invisible))
3849 (eq org-agenda-dim-blocked-tasks 'invisible)))
3850 org-blocked-by-checkboxes
3851 invis1 b e p ov h l)
3852 (goto-char (point-min))
3853 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3854 (and pos (goto-char (1+ pos))))
3855 (setq org-blocked-by-checkboxes nil invis1 invis)
3856 (let ((marker (org-get-at-bol 'org-hd-marker)))
3857 (when (and marker
3858 (with-current-buffer (marker-buffer marker)
3859 (save-excursion (goto-char marker)
3860 (org-entry-blocked-p))))
3861 (if org-blocked-by-checkboxes (setq invis1 nil))
3862 (setq b (if invis1
3863 (max (point-min) (1- (point-at-bol)))
3864 (point-at-bol))
3865 e (point-at-eol)
3866 ov (make-overlay b e))
3867 (if invis1
3868 (progn (overlay-put ov 'invisible t)
3869 (overlay-put ov 'intangible t))
3870 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3871 (overlay-put ov 'org-type 'org-blocked-todo))))))
3872 (when (org-called-interactively-p 'interactive)
3873 (message "Dim or hide blocked tasks...done")))
3875 (defvar org-agenda-skip-function nil
3876 "Function to be called at each match during agenda construction.
3877 If this function returns nil, the current match should not be skipped.
3878 Otherwise, the function must return a position from where the search
3879 should be continued.
3880 This may also be a Lisp form, it will be evaluated.
3881 Never set this variable using `setq' or so, because then it will apply
3882 to all future agenda commands. If you do want a global skipping condition,
3883 use the option `org-agenda-skip-function-global' instead.
3884 The correct usage for `org-agenda-skip-function' is to bind it with
3885 `let' to scope it dynamically into the agenda-constructing command.
3886 A good way to set it is through options in `org-agenda-custom-commands'.")
3888 (defun org-agenda-skip ()
3889 "Throw to `:skip' in places that should be skipped.
3890 Also moves point to the end of the skipped region, so that search can
3891 continue from there."
3892 (let ((p (point-at-bol)) to)
3893 (when (or
3894 (save-excursion (goto-char p) (looking-at comment-start-skip))
3895 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3896 (get-text-property p :org-archived)
3897 (org-end-of-subtree t))
3898 (and org-agenda-skip-comment-trees
3899 (get-text-property p :org-comment)
3900 (org-end-of-subtree t))
3901 (and (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3902 (org-agenda-skip-eval org-agenda-skip-function)))
3903 (goto-char to))
3904 (org-in-src-block-p t))
3905 (throw :skip t))))
3907 (defun org-agenda-skip-eval (form)
3908 "If FORM is a function or a list, call (or eval) it and return the result.
3909 `save-excursion' and `save-match-data' are wrapped around the call, so point
3910 and match data are returned to the previous state no matter what these
3911 functions do."
3912 (let (fp)
3913 (and form
3914 (or (setq fp (functionp form))
3915 (consp form))
3916 (save-excursion
3917 (save-match-data
3918 (if fp
3919 (funcall form)
3920 (eval form)))))))
3922 (defvar org-agenda-markers nil
3923 "List of all currently active markers created by `org-agenda'.")
3924 (defvar org-agenda-last-marker-time (org-float-time)
3925 "Creation time of the last agenda marker.")
3927 (defun org-agenda-new-marker (&optional pos)
3928 "Return a new agenda marker.
3929 Org-mode keeps a list of these markers and resets them when they are
3930 no longer in use."
3931 (let ((m (copy-marker (or pos (point)))))
3932 (setq org-agenda-last-marker-time (org-float-time))
3933 (if org-agenda-buffer
3934 (with-current-buffer org-agenda-buffer
3935 (push m org-agenda-markers))
3936 (push m org-agenda-markers))
3939 (defun org-agenda-reset-markers ()
3940 "Reset markers created by `org-agenda'."
3941 (while org-agenda-markers
3942 (move-marker (pop org-agenda-markers) nil)))
3944 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3945 "Save relative positions of markers in region.
3946 This check for agenda markers in all agenda buffers currently active."
3947 (dolist (buf (buffer-list))
3948 (with-current-buffer buf
3949 (when (eq major-mode 'org-agenda-mode)
3950 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3951 org-agenda-markers)))))
3953 ;;; Entry text mode
3955 (defun org-agenda-entry-text-show-here ()
3956 "Add some text from the entry as context to the current line."
3957 (let (m txt o)
3958 (setq m (org-get-at-bol 'org-hd-marker))
3959 (unless (marker-buffer m)
3960 (error "No marker points to an entry here"))
3961 (setq txt (concat "\n" (org-no-properties
3962 (org-agenda-get-some-entry-text
3963 m org-agenda-entry-text-maxlines
3964 org-agenda-entry-text-leaders))))
3965 (when (string-match "\\S-" txt)
3966 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3967 (overlay-put o 'evaporate t)
3968 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3969 (overlay-put o 'after-string txt))))
3971 (defun org-agenda-entry-text-show ()
3972 "Add entry context for all agenda lines."
3973 (interactive)
3974 (save-excursion
3975 (goto-char (point-max))
3976 (beginning-of-line 1)
3977 (while (not (bobp))
3978 (when (org-get-at-bol 'org-hd-marker)
3979 (org-agenda-entry-text-show-here))
3980 (beginning-of-line 0))))
3982 (defun org-agenda-entry-text-hide ()
3983 "Remove any shown entry context."
3984 (delq nil
3985 (mapcar (lambda (o)
3986 (if (eq (overlay-get o 'org-overlay-type)
3987 'agenda-entry-content)
3988 (progn (delete-overlay o) t)))
3989 (overlays-in (point-min) (point-max)))))
3991 (defun org-agenda-get-day-face (date)
3992 "Return the face DATE should be displayed with."
3993 (or (and (functionp org-agenda-day-face-function)
3994 (funcall org-agenda-day-face-function date))
3995 (cond ((org-agenda-todayp date)
3996 'org-agenda-date-today)
3997 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3998 'org-agenda-date-weekend)
3999 (t 'org-agenda-date))))
4001 ;;; Agenda timeline
4003 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
4004 (defvar org-agenda-show-log-scoped) ;; dynamically scope in `org-timeline' or `org-agenda-list'
4006 (defun org-timeline (&optional dotodo)
4007 "Show a time-sorted view of the entries in the current org file.
4008 Only entries with a time stamp of today or later will be listed. With
4009 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
4010 under the current date.
4011 If the buffer contains an active region, only check the region for
4012 dates."
4013 (interactive "P")
4014 (let* ((dopast t)
4015 (org-agenda-show-log-scoped org-agenda-show-log)
4016 (org-agenda-show-log org-agenda-show-log-scoped)
4017 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
4018 (current-buffer))))
4019 (date (calendar-current-date))
4020 (beg (if (org-region-active-p) (region-beginning) (point-min)))
4021 (end (if (org-region-active-p) (region-end) (point-max)))
4022 (day-numbers (org-get-all-dates
4023 beg end 'no-ranges
4024 t org-agenda-show-log-scoped ; always include today
4025 org-timeline-show-empty-dates))
4026 (org-deadline-warning-days 0)
4027 (org-agenda-only-exact-dates t)
4028 (today (org-today))
4029 (past t)
4030 args
4031 s e rtn d emptyp)
4032 (setq org-agenda-redo-command
4033 (list 'let
4034 (list (list 'org-agenda-show-log 'org-agenda-show-log))
4035 (list 'org-switch-to-buffer-other-window (current-buffer))
4036 (list 'org-timeline (list 'quote dotodo))))
4037 (put 'org-agenda-redo-command 'org-lprops nil)
4038 (if (not dopast)
4039 ;; Remove past dates from the list of dates.
4040 (setq day-numbers (delq nil (mapcar (lambda(x)
4041 (if (>= x today) x nil))
4042 day-numbers))))
4043 (org-agenda-prepare (concat "Timeline " (file-name-nondirectory entry)))
4044 (org-compile-prefix-format 'timeline)
4045 (org-set-sorting-strategy 'timeline)
4046 (if org-agenda-show-log-scoped (push :closed args))
4047 (push :timestamp args)
4048 (push :deadline args)
4049 (push :scheduled args)
4050 (push :sexp args)
4051 (if dotodo (push :todo args))
4052 (insert "Timeline of file " entry "\n")
4053 (add-text-properties (point-min) (point)
4054 (list 'face 'org-agenda-structure))
4055 (org-agenda-mark-header-line (point-min))
4056 (while (setq d (pop day-numbers))
4057 (if (and (listp d) (eq (car d) :omitted))
4058 (progn
4059 (setq s (point))
4060 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
4061 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
4062 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
4063 (if (and (>= d today)
4064 dopast
4065 past)
4066 (progn
4067 (setq past nil)
4068 (insert (make-string 79 ?-) "\n")))
4069 (setq date (calendar-gregorian-from-absolute d))
4070 (setq s (point))
4071 (setq rtn (and (not emptyp)
4072 (apply 'org-agenda-get-day-entries entry
4073 date args)))
4074 (if (or rtn (equal d today) org-timeline-show-empty-dates)
4075 (progn
4076 (insert
4077 (if (stringp org-agenda-format-date)
4078 (format-time-string org-agenda-format-date
4079 (org-time-from-absolute date))
4080 (funcall org-agenda-format-date date))
4081 "\n")
4082 (put-text-property s (1- (point)) 'face
4083 (org-agenda-get-day-face date))
4084 (put-text-property s (1- (point)) 'org-date-line t)
4085 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4086 (if (equal d today)
4087 (put-text-property s (1- (point)) 'org-today t))
4088 (and rtn (insert (org-agenda-finalize-entries rtn 'timeline) "\n"))
4089 (put-text-property s (1- (point)) 'day d)))))
4090 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
4091 (point-min)))
4092 (add-text-properties
4093 (point-min) (point-max)
4094 `(org-agenda-type timeline org-redo-cmd ,org-agenda-redo-command))
4095 (org-agenda-finalize)
4096 (setq buffer-read-only t)))
4098 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
4099 "Return a list of all relevant day numbers from BEG to END buffer positions.
4100 If NO-RANGES is non-nil, include only the start and end dates of a range,
4101 not every single day in the range. If FORCE-TODAY is non-nil, make
4102 sure that TODAY is included in the list. If INACTIVE is non-nil, also
4103 inactive time stamps (those in square brackets) are included.
4104 When EMPTY is non-nil, also include days without any entries."
4105 (let ((re (concat
4106 (if pre-re pre-re "")
4107 (if inactive org-ts-regexp-both org-ts-regexp)))
4108 dates dates1 date day day1 day2 ts1 ts2 pos)
4109 (if force-today
4110 (setq dates (list (org-today))))
4111 (save-excursion
4112 (goto-char beg)
4113 (while (re-search-forward re end t)
4114 (setq day (time-to-days (org-time-string-to-time
4115 (substring (match-string 1) 0 10)
4116 (current-buffer) (match-beginning 0))))
4117 (or (memq day dates) (push day dates)))
4118 (unless no-ranges
4119 (goto-char beg)
4120 (while (re-search-forward org-tr-regexp end t)
4121 (setq pos (match-beginning 0))
4122 (setq ts1 (substring (match-string 1) 0 10)
4123 ts2 (substring (match-string 2) 0 10)
4124 day1 (time-to-days (org-time-string-to-time
4125 ts1 (current-buffer) pos))
4126 day2 (time-to-days (org-time-string-to-time
4127 ts2 (current-buffer) pos)))
4128 (while (< (setq day1 (1+ day1)) day2)
4129 (or (memq day1 dates) (push day1 dates)))))
4130 (setq dates (sort dates '<))
4131 (when empty
4132 (while (setq day (pop dates))
4133 (setq day2 (car dates))
4134 (push day dates1)
4135 (when (and day2 empty)
4136 (if (or (eq empty t)
4137 (and (numberp empty) (<= (- day2 day) empty)))
4138 (while (< (setq day (1+ day)) day2)
4139 (push (list day) dates1))
4140 (push (cons :omitted (- day2 day)) dates1))))
4141 (setq dates (nreverse dates1)))
4142 dates)))
4144 ;;; Agenda Daily/Weekly
4146 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4147 "Start day for the agenda view.
4148 Custom commands can set this variable in the options section.
4149 This is usually a string like \"2007-11-01\", \"+2d\" or any other
4150 input allowed when reading a date through the Org calendar.
4151 See the docstring of `org-read-date' for details.")
4152 (defvar org-starting-day nil) ; local variable in the agenda buffer
4153 (defvar org-arg-loc nil) ; local variable
4155 (defvar org-agenda-buffer-tmp-name nil)
4156 ;;;###autoload
4157 (defun org-agenda-list (&optional arg start-day span with-hour)
4158 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4159 The view will be for the current day or week, but from the overview buffer
4160 you will be able to go to other days/weeks.
4162 With a numeric prefix argument in an interactive call, the agenda will
4163 span ARG days. Lisp programs should instead specify SPAN to change
4164 the number of days. SPAN defaults to `org-agenda-span'.
4166 START-DAY defaults to TODAY, or to the most recent match for the weekday
4167 given in `org-agenda-start-on-weekday'.
4169 When WITH-HOUR is non-nil, only include scheduled and deadline
4170 items if they have an hour specification like [h]h:mm."
4171 (interactive "P")
4172 (if org-agenda-overriding-arguments
4173 (setq arg (car org-agenda-overriding-arguments)
4174 start-day (nth 1 org-agenda-overriding-arguments)
4175 span (nth 2 org-agenda-overriding-arguments)))
4176 (if (and (integerp arg) (> arg 0))
4177 (setq span arg arg nil))
4178 (catch 'exit
4179 (setq org-agenda-buffer-name
4180 (or org-agenda-buffer-tmp-name
4181 (if org-agenda-sticky
4182 (cond ((and org-keys (stringp org-match))
4183 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4184 (org-keys
4185 (format "*Org Agenda(%s)*" org-keys))
4186 (t "*Org Agenda(a)*")))
4187 org-agenda-buffer-name))
4188 (org-agenda-prepare "Day/Week")
4189 (setq start-day (or start-day org-agenda-start-day))
4190 (if (stringp start-day)
4191 ;; Convert to an absolute day number
4192 (setq start-day (time-to-days (org-read-date nil t start-day))))
4193 (org-compile-prefix-format 'agenda)
4194 (org-set-sorting-strategy 'agenda)
4195 (let* ((span (org-agenda-ndays-to-span
4196 (or span org-agenda-ndays org-agenda-span)))
4197 (today (org-today))
4198 (sd (or start-day today))
4199 (ndays (org-agenda-span-to-ndays span sd))
4200 (org-agenda-start-on-weekday
4201 (if (or (eq ndays 7) (eq ndays 14))
4202 org-agenda-start-on-weekday))
4203 (thefiles (org-agenda-files nil 'ifmode))
4204 (files thefiles)
4205 (start (if (or (null org-agenda-start-on-weekday)
4206 (< ndays 7))
4208 (let* ((nt (calendar-day-of-week
4209 (calendar-gregorian-from-absolute sd)))
4210 (n1 org-agenda-start-on-weekday)
4211 (d (- nt n1)))
4212 (- sd (+ (if (< d 0) 7 0) d)))))
4213 (day-numbers (list start))
4214 (day-cnt 0)
4215 (inhibit-redisplay (not debug-on-error))
4216 (org-agenda-show-log-scoped org-agenda-show-log)
4217 s e rtn rtnall file date d start-pos end-pos todayp
4218 clocktable-start clocktable-end filter)
4219 (setq org-agenda-redo-command
4220 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4221 (dotimes (n (1- ndays))
4222 (push (1+ (car day-numbers)) day-numbers))
4223 (setq day-numbers (nreverse day-numbers))
4224 (setq clocktable-start (car day-numbers)
4225 clocktable-end (1+ (or (org-last day-numbers) 0)))
4226 (org-set-local 'org-starting-day (car day-numbers))
4227 (org-set-local 'org-arg-loc arg)
4228 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
4229 (unless org-agenda-compact-blocks
4230 (let* ((d1 (car day-numbers))
4231 (d2 (org-last day-numbers))
4232 (w1 (org-days-to-iso-week d1))
4233 (w2 (org-days-to-iso-week d2)))
4234 (setq s (point))
4235 (if org-agenda-overriding-header
4236 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4237 nil 'face 'org-agenda-structure) "\n")
4238 (insert (org-agenda-span-name span)
4239 "-agenda"
4240 (if (< (- d2 d1) 350)
4241 (if (= w1 w2)
4242 (format " (W%02d)" w1)
4243 (format " (W%02d-W%02d)" w1 w2))
4245 ":\n")))
4246 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4247 'org-date-line t))
4248 (org-agenda-mark-header-line s))
4249 (while (setq d (pop day-numbers))
4250 (setq date (calendar-gregorian-from-absolute d)
4251 s (point))
4252 (if (or (setq todayp (= d today))
4253 (and (not start-pos) (= d sd)))
4254 (setq start-pos (point))
4255 (if (and start-pos (not end-pos))
4256 (setq end-pos (point))))
4257 (setq files thefiles
4258 rtnall nil)
4259 (while (setq file (pop files))
4260 (catch 'nextfile
4261 (org-check-agenda-file file)
4262 (let ((org-agenda-entry-types org-agenda-entry-types))
4263 ;; Starred types override non-starred equivalents
4264 (when (member :deadline* org-agenda-entry-types)
4265 (setq org-agenda-entry-types
4266 (delq :deadline org-agenda-entry-types)))
4267 (when (member :scheduled* org-agenda-entry-types)
4268 (setq org-agenda-entry-types
4269 (delq :scheduled org-agenda-entry-types)))
4270 ;; Honor with-hour
4271 (when with-hour
4272 (when (member :deadline org-agenda-entry-types)
4273 (setq org-agenda-entry-types
4274 (delq :deadline org-agenda-entry-types))
4275 (push :deadline* org-agenda-entry-types))
4276 (when (member :scheduled org-agenda-entry-types)
4277 (setq org-agenda-entry-types
4278 (delq :scheduled org-agenda-entry-types))
4279 (push :scheduled* org-agenda-entry-types)))
4280 (unless org-agenda-include-deadlines
4281 (setq org-agenda-entry-types
4282 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4283 (cond
4284 ((memq org-agenda-show-log-scoped '(only clockcheck))
4285 (setq rtn (org-agenda-get-day-entries
4286 file date :closed)))
4287 (org-agenda-show-log-scoped
4288 (setq rtn (apply 'org-agenda-get-day-entries
4289 file date
4290 (append '(:closed) org-agenda-entry-types))))
4292 (setq rtn (apply 'org-agenda-get-day-entries
4293 file date
4294 org-agenda-entry-types)))))
4295 (setq rtnall (append rtnall rtn)))) ;; all entries
4296 (if org-agenda-include-diary
4297 (let ((org-agenda-search-headline-for-time t))
4298 (require 'diary-lib)
4299 (setq rtn (org-get-entries-from-diary date))
4300 (setq rtnall (append rtnall rtn))))
4301 (if (or rtnall org-agenda-show-all-dates)
4302 (progn
4303 (setq day-cnt (1+ day-cnt))
4304 (insert
4305 (if (stringp org-agenda-format-date)
4306 (format-time-string org-agenda-format-date
4307 (org-time-from-absolute date))
4308 (funcall org-agenda-format-date date))
4309 "\n")
4310 (put-text-property s (1- (point)) 'face
4311 (org-agenda-get-day-face date))
4312 (put-text-property s (1- (point)) 'org-date-line t)
4313 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4314 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4315 (when todayp
4316 (put-text-property s (1- (point)) 'org-today t))
4317 (setq rtnall
4318 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4319 (if rtnall (insert ;; all entries
4320 (org-agenda-finalize-entries rtnall 'agenda)
4321 "\n"))
4322 (put-text-property s (1- (point)) 'day d)
4323 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4324 (when (and org-agenda-clockreport-mode clocktable-start)
4325 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4326 ;; the above line is to ensure the restricted range!
4327 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4328 tbl)
4329 (setq p (org-plist-delete p :block))
4330 (setq p (plist-put p :tstart clocktable-start))
4331 (setq p (plist-put p :tend clocktable-end))
4332 (setq p (plist-put p :scope 'agenda))
4333 (when (and (eq org-agenda-clockreport-mode 'with-filter)
4334 (setq filter (or org-agenda-tag-filter-while-redo
4335 (get 'org-agenda-tag-filter :preset-filter))))
4336 (setq p (plist-put p :tags (mapconcat (lambda (x)
4337 (if (string-match "[<>=]" x)
4340 filter ""))))
4341 (setq tbl (apply 'org-clock-get-clocktable p))
4342 (insert tbl)))
4343 (goto-char (point-min))
4344 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4345 (unless (and (pos-visible-in-window-p (point-min))
4346 (pos-visible-in-window-p (point-max)))
4347 (goto-char (1- (point-max)))
4348 (recenter -1)
4349 (if (not (pos-visible-in-window-p (or start-pos 1)))
4350 (progn
4351 (goto-char (or start-pos 1))
4352 (recenter 1))))
4353 (goto-char (or start-pos 1))
4354 (add-text-properties (point-min) (point-max)
4355 `(org-agenda-type agenda
4356 org-last-args (,arg ,start-day ,span)
4357 org-redo-cmd ,org-agenda-redo-command
4358 org-series-cmd ,org-cmd))
4359 (if (eq org-agenda-show-log-scoped 'clockcheck)
4360 (org-agenda-show-clocking-issues))
4361 (org-agenda-finalize)
4362 (setq buffer-read-only t)
4363 (message ""))))
4365 (defun org-agenda-ndays-to-span (n)
4366 "Return a span symbol for a span of N days, or N if none matches."
4367 (cond ((symbolp n) n)
4368 ((= n 1) 'day)
4369 ((= n 7) 'week)
4370 ((= n 14) 'fortnight)
4371 (t n)))
4373 (defun org-agenda-span-to-ndays (span &optional start-day)
4374 "Return ndays from SPAN, possibly starting at START-DAY.
4375 START-DAY is an absolute time value."
4376 (cond ((numberp span) span)
4377 ((eq span 'day) 1)
4378 ((eq span 'week) 7)
4379 ((eq span 'fortnight) 14)
4380 ((eq span 'month)
4381 (let ((date (calendar-gregorian-from-absolute start-day)))
4382 (calendar-last-day-of-month (car date) (caddr date))))
4383 ((eq span 'year)
4384 (let ((date (calendar-gregorian-from-absolute start-day)))
4385 (if (calendar-leap-year-p (caddr date)) 366 365)))))
4387 (defun org-agenda-span-name (span)
4388 "Return a SPAN name."
4389 (if (null span)
4391 (if (symbolp span)
4392 (capitalize (symbol-name span))
4393 (format "%d days" span))))
4395 ;;; Agenda word search
4397 (defvar org-agenda-search-history nil)
4399 (defvar org-search-syntax-table nil
4400 "Special syntax table for org-mode search.
4401 In this table, we have single quotes not as word constituents, to
4402 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4404 (defvar org-mode-syntax-table) ; From org.el
4405 (defun org-search-syntax-table ()
4406 (unless org-search-syntax-table
4407 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4408 (modify-syntax-entry ?' "." org-search-syntax-table)
4409 (modify-syntax-entry ?` "." org-search-syntax-table))
4410 org-search-syntax-table)
4412 (defvar org-agenda-last-search-view-search-was-boolean nil)
4414 ;;;###autoload
4415 (defun org-search-view (&optional todo-only string edit-at)
4416 "Show all entries that contain a phrase or words or regular expressions.
4418 With optional prefix argument TODO-ONLY, only consider entries that are
4419 TODO entries. The argument STRING can be used to pass a default search
4420 string into this function. If EDIT-AT is non-nil, it means that the
4421 user should get a chance to edit this string, with cursor at position
4422 EDIT-AT.
4424 The search string can be viewed either as a phrase that should be found as
4425 is, or it can be broken into a number of snippets, each of which must match
4426 in a Boolean way to select an entry. The default depends on the variable
4427 `org-agenda-search-view-always-boolean'.
4428 Even if this is turned off (the default) you can always switch to
4429 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4431 The default is a direct search of the whole phrase, where each space in
4432 the search string can expand to an arbitrary amount of whitespace,
4433 including newlines.
4435 If using a Boolean search, the search string is split on whitespace and
4436 each snippet is searched separately, with logical AND to select an entry.
4437 Words prefixed with a minus must *not* occur in the entry. Words without
4438 a prefix or prefixed with a plus must occur in the entry. Matching is
4439 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4440 match whole words, not parts of a word) if
4441 `org-agenda-search-view-force-full-words' is set (default is nil).
4443 Boolean search snippets enclosed by curly braces are interpreted as
4444 regular expressions that must or (when preceded with \"-\") must not
4445 match in the entry. Snippets enclosed into double quotes will be taken
4446 as a whole, to include whitespace.
4448 - If the search string starts with an asterisk, search only in headlines.
4449 - If (possibly after the leading star) the search string starts with an
4450 exclamation mark, this also means to look at TODO entries only, an effect
4451 that can also be achieved with a prefix argument.
4452 - If (possibly after star and exclamation mark) the search string starts
4453 with a colon, this will mean that the (non-regexp) snippets of the
4454 Boolean search must match as full words.
4456 This command searches the agenda files, and in addition the files listed
4457 in `org-agenda-text-search-extra-files'."
4458 (interactive "P")
4459 (if org-agenda-overriding-arguments
4460 (setq todo-only (car org-agenda-overriding-arguments)
4461 string (nth 1 org-agenda-overriding-arguments)
4462 edit-at (nth 2 org-agenda-overriding-arguments)))
4463 (let* ((props (list 'face nil
4464 'done-face 'org-agenda-done
4465 'org-not-done-regexp org-not-done-regexp
4466 'org-todo-regexp org-todo-regexp
4467 'org-complex-heading-regexp org-complex-heading-regexp
4468 'mouse-face 'highlight
4469 'help-echo (format "mouse-2 or RET jump to location")))
4470 (full-words org-agenda-search-view-force-full-words)
4471 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4472 regexp rtn rtnall files file pos inherited-tags
4473 marker category level tags c neg re boolean
4474 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4475 (unless (and (not edit-at)
4476 (stringp string)
4477 (string-match "\\S-" string))
4478 (setq string (read-string
4479 (if org-agenda-search-view-always-boolean
4480 "[+-]Word/{Regexp} ...: "
4481 "Phrase or [+-]Word/{Regexp} ...: ")
4482 (cond
4483 ((integerp edit-at) (cons string edit-at))
4484 (edit-at string))
4485 'org-agenda-search-history)))
4486 (catch 'exit
4487 (if org-agenda-sticky
4488 (setq org-agenda-buffer-name
4489 (if (stringp string)
4490 (format "*Org Agenda(%s:%s)*"
4491 (or org-keys (or (and todo-only "S") "s")) string)
4492 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4493 (org-agenda-prepare "SEARCH")
4494 (org-compile-prefix-format 'search)
4495 (org-set-sorting-strategy 'search)
4496 (setq org-agenda-redo-command
4497 (list 'org-search-view (if todo-only t nil)
4498 (list 'if 'current-prefix-arg nil string)))
4499 (setq org-agenda-query-string string)
4500 (if (equal (string-to-char string) ?*)
4501 (setq hdl-only t
4502 words (substring string 1))
4503 (setq words string))
4504 (when (equal (string-to-char words) ?!)
4505 (setq todo-only t
4506 words (substring words 1)))
4507 (when (equal (string-to-char words) ?:)
4508 (setq full-words t
4509 words (substring words 1)))
4510 (if (or org-agenda-search-view-always-boolean
4511 (member (string-to-char words) '(?- ?+ ?\{)))
4512 (setq boolean t))
4513 (setq words (org-split-string words))
4514 (let (www w)
4515 (while (setq w (pop words))
4516 (while (and (string-match "\\\\\\'" w) words)
4517 (setq w (concat (substring w 0 -1) " " (pop words))))
4518 (push w www))
4519 (setq words (nreverse www) www nil)
4520 (while (setq w (pop words))
4521 (when (and (string-match "\\`[-+]?{" w)
4522 (not (string-match "}\\'" w)))
4523 (while (and words (not (string-match "}\\'" (car words))))
4524 (setq w (concat w " " (pop words))))
4525 (setq w (concat w " " (pop words))))
4526 (push w www))
4527 (setq words (nreverse www)))
4528 (setq org-agenda-last-search-view-search-was-boolean boolean)
4529 (when boolean
4530 (let (wds w)
4531 (while (setq w (pop words))
4532 (if (or (equal (substring w 0 1) "\"")
4533 (and (> (length w) 1)
4534 (member (substring w 0 1) '("+" "-"))
4535 (equal (substring w 1 2) "\"")))
4536 (while (and words (not (equal (substring w -1) "\"")))
4537 (setq w (concat w " " (pop words)))))
4538 (and (string-match "\\`\\([-+]?\\)\"" w)
4539 (setq w (replace-match "\\1" nil nil w)))
4540 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4541 (push w wds))
4542 (setq words (nreverse wds))))
4543 (if boolean
4544 (mapc (lambda (w)
4545 (setq c (string-to-char w))
4546 (if (equal c ?-)
4547 (setq neg t w (substring w 1))
4548 (if (equal c ?+)
4549 (setq neg nil w (substring w 1))
4550 (setq neg nil)))
4551 (if (string-match "\\`{.*}\\'" w)
4552 (setq re (substring w 1 -1))
4553 (if full-words
4554 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4555 (setq re (regexp-quote (downcase w)))))
4556 (if neg (push re regexps-) (push re regexps+)))
4557 words)
4558 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4559 regexps+))
4560 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4561 (if (not regexps+)
4562 (setq regexp org-outline-regexp-bol)
4563 (setq regexp (pop regexps+))
4564 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4565 regexp))))
4566 (setq files (org-agenda-files nil 'ifmode))
4567 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4568 (pop org-agenda-text-search-extra-files)
4569 (setq files (org-add-archive-files files)))
4570 (setq files (append files org-agenda-text-search-extra-files)
4571 rtnall nil)
4572 (while (setq file (pop files))
4573 (setq ee nil)
4574 (catch 'nextfile
4575 (org-check-agenda-file file)
4576 (setq buffer (if (file-exists-p file)
4577 (org-get-agenda-file-buffer file)
4578 (error "No such file %s" file)))
4579 (if (not buffer)
4580 ;; If file does not exist, make sure an error message is sent
4581 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4582 file))))
4583 (with-current-buffer buffer
4584 (with-syntax-table (org-search-syntax-table)
4585 (unless (derived-mode-p 'org-mode)
4586 (error "Agenda file %s is not in `org-mode'" file))
4587 (let ((case-fold-search t))
4588 (save-excursion
4589 (save-restriction
4590 (if (eq buffer org-agenda-restrict)
4591 (narrow-to-region org-agenda-restrict-begin
4592 org-agenda-restrict-end)
4593 (widen))
4594 (goto-char (point-min))
4595 (unless (or (org-at-heading-p)
4596 (outline-next-heading))
4597 (throw 'nextfile t))
4598 (goto-char (max (point-min) (1- (point))))
4599 (while (re-search-forward regexp nil t)
4600 (org-back-to-heading t)
4601 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4602 (> (org-reduced-level (org-outline-level))
4603 org-agenda-search-view-max-outline-level)
4604 (forward-line -1)
4605 (outline-back-to-heading t)))
4606 (skip-chars-forward "* ")
4607 (setq beg (point-at-bol)
4608 beg1 (point)
4609 end (progn
4610 (outline-next-heading)
4611 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4612 (> (org-reduced-level (org-outline-level))
4613 org-agenda-search-view-max-outline-level)
4614 (forward-line 1)
4615 (outline-next-heading)))
4616 (point)))
4618 (catch :skip
4619 (goto-char beg)
4620 (org-agenda-skip)
4621 (setq str (buffer-substring-no-properties
4622 (point-at-bol)
4623 (if hdl-only (point-at-eol) end)))
4624 (mapc (lambda (wr) (when (string-match wr str)
4625 (goto-char (1- end))
4626 (throw :skip t)))
4627 regexps-)
4628 (mapc (lambda (wr) (unless (string-match wr str)
4629 (goto-char (1- end))
4630 (throw :skip t)))
4631 (if todo-only
4632 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
4633 regexps+)
4634 regexps+))
4635 (goto-char beg)
4636 (setq marker (org-agenda-new-marker (point))
4637 category (org-get-category)
4638 level (make-string (org-reduced-level (org-outline-level)) ? )
4639 inherited-tags
4640 (or (eq org-agenda-show-inherited-tags 'always)
4641 (and (listp org-agenda-show-inherited-tags)
4642 (memq 'todo org-agenda-show-inherited-tags))
4643 (and (eq org-agenda-show-inherited-tags t)
4644 (or (eq org-agenda-use-tag-inheritance t)
4645 (memq 'todo org-agenda-use-tag-inheritance))))
4646 tags (org-get-tags-at nil (not inherited-tags))
4647 txt (org-agenda-format-item
4649 (buffer-substring-no-properties
4650 beg1 (point-at-eol))
4651 level category tags t))
4652 (org-add-props txt props
4653 'org-marker marker 'org-hd-marker marker
4654 'org-todo-regexp org-todo-regexp
4655 'level level
4656 'org-complex-heading-regexp org-complex-heading-regexp
4657 'priority 1000
4658 'type "search")
4659 (push txt ee)
4660 (goto-char (1- end))))))))))
4661 (setq rtn (nreverse ee))
4662 (setq rtnall (append rtnall rtn)))
4663 (if org-agenda-overriding-header
4664 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4665 nil 'face 'org-agenda-structure) "\n")
4666 (insert "Search words: ")
4667 (add-text-properties (point-min) (1- (point))
4668 (list 'face 'org-agenda-structure))
4669 (setq pos (point))
4670 (insert string "\n")
4671 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4672 (setq pos (point))
4673 (unless org-agenda-multi
4674 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
4675 (add-text-properties pos (1- (point))
4676 (list 'face 'org-agenda-structure))))
4677 (org-agenda-mark-header-line (point-min))
4678 (when rtnall
4679 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4680 (goto-char (point-min))
4681 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4682 (add-text-properties (point-min) (point-max)
4683 `(org-agenda-type search
4684 org-last-args (,todo-only ,string ,edit-at)
4685 org-redo-cmd ,org-agenda-redo-command
4686 org-series-cmd ,org-cmd))
4687 (org-agenda-finalize)
4688 (setq buffer-read-only t))))
4690 ;;; Agenda TODO list
4692 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4693 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4694 (concat
4695 (if (or (equal keywords "ALL") (not keywords))
4696 (propertize "ALL" 'face 'warning)
4697 (mapconcat
4698 (lambda (kw)
4699 (propertize kw 'face (org-get-todo-face kw)))
4700 (org-split-string keywords "|")
4701 "|"))
4702 "\n"))
4704 (defvar org-select-this-todo-keyword nil)
4705 (defvar org-last-arg nil)
4707 ;;;###autoload
4708 (defun org-todo-list (&optional arg)
4709 "Show all (not done) TODO entries from all agenda file in a single list.
4710 The prefix arg can be used to select a specific TODO keyword and limit
4711 the list to these. When using \\[universal-argument], you will be prompted
4712 for a keyword. A numeric prefix directly selects the Nth keyword in
4713 `org-todo-keywords-1'."
4714 (interactive "P")
4715 (if org-agenda-overriding-arguments
4716 (setq arg org-agenda-overriding-arguments))
4717 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4718 (let* ((today (org-today))
4719 (date (calendar-gregorian-from-absolute today))
4720 (kwds org-todo-keywords-for-agenda)
4721 (completion-ignore-case t)
4722 (org-select-this-todo-keyword
4723 (if (stringp arg) arg
4724 (and arg (integerp arg) (> arg 0)
4725 (nth (1- arg) kwds))))
4726 rtn rtnall files file pos)
4727 (when (equal arg '(4))
4728 (setq org-select-this-todo-keyword
4729 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4730 (mapcar 'list kwds) nil nil)))
4731 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4732 (catch 'exit
4733 (if org-agenda-sticky
4734 (setq org-agenda-buffer-name
4735 (if (stringp org-select-this-todo-keyword)
4736 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4737 org-select-this-todo-keyword)
4738 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4739 (org-agenda-prepare "TODO")
4740 (org-compile-prefix-format 'todo)
4741 (org-set-sorting-strategy 'todo)
4742 (setq org-agenda-redo-command
4743 `(org-todo-list (or (and (numberp current-prefix-arg)
4744 current-prefix-arg)
4745 ,org-select-this-todo-keyword
4746 current-prefix-arg ,arg)))
4747 (setq files (org-agenda-files nil 'ifmode)
4748 rtnall nil)
4749 (while (setq file (pop files))
4750 (catch 'nextfile
4751 (org-check-agenda-file file)
4752 (setq rtn (org-agenda-get-day-entries file date :todo))
4753 (setq rtnall (append rtnall rtn))))
4754 (if org-agenda-overriding-header
4755 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4756 nil 'face 'org-agenda-structure) "\n")
4757 (insert "Global list of TODO items of type: ")
4758 (add-text-properties (point-min) (1- (point))
4759 (list 'face 'org-agenda-structure
4760 'short-heading
4761 (concat "ToDo: "
4762 (or org-select-this-todo-keyword "ALL"))))
4763 (org-agenda-mark-header-line (point-min))
4764 (insert (org-agenda-propertize-selected-todo-keywords
4765 org-select-this-todo-keyword))
4766 (setq pos (point))
4767 (unless org-agenda-multi
4768 (insert "Available with `N r': (0)[ALL]")
4769 (let ((n 0) s)
4770 (mapc (lambda (x)
4771 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4772 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4773 (insert "\n "))
4774 (insert " " s))
4775 kwds))
4776 (insert "\n"))
4777 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4778 (org-agenda-mark-header-line (point-min))
4779 (when rtnall
4780 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4781 (goto-char (point-min))
4782 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4783 (add-text-properties (point-min) (point-max)
4784 `(org-agenda-type todo
4785 org-last-args ,arg
4786 org-redo-cmd ,org-agenda-redo-command
4787 org-series-cmd ,org-cmd))
4788 (org-agenda-finalize)
4789 (setq buffer-read-only t))))
4791 ;;; Agenda tags match
4793 ;;;###autoload
4794 (defun org-tags-view (&optional todo-only match)
4795 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4796 The prefix arg TODO-ONLY limits the search to TODO entries."
4797 (interactive "P")
4798 (if org-agenda-overriding-arguments
4799 (setq todo-only (car org-agenda-overriding-arguments)
4800 match (nth 1 org-agenda-overriding-arguments)))
4801 (let* ((org-tags-match-list-sublevels
4802 org-tags-match-list-sublevels)
4803 (completion-ignore-case t)
4804 rtn rtnall files file pos matcher
4805 buffer)
4806 (when (and (stringp match) (not (string-match "\\S-" match)))
4807 (setq match nil))
4808 (catch 'exit
4809 (if org-agenda-sticky
4810 (setq org-agenda-buffer-name
4811 (if (stringp match)
4812 (format "*Org Agenda(%s:%s)*"
4813 (or org-keys (or (and todo-only "M") "m")) match)
4814 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4815 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4816 ;; expanding tags within `org-make-tags-matcher'
4817 (org-agenda-prepare (concat "TAGS " match))
4818 (setq matcher (org-make-tags-matcher match)
4819 match (car matcher) matcher (cdr matcher))
4820 (org-compile-prefix-format 'tags)
4821 (org-set-sorting-strategy 'tags)
4822 (setq org-agenda-query-string match)
4823 (setq org-agenda-redo-command
4824 (list 'org-tags-view `(quote ,todo-only)
4825 (list 'if 'current-prefix-arg nil `(quote ,org-agenda-query-string))))
4826 (setq files (org-agenda-files nil 'ifmode)
4827 rtnall nil)
4828 (while (setq file (pop files))
4829 (catch 'nextfile
4830 (org-check-agenda-file file)
4831 (setq buffer (if (file-exists-p file)
4832 (org-get-agenda-file-buffer file)
4833 (error "No such file %s" file)))
4834 (if (not buffer)
4835 ;; If file does not exist, error message to agenda
4836 (setq rtn (list
4837 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4838 rtnall (append rtnall rtn))
4839 (with-current-buffer buffer
4840 (unless (derived-mode-p 'org-mode)
4841 (error "Agenda file %s is not in `org-mode'" file))
4842 (save-excursion
4843 (save-restriction
4844 (if (eq buffer org-agenda-restrict)
4845 (narrow-to-region org-agenda-restrict-begin
4846 org-agenda-restrict-end)
4847 (widen))
4848 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4849 (setq rtnall (append rtnall rtn))))))))
4850 (if org-agenda-overriding-header
4851 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4852 nil 'face 'org-agenda-structure) "\n")
4853 (insert "Headlines with TAGS match: ")
4854 (add-text-properties (point-min) (1- (point))
4855 (list 'face 'org-agenda-structure
4856 'short-heading
4857 (concat "Match: " match)))
4858 (setq pos (point))
4859 (insert match "\n")
4860 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4861 (setq pos (point))
4862 (unless org-agenda-multi
4863 (insert "Press `C-u r' to search again with new search string\n"))
4864 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4865 (org-agenda-mark-header-line (point-min))
4866 (when rtnall
4867 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4868 (goto-char (point-min))
4869 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4870 (add-text-properties (point-min) (point-max)
4871 `(org-agenda-type tags
4872 org-last-args (,todo-only ,match)
4873 org-redo-cmd ,org-agenda-redo-command
4874 org-series-cmd ,org-cmd))
4875 (org-agenda-finalize)
4876 (setq buffer-read-only t))))
4878 ;;; Agenda Finding stuck projects
4880 (defvar org-agenda-skip-regexp nil
4881 "Regular expression used in skipping subtrees for the agenda.
4882 This is basically a temporary global variable that can be set and then
4883 used by user-defined selections using `org-agenda-skip-function'.")
4885 (defvar org-agenda-overriding-header nil
4886 "When set during agenda, todo and tags searches it replaces the header.
4887 This variable should not be set directly, but custom commands can bind it
4888 in the options section.")
4890 (defun org-agenda-skip-entry-when-regexp-matches ()
4891 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4892 If yes, it returns the end position of this entry, causing agenda commands
4893 to skip the entry but continuing the search in the subtree. This is a
4894 function that can be put into `org-agenda-skip-function' for the duration
4895 of a command."
4896 (let ((end (save-excursion (org-end-of-subtree t)))
4897 skip)
4898 (save-excursion
4899 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4900 (and skip end)))
4902 (defun org-agenda-skip-subtree-when-regexp-matches ()
4903 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4904 If yes, it returns the end position of this tree, causing agenda commands
4905 to skip this subtree. This is a function that can be put into
4906 `org-agenda-skip-function' for the duration of a command."
4907 (let ((end (save-excursion (org-end-of-subtree t)))
4908 skip)
4909 (save-excursion
4910 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4911 (and skip end)))
4913 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4914 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4915 If yes, it returns the end position of the current entry (NOT the tree),
4916 causing agenda commands to skip the entry but continuing the search in
4917 the subtree. This is a function that can be put into
4918 `org-agenda-skip-function' for the duration of a command. An important
4919 use of this function is for the stuck project list."
4920 (let ((end (save-excursion (org-end-of-subtree t)))
4921 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4922 skip)
4923 (save-excursion
4924 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4925 (and skip entry-end)))
4927 (defun org-agenda-skip-entry-if (&rest conditions)
4928 "Skip entry if any of CONDITIONS is true.
4929 See `org-agenda-skip-if' for details."
4930 (org-agenda-skip-if nil conditions))
4932 (defun org-agenda-skip-subtree-if (&rest conditions)
4933 "Skip subtree if any of CONDITIONS is true.
4934 See `org-agenda-skip-if' for details."
4935 (org-agenda-skip-if t conditions))
4937 (defun org-agenda-skip-if (subtree conditions)
4938 "Checks current entity for CONDITIONS.
4939 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4940 the entry (i.e. the text before the next heading) is checked.
4942 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4943 from different tests. Valid conditions are:
4945 scheduled Check if there is a scheduled cookie
4946 notscheduled Check if there is no scheduled cookie
4947 deadline Check if there is a deadline
4948 notdeadline Check if there is no deadline
4949 timestamp Check if there is a timestamp (also deadline or scheduled)
4950 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4951 regexp Check if regexp matches
4952 notregexp Check if regexp does not match.
4953 todo Check if TODO keyword matches
4954 nottodo Check if TODO keyword does not match
4956 The regexp is taken from the conditions list, it must come right after
4957 the `regexp' or `notregexp' element.
4959 `todo' and `nottodo' accept as an argument a list of todo
4960 keywords, which may include \"*\" to match any todo keyword.
4962 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4964 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4966 Instead of a list, a keyword class may be given. For example:
4968 (org-agenda-skip-entry-if 'nottodo 'done)
4970 would skip entries that haven't been marked with any of \"DONE\"
4971 keywords. Possible classes are: `todo', `done', `any'.
4973 If any of these conditions is met, this function returns the end point of
4974 the entity, causing the search to continue from there. This is a function
4975 that can be put into `org-agenda-skip-function' for the duration of a command."
4976 (let (beg end m)
4977 (org-back-to-heading t)
4978 (setq beg (point)
4979 end (if subtree
4980 (progn (org-end-of-subtree t) (point))
4981 (progn (outline-next-heading) (1- (point)))))
4982 (goto-char beg)
4983 (and
4985 (and (memq 'scheduled conditions)
4986 (re-search-forward org-scheduled-time-regexp end t))
4987 (and (memq 'notscheduled conditions)
4988 (not (re-search-forward org-scheduled-time-regexp end t)))
4989 (and (memq 'deadline conditions)
4990 (re-search-forward org-deadline-time-regexp end t))
4991 (and (memq 'notdeadline conditions)
4992 (not (re-search-forward org-deadline-time-regexp end t)))
4993 (and (memq 'timestamp conditions)
4994 (re-search-forward org-ts-regexp end t))
4995 (and (memq 'nottimestamp conditions)
4996 (not (re-search-forward org-ts-regexp end t)))
4997 (and (setq m (memq 'regexp conditions))
4998 (stringp (nth 1 m))
4999 (re-search-forward (nth 1 m) end t))
5000 (and (setq m (memq 'notregexp conditions))
5001 (stringp (nth 1 m))
5002 (not (re-search-forward (nth 1 m) end t)))
5003 (and (or
5004 (setq m (memq 'nottodo conditions))
5005 (setq m (memq 'todo-unblocked conditions))
5006 (setq m (memq 'nottodo-unblocked conditions))
5007 (setq m (memq 'todo conditions)))
5008 (org-agenda-skip-if-todo m end)))
5009 end)))
5011 (defun org-agenda-skip-if-todo (args end)
5012 "Helper function for `org-agenda-skip-if', do not use it directly.
5013 ARGS is a list with first element either `todo', `nottodo',
5014 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
5015 a list of TODO keywords, or a state symbol `todo' or `done' or
5016 `any'."
5017 (let ((kw (car args))
5018 (arg (cadr args))
5019 todo-wds todo-re)
5020 (setq todo-wds
5021 (org-uniquify
5022 (cond
5023 ((listp arg) ;; list of keywords
5024 (if (member "*" arg)
5025 (mapcar 'substring-no-properties org-todo-keywords-1)
5026 arg))
5027 ((symbolp arg) ;; keyword class name
5028 (cond
5029 ((eq arg 'todo)
5030 (org-delete-all org-done-keywords
5031 (mapcar 'substring-no-properties
5032 org-todo-keywords-1)))
5033 ((eq arg 'done) org-done-keywords)
5034 ((eq arg 'any)
5035 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
5036 (setq todo-re
5037 (concat "^\\*+[ \t]+\\<\\("
5038 (mapconcat 'identity todo-wds "\\|")
5039 "\\)\\>"))
5040 (cond
5041 ((eq kw 'todo) (re-search-forward todo-re end t))
5042 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
5043 ((eq kw 'todo-unblocked)
5044 (catch 'unblocked
5045 (while (re-search-forward todo-re end t)
5046 (or (org-entry-blocked-p) (throw 'unblocked t)))
5047 nil))
5048 ((eq kw 'nottodo-unblocked)
5049 (catch 'unblocked
5050 (while (re-search-forward todo-re end t)
5051 (or (org-entry-blocked-p) (throw 'unblocked nil)))
5055 ;;;###autoload
5056 (defun org-agenda-list-stuck-projects (&rest ignore)
5057 "Create agenda view for projects that are stuck.
5058 Stuck projects are project that have no next actions. For the definitions
5059 of what a project is and how to check if it stuck, customize the variable
5060 `org-stuck-projects'."
5061 (interactive)
5062 (let* ((org-agenda-skip-function
5063 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
5064 ;; We could have used org-agenda-skip-if here.
5065 (org-agenda-overriding-header
5066 (or org-agenda-overriding-header "List of stuck projects: "))
5067 (matcher (nth 0 org-stuck-projects))
5068 (todo (nth 1 org-stuck-projects))
5069 (todo-wds (if (member "*" todo)
5070 (progn
5071 (org-agenda-prepare-buffers (org-agenda-files
5072 nil 'ifmode))
5073 (org-delete-all
5074 org-done-keywords-for-agenda
5075 (copy-sequence org-todo-keywords-for-agenda)))
5076 todo))
5077 (todo-re (concat "^\\*+[ \t]+\\("
5078 (mapconcat 'identity todo-wds "\\|")
5079 "\\)\\>"))
5080 (tags (nth 2 org-stuck-projects))
5081 (tags-re (if (member "*" tags)
5082 (concat org-outline-regexp-bol
5083 (org-re ".*:[[:alnum:]_@#%]+:[ \t]*$"))
5084 (if tags
5085 (concat org-outline-regexp-bol
5086 ".*:\\("
5087 (mapconcat 'identity tags "\\|")
5088 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
5089 (gen-re (nth 3 org-stuck-projects))
5090 (re-list
5091 (delq nil
5092 (list
5093 (if todo todo-re)
5094 (if tags tags-re)
5095 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
5096 gen-re)))))
5097 (setq org-agenda-skip-regexp
5098 (if re-list
5099 (mapconcat 'identity re-list "\\|")
5100 (error "No information how to identify unstuck projects")))
5101 (org-tags-view nil matcher)
5102 (setq org-agenda-buffer-name (buffer-name))
5103 (with-current-buffer org-agenda-buffer-name
5104 (setq org-agenda-redo-command
5105 `(org-agenda-list-stuck-projects ,current-prefix-arg)))))
5107 ;;; Diary integration
5109 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
5110 (defvar diary-list-entries-hook)
5111 (defvar diary-time-regexp)
5112 (defun org-get-entries-from-diary (date)
5113 "Get the (Emacs Calendar) diary entries for DATE."
5114 (require 'diary-lib)
5115 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
5116 (diary-display-hook '(fancy-diary-display))
5117 (diary-display-function 'fancy-diary-display)
5118 (pop-up-frames nil)
5119 (diary-list-entries-hook
5120 (cons 'org-diary-default-entry diary-list-entries-hook))
5121 (diary-file-name-prefix nil) ; turn this feature off
5122 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
5123 entries
5124 (org-disable-agenda-to-diary t))
5125 (save-excursion
5126 (save-window-excursion
5127 (funcall (if (fboundp 'diary-list-entries)
5128 'diary-list-entries 'list-diary-entries)
5129 date 1)))
5130 (if (not (get-buffer diary-fancy-buffer))
5131 (setq entries nil)
5132 (with-current-buffer diary-fancy-buffer
5133 (setq buffer-read-only nil)
5134 (if (zerop (buffer-size))
5135 ;; No entries
5136 (setq entries nil)
5137 ;; Omit the date and other unnecessary stuff
5138 (org-agenda-cleanup-fancy-diary)
5139 ;; Add prefix to each line and extend the text properties
5140 (if (zerop (buffer-size))
5141 (setq entries nil)
5142 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
5143 (setq entries
5144 (with-temp-buffer
5145 (insert entries) (goto-char (point-min))
5146 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5147 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5148 (replace-match (concat "; " (match-string 1)))))
5149 (buffer-string)))))
5150 (set-buffer-modified-p nil)
5151 (kill-buffer diary-fancy-buffer)))
5152 (when entries
5153 (setq entries (org-split-string entries "\n"))
5154 (setq entries
5155 (mapcar
5156 (lambda (x)
5157 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5158 ;; Extend the text properties to the beginning of the line
5159 (org-add-props x (text-properties-at (1- (length x)) x)
5160 'type "diary" 'date date 'face 'org-agenda-diary))
5161 entries)))))
5163 (defvar org-agenda-cleanup-fancy-diary-hook nil
5164 "Hook run when the fancy diary buffer is cleaned up.")
5166 (defun org-agenda-cleanup-fancy-diary ()
5167 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5168 This gets rid of the date, the underline under the date, and
5169 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5170 date. It also removes lines that contain only whitespace."
5171 (goto-char (point-min))
5172 (if (looking-at ".*?:[ \t]*")
5173 (progn
5174 (replace-match "")
5175 (re-search-forward "\n=+$" nil t)
5176 (replace-match "")
5177 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5178 (re-search-forward "\n=+$" nil t)
5179 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5180 (goto-char (point-min))
5181 (while (re-search-forward "^ +\n" nil t)
5182 (replace-match ""))
5183 (goto-char (point-min))
5184 (if (re-search-forward "^Org-mode dummy\n?" nil t)
5185 (replace-match ""))
5186 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5188 ;; Make sure entries from the diary have the right text properties.
5189 (eval-after-load "diary-lib"
5190 '(if (boundp 'diary-modify-entry-list-string-function)
5191 ;; We can rely on the hook, nothing to do
5193 ;; Hook not available, must use advice to make this work
5194 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5195 "Make the position visible."
5196 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5197 (stringp string)
5198 buffer-file-name)
5199 (setq string (org-modify-diary-entry-string string))))))
5201 (defun org-modify-diary-entry-string (string)
5202 "Add text properties to string, allowing org-mode to act on it."
5203 (org-add-props string nil
5204 'mouse-face 'highlight
5205 'help-echo (if buffer-file-name
5206 (format "mouse-2 or RET jump to diary file %s"
5207 (abbreviate-file-name buffer-file-name))
5209 'org-agenda-diary-link t
5210 'org-marker (org-agenda-new-marker (point-at-bol))))
5212 (defun org-diary-default-entry ()
5213 "Add a dummy entry to the diary.
5214 Needed to avoid empty dates which mess up holiday display."
5215 ;; Catch the error if dealing with the new add-to-diary-alist
5216 (when org-disable-agenda-to-diary
5217 (condition-case nil
5218 (org-add-to-diary-list original-date "Org-mode dummy" "")
5219 (error
5220 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
5222 (defun org-add-to-diary-list (&rest args)
5223 (if (fboundp 'diary-add-to-list)
5224 (apply 'diary-add-to-list args)
5225 (apply 'add-to-diary-list args)))
5227 (defvar org-diary-last-run-time nil)
5229 ;;;###autoload
5230 (defun org-diary (&rest args)
5231 "Return diary information from org files.
5232 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5233 It accesses org files and extracts information from those files to be
5234 listed in the diary. The function accepts arguments specifying what
5235 items should be listed. For a list of arguments allowed here, see the
5236 variable `org-agenda-entry-types'.
5238 The call in the diary file should look like this:
5240 &%%(org-diary) ~/path/to/some/orgfile.org
5242 Use a separate line for each org file to check. Or, if you omit the file name,
5243 all files listed in `org-agenda-files' will be checked automatically:
5245 &%%(org-diary)
5247 If you don't give any arguments (as in the example above), the default value
5248 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5249 So the example above may also be written as
5251 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5253 The function expects the lisp variables `entry' and `date' to be provided
5254 by the caller, because this is how the calendar works. Don't use this
5255 function from a program - use `org-agenda-get-day-entries' instead."
5256 (when (> (- (org-float-time)
5257 org-agenda-last-marker-time)
5259 ;; I am not sure if this works with sticky agendas, because the marker
5260 ;; list is then no longer a global variable.
5261 (org-agenda-reset-markers))
5262 (org-compile-prefix-format 'agenda)
5263 (org-set-sorting-strategy 'agenda)
5264 (setq args (or args org-agenda-entry-types))
5265 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5266 (list entry)
5267 (org-agenda-files t)))
5268 (time (org-float-time))
5269 file rtn results)
5270 (when (or (not org-diary-last-run-time)
5271 (> (- time
5272 org-diary-last-run-time)
5274 (org-agenda-prepare-buffers files))
5275 (setq org-diary-last-run-time time)
5276 ;; If this is called during org-agenda, don't return any entries to
5277 ;; the calendar. Org Agenda will list these entries itself.
5278 (if org-disable-agenda-to-diary (setq files nil))
5279 (while (setq file (pop files))
5280 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5281 (setq results (append results rtn)))
5282 (when results
5283 (setq results
5284 (mapcar (lambda (i) (replace-regexp-in-string
5285 org-bracket-link-regexp "\\3" i)) results))
5286 (concat (org-agenda-finalize-entries results) "\n"))))
5288 ;;; Agenda entry finders
5290 (defun org-agenda-get-day-entries (file date &rest args)
5291 "Does the work for `org-diary' and `org-agenda'.
5292 FILE is the path to a file to be checked for entries. DATE is date like
5293 the one returned by `calendar-current-date'. ARGS are symbols indicating
5294 which kind of entries should be extracted. For details about these, see
5295 the documentation of `org-diary'."
5296 (setq args (or args org-agenda-entry-types))
5297 (let* ((org-startup-folded nil)
5298 (org-startup-align-all-tables nil)
5299 (buffer (if (file-exists-p file)
5300 (org-get-agenda-file-buffer file)
5301 (error "No such file %s" file)))
5302 arg results rtn deadline-results)
5303 (if (not buffer)
5304 ;; If file does not exist, make sure an error message ends up in diary
5305 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5306 (with-current-buffer buffer
5307 (unless (derived-mode-p 'org-mode)
5308 (error "Agenda file %s is not in `org-mode'" file))
5309 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5310 (let ((case-fold-search nil))
5311 (save-excursion
5312 (save-restriction
5313 (if (eq buffer org-agenda-restrict)
5314 (narrow-to-region org-agenda-restrict-begin
5315 org-agenda-restrict-end)
5316 (widen))
5317 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
5318 (while (setq arg (pop args))
5319 (cond
5320 ((and (eq arg :todo)
5321 (equal date (calendar-gregorian-from-absolute
5322 (org-today))))
5323 (setq rtn (org-agenda-get-todos))
5324 (setq results (append results rtn)))
5325 ((eq arg :timestamp)
5326 (setq rtn (org-agenda-get-blocks))
5327 (setq results (append results rtn))
5328 (setq rtn (org-agenda-get-timestamps deadline-results))
5329 (setq results (append results rtn)))
5330 ((eq arg :sexp)
5331 (setq rtn (org-agenda-get-sexps))
5332 (setq results (append results rtn)))
5333 ((eq arg :scheduled)
5334 (setq rtn (org-agenda-get-scheduled deadline-results))
5335 (setq results (append results rtn)))
5336 ((eq arg :scheduled*)
5337 (setq rtn (org-agenda-get-scheduled deadline-results t))
5338 (setq results (append results rtn)))
5339 ((eq arg :closed)
5340 (setq rtn (org-agenda-get-progress))
5341 (setq results (append results rtn)))
5342 ((eq arg :deadline)
5343 (setq rtn (org-agenda-get-deadlines))
5344 (setq deadline-results (copy-sequence rtn))
5345 (setq results (append results rtn)))
5346 ((eq arg :deadline*)
5347 (setq rtn (org-agenda-get-deadlines t))
5348 (setq deadline-results (copy-sequence rtn))
5349 (setq results (append results rtn))))))))
5350 results))))
5352 (defsubst org-em (x y list)
5353 "Is X or Y a member of LIST?"
5354 (or (memq x list) (memq y list)))
5356 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5357 (defvar org-agenda-sorting-strategy-selected nil)
5359 (defun org-agenda-get-todos ()
5360 "Return the TODO information for agenda display."
5361 (let* ((props (list 'face nil
5362 'done-face 'org-agenda-done
5363 'org-not-done-regexp org-not-done-regexp
5364 'org-todo-regexp org-todo-regexp
5365 'org-complex-heading-regexp org-complex-heading-regexp
5366 'mouse-face 'highlight
5367 'help-echo
5368 (format "mouse-2 or RET jump to org file %s"
5369 (abbreviate-file-name buffer-file-name))))
5370 (regexp (format org-heading-keyword-regexp-format
5371 (cond
5372 ((and org-select-this-todo-keyword
5373 (equal org-select-this-todo-keyword "*"))
5374 org-todo-regexp)
5375 (org-select-this-todo-keyword
5376 (concat "\\("
5377 (mapconcat 'identity
5378 (org-split-string
5379 org-select-this-todo-keyword
5380 "|")
5381 "\\|") "\\)"))
5382 (t org-not-done-regexp))))
5383 marker priority category level tags todo-state ts-date ts-date-type
5384 ee txt beg end inherited-tags todo-state-end-pos)
5385 (goto-char (point-min))
5386 (while (re-search-forward regexp nil t)
5387 (catch :skip
5388 (save-match-data
5389 (beginning-of-line)
5390 (org-agenda-skip)
5391 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5392 (unless (and (setq todo-state (org-get-todo-state))
5393 (setq todo-state-end-pos (match-end 2)))
5394 (goto-char end)
5395 (throw :skip nil))
5396 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5397 (goto-char (1+ beg))
5398 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5399 (throw :skip nil)))
5400 (goto-char (match-beginning 2))
5401 (setq marker (org-agenda-new-marker (match-beginning 0))
5402 category (org-get-category)
5403 ts-date (let (ts)
5404 (save-match-data
5405 (cond ((org-em 'scheduled-up 'scheduled-down
5406 org-agenda-sorting-strategy-selected)
5407 (setq ts (org-entry-get (point) "SCHEDULED")
5408 ts-date-type " scheduled"))
5409 ((org-em 'deadline-up 'deadline-down
5410 org-agenda-sorting-strategy-selected)
5411 (setq ts (org-entry-get (point) "DEADLINE")
5412 ts-date-type " deadline"))
5413 ((org-em 'ts-up 'ts-down
5414 org-agenda-sorting-strategy-selected)
5415 (setq ts (org-entry-get (point) "TIMESTAMP")
5416 ts-date-type " timestamp"))
5417 ((org-em 'tsia-up 'tsia-down
5418 org-agenda-sorting-strategy-selected)
5419 (setq ts (org-entry-get (point) "TIMESTAMP_IA")
5420 ts-date-type " timestamp_ia"))
5421 ((org-em 'timestamp-up 'timestamp-down
5422 org-agenda-sorting-strategy-selected)
5423 (setq ts (or (org-entry-get (point) "SCHEDULED")
5424 (org-entry-get (point) "DEADLINE")
5425 (org-entry-get (point) "TIMESTAMP")
5426 (org-entry-get (point) "TIMESTAMP_IA"))
5427 ts-date-type ""))
5428 (t (setq ts-date-type "")))
5429 (when ts (ignore-errors (org-time-string-to-absolute ts)))))
5430 txt (org-trim (buffer-substring (match-beginning 2) (match-end 0)))
5431 inherited-tags
5432 (or (eq org-agenda-show-inherited-tags 'always)
5433 (and (listp org-agenda-show-inherited-tags)
5434 (memq 'todo org-agenda-show-inherited-tags))
5435 (and (eq org-agenda-show-inherited-tags t)
5436 (or (eq org-agenda-use-tag-inheritance t)
5437 (memq 'todo org-agenda-use-tag-inheritance))))
5438 tags (org-get-tags-at nil (not inherited-tags))
5439 level (make-string (org-reduced-level (org-outline-level)) ? )
5440 txt (org-agenda-format-item "" txt level category tags t)
5441 priority (1+ (org-get-priority txt)))
5442 (org-add-props txt props
5443 'org-marker marker 'org-hd-marker marker
5444 'priority priority
5445 'level level
5446 'ts-date ts-date
5447 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5448 (push txt ee)
5449 (if org-agenda-todo-list-sublevels
5450 (goto-char todo-state-end-pos)
5451 (org-end-of-subtree 'invisible))))
5452 (nreverse ee)))
5454 (defun org-agenda-todo-custom-ignore-p (time n)
5455 "Check whether timestamp is farther away than n number of days.
5456 This function is invoked if `org-agenda-todo-ignore-deadlines',
5457 `org-agenda-todo-ignore-scheduled' or
5458 `org-agenda-todo-ignore-timestamp' is set to an integer."
5459 (let ((days (org-time-stamp-to-now
5460 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5461 (if (>= n 0)
5462 (>= days n)
5463 (<= days n))))
5465 ;;;###autoload
5466 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5467 (&optional end)
5468 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5469 (when (or org-agenda-todo-ignore-with-date
5470 org-agenda-todo-ignore-scheduled
5471 org-agenda-todo-ignore-deadlines
5472 org-agenda-todo-ignore-timestamp)
5473 (setq end (or end (save-excursion (outline-next-heading) (point))))
5474 (save-excursion
5475 (or (and org-agenda-todo-ignore-with-date
5476 (re-search-forward org-ts-regexp end t))
5477 (and org-agenda-todo-ignore-scheduled
5478 (re-search-forward org-scheduled-time-regexp end t)
5479 (cond
5480 ((eq org-agenda-todo-ignore-scheduled 'future)
5481 (> (org-time-stamp-to-now
5482 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5483 ((eq org-agenda-todo-ignore-scheduled 'past)
5484 (<= (org-time-stamp-to-now
5485 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5486 ((numberp org-agenda-todo-ignore-scheduled)
5487 (org-agenda-todo-custom-ignore-p
5488 (match-string 1) org-agenda-todo-ignore-scheduled))
5489 (t)))
5490 (and org-agenda-todo-ignore-deadlines
5491 (re-search-forward org-deadline-time-regexp end t)
5492 (cond
5493 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5494 ((eq org-agenda-todo-ignore-deadlines 'far)
5495 (not (org-deadline-close (match-string 1))))
5496 ((eq org-agenda-todo-ignore-deadlines 'future)
5497 (> (org-time-stamp-to-now
5498 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5499 ((eq org-agenda-todo-ignore-deadlines 'past)
5500 (<= (org-time-stamp-to-now
5501 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5502 ((numberp org-agenda-todo-ignore-deadlines)
5503 (org-agenda-todo-custom-ignore-p
5504 (match-string 1) org-agenda-todo-ignore-deadlines))
5505 (t (org-deadline-close (match-string 1)))))
5506 (and org-agenda-todo-ignore-timestamp
5507 (let ((buffer (current-buffer))
5508 (regexp
5509 (concat
5510 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5511 (start (point)))
5512 ;; Copy current buffer into a temporary one
5513 (with-temp-buffer
5514 (insert-buffer-substring buffer start end)
5515 (goto-char (point-min))
5516 ;; Delete SCHEDULED and DEADLINE items
5517 (while (re-search-forward regexp end t)
5518 (delete-region (match-beginning 0) (match-end 0)))
5519 (goto-char (point-min))
5520 ;; No search for timestamp left
5521 (when (re-search-forward org-ts-regexp nil t)
5522 (cond
5523 ((eq org-agenda-todo-ignore-timestamp 'future)
5524 (> (org-time-stamp-to-now
5525 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5526 ((eq org-agenda-todo-ignore-timestamp 'past)
5527 (<= (org-time-stamp-to-now
5528 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5529 ((numberp org-agenda-todo-ignore-timestamp)
5530 (org-agenda-todo-custom-ignore-p
5531 (match-string 1) org-agenda-todo-ignore-timestamp))
5532 (t))))))))))
5534 (defun org-agenda-get-timestamps (&optional deadline-results)
5535 "Return the date stamp information for agenda display."
5536 (let* ((props (list 'face 'org-agenda-calendar-event
5537 'org-not-done-regexp org-not-done-regexp
5538 'org-todo-regexp org-todo-regexp
5539 'org-complex-heading-regexp org-complex-heading-regexp
5540 'mouse-face 'highlight
5541 'help-echo
5542 (format "mouse-2 or RET jump to org file %s"
5543 (abbreviate-file-name buffer-file-name))))
5544 (d1 (calendar-absolute-from-gregorian date))
5546 (deadline-position-alist
5547 (mapcar (lambda (a) (and (setq mm (get-text-property
5548 0 'org-hd-marker a))
5549 (cons (marker-position mm) a)))
5550 deadline-results))
5551 (remove-re org-ts-regexp)
5552 (regexp
5553 (concat
5554 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5555 (regexp-quote
5556 (substring
5557 (format-time-string
5558 (car org-time-stamp-formats)
5559 (apply 'encode-time ; DATE bound by calendar
5560 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5561 1 11))
5562 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5563 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5564 marker hdmarker deadlinep scheduledp clockp closedp inactivep
5565 donep tmp priority category level ee txt timestr tags
5566 b0 b3 e3 head todo-state end-of-match show-all warntime habitp
5567 inherited-tags ts-date)
5568 (goto-char (point-min))
5569 (while (setq end-of-match (re-search-forward regexp nil t))
5570 (setq b0 (match-beginning 0)
5571 b3 (match-beginning 3) e3 (match-end 3)
5572 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
5573 habitp (and (functionp 'org-is-habit-p) (save-match-data (org-is-habit-p)))
5574 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5575 (member todo-state
5576 org-agenda-repeating-timestamp-show-all)))
5577 (catch :skip
5578 (and (org-at-date-range-p) (throw :skip nil))
5579 (org-agenda-skip)
5580 (if (and (match-end 1)
5581 (not (= d1 (org-time-string-to-absolute
5582 (match-string 1) d1 nil show-all
5583 (current-buffer) b0))))
5584 (throw :skip nil))
5585 (if (and e3
5586 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
5587 (throw :skip nil))
5588 (setq tmp (buffer-substring (max (point-min)
5589 (- b0 org-ds-keyword-length))
5591 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
5592 inactivep (= (char-after b0) ?\[)
5593 deadlinep (string-match org-deadline-regexp tmp)
5594 scheduledp (string-match org-scheduled-regexp tmp)
5595 closedp (and org-agenda-include-inactive-timestamps
5596 (string-match org-closed-string tmp))
5597 clockp (and org-agenda-include-inactive-timestamps
5598 (or (string-match org-clock-string tmp)
5599 (string-match "]-+\\'" tmp)))
5600 warntime (get-text-property (point) 'org-appt-warntime)
5601 donep (member todo-state org-done-keywords))
5602 (if (or scheduledp deadlinep closedp clockp
5603 (and donep org-agenda-skip-timestamp-if-done))
5604 (throw :skip t))
5605 (if (string-match ">" timestr)
5606 ;; substring should only run to end of time stamp
5607 (setq timestr (substring timestr 0 (match-end 0))))
5608 (setq marker (org-agenda-new-marker b0)
5609 category (org-get-category b0))
5610 (save-excursion
5611 (if (not (re-search-backward org-outline-regexp-bol nil t))
5612 (throw :skip nil)
5613 (goto-char (match-beginning 0))
5614 (if (and (eq t org-agenda-skip-timestamp-if-deadline-is-shown)
5615 (assoc (point) deadline-position-alist))
5616 (throw :skip nil))
5617 (setq hdmarker (org-agenda-new-marker)
5618 inherited-tags
5619 (or (eq org-agenda-show-inherited-tags 'always)
5620 (and (listp org-agenda-show-inherited-tags)
5621 (memq 'agenda org-agenda-show-inherited-tags))
5622 (and (eq org-agenda-show-inherited-tags t)
5623 (or (eq org-agenda-use-tag-inheritance t)
5624 (memq 'agenda org-agenda-use-tag-inheritance))))
5625 tags (org-get-tags-at nil (not inherited-tags))
5626 level (make-string (org-reduced-level (org-outline-level)) ? ))
5627 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5628 (setq head (or (match-string 1) ""))
5629 (setq txt (org-agenda-format-item
5630 (if inactivep org-agenda-inactive-leader nil)
5631 head level category tags timestr
5632 remove-re habitp)))
5633 (setq priority (org-get-priority txt))
5634 (org-add-props txt props 'priority priority
5635 'org-marker marker 'org-hd-marker hdmarker
5636 'date date
5637 'level level
5638 'ts-date
5639 (ignore-errors (org-time-string-to-absolute timestr))
5640 'todo-state todo-state
5641 'warntime warntime
5642 'type "timestamp")
5643 (push txt ee))
5644 (if org-agenda-skip-additional-timestamps-same-entry
5645 (outline-next-heading)
5646 (goto-char end-of-match))))
5647 (nreverse ee)))
5649 (defun org-agenda-get-sexps ()
5650 "Return the sexp information for agenda display."
5651 (require 'diary-lib)
5652 (let* ((props (list 'face 'org-agenda-calendar-sexp
5653 'mouse-face 'highlight
5654 'help-echo
5655 (format "mouse-2 or RET jump to org file %s"
5656 (abbreviate-file-name buffer-file-name))))
5657 (regexp "^&?%%(")
5658 marker category extra level ee txt tags entry
5659 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5660 (goto-char (point-min))
5661 (while (re-search-forward regexp nil t)
5662 (catch :skip
5663 (org-agenda-skip)
5664 (setq beg (match-beginning 0))
5665 (goto-char (1- (match-end 0)))
5666 (setq b (point))
5667 (forward-sexp 1)
5668 (setq sexp (buffer-substring b (point)))
5669 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5670 (org-trim (match-string 1))
5671 ""))
5672 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5673 (when result
5674 (setq marker (org-agenda-new-marker beg)
5675 level (make-string (org-reduced-level (org-outline-level)) ? )
5676 category (org-get-category beg)
5677 inherited-tags
5678 (or (eq org-agenda-show-inherited-tags 'always)
5679 (and (listp org-agenda-show-inherited-tags)
5680 (memq 'agenda org-agenda-show-inherited-tags))
5681 (and (eq org-agenda-show-inherited-tags t)
5682 (or (eq org-agenda-use-tag-inheritance t)
5683 (memq 'agenda org-agenda-use-tag-inheritance))))
5684 tags (org-get-tags-at nil (not inherited-tags))
5685 todo-state (org-get-todo-state)
5686 warntime (get-text-property (point) 'org-appt-warntime)
5687 extra nil)
5689 (dolist (r (if (stringp result)
5690 (list result)
5691 result)) ;; we expect a list here
5692 (when (and org-agenda-diary-sexp-prefix
5693 (string-match org-agenda-diary-sexp-prefix r))
5694 (setq extra (match-string 0 r)
5695 r (replace-match "" nil nil r)))
5696 (if (string-match "\\S-" r)
5697 (setq txt r)
5698 (setq txt "SEXP entry returned empty string"))
5699 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5700 (org-add-props txt props 'org-marker marker
5701 'date date 'todo-state todo-state
5702 'tags tags 'level level
5703 'type "sexp" 'warntime warntime)
5704 (push txt ee)))))
5705 (nreverse ee)))
5707 ;; Calendar sanity: define some functions that are independent of
5708 ;; `calendar-date-style'.
5709 ;; Normally I would like to use ISO format when calling the diary functions,
5710 ;; but to make sure we still have Emacs 22 compatibility we bind
5711 ;; also `european-calendar-style' and use european format
5712 (defun org-anniversary (year month day &optional mark)
5713 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5714 (org-no-warnings
5715 (let ((calendar-date-style 'european) (european-calendar-style t))
5716 (diary-anniversary day month year mark))))
5717 (defun org-cyclic (N year month day &optional mark)
5718 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5719 (org-no-warnings
5720 (let ((calendar-date-style 'european) (european-calendar-style t))
5721 (diary-cyclic N day month year mark))))
5722 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5723 "Like `diary-block', but with fixed (ISO) order of arguments."
5724 (org-no-warnings
5725 (let ((calendar-date-style 'european) (european-calendar-style t))
5726 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
5727 (defun org-date (year month day &optional mark)
5728 "Like `diary-date', but with fixed (ISO) order of arguments."
5729 (org-no-warnings
5730 (let ((calendar-date-style 'european) (european-calendar-style t))
5731 (diary-date day month year mark))))
5733 ;; Define the` org-class' function
5734 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5735 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5736 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5737 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5738 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5739 `holidays', then any date that is known by the Emacs calendar to be a
5740 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5741 then those holidays will be skipped."
5742 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5743 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5744 (d (calendar-absolute-from-gregorian date))
5745 (h (when skip-weeks (calendar-check-holidays date))))
5746 (and
5747 (<= date1 d)
5748 (<= d date2)
5749 (= (calendar-day-of-week date) dayname)
5750 (or (not skip-weeks)
5751 (progn
5752 (require 'cal-iso)
5753 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5754 (not (or (and h (memq 'holidays skip-weeks))
5755 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5756 entry)))
5758 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
5759 "Like `org-class', but honor `calendar-date-style'.
5760 The order of the first 2 times 3 arguments depends on the variable
5761 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
5762 So for American calendars, give this as MONTH DAY YEAR, for European as
5763 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
5764 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
5765 is any number of ISO weeks in the block period for which the item should
5766 be skipped.
5768 This function is here only for backward compatibility and it is deprecated,
5769 please use `org-class' instead."
5770 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
5771 (date2 (org-order-calendar-date-args m2 d2 y2)))
5772 (org-class
5773 (nth 2 date1) (car date1) (nth 1 date1)
5774 (nth 2 date2) (car date2) (nth 1 date2)
5775 dayname skip-weeks)))
5776 (make-obsolete 'org-diary-class 'org-class "")
5778 (defalias 'org-get-closed 'org-agenda-get-progress)
5779 (defun org-agenda-get-progress ()
5780 "Return the logged TODO entries for agenda display."
5781 (let* ((props (list 'mouse-face 'highlight
5782 'org-not-done-regexp org-not-done-regexp
5783 'org-todo-regexp org-todo-regexp
5784 'org-complex-heading-regexp org-complex-heading-regexp
5785 'help-echo
5786 (format "mouse-2 or RET jump to org file %s"
5787 (abbreviate-file-name buffer-file-name))))
5788 (items (if (consp org-agenda-show-log-scoped)
5789 org-agenda-show-log-scoped
5790 (if (eq org-agenda-show-log-scoped 'clockcheck)
5791 '(clock)
5792 org-agenda-log-mode-items)))
5793 (parts
5794 (delq nil
5795 (list
5796 (if (memq 'closed items) (concat "\\<" org-closed-string))
5797 (if (memq 'clock items) (concat "\\<" org-clock-string))
5798 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5799 (parts-re (if parts (mapconcat 'identity parts "\\|")
5800 (error "`org-agenda-log-mode-items' is empty")))
5801 (regexp (concat
5802 "\\(" parts-re "\\)"
5803 " *\\["
5804 (regexp-quote
5805 (substring
5806 (format-time-string
5807 (car org-time-stamp-formats)
5808 (apply 'encode-time ; DATE bound by calendar
5809 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5810 1 11))))
5811 (org-agenda-search-headline-for-time nil)
5812 marker hdmarker priority category level tags closedp
5813 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5814 (goto-char (point-min))
5815 (while (re-search-forward regexp nil t)
5816 (catch :skip
5817 (org-agenda-skip)
5818 (setq marker (org-agenda-new-marker (match-beginning 0))
5819 closedp (equal (match-string 1) org-closed-string)
5820 statep (equal (string-to-char (match-string 1)) ?-)
5821 clockp (not (or closedp statep))
5822 state (and statep (match-string 2))
5823 category (org-get-category (match-beginning 0))
5824 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5825 (when (string-match "\\]" timestr)
5826 ;; substring should only run to end of time stamp
5827 (setq rest (substring timestr (match-end 0))
5828 timestr (substring timestr 0 (match-end 0)))
5829 (if (and (not closedp) (not statep)
5830 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5831 rest))
5832 (progn (setq timestr (concat (substring timestr 0 -1)
5833 "-" (match-string 1 rest) "]"))
5834 (setq clocked (match-string 2 rest)))
5835 (setq clocked "-")))
5836 (save-excursion
5837 (setq extra
5838 (cond
5839 ((not org-agenda-log-mode-add-notes) nil)
5840 (statep
5841 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5842 (match-string 1)))
5843 (clockp
5844 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5845 (match-string 1)))))
5846 (if (not (re-search-backward org-outline-regexp-bol nil t))
5847 (throw :skip nil)
5848 (goto-char (match-beginning 0))
5849 (setq hdmarker (org-agenda-new-marker)
5850 inherited-tags
5851 (or (eq org-agenda-show-inherited-tags 'always)
5852 (and (listp org-agenda-show-inherited-tags)
5853 (memq 'todo org-agenda-show-inherited-tags))
5854 (and (eq org-agenda-show-inherited-tags t)
5855 (or (eq org-agenda-use-tag-inheritance t)
5856 (memq 'todo org-agenda-use-tag-inheritance))))
5857 tags (org-get-tags-at nil (not inherited-tags))
5858 level (make-string (org-reduced-level (org-outline-level)) ? ))
5859 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5860 (setq txt (match-string 1))
5861 (when extra
5862 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5863 (setq txt (concat (substring txt 0 (match-beginning 1))
5864 " - " extra " " (match-string 2 txt)))
5865 (setq txt (concat txt " - " extra))))
5866 (setq txt (org-agenda-format-item
5867 (cond
5868 (closedp "Closed: ")
5869 (statep (concat "State: (" state ")"))
5870 (t (concat "Clocked: (" clocked ")")))
5871 txt level category tags timestr)))
5872 (setq priority 100000)
5873 (org-add-props txt props
5874 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5875 'priority priority 'level level
5876 'type "closed" 'date date
5877 'undone-face 'org-warning 'done-face 'org-agenda-done)
5878 (push txt ee))
5879 (goto-char (point-at-eol))))
5880 (nreverse ee)))
5882 (defun org-agenda-show-clocking-issues ()
5883 "Add overlays, showing issues with clocking.
5884 See also the user option `org-agenda-clock-consistency-checks'."
5885 (interactive)
5886 (let* ((org-time-clocksum-use-effort-durations nil)
5887 (pl org-agenda-clock-consistency-checks)
5888 (re (concat "^[ \t]*"
5889 org-clock-string
5890 "[ \t]+"
5891 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5892 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5893 (tlstart 0.)
5894 (tlend 0.)
5895 (maxtime (org-hh:mm-string-to-minutes
5896 (or (plist-get pl :max-duration) "24:00")))
5897 (mintime (org-hh:mm-string-to-minutes
5898 (or (plist-get pl :min-duration) 0)))
5899 (maxgap (org-hh:mm-string-to-minutes
5900 ;; default 30:00 means never complain
5901 (or (plist-get pl :max-gap) "30:00")))
5902 (gapok (mapcar 'org-hh:mm-string-to-minutes
5903 (plist-get pl :gap-ok-around)))
5904 (def-face (or (plist-get pl :default-face)
5905 '((:background "DarkRed") (:foreground "white"))))
5906 issue face m te ts dt ov)
5907 (goto-char (point-min))
5908 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5909 (setq issue nil face def-face)
5910 (catch 'next
5911 (setq m (org-get-at-bol 'org-marker)
5912 te nil ts nil)
5913 (unless (and m (markerp m))
5914 (setq issue "No valid clock line") (throw 'next t))
5915 (org-with-point-at m
5916 (save-excursion
5917 (goto-char (point-at-bol))
5918 (unless (looking-at re)
5919 (error "No valid Clock line")
5920 (throw 'next t))
5921 (unless (match-end 3)
5922 (setq issue "No end time"
5923 face (or (plist-get pl :no-end-time-face) face))
5924 (throw 'next t))
5925 (setq ts (match-string 1)
5926 te (match-string 3)
5927 ts (org-float-time
5928 (apply 'encode-time (org-parse-time-string ts)))
5929 te (org-float-time
5930 (apply 'encode-time (org-parse-time-string te)))
5931 dt (- te ts))))
5932 (cond
5933 ((> dt (* 60 maxtime))
5934 ;; a very long clocking chunk
5935 (setq issue (format "Clocking interval is very long: %s"
5936 (org-minutes-to-clocksum-string
5937 (floor (/ (float dt) 60.))))
5938 face (or (plist-get pl :long-face) face)))
5939 ((< dt (* 60 mintime))
5940 ;; a very short clocking chunk
5941 (setq issue (format "Clocking interval is very short: %s"
5942 (org-minutes-to-clocksum-string
5943 (floor (/ (float dt) 60.))))
5944 face (or (plist-get pl :short-face) face)))
5945 ((and (> tlend 0) (< ts tlend))
5946 ;; Two clock entries are overlapping
5947 (setq issue (format "Clocking overlap: %d minutes"
5948 (/ (- tlend ts) 60))
5949 face (or (plist-get pl :overlap-face) face)))
5950 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5951 ;; There is a gap, lets see if we need to report it
5952 (unless (org-agenda-check-clock-gap tlend ts gapok)
5953 (setq issue (format "Clocking gap: %d minutes"
5954 (/ (- ts tlend) 60))
5955 face (or (plist-get pl :gap-face) face))))
5956 (t nil)))
5957 (setq tlend (or te tlend) tlstart (or ts tlstart))
5958 (when issue
5959 ;; OK, there was some issue, add an overlay to show the issue
5960 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5961 (overlay-put ov 'before-string
5962 (concat
5963 (org-add-props
5964 (format "%-43s" (concat " " issue))
5966 'face face)
5967 "\n"))
5968 (overlay-put ov 'evaporate t)))))
5970 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5971 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5972 (catch 'exit
5973 (unless ok-list
5974 ;; there are no OK times for gaps...
5975 (throw 'exit nil))
5976 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5977 ;; This is more than 24 hours, so it is OK.
5978 ;; because we have at least one OK time, that must be in the
5979 ;; 24 hour interval.
5980 (throw 'exit t))
5981 ;; We have a shorter gap.
5982 ;; Now we have to get the minute of the day when these times are
5983 (let* ((t1dec (decode-time (seconds-to-time t1)))
5984 (t2dec (decode-time (seconds-to-time t2)))
5985 ;; compute the minute on the day
5986 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5987 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5988 (when (< min2 min1)
5989 ;; if min2 is smaller than min1, this means it is on the next day.
5990 ;; Wrap it to after midnight.
5991 (setq min2 (+ min2 1440)))
5992 ;; Now check if any of the OK times is in the gap
5993 (mapc (lambda (x)
5994 ;; Wrap the time to after midnight if necessary
5995 (if (< x min1) (setq x (+ x 1440)))
5996 ;; Check if in interval
5997 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5998 ok-list)
5999 ;; Nope, this gap is not OK
6000 nil)))
6002 (defun org-agenda-get-deadlines (&optional with-hour)
6003 "Return the deadline information for agenda display.
6004 When WITH-HOUR is non-nil, only return deadlines with an hour
6005 specification like [h]h:mm."
6006 (let* ((props (list 'mouse-face 'highlight
6007 'org-not-done-regexp org-not-done-regexp
6008 'org-todo-regexp org-todo-regexp
6009 'org-complex-heading-regexp org-complex-heading-regexp
6010 'help-echo
6011 (format "mouse-2 or RET jump to org file %s"
6012 (abbreviate-file-name buffer-file-name))))
6013 (regexp (if with-hour
6014 org-deadline-time-hour-regexp
6015 org-deadline-time-regexp))
6016 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
6017 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
6018 (dl0 (car org-agenda-deadline-leaders))
6019 (dl1 (nth 1 org-agenda-deadline-leaders))
6020 (dl2 (or (nth 2 org-agenda-deadline-leaders) dl1))
6021 d2 diff dfrac wdays pos pos1 category level
6022 tags suppress-prewarning ee txt head face s todo-state
6023 show-all upcomingp donep timestr warntime inherited-tags ts-date)
6024 (goto-char (point-min))
6025 (while (re-search-forward regexp nil t)
6026 (catch :skip
6027 (org-agenda-skip)
6028 (setq s (match-string 1)
6029 txt nil
6030 pos (1- (match-beginning 1))
6031 todo-state (save-match-data (org-get-todo-state))
6032 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6033 (member todo-state
6034 org-agenda-repeating-timestamp-show-all))
6035 d2 (org-time-string-to-absolute
6036 s d1 'past show-all (current-buffer) pos)
6037 diff (- d2 d1))
6038 (setq suppress-prewarning
6039 (let ((ds (and org-agenda-skip-deadline-prewarning-if-scheduled
6040 (let ((item (buffer-substring (point-at-bol)
6041 (point-at-eol))))
6042 (save-match-data
6043 (and (string-match
6044 org-scheduled-time-regexp item)
6045 (match-string 1 item)))))))
6046 (cond
6047 ((not ds) nil)
6048 ;; The current item has a scheduled date (in ds), so
6049 ;; evaluate its prewarning lead time.
6050 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
6051 ;; Use global prewarning-restart lead time.
6052 org-agenda-skip-deadline-prewarning-if-scheduled)
6053 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
6054 'pre-scheduled)
6055 ;; Set prewarning to no earlier than scheduled.
6056 (min (- d2 (org-time-string-to-absolute
6057 ds d1 'past show-all (current-buffer) pos))
6058 org-deadline-warning-days))
6059 ;; Set prewarning to deadline.
6060 (t 0))))
6061 (setq wdays (if suppress-prewarning
6062 (let ((org-deadline-warning-days suppress-prewarning))
6063 (org-get-wdays s))
6064 (org-get-wdays s))
6065 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
6066 upcomingp (and todayp (> diff 0)))
6067 ;; When to show a deadline in the calendar:
6068 ;; If the expiration is within wdays warning time.
6069 ;; Past-due deadlines are only shown on the current date
6070 (if (and (or (and (<= diff wdays)
6071 (and todayp (not org-agenda-only-exact-dates)))
6072 (= diff 0)))
6073 (save-excursion
6074 ;; (setq todo-state (org-get-todo-state))
6075 (setq donep (member todo-state org-done-keywords))
6076 (if (and donep
6077 (or org-agenda-skip-deadline-if-done
6078 (not (= diff 0))))
6079 (setq txt nil)
6080 (setq category (org-get-category)
6081 warntime (get-text-property (point) 'org-appt-warntime))
6082 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6083 (throw :skip nil)
6084 (goto-char (match-end 0))
6085 (setq pos1 (match-beginning 0))
6086 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6087 (setq inherited-tags
6088 (or (eq org-agenda-show-inherited-tags 'always)
6089 (and (listp org-agenda-show-inherited-tags)
6090 (memq 'agenda org-agenda-show-inherited-tags))
6091 (and (eq org-agenda-show-inherited-tags t)
6092 (or (eq org-agenda-use-tag-inheritance t)
6093 (memq 'agenda org-agenda-use-tag-inheritance))))
6094 tags (org-get-tags-at pos1 (not inherited-tags)))
6095 (setq head (buffer-substring
6096 (point)
6097 (progn (skip-chars-forward "^\r\n")
6098 (point))))
6099 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6100 (setq timestr
6101 (concat (substring s (match-beginning 1)) " "))
6102 (setq timestr 'time))
6103 (setq txt (org-agenda-format-item
6104 (cond ((= diff 0) dl0)
6105 ((> diff 0)
6106 (if (functionp dl1)
6107 (funcall dl1 diff date)
6108 (format dl1 diff)))
6110 (if (functionp dl2)
6111 (funcall dl2 diff date)
6112 (format dl2 (if (string= dl2 dl1)
6113 diff (abs diff))))))
6114 head level category tags
6115 (if (not (= diff 0)) nil timestr)))))
6116 (when txt
6117 (setq face (org-agenda-deadline-face dfrac))
6118 (org-add-props txt props
6119 'org-marker (org-agenda-new-marker pos)
6120 'warntime warntime
6121 'level level
6122 'ts-date d2
6123 'org-hd-marker (org-agenda-new-marker pos1)
6124 'priority (+ (- diff)
6125 (org-get-priority txt))
6126 'todo-state todo-state
6127 'type (if upcomingp "upcoming-deadline" "deadline")
6128 'date (if upcomingp date d2)
6129 'face (if donep 'org-agenda-done face)
6130 'undone-face face 'done-face 'org-agenda-done)
6131 (push txt ee))))))
6132 (nreverse ee)))
6134 (defun org-agenda-deadline-face (fraction)
6135 "Return the face to displaying a deadline item.
6136 FRACTION is what fraction of the head-warning time has passed."
6137 (let ((faces org-agenda-deadline-faces) f)
6138 (catch 'exit
6139 (while (setq f (pop faces))
6140 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
6142 (defun org-agenda-get-scheduled (&optional deadline-results with-hour)
6143 "Return the scheduled information for agenda display.
6144 When WITH-HOUR is non-nil, only return scheduled items with
6145 an hour specification like [h]h:mm."
6146 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
6147 'org-todo-regexp org-todo-regexp
6148 'org-complex-heading-regexp org-complex-heading-regexp
6149 'done-face 'org-agenda-done
6150 'mouse-face 'highlight
6151 'help-echo
6152 (format "mouse-2 or RET jump to org file %s"
6153 (abbreviate-file-name buffer-file-name))))
6154 (regexp (if with-hour
6155 org-scheduled-time-hour-regexp
6156 org-scheduled-time-regexp))
6157 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
6158 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
6160 (deadline-position-alist
6161 (mapcar (lambda (a) (and (setq mm (get-text-property
6162 0 'org-hd-marker a))
6163 (cons (marker-position mm) a)))
6164 deadline-results))
6165 d2 diff pos pos1 category level tags donep
6166 ee txt head pastschedp todo-state face timestr s habitp show-all
6167 did-habit-check-p warntime inherited-tags ts-date suppress-delay
6168 ddays)
6169 (goto-char (point-min))
6170 (while (re-search-forward regexp nil t)
6171 (catch :skip
6172 (org-agenda-skip)
6173 (setq s (match-string 1)
6174 txt nil
6175 pos (1- (match-beginning 1))
6176 todo-state (save-match-data (org-get-todo-state))
6177 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6178 (member todo-state
6179 org-agenda-repeating-timestamp-show-all))
6180 d2 (org-time-string-to-absolute
6181 s d1 'past show-all (current-buffer) pos)
6182 diff (- d2 d1)
6183 warntime (get-text-property (point) 'org-appt-warntime))
6184 (setq pastschedp (and todayp (< diff 0)))
6185 (setq did-habit-check-p nil)
6186 (setq suppress-delay
6187 (let ((ds (and org-agenda-skip-scheduled-delay-if-deadline
6188 (let ((item (buffer-substring (point-at-bol) (point-at-eol))))
6189 (save-match-data
6190 (and (string-match
6191 org-deadline-time-regexp item)
6192 (match-string 1 item)))))))
6193 (cond
6194 ((not ds) nil)
6195 ;; The current item has a deadline date (in ds), so
6196 ;; evaluate its delay time.
6197 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6198 ;; Use global delay time.
6199 (- org-agenda-skip-scheduled-delay-if-deadline))
6200 ((eq org-agenda-skip-scheduled-delay-if-deadline
6201 'post-deadline)
6202 ;; Set delay to no later than deadline.
6203 (min (- d2 (org-time-string-to-absolute
6204 ds d1 'past show-all (current-buffer) pos))
6205 org-scheduled-delay-days))
6206 (t 0))))
6207 (setq ddays (if suppress-delay
6208 (let ((org-scheduled-delay-days suppress-delay))
6209 (org-get-wdays s t t))
6210 (org-get-wdays s t)))
6211 ;; Use a delay of 0 when there is a repeater and the delay is
6212 ;; of the form --3d
6213 (when (and (save-match-data (string-match "--[0-9]+[hdwmy]" s))
6214 (< (org-time-string-to-absolute s)
6215 (org-time-string-to-absolute
6216 s d2 'past nil (current-buffer) pos)))
6217 (setq ddays 0))
6218 ;; When to show a scheduled item in the calendar:
6219 ;; If it is on or past the date.
6220 (when (or (and (> ddays 0) (= diff (- ddays)))
6221 (and (zerop ddays) (= diff 0))
6222 (and (< (+ diff ddays) 0)
6223 (< (abs diff) org-scheduled-past-days)
6224 (and todayp (not org-agenda-only-exact-dates)))
6225 ;; org-is-habit-p uses org-entry-get, which is expansive
6226 ;; so we go extra mile to only call it once
6227 (and todayp
6228 (boundp 'org-habit-show-all-today)
6229 org-habit-show-all-today
6230 (setq did-habit-check-p t)
6231 (setq habitp (and (functionp 'org-is-habit-p)
6232 (org-is-habit-p)))))
6233 (save-excursion
6234 (setq donep (member todo-state org-done-keywords))
6235 (if (and donep
6236 (or org-agenda-skip-scheduled-if-done
6237 (not (= diff 0))
6238 (and (functionp 'org-is-habit-p)
6239 (org-is-habit-p))))
6240 (setq txt nil)
6241 (setq habitp (if did-habit-check-p habitp
6242 (and (functionp 'org-is-habit-p)
6243 (org-is-habit-p))))
6244 (setq category (org-get-category))
6245 (if (and (eq org-agenda-skip-scheduled-if-deadline-is-shown
6246 'repeated-after-deadline)
6247 (org-get-deadline-time (point))
6248 (<= 0 (- d2 (time-to-days (org-get-deadline-time (point))))))
6249 (throw :skip nil))
6250 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6251 (throw :skip nil)
6252 (goto-char (match-end 0))
6253 (setq pos1 (match-beginning 0))
6254 (if habitp
6255 (if (or (not org-habit-show-habits)
6256 (and (not todayp)
6257 (boundp 'org-habit-show-habits-only-for-today)
6258 org-habit-show-habits-only-for-today))
6259 (throw :skip nil))
6260 (if (and
6261 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
6262 (and (eq org-agenda-skip-scheduled-if-deadline-is-shown 'not-today)
6263 pastschedp))
6264 (setq mm (assoc pos1 deadline-position-alist)))
6265 (throw :skip nil)))
6266 (setq inherited-tags
6267 (or (eq org-agenda-show-inherited-tags 'always)
6268 (and (listp org-agenda-show-inherited-tags)
6269 (memq 'agenda org-agenda-show-inherited-tags))
6270 (and (eq org-agenda-show-inherited-tags t)
6271 (or (eq org-agenda-use-tag-inheritance t)
6272 (memq 'agenda org-agenda-use-tag-inheritance))))
6274 tags (org-get-tags-at nil (not inherited-tags)))
6275 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6276 (setq head (buffer-substring
6277 (point)
6278 (progn (skip-chars-forward "^\r\n") (point))))
6279 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6280 (setq timestr
6281 (concat (substring s (match-beginning 1)) " "))
6282 (setq timestr 'time))
6283 (setq txt (org-agenda-format-item
6284 (if (= diff 0)
6285 (car org-agenda-scheduled-leaders)
6286 (format (nth 1 org-agenda-scheduled-leaders)
6287 (- 1 diff)))
6288 head level category tags
6289 (if (not (= diff 0)) nil timestr)
6290 nil habitp))))
6291 (when txt
6292 (setq face
6293 (cond
6294 ((and (not habitp) pastschedp)
6295 'org-scheduled-previously)
6296 (todayp 'org-scheduled-today)
6297 (t 'org-scheduled))
6298 habitp (and habitp (org-habit-parse-todo)))
6299 (org-add-props txt props
6300 'undone-face face
6301 'face (if donep 'org-agenda-done face)
6302 'org-marker (org-agenda-new-marker pos)
6303 'org-hd-marker (org-agenda-new-marker pos1)
6304 'type (if pastschedp "past-scheduled" "scheduled")
6305 'date (if pastschedp d2 date)
6306 'ts-date d2
6307 'warntime warntime
6308 'level level
6309 'priority (if habitp
6310 (org-habit-get-priority habitp)
6311 (+ 94 (- 5 diff) (org-get-priority txt)))
6312 'org-habit-p habitp
6313 'todo-state todo-state)
6314 (push txt ee))))))
6315 (nreverse ee)))
6317 (defun org-agenda-get-blocks ()
6318 "Return the date-range information for agenda display."
6319 (let* ((props (list 'face nil
6320 'org-not-done-regexp org-not-done-regexp
6321 'org-todo-regexp org-todo-regexp
6322 'org-complex-heading-regexp org-complex-heading-regexp
6323 'mouse-face 'highlight
6324 'help-echo
6325 (format "mouse-2 or RET jump to org file %s"
6326 (abbreviate-file-name buffer-file-name))))
6327 (regexp org-tr-regexp)
6328 (d0 (calendar-absolute-from-gregorian date))
6329 marker hdmarker ee txt d1 d2 s1 s2 category
6330 level todo-state tags pos head donep inherited-tags)
6331 (goto-char (point-min))
6332 (while (re-search-forward regexp nil t)
6333 (catch :skip
6334 (org-agenda-skip)
6335 (setq pos (point))
6336 (let ((start-time (match-string 1))
6337 (end-time (match-string 2)))
6338 (setq s1 (match-string 1)
6339 s2 (match-string 2)
6340 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
6341 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
6342 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6343 ;; Only allow days between the limits, because the normal
6344 ;; date stamps will catch the limits.
6345 (save-excursion
6346 (setq todo-state (org-get-todo-state))
6347 (setq donep (member todo-state org-done-keywords))
6348 (if (and donep org-agenda-skip-timestamp-if-done)
6349 (throw :skip t))
6350 (setq marker (org-agenda-new-marker (point))
6351 category (org-get-category))
6352 (if (not (re-search-backward org-outline-regexp-bol nil t))
6353 (throw :skip nil)
6354 (goto-char (match-beginning 0))
6355 (setq hdmarker (org-agenda-new-marker (point))
6356 inherited-tags
6357 (or (eq org-agenda-show-inherited-tags 'always)
6358 (and (listp org-agenda-show-inherited-tags)
6359 (memq 'agenda org-agenda-show-inherited-tags))
6360 (and (eq org-agenda-show-inherited-tags t)
6361 (or (eq org-agenda-use-tag-inheritance t)
6362 (memq 'agenda org-agenda-use-tag-inheritance))))
6364 tags (org-get-tags-at nil (not inherited-tags)))
6365 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6366 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
6367 (setq head (match-string 1))
6368 (let ((remove-re
6369 (if org-agenda-remove-timeranges-from-blocks
6370 (concat
6371 "<" (regexp-quote s1) ".*?>"
6372 "--"
6373 "<" (regexp-quote s2) ".*?>")
6374 nil)))
6375 (setq txt (org-agenda-format-item
6376 (format
6377 (nth (if (= d1 d2) 0 1)
6378 org-agenda-timerange-leaders)
6379 (1+ (- d0 d1)) (1+ (- d2 d1)))
6380 head level category tags
6381 (cond ((and (= d1 d0) (= d2 d0))
6382 (concat "<" start-time ">--<" end-time ">"))
6383 ((= d1 d0)
6384 (concat "<" start-time ">"))
6385 ((= d2 d0)
6386 (concat "<" end-time ">")))
6387 remove-re))))
6388 (org-add-props txt props
6389 'org-marker marker 'org-hd-marker hdmarker
6390 'type "block" 'date date
6391 'level level
6392 'todo-state todo-state
6393 'priority (org-get-priority txt))
6394 (push txt ee))))
6395 (goto-char pos)))
6396 ;; Sort the entries by expiration date.
6397 (nreverse ee)))
6399 ;;; Agenda presentation and sorting
6401 (defvar org-prefix-has-time nil
6402 "A flag, set by `org-compile-prefix-format'.
6403 The flag is set if the currently compiled format contains a `%t'.")
6404 (defvar org-prefix-has-tag nil
6405 "A flag, set by `org-compile-prefix-format'.
6406 The flag is set if the currently compiled format contains a `%T'.")
6407 (defvar org-prefix-has-effort nil
6408 "A flag, set by `org-compile-prefix-format'.
6409 The flag is set if the currently compiled format contains a `%e'.")
6410 (defvar org-prefix-has-breadcrumbs nil
6411 "A flag, set by `org-compile-prefix-format'.
6412 The flag is set if the currently compiled format contains a `%b'.")
6413 (defvar org-prefix-category-length nil
6414 "Used by `org-compile-prefix-format' to remember the category field width.")
6415 (defvar org-prefix-category-max-length nil
6416 "Used by `org-compile-prefix-format' to remember the category field width.")
6418 (defun org-agenda-get-category-icon (category)
6419 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6420 (dolist (entry org-agenda-category-icon-alist)
6421 (when (org-string-match-p (car entry) category)
6422 (if (listp (cadr entry))
6423 (return (cadr entry))
6424 (return (apply 'create-image (cdr entry)))))))
6426 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6427 remove-re habitp)
6428 "Format TXT to be inserted into the agenda buffer.
6429 In particular, add the prefix and corresponding text properties.
6431 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6432 LEVEL may be a string to replace the `%l' specifier.
6433 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6434 category taken from local variable or file name. It will replace the `%c'
6435 specifier in the format.
6436 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6437 TXT for sorting of this entry, and for the `%t' specifier in the format.
6438 When DOTIME is a string, this string is searched for a time before TXT is.
6439 TAGS can be the tags of the headline.
6440 Any match of REMOVE-RE will be removed from TXT."
6441 ;; We keep the org-prefix-* variable values along with a compiled
6442 ;; formatter, so that multiple agendas existing at the same time do
6443 ;; not step on each other toes.
6445 ;; It was inconvenient to make these variables buffer local in
6446 ;; Agenda buffers, because this function expects to be called with
6447 ;; the buffer where item comes from being current, and not agenda
6448 ;; buffer
6449 (let* ((bindings (car org-prefix-format-compiled))
6450 (formatter (cadr org-prefix-format-compiled)))
6451 (loop for (var value) in bindings
6452 do (set var value))
6453 (save-match-data
6454 ;; Diary entries sometimes have extra whitespace at the beginning
6455 (setq txt (org-trim txt))
6457 ;; Fix the tags part in txt
6458 (setq txt (org-agenda-fix-displayed-tags
6459 txt tags
6460 org-agenda-show-inherited-tags
6461 org-agenda-hide-tags-regexp))
6463 (let* ((category (or category
6464 (if buffer-file-name
6465 (file-name-sans-extension
6466 (file-name-nondirectory buffer-file-name))
6467 "")))
6468 (category-icon (org-agenda-get-category-icon category))
6469 (category-icon (if category-icon
6470 (propertize " " 'display category-icon)
6471 ""))
6472 (effort (and (not (string= txt ""))
6473 (get-text-property 1 'effort txt)))
6474 ;; time, tag, effort are needed for the eval of the prefix format
6475 (tag (if tags (nth (1- (length tags)) tags) ""))
6476 time
6477 (ts (if dotime (concat
6478 (if (stringp dotime) dotime "")
6479 (and org-agenda-search-headline-for-time txt))))
6480 (time-of-day (and dotime (org-get-time-of-day ts)))
6481 stamp plain s0 s1 s2 rtn srp l
6482 duration breadcrumbs)
6483 (and (derived-mode-p 'org-mode) buffer-file-name
6484 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6485 (when (and dotime time-of-day)
6486 ;; Extract starting and ending time and move them to prefix
6487 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6488 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6489 (setq s0 (match-string 0 ts)
6490 srp (and stamp (match-end 3))
6491 s1 (match-string (if plain 1 2) ts)
6492 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6494 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6495 ;; them, we might want to remove them there to avoid duplication.
6496 ;; The user can turn this off with a variable.
6497 (if (and org-prefix-has-time
6498 org-agenda-remove-times-when-in-prefix (or stamp plain)
6499 (string-match (concat (regexp-quote s0) " *") txt)
6500 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6501 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6502 (= (match-beginning 0) 0)
6504 (setq txt (replace-match "" nil nil txt))))
6505 ;; Normalize the time(s) to 24 hour
6506 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6507 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6509 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
6510 (let (org-time-clocksum-use-effort-durations)
6511 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6512 (setq s2
6513 (org-minutes-to-clocksum-string
6514 (+ (org-hh:mm-string-to-minutes s1)
6515 org-agenda-default-appointment-duration)))))
6517 ;; Compute the duration
6518 (when s2
6519 (setq duration (- (org-hh:mm-string-to-minutes s2)
6520 (org-hh:mm-string-to-minutes s1)))))
6522 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
6523 txt)
6524 ;; Tags are in the string
6525 (if (or (eq org-agenda-remove-tags t)
6526 (and org-agenda-remove-tags
6527 org-prefix-has-tag))
6528 (setq txt (replace-match "" t t txt))
6529 (setq txt (replace-match
6530 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6531 (match-string 2 txt))
6532 t t txt))))
6534 (when remove-re
6535 (while (string-match remove-re txt)
6536 (setq txt (replace-match "" t t txt))))
6538 ;; Set org-heading property on `txt' to mark the start of the
6539 ;; heading.
6540 (add-text-properties 0 (length txt) '(org-heading t) txt)
6542 ;; Prepare the variables needed in the eval of the compiled format
6543 (if org-prefix-has-breadcrumbs
6544 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6545 (let ((s (org-display-outline-path nil nil "->" t)))
6546 (if (eq "" s) "" (concat s "->"))))))
6547 (setq time (cond (s2 (concat
6548 (org-agenda-time-of-day-to-ampm-maybe s1)
6549 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6550 (if org-agenda-timegrid-use-ampm " ")))
6551 (s1 (concat
6552 (org-agenda-time-of-day-to-ampm-maybe s1)
6553 (if org-agenda-timegrid-use-ampm
6554 "........ "
6555 "......")))
6556 (t ""))
6557 extra (or (and (not habitp) extra) "")
6558 category (if (symbolp category) (symbol-name category) category)
6559 level (or level ""))
6560 (if (string-match org-bracket-link-regexp category)
6561 (progn
6562 (setq l (if (match-end 3)
6563 (- (match-end 3) (match-beginning 3))
6564 (- (match-end 1) (match-beginning 1))))
6565 (when (< l (or org-prefix-category-length 0))
6566 (setq category (copy-sequence category))
6567 (org-add-props category nil
6568 'extra-space (make-string
6569 (- org-prefix-category-length l 1) ?\ ))))
6570 (if (and org-prefix-category-max-length
6571 (>= (length category) org-prefix-category-max-length))
6572 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6573 ;; Evaluate the compiled format
6574 (setq rtn (concat (eval formatter) txt))
6576 ;; And finally add the text properties
6577 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6578 (org-add-props rtn nil
6579 'org-category category
6580 'tags (mapcar 'org-downcase-keep-props tags)
6581 'org-highest-priority org-highest-priority
6582 'org-lowest-priority org-lowest-priority
6583 'time-of-day time-of-day
6584 'duration duration
6585 'breadcrumbs breadcrumbs
6586 'txt txt
6587 'level level
6588 'time time
6589 'extra extra
6590 'format org-prefix-format-compiled
6591 'dotime dotime)))))
6593 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6594 "Remove tags string from TXT, and add a modified list of tags.
6595 The modified list may contain inherited tags, and tags matched by
6596 `org-agenda-hide-tags-regexp' will be removed."
6597 (when (or add-inherited hide-re)
6598 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
6599 (setq txt (substring txt 0 (match-beginning 0))))
6600 (setq tags
6601 (delq nil
6602 (mapcar (lambda (tg)
6603 (if (or (and hide-re (string-match hide-re tg))
6604 (and (not add-inherited)
6605 (get-text-property 0 'inherited tg)))
6607 tg))
6608 tags)))
6609 (when tags
6610 (let ((have-i (get-text-property 0 'inherited (car tags)))
6612 (setq txt (concat txt " :"
6613 (mapconcat
6614 (lambda (x)
6615 (setq i (get-text-property 0 'inherited x))
6616 (if (and have-i (not i))
6617 (progn
6618 (setq have-i nil)
6619 (concat ":" x))
6621 tags ":")
6622 (if have-i "::" ":"))))))
6623 txt)
6625 (defun org-downcase-keep-props (s)
6626 (let ((props (text-properties-at 0 s)))
6627 (setq s (downcase s))
6628 (add-text-properties 0 (length s) props s)
6631 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6633 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6634 "Add a time-grid for agenda items which need it.
6636 LIST is the list of agenda items formatted by `org-agenda-list'.
6637 NDAYS is the span of the current agenda view.
6638 TODAYP is `t' when the current agenda view is on today."
6639 (catch 'exit
6640 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6641 ((and todayp (member 'today (car org-agenda-time-grid))))
6642 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6643 ((member 'weekly (car org-agenda-time-grid)))
6644 (t (throw 'exit list)))
6645 (let* ((have (delq nil (mapcar
6646 (lambda (x) (get-text-property 1 'time-of-day x))
6647 list)))
6648 (string (nth 1 org-agenda-time-grid))
6649 (gridtimes (nth 2 org-agenda-time-grid))
6650 (req (car org-agenda-time-grid))
6651 (remove (member 'remove-match req))
6652 new time)
6653 (if (and (member 'require-timed req) (not have))
6654 ;; don't show empty grid
6655 (throw 'exit list))
6656 (while (setq time (pop gridtimes))
6657 (unless (and remove (member time have))
6658 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6659 (push (org-agenda-format-item
6660 nil string nil "" nil
6661 (concat (substring time 0 -2) ":" (substring time -2)))
6662 new)
6663 (put-text-property
6664 2 (length (car new)) 'face 'org-time-grid (car new))))
6665 (when (and todayp org-agenda-show-current-time-in-grid)
6666 (push (org-agenda-format-item
6667 nil org-agenda-current-time-string nil "" nil
6668 (format-time-string "%H:%M "))
6669 new)
6670 (put-text-property
6671 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6673 (if (member 'time-up org-agenda-sorting-strategy-selected)
6674 (append new list)
6675 (append list new)))))
6677 (defun org-compile-prefix-format (key)
6678 "Compile the prefix format into a Lisp form that can be evaluated.
6679 The resulting form and associated variable bindings is returned
6680 and stored in the variable `org-prefix-format-compiled'."
6681 (setq org-prefix-has-time nil
6682 org-prefix-has-tag nil
6683 org-prefix-category-length nil
6684 org-prefix-has-effort nil
6685 org-prefix-has-breadcrumbs nil)
6686 (let ((s (cond
6687 ((stringp org-agenda-prefix-format)
6688 org-agenda-prefix-format)
6689 ((assq key org-agenda-prefix-format)
6690 (cdr (assq key org-agenda-prefix-format)))
6691 (t " %-12:c%?-12t% s")))
6692 (start 0)
6693 varform vars var e c f opt)
6694 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6695 s start)
6696 (setq var (or (cdr (assoc (match-string 4 s)
6697 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6698 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6699 'eval)
6700 c (or (match-string 3 s) "")
6701 opt (match-beginning 1)
6702 start (1+ (match-beginning 0)))
6703 (if (equal var 'time) (setq org-prefix-has-time t))
6704 (if (equal var 'tag) (setq org-prefix-has-tag t))
6705 (if (equal var 'effort) (setq org-prefix-has-effort t))
6706 (if (equal var 'breadcrumbs) (setq org-prefix-has-breadcrumbs t))
6707 (setq f (concat "%" (match-string 2 s) "s"))
6708 (when (equal var 'category)
6709 (setq org-prefix-category-length
6710 (floor (abs (string-to-number (match-string 2 s)))))
6711 (setq org-prefix-category-max-length
6712 (let ((x (match-string 2 s)))
6713 (save-match-data
6714 (if (string-match "\\.[0-9]+" x)
6715 (string-to-number (substring (match-string 0 x) 1)))))))
6716 (if (eq var 'eval)
6717 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6718 (if opt
6719 (setq varform
6720 `(if (or (equal "" ,var) (equal nil ,var))
6722 (format ,f (concat ,var ,c))))
6723 (setq varform
6724 `(format ,f (if (or (equal ,var "")
6725 (equal ,var nil)) ""
6726 (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6727 (setq s (replace-match "%s" t nil s))
6728 (push varform vars))
6729 (setq vars (nreverse vars))
6730 (with-current-buffer (or org-agenda-buffer (current-buffer))
6731 (setq org-prefix-format-compiled
6732 (list
6733 `((org-prefix-has-time ,org-prefix-has-time)
6734 (org-prefix-has-tag ,org-prefix-has-tag)
6735 (org-prefix-category-length ,org-prefix-category-length)
6736 (org-prefix-has-effort ,org-prefix-has-effort)
6737 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6738 `(format ,s ,@vars))))))
6740 (defun org-set-sorting-strategy (key)
6741 (if (symbolp (car org-agenda-sorting-strategy))
6742 ;; the old format
6743 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6744 (setq org-agenda-sorting-strategy-selected
6745 (or (cdr (assq key org-agenda-sorting-strategy))
6746 (cdr (assq 'agenda org-agenda-sorting-strategy))
6747 '(time-up category-keep priority-down)))))
6749 (defun org-get-time-of-day (s &optional string mod24)
6750 "Check string S for a time of day.
6751 If found, return it as a military time number between 0 and 2400.
6752 If not found, return nil.
6753 The optional STRING argument forces conversion into a 5 character wide string
6754 HH:MM."
6755 (save-match-data
6756 (when
6757 (and
6758 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6759 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6760 (not (eq (get-text-property 1 'face s) 'org-link)))
6761 (let* ((h (string-to-number (match-string 1 s)))
6762 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6763 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6764 (am-p (equal ampm "am"))
6765 (h1 (cond ((not ampm) h)
6766 ((= h 12) (if am-p 0 12))
6767 (t (+ h (if am-p 0 12)))))
6768 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6769 (mod h1 24) h1))
6770 (t0 (+ (* 100 h2) m))
6771 (t1 (concat (if (>= h1 24) "+" " ")
6772 (if (and org-agenda-time-leading-zero
6773 (< t0 1000)) "0" "")
6774 (if (< t0 100) "0" "")
6775 (if (< t0 10) "0" "")
6776 (int-to-string t0))))
6777 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6779 (defvar org-agenda-before-sorting-filter-function nil
6780 "Function to be applied to agenda items prior to sorting.
6781 Prior to sorting also means just before they are inserted into the agenda.
6783 To aid sorting, you may revisit the original entries and add more text
6784 properties which will later be used by the sorting functions.
6786 The function should take a string argument, an agenda line.
6787 It has access to the text properties in that line, which contain among
6788 other things, the property `org-hd-marker' that points to the entry
6789 where the line comes from. Note that not all lines going into the agenda
6790 have this property, only most.
6792 The function should return the modified string. It is probably best
6793 to ONLY change text properties.
6795 You can also use this function as a filter, by returning nil for lines
6796 you don't want to have in the agenda at all. For this application, you
6797 could bind the variable in the options section of a custom command.")
6799 (defun org-agenda-finalize-entries (list &optional type)
6800 "Sort, limit and concatenate the LIST of agenda items.
6801 The optional argument TYPE tells the agenda type."
6802 (let ((max-effort (cond ((listp org-agenda-max-effort)
6803 (cdr (assoc type org-agenda-max-effort)))
6804 (t org-agenda-max-effort)))
6805 (max-todo (cond ((listp org-agenda-max-todos)
6806 (cdr (assoc type org-agenda-max-todos)))
6807 (t org-agenda-max-todos)))
6808 (max-tags (cond ((listp org-agenda-max-tags)
6809 (cdr (assoc type org-agenda-max-tags)))
6810 (t org-agenda-max-tags)))
6811 (max-entries (cond ((listp org-agenda-max-entries)
6812 (cdr (assoc type org-agenda-max-entries)))
6813 (t org-agenda-max-entries))))
6814 (when org-agenda-before-sorting-filter-function
6815 (setq list
6816 (delq nil
6817 (mapcar
6818 org-agenda-before-sorting-filter-function list))))
6819 (setq list (mapcar 'org-agenda-highlight-todo list)
6820 list (mapcar 'identity (sort list 'org-entries-lessp)))
6821 (when max-effort
6822 (setq list (org-agenda-limit-entries
6823 list 'effort-minutes max-effort
6824 (lambda (e) (or e (if org-sort-agenda-noeffort-is-high
6825 32767 -1))))))
6826 (when max-todo
6827 (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
6828 (when max-tags
6829 (setq list (org-agenda-limit-entries list 'tags max-tags)))
6830 (when max-entries
6831 (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
6832 (mapconcat 'identity list "\n")))
6834 (defun org-agenda-limit-entries (list prop limit &optional fn)
6835 "Limit the number of agenda entries."
6836 (let ((include (and limit (< limit 0))))
6837 (if limit
6838 (let ((fun (or fn (lambda (p) (if p 1))))
6839 (lim 0))
6840 (delq nil
6841 (mapcar
6842 (lambda (e)
6843 (let ((pval (funcall
6844 fun (get-text-property (1- (length e))
6845 prop e))))
6846 (if pval (setq lim (+ lim pval)))
6847 (cond ((and pval (<= lim (abs limit))) e)
6848 ((and include (not pval)) e))))
6849 list)))
6850 list)))
6852 (defun org-agenda-limit-interactively (remove)
6853 "In agenda, interactively limit entries to various maximums."
6854 (interactive "P")
6855 (if remove
6856 (progn (setq org-agenda-max-entries nil
6857 org-agenda-max-todos nil
6858 org-agenda-max-tags nil
6859 org-agenda-max-effort nil)
6860 (org-agenda-redo))
6861 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6862 (msg (cond ((= max ?E) "How many minutes? ")
6863 ((= max ?e) "How many entries? ")
6864 ((= max ?t) "How many TODO entries? ")
6865 ((= max ?T) "How many tagged entries? ")
6866 (t (user-error "Wrong input"))))
6867 (num (string-to-number (read-from-minibuffer msg))))
6868 (cond ((equal max ?e)
6869 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6870 ((equal max ?t)
6871 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6872 ((equal max ?T)
6873 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6874 ((equal max ?E)
6875 (let ((org-agenda-max-effort num)) (org-agenda-redo))))))
6876 (org-agenda-fit-window-to-buffer))
6878 (defun org-agenda-highlight-todo (x)
6879 (let ((org-done-keywords org-done-keywords-for-agenda)
6880 (case-fold-search nil)
6882 (if (eq x 'line)
6883 (save-excursion
6884 (beginning-of-line 1)
6885 (setq re (org-get-at-bol 'org-todo-regexp))
6886 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6887 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6888 (add-text-properties (match-beginning 0) (match-end 1)
6889 (list 'face (org-get-todo-face 1)))
6890 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6891 (delete-region (match-beginning 1) (1- (match-end 0)))
6892 (goto-char (match-beginning 1))
6893 (insert (format org-agenda-todo-keyword-format s)))))
6894 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6895 (setq re (get-text-property 0 'org-todo-regexp x))
6896 (when (and re
6897 ;; Test `pl' because if there's no heading content,
6898 ;; there's no point matching to highlight. Note
6899 ;; that if we didn't test `pl' first, and there
6900 ;; happened to be no keyword from `org-todo-regexp'
6901 ;; on this heading line, then the `equal' comparison
6902 ;; afterwards would spuriously succeed in the case
6903 ;; where `pl' is nil -- causing an args-out-of-range
6904 ;; error when we try to add text properties to text
6905 ;; that isn't there.
6907 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6908 x pl) pl))
6909 (add-text-properties
6910 (or (match-end 1) (match-end 0)) (match-end 0)
6911 (list 'face (org-get-todo-face (match-string 2 x)))
6913 (when (match-end 1)
6914 (setq x (concat (substring x 0 (match-end 1))
6915 (format org-agenda-todo-keyword-format
6916 (match-string 2 x))
6917 (org-add-props " " (text-properties-at 0 x))
6918 (substring x (match-end 3)))))))
6919 x)))
6921 (defsubst org-cmp-values (a b property)
6922 "Compare the numeric value of text PROPERTY for string A and B."
6923 (let ((pa (or (get-text-property (1- (length a)) property a) 0))
6924 (pb (or (get-text-property (1- (length b)) property b) 0)))
6925 (cond ((> pa pb) +1)
6926 ((< pa pb) -1))))
6928 (defsubst org-cmp-effort (a b)
6929 "Compare the effort values of string A and B."
6930 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6931 (ea (or (get-text-property (1- (length a)) 'effort-minutes a) def))
6932 (eb (or (get-text-property (1- (length b)) 'effort-minutes b) def)))
6933 (cond ((> ea eb) +1)
6934 ((< ea eb) -1))))
6936 (defsubst org-cmp-category (a b)
6937 "Compare the string values of categories of strings A and B."
6938 (let ((ca (or (get-text-property (1- (length a)) 'org-category a) ""))
6939 (cb (or (get-text-property (1- (length b)) 'org-category b) "")))
6940 (cond ((string-lessp ca cb) -1)
6941 ((string-lessp cb ca) +1))))
6943 (defsubst org-cmp-todo-state (a b)
6944 "Compare the todo states of strings A and B."
6945 (let* ((ma (or (get-text-property 1 'org-marker a)
6946 (get-text-property 1 'org-hd-marker a)))
6947 (mb (or (get-text-property 1 'org-marker b)
6948 (get-text-property 1 'org-hd-marker b)))
6949 (fa (and ma (marker-buffer ma)))
6950 (fb (and mb (marker-buffer mb)))
6951 (todo-kwds
6952 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6953 (and fb (with-current-buffer fb org-todo-keywords-1))))
6954 (ta (or (get-text-property 1 'todo-state a) ""))
6955 (tb (or (get-text-property 1 'todo-state b) ""))
6956 (la (- (length (member ta todo-kwds))))
6957 (lb (- (length (member tb todo-kwds))))
6958 (donepa (member ta org-done-keywords-for-agenda))
6959 (donepb (member tb org-done-keywords-for-agenda)))
6960 (cond ((and donepa (not donepb)) -1)
6961 ((and (not donepa) donepb) +1)
6962 ((< la lb) -1)
6963 ((< lb la) +1))))
6965 (defsubst org-cmp-alpha (a b)
6966 "Compare the headlines, alphabetically."
6967 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6968 (plb (text-property-any 0 (length b) 'org-heading t b))
6969 (ta (and pla (substring a pla)))
6970 (tb (and plb (substring b plb))))
6971 (when pla
6972 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6973 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6974 (setq ta (substring ta (match-end 0))))
6975 (setq ta (downcase ta)))
6976 (when plb
6977 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6978 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6979 (setq tb (substring tb (match-end 0))))
6980 (setq tb (downcase tb)))
6981 (cond ((not ta) +1)
6982 ((not tb) -1)
6983 ((string-lessp ta tb) -1)
6984 ((string-lessp tb ta) +1))))
6986 (defsubst org-cmp-tag (a b)
6987 "Compare the string values of the first tags of A and B."
6988 (let ((ta (car (last (get-text-property 1 'tags a))))
6989 (tb (car (last (get-text-property 1 'tags b)))))
6990 (cond ((not ta) +1)
6991 ((not tb) -1)
6992 ((string-lessp ta tb) -1)
6993 ((string-lessp tb ta) +1))))
6995 (defsubst org-cmp-time (a b)
6996 "Compare the time-of-day values of strings A and B."
6997 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6998 (ta (or (get-text-property 1 'time-of-day a) def))
6999 (tb (or (get-text-property 1 'time-of-day b) def)))
7000 (cond ((< ta tb) -1)
7001 ((< tb ta) +1))))
7003 (defsubst org-cmp-ts (a b type)
7004 "Compare the timestamps values of entries A and B.
7005 When TYPE is \"scheduled\", \"deadline\", \"timestamp\" or
7006 \"timestamp_ia\", compare within each of these type. When TYPE
7007 is the empty string, compare all timestamps without respect of
7008 their type."
7009 (let* ((def (if org-sort-agenda-notime-is-late most-positive-fixnum -1))
7010 (ta (or (and (string-match type (or (get-text-property 1 type a) ""))
7011 (get-text-property 1 'ts-date a)) def))
7012 (tb (or (and (string-match type (or (get-text-property 1 type b) ""))
7013 (get-text-property 1 'ts-date b)) def)))
7014 (cond ((< ta tb) -1)
7015 ((< tb ta) +1))))
7017 (defsubst org-cmp-habit-p (a b)
7018 "Compare the todo states of strings A and B."
7019 (let ((ha (get-text-property 1 'org-habit-p a))
7020 (hb (get-text-property 1 'org-habit-p b)))
7021 (cond ((and ha (not hb)) -1)
7022 ((and (not ha) hb) +1))))
7024 (defun org-entries-lessp (a b)
7025 "Predicate for sorting agenda entries."
7026 ;; The following variables will be used when the form is evaluated.
7027 ;; So even though the compiler complains, keep them.
7028 (let* ((ss org-agenda-sorting-strategy-selected)
7029 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
7030 (org-cmp-ts a b "")))
7031 (timestamp-down (if timestamp-up (- timestamp-up) nil))
7032 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
7033 (org-cmp-ts a b "scheduled")))
7034 (scheduled-down (if scheduled-up (- scheduled-up) nil))
7035 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
7036 (org-cmp-ts a b "deadline")))
7037 (deadline-down (if deadline-up (- deadline-up) nil))
7038 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
7039 (org-cmp-ts a b "iatimestamp_ia")))
7040 (tsia-down (if tsia-up (- tsia-up) nil))
7041 (ts-up (and (org-em 'ts-up 'ts-down ss)
7042 (org-cmp-ts a b "timestamp")))
7043 (ts-down (if ts-up (- ts-up) nil))
7044 (time-up (and (org-em 'time-up 'time-down ss)
7045 (org-cmp-time a b)))
7046 (time-down (if time-up (- time-up) nil))
7047 (stats-up (and (org-em 'stats-up 'stats-down ss)
7048 (org-cmp-values a b 'org-stats)))
7049 (stats-down (if stats-up (- stats-up) nil))
7050 (priority-up (and (org-em 'priority-up 'priority-down ss)
7051 (org-cmp-values a b 'priority)))
7052 (priority-down (if priority-up (- priority-up) nil))
7053 (effort-up (and (org-em 'effort-up 'effort-down ss)
7054 (org-cmp-effort a b)))
7055 (effort-down (if effort-up (- effort-up) nil))
7056 (category-up (and (or (org-em 'category-up 'category-down ss)
7057 (memq 'category-keep ss))
7058 (org-cmp-category a b)))
7059 (category-down (if category-up (- category-up) nil))
7060 (category-keep (if category-up +1 nil))
7061 (tag-up (and (org-em 'tag-up 'tag-down ss)
7062 (org-cmp-tag a b)))
7063 (tag-down (if tag-up (- tag-up) nil))
7064 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
7065 (org-cmp-todo-state a b)))
7066 (todo-state-down (if todo-state-up (- todo-state-up) nil))
7067 (habit-up (and (org-em 'habit-up 'habit-down ss)
7068 (org-cmp-habit-p a b)))
7069 (habit-down (if habit-up (- habit-up) nil))
7070 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
7071 (org-cmp-alpha a b)))
7072 (alpha-down (if alpha-up (- alpha-up) nil))
7073 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
7074 user-defined-up user-defined-down)
7075 (if (and need-user-cmp org-agenda-cmp-user-defined
7076 (functionp org-agenda-cmp-user-defined))
7077 (setq user-defined-up
7078 (funcall org-agenda-cmp-user-defined a b)
7079 user-defined-down (if user-defined-up (- user-defined-up) nil)))
7080 (cdr (assoc
7081 (eval (cons 'or org-agenda-sorting-strategy-selected))
7082 '((-1 . t) (1 . nil) (nil . nil))))))
7084 ;;; Agenda restriction lock
7086 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
7087 "Overlay to mark the headline to which agenda commands are restricted.")
7088 (overlay-put org-agenda-restriction-lock-overlay
7089 'face 'org-agenda-restriction-lock)
7090 (overlay-put org-agenda-restriction-lock-overlay
7091 'help-echo "Agendas are currently limited to this subtree.")
7092 (org-detach-overlay org-agenda-restriction-lock-overlay)
7094 ;;;###autoload
7095 (defun org-agenda-set-restriction-lock (&optional type)
7096 "Set restriction lock for agenda, to current subtree or file.
7097 Restriction will be the file if TYPE is `file', or if type is the
7098 universal prefix '(4), or if the cursor is before the first headline
7099 in the file. Otherwise, restriction will be to the current subtree."
7100 (interactive "P")
7101 (org-agenda-remove-restriction-lock 'noupdate)
7102 (and (equal type '(4)) (setq type 'file))
7103 (setq type (cond
7104 (type type)
7105 ((org-at-heading-p) 'subtree)
7106 ((condition-case nil (org-back-to-heading t) (error nil))
7107 'subtree)
7108 (t 'file)))
7109 (if (eq type 'subtree)
7110 (progn
7111 (setq org-agenda-restrict (current-buffer))
7112 (setq org-agenda-overriding-restriction 'subtree)
7113 (put 'org-agenda-files 'org-restrict
7114 (list (buffer-file-name (buffer-base-buffer))))
7115 (org-back-to-heading t)
7116 (move-overlay org-agenda-restriction-lock-overlay
7117 (point)
7118 (if org-agenda-restriction-lock-highlight-subtree
7119 (save-excursion (org-end-of-subtree t t) (point))
7120 (point-at-eol)))
7121 (move-marker org-agenda-restrict-begin (point))
7122 (move-marker org-agenda-restrict-end
7123 (save-excursion (org-end-of-subtree t t)))
7124 (message "Locking agenda restriction to subtree"))
7125 (put 'org-agenda-files 'org-restrict
7126 (list (buffer-file-name (buffer-base-buffer))))
7127 (setq org-agenda-restrict nil)
7128 (setq org-agenda-overriding-restriction 'file)
7129 (move-marker org-agenda-restrict-begin nil)
7130 (move-marker org-agenda-restrict-end nil)
7131 (message "Locking agenda restriction to file"))
7132 (setq current-prefix-arg nil)
7133 (org-agenda-maybe-redo))
7135 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7136 "Remove the agenda restriction lock."
7137 (interactive "P")
7138 (org-detach-overlay org-agenda-restriction-lock-overlay)
7139 (org-detach-overlay org-speedbar-restriction-lock-overlay)
7140 (setq org-agenda-overriding-restriction nil)
7141 (setq org-agenda-restrict nil)
7142 (put 'org-agenda-files 'org-restrict nil)
7143 (move-marker org-agenda-restrict-begin nil)
7144 (move-marker org-agenda-restrict-end nil)
7145 (setq current-prefix-arg nil)
7146 (message "Agenda restriction lock removed")
7147 (or noupdate (org-agenda-maybe-redo)))
7149 (defun org-agenda-maybe-redo ()
7150 "If there is any window showing the agenda view, update it."
7151 (let ((w (get-buffer-window org-agenda-buffer-name t))
7152 (w0 (selected-window)))
7153 (when w
7154 (select-window w)
7155 (org-agenda-redo)
7156 (select-window w0)
7157 (if org-agenda-overriding-restriction
7158 (message "Agenda view shifted to new %s restriction"
7159 org-agenda-overriding-restriction)
7160 (message "Agenda restriction lock removed")))))
7162 ;;; Agenda commands
7164 (defun org-agenda-check-type (error &rest types)
7165 "Check if agenda buffer is of allowed type.
7166 If ERROR is non-nil, throw an error, otherwise just return nil.
7167 Allowed types are 'agenda 'timeline 'todo 'tags 'search."
7168 (if (not org-agenda-type)
7169 (error "No Org agenda currently displayed")
7170 (if (memq org-agenda-type types)
7172 (if error
7173 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
7174 nil))))
7176 (defun org-agenda-Quit ()
7177 "Exit the agenda and kill buffers loaded by `org-agenda'.
7178 Also restore the window configuration."
7179 (interactive)
7180 (if org-agenda-columns-active
7181 (org-columns-quit)
7182 (let ((buf (current-buffer)))
7183 (if (eq org-agenda-window-setup 'other-frame)
7184 (progn
7185 (org-agenda-reset-markers)
7186 (kill-buffer buf)
7187 (org-columns-remove-overlays)
7188 (setq org-agenda-archives-mode nil)
7189 (delete-frame))
7190 (and (not (eq org-agenda-window-setup 'current-window))
7191 (not (one-window-p))
7192 (delete-window))
7193 (org-agenda-reset-markers)
7194 (kill-buffer buf)
7195 (org-columns-remove-overlays)
7196 (setq org-agenda-archives-mode nil)))
7197 (setq org-agenda-buffer nil)
7198 ;; Maybe restore the pre-agenda window configuration.
7199 (and org-agenda-restore-windows-after-quit
7200 (not (eq org-agenda-window-setup 'other-frame))
7201 org-agenda-pre-window-conf
7202 (set-window-configuration org-agenda-pre-window-conf)
7203 (setq org-agenda-pre-window-conf nil))))
7205 (defun org-agenda-quit ()
7206 "Exit the agenda and restore the window configuration.
7207 When `org-agenda-sticky' is non-nil, only bury the agenda."
7208 (interactive)
7209 (if (and (eq org-indirect-buffer-display 'other-window)
7210 org-last-indirect-buffer)
7211 (let ((org-last-indirect-window
7212 (get-buffer-window org-last-indirect-buffer)))
7213 (if org-last-indirect-window
7214 (delete-window org-last-indirect-window))))
7215 (if org-agenda-columns-active
7216 (org-columns-quit)
7217 (if org-agenda-sticky
7218 (let ((buf (current-buffer)))
7219 (if (eq org-agenda-window-setup 'other-frame)
7220 (progn
7221 (delete-frame))
7222 (and (not (eq org-agenda-window-setup 'current-window))
7223 (not (one-window-p))
7224 (delete-window)))
7225 (with-current-buffer buf
7226 (bury-buffer)
7227 ;; Maybe restore the pre-agenda window configuration.
7228 (and org-agenda-restore-windows-after-quit
7229 (not (eq org-agenda-window-setup 'other-frame))
7230 org-agenda-pre-window-conf
7231 (set-window-configuration org-agenda-pre-window-conf)
7232 (setq org-agenda-pre-window-conf nil))))
7233 (org-agenda-Quit))))
7235 (defun org-agenda-exit ()
7236 "Exit the agenda and restore the window configuration.
7237 Also kill Org-mode buffers loaded by `org-agenda'. Org-mode
7238 buffers visited directly by the user will not be touched."
7239 (interactive)
7240 (org-release-buffers org-agenda-new-buffers)
7241 (setq org-agenda-new-buffers nil)
7242 (org-agenda-Quit))
7244 (defun org-agenda-kill-all-agenda-buffers ()
7245 "Kill all buffers in `org-agenda-mode'.
7246 This is used when toggling sticky agendas.
7247 You can also explicitly invoke it with `C-c a C-k'."
7248 (interactive)
7249 (let (blist)
7250 (dolist (buf (buffer-list))
7251 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7252 (push buf blist)))
7253 (mapc 'kill-buffer blist)))
7255 (defun org-agenda-execute (arg)
7256 "Execute another agenda command, keeping same window.
7257 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7258 in the agenda."
7259 (interactive "P")
7260 (let ((org-agenda-window-setup 'current-window))
7261 (org-agenda arg)))
7263 (defun org-agenda-redo (&optional all)
7264 "Rebuild possibly ALL agenda view(s) in the current buffer."
7265 (interactive "P")
7266 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7267 (cpa (unless (eq all t) current-prefix-arg))
7268 (org-agenda-doing-sticky-redo org-agenda-sticky)
7269 (org-agenda-sticky nil)
7270 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7271 org-agenda-buffer-name))
7272 (org-agenda-keep-modes t)
7273 (tag-filter org-agenda-tag-filter)
7274 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7275 (top-hl-filter org-agenda-top-headline-filter)
7276 (cat-filter org-agenda-category-filter)
7277 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7278 (re-filter org-agenda-regexp-filter)
7279 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7280 (effort-filter org-agenda-effort-filter)
7281 (effort-preset (get 'org-agenda-effort-filter :preset-filter))
7282 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7283 (cols org-agenda-columns-active)
7284 (line (org-current-line))
7285 (window-line (- line (org-current-line (window-start))))
7286 (lprops (get 'org-agenda-redo-command 'org-lprops))
7287 (redo-cmd (get-text-property p 'org-redo-cmd))
7288 (last-args (get-text-property p 'org-last-args))
7289 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7290 (org-agenda-overriding-cmd-arguments
7291 (unless (eq all t)
7292 (cond ((listp last-args)
7293 (cons (or cpa (car last-args)) (cdr last-args)))
7294 ((stringp last-args)
7295 last-args))))
7296 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7297 (put 'org-agenda-tag-filter :preset-filter nil)
7298 (put 'org-agenda-category-filter :preset-filter nil)
7299 (put 'org-agenda-regexp-filter :preset-filter nil)
7300 (put 'org-agenda-effort-filter :preset-filter nil)
7301 (and cols (org-columns-quit))
7302 (message "Rebuilding agenda buffer...")
7303 (if series-redo-cmd
7304 (eval series-redo-cmd)
7305 (org-let lprops redo-cmd))
7306 (setq org-agenda-undo-list nil
7307 org-agenda-pending-undo-list nil
7308 org-agenda-tag-filter tag-filter
7309 org-agenda-category-filter cat-filter
7310 org-agenda-regexp-filter re-filter
7311 org-agenda-effort-filter effort-filter
7312 org-agenda-top-headline-filter top-hl-filter)
7313 (message "Rebuilding agenda buffer...done")
7314 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7315 (put 'org-agenda-category-filter :preset-filter cat-preset)
7316 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7317 (put 'org-agenda-effort-filter :preset-filter effort-preset)
7318 (let ((tag (or tag-filter tag-preset))
7319 (cat (or cat-filter cat-preset))
7320 (effort (or effort-filter effort-preset))
7321 (re (or re-filter re-preset)))
7322 (when tag (org-agenda-filter-apply tag 'tag))
7323 (when cat (org-agenda-filter-apply cat 'category))
7324 (when effort (org-agenda-filter-apply effort 'effort))
7325 (when re (org-agenda-filter-apply re 'regexp)))
7326 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7327 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
7328 (org-goto-line line)
7329 (recenter window-line)))
7331 (defvar org-global-tags-completion-table nil)
7332 (defvar org-agenda-filter-form nil)
7333 (defvar org-agenda-filtered-by-category nil)
7335 (defun org-agenda-filter-by-category (strip)
7336 "Keep only those lines in the agenda buffer that have a specific category.
7337 The category is that of the current line."
7338 (interactive "P")
7339 (if (and org-agenda-filtered-by-category
7340 org-agenda-category-filter)
7341 (org-agenda-filter-show-all-cat)
7342 (let ((cat (org-no-properties (org-get-at-eol 'org-category 1))))
7343 (cond
7344 ((and cat strip)
7345 (org-agenda-filter-apply
7346 (push (concat "-" cat) org-agenda-category-filter) 'category))
7347 ((and cat)
7348 (org-agenda-filter-apply
7349 (setq org-agenda-category-filter
7350 (list (concat "+" cat))) 'category))
7351 (t (error "No category at point"))))))
7353 (defun org-find-top-headline (&optional pos)
7354 "Find the topmost parent headline and return it."
7355 (save-excursion
7356 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
7357 (if pos (goto-char pos))
7358 ;; Skip up to the topmost parent
7359 (while (ignore-errors (outline-up-heading 1) t))
7360 (ignore-errors
7361 (nth 4 (org-heading-components))))))
7363 (defvar org-agenda-filtered-by-top-headline nil)
7364 (defun org-agenda-filter-by-top-headline (strip)
7365 "Keep only those lines that are descendants from the same top headline.
7366 The top headline is that of the current line."
7367 (interactive "P")
7368 (if org-agenda-filtered-by-top-headline
7369 (progn
7370 (setq org-agenda-filtered-by-top-headline nil
7371 org-agenda-top-headline-filter nil)
7372 (org-agenda-filter-show-all-top-filter))
7373 (let ((toph (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7374 (if toph (org-agenda-filter-top-headline-apply toph strip)
7375 (error "No top-level headline at point")))))
7377 (defvar org-agenda-regexp-filter nil)
7378 (defun org-agenda-filter-by-regexp (strip)
7379 "Filter agenda entries by a regular expression.
7380 Regexp filters are cumulative.
7381 With no prefix argument, keep entries matching the regexp.
7382 With one prefix argument, filter out entries matching the regexp.
7383 With two prefix arguments, remove the regexp filters."
7384 (interactive "P")
7385 (if (not (equal strip '(16)))
7386 (let ((flt (concat (if (equal strip '(4)) "-" "+")
7387 (read-from-minibuffer
7388 (if (equal strip '(4))
7389 "Filter out entries matching regexp: "
7390 "Narrow to entries matching regexp: ")))))
7391 (push flt org-agenda-regexp-filter)
7392 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
7393 (org-agenda-filter-show-all-re)
7394 (message "Regexp filter removed")))
7396 (defvar org-agenda-effort-filter nil)
7397 (defun org-agenda-filter-by-effort (strip)
7398 "Filter agenda entries by effort.
7399 With no prefix argument, keep entries matching the effort condition.
7400 With one prefix argument, filter out entries matching the condition.
7401 With two prefix arguments, remove the effort filters."
7402 (interactive "P")
7403 (cond ((member strip '(nil 4))
7404 (let ((efforts (org-split-string
7405 (or (cdr (assoc (concat org-effort-property "_ALL")
7406 org-global-properties))
7407 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
7408 "")))
7409 (eff -1)
7410 effort-prompt op)
7411 (while (not (member op '(?< ?> ?=)))
7412 (setq op (read-char-exclusive "Effort operator? (> = or <)")))
7413 (loop for i from 0 to 9 do
7414 (setq effort-prompt
7415 (concat
7416 effort-prompt " ["
7417 (if (= i 9) "0" (int-to-string (1+ i)))
7418 "]" (nth i efforts))))
7419 (message "Effort %s%s" (char-to-string op) effort-prompt)
7420 (while (or (< eff 0) (> eff 9))
7421 (setq eff (string-to-number (char-to-string (read-char-exclusive)))))
7422 (setq org-agenda-effort-filter
7423 (list (concat (if strip "-" "+")
7424 (char-to-string op) (nth (1- eff) efforts))))
7425 (org-agenda-filter-apply org-agenda-effort-filter 'effort)))
7426 (t (org-agenda-filter-show-all-effort)
7427 (message "Effort filter removed"))))
7429 (defun org-agenda-filter-remove-all ()
7430 "Remove all filters from the current agenda buffer."
7431 (interactive)
7432 (when org-agenda-tag-filter
7433 (org-agenda-filter-show-all-tag))
7434 (when org-agenda-category-filter
7435 (org-agenda-filter-show-all-cat))
7436 (when org-agenda-regexp-filter
7437 (org-agenda-filter-show-all-re))
7438 (when org-agenda-top-headline-filter
7439 (org-agenda-filter-show-all-top-filter))
7440 (when org-agenda-effort-filter
7441 (org-agenda-filter-show-all-effort))
7442 (org-agenda-finalize))
7444 (defun org-agenda-filter-by-tag (strip &optional char narrow)
7445 "Keep only those lines in the agenda buffer that have a specific tag.
7446 The tag is selected with its fast selection letter, as configured.
7447 With prefix argument STRIP, remove all lines that do have the tag.
7448 A lisp caller can specify CHAR. NARROW means that the new tag should be
7449 used to narrow the search - the interactive user can also press `-' or `+'
7450 to switch to narrowing."
7451 (interactive "P")
7452 (let* ((alist org-tag-alist-for-agenda)
7453 (tag-chars (mapconcat
7454 (lambda (x) (if (and (not (symbolp (car x)))
7455 (cdr x))
7456 (char-to-string (cdr x))
7457 ""))
7458 alist ""))
7459 (inhibit-read-only t)
7460 (current org-agenda-tag-filter)
7461 maybe-refresh a n tag)
7462 (unless char
7463 (message
7464 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow"
7465 (if narrow "Narrow" "Filter") tag-chars
7466 (if org-agenda-auto-exclude-function "[RET], " ""))
7467 (setq char (read-char-exclusive)))
7468 (when (member char '(?+ ?-))
7469 ;; Narrowing down
7470 (cond ((equal char ?-) (setq strip t narrow t))
7471 ((equal char ?+) (setq strip nil narrow t)))
7472 (message
7473 "Narrow by tag [%s ], [TAB], [/]:off" tag-chars)
7474 (setq char (read-char-exclusive)))
7475 (when (equal char ?\t)
7476 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7477 (org-set-local 'org-global-tags-completion-table
7478 (org-global-tags-completion-table)))
7479 (let ((completion-ignore-case t))
7480 (setq tag (org-icompleting-read
7481 "Tag: " org-global-tags-completion-table))))
7482 (cond
7483 ((equal char ?\r)
7484 (org-agenda-filter-show-all-tag)
7485 (when org-agenda-auto-exclude-function
7486 (setq org-agenda-tag-filter nil)
7487 (dolist (tag (org-agenda-get-represented-tags))
7488 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7489 (if modifier
7490 (push modifier org-agenda-tag-filter))))
7491 (if (not (null org-agenda-tag-filter))
7492 (org-agenda-filter-apply org-agenda-tag-filter 'tag)))
7493 (setq maybe-refresh t))
7494 ((equal char ?/)
7495 (org-agenda-filter-show-all-tag)
7496 (when (get 'org-agenda-tag-filter :preset-filter)
7497 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
7498 (setq maybe-refresh t))
7499 ((equal char ?. )
7500 (setq org-agenda-tag-filter
7501 (mapcar (lambda(tag) (concat "+" tag))
7502 (org-get-at-bol 'tags)))
7503 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
7504 (setq maybe-refresh t))
7505 ((or (equal char ?\ )
7506 (setq a (rassoc char alist))
7507 (and tag (setq a (cons tag nil))))
7508 (org-agenda-filter-show-all-tag)
7509 (setq tag (car a))
7510 (setq org-agenda-tag-filter
7511 (cons (concat (if strip "-" "+") tag)
7512 (if narrow current nil)))
7513 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
7514 (setq maybe-refresh t))
7515 (t (error "Invalid tag selection character %c" char)))
7516 (when (and maybe-refresh
7517 (eq org-agenda-clockreport-mode 'with-filter))
7518 (org-agenda-redo))))
7520 (defun org-agenda-get-represented-tags ()
7521 "Get a list of all tags currently represented in the agenda."
7522 (let (p tags)
7523 (save-excursion
7524 (goto-char (point-min))
7525 (while (setq p (next-single-property-change (point) 'tags))
7526 (goto-char p)
7527 (mapc (lambda (x) (add-to-list 'tags x))
7528 (get-text-property (point) 'tags))))
7529 tags))
7531 (defun org-agenda-filter-by-tag-refine (strip &optional char)
7532 "Refine the current filter. See `org-agenda-filter-by-tag'."
7533 (interactive "P")
7534 (org-agenda-filter-by-tag strip char 'refine))
7536 (defun org-agenda-filter-make-matcher (filter type)
7537 "Create the form that tests a line for agenda filter."
7538 (let (f f1)
7539 (cond
7540 ;; Tag filter
7541 ((eq type 'tag)
7542 (setq filter
7543 (delete-dups
7544 (append (get 'org-agenda-tag-filter :preset-filter)
7545 filter)))
7546 (dolist (x filter)
7547 (let ((nfilter (org-agenda-filter-expand-tags filter)) nf nf1
7548 (ffunc
7549 (lambda (nf0 nf01 fltr notgroup op)
7550 (dolist (x fltr)
7551 (if (member x '("-" "+"))
7552 (setq nf01 (if (equal x "-") 'tags '(not tags)))
7553 (setq nf01 (list 'member (downcase (substring x 1))
7554 'tags))
7555 (when (equal (string-to-char x) ?-)
7556 (setq nf01 (list 'not nf01))
7557 (when (not notgroup) (setq op 'and))))
7558 (push nf01 nf0))
7559 (if notgroup
7560 (push (cons 'and nf0) f)
7561 (push (cons (or op 'or) nf0) f)))))
7562 (cond ((equal filter '("+"))
7563 (setq f (list (list 'not 'tags))))
7564 ((equal nfilter filter)
7565 (funcall ffunc f1 f filter t nil))
7566 (t (funcall ffunc nf1 nf nfilter nil nil))))))
7567 ;; Category filter
7568 ((eq type 'category)
7569 (setq filter
7570 (delete-dups
7571 (append (get 'org-agenda-category-filter :preset-filter)
7572 filter)))
7573 (dolist (x filter)
7574 (if (equal "-" (substring x 0 1))
7575 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7576 (setq f1 (list 'equal (substring x 1) 'cat)))
7577 (push f1 f)))
7578 ;; Regexp filter
7579 ((eq type 'regexp)
7580 (setq filter
7581 (delete-dups
7582 (append (get 'org-agenda-regexp-filter :preset-filter)
7583 filter)))
7584 (dolist (x filter)
7585 (if (equal "-" (substring x 0 1))
7586 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7587 (setq f1 (list 'string-match (substring x 1) 'txt)))
7588 (push f1 f)))
7589 ;; Effort filter
7590 ((eq type 'effort)
7591 (setq filter
7592 (delete-dups
7593 (append (get 'org-agenda-effort-filter :preset-filter)
7594 filter)))
7595 (dolist (x filter)
7596 (push (org-agenda-filter-effort-form x) f))))
7597 (cons 'and (nreverse f))))
7599 (defun org-agenda-filter-effort-form (e)
7600 "Return the form to compare the effort of the current line with what E says.
7601 E looks like \"+<2:25\"."
7602 (let (op)
7603 (setq e (substring e 1))
7604 (setq op (string-to-char e) e (substring e 1))
7605 (setq op (cond ((equal op ?<) '<=)
7606 ((equal op ?>) '>=)
7607 ((equal op ??) op)
7608 (t '=)))
7609 (list 'org-agenda-compare-effort (list 'quote op)
7610 (org-duration-string-to-minutes e))))
7612 (defun org-agenda-compare-effort (op value)
7613 "Compare the effort of the current line with VALUE, using OP.
7614 If the line does not have an effort defined, return nil."
7615 (let ((eff (org-get-at-eol 'effort-minutes 1)))
7616 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 -1))
7617 value)))
7619 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7620 "Expand group tags in FILTER for the agenda.
7621 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7622 (if org-group-tags
7623 (let ((case-fold-search t) rtn)
7624 (mapc
7625 (lambda (f)
7626 (let (f0 dir)
7627 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7628 (setq dir (match-string 1 f) f0 (match-string 2 f))
7629 (setq dir (if no-operator "" "+") f0 f))
7630 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7631 (org-tags-expand f0 t t))
7632 rtn))))
7633 filter)
7634 (reverse rtn))
7635 filter))
7637 (defun org-agenda-filter-apply (filter type)
7638 "Set FILTER as the new agenda filter and apply it."
7639 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7640 (if org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7641 (let (tags cat txt effort-minutes)
7642 (setq org-agenda-filter-form
7643 (org-agenda-filter-make-matcher filter type))
7644 (if (and (eq type 'category)
7645 (not (equal (substring (car filter) 0 1) "-")))
7646 ;; Only set `org-agenda-filtered-by-category' to t
7647 ;; when a unique category is used as the filter
7648 (setq org-agenda-filtered-by-category t))
7649 (org-agenda-set-mode-name)
7650 (save-excursion
7651 (goto-char (point-min))
7652 (while (not (eobp))
7653 (if (org-get-at-bol 'org-marker)
7654 (progn
7655 (setq tags ; used in eval
7656 (apply 'append
7657 (mapcar (lambda (f)
7658 (org-agenda-filter-expand-tags (list f) t))
7659 (org-get-at-bol 'tags)))
7660 cat (org-get-at-eol 'org-category 1)
7661 txt (org-get-at-eol 'txt 1)
7662 effort-minutes (org-get-at-eol 'effort-minutes 1))
7663 (if (not (eval org-agenda-filter-form))
7664 (org-agenda-filter-hide-line type))
7665 (beginning-of-line 2))
7666 (beginning-of-line 2))))
7667 (if (get-char-property (point) 'invisible)
7668 (ignore-errors (org-agenda-previous-line)))))
7670 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7671 "Filter by top headline HL."
7672 (org-agenda-set-mode-name)
7673 (save-excursion
7674 (goto-char (point-min))
7675 (while (not (eobp))
7676 (let* ((pos (org-get-at-bol 'org-hd-marker))
7677 (tophl (and pos (org-find-top-headline pos))))
7678 (if (and tophl (funcall (if negative 'identity 'not)
7679 (string= hl tophl)))
7680 (org-agenda-filter-hide-line 'top-headline)))
7681 (beginning-of-line 2)))
7682 (if (get-char-property (point) 'invisible)
7683 (org-agenda-previous-line))
7684 (setq org-agenda-top-headline-filter hl
7685 org-agenda-filtered-by-top-headline t))
7687 (defun org-agenda-filter-hide-line (type)
7688 "Hide lines with TYPE in the agenda buffer."
7689 (let* ((b (max (point-min) (1- (point-at-bol))))
7690 (e (point-at-eol)))
7691 (let ((inhibit-read-only t))
7692 (add-text-properties
7693 b e `(invisible org-filtered org-filter-type ,type)))))
7695 (defun org-agenda-remove-filter (type)
7696 (interactive)
7697 "Remove filter of type TYPE from the agenda buffer."
7698 (save-excursion
7699 (goto-char (point-min))
7700 (let ((inhibit-read-only t) pos)
7701 (while (setq pos (text-property-any (point) (point-max) 'org-filter-type type))
7702 (goto-char pos)
7703 (remove-text-properties
7704 (point) (next-single-property-change (point) 'org-filter-type)
7705 `(invisible org-filtered org-filter-type ,type))))
7706 (set (intern (format "org-agenda-%s-filter" (intern-soft type))) nil)
7707 (setq org-agenda-filter-form nil)
7708 (org-agenda-set-mode-name)
7709 (org-agenda-finalize)))
7711 (defun org-agenda-filter-show-all-tag nil
7712 (org-agenda-remove-filter 'tag))
7713 (defun org-agenda-filter-show-all-re nil
7714 (org-agenda-remove-filter 'regexp))
7715 (defun org-agenda-filter-show-all-effort nil
7716 (org-agenda-remove-filter 'effort))
7717 (defun org-agenda-filter-show-all-cat nil
7718 (org-agenda-remove-filter 'category))
7719 (defun org-agenda-filter-show-all-top-filter nil
7720 (org-agenda-remove-filter 'top-headline))
7722 (defun org-agenda-manipulate-query-add ()
7723 "Manipulate the query by adding a search term with positive selection.
7724 Positive selection means the term must be matched for selection of an entry."
7725 (interactive)
7726 (org-agenda-manipulate-query ?\[))
7727 (defun org-agenda-manipulate-query-subtract ()
7728 "Manipulate the query by adding a search term with negative selection.
7729 Negative selection means term must not be matched for selection of an entry."
7730 (interactive)
7731 (org-agenda-manipulate-query ?\]))
7732 (defun org-agenda-manipulate-query-add-re ()
7733 "Manipulate the query by adding a search regexp with positive selection.
7734 Positive selection means the regexp must match for selection of an entry."
7735 (interactive)
7736 (org-agenda-manipulate-query ?\{))
7737 (defun org-agenda-manipulate-query-subtract-re ()
7738 "Manipulate the query by adding a search regexp with negative selection.
7739 Negative selection means regexp must not match for selection of an entry."
7740 (interactive)
7741 (org-agenda-manipulate-query ?\}))
7742 (defun org-agenda-manipulate-query (char)
7743 (cond
7744 ((memq org-agenda-type '(timeline agenda))
7745 (let ((org-agenda-include-inactive-timestamps t))
7746 (org-agenda-redo))
7747 (message "Display now includes inactive timestamps as well"))
7748 ((eq org-agenda-type 'search)
7749 (org-add-to-string
7750 'org-agenda-query-string
7751 (if org-agenda-last-search-view-search-was-boolean
7752 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7753 (?\{ . " +{}") (?\} . " -{}"))))
7754 " "))
7755 (setq org-agenda-redo-command
7756 (list 'org-search-view
7757 (car (get-text-property (min (1- (point-max)) (point))
7758 'org-last-args))
7759 org-agenda-query-string
7760 (+ (length org-agenda-query-string)
7761 (if (member char '(?\{ ?\})) 0 1))))
7762 (set-register org-agenda-query-register org-agenda-query-string)
7763 (let ((org-agenda-overriding-arguments
7764 (cdr org-agenda-redo-command)))
7765 (org-agenda-redo)))
7766 (t (error "Cannot manipulate query for %s-type agenda buffers"
7767 org-agenda-type))))
7769 (defun org-add-to-string (var string)
7770 (set var (concat (symbol-value var) string)))
7772 (defun org-agenda-goto-date (span)
7773 "Jump to DATE in agenda."
7774 (interactive "P")
7775 (let* ((org-read-date-prefer-future
7776 (eval org-agenda-jump-prefer-future))
7777 (date (org-read-date))
7778 (day (time-to-days (org-time-string-to-time date)))
7779 (org-agenda-sticky-orig org-agenda-sticky)
7780 (org-agenda-buffer-tmp-name (buffer-name))
7781 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7782 (0-arg (or current-prefix-arg (car args)))
7783 (2-arg (nth 2 args))
7784 (with-hour-p (nth 4 org-agenda-redo-command))
7785 (newcmd (list 'org-agenda-list 0-arg date
7786 (org-agenda-span-to-ndays
7787 2-arg (org-time-string-to-absolute date))
7788 with-hour-p))
7789 (newargs (cdr newcmd))
7790 (inhibit-read-only t)
7791 org-agenda-sticky)
7792 (if (not (org-agenda-check-type t 'agenda))
7793 (error "Not available in non-agenda views")
7794 (add-text-properties (point-min) (point-max)
7795 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7796 (org-agenda-redo)
7797 (goto-char (point-min))
7798 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7799 (save-excursion (move-beginning-of-line 2) (eobp))))
7800 (move-beginning-of-line 2))
7801 (setq org-agenda-sticky org-agenda-sticky-orig
7802 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7804 (defun org-agenda-goto-today ()
7805 "Go to today."
7806 (interactive)
7807 (org-agenda-check-type t 'timeline 'agenda)
7808 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7809 (curspan (nth 2 args))
7810 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7811 (cond
7812 (tdpos (goto-char tdpos))
7813 ((eq org-agenda-type 'agenda)
7814 (let* ((sd (org-agenda-compute-starting-span
7815 (org-today) (or curspan org-agenda-ndays org-agenda-span)))
7816 (org-agenda-overriding-arguments args))
7817 (setf (nth 1 org-agenda-overriding-arguments) sd)
7818 (org-agenda-redo)
7819 (org-agenda-find-same-or-today-or-agenda)))
7820 (t (error "Cannot find today")))))
7822 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7823 (goto-char
7824 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7825 (text-property-any (point-min) (point-max) 'org-today t)
7826 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7827 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7828 (org-agenda-backward-block))
7829 (point-min))))
7831 (defun org-agenda-backward-block ()
7832 "Move backward by one agenda block."
7833 (interactive)
7834 (org-agenda-forward-block 'backward))
7836 (defun org-agenda-forward-block (&optional backward)
7837 "Move forward by one agenda block.
7838 When optional argument BACKWARD is set, go backward"
7839 (interactive)
7840 (cond ((not (derived-mode-p 'org-agenda-mode))
7841 (user-error
7842 "Cannot execute this command outside of org-agenda-mode buffers"))
7843 ((looking-at (if backward "\\`" "\\'"))
7844 (message "Already at the %s block" (if backward "first" "last")))
7845 (t (let ((pos (prog1 (point)
7846 (ignore-errors (if backward (backward-char 1)
7847 (move-end-of-line 1)))))
7848 (f (if backward
7849 'previous-single-property-change
7850 'next-single-property-change))
7851 moved dest)
7852 (while (and (setq dest (funcall
7853 f (point) 'org-agenda-structural-header))
7854 (not (get-text-property
7855 (point) 'org-agenda-structural-header)))
7856 (setq moved t)
7857 (goto-char dest))
7858 (if moved (move-beginning-of-line 1)
7859 (goto-char (if backward (point-min) (point-max)))
7860 (move-beginning-of-line 1)
7861 (message "No %s block" (if backward "previous" "further")))))))
7863 (defun org-agenda-later (arg)
7864 "Go forward in time by the current span.
7865 With prefix ARG, go forward that many times the current span."
7866 (interactive "p")
7867 (org-agenda-check-type t 'agenda)
7868 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7869 (span (or (nth 2 args) org-agenda-current-span))
7870 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7871 (greg (calendar-gregorian-from-absolute sd))
7872 (cnt (org-get-at-bol 'org-day-cnt))
7873 greg2)
7874 (cond
7875 ((numberp span)
7876 (setq sd (+ (* span arg) sd)))
7877 ((eq span 'day)
7878 (setq sd (+ arg sd)))
7879 ((eq span 'week)
7880 (setq sd (+ (* 7 arg) sd)))
7881 ((eq span 'fortnight)
7882 (setq sd (+ (* 14 arg) sd)))
7883 ((eq span 'month)
7884 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7885 sd (calendar-absolute-from-gregorian greg2))
7886 (setcar greg2 (1+ (car greg2))))
7887 ((eq span 'year)
7888 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7889 sd (calendar-absolute-from-gregorian greg2))
7890 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7892 (setq sd (+ (* span arg) sd))))
7893 (let ((org-agenda-overriding-cmd
7894 ;; `cmd' may have been set by `org-agenda-run-series' which
7895 ;; uses `org-agenda-overriding-cmd' to decide whether
7896 ;; overriding is allowed for `cmd'
7897 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7898 (org-agenda-overriding-arguments
7899 (list (car args) sd span)))
7900 (org-agenda-redo)
7901 (org-agenda-find-same-or-today-or-agenda cnt))))
7903 (defun org-agenda-earlier (arg)
7904 "Go backward in time by the current span.
7905 With prefix ARG, go backward that many times the current span."
7906 (interactive "p")
7907 (org-agenda-later (- arg)))
7909 (defun org-agenda-view-mode-dispatch ()
7910 "Call one of the view mode commands."
7911 (interactive)
7912 (message "View: [d]ay [w]eek for[t]night [m]onth [y]ear [SPC]reset [q]uit/abort
7913 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7914 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7915 (let ((a (read-char-exclusive)))
7916 (case a
7917 (?\ (call-interactively 'org-agenda-reset-view))
7918 (?d (call-interactively 'org-agenda-day-view))
7919 (?w (call-interactively 'org-agenda-week-view))
7920 (?t (call-interactively 'org-agenda-fortnight-view))
7921 (?m (call-interactively 'org-agenda-month-view))
7922 (?y (call-interactively 'org-agenda-year-view))
7923 (?l (call-interactively 'org-agenda-log-mode))
7924 (?L (org-agenda-log-mode '(4)))
7925 (?c (org-agenda-log-mode 'clockcheck))
7926 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
7927 (?a (call-interactively 'org-agenda-archives-mode))
7928 (?A (org-agenda-archives-mode 'files))
7929 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7930 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7931 (?G (call-interactively 'org-agenda-toggle-time-grid))
7932 (?D (call-interactively 'org-agenda-toggle-diary))
7933 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7934 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7935 (org-agenda-check-type t 'timeline 'agenda)
7936 (org-agenda-redo))
7937 (message "Display now includes inactive timestamps as well"))
7938 (?q (message "Abort"))
7939 (otherwise (error "Invalid key" )))))
7941 (defun org-agenda-reset-view ()
7942 "Switch to default view for agenda."
7943 (interactive)
7944 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
7945 (defun org-agenda-day-view (&optional day-of-month)
7946 "Switch to daily view for agenda.
7947 With argument DAY-OF-MONTH, switch to that day of the month."
7948 (interactive "P")
7949 (org-agenda-change-time-span 'day day-of-month))
7950 (defun org-agenda-week-view (&optional iso-week)
7951 "Switch to daily view for agenda.
7952 With argument ISO-WEEK, switch to the corresponding ISO week.
7953 If ISO-WEEK has more then 2 digits, only the last two encode the
7954 week. Any digits before this encode a year. So 200712 means
7955 week 12 of year 2007. Years in the range 1938-2037 can also be
7956 written as 2-digit years."
7957 (interactive "P")
7958 (org-agenda-change-time-span 'week iso-week))
7959 (defun org-agenda-fortnight-view (&optional iso-week)
7960 "Switch to daily view for agenda.
7961 With argument ISO-WEEK, switch to the corresponding ISO week.
7962 If ISO-WEEK has more then 2 digits, only the last two encode the
7963 week. Any digits before this encode a year. So 200712 means
7964 week 12 of year 2007. Years in the range 1938-2037 can also be
7965 written as 2-digit years."
7966 (interactive "P")
7967 (org-agenda-change-time-span 'fortnight iso-week))
7968 (defun org-agenda-month-view (&optional month)
7969 "Switch to monthly view for agenda.
7970 With argument MONTH, switch to that month."
7971 (interactive "P")
7972 (org-agenda-change-time-span 'month month))
7973 (defun org-agenda-year-view (&optional year)
7974 "Switch to yearly view for agenda.
7975 With argument YEAR, switch to that year.
7976 If MONTH has more then 2 digits, only the last two encode the
7977 month. Any digits before this encode a year. So 200712 means
7978 December year 2007. Years in the range 1938-2037 can also be
7979 written as 2-digit years."
7980 (interactive "P")
7981 (when year
7982 (setq year (org-small-year-to-year year)))
7983 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
7984 (org-agenda-change-time-span 'year year)
7985 (error "Abort")))
7987 (defun org-agenda-change-time-span (span &optional n)
7988 "Change the agenda view to SPAN.
7989 SPAN may be `day', `week', `fortnight', `month', `year'."
7990 (org-agenda-check-type t 'agenda)
7991 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7992 (curspan (nth 2 args)))
7993 (if (and (not n) (equal curspan span))
7994 (error "Viewing span is already \"%s\"" span))
7995 (let* ((sd (or (org-get-at-bol 'day)
7996 (nth 1 args)
7997 org-starting-day))
7998 (sd (org-agenda-compute-starting-span sd span n))
7999 (org-agenda-overriding-cmd
8000 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
8001 (org-agenda-overriding-arguments
8002 (list (car args) sd span)))
8003 (org-agenda-redo)
8004 (org-agenda-find-same-or-today-or-agenda))
8005 (org-agenda-set-mode-name)
8006 (message "Switched to %s view" span)))
8008 (defun org-agenda-compute-starting-span (sd span &optional n)
8009 "Compute starting date for agenda.
8010 SPAN may be `day', `week', `fortnight', `month', `year'. The return value
8011 is a cons cell with the starting date and the number of days,
8012 so that the date SD will be in that range."
8013 (let* ((greg (calendar-gregorian-from-absolute sd))
8014 (dg (nth 1 greg))
8015 (mg (car greg))
8016 (yg (nth 2 greg)))
8017 (cond
8018 ((eq span 'day)
8019 (when n
8020 (setq sd (+ (calendar-absolute-from-gregorian
8021 (list mg 1 yg))
8022 n -1))))
8023 ((or (eq span 'week) (eq span 'fortnight))
8024 (let* ((nt (calendar-day-of-week
8025 (calendar-gregorian-from-absolute sd)))
8026 (d (if org-agenda-start-on-weekday
8027 (- nt org-agenda-start-on-weekday)
8030 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
8031 (when n
8032 (require 'cal-iso)
8033 (when (> n 99)
8034 (setq y1 (org-small-year-to-year (/ n 100))
8035 n (mod n 100)))
8036 (setq sd
8037 (calendar-absolute-from-iso
8038 (list n 1
8039 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
8040 ((eq span 'month)
8041 (let (y1)
8042 (when (and n (> n 99))
8043 (setq y1 (org-small-year-to-year (/ n 100))
8044 n (mod n 100)))
8045 (setq sd (calendar-absolute-from-gregorian
8046 (list (or n mg) 1 (or y1 yg))))))
8047 ((eq span 'year)
8048 (setq sd (calendar-absolute-from-gregorian
8049 (list 1 1 (or n yg))))))
8050 sd))
8052 (defun org-agenda-next-date-line (&optional arg)
8053 "Jump to the next line indicating a date in agenda buffer."
8054 (interactive "p")
8055 (org-agenda-check-type t 'agenda 'timeline)
8056 (beginning-of-line 1)
8057 ;; This does not work if user makes date format that starts with a blank
8058 (if (looking-at "^\\S-") (forward-char 1))
8059 (if (not (re-search-forward "^\\S-" nil t arg))
8060 (progn
8061 (backward-char 1)
8062 (error "No next date after this line in this buffer")))
8063 (goto-char (match-beginning 0)))
8065 (defun org-agenda-previous-date-line (&optional arg)
8066 "Jump to the previous line indicating a date in agenda buffer."
8067 (interactive "p")
8068 (org-agenda-check-type t 'agenda 'timeline)
8069 (beginning-of-line 1)
8070 (if (not (re-search-backward "^\\S-" nil t arg))
8071 (error "No previous date before this line in this buffer")))
8073 ;; Initialize the highlight
8074 (defvar org-hl (make-overlay 1 1))
8075 (overlay-put org-hl 'face 'highlight)
8077 (defun org-highlight (begin end &optional buffer)
8078 "Highlight a region with overlay."
8079 (move-overlay org-hl begin end (or buffer (current-buffer))))
8081 (defun org-unhighlight ()
8082 "Detach overlay INDEX."
8083 (org-detach-overlay org-hl))
8085 (defun org-unhighlight-once ()
8086 "Remove the highlight from its position, and this function from the hook."
8087 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8088 (org-unhighlight))
8090 (defvar org-agenda-pre-follow-window-conf nil)
8091 (defun org-agenda-follow-mode ()
8092 "Toggle follow mode in an agenda buffer."
8093 (interactive)
8094 (unless org-agenda-follow-mode
8095 (setq org-agenda-pre-follow-window-conf
8096 (current-window-configuration)))
8097 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8098 (unless org-agenda-follow-mode
8099 (set-window-configuration org-agenda-pre-follow-window-conf))
8100 (org-agenda-set-mode-name)
8101 (org-agenda-do-context-action)
8102 (message "Follow mode is %s"
8103 (if org-agenda-follow-mode "on" "off")))
8105 (defun org-agenda-entry-text-mode (&optional arg)
8106 "Toggle entry text mode in an agenda buffer."
8107 (interactive "P")
8108 (if (or org-agenda-tag-filter
8109 org-agenda-category-filter
8110 org-agenda-regexp-filter
8111 org-agenda-top-headline-filter)
8112 (user-error "Can't show entry text in filtered views")
8113 (setq org-agenda-entry-text-mode (or (integerp arg)
8114 (not org-agenda-entry-text-mode)))
8115 (org-agenda-entry-text-hide)
8116 (and org-agenda-entry-text-mode
8117 (let ((org-agenda-entry-text-maxlines
8118 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8119 (org-agenda-entry-text-show)))
8120 (org-agenda-set-mode-name)
8121 (message "Entry text mode is %s%s"
8122 (if org-agenda-entry-text-mode "on" "off")
8123 (if (not org-agenda-entry-text-mode) ""
8124 (format " (maximum number of lines is %d)"
8125 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8127 (defun org-agenda-clockreport-mode (&optional with-filter)
8128 "Toggle clocktable mode in an agenda buffer.
8129 With prefix arg WITH-FILTER, make the clocktable respect the current
8130 agenda filter."
8131 (interactive "P")
8132 (org-agenda-check-type t 'agenda)
8133 (if with-filter
8134 (setq org-agenda-clockreport-mode 'with-filter)
8135 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
8136 (org-agenda-set-mode-name)
8137 (org-agenda-redo)
8138 (message "Clocktable mode is %s"
8139 (if org-agenda-clockreport-mode "on" "off")))
8141 (defun org-agenda-log-mode (&optional special)
8142 "Toggle log mode in an agenda buffer.
8143 With argument SPECIAL, show all possible log items, not only the ones
8144 configured in `org-agenda-log-mode-items'.
8145 With a double `C-u' prefix arg, show *only* log items, nothing else."
8146 (interactive "P")
8147 (org-agenda-check-type t 'agenda 'timeline)
8148 (setq org-agenda-show-log
8149 (cond
8150 ((equal special '(16)) 'only)
8151 ((eq special 'clockcheck)
8152 (if (eq org-agenda-show-log 'clockcheck)
8153 nil 'clockcheck))
8154 (special '(closed clock state))
8155 (t (not org-agenda-show-log))))
8156 (org-agenda-set-mode-name)
8157 (org-agenda-redo)
8158 (message "Log mode is %s"
8159 (if org-agenda-show-log "on" "off")))
8161 (defun org-agenda-archives-mode (&optional with-files)
8162 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8163 When called with a prefix argument, include all archive files as well."
8164 (interactive "P")
8165 (setq org-agenda-archives-mode
8166 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8167 (org-agenda-set-mode-name)
8168 (org-agenda-redo)
8169 (message
8170 "%s"
8171 (cond
8172 ((eq org-agenda-archives-mode nil)
8173 "No archives are included")
8174 ((eq org-agenda-archives-mode 'trees)
8175 (format "Trees with :%s: tag are included" org-archive-tag))
8176 ((eq org-agenda-archives-mode t)
8177 (format "Trees with :%s: tag and all active archive files are included"
8178 org-archive-tag)))))
8180 (defun org-agenda-toggle-diary ()
8181 "Toggle diary inclusion in an agenda buffer."
8182 (interactive)
8183 (org-agenda-check-type t 'agenda)
8184 (setq org-agenda-include-diary (not org-agenda-include-diary))
8185 (org-agenda-redo)
8186 (org-agenda-set-mode-name)
8187 (message "Diary inclusion turned %s"
8188 (if org-agenda-include-diary "on" "off")))
8190 (defun org-agenda-toggle-deadlines ()
8191 "Toggle inclusion of entries with a deadline in an agenda buffer."
8192 (interactive)
8193 (org-agenda-check-type t 'agenda)
8194 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8195 (org-agenda-redo)
8196 (org-agenda-set-mode-name)
8197 (message "Deadlines inclusion turned %s"
8198 (if org-agenda-include-deadlines "on" "off")))
8200 (defun org-agenda-toggle-time-grid ()
8201 "Toggle time grid in an agenda buffer."
8202 (interactive)
8203 (org-agenda-check-type t 'agenda)
8204 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8205 (org-agenda-redo)
8206 (org-agenda-set-mode-name)
8207 (message "Time-grid turned %s"
8208 (if org-agenda-use-time-grid "on" "off")))
8210 (defun org-agenda-set-mode-name ()
8211 "Set the mode name to indicate all the small mode settings."
8212 (setq mode-name
8213 (list "Org-Agenda"
8214 (if (get 'org-agenda-files 'org-restrict) " []" "")
8216 '(:eval (org-agenda-span-name org-agenda-current-span))
8217 (if org-agenda-follow-mode " Follow" "")
8218 (if org-agenda-entry-text-mode " ETxt" "")
8219 (if org-agenda-include-diary " Diary" "")
8220 (if org-agenda-include-deadlines " Ddl" "")
8221 (if org-agenda-use-time-grid " Grid" "")
8222 (if (and (boundp 'org-habit-show-habits)
8223 org-habit-show-habits) " Habit" "")
8224 (cond
8225 ((consp org-agenda-show-log) " LogAll")
8226 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8227 (org-agenda-show-log " Log")
8228 (t ""))
8229 (if (or org-agenda-category-filter
8230 (get 'org-agenda-category-filter :preset-filter))
8231 '(:eval (org-propertize
8232 (concat " <"
8233 (mapconcat
8234 'identity
8235 (append
8236 (get 'org-agenda-category-filter :preset-filter)
8237 org-agenda-category-filter)
8239 ">")
8240 'face 'org-agenda-filter-category
8241 'help-echo "Category used in filtering")) "")
8242 (if (or org-agenda-tag-filter
8243 (get 'org-agenda-tag-filter :preset-filter))
8244 '(:eval (org-propertize
8245 (concat " {"
8246 (mapconcat
8247 'identity
8248 (append
8249 (get 'org-agenda-tag-filter :preset-filter)
8250 org-agenda-tag-filter)
8252 "}")
8253 'face 'org-agenda-filter-tags
8254 'help-echo "Tags used in filtering")) "")
8255 (if (or org-agenda-effort-filter
8256 (get 'org-agenda-effort-filter :preset-filter))
8257 '(:eval (org-propertize
8258 (concat " {"
8259 (mapconcat
8260 'identity
8261 (append
8262 (get 'org-agenda-effort-filter :preset-filter)
8263 org-agenda-effort-filter)
8265 "}")
8266 'face 'org-agenda-filter-effort
8267 'help-echo "Effort conditions used in filtering")) "")
8268 (if (or org-agenda-regexp-filter
8269 (get 'org-agenda-regexp-filter :preset-filter))
8270 '(:eval (org-propertize
8271 (concat " ["
8272 (mapconcat
8273 'identity
8274 (append
8275 (get 'org-agenda-regexp-filter :preset-filter)
8276 org-agenda-regexp-filter)
8278 "]")
8279 'face 'org-agenda-filter-regexp
8280 'help-echo "Regexp used in filtering")) "")
8281 (if org-agenda-archives-mode
8282 (if (eq org-agenda-archives-mode t)
8283 " Archives"
8284 (format " :%s:" org-archive-tag))
8286 (if org-agenda-clockreport-mode
8287 (if (eq org-agenda-clockreport-mode 'with-filter)
8288 " Clock{}" " Clock")
8289 "")))
8290 (force-mode-line-update))
8292 (define-obsolete-function-alias
8293 'org-agenda-post-command-hook 'org-agenda-update-agenda-type "24.3")
8295 (defun org-agenda-update-agenda-type ()
8296 "Update the agenda type after each command."
8297 (setq org-agenda-type
8298 (or (get-text-property (point) 'org-agenda-type)
8299 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8301 (defun org-agenda-next-line ()
8302 "Move cursor to the next line, and show if follow mode is active."
8303 (interactive)
8304 (call-interactively 'next-line)
8305 (org-agenda-do-context-action))
8307 (defun org-agenda-previous-line ()
8308 "Move cursor to the previous line, and show if follow-mode is active."
8309 (interactive)
8310 (call-interactively 'previous-line)
8311 (org-agenda-do-context-action))
8313 (defun org-agenda-next-item (n)
8314 "Move cursor to next agenda item."
8315 (interactive "p")
8316 (let ((col (current-column)))
8317 (dotimes (c n)
8318 (when (next-single-property-change (point-at-eol) 'org-marker)
8319 (move-end-of-line 1)
8320 (goto-char (next-single-property-change (point) 'org-marker))))
8321 (org-move-to-column col))
8322 (org-agenda-do-context-action))
8324 (defun org-agenda-previous-item (n)
8325 "Move cursor to next agenda item."
8326 (interactive "p")
8327 (dotimes (c n)
8328 (let ((col (current-column))
8329 (goto (save-excursion
8330 (move-end-of-line 0)
8331 (previous-single-property-change (point) 'org-marker))))
8332 (if goto (goto-char goto))
8333 (org-move-to-column col)))
8334 (org-agenda-do-context-action))
8336 (defun org-agenda-do-context-action ()
8337 "Show outline path and, maybe, follow mode window."
8338 (let ((m (org-get-at-bol 'org-marker)))
8339 (when (and (markerp m) (marker-buffer m))
8340 (and org-agenda-follow-mode
8341 (if org-agenda-follow-indirect
8342 (org-agenda-tree-to-indirect-buffer nil)
8343 (org-agenda-show)))
8344 (and org-agenda-show-outline-path
8345 (org-with-point-at m (org-display-outline-path t))))))
8347 (defun org-agenda-show-tags ()
8348 "Show the tags applicable to the current item."
8349 (interactive)
8350 (let* ((tags (org-get-at-bol 'tags)))
8351 (if tags
8352 (message "Tags are :%s:"
8353 (org-no-properties (mapconcat 'identity tags ":")))
8354 (message "No tags associated with this line"))))
8356 (defun org-agenda-goto (&optional highlight)
8357 "Go to the Org-mode file which contains the item at point."
8358 (interactive)
8359 (let* ((marker (or (org-get-at-bol 'org-marker)
8360 (org-agenda-error)))
8361 (buffer (marker-buffer marker))
8362 (pos (marker-position marker)))
8363 (switch-to-buffer-other-window buffer)
8364 (widen)
8365 (push-mark)
8366 (goto-char pos)
8367 (when (derived-mode-p 'org-mode)
8368 (org-show-context 'agenda)
8369 (save-excursion
8370 (and (outline-next-heading)
8371 (org-flag-heading nil)))) ; show the next heading
8372 (when (outline-invisible-p)
8373 (show-entry)) ; display invisible text
8374 (recenter (/ (window-height) 2))
8375 (run-hooks 'org-agenda-after-show-hook)
8376 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8378 (defvar org-agenda-after-show-hook nil
8379 "Normal hook run after an item has been shown from the agenda.
8380 Point is in the buffer where the item originated.")
8382 (defun org-agenda-kill ()
8383 "Kill the entry or subtree belonging to the current agenda entry."
8384 (interactive)
8385 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8386 (let* ((bufname-orig (buffer-name))
8387 (marker (or (org-get-at-bol 'org-marker)
8388 (org-agenda-error)))
8389 (buffer (marker-buffer marker))
8390 (pos (marker-position marker))
8391 (type (org-get-at-bol 'type))
8392 dbeg dend (n 0) conf)
8393 (org-with-remote-undo buffer
8394 (with-current-buffer buffer
8395 (save-excursion
8396 (goto-char pos)
8397 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8398 (setq dbeg (progn (org-back-to-heading t) (point))
8399 dend (org-end-of-subtree t t))
8400 (setq dbeg (point-at-bol)
8401 dend (min (point-max) (1+ (point-at-eol)))))
8402 (goto-char dbeg)
8403 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8404 (setq conf (or (eq t org-agenda-confirm-kill)
8405 (and (numberp org-agenda-confirm-kill)
8406 (> n org-agenda-confirm-kill))))
8407 (and conf
8408 (not (y-or-n-p
8409 (format "Delete entry with %d lines in buffer \"%s\"? "
8410 n (buffer-name buffer))))
8411 (error "Abort"))
8412 (let ((org-agenda-buffer-name bufname-orig))
8413 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8414 (with-current-buffer buffer (delete-region dbeg dend))
8415 (message "Agenda item and source killed"))))
8417 (defvar org-archive-default-command) ; defined in org-archive.el
8418 (defun org-agenda-archive-default ()
8419 "Archive the entry or subtree belonging to the current agenda entry."
8420 (interactive)
8421 (require 'org-archive)
8422 (org-agenda-archive-with org-archive-default-command))
8424 (defun org-agenda-archive-default-with-confirmation ()
8425 "Archive the entry or subtree belonging to the current agenda entry."
8426 (interactive)
8427 (require 'org-archive)
8428 (org-agenda-archive-with org-archive-default-command 'confirm))
8430 (defun org-agenda-archive ()
8431 "Archive the entry or subtree belonging to the current agenda entry."
8432 (interactive)
8433 (org-agenda-archive-with 'org-archive-subtree))
8435 (defun org-agenda-archive-to-archive-sibling ()
8436 "Move the entry to the archive sibling."
8437 (interactive)
8438 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8440 (defun org-agenda-archive-with (cmd &optional confirm)
8441 "Move the entry to the archive sibling."
8442 (interactive)
8443 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8444 (let* ((bufname-orig (buffer-name))
8445 (marker (or (org-get-at-bol 'org-marker)
8446 (org-agenda-error)))
8447 (buffer (marker-buffer marker))
8448 (pos (marker-position marker)))
8449 (org-with-remote-undo buffer
8450 (with-current-buffer buffer
8451 (if (derived-mode-p 'org-mode)
8452 (if (and confirm
8453 (not (y-or-n-p "Archive this subtree or entry? ")))
8454 (error "Abort")
8455 (save-window-excursion
8456 (goto-char pos)
8457 (let ((org-agenda-buffer-name bufname-orig))
8458 (org-remove-subtree-entries-from-agenda))
8459 (org-back-to-heading t)
8460 (funcall cmd)))
8461 (error "Archiving works only in Org-mode files"))))))
8463 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8464 "Remove all lines in the agenda that correspond to a given subtree.
8465 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8466 If this information is not given, the function uses the tree at point."
8467 (let ((buf (or buf (current-buffer))) m p)
8468 (save-excursion
8469 (unless (and beg end)
8470 (org-back-to-heading t)
8471 (setq beg (point))
8472 (org-end-of-subtree t)
8473 (setq end (point)))
8474 (set-buffer (get-buffer org-agenda-buffer-name))
8475 (save-excursion
8476 (goto-char (point-max))
8477 (beginning-of-line 1)
8478 (while (not (bobp))
8479 (when (and (setq m (org-get-at-bol 'org-marker))
8480 (equal buf (marker-buffer m))
8481 (setq p (marker-position m))
8482 (>= p beg)
8483 (< p end))
8484 (let ((inhibit-read-only t))
8485 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8486 (beginning-of-line 0))))))
8488 (defun org-agenda-refile (&optional goto rfloc no-update)
8489 "Refile the item at point.
8491 When GOTO is 0 or '(64), clear the refile cache.
8492 When GOTO is '(16), go to the location of the last refiled item.
8493 RFLOC can be a refile location obtained in a different way.
8494 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8495 (interactive "P")
8496 (cond
8497 ((member goto '(0 (64)))
8498 (org-refile-cache-clear))
8499 ((equal goto '(16))
8500 (org-refile-goto-last-stored))
8502 (let* ((buffer-orig (buffer-name))
8503 (marker (or (org-get-at-bol 'org-hd-marker)
8504 (org-agenda-error)))
8505 (buffer (marker-buffer marker))
8506 (pos (marker-position marker))
8507 (rfloc (or rfloc
8508 (org-refile-get-location
8509 (if goto "Goto" "Refile to") buffer
8510 org-refile-allow-creating-parent-nodes))))
8511 (with-current-buffer buffer
8512 (save-excursion
8513 (save-restriction
8514 (widen)
8515 (goto-char marker)
8516 (let ((org-agenda-buffer-name buffer-orig))
8517 (org-remove-subtree-entries-from-agenda))
8518 (org-refile goto buffer rfloc)))))
8519 (unless no-update (org-agenda-redo)))))
8521 (defun org-agenda-open-link (&optional arg)
8522 "Open the link(s) in the current entry, if any.
8523 This looks for a link in the displayed line in the agenda.
8524 It also looks at the text of the entry itself."
8525 (interactive "P")
8526 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8527 (org-get-at-bol 'org-marker)))
8528 (buffer (and marker (marker-buffer marker)))
8529 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8530 (lkall (and buffer (org-offer-links-in-entry
8531 buffer marker arg prefix)))
8532 (lk0 (car lkall))
8533 (lk (if (stringp lk0) (list lk0) lk0))
8534 (lkend (cdr lkall))
8535 trg)
8536 (cond
8537 ((and buffer lk)
8538 (mapcar (lambda(l)
8539 (with-current-buffer buffer
8540 (setq trg (and (string-match org-bracket-link-regexp l)
8541 (match-string 1 l)))
8542 (if (or (not trg) (string-match org-any-link-re trg))
8543 (save-excursion
8544 (save-restriction
8545 (widen)
8546 (goto-char marker)
8547 (when (search-forward l nil lkend)
8548 (goto-char (match-beginning 0))
8549 (org-open-at-point))))
8550 ;; This is an internal link, widen the buffer
8551 (switch-to-buffer-other-window buffer)
8552 (widen)
8553 (goto-char marker)
8554 (when (search-forward l nil lkend)
8555 (goto-char (match-beginning 0))
8556 (org-open-at-point)))))
8557 lk))
8558 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8559 (save-excursion
8560 (beginning-of-line 1)
8561 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8562 (org-open-link-from-string (match-string 1)))
8563 (t (message "No link to open here")))))
8565 (defun org-agenda-copy-local-variable (var)
8566 "Get a variable from a referenced buffer and install it here."
8567 (let ((m (org-get-at-bol 'org-marker)))
8568 (when (and m (buffer-live-p (marker-buffer m)))
8569 (org-set-local var (with-current-buffer (marker-buffer m)
8570 (symbol-value var))))))
8572 (defun org-agenda-switch-to (&optional delete-other-windows)
8573 "Go to the Org-mode file which contains the item at point."
8574 (interactive)
8575 (if (and org-return-follows-link
8576 (not (org-get-at-bol 'org-marker))
8577 (org-in-regexp org-bracket-link-regexp))
8578 (org-open-link-from-string (match-string 0))
8579 (let* ((marker (or (org-get-at-bol 'org-marker)
8580 (org-agenda-error)))
8581 (buffer (marker-buffer marker))
8582 (pos (marker-position marker)))
8583 (org-pop-to-buffer-same-window buffer)
8584 (and delete-other-windows (delete-other-windows))
8585 (widen)
8586 (goto-char pos)
8587 (org-back-to-heading t)
8588 (when (derived-mode-p 'org-mode)
8589 (org-show-context 'agenda)
8590 (save-excursion
8591 (and (outline-next-heading)
8592 (org-flag-heading nil))) ; show the next heading
8593 (when (outline-invisible-p)
8594 (show-entry)) ; display invisible text
8595 (run-hooks 'org-agenda-after-show-hook)))))
8597 (defun org-agenda-goto-mouse (ev)
8598 "Go to the Org-mode file which contains the item at the mouse click."
8599 (interactive "e")
8600 (mouse-set-point ev)
8601 (org-agenda-goto))
8603 (defun org-agenda-show (&optional full-entry)
8604 "Display the Org-mode file which contains the item at point.
8605 With prefix argument FULL-ENTRY, make the entire entry visible
8606 if it was hidden in the outline."
8607 (interactive "P")
8608 (let ((win (selected-window)))
8609 (if full-entry
8610 (let ((org-show-entry-below t))
8611 (org-agenda-goto t))
8612 (org-agenda-goto t))
8613 (select-window win)))
8615 (defvar org-agenda-show-window nil)
8616 (defun org-agenda-show-and-scroll-up (&optional arg)
8617 "Display the Org-mode file which contains the item at point.
8618 When called repeatedly, scroll the window that is displaying the buffer.
8619 With a \\[universal-argument] prefix, use `org-show-entry' instead of
8620 `show-subtree' to display the item, so that drawers and logbooks stay
8621 folded."
8622 (interactive "P")
8623 (let ((win (selected-window)))
8624 (if (and (window-live-p org-agenda-show-window)
8625 (eq this-command last-command))
8626 (progn
8627 (select-window org-agenda-show-window)
8628 (ignore-errors (scroll-up)))
8629 (org-agenda-goto t)
8630 (if arg (org-show-entry) (show-subtree))
8631 (setq org-agenda-show-window (selected-window)))
8632 (select-window win)))
8634 (defun org-agenda-show-scroll-down ()
8635 "Scroll down the window showing the agenda."
8636 (interactive)
8637 (let ((win (selected-window)))
8638 (when (window-live-p org-agenda-show-window)
8639 (select-window org-agenda-show-window)
8640 (ignore-errors (scroll-down))
8641 (select-window win))))
8643 (defun org-agenda-show-1 (&optional more)
8644 "Display the Org-mode file which contains the item at point.
8645 The prefix arg selects the amount of information to display:
8647 0 hide the subtree
8648 1 just show the entry according to defaults.
8649 2 show the children view
8650 3 show the subtree view
8651 4 show the entire subtree and any LOGBOOK drawers
8652 5 show the entire subtree and any drawers
8653 With prefix argument FULL-ENTRY, make the entire entry visible
8654 if it was hidden in the outline."
8655 (interactive "p")
8656 (let ((win (selected-window)))
8657 (org-agenda-goto t)
8658 (org-recenter-heading 1)
8659 (cond
8660 ((= more 0)
8661 (hide-subtree)
8662 (save-excursion
8663 (org-back-to-heading)
8664 (run-hook-with-args 'org-cycle-hook 'folded))
8665 (message "Remote: FOLDED"))
8666 ((and (org-called-interactively-p 'any) (= more 1))
8667 (message "Remote: show with default settings"))
8668 ((= more 2)
8669 (show-entry)
8670 (show-children)
8671 (save-excursion
8672 (org-back-to-heading)
8673 (run-hook-with-args 'org-cycle-hook 'children))
8674 (message "Remote: CHILDREN"))
8675 ((= more 3)
8676 (show-subtree)
8677 (save-excursion
8678 (org-back-to-heading)
8679 (run-hook-with-args 'org-cycle-hook 'subtree))
8680 (message "Remote: SUBTREE"))
8681 ((= more 4)
8682 (show-subtree)
8683 (save-excursion
8684 (org-back-to-heading)
8685 (org-cycle-hide-drawers 'subtree '("LOGBOOK")))
8686 (message "Remote: SUBTREE AND LOGBOOK"))
8687 ((> more 4)
8688 (show-subtree)
8689 (message "Remote: SUBTREE AND ALL DRAWERS")))
8690 (select-window win)))
8692 (defun org-recenter-heading (n)
8693 (save-excursion
8694 (org-back-to-heading)
8695 (recenter n)))
8697 (defvar org-agenda-cycle-counter nil)
8698 (defun org-agenda-cycle-show (&optional n)
8699 "Show the current entry in another window, with default settings.
8700 Default settings are taken from `org-show-hierarchy-above' and siblings.
8701 When use repeatedly in immediate succession, the remote entry will cycle
8702 through visibility
8704 children -> subtree -> folded
8706 When called with a numeric prefix arg, that arg will be passed through to
8707 `org-agenda-show-1'. For the interpretation of that argument, see the
8708 docstring of `org-agenda-show-1'."
8709 (interactive "P")
8710 (if (integerp n)
8711 (setq org-agenda-cycle-counter n)
8712 (if (not (eq last-command this-command))
8713 (setq org-agenda-cycle-counter 1)
8714 (if (equal org-agenda-cycle-counter 0)
8715 (setq org-agenda-cycle-counter 2)
8716 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8717 (if (> org-agenda-cycle-counter 3)
8718 (setq org-agenda-cycle-counter 0)))))
8719 (org-agenda-show-1 org-agenda-cycle-counter))
8721 (defun org-agenda-recenter (arg)
8722 "Display the Org-mode file which contains the item at point and recenter."
8723 (interactive "P")
8724 (let ((win (selected-window)))
8725 (org-agenda-goto t)
8726 (recenter arg)
8727 (select-window win)))
8729 (defun org-agenda-show-mouse (ev)
8730 "Display the Org-mode file which contains the item at the mouse click."
8731 (interactive "e")
8732 (mouse-set-point ev)
8733 (org-agenda-show))
8735 (defun org-agenda-check-no-diary ()
8736 "Check if the entry is a diary link and abort if yes."
8737 (if (org-get-at-bol 'org-agenda-diary-link)
8738 (org-agenda-error)))
8740 (defun org-agenda-error ()
8741 "Throw an error when a command is not allowed in the agenda."
8742 (user-error "Command not allowed in this line"))
8744 (defun org-agenda-tree-to-indirect-buffer (arg)
8745 "Show the subtree corresponding to the current entry in an indirect buffer.
8746 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8748 With a numerical prefix ARG, go up to this level and then take that tree.
8749 With a negative numeric ARG, go up by this number of levels.
8750 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
8751 use the dedicated frame)."
8752 (interactive "P")
8753 (if current-prefix-arg
8754 (org-agenda-do-tree-to-indirect-buffer arg)
8755 (let ((agenda-buffer (buffer-name))
8756 (agenda-window (selected-window))
8757 (indirect-window
8758 (and org-last-indirect-buffer
8759 (get-buffer-window org-last-indirect-buffer))))
8760 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8761 (unless (or (eq org-indirect-buffer-display 'new-frame)
8762 (eq org-indirect-buffer-display 'dedicated-frame))
8763 (unwind-protect
8764 (unless (and indirect-window (window-live-p indirect-window))
8765 (setq indirect-window (split-window agenda-window)))
8766 (and indirect-window (select-window indirect-window))
8767 (switch-to-buffer org-last-indirect-buffer :norecord)
8768 (fit-window-to-buffer indirect-window)))
8769 (select-window (get-buffer-window agenda-buffer)))))
8771 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8772 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8773 (org-agenda-check-no-diary)
8774 (let* ((marker (or (org-get-at-bol 'org-marker)
8775 (org-agenda-error)))
8776 (buffer (marker-buffer marker))
8777 (pos (marker-position marker)))
8778 (with-current-buffer buffer
8779 (save-excursion
8780 (goto-char pos)
8781 (funcall 'org-tree-to-indirect-buffer arg)))))
8783 (defvar org-last-heading-marker (make-marker)
8784 "Marker pointing to the headline that last changed its TODO state
8785 by a remote command from the agenda.")
8787 (defun org-agenda-todo-nextset ()
8788 "Switch TODO entry to next sequence."
8789 (interactive)
8790 (org-agenda-todo 'nextset))
8792 (defun org-agenda-todo-previousset ()
8793 "Switch TODO entry to previous sequence."
8794 (interactive)
8795 (org-agenda-todo 'previousset))
8797 (defun org-agenda-todo (&optional arg)
8798 "Cycle TODO state of line at point, also in Org-mode file.
8799 This changes the line at point, all other lines in the agenda referring to
8800 the same tree node, and the headline of the tree node in the Org-mode file."
8801 (interactive "P")
8802 (org-agenda-check-no-diary)
8803 (let* ((col (current-column))
8804 (marker (or (org-get-at-bol 'org-marker)
8805 (org-agenda-error)))
8806 (buffer (marker-buffer marker))
8807 (pos (marker-position marker))
8808 (hdmarker (org-get-at-bol 'org-hd-marker))
8809 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
8810 (inhibit-read-only t)
8811 org-agenda-headline-snapshot-before-repeat newhead just-one)
8812 (org-with-remote-undo buffer
8813 (with-current-buffer buffer
8814 (widen)
8815 (goto-char pos)
8816 (org-show-context 'agenda)
8817 (save-excursion
8818 (and (outline-next-heading)
8819 (org-flag-heading nil))) ; show the next heading
8820 (let ((current-prefix-arg arg))
8821 (call-interactively 'org-todo))
8822 (and (bolp) (forward-char 1))
8823 (setq newhead (org-get-heading))
8824 (when (and (org-bound-and-true-p
8825 org-agenda-headline-snapshot-before-repeat)
8826 (not (equal org-agenda-headline-snapshot-before-repeat
8827 newhead))
8828 todayp)
8829 (setq newhead org-agenda-headline-snapshot-before-repeat
8830 just-one t))
8831 (save-excursion
8832 (org-back-to-heading)
8833 (move-marker org-last-heading-marker (point))))
8834 (beginning-of-line 1)
8835 (save-window-excursion
8836 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8837 (when (org-bound-and-true-p org-clock-out-when-done)
8838 (string-match (concat "^" (regexp-opt org-done-keywords-for-agenda))
8839 newhead)
8840 (org-agenda-unmark-clocking-task))
8841 (org-move-to-column col))))
8843 (defun org-agenda-add-note (&optional arg)
8844 "Add a time-stamped note to the entry at point."
8845 (interactive "P")
8846 (org-agenda-check-no-diary)
8847 (let* ((marker (or (org-get-at-bol 'org-marker)
8848 (org-agenda-error)))
8849 (buffer (marker-buffer marker))
8850 (pos (marker-position marker))
8851 (hdmarker (org-get-at-bol 'org-hd-marker))
8852 (inhibit-read-only t))
8853 (with-current-buffer buffer
8854 (widen)
8855 (goto-char pos)
8856 (org-show-context 'agenda)
8857 (save-excursion
8858 (and (outline-next-heading)
8859 (org-flag-heading nil))) ; show the next heading
8860 (org-add-note))))
8862 (defun org-agenda-change-all-lines (newhead hdmarker
8863 &optional fixface just-this)
8864 "Change all lines in the agenda buffer which match HDMARKER.
8865 The new content of the line will be NEWHEAD (as modified by
8866 `org-agenda-format-item'). HDMARKER is checked with
8867 `equal' against all `org-hd-marker' text properties in the file.
8868 If FIXFACE is non-nil, the face of each item is modified according to
8869 the new TODO state.
8870 If JUST-THIS is non-nil, change just the current line, not all.
8871 If FORCE-TAGS is non nil, the car of it returns the new tags."
8872 (let* ((inhibit-read-only t)
8873 (line (org-current-line))
8874 (org-agenda-buffer (current-buffer))
8875 (thetags (with-current-buffer (marker-buffer hdmarker)
8876 (save-excursion (save-restriction (widen)
8877 (goto-char hdmarker)
8878 (org-get-tags-at)))))
8879 props m pl undone-face done-face finish new dotime level cat tags)
8880 (save-excursion
8881 (goto-char (point-max))
8882 (beginning-of-line 1)
8883 (while (not finish)
8884 (setq finish (bobp))
8885 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8886 (or (not just-this) (= (org-current-line) line))
8887 (equal m hdmarker))
8888 (setq props (text-properties-at (point))
8889 dotime (org-get-at-bol 'dotime)
8890 cat (org-get-at-eol 'org-category 1)
8891 level (org-get-at-bol 'level)
8892 tags thetags
8894 (let ((org-prefix-format-compiled
8895 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8896 org-prefix-format-compiled))
8897 (extra (org-get-at-bol 'extra)))
8898 (with-current-buffer (marker-buffer hdmarker)
8899 (save-excursion
8900 (save-restriction
8901 (widen)
8902 (org-agenda-format-item extra newhead level cat tags dotime)))))
8903 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8904 undone-face (org-get-at-bol 'undone-face)
8905 done-face (org-get-at-bol 'done-face))
8906 (beginning-of-line 1)
8907 (cond
8908 ((equal new "")
8909 (and (looking-at ".*\n?") (replace-match "")))
8910 ((looking-at ".*")
8911 (replace-match new t t)
8912 (beginning-of-line 1)
8913 (add-text-properties (point-at-bol) (point-at-eol) props)
8914 (when fixface
8915 (add-text-properties
8916 (point-at-bol) (point-at-eol)
8917 (list 'face
8918 (if org-last-todo-state-is-todo
8919 undone-face done-face))))
8920 (org-agenda-highlight-todo 'line)
8921 (beginning-of-line 1))
8922 (t (error "Line update did not work")))
8923 (save-restriction
8924 (narrow-to-region (point-at-bol) (point-at-eol))
8925 (org-agenda-finalize)))
8926 (beginning-of-line 0)))))
8928 (defun org-agenda-align-tags (&optional line)
8929 "Align all tags in agenda items to `org-agenda-tags-column'."
8930 (let ((inhibit-read-only t) l c)
8931 (save-excursion
8932 (goto-char (if line (point-at-bol) (point-min)))
8933 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
8934 (if line (point-at-eol) nil) t)
8935 (add-text-properties
8936 (match-beginning 2) (match-end 2)
8937 (list 'face (delq nil (let ((prop (get-text-property
8938 (match-beginning 2) 'face)))
8939 (or (listp prop) (setq prop (list prop)))
8940 (if (memq 'org-tag prop)
8941 prop
8942 (cons 'org-tag prop))))))
8943 (setq l (- (match-end 2) (match-beginning 2))
8944 c (if (< org-agenda-tags-column 0)
8945 (- (abs org-agenda-tags-column) l)
8946 org-agenda-tags-column))
8947 (delete-region (match-beginning 1) (match-end 1))
8948 (goto-char (match-beginning 1))
8949 (insert (org-add-props
8950 (make-string (max 1 (- c (current-column))) ?\ )
8951 (plist-put (copy-sequence (text-properties-at (point)))
8952 'face nil))))
8953 (goto-char (point-min))
8954 (org-font-lock-add-tag-faces (point-max)))))
8956 (defun org-agenda-priority-up ()
8957 "Increase the priority of line at point, also in Org-mode file."
8958 (interactive)
8959 (org-agenda-priority 'up))
8961 (defun org-agenda-priority-down ()
8962 "Decrease the priority of line at point, also in Org-mode file."
8963 (interactive)
8964 (org-agenda-priority 'down))
8966 (defun org-agenda-priority (&optional force-direction)
8967 "Set the priority of line at point, also in Org-mode file.
8968 This changes the line at point, all other lines in the agenda referring to
8969 the same tree node, and the headline of the tree node in the Org-mode file.
8970 Called with a universal prefix arg, show the priority instead of setting it."
8971 (interactive "P")
8972 (if (equal force-direction '(4))
8973 (org-show-priority)
8974 (unless org-enable-priority-commands
8975 (error "Priority commands are disabled"))
8976 (org-agenda-check-no-diary)
8977 (let* ((col (current-column))
8978 (marker (or (org-get-at-bol 'org-marker)
8979 (org-agenda-error)))
8980 (hdmarker (org-get-at-bol 'org-hd-marker))
8981 (buffer (marker-buffer hdmarker))
8982 (pos (marker-position hdmarker))
8983 (inhibit-read-only t)
8984 newhead)
8985 (org-with-remote-undo buffer
8986 (with-current-buffer buffer
8987 (widen)
8988 (goto-char pos)
8989 (org-show-context 'agenda)
8990 (save-excursion
8991 (and (outline-next-heading)
8992 (org-flag-heading nil))) ; show the next heading
8993 (funcall 'org-priority force-direction)
8994 (end-of-line 1)
8995 (setq newhead (org-get-heading)))
8996 (org-agenda-change-all-lines newhead hdmarker)
8997 (org-move-to-column col)))))
8999 ;; FIXME: should fix the tags property of the agenda line.
9000 (defun org-agenda-set-tags (&optional tag onoff)
9001 "Set tags for the current headline."
9002 (interactive)
9003 (org-agenda-check-no-diary)
9004 (if (and (org-region-active-p) (org-called-interactively-p 'any))
9005 (call-interactively 'org-change-tag-in-region)
9006 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9007 (org-agenda-error)))
9008 (buffer (marker-buffer hdmarker))
9009 (pos (marker-position hdmarker))
9010 (inhibit-read-only t)
9011 newhead)
9012 (org-with-remote-undo buffer
9013 (with-current-buffer buffer
9014 (widen)
9015 (goto-char pos)
9016 (save-excursion
9017 (org-show-context 'agenda))
9018 (save-excursion
9019 (and (outline-next-heading)
9020 (org-flag-heading nil))) ; show the next heading
9021 (goto-char pos)
9022 (if tag
9023 (org-toggle-tag tag onoff)
9024 (call-interactively 'org-set-tags))
9025 (end-of-line 1)
9026 (setq newhead (org-get-heading)))
9027 (org-agenda-change-all-lines newhead hdmarker)
9028 (beginning-of-line 1)))))
9030 (defun org-agenda-set-property ()
9031 "Set a property for the current headline."
9032 (interactive)
9033 (org-agenda-check-no-diary)
9034 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9035 (org-agenda-error)))
9036 (buffer (marker-buffer hdmarker))
9037 (pos (marker-position hdmarker))
9038 (inhibit-read-only t)
9039 newhead)
9040 (org-with-remote-undo buffer
9041 (with-current-buffer buffer
9042 (widen)
9043 (goto-char pos)
9044 (save-excursion
9045 (org-show-context 'agenda))
9046 (save-excursion
9047 (and (outline-next-heading)
9048 (org-flag-heading nil))) ; show the next heading
9049 (goto-char pos)
9050 (call-interactively 'org-set-property)))))
9052 (defun org-agenda-set-effort ()
9053 "Set the effort property for the current headline."
9054 (interactive)
9055 (org-agenda-check-no-diary)
9056 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9057 (org-agenda-error)))
9058 (buffer (marker-buffer hdmarker))
9059 (pos (marker-position hdmarker))
9060 (inhibit-read-only t)
9061 newhead)
9062 (org-with-remote-undo buffer
9063 (with-current-buffer buffer
9064 (widen)
9065 (goto-char pos)
9066 (save-excursion
9067 (org-show-context 'agenda))
9068 (save-excursion
9069 (and (outline-next-heading)
9070 (org-flag-heading nil))) ; show the next heading
9071 (goto-char pos)
9072 (call-interactively 'org-set-effort)
9073 (end-of-line 1)
9074 (setq newhead (org-get-heading)))
9075 (org-agenda-change-all-lines newhead hdmarker))))
9077 (defun org-agenda-toggle-archive-tag ()
9078 "Toggle the archive tag for the current entry."
9079 (interactive)
9080 (org-agenda-check-no-diary)
9081 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9082 (org-agenda-error)))
9083 (buffer (marker-buffer hdmarker))
9084 (pos (marker-position hdmarker))
9085 (inhibit-read-only t)
9086 newhead)
9087 (org-with-remote-undo buffer
9088 (with-current-buffer buffer
9089 (widen)
9090 (goto-char pos)
9091 (org-show-context 'agenda)
9092 (save-excursion
9093 (and (outline-next-heading)
9094 (org-flag-heading nil))) ; show the next heading
9095 (call-interactively 'org-toggle-archive-tag)
9096 (end-of-line 1)
9097 (setq newhead (org-get-heading)))
9098 (org-agenda-change-all-lines newhead hdmarker)
9099 (beginning-of-line 1))))
9101 (defun org-agenda-do-date-later (arg)
9102 (interactive "P")
9103 (cond
9104 ((or (equal arg '(16))
9105 (memq last-command
9106 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9107 (setq this-command 'org-agenda-date-later-minutes)
9108 (org-agenda-date-later-minutes 1))
9109 ((or (equal arg '(4))
9110 (memq last-command
9111 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9112 (setq this-command 'org-agenda-date-later-hours)
9113 (org-agenda-date-later-hours 1))
9115 (org-agenda-date-later (prefix-numeric-value arg)))))
9117 (defun org-agenda-do-date-earlier (arg)
9118 (interactive "P")
9119 (cond
9120 ((or (equal arg '(16))
9121 (memq last-command
9122 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9123 (setq this-command 'org-agenda-date-earlier-minutes)
9124 (org-agenda-date-earlier-minutes 1))
9125 ((or (equal arg '(4))
9126 (memq last-command
9127 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9128 (setq this-command 'org-agenda-date-earlier-hours)
9129 (org-agenda-date-earlier-hours 1))
9131 (org-agenda-date-earlier (prefix-numeric-value arg)))))
9133 (defun org-agenda-date-later (arg &optional what)
9134 "Change the date of this item to ARG day(s) later."
9135 (interactive "p")
9136 (org-agenda-check-type t 'agenda 'timeline)
9137 (org-agenda-check-no-diary)
9138 (let* ((marker (or (org-get-at-bol 'org-marker)
9139 (org-agenda-error)))
9140 (buffer (marker-buffer marker))
9141 (pos (marker-position marker))
9142 cdate today)
9143 (org-with-remote-undo buffer
9144 (with-current-buffer buffer
9145 (widen)
9146 (goto-char pos)
9147 (if (not (org-at-timestamp-p))
9148 (error "Cannot find time stamp"))
9149 (when (and org-agenda-move-date-from-past-immediately-to-today
9150 (equal arg 1)
9151 (or (not what) (eq what 'day))
9152 (not (save-match-data (org-at-date-range-p))))
9153 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9154 cdate (calendar-absolute-from-gregorian
9155 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9156 today (org-today))
9157 (if (> today cdate)
9158 ;; immediately shift to today
9159 (setq arg (- today cdate))))
9160 (org-timestamp-change arg (or what 'day))
9161 (when (and (org-at-date-range-p)
9162 (re-search-backward org-tr-regexp-both (point-at-bol)))
9163 (let ((end org-last-changed-timestamp))
9164 (org-timestamp-change arg (or what 'day))
9165 (setq org-last-changed-timestamp
9166 (concat org-last-changed-timestamp "--" end)))))
9167 (org-agenda-show-new-time marker org-last-changed-timestamp))
9168 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9170 (defun org-agenda-date-earlier (arg &optional what)
9171 "Change the date of this item to ARG day(s) earlier."
9172 (interactive "p")
9173 (org-agenda-date-later (- arg) what))
9175 (defun org-agenda-date-later-minutes (arg)
9176 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9177 (interactive "p")
9178 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9179 (org-agenda-date-later arg 'minute))
9181 (defun org-agenda-date-earlier-minutes (arg)
9182 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9183 (interactive "p")
9184 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9185 (org-agenda-date-earlier arg 'minute))
9187 (defun org-agenda-date-later-hours (arg)
9188 "Change the time of this item, in hour steps."
9189 (interactive "p")
9190 (org-agenda-date-later arg 'hour))
9192 (defun org-agenda-date-earlier-hours (arg)
9193 "Change the time of this item, in hour steps."
9194 (interactive "p")
9195 (org-agenda-date-earlier arg 'hour))
9197 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9198 "Show new date stamp via text properties."
9199 ;; We use text properties to make this undoable
9200 (let ((inhibit-read-only t))
9201 (setq stamp (concat prefix " => " stamp " "))
9202 (save-excursion
9203 (goto-char (point-max))
9204 (while (not (bobp))
9205 (when (equal marker (org-get-at-bol 'org-marker))
9206 (remove-text-properties (point-at-bol) (point-at-eol) '(display))
9207 (org-move-to-column (- (window-width) (length stamp)) t)
9208 (if (featurep 'xemacs)
9209 ;; Use `duplicable' property to trigger undo recording
9210 (let ((ex (make-extent nil nil))
9211 (gl (make-glyph stamp)))
9212 (set-glyph-face gl 'secondary-selection)
9213 (set-extent-properties
9214 ex (list 'invisible t 'end-glyph gl 'duplicable t))
9215 (insert-extent ex (1- (point)) (point-at-eol)))
9216 (add-text-properties
9217 (1- (point)) (point-at-eol)
9218 (list 'display (org-add-props stamp nil
9219 'face '(secondary-selection default)))))
9220 (beginning-of-line 1))
9221 (beginning-of-line 0)))))
9223 (defun org-agenda-date-prompt (arg)
9224 "Change the date of this item. Date is prompted for, with default today.
9225 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9226 be used to request time specification in the time stamp."
9227 (interactive "P")
9228 (org-agenda-check-type t 'agenda 'timeline)
9229 (org-agenda-check-no-diary)
9230 (let* ((marker (or (org-get-at-bol 'org-marker)
9231 (org-agenda-error)))
9232 (buffer (marker-buffer marker))
9233 (pos (marker-position marker)))
9234 (org-with-remote-undo buffer
9235 (with-current-buffer buffer
9236 (widen)
9237 (goto-char pos)
9238 (if (not (org-at-timestamp-p t))
9239 (error "Cannot find time stamp"))
9240 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
9241 (org-agenda-show-new-time marker org-last-changed-timestamp))
9242 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9244 (defun org-agenda-schedule (arg &optional time)
9245 "Schedule the item at point.
9246 ARG is passed through to `org-schedule'."
9247 (interactive "P")
9248 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9249 (org-agenda-check-no-diary)
9250 (let* ((marker (or (org-get-at-bol 'org-marker)
9251 (org-agenda-error)))
9252 (type (marker-insertion-type marker))
9253 (buffer (marker-buffer marker))
9254 (pos (marker-position marker))
9255 (org-insert-labeled-timestamps-at-point nil)
9257 (set-marker-insertion-type marker t)
9258 (org-with-remote-undo buffer
9259 (with-current-buffer buffer
9260 (widen)
9261 (goto-char pos)
9262 (setq ts (org-schedule arg time)))
9263 (org-agenda-show-new-time marker ts " S"))
9264 (message "%s" ts)))
9266 (defun org-agenda-deadline (arg &optional time)
9267 "Schedule the item at point.
9268 ARG is passed through to `org-deadline'."
9269 (interactive "P")
9270 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9271 (org-agenda-check-no-diary)
9272 (let* ((marker (or (org-get-at-bol 'org-marker)
9273 (org-agenda-error)))
9274 (buffer (marker-buffer marker))
9275 (pos (marker-position marker))
9276 (org-insert-labeled-timestamps-at-point nil)
9278 (org-with-remote-undo buffer
9279 (with-current-buffer buffer
9280 (widen)
9281 (goto-char pos)
9282 (setq ts (org-deadline arg time)))
9283 (org-agenda-show-new-time marker ts " D"))
9284 (message "%s" ts)))
9286 (defun org-agenda-clock-in (&optional arg)
9287 "Start the clock on the currently selected item."
9288 (interactive "P")
9289 (org-agenda-check-no-diary)
9290 (if (equal arg '(4))
9291 (org-clock-in arg)
9292 (let* ((marker (or (org-get-at-bol 'org-marker)
9293 (org-agenda-error)))
9294 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9295 (pos (marker-position marker))
9296 (col (current-column))
9297 newhead)
9298 (org-with-remote-undo (marker-buffer marker)
9299 (with-current-buffer (marker-buffer marker)
9300 (widen)
9301 (goto-char pos)
9302 (org-show-context 'agenda)
9303 (org-show-entry)
9304 (org-cycle-hide-drawers 'children)
9305 (org-clock-in arg)
9306 (setq newhead (org-get-heading)))
9307 (org-agenda-change-all-lines newhead hdmarker))
9308 (org-move-to-column col))))
9310 (defun org-agenda-clock-out ()
9311 "Stop the currently running clock."
9312 (interactive)
9313 (unless (marker-buffer org-clock-marker)
9314 (error "No running clock"))
9315 (let ((marker (make-marker)) (col (current-column)) newhead)
9316 (org-with-remote-undo (marker-buffer org-clock-marker)
9317 (with-current-buffer (marker-buffer org-clock-marker)
9318 (save-excursion
9319 (save-restriction
9320 (widen)
9321 (goto-char org-clock-marker)
9322 (org-back-to-heading t)
9323 (move-marker marker (point))
9324 (org-clock-out)
9325 (setq newhead (org-get-heading))))))
9326 (org-agenda-change-all-lines newhead marker)
9327 (move-marker marker nil)
9328 (org-move-to-column col)
9329 (org-agenda-unmark-clocking-task)))
9331 (defun org-agenda-clock-cancel (&optional arg)
9332 "Cancel the currently running clock."
9333 (interactive "P")
9334 (unless (marker-buffer org-clock-marker)
9335 (user-error "No running clock"))
9336 (org-with-remote-undo (marker-buffer org-clock-marker)
9337 (org-clock-cancel)))
9339 (defun org-agenda-clock-goto ()
9340 "Jump to the currently clocked in task within the agenda.
9341 If the currently clocked in task is not listed in the agenda
9342 buffer, display it in another window."
9343 (interactive)
9344 (let (pos)
9345 (mapc (lambda (o)
9346 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
9347 (setq pos (overlay-start o))))
9348 (overlays-in (point-min) (point-max)))
9349 (cond (pos (goto-char pos))
9350 ;; If the currently clocked entry is not in the agenda
9351 ;; buffer, we visit it in another window:
9352 (org-clock-current-task
9353 (org-switch-to-buffer-other-window (org-clock-goto)))
9354 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9356 (defun org-agenda-diary-entry-in-org-file ()
9357 "Make a diary entry in the file `org-agenda-diary-file'."
9358 (let (d1 d2 char (text "") dp1 dp2)
9359 (if (equal (buffer-name) "*Calendar*")
9360 (setq d1 (calendar-cursor-to-date t)
9361 d2 (car calendar-mark-ring))
9362 (setq dp1 (get-text-property (point-at-bol) 'day))
9363 (unless dp1 (user-error "No date defined in current line"))
9364 (setq d1 (calendar-gregorian-from-absolute dp1)
9365 d2 (and (ignore-errors (mark))
9366 (save-excursion
9367 (goto-char (mark))
9368 (setq dp2 (get-text-property (point-at-bol) 'day)))
9369 (calendar-gregorian-from-absolute dp2))))
9370 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9371 (setq char (read-char-exclusive))
9372 (cond
9373 ((equal char ?d)
9374 (setq text (read-string "Day entry: "))
9375 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9376 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9377 ((equal char ?a)
9378 (setq d1 (list (car d1) (nth 1 d1)
9379 (read-number (format "Reference year [%d]: " (nth 2 d1))
9380 (nth 2 d1))))
9381 (setq text (read-string "Anniversary (use %d to show years): "))
9382 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9383 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9384 ((equal char ?b)
9385 (setq text (read-string "Block entry: "))
9386 (unless (and d1 d2 (not (equal d1 d2)))
9387 (user-error "No block of days selected"))
9388 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9389 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9390 ((equal char ?j)
9391 (org-switch-to-buffer-other-window
9392 (find-file-noselect org-agenda-diary-file))
9393 (require 'org-datetree)
9394 (org-datetree-find-date-create d1)
9395 (org-reveal t))
9396 (t (user-error "Invalid selection character `%c'" char)))))
9398 (defcustom org-agenda-insert-diary-strategy 'date-tree
9399 "Where in `org-agenda-diary-file' should new entries be added?
9400 Valid values:
9402 date-tree in the date tree, as child of the date
9403 top-level as top-level entries at the end of the file."
9404 :group 'org-agenda
9405 :type '(choice
9406 (const :tag "in a date tree" date-tree)
9407 (const :tag "as top level at end of file" top-level)))
9409 (defcustom org-agenda-insert-diary-extract-time nil
9410 "Non-nil means extract any time specification from the diary entry."
9411 :group 'org-agenda
9412 :version "24.1"
9413 :type 'boolean)
9415 (defcustom org-agenda-bulk-mark-char ">"
9416 "A single-character string to be used as the bulk mark."
9417 :group 'org-agenda
9418 :version "24.1"
9419 :type 'string)
9421 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9422 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9423 If TEXT is not empty, it will become the headline of the new entry, and
9424 the resulting entry will not be shown. When TEXT is empty, switch to
9425 `org-agenda-diary-file' and let the user finish the entry there."
9426 (let ((cw (current-window-configuration)))
9427 (org-switch-to-buffer-other-window
9428 (find-file-noselect org-agenda-diary-file))
9429 (widen)
9430 (goto-char (point-min))
9431 (cond
9432 ((eq type 'anniversary)
9433 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
9434 (progn
9435 (or (org-at-heading-p t)
9436 (progn
9437 (outline-next-heading)
9438 (insert "* Anniversaries\n\n")
9439 (beginning-of-line -1)))))
9440 (outline-next-heading)
9441 (org-back-over-empty-lines)
9442 (backward-char 1)
9443 (insert "\n")
9444 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9445 (nth 2 d1) (car d1) (nth 1 d1) text)))
9446 ((eq type 'day)
9447 (let ((org-prefix-has-time t)
9448 (org-agenda-time-leading-zero t)
9449 fmt time time2)
9450 (if org-agenda-insert-diary-extract-time
9451 ;; Use org-agenda-format-item to parse text for a time-range and
9452 ;; remove it. FIXME: This is a hack, we should refactor
9453 ;; that function to make time extraction available separately
9454 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9455 time (get-text-property 0 'time fmt)
9456 time2 (if (> (length time) 0)
9457 ;; split-string removes trailing ...... if
9458 ;; no end time given. First space
9459 ;; separates time from date.
9460 (concat " " (car (split-string time "\\.")))
9461 nil)
9462 text (get-text-property 0 'txt fmt)))
9463 (if (eq org-agenda-insert-diary-strategy 'top-level)
9464 (org-agenda-insert-diary-as-top-level text)
9465 (require 'org-datetree)
9466 (org-datetree-find-date-create d1)
9467 (org-agenda-insert-diary-make-new-entry text))
9468 (org-insert-time-stamp (org-time-from-absolute
9469 (calendar-absolute-from-gregorian d1))
9470 nil nil nil nil time2))
9471 (end-of-line 0))
9472 ((eq type 'block)
9473 (if (> (calendar-absolute-from-gregorian d1)
9474 (calendar-absolute-from-gregorian d2))
9475 (setq d1 (prog1 d2 (setq d2 d1))))
9476 (if (eq org-agenda-insert-diary-strategy 'top-level)
9477 (org-agenda-insert-diary-as-top-level text)
9478 (require 'org-datetree)
9479 (org-datetree-find-date-create d1)
9480 (org-agenda-insert-diary-make-new-entry text))
9481 (org-insert-time-stamp (org-time-from-absolute
9482 (calendar-absolute-from-gregorian d1)))
9483 (insert "--")
9484 (org-insert-time-stamp (org-time-from-absolute
9485 (calendar-absolute-from-gregorian d2)))
9486 (end-of-line 0)))
9487 (if (string-match "\\S-" text)
9488 (progn
9489 (set-window-configuration cw)
9490 (message "%s entry added to %s"
9491 (capitalize (symbol-name type))
9492 (abbreviate-file-name org-agenda-diary-file)))
9493 (org-reveal t)
9494 (message "Please finish entry here"))))
9496 (defun org-agenda-insert-diary-as-top-level (text)
9497 "Make new entry as a top-level entry at the end of the file.
9498 Add TEXT as headline, and position the cursor in the second line so that
9499 a timestamp can be added there."
9500 (widen)
9501 (goto-char (point-max))
9502 (or (bolp) (insert "\n"))
9503 (insert "* " text "\n")
9504 (if org-adapt-indentation (org-indent-to-column 2)))
9506 (defun org-agenda-insert-diary-make-new-entry (text)
9507 "Make new entry as last child of current entry.
9508 Add TEXT as headline, and position the cursor in the second line so that
9509 a timestamp can be added there."
9510 (let ((org-show-following-heading t)
9511 (org-show-siblings t)
9512 (org-show-hierarchy-above t)
9513 (org-show-entry-below t)
9514 col)
9515 (outline-next-heading)
9516 (org-back-over-empty-lines)
9517 (or (looking-at "[ \t]*$")
9518 (progn (insert "\n") (backward-char 1)))
9519 (org-insert-heading nil t)
9520 (org-do-demote)
9521 (setq col (current-column))
9522 (insert text "\n")
9523 (if org-adapt-indentation (org-indent-to-column col))
9524 (let ((org-show-following-heading t)
9525 (org-show-siblings t)
9526 (org-show-hierarchy-above t)
9527 (org-show-entry-below t))
9528 (org-show-context))))
9530 (defun org-agenda-diary-entry ()
9531 "Make a diary entry, like the `i' command from the calendar.
9532 All the standard commands work: block, weekly etc.
9533 When `org-agenda-diary-file' points to a file,
9534 `org-agenda-diary-entry-in-org-file' is called instead to create
9535 entries in that Org-mode file."
9536 (interactive)
9537 (if (not (eq org-agenda-diary-file 'diary-file))
9538 (org-agenda-diary-entry-in-org-file)
9539 (require 'diary-lib)
9540 (let* ((char (progn
9541 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
9542 (read-char-exclusive)))
9543 (cmd (cdr (assoc char
9544 '((?d . insert-diary-entry)
9545 (?w . insert-weekly-diary-entry)
9546 (?m . insert-monthly-diary-entry)
9547 (?y . insert-yearly-diary-entry)
9548 (?a . insert-anniversary-diary-entry)
9549 (?b . insert-block-diary-entry)
9550 (?c . insert-cyclic-diary-entry)))))
9551 (oldf (symbol-function 'calendar-cursor-to-date))
9552 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9553 (point (point))
9554 (mark (or (mark t) (point))))
9555 (unless cmd
9556 (user-error "No command associated with <%c>" char))
9557 (unless (and (get-text-property point 'day)
9558 (or (not (equal ?b char))
9559 (get-text-property mark 'day)))
9560 (user-error "Don't know which date to use for diary entry"))
9561 ;; We implement this by hacking the `calendar-cursor-to-date' function
9562 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9563 (let ((calendar-mark-ring
9564 (list (calendar-gregorian-from-absolute
9565 (or (get-text-property mark 'day)
9566 (get-text-property point 'day))))))
9567 (unwind-protect
9568 (progn
9569 (fset 'calendar-cursor-to-date
9570 (lambda (&optional error dummy)
9571 (calendar-gregorian-from-absolute
9572 (get-text-property point 'day))))
9573 (call-interactively cmd))
9574 (fset 'calendar-cursor-to-date oldf))))))
9576 (defun org-agenda-execute-calendar-command (cmd)
9577 "Execute a calendar command from the agenda with date from cursor."
9578 (org-agenda-check-type t 'agenda 'timeline)
9579 (require 'diary-lib)
9580 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9581 (user-error "Don't know which date to use for the calendar command"))
9582 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9583 (point (point))
9584 (date (calendar-gregorian-from-absolute
9585 (get-text-property point 'day)))
9586 ;; the following 2 vars are needed in the calendar
9587 (displayed-month (car date))
9588 (displayed-year (nth 2 date)))
9589 (unwind-protect
9590 (progn
9591 (fset 'calendar-cursor-to-date
9592 (lambda (&optional error dummy)
9593 (calendar-gregorian-from-absolute
9594 (get-text-property point 'day))))
9595 (call-interactively cmd))
9596 (fset 'calendar-cursor-to-date oldf))))
9598 (defun org-agenda-phases-of-moon ()
9599 "Display the phases of the moon for the 3 months around the cursor date."
9600 (interactive)
9601 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
9603 (defun org-agenda-holidays ()
9604 "Display the holidays for the 3 months around the cursor date."
9605 (interactive)
9606 (org-agenda-execute-calendar-command 'list-calendar-holidays))
9608 (defvar calendar-longitude) ; defined in calendar.el
9609 (defvar calendar-latitude) ; defined in calendar.el
9610 (defvar calendar-location-name) ; defined in calendar.el
9612 (defun org-agenda-sunrise-sunset (arg)
9613 "Display sunrise and sunset for the cursor date.
9614 Latitude and longitude can be specified with the variables
9615 `calendar-latitude' and `calendar-longitude'. When called with prefix
9616 argument, latitude and longitude will be prompted for."
9617 (interactive "P")
9618 (require 'solar)
9619 (let ((calendar-longitude (if arg nil calendar-longitude))
9620 (calendar-latitude (if arg nil calendar-latitude))
9621 (calendar-location-name
9622 (if arg "the given coordinates" calendar-location-name)))
9623 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9625 (defun org-agenda-goto-calendar ()
9626 "Open the Emacs calendar with the date at the cursor."
9627 (interactive)
9628 (org-agenda-check-type t 'agenda 'timeline)
9629 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9630 (user-error "Don't know which date to open in calendar")))
9631 (date (calendar-gregorian-from-absolute day))
9632 (calendar-move-hook nil)
9633 (calendar-view-holidays-initially-flag nil)
9634 (calendar-view-diary-initially-flag nil))
9635 (calendar)
9636 (calendar-goto-date date)))
9638 ;;;###autoload
9639 (defun org-calendar-goto-agenda ()
9640 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
9641 This is a command that has to be installed in `calendar-mode-map'."
9642 (interactive)
9643 (org-agenda-list nil (calendar-absolute-from-gregorian
9644 (calendar-cursor-to-date))
9645 nil))
9647 (defun org-agenda-convert-date ()
9648 (interactive)
9649 (org-agenda-check-type t 'agenda 'timeline)
9650 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9651 date s)
9652 (unless day
9653 (user-error "Don't know which date to convert"))
9654 (setq date (calendar-gregorian-from-absolute day))
9655 (setq s (concat
9656 "Gregorian: " (calendar-date-string date) "\n"
9657 "ISO: " (calendar-iso-date-string date) "\n"
9658 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9659 "Julian: " (calendar-julian-date-string date) "\n"
9660 "Astron. JD: " (calendar-astro-date-string date)
9661 " (Julian date number at noon UTC)\n"
9662 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9663 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9664 "French: " (calendar-french-date-string date) "\n"
9665 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
9666 "Mayan: " (calendar-mayan-date-string date) "\n"
9667 "Coptic: " (calendar-coptic-date-string date) "\n"
9668 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9669 "Persian: " (calendar-persian-date-string date) "\n"
9670 "Chinese: " (calendar-chinese-date-string date) "\n"))
9671 (with-output-to-temp-buffer "*Dates*"
9672 (princ s))
9673 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9675 ;;; Bulk commands
9677 (defun org-agenda-bulk-marked-p ()
9678 (eq (get-char-property (point-at-bol) 'type)
9679 'org-marked-entry-overlay))
9681 (defun org-agenda-bulk-mark (&optional arg)
9682 "Mark the entry at point for future bulk action."
9683 (interactive "p")
9684 (dotimes (i (or arg 1))
9685 (unless (org-get-at-bol 'org-agenda-diary-link)
9686 (let* ((m (org-get-at-bol 'org-hd-marker))
9688 (unless (org-agenda-bulk-marked-p)
9689 (unless m (user-error "Nothing to mark at point"))
9690 (push m org-agenda-bulk-marked-entries)
9691 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9692 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9693 (org-get-todo-face "TODO")
9694 'evaporate)
9695 (overlay-put ov 'type 'org-marked-entry-overlay))
9696 (end-of-line 1)
9697 (or (ignore-errors
9698 (goto-char (next-single-property-change (point) 'txt)))
9699 (beginning-of-line 2))
9700 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9701 (beginning-of-line 2))
9702 (message "%d entries marked for bulk action"
9703 (length org-agenda-bulk-marked-entries))))))
9705 (defun org-agenda-bulk-mark-all ()
9706 "Mark all entries for future agenda bulk action."
9707 (interactive)
9708 (org-agenda-bulk-mark-regexp "."))
9710 (defun org-agenda-bulk-mark-regexp (regexp)
9711 "Mark entries matching REGEXP for future agenda bulk action."
9712 (interactive "sMark entries matching regexp: ")
9713 (let ((entries-marked 0) txt-at-point)
9714 (save-excursion
9715 (goto-char (point-min))
9716 (goto-char (next-single-property-change (point) 'txt))
9717 (while (and (re-search-forward regexp nil t)
9718 (setq txt-at-point (get-text-property (point) 'txt)))
9719 (when (string-match regexp txt-at-point)
9720 (setq entries-marked (1+ entries-marked))
9721 (call-interactively 'org-agenda-bulk-mark))))
9722 (if (not entries-marked)
9723 (message "No entry matching this regexp."))))
9725 (defun org-agenda-bulk-unmark (&optional arg)
9726 "Unmark the entry at point for future bulk action."
9727 (interactive "P")
9728 (if arg
9729 (org-agenda-bulk-unmark-all)
9730 (cond ((org-agenda-bulk-marked-p)
9731 (org-agenda-bulk-remove-overlays
9732 (point-at-bol) (+ 2 (point-at-bol)))
9733 (setq org-agenda-bulk-marked-entries
9734 (delete (org-get-at-bol 'org-hd-marker)
9735 org-agenda-bulk-marked-entries))
9736 (end-of-line 1)
9737 (or (ignore-errors
9738 (goto-char (next-single-property-change (point) 'txt)))
9739 (beginning-of-line 2))
9740 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9741 (beginning-of-line 2))
9742 (message "%d entries left marked for bulk action"
9743 (length org-agenda-bulk-marked-entries)))
9744 (t (message "No entry to unmark here")))))
9746 (defun org-agenda-bulk-toggle-all ()
9747 "Toggle all marks for bulk action."
9748 (interactive)
9749 (save-excursion
9750 (goto-char (point-min))
9751 (while (ignore-errors
9752 (goto-char (next-single-property-change (point) 'txt)))
9753 (org-agenda-bulk-toggle))))
9755 (defun org-agenda-bulk-toggle ()
9756 "Toggle the mark at point for bulk action."
9757 (interactive)
9758 (if (org-agenda-bulk-marked-p)
9759 (org-agenda-bulk-unmark)
9760 (org-agenda-bulk-mark)))
9762 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9763 "Remove the mark overlays between BEG and END in the agenda buffer.
9764 BEG and END default to the buffer limits.
9766 This only removes the overlays, it does not remove the markers
9767 from the list in `org-agenda-bulk-marked-entries'."
9768 (interactive)
9769 (mapc (lambda (ov)
9770 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9771 (delete-overlay ov)))
9772 (overlays-in (or beg (point-min)) (or end (point-max)))))
9774 (defun org-agenda-bulk-unmark-all ()
9775 "Remove all marks in the agenda buffer.
9776 This will remove the markers and the overlays."
9777 (interactive)
9778 (if (null org-agenda-bulk-marked-entries)
9779 (message "No entry to unmark")
9780 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
9781 (setq org-agenda-bulk-marked-entries nil)
9782 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9784 (defcustom org-agenda-persistent-marks nil
9785 "Non-nil means marked items will stay marked after a bulk action.
9786 You can toggle this interactively by typing `p' when prompted for a
9787 bulk action."
9788 :group 'org-agenda
9789 :version "24.1"
9790 :type 'boolean)
9792 (defun org-agenda-bulk-action (&optional arg)
9793 "Execute an remote-editing action on all marked entries.
9794 The prefix arg is passed through to the command if possible."
9795 (interactive "P")
9796 ;; Make sure we have markers, and only valid ones
9797 (unless org-agenda-bulk-marked-entries (user-error "No entries are marked"))
9798 (mapc
9799 (lambda (m)
9800 (unless (and (markerp m)
9801 (marker-buffer m)
9802 (buffer-live-p (marker-buffer m))
9803 (marker-position m))
9804 (user-error "Marker %s for bulk command is invalid" m)))
9805 org-agenda-bulk-marked-entries)
9807 ;; Prompt for the bulk command
9808 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
9809 (message (concat msg "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9810 "[S]catter [f]unction "
9811 (when org-agenda-bulk-custom-functions
9812 (concat " Custom: ["
9813 (mapconcat (lambda(f) (char-to-string (car f)))
9814 org-agenda-bulk-custom-functions "")
9815 "]"))))
9816 (catch 'exit
9817 (let* ((action (read-char-exclusive))
9818 (org-log-refile (if org-log-refile 'time nil))
9819 (entries (reverse org-agenda-bulk-marked-entries))
9820 (org-overriding-default-time
9821 (if (get-text-property (point) 'org-agenda-date-header)
9822 (org-get-cursor-date)))
9823 redo-at-end
9824 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
9825 (cond
9826 ((equal action ?p)
9827 (let ((org-agenda-persistent-marks
9828 (not org-agenda-persistent-marks)))
9829 (org-agenda-bulk-action)
9830 (throw 'exit nil)))
9832 ((equal action ?$)
9833 (setq cmd '(org-agenda-archive)))
9835 ((equal action ?A)
9836 (setq cmd '(org-agenda-archive-to-archive-sibling)))
9838 ((member action '(?r ?w))
9839 (setq rfloc (org-refile-get-location
9840 "Refile to"
9841 (marker-buffer (car entries))
9842 org-refile-allow-creating-parent-nodes))
9843 (if (nth 3 rfloc)
9844 (setcar (nthcdr 3 rfloc)
9845 (move-marker (make-marker) (nth 3 rfloc)
9846 (or (get-file-buffer (nth 1 rfloc))
9847 (find-buffer-visiting (nth 1 rfloc))
9848 (error "This should not happen")))))
9850 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
9851 redo-at-end t))
9853 ((equal action ?t)
9854 (setq state (org-icompleting-read
9855 "Todo state: "
9856 (with-current-buffer (marker-buffer (car entries))
9857 (mapcar 'list org-todo-keywords-1))))
9858 (setq cmd `(let ((org-inhibit-blocking t)
9859 (org-inhibit-logging 'note))
9860 (org-agenda-todo ,state))))
9862 ((memq action '(?- ?+))
9863 (setq tag (org-icompleting-read
9864 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9865 (with-current-buffer (marker-buffer (car entries))
9866 (delq nil
9867 (mapcar (lambda (x)
9868 (if (stringp (car x)) x)) org-tag-alist)))))
9869 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
9871 ((memq action '(?s ?d))
9872 (let* ((time
9873 (unless arg
9874 (org-read-date
9875 nil nil nil
9876 (if (eq action ?s) "(Re)Schedule to" "(Re)Set Deadline to")
9877 org-overriding-default-time)))
9878 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
9879 (setq cmd `(eval '(,c1 arg ,time)))))
9881 ((equal action ?S)
9882 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
9883 (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
9884 (let ((days (read-number
9885 (format "Scatter tasks across how many %sdays: "
9886 (if arg "week" "")) 7)))
9887 (setq cmd
9888 `(let ((distance (1+ (random ,days))))
9889 (if arg
9890 (let ((dist distance)
9891 (day-of-week
9892 (calendar-day-of-week
9893 (calendar-gregorian-from-absolute (org-today)))))
9894 (dotimes (i (1+ dist))
9895 (while (member day-of-week org-agenda-weekend-days)
9896 (incf distance)
9897 (incf day-of-week)
9898 (if (= day-of-week 7)
9899 (setq day-of-week 0)))
9900 (incf day-of-week)
9901 (if (= day-of-week 7)
9902 (setq day-of-week 0)))))
9903 ;; silently fail when try to replan a sexp entry
9904 (condition-case nil
9905 (let* ((date (calendar-gregorian-from-absolute
9906 (+ (org-today) distance)))
9907 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9908 (nth 2 date))))
9909 (org-agenda-schedule nil time))
9910 (error nil)))))))
9912 ((assoc action org-agenda-bulk-custom-functions)
9913 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
9914 redo-at-end t))
9916 ((equal action ?f)
9917 (setq cmd (list (intern
9918 (org-icompleting-read "Function: "
9919 obarray 'fboundp t nil nil)))))
9921 (t (user-error "Invalid bulk action")))
9923 ;; Sort the markers, to make sure that parents are handled before children
9924 (setq entries (sort entries
9925 (lambda (a b)
9926 (cond
9927 ((equal (marker-buffer a) (marker-buffer b))
9928 (< (marker-position a) (marker-position b)))
9930 (string< (buffer-name (marker-buffer a))
9931 (buffer-name (marker-buffer b))))))))
9933 ;; Now loop over all markers and apply cmd
9934 (while (setq e (pop entries))
9935 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
9936 (if (not pos)
9937 (progn (message "Skipping removed entry at %s" e)
9938 (setq cntskip (1+ cntskip)))
9939 (goto-char pos)
9940 (let (org-loop-over-headlines-in-active-region)
9941 (eval cmd))
9942 (setq cnt (1+ cnt))))
9943 (when redo-at-end (org-agenda-redo))
9944 (unless org-agenda-persistent-marks
9945 (org-agenda-bulk-unmark-all))
9946 (message "Acted on %d entries%s%s"
9948 (if (= cntskip 0)
9950 (format ", skipped %d (disappeared before their turn)"
9951 cntskip))
9952 (if (not org-agenda-persistent-marks)
9953 "" " (kept marked)"))))))
9955 (defun org-agenda-capture (&optional with-time)
9956 "Call `org-capture' with the date at point.
9957 With a `C-1' prefix, use the HH:MM value at point (if any) or the
9958 current HH:MM time."
9959 (interactive "P")
9960 (if (not (eq major-mode 'org-agenda-mode))
9961 (user-error "You cannot do this outside of agenda buffers")
9962 (let ((org-overriding-default-time
9963 (org-get-cursor-date (equal with-time 1))))
9964 (call-interactively 'org-capture))))
9966 ;;; Dragging agenda lines forward/backward
9968 (defun org-agenda-reapply-filters ()
9969 "Re-apply all agenda filters."
9970 (mapcar
9971 (lambda(f) (when (car f) (org-agenda-filter-apply (car f) (cadr f))))
9972 `((,org-agenda-tag-filter tag)
9973 (,org-agenda-category-filter category)
9974 (,org-agenda-regexp-filter regexp)
9975 (,org-agenda-effort-filter effort)
9976 (,(get 'org-agenda-tag-filter :preset-filter) tag)
9977 (,(get 'org-agenda-category-filter :preset-filter) category)
9978 (,(get 'org-agenda-effort-filter :preset-filter) effort)
9979 (,(get 'org-agenda-regexp-filter :preset-filter) regexp))))
9981 (defun org-agenda-drag-line-forward (arg &optional backward)
9982 "Drag an agenda line forward by ARG lines.
9983 When the optional argument `backward' is non-nil, move backward."
9984 (interactive "p")
9985 (let ((inhibit-read-only t) lst line)
9986 (if (or (not (get-text-property (point) 'txt))
9987 (save-excursion
9988 (dotimes (n arg)
9989 (move-beginning-of-line (if backward 0 2))
9990 (push (not (get-text-property (point) 'txt)) lst))
9991 (delq nil lst)))
9992 (message "Cannot move line forward")
9993 (let ((end (save-excursion (move-beginning-of-line 2) (point))))
9994 (move-beginning-of-line 1)
9995 (setq line (buffer-substring (point) end))
9996 (delete-region (point) end)
9997 (move-beginning-of-line (funcall (if backward '1- '1+) arg))
9998 (insert line)
9999 (org-agenda-reapply-filters)
10000 (org-agenda-mark-clocking-task)
10001 (move-beginning-of-line 0)))))
10003 (defun org-agenda-drag-line-backward (arg)
10004 "Drag an agenda line backward by ARG lines."
10005 (interactive "p")
10006 (org-agenda-drag-line-forward arg t))
10008 ;;; Flagging notes
10010 (defun org-agenda-show-the-flagging-note ()
10011 "Display the flagging note in the other window.
10012 When called a second time in direct sequence, offer to remove the FLAGGING
10013 tag and (if present) the flagging note."
10014 (interactive)
10015 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
10016 (win (selected-window))
10017 note heading newhead)
10018 (unless hdmarker
10019 (user-error "No linked entry at point"))
10020 (if (and (eq this-command last-command)
10021 (y-or-n-p "Unflag and remove any flagging note? "))
10022 (progn
10023 (org-agenda-remove-flag hdmarker)
10024 (let ((win (get-buffer-window "*Flagging Note*")))
10025 (and win (delete-window win)))
10026 (message "Entry unflagged"))
10027 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
10028 (unless note
10029 (user-error "No flagging note"))
10030 (org-kill-new note)
10031 (org-switch-to-buffer-other-window "*Flagging Note*")
10032 (erase-buffer)
10033 (insert note)
10034 (goto-char (point-min))
10035 (while (re-search-forward "\\\\n" nil t)
10036 (replace-match "\n" t t))
10037 (goto-char (point-min))
10038 (select-window win)
10039 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
10041 (defun org-agenda-remove-flag (marker)
10042 "Remove the FLAGGED tag and any flagging note in the entry."
10043 (let (newhead)
10044 (org-with-point-at marker
10045 (org-toggle-tag "FLAGGED" 'off)
10046 (org-entry-delete nil "THEFLAGGINGNOTE")
10047 (setq newhead (org-get-heading)))
10048 (org-agenda-change-all-lines newhead marker)
10049 (message "Entry unflagged")))
10051 (defun org-agenda-get-any-marker (&optional pos)
10052 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
10053 (get-text-property (or pos (point-at-bol)) 'org-marker)))
10055 ;;; Appointment reminders
10057 (defvar appt-time-msg-list) ; defined in appt.el
10059 ;;;###autoload
10060 (defun org-agenda-to-appt (&optional refresh filter &rest args)
10061 "Activate appointments found in `org-agenda-files'.
10062 With a \\[universal-argument] prefix, refresh the list of
10063 appointments.
10065 If FILTER is t, interactively prompt the user for a regular
10066 expression, and filter out entries that don't match it.
10068 If FILTER is a string, use this string as a regular expression
10069 for filtering entries out.
10071 If FILTER is a function, filter out entries against which
10072 calling the function returns nil. This function takes one
10073 argument: an entry from `org-agenda-get-day-entries'.
10075 FILTER can also be an alist with the car of each cell being
10076 either 'headline or 'category. For example:
10078 '((headline \"IMPORTANT\")
10079 (category \"Work\"))
10081 will only add headlines containing IMPORTANT or headlines
10082 belonging to the \"Work\" category.
10084 ARGS are symbols indicating what kind of entries to consider.
10085 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
10086 \(i.e., deadlines and scheduled items with a hh:mm specification)
10087 and :timestamp entries. See the docstring of `org-diary' for
10088 details and examples.
10090 If an entry has a APPT_WARNTIME property, its value will be used
10091 to override `appt-message-warning-time'."
10092 (interactive "P")
10093 (if refresh (setq appt-time-msg-list nil))
10094 (if (eq filter t)
10095 (setq filter (read-from-minibuffer "Regexp filter: ")))
10096 (let* ((cnt 0) ; count added events
10097 (scope (or args '(:deadline* :scheduled* :timestamp)))
10098 (org-agenda-new-buffers nil)
10099 (org-deadline-warning-days 0)
10100 ;; Do not use `org-today' here because appt only takes
10101 ;; time and without date as argument, so it may pass wrong
10102 ;; information otherwise
10103 (today (org-date-to-gregorian
10104 (time-to-days (current-time))))
10105 (org-agenda-restrict nil)
10106 (files (org-agenda-files 'unrestricted)) entries file
10107 (org-agenda-buffer nil))
10108 ;; Get all entries which may contain an appt
10109 (org-agenda-prepare-buffers files)
10110 (while (setq file (pop files))
10111 (setq entries
10112 (delq nil
10113 (append entries
10114 (apply 'org-agenda-get-day-entries
10115 file today scope)))))
10116 ;; Map thru entries and find if we should filter them out
10117 (mapc
10118 (lambda(x)
10119 (let* ((evt (org-trim
10120 (replace-regexp-in-string
10121 org-bracket-link-regexp "\\3"
10122 (or (get-text-property 1 'txt x) ""))))
10123 (cat (get-text-property (1- (length x)) 'org-category x))
10124 (tod (get-text-property 1 'time-of-day x))
10125 (ok (or (null filter)
10126 (and (stringp filter) (string-match filter evt))
10127 (and (functionp filter) (funcall filter x))
10128 (and (listp filter)
10129 (let ((cat-filter (cadr (assoc 'category filter)))
10130 (evt-filter (cadr (assoc 'headline filter))))
10131 (or (and (stringp cat-filter)
10132 (string-match cat-filter cat))
10133 (and (stringp evt-filter)
10134 (string-match evt-filter evt)))))))
10135 (wrn (get-text-property 1 'warntime x)))
10136 ;; FIXME: Shall we remove text-properties for the appt text?
10137 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
10138 (when (and ok tod)
10139 (setq tod (concat "00" (number-to-string tod))
10140 tod (when (string-match
10141 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
10142 (concat (match-string 1 tod) ":"
10143 (match-string 2 tod))))
10144 (if (version< emacs-version "23.3")
10145 (appt-add tod evt)
10146 (appt-add tod evt wrn))
10147 (setq cnt (1+ cnt))))) entries)
10148 (org-release-buffers org-agenda-new-buffers)
10149 (if (eq cnt 0)
10150 (message "No event to add")
10151 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
10153 (defun org-agenda-todayp (date)
10154 "Does DATE mean today, when considering `org-extend-today-until'?"
10155 (let ((today (org-today))
10156 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
10157 date)))
10158 (eq date today)))
10160 (defun org-agenda-todo-yesterday (&optional arg)
10161 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
10162 (interactive "P")
10163 (let* ((hour (third (decode-time
10164 (org-current-time))))
10165 (org-extend-today-until (1+ hour)))
10166 (org-agenda-todo arg)))
10168 (provide 'org-agenda)
10170 ;;; org-agenda.el ends here