Use ‘eq’ instead of ‘equal’ for symbols
[org-mode.git] / lisp / org-agenda.el
blob82d62f6e07b9de7629531f60069c52f675df1391
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004-2016 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 'cl-lib)
49 (require 'org)
50 (require 'org-macs)
52 (declare-function diary-add-to-list "diary-lib"
53 (date string specifier &optional marker globcolor literal))
54 (declare-function calendar-iso-to-absolute "cal-iso" (date))
55 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
56 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
57 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
58 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
59 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
60 (declare-function calendar-french-date-string "cal-french" (&optional date))
61 (declare-function calendar-goto-date "cal-move" (date))
62 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
63 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
64 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
65 (declare-function calendar-iso-from-absolute "cal-iso" (date))
66 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
67 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
68 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
69 (declare-function calendar-check-holidays "holidays" (date))
71 (declare-function org-columns-remove-overlays "org-colview" ())
72 (declare-function org-datetree-find-date-create "org-datetree"
73 (date &optional keep-restriction))
74 (declare-function org-columns-quit "org-colview" ())
75 (declare-function diary-date-display-form "diary-lib" (&optional type))
76 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
77 (declare-function org-habit-insert-consistency-graphs
78 "org-habit" (&optional line))
79 (declare-function org-is-habit-p "org-habit" (&optional pom))
80 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
81 (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
82 (declare-function org-agenda-columns "org-colview" ())
83 (declare-function org-add-archive-files "org-archive" (files))
84 (declare-function org-capture "org-capture" (&optional goto keys))
86 (defvar calendar-mode-map)
87 (defvar org-clock-current-task)
88 (defvar org-current-tag-alist)
89 (defvar org-mobile-force-id-on-agenda-items)
90 (defvar org-habit-show-habits)
91 (defvar org-habit-show-habits-only-for-today)
92 (defvar org-habit-show-all-today)
94 ;; Defined somewhere in this file, but used before definition.
95 (defvar org-agenda-buffer-name "*Org Agenda*")
96 (defvar org-agenda-overriding-header nil)
97 (defvar org-agenda-title-append nil)
98 (with-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
99 (with-no-warnings (defvar date)) ;; unprefixed, from calendar.el
100 (defvar original-date) ; dynamically scoped, calendar.el does scope this
102 (defvar org-agenda-undo-list nil
103 "List of undoable operations in the agenda since last refresh.")
104 (defvar org-agenda-pending-undo-list nil
105 "In a series of undo commands, this is the list of remaining undo items.")
107 (defcustom org-agenda-confirm-kill 1
108 "When set, remote killing from the agenda buffer needs confirmation.
109 When t, a confirmation is always needed. When a number N, confirmation is
110 only needed when the text to be killed contains more than N non-white lines."
111 :group 'org-agenda
112 :type '(choice
113 (const :tag "Never" nil)
114 (const :tag "Always" t)
115 (integer :tag "When more than N lines")))
117 (defcustom org-agenda-compact-blocks nil
118 "Non-nil means make the block agenda more compact.
119 This is done globally by leaving out lines like the agenda span
120 name and week number or the separator lines."
121 :group 'org-agenda
122 :type 'boolean)
124 (defcustom org-agenda-block-separator ?=
125 "The separator between blocks in the agenda.
126 If this is a string, it will be used as the separator, with a newline added.
127 If it is a character, it will be repeated to fill the window width.
128 If nil the separator is disabled. In `org-agenda-custom-commands' this
129 addresses the separator between the current and the previous block."
130 :group 'org-agenda
131 :type '(choice
132 (const :tag "Disabled" nil)
133 (character)
134 (string)))
136 (defgroup org-agenda-export nil
137 "Options concerning exporting agenda views in Org-mode."
138 :tag "Org Agenda Export"
139 :group 'org-agenda)
141 (defcustom org-agenda-with-colors t
142 "Non-nil means use colors in agenda views."
143 :group 'org-agenda-export
144 :type 'boolean)
146 (defcustom org-agenda-exporter-settings nil
147 "Alist of variable/value pairs that should be active during agenda export.
148 This is a good place to set options for ps-print and for htmlize.
149 Note that the way this is implemented, the values will be evaluated
150 before assigned to the variables. So make sure to quote values you do
151 *not* want evaluated, for example
153 (setq org-agenda-exporter-settings
154 \\='((ps-print-color-p \\='black-white)))"
155 :group 'org-agenda-export
156 :type '(repeat
157 (list
158 (variable)
159 (sexp :tag "Value"))))
161 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
162 "Hook run in a temporary buffer before writing the agenda to an export file.
163 A useful function for this hook is `org-agenda-add-entry-text'."
164 :group 'org-agenda-export
165 :type 'hook
166 :options '(org-agenda-add-entry-text))
168 (defcustom org-agenda-add-entry-text-maxlines 0
169 "Maximum number of entry text lines to be added to agenda.
170 This is only relevant when `org-agenda-add-entry-text' is part of
171 `org-agenda-before-write-hook', which is the default.
172 When this is 0, nothing will happen. When it is greater than 0, it
173 specifies the maximum number of lines that will be added for each entry
174 that is listed in the agenda view.
176 Note that this variable is not used during display, only when exporting
177 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
178 and `org-agenda-entry-text-maxlines'."
179 :group 'org-agenda
180 :type 'integer)
182 (defcustom org-agenda-add-entry-text-descriptive-links t
183 "Non-nil means export org-links as descriptive links in agenda added text.
184 This variable applies to the text added to the agenda when
185 `org-agenda-add-entry-text-maxlines' is larger than 0.
186 When this variable nil, the URL will (also) be shown."
187 :group 'org-agenda
188 :type 'boolean)
190 (defcustom org-agenda-export-html-style nil
191 "The style specification for exported HTML Agenda files.
192 If this variable contains a string, it will replace the default <style>
193 section as produced by `htmlize'.
194 Since there are different ways of setting style information, this variable
195 needs to contain the full HTML structure to provide a style, including the
196 surrounding HTML tags. The style specifications should include definitions
197 the fonts used by the agenda, here is an example:
199 <style type=\"text/css\">
200 p { font-weight: normal; color: gray; }
201 .org-agenda-structure {
202 font-size: 110%;
203 color: #003399;
204 font-weight: 600;
206 .org-todo {
207 color: #cc6666;
208 font-weight: bold;
210 .org-agenda-done {
211 color: #339933;
213 .org-done {
214 color: #339933;
216 .title { text-align: center; }
217 .todo, .deadline { color: red; }
218 .done { color: green; }
219 </style>
221 or, if you want to keep the style in a file,
223 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
225 As the value of this option simply gets inserted into the HTML <head> header,
226 you can \"misuse\" it to also add other text to the header."
227 :group 'org-agenda-export
228 :group 'org-export-html
229 :type '(choice
230 (const nil)
231 (string)))
233 (defcustom org-agenda-persistent-filter nil
234 "When set, keep filters from one agenda view to the next."
235 :group 'org-agenda
236 :type 'boolean)
238 (defgroup org-agenda-custom-commands nil
239 "Options concerning agenda views in Org-mode."
240 :tag "Org Agenda Custom Commands"
241 :group 'org-agenda)
243 (defconst org-sorting-choice
244 '(choice
245 (const time-up) (const time-down)
246 (const timestamp-up) (const timestamp-down)
247 (const scheduled-up) (const scheduled-down)
248 (const deadline-up) (const deadline-down)
249 (const ts-up) (const ts-down)
250 (const tsia-up) (const tsia-down)
251 (const category-keep) (const category-up) (const category-down)
252 (const tag-down) (const tag-up)
253 (const priority-up) (const priority-down)
254 (const todo-state-up) (const todo-state-down)
255 (const effort-up) (const effort-down)
256 (const habit-up) (const habit-down)
257 (const alpha-up) (const alpha-down)
258 (const user-defined-up) (const user-defined-down))
259 "Sorting choices.")
261 ;; Keep custom values for `org-agenda-filter-preset' compatible with
262 ;; the new variable `org-agenda-tag-filter-preset'.
263 (defvaralias 'org-agenda-filter-preset 'org-agenda-tag-filter-preset)
264 (defvaralias 'org-agenda-filter 'org-agenda-tag-filter)
266 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
267 "List of types searched for when creating the daily/weekly agenda.
268 This variable is a list of symbols that controls the types of
269 items that appear in the daily/weekly agenda. Allowed symbols in this
270 list are are
272 :timestamp List items containing a date stamp or date range matching
273 the selected date. This includes sexp entries in angular
274 brackets.
276 :sexp List entries resulting from plain diary-like sexps.
278 :deadline List deadline due on that date. When the date is today,
279 also list any deadlines past due, or due within
280 `org-deadline-warning-days'. `:deadline' must appear before
281 `:scheduled' if the setting of
282 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
283 any effect.
285 :deadline* Same as above, but only include the deadline if it has an
286 hour specification as [h]h:mm.
288 :scheduled List all items which are scheduled for the given date.
289 The diary for *today* also contains items which were
290 scheduled earlier and are not yet marked DONE.
292 :scheduled* Same as above, but only include the scheduled item if it
293 has an hour specification as [h]h:mm.
295 By default, all four non-starred types are turned on.
297 When :scheduled* or :deadline* are included, :schedule or :deadline
298 will be ignored.
300 Never set this variable globally using `setq', because then it
301 will apply to all future agenda commands. Instead, bind it with
302 `let' to scope it dynamically into the agenda-constructing
303 command. A good way to set it is through options in
304 `org-agenda-custom-commands'. For a more flexible (though
305 somewhat less efficient) way of determining what is included in
306 the daily/weekly agenda, see `org-agenda-skip-function'.")
308 (defconst org-agenda-custom-commands-local-options
309 `(repeat :tag "Local settings for this command. Remember to quote values"
310 (choice :tag "Setting"
311 (list :tag "Heading for this block"
312 (const org-agenda-overriding-header)
313 (string :tag "Headline"))
314 (list :tag "Files to be searched"
315 (const org-agenda-files)
316 (list
317 (const :format "" quote)
318 (repeat (file))))
319 (list :tag "Sorting strategy"
320 (const org-agenda-sorting-strategy)
321 (list
322 (const :format "" quote)
323 (repeat
324 ,org-sorting-choice)))
325 (list :tag "Prefix format"
326 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
327 (string))
328 (list :tag "Number of days in agenda"
329 (const org-agenda-span)
330 (choice (const :tag "Day" day)
331 (const :tag "Week" week)
332 (const :tag "Fortnight" fortnight)
333 (const :tag "Month" month)
334 (const :tag "Year" year)
335 (integer :tag "Custom")))
336 (list :tag "Fixed starting date"
337 (const org-agenda-start-day)
338 (string :value "2007-11-01"))
339 (list :tag "Start on day of week"
340 (const org-agenda-start-on-weekday)
341 (choice :value 1
342 (const :tag "Today" nil)
343 (integer :tag "Weekday No.")))
344 (list :tag "Include data from diary"
345 (const org-agenda-include-diary)
346 (boolean))
347 (list :tag "Deadline Warning days"
348 (const org-deadline-warning-days)
349 (integer :value 1))
350 (list :tag "Category filter preset"
351 (const org-agenda-category-filter-preset)
352 (list
353 (const :format "" quote)
354 (repeat
355 (string :tag "+category or -category"))))
356 (list :tag "Tags filter preset"
357 (const org-agenda-tag-filter-preset)
358 (list
359 (const :format "" quote)
360 (repeat
361 (string :tag "+tag or -tag"))))
362 (list :tag "Effort filter preset"
363 (const org-agenda-effort-filter-preset)
364 (list
365 (const :format "" quote)
366 (repeat
367 (string :tag "+=10 or -=10 or +<10 or ->10"))))
368 (list :tag "Regexp filter preset"
369 (const org-agenda-regexp-filter-preset)
370 (list
371 (const :format "" quote)
372 (repeat
373 (string :tag "+regexp or -regexp"))))
374 (list :tag "Set daily/weekly entry types"
375 (const org-agenda-entry-types)
376 (list
377 (const :format "" quote)
378 (set :greedy t :value ,org-agenda-entry-types
379 (const :deadline)
380 (const :scheduled)
381 (const :deadline*)
382 (const :scheduled*)
383 (const :timestamp)
384 (const :sexp))))
385 (list :tag "Standard skipping condition"
386 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
387 (const org-agenda-skip-function)
388 (list
389 (const :format "" quote)
390 (list
391 (choice
392 :tag "Skipping range"
393 (const :tag "Skip entry" org-agenda-skip-entry-if)
394 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
395 (repeat :inline t :tag "Conditions for skipping"
396 (choice
397 :tag "Condition type"
398 (list :tag "Regexp matches" :inline t
399 (const :format "" 'regexp)
400 (regexp))
401 (list :tag "Regexp does not match" :inline t
402 (const :format "" 'notregexp)
403 (regexp))
404 (list :tag "TODO state is" :inline t
405 (const 'todo)
406 (choice
407 (const :tag "Any not-done state" 'todo)
408 (const :tag "Any done state" 'done)
409 (const :tag "Any state" 'any)
410 (list :tag "Keyword list"
411 (const :format "" quote)
412 (repeat (string :tag "Keyword")))))
413 (list :tag "TODO state is not" :inline t
414 (const 'nottodo)
415 (choice
416 (const :tag "Any not-done state" 'todo)
417 (const :tag "Any done state" 'done)
418 (const :tag "Any state" 'any)
419 (list :tag "Keyword list"
420 (const :format "" quote)
421 (repeat (string :tag "Keyword")))))
422 (const :tag "scheduled" 'scheduled)
423 (const :tag "not scheduled" 'notscheduled)
424 (const :tag "deadline" 'deadline)
425 (const :tag "no deadline" 'notdeadline)
426 (const :tag "timestamp" 'timestamp)
427 (const :tag "no timestamp" 'nottimestamp))))))
428 (list :tag "Non-standard skipping condition"
429 :value (org-agenda-skip-function)
430 (const org-agenda-skip-function)
431 (sexp :tag "Function or form (quoted!)"))
432 (list :tag "Any variable"
433 (variable :tag "Variable")
434 (sexp :tag "Value (sexp)"))))
435 "Selection of examples for agenda command settings.
436 This will be spliced into the custom type of
437 `org-agenda-custom-commands'.")
440 (defcustom org-agenda-custom-commands
441 '(("n" "Agenda and all TODOs" ((agenda "") (alltodo ""))))
442 "Custom commands for the agenda.
443 These commands will be offered on the splash screen displayed by the
444 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
446 (key desc type match settings files)
448 key The key (one or more characters as a string) to be associated
449 with the command.
450 desc A description of the command, when omitted or nil, a default
451 description is built using MATCH.
452 type The command type, any of the following symbols:
453 agenda The daily/weekly agenda.
454 todo Entries with a specific TODO keyword, in all agenda files.
455 search Entries containing search words entry or headline.
456 tags Tags/Property/TODO match in all agenda files.
457 tags-todo Tags/P/T match in all agenda files, TODO entries only.
458 todo-tree Sparse tree of specific TODO keyword in *current* file.
459 tags-tree Sparse tree with all tags matches in *current* file.
460 occur-tree Occur sparse tree for *current* file.
461 ... A user-defined function.
462 match What to search for:
463 - a single keyword for TODO keyword searches
464 - a tags match expression for tags searches
465 - a word search expression for text searches.
466 - a regular expression for occur searches
467 For all other commands, this should be the empty string.
468 settings A list of option settings, similar to that in a let form, so like
469 this: ((opt1 val1) (opt2 val2) ...). The values will be
470 evaluated at the moment of execution, so quote them when needed.
471 files A list of files file to write the produced agenda buffer to
472 with the command `org-store-agenda-views'.
473 If a file name ends in \".html\", an HTML version of the buffer
474 is written out. If it ends in \".ps\", a postscript version is
475 produced. Otherwise, only the plain text is written to the file.
477 You can also define a set of commands, to create a composite agenda buffer.
478 In this case, an entry looks like this:
480 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
482 where
484 desc A description string to be displayed in the dispatcher menu.
485 cmd An agenda command, similar to the above. However, tree commands
486 are not allowed, but instead you can get agenda and global todo list.
487 So valid commands for a set are:
488 (agenda \"\" settings)
489 (alltodo \"\" settings)
490 (stuck \"\" settings)
491 (todo \"match\" settings files)
492 (search \"match\" settings files)
493 (tags \"match\" settings files)
494 (tags-todo \"match\" settings files)
496 Each command can carry a list of options, and another set of options can be
497 given for the whole set of commands. Individual command options take
498 precedence over the general options.
500 When using several characters as key to a command, the first characters
501 are prefix commands. For the dispatcher to display useful information, you
502 should provide a description for the prefix, like
504 (setq org-agenda-custom-commands
505 \\='((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
506 (\"hl\" tags \"+HOME+Lisa\")
507 (\"hp\" tags \"+HOME+Peter\")
508 (\"hk\" tags \"+HOME+Kim\")))"
509 :group 'org-agenda-custom-commands
510 :type `(repeat
511 (choice :value ("x" "Describe command here" tags "" nil)
512 (list :tag "Single command"
513 (string :tag "Access Key(s) ")
514 (option (string :tag "Description"))
515 (choice
516 (const :tag "Agenda" agenda)
517 (const :tag "TODO list" alltodo)
518 (const :tag "Search words" search)
519 (const :tag "Stuck projects" stuck)
520 (const :tag "Tags/Property match (all agenda files)" tags)
521 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
522 (const :tag "TODO keyword search (all agenda files)" todo)
523 (const :tag "Tags sparse tree (current buffer)" tags-tree)
524 (const :tag "TODO keyword tree (current buffer)" todo-tree)
525 (const :tag "Occur tree (current buffer)" occur-tree)
526 (sexp :tag "Other, user-defined function"))
527 (string :tag "Match (only for some commands)")
528 ,org-agenda-custom-commands-local-options
529 (option (repeat :tag "Export" (file :tag "Export to"))))
530 (list :tag "Command series, all agenda files"
531 (string :tag "Access Key(s)")
532 (string :tag "Description ")
533 (repeat :tag "Component"
534 (choice
535 (list :tag "Agenda"
536 (const :format "" agenda)
537 (const :tag "" :format "" "")
538 ,org-agenda-custom-commands-local-options)
539 (list :tag "TODO list (all keywords)"
540 (const :format "" alltodo)
541 (const :tag "" :format "" "")
542 ,org-agenda-custom-commands-local-options)
543 (list :tag "Search words"
544 (const :format "" search)
545 (string :tag "Match")
546 ,org-agenda-custom-commands-local-options)
547 (list :tag "Stuck projects"
548 (const :format "" stuck)
549 (const :tag "" :format "" "")
550 ,org-agenda-custom-commands-local-options)
551 (list :tag "Tags search"
552 (const :format "" tags)
553 (string :tag "Match")
554 ,org-agenda-custom-commands-local-options)
555 (list :tag "Tags search, TODO entries only"
556 (const :format "" tags-todo)
557 (string :tag "Match")
558 ,org-agenda-custom-commands-local-options)
559 (list :tag "TODO keyword search"
560 (const :format "" todo)
561 (string :tag "Match")
562 ,org-agenda-custom-commands-local-options)
563 (list :tag "Other, user-defined function"
564 (symbol :tag "function")
565 (string :tag "Match")
566 ,org-agenda-custom-commands-local-options)))
568 (repeat :tag "Settings for entire command set"
569 (list (variable :tag "Any variable")
570 (sexp :tag "Value")))
571 (option (repeat :tag "Export" (file :tag "Export to"))))
572 (cons :tag "Prefix key documentation"
573 (string :tag "Access Key(s)")
574 (string :tag "Description ")))))
576 (defcustom org-agenda-query-register ?o
577 "The register holding the current query string.
578 The purpose of this is that if you construct a query string interactively,
579 you can then use it to define a custom command."
580 :group 'org-agenda-custom-commands
581 :type 'character)
583 (defcustom org-stuck-projects
584 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
585 "How to identify stuck projects.
586 This is a list of four items:
587 1. A tags/todo/property matcher string that is used to identify a project.
588 See the manual for a description of tag and property searches.
589 The entire tree below a headline matched by this is considered one project.
590 2. A list of TODO keywords identifying non-stuck projects.
591 If the project subtree contains any headline with one of these todo
592 keywords, the project is considered to be not stuck. If you specify
593 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
594 3. A list of tags identifying non-stuck projects.
595 If the project subtree contains any headline with one of these tags,
596 the project is considered to be not stuck. If you specify \"*\" as
597 a tag, any tag will mark the project unstuck. Note that this is about
598 the explicit presence of a tag somewhere in the subtree, inherited
599 tags do not count here. If inherited tags make a project not stuck,
600 use \"-TAG\" in the tags part of the matcher under (1.) above.
601 4. An arbitrary regular expression matching non-stuck projects.
603 If the project turns out to be not stuck, search continues also in the
604 subtree to see if any of the subtasks have project status.
606 See also the variable `org-tags-match-list-sublevels' which applies
607 to projects matched by this search as well.
609 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
610 or `C-c a #' to produce the list."
611 :group 'org-agenda-custom-commands
612 :type '(list
613 (string :tag "Tags/TODO match to identify a project")
614 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
615 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
616 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
618 (defgroup org-agenda-skip nil
619 "Options concerning skipping parts of agenda files."
620 :tag "Org Agenda Skip"
621 :group 'org-agenda)
623 (defcustom org-agenda-skip-function-global nil
624 "Function to be called at each match during agenda construction.
625 If this function returns nil, the current match should not be skipped.
626 If the function decided to skip an agenda match, is must return the
627 buffer position from which the search should be continued.
628 This may also be a Lisp form, which will be evaluated.
630 This variable will be applied to every agenda match, including
631 tags/property searches and TODO lists. So try to make the test function
632 do its checking as efficiently as possible. To implement a skipping
633 condition just for specific agenda commands, use the variable
634 `org-agenda-skip-function' which can be set in the options section
635 of custom agenda commands."
636 :group 'org-agenda-skip
637 :type 'sexp)
639 (defgroup org-agenda-daily/weekly nil
640 "Options concerning the daily/weekly agenda."
641 :tag "Org Agenda Daily/Weekly"
642 :group 'org-agenda)
643 (defgroup org-agenda-todo-list nil
644 "Options concerning the global todo list agenda view."
645 :tag "Org Agenda Todo List"
646 :group 'org-agenda)
647 (defgroup org-agenda-match-view nil
648 "Options concerning the general tags/property/todo match agenda view."
649 :tag "Org Agenda Match View"
650 :group 'org-agenda)
651 (defgroup org-agenda-search-view nil
652 "Options concerning the search agenda view."
653 :tag "Org Agenda Search View"
654 :group 'org-agenda)
656 (defvar org-agenda-archives-mode nil
657 "Non-nil means the agenda will include archived items.
658 If this is the symbol `trees', trees in the selected agenda scope
659 that are marked with the ARCHIVE tag will be included anyway. When this is
660 t, also all archive files associated with the current selection of agenda
661 files will be included.")
663 (defcustom org-agenda-restriction-lock-highlight-subtree t
664 "Non-nil means highlight the whole subtree when restriction is active.
665 Otherwise only highlight the headline. Highlighting the whole subtree is
666 useful to ensure no edits happen beyond the restricted region."
667 :group 'org-agenda
668 :type 'boolean)
670 (defcustom org-agenda-skip-comment-trees t
671 "Non-nil means skip trees that start with the COMMENT keyword.
672 When nil, these trees are also scanned by agenda commands."
673 :group 'org-agenda-skip
674 :type 'boolean)
676 (defcustom org-agenda-todo-list-sublevels t
677 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
678 When nil, the sublevels of a TODO entry are not checked, resulting in
679 potentially much shorter TODO lists."
680 :group 'org-agenda-skip
681 :group 'org-agenda-todo-list
682 :type 'boolean)
684 (defcustom org-agenda-todo-ignore-with-date nil
685 "Non-nil means don't show entries with a date in the global todo list.
686 You can use this if you prefer to mark mere appointments with a TODO keyword,
687 but don't want them to show up in the TODO list.
688 When this is set, it also covers deadlines and scheduled items, the settings
689 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
690 will be ignored.
691 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
692 :group 'org-agenda-skip
693 :group 'org-agenda-todo-list
694 :type 'boolean)
696 (defcustom org-agenda-todo-ignore-timestamp nil
697 "Non-nil means don't show entries with a timestamp.
698 This applies when creating the global todo list.
699 Valid values are:
701 past Don't show entries for today or in the past.
703 future Don't show entries with a timestamp in the future.
704 The idea behind this is that if it has a future
705 timestamp, you don't want to think about it until the
706 date.
708 all Don't show any entries with a timestamp in the global todo list.
709 The idea behind this is that by setting a timestamp, you
710 have already \"taken care\" of this item.
712 This variable can also have an integer as a value. If positive (N),
713 todos with a timestamp N or more days in the future will be ignored. If
714 negative (-N), todos with a timestamp N or more days in the past will be
715 ignored. If 0, todos with a timestamp either today or in the future will
716 be ignored. For example, a value of -1 will exclude todos with a
717 timestamp in the past (yesterday or earlier), while a value of 7 will
718 exclude todos with a timestamp a week or more in the future.
720 See also `org-agenda-todo-ignore-with-date'.
721 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
722 to make his option also apply to the tags-todo list."
723 :group 'org-agenda-skip
724 :group 'org-agenda-todo-list
725 :version "24.1"
726 :type '(choice
727 (const :tag "Ignore future timestamp todos" future)
728 (const :tag "Ignore past or present timestamp todos" past)
729 (const :tag "Ignore all timestamp todos" all)
730 (const :tag "Show timestamp todos" nil)
731 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
733 (defcustom org-agenda-todo-ignore-scheduled nil
734 "Non-nil means, ignore some scheduled TODO items when making TODO list.
735 This applies when creating the global todo list.
736 Valid values are:
738 past Don't show entries scheduled today or in the past.
740 future Don't show entries scheduled in the future.
741 The idea behind this is that by scheduling it, you don't want to
742 think about it until the scheduled date.
744 all Don't show any scheduled entries in the global todo list.
745 The idea behind this is that by scheduling it, you have already
746 \"taken care\" of this item.
748 t Same as `all', for backward compatibility.
750 This variable can also have an integer as a value. See
751 `org-agenda-todo-ignore-timestamp' for more details.
753 See also `org-agenda-todo-ignore-with-date'.
754 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
755 to make his option also apply to the tags-todo list."
756 :group 'org-agenda-skip
757 :group 'org-agenda-todo-list
758 :type '(choice
759 (const :tag "Ignore future-scheduled todos" future)
760 (const :tag "Ignore past- or present-scheduled todos" past)
761 (const :tag "Ignore all scheduled todos" all)
762 (const :tag "Ignore all scheduled todos (compatibility)" t)
763 (const :tag "Show scheduled todos" nil)
764 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
766 (defcustom org-agenda-todo-ignore-deadlines nil
767 "Non-nil means ignore some deadline TODO items when making TODO list.
768 There are different motivations for using different values, please think
769 carefully when configuring this variable.
771 This applies when creating the global todo list.
772 Valid values are:
774 near Don't show near deadline entries. A deadline is near when it is
775 closer than `org-deadline-warning-days' days. The idea behind this
776 is that such items will appear in the agenda anyway.
778 far Don't show TODO entries where a deadline has been defined, but
779 the deadline is not near. This is useful if you don't want to
780 use the todo list to figure out what to do now.
782 past Don't show entries with a deadline timestamp for today or in the past.
784 future Don't show entries with a deadline timestamp in the future, not even
785 when they become `near' ones. Use it with caution.
787 all Ignore all TODO entries that do have a deadline.
789 t Same as `near', for backward compatibility.
791 This variable can also have an integer as a value. See
792 `org-agenda-todo-ignore-timestamp' for more details.
794 See also `org-agenda-todo-ignore-with-date'.
795 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
796 to make his option also apply to the tags-todo list."
797 :group 'org-agenda-skip
798 :group 'org-agenda-todo-list
799 :type '(choice
800 (const :tag "Ignore near deadlines" near)
801 (const :tag "Ignore near deadlines (compatibility)" t)
802 (const :tag "Ignore far deadlines" far)
803 (const :tag "Ignore all TODOs with a deadlines" all)
804 (const :tag "Show all TODOs, even if they have a deadline" nil)
805 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
807 (defcustom org-agenda-todo-ignore-time-comparison-use-seconds nil
808 "Time unit to use when possibly ignoring an agenda item.
810 See the docstring of various `org-agenda-todo-ignore-*' options.
811 The default is to compare time stamps using days. An item is thus
812 considered to be in the future if it is at least one day after today.
813 Non-nil means to compare time stamps using seconds. An item is then
814 considered future if it has a time value later than current time."
815 :group 'org-agenda-skip
816 :group 'org-agenda-todo-list
817 :version "24.4"
818 :package-version '(Org . "8.0")
819 :type '(choice
820 (const :tag "Compare time with days" nil)
821 (const :tag "Compare time with seconds" t)))
823 (defcustom org-agenda-tags-todo-honor-ignore-options nil
824 "Non-nil means honor todo-list ignores options also in tags-todo search.
825 The variables
826 `org-agenda-todo-ignore-with-date',
827 `org-agenda-todo-ignore-timestamp',
828 `org-agenda-todo-ignore-scheduled',
829 `org-agenda-todo-ignore-deadlines'
830 make the global TODO list skip entries that have time stamps of certain
831 kinds. If this option is set, the same options will also apply for the
832 tags-todo search, which is the general tags/property matcher
833 restricted to unfinished TODO entries only."
834 :group 'org-agenda-skip
835 :group 'org-agenda-todo-list
836 :group 'org-agenda-match-view
837 :type 'boolean)
839 (defcustom org-agenda-skip-scheduled-if-done nil
840 "Non-nil means don't show scheduled items in agenda when they are done.
841 This is relevant for the daily/weekly agenda, not for the TODO list. And
842 it applies only to the actual date of the scheduling. Warnings about
843 an item with a past scheduling dates are always turned off when the item
844 is DONE."
845 :group 'org-agenda-skip
846 :group 'org-agenda-daily/weekly
847 :type 'boolean)
849 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
850 "Non-nil means skip scheduling line if same entry shows because of deadline.
852 In the agenda of today, an entry can show up multiple times
853 because it is both scheduled and has a nearby deadline, and maybe
854 a plain time stamp as well.
856 When this variable is nil, the entry will be shown several times.
858 When set to t, then only the deadline is shown and the fact that
859 the entry is scheduled today or was scheduled previously is not
860 shown.
862 When set to the symbol `not-today', skip scheduled previously,
863 but not scheduled today.
865 When set to the symbol `repeated-after-deadline', skip scheduled
866 items if they are repeated beyond the current deadline."
867 :group 'org-agenda-skip
868 :group 'org-agenda-daily/weekly
869 :type '(choice
870 (const :tag "Never" nil)
871 (const :tag "Always" t)
872 (const :tag "Not when scheduled today" not-today)
873 (const :tag "When repeated past deadline" repeated-after-deadline)))
875 (defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
876 "Non-nil means skip timestamp line if same entry shows because of deadline.
877 In the agenda of today, an entry can show up multiple times
878 because it has both a plain timestamp and has a nearby deadline.
879 When this variable is t, then only the deadline is shown and the
880 fact that the entry has a timestamp for or including today is not
881 shown. When this variable is nil, the entry will be shown
882 several times."
883 :group 'org-agenda-skip
884 :group 'org-agenda-daily/weekly
885 :version "24.1"
886 :type '(choice
887 (const :tag "Never" nil)
888 (const :tag "Always" t)))
890 (defcustom org-agenda-skip-deadline-if-done nil
891 "Non-nil means don't show deadlines when the corresponding item is done.
892 When nil, the deadline is still shown and should give you a happy feeling.
893 This is relevant for the daily/weekly agenda. And it applied only to the
894 actually date of the deadline. Warnings about approaching and past-due
895 deadlines are always turned off when the item is DONE."
896 :group 'org-agenda-skip
897 :group 'org-agenda-daily/weekly
898 :type 'boolean)
900 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
901 "Non-nil means skip deadline prewarning when entry is also scheduled.
902 This will apply on all days where a prewarning for the deadline would
903 be shown, but not at the day when the entry is actually due. On that day,
904 the deadline will be shown anyway.
905 This variable may be set to nil, t, the symbol `pre-scheduled',
906 or a number which will then give the number of days before the actual
907 deadline when the prewarnings should resume. The symbol `pre-scheduled'
908 eliminates the deadline prewarning only prior to the scheduled date.
909 This can be used in a workflow where the first showing of the deadline will
910 trigger you to schedule it, and then you don't want to be reminded of it
911 because you will take care of it on the day when scheduled."
912 :group 'org-agenda-skip
913 :group 'org-agenda-daily/weekly
914 :version "24.1"
915 :type '(choice
916 (const :tag "Always show prewarning" nil)
917 (const :tag "Remove prewarning prior to scheduled date" pre-scheduled)
918 (const :tag "Remove prewarning if entry is scheduled" t)
919 (integer :tag "Restart prewarning N days before deadline")))
921 (defcustom org-agenda-skip-scheduled-delay-if-deadline nil
922 "Non-nil means skip scheduled delay when entry also has a deadline.
923 This variable may be set to nil, t, the symbol `post-deadline',
924 or a number which will then give the number of days after the actual
925 scheduled date when the delay should expire. The symbol `post-deadline'
926 eliminates the schedule delay when the date is posterior to the deadline."
927 :group 'org-agenda-skip
928 :group 'org-agenda-daily/weekly
929 :version "24.4"
930 :package-version '(Org . "8.0")
931 :type '(choice
932 (const :tag "Always honor delay" nil)
933 (const :tag "Ignore delay if posterior to the deadline" post-deadline)
934 (const :tag "Ignore delay if entry has a deadline" t)
935 (integer :tag "Honor delay up until N days after the scheduled date")))
937 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
938 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
939 When non-nil, after the search for timestamps has matched once in an
940 entry, the rest of the entry will not be searched."
941 :group 'org-agenda-skip
942 :type 'boolean)
944 (defcustom org-agenda-skip-timestamp-if-done nil
945 "Non-nil means don't select item by timestamp or -range if it is DONE."
946 :group 'org-agenda-skip
947 :group 'org-agenda-daily/weekly
948 :type 'boolean)
950 (defcustom org-agenda-dim-blocked-tasks t
951 "Non-nil means dim blocked tasks in the agenda display.
952 This causes some overhead during agenda construction, but if you
953 have turned on `org-enforce-todo-dependencies',
954 `org-enforce-todo-checkbox-dependencies', or any other blocking
955 mechanism, this will create useful feedback in the agenda.
957 Instead of t, this variable can also have the value `invisible'.
958 Then blocked tasks will be invisible and only become visible when
959 they become unblocked. An exemption to this behavior is when a task is
960 blocked because of unchecked checkboxes below it. Since checkboxes do
961 not show up in the agenda views, making this task invisible you remove any
962 trace from agenda views that there is something to do. Therefore, a task
963 that is blocked because of checkboxes will never be made invisible, it
964 will only be dimmed."
965 :group 'org-agenda-daily/weekly
966 :group 'org-agenda-todo-list
967 :version "24.3"
968 :type '(choice
969 (const :tag "Do not dim" nil)
970 (const :tag "Dim to a gray face" t)
971 (const :tag "Make invisible" invisible)))
973 (defcustom org-timeline-show-empty-dates 3
974 "Non-nil means `org-timeline' also shows dates without an entry.
975 When nil, only the days which actually have entries are shown.
976 When t, all days between the first and the last date are shown.
977 When an integer, show also empty dates, but if there is a gap of more than
978 N days, just insert a special line indicating the size of the gap."
979 :group 'org-agenda-skip
980 :type '(choice
981 (const :tag "None" nil)
982 (const :tag "All" t)
983 (integer :tag "at most")))
985 (defgroup org-agenda-startup nil
986 "Options concerning initial settings in the Agenda in Org Mode."
987 :tag "Org Agenda Startup"
988 :group 'org-agenda)
990 (defcustom org-agenda-menu-show-matcher t
991 "Non-nil means show the match string in the agenda dispatcher menu.
992 When nil, the matcher string is not shown, but is put into the help-echo
993 property so than moving the mouse over the command shows it.
994 Setting it to nil is good if matcher strings are very long and/or if
995 you want to use two-columns display (see `org-agenda-menu-two-columns')."
996 :group 'org-agenda
997 :version "24.1"
998 :type 'boolean)
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 (defcustom org-agenda-finalize-hook nil
1009 "Hook run just before displaying an agenda buffer.
1010 The buffer is still writable when the hook is called.
1012 You can modify some of the buffer substrings but you should be
1013 extra careful not to modify the text properties of the agenda
1014 headlines as the agenda display heavily relies on them."
1015 :group 'org-agenda-startup
1016 :type 'hook)
1018 (defcustom org-agenda-mouse-1-follows-link nil
1019 "Non-nil means mouse-1 on a link will follow the link in the agenda.
1020 A longer mouse click will still set point. Needs to be set
1021 before org.el is loaded."
1022 :group 'org-agenda-startup
1023 :type 'boolean)
1025 (defcustom org-agenda-start-with-follow-mode nil
1026 "The initial value of follow mode in a newly created agenda window."
1027 :group 'org-agenda-startup
1028 :type 'boolean)
1030 (defcustom org-agenda-follow-indirect nil
1031 "Non-nil means `org-agenda-follow-mode' displays only the
1032 current item's tree, in an indirect buffer."
1033 :group 'org-agenda
1034 :version "24.1"
1035 :type 'boolean)
1037 (defcustom org-agenda-show-outline-path t
1038 "Non-nil means show outline path in echo area after line motion."
1039 :group 'org-agenda-startup
1040 :type 'boolean)
1042 (defcustom org-agenda-start-with-entry-text-mode nil
1043 "The initial value of entry-text-mode in a newly created agenda window."
1044 :group 'org-agenda-startup
1045 :type 'boolean)
1047 (defcustom org-agenda-entry-text-maxlines 5
1048 "Number of text lines to be added when `E' is pressed in the agenda.
1050 Note that this variable only used during agenda display. Add add entry text
1051 when exporting the agenda, configure the variable
1052 `org-agenda-add-entry-ext-maxlines'."
1053 :group 'org-agenda
1054 :type 'integer)
1056 (defcustom org-agenda-entry-text-exclude-regexps nil
1057 "List of regular expressions to clean up entry text.
1058 The complete matches of all regular expressions in this list will be
1059 removed from entry text before it is shown in the agenda."
1060 :group 'org-agenda
1061 :type '(repeat (regexp)))
1063 (defcustom org-agenda-entry-text-leaders " > "
1064 "Text prepended to the entry text in agenda buffers."
1065 :version "24.4"
1066 :package-version '(Org . "8.0")
1067 :group 'org-agenda
1068 :type 'string)
1070 (defvar org-agenda-entry-text-cleanup-hook nil
1071 "Hook that is run after basic cleanup of entry text to be shown in agenda.
1072 This cleanup is done in a temporary buffer, so the function may inspect and
1073 change the entire buffer.
1074 Some default stuff like drawers and scheduling/deadline dates will already
1075 have been removed when this is called, as will any matches for regular
1076 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
1078 (defvar org-agenda-include-inactive-timestamps nil
1079 "Non-nil means include inactive time stamps in agenda and timeline.
1080 Dynamically scoped.")
1082 (defgroup org-agenda-windows nil
1083 "Options concerning the windows used by the Agenda in Org Mode."
1084 :tag "Org Agenda Windows"
1085 :group 'org-agenda)
1087 (defcustom org-agenda-window-setup 'reorganize-frame
1088 "How the agenda buffer should be displayed.
1089 Possible values for this option are:
1091 current-window Show agenda in the current window, keeping all other windows.
1092 other-window Use `switch-to-buffer-other-window' to display agenda.
1093 only-window Show agenda, deleting all other windows.
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 only-window)
1105 (const reorganize-frame)))
1107 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
1108 "The min and max height of the agenda window as a fraction of frame height.
1109 The value of the variable is a cons cell with two numbers between 0 and 1.
1110 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
1111 :group 'org-agenda-windows
1112 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
1114 (defcustom org-agenda-restore-windows-after-quit nil
1115 "Non-nil means restore window configuration upon exiting agenda.
1116 Before the window configuration is changed for displaying the agenda,
1117 the current status is recorded. When the agenda is exited with
1118 `q' or `x' and this option is set, the old state is restored. If
1119 `org-agenda-window-setup' is `other-frame', the value of this
1120 option will be ignored."
1121 :group 'org-agenda-windows
1122 :type 'boolean)
1124 (defcustom org-agenda-span 'week
1125 "Number of days to include in overview display.
1126 Can be day, week, month, year, or any number of days.
1127 Custom commands can set this variable in the options section."
1128 :group 'org-agenda-daily/weekly
1129 :type '(choice (const :tag "Day" day)
1130 (const :tag "Week" week)
1131 (const :tag "Fortnight" fortnight)
1132 (const :tag "Month" month)
1133 (const :tag "Year" year)
1134 (integer :tag "Custom")))
1136 (defcustom org-agenda-start-on-weekday 1
1137 "Non-nil means start the overview always on the specified weekday.
1138 0 denotes Sunday, 1 denotes Monday, etc.
1139 When nil, always start on the current day.
1140 Custom commands can set this variable in the options section."
1141 :group 'org-agenda-daily/weekly
1142 :type '(choice (const :tag "Today" nil)
1143 (integer :tag "Weekday No.")))
1145 (defcustom org-agenda-show-all-dates t
1146 "Non-nil means `org-agenda' shows every day in the selected range.
1147 When nil, only the days which actually have entries are shown."
1148 :group 'org-agenda-daily/weekly
1149 :type 'boolean)
1151 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1152 "Format string for displaying dates in the agenda.
1153 Used by the daily/weekly agenda and by the timeline. This should be
1154 a format string understood by `format-time-string', or a function returning
1155 the formatted date as a string. The function must take a single argument,
1156 a calendar-style date list like (month day year)."
1157 :group 'org-agenda-daily/weekly
1158 :type '(choice
1159 (string :tag "Format string")
1160 (function :tag "Function")))
1162 (defun org-agenda-format-date-aligned (date)
1163 "Format a DATE string for display in the daily/weekly agenda, or timeline.
1164 This function makes sure that dates are aligned for easy reading."
1165 (require 'cal-iso)
1166 (let* ((dayname (calendar-day-name date))
1167 (day (cadr date))
1168 (day-of-week (calendar-day-of-week date))
1169 (month (car date))
1170 (monthname (calendar-month-name month))
1171 (year (nth 2 date))
1172 (iso-week (org-days-to-iso-week
1173 (calendar-absolute-from-gregorian date)))
1174 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1175 (1- year))
1176 ((and (= month 12) (<= iso-week 1))
1177 (1+ year))
1178 (t year)))
1179 (weekstring (if (= day-of-week 1)
1180 (format " W%02d" iso-week)
1181 "")))
1182 (format "%-10s %2d %s %4d%s"
1183 dayname day monthname year weekstring)))
1185 (defcustom org-agenda-time-leading-zero nil
1186 "Non-nil means use leading zero for military times in agenda.
1187 For example, 9:30am would become 09:30 rather than 9:30."
1188 :group 'org-agenda-daily/weekly
1189 :version "24.1"
1190 :type 'boolean)
1192 (defcustom org-agenda-timegrid-use-ampm nil
1193 "When set, show AM/PM style timestamps on the timegrid."
1194 :group 'org-agenda
1195 :version "24.1"
1196 :type 'boolean)
1198 (defun org-agenda-time-of-day-to-ampm (time)
1199 "Convert TIME of a string like \"13:45\" to an AM/PM style time string."
1200 (let* ((hour-number (string-to-number (substring time 0 -3)))
1201 (minute (substring time -2))
1202 (ampm "am"))
1203 (cond
1204 ((equal hour-number 12)
1205 (setq ampm "pm"))
1206 ((> hour-number 12)
1207 (setq ampm "pm")
1208 (setq hour-number (- hour-number 12))))
1209 (concat
1210 (if org-agenda-time-leading-zero
1211 (format "%02d" hour-number)
1212 (format "%02s" (number-to-string hour-number)))
1213 ":" minute ampm)))
1215 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1216 "Conditionally convert TIME to AM/PM format based on `org-agenda-timegrid-use-ampm'."
1217 (if org-agenda-timegrid-use-ampm
1218 (org-agenda-time-of-day-to-ampm time)
1219 time))
1221 (defcustom org-agenda-weekend-days '(6 0)
1222 "Which days are weekend?
1223 These days get the special face `org-agenda-date-weekend' in the agenda
1224 and timeline buffers."
1225 :group 'org-agenda-daily/weekly
1226 :type '(set :greedy t
1227 (const :tag "Monday" 1)
1228 (const :tag "Tuesday" 2)
1229 (const :tag "Wednesday" 3)
1230 (const :tag "Thursday" 4)
1231 (const :tag "Friday" 5)
1232 (const :tag "Saturday" 6)
1233 (const :tag "Sunday" 0)))
1235 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1236 "Non-nil means jump to today when moving a past date forward in time.
1237 When using S-right in the agenda to move a a date forward, and the date
1238 stamp currently points to the past, the first key press will move it
1239 to today. WHen nil, just move one day forward even if the date stays
1240 in the past."
1241 :group 'org-agenda-daily/weekly
1242 :version "24.1"
1243 :type 'boolean)
1245 (defcustom org-agenda-include-diary nil
1246 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1247 Custom commands can set this variable in the options section."
1248 :group 'org-agenda-daily/weekly
1249 :type 'boolean)
1251 (defcustom org-agenda-include-deadlines t
1252 "If non-nil, include entries within their deadline warning period.
1253 Custom commands can set this variable in the options section."
1254 :group 'org-agenda-daily/weekly
1255 :version "24.1"
1256 :type 'boolean)
1258 (defcustom org-agenda-repeating-timestamp-show-all t
1259 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1260 When set to a list of strings, only show occurrences of repeating
1261 stamps for these TODO keywords. When nil, only one occurrence is
1262 shown, either today or the nearest into the future."
1263 :group 'org-agenda-daily/weekly
1264 :type '(choice
1265 (const :tag "Show repeating stamps" t)
1266 (repeat :tag "Show repeating stamps for these TODO keywords"
1267 (string :tag "TODO Keyword"))
1268 (const :tag "Don't show repeating stamps" nil)))
1270 (defcustom org-scheduled-past-days 10000
1271 "Number of days to continue listing scheduled items not marked DONE.
1272 When an item is scheduled on a date, it shows up in the agenda on
1273 this day and will be listed until it is marked done or for the
1274 number of days given here."
1275 :group 'org-agenda-daily/weekly
1276 :type 'integer)
1278 (defcustom org-agenda-log-mode-items '(closed clock)
1279 "List of items that should be shown in agenda log mode.
1280 \\<org-agenda-mode-map>\
1281 This list may contain the following symbols:
1283 closed Show entries that have been closed on that day.
1284 clock Show entries that have received clocked time on that day.
1285 state Show all logged state changes.
1286 Note that instead of changing this variable, you can also press \
1287 `\\[universal-argument] \\[org-agenda-log-mode]' in
1288 the agenda to display all available LOG items temporarily."
1289 :group 'org-agenda-daily/weekly
1290 :type '(set :greedy t (const closed) (const clock) (const state)))
1292 (defcustom org-agenda-clock-consistency-checks
1293 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1294 :gap-ok-around ("4:00")
1295 :default-face ((:background "DarkRed") (:foreground "white"))
1296 :overlap-face nil :gap-face nil :no-end-time-face nil
1297 :long-face nil :short-face nil)
1298 "This is a property list, with the following keys:
1300 :max-duration Mark clocking chunks that are longer than this time.
1301 This is a time string like \"HH:MM\", or the number
1302 of minutes as an integer.
1304 :min-duration Mark clocking chunks that are shorter that this.
1305 This is a time string like \"HH:MM\", or the number
1306 of minutes as an integer.
1308 :max-gap Mark gaps between clocking chunks that are longer than
1309 this duration. A number of minutes, or a string
1310 like \"HH:MM\".
1312 :gap-ok-around List of times during the day which are usually not working
1313 times. When a gap is detected, but the gap contains any
1314 of these times, the gap is *not* reported. For example,
1315 if this is (\"4:00\" \"13:00\") then gaps that contain
1316 4:00 in the morning (i.e. the night) and 13:00
1317 (i.e. a typical lunch time) do not cause a warning.
1318 You should have at least one time during the night in this
1319 list, or otherwise the first task each morning will trigger
1320 a warning because it follows a long gap.
1322 Furthermore, the following properties can be used to define faces for
1323 issue display.
1325 :default-face the default face, if the specific face is undefined
1326 :overlap-face face for overlapping clocks
1327 :gap-face face for gaps between clocks
1328 :no-end-time-face face for incomplete clocks
1329 :long-face face for clock intervals that are too long
1330 :short-face face for clock intervals that are too short"
1331 :group 'org-agenda-daily/weekly
1332 :group 'org-clock
1333 :version "24.1"
1334 :type 'plist)
1336 (defcustom org-agenda-log-mode-add-notes t
1337 "Non-nil means add first line of notes to log entries in agenda views.
1338 If a log item like a state change or a clock entry is associated with
1339 notes, the first line of these notes will be added to the entry in the
1340 agenda display."
1341 :group 'org-agenda-daily/weekly
1342 :type 'boolean)
1344 (defcustom org-agenda-start-with-log-mode nil
1345 "The initial value of log-mode in a newly created agenda window.
1346 See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
1347 explanations on the possible values."
1348 :group 'org-agenda-startup
1349 :group 'org-agenda-daily/weekly
1350 :type '(choice (const :tag "Don't show log items" nil)
1351 (const :tag "Show only log items" only)
1352 (const :tag "Show all possible log items" clockcheck)
1353 (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
1354 (choice (const :tag "Show closed log items" closed)
1355 (const :tag "Show clocked log items" clock)
1356 (const :tag "Show all logged state changes" state)))))
1358 (defcustom org-agenda-start-with-clockreport-mode nil
1359 "The initial value of clockreport-mode in a newly created agenda window."
1360 :group 'org-agenda-startup
1361 :group 'org-agenda-daily/weekly
1362 :type 'boolean)
1364 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1365 "Property list with parameters for the clocktable in clockreport mode.
1366 This is the display mode that shows a clock table in the daily/weekly
1367 agenda, the properties for this dynamic block can be set here.
1368 The usual clocktable parameters are allowed here, but you cannot set
1369 the properties :name, :tstart, :tend, :block, and :scope - these will
1370 be overwritten to make sure the content accurately reflects the
1371 current display in the agenda."
1372 :group 'org-agenda-daily/weekly
1373 :type 'plist)
1375 (defcustom org-agenda-search-view-always-boolean nil
1376 "Non-nil means the search string is interpreted as individual parts.
1378 The search string for search view can either be interpreted as a phrase,
1379 or as a list of snippets that define a boolean search for a number of
1380 strings.
1382 When this is non-nil, the string will be split on whitespace, and each
1383 snippet will be searched individually, and all must match in order to
1384 select an entry. A snippet is then a single string of non-white
1385 characters, or a string in double quotes, or a regexp in {} braces.
1386 If a snippet is preceded by \"-\", the snippet must *not* match.
1387 \"+\" is syntactic sugar for positive selection. Each snippet may
1388 be found as a full word or a partial word, but see the variable
1389 `org-agenda-search-view-force-full-words'.
1391 When this is nil, search will look for the entire search phrase as one,
1392 with each space character matching any amount of whitespace, including
1393 line breaks.
1395 Even when this is nil, you can still switch to Boolean search dynamically
1396 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1397 is a regexp marked with braces like \"{abc}\", this will also switch to
1398 boolean search."
1399 :group 'org-agenda-search-view
1400 :version "24.1"
1401 :type 'boolean)
1403 (defvaralias 'org-agenda-search-view-search-words-only
1404 'org-agenda-search-view-always-boolean)
1406 (defcustom org-agenda-search-view-force-full-words nil
1407 "Non-nil means, search words must be matches as complete words.
1408 When nil, they may also match part of a word."
1409 :group 'org-agenda-search-view
1410 :version "24.1"
1411 :type 'boolean)
1413 (defcustom org-agenda-search-view-max-outline-level 0
1414 "Maximum outline level to display in search view.
1415 E.g. when this is set to 1, the search view will only
1416 show headlines of level 1. When set to 0, the default
1417 value, don't limit agenda view by outline level."
1418 :group 'org-agenda-search-view
1419 :version "24.4"
1420 :package-version '(Org . "8.3")
1421 :type 'integer)
1423 (defgroup org-agenda-time-grid nil
1424 "Options concerning the time grid in the Org Agenda."
1425 :tag "Org Agenda Time Grid"
1426 :group 'org-agenda)
1428 (defcustom org-agenda-search-headline-for-time t
1429 "Non-nil means search headline for a time-of-day.
1430 If the headline contains a time-of-day in one format or another, it will
1431 be used to sort the entry into the time sequence of items for a day.
1432 Some people have time stamps in the headline that refer to the creation
1433 time or so, and then this produces an unwanted side effect. If this is
1434 the case for your, use this variable to turn off searching the headline
1435 for a time."
1436 :group 'org-agenda-time-grid
1437 :type 'boolean)
1439 (defcustom org-agenda-use-time-grid t
1440 "Non-nil means show a time grid in the agenda schedule.
1441 A time grid is a set of lines for specific times (like every two hours between
1442 8:00 and 20:00). The items scheduled for a day at specific times are
1443 sorted in between these lines.
1444 For details about when the grid will be shown, and what it will look like, see
1445 the variable `org-agenda-time-grid'."
1446 :group 'org-agenda-time-grid
1447 :type 'boolean)
1449 (defcustom org-agenda-time-grid
1450 '((daily today require-timed)
1451 "----------------"
1452 (800 1000 1200 1400 1600 1800 2000))
1454 "The settings for time grid for agenda display.
1455 This is a list of three items. The first item is again a list. It contains
1456 symbols specifying conditions when the grid should be displayed:
1458 daily if the agenda shows a single day
1459 weekly if the agenda shows an entire week
1460 today show grid on current date, independent of daily/weekly display
1461 require-timed show grid only if at least one item has a time specification
1462 remove-match skip grid times already present in an entry
1464 The second item is a string which will be placed behind the grid time.
1466 The third item is a list of integers, indicating the times that should have
1467 a grid line."
1468 :group 'org-agenda-time-grid
1469 :type
1470 '(list
1471 (set :greedy t :tag "Grid Display Options"
1472 (const :tag "Show grid in single day agenda display" daily)
1473 (const :tag "Show grid in weekly agenda display" weekly)
1474 (const :tag "Always show grid for today" today)
1475 (const :tag "Show grid only if any timed entries are present"
1476 require-timed)
1477 (const :tag "Skip grid times already present in an entry"
1478 remove-match))
1479 (string :tag "Grid String")
1480 (repeat :tag "Grid Times" (integer :tag "Time"))))
1482 (defcustom org-agenda-show-current-time-in-grid t
1483 "Non-nil means show the current time in the time grid."
1484 :group 'org-agenda-time-grid
1485 :version "24.1"
1486 :type 'boolean)
1488 (defcustom org-agenda-current-time-string
1489 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1490 "The string for the current time marker in the agenda."
1491 :group 'org-agenda-time-grid
1492 :version "24.1"
1493 :type 'string)
1495 (defgroup org-agenda-sorting nil
1496 "Options concerning sorting in the Org Agenda."
1497 :tag "Org Agenda Sorting"
1498 :group 'org-agenda)
1500 (defcustom org-agenda-sorting-strategy
1501 '((agenda habit-down time-up priority-down category-keep)
1502 (todo priority-down category-keep)
1503 (tags priority-down category-keep)
1504 (search category-keep))
1505 "Sorting structure for the agenda items of a single day.
1506 This is a list of symbols which will be used in sequence to determine
1507 if an entry should be listed before another entry. The following
1508 symbols are recognized:
1510 time-up Put entries with time-of-day indications first, early first
1511 time-down Put entries with time-of-day indications first, late first
1512 timestamp-up Sort by any timestamp, early first
1513 timestamp-down Sort by any timestamp, late first
1514 scheduled-up Sort by scheduled timestamp, early first
1515 scheduled-down Sort by scheduled timestamp, late first
1516 deadline-up Sort by deadline timestamp, early first
1517 deadline-down Sort by deadline timestamp, late first
1518 ts-up Sort by active timestamp, early first
1519 ts-down Sort by active timestamp, late first
1520 tsia-up Sort by inactive timestamp, early first
1521 tsia-down Sort by inactive timestamp, late first
1522 category-keep Keep the default order of categories, corresponding to the
1523 sequence in `org-agenda-files'.
1524 category-up Sort alphabetically by category, A-Z.
1525 category-down Sort alphabetically by category, Z-A.
1526 tag-up Sort alphabetically by last tag, A-Z.
1527 tag-down Sort alphabetically by last tag, Z-A.
1528 priority-up Sort numerically by priority, high priority last.
1529 priority-down Sort numerically by priority, high priority first.
1530 todo-state-up Sort by todo state, tasks that are done last.
1531 todo-state-down Sort by todo state, tasks that are done first.
1532 effort-up Sort numerically by estimated effort, high effort last.
1533 effort-down Sort numerically by estimated effort, high effort first.
1534 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1535 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1536 habit-up Put entries that are habits first
1537 habit-down Put entries that are habits last
1538 alpha-up Sort headlines alphabetically
1539 alpha-down Sort headlines alphabetically, reversed
1541 The different possibilities will be tried in sequence, and testing stops
1542 if one comparison returns a \"not-equal\". For example, the default
1543 '(time-up category-keep priority-down)
1544 means: Pull out all entries having a specified time of day and sort them,
1545 in order to make a time schedule for the current day the first thing in the
1546 agenda listing for the day. Of the entries without a time indication, keep
1547 the grouped in categories, don't sort the categories, but keep them in
1548 the sequence given in `org-agenda-files'. Within each category sort by
1549 priority.
1551 Leaving out `category-keep' would mean that items will be sorted across
1552 categories by priority.
1554 Instead of a single list, this can also be a set of list for specific
1555 contents, with a context symbol in the car of the list, any of
1556 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1558 Custom commands can bind this variable in the options section."
1559 :group 'org-agenda-sorting
1560 :type `(choice
1561 (repeat :tag "General" ,org-sorting-choice)
1562 (list :tag "Individually"
1563 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1564 (repeat ,org-sorting-choice))
1565 (cons (const :tag "Strategy for TODO lists" todo)
1566 (repeat ,org-sorting-choice))
1567 (cons (const :tag "Strategy for Tags matches" tags)
1568 (repeat ,org-sorting-choice))
1569 (cons (const :tag "Strategy for search matches" search)
1570 (repeat ,org-sorting-choice)))))
1572 (defcustom org-agenda-cmp-user-defined nil
1573 "A function to define the comparison `user-defined'.
1574 This function must receive two arguments, agenda entry a and b.
1575 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1576 the user comparison, return nil.
1577 When this is defined, you can make `user-defined-up' and `user-defined-down'
1578 part of an agenda sorting strategy."
1579 :group 'org-agenda-sorting
1580 :type 'symbol)
1582 (defcustom org-sort-agenda-notime-is-late t
1583 "Non-nil means items without time are considered late.
1584 This is only relevant for sorting. When t, items which have no explicit
1585 time like 15:30 will be considered as 99:01, i.e. later than any items which
1586 do have a time. When nil, the default time is before 0:00. You can use this
1587 option to decide if the schedule for today should come before or after timeless
1588 agenda entries."
1589 :group 'org-agenda-sorting
1590 :type 'boolean)
1592 (defcustom org-sort-agenda-noeffort-is-high t
1593 "Non-nil means items without effort estimate are sorted as high effort.
1594 This also applies when filtering an agenda view with respect to the
1595 < or > effort operator. Then, tasks with no effort defined will be treated
1596 as tasks with high effort.
1597 When nil, such items are sorted as 0 minutes effort."
1598 :group 'org-agenda-sorting
1599 :type 'boolean)
1601 (defgroup org-agenda-line-format nil
1602 "Options concerning the entry prefix in the Org agenda display."
1603 :tag "Org Agenda Line Format"
1604 :group 'org-agenda)
1606 (defcustom org-agenda-prefix-format
1607 '((agenda . " %i %-12:c%?-12t% s")
1608 (timeline . " % s")
1609 (todo . " %i %-12:c")
1610 (tags . " %i %-12:c")
1611 (search . " %i %-12:c"))
1612 "Format specifications for the prefix of items in the agenda views.
1613 An alist with five entries, each for the different agenda types. The
1614 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1615 The values are format strings.
1617 This format works similar to a printf format, with the following meaning:
1619 %c the category of the item, \"Diary\" for entries from the diary,
1620 or as given by the CATEGORY keyword or derived from the file name
1621 %e the effort required by the item
1622 %l the level of the item (insert X space(s) if item is of level X)
1623 %i the icon category of the item, see `org-agenda-category-icon-alist'
1624 %T the last tag of the item (ignore inherited tags, which come first)
1625 %t the HH:MM time-of-day specification if one applies to the entry
1626 %s Scheduling/Deadline information, a short string
1627 %b show breadcrumbs, i.e., the names of the higher levels
1628 %(expression) Eval EXPRESSION and replace the control string
1629 by the result
1631 All specifiers work basically like the standard `%s' of printf, but may
1632 contain two additional characters: a question mark just after the `%'
1633 and a whitespace/punctuation character just before the final letter.
1635 If the first character after `%' is a question mark, the entire field
1636 will only be included if the corresponding value applies to the current
1637 entry. This is useful for fields which should have fixed width when
1638 present, but zero width when absent. For example, \"%?-12t\" will
1639 result in a 12 character time field if a time of the day is specified,
1640 but will completely disappear in entries which do not contain a time.
1642 If there is punctuation or whitespace character just before the
1643 final format letter, this character will be appended to the field
1644 value if the value is not empty. For example, the format
1645 \"%-12:c\" leads to \"Diary: \" if the category is \"Diary\". If
1646 the category is empty, no additional colon is inserted.
1648 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1649 which means:
1651 - Indent the line with two space characters
1652 - Give the category a 12 chars wide field, padded with whitespace on
1653 the right (because of `-'). Append a colon if there is a category
1654 (because of `:').
1655 - If there is a time-of-day, put it into a 12 chars wide field. If no
1656 time, don't put in an empty field, just skip it (because of '?').
1657 - Finally, put the scheduling information.
1659 See also the variables `org-agenda-remove-times-when-in-prefix' and
1660 `org-agenda-remove-tags'.
1662 Custom commands can set this variable in the options section."
1663 :type '(choice
1664 (string :tag "General format")
1665 (list :greedy t :tag "View dependent"
1666 (cons (const agenda) (string :tag "Format"))
1667 (cons (const timeline) (string :tag "Format"))
1668 (cons (const todo) (string :tag "Format"))
1669 (cons (const tags) (string :tag "Format"))
1670 (cons (const search) (string :tag "Format"))))
1671 :group 'org-agenda-line-format)
1673 (defvar org-prefix-format-compiled nil
1674 "The compiled prefix format and associated variables.
1675 This is a list where first element is a list of variable bindings, and second
1676 element is the compiled format expression. See the variable
1677 `org-agenda-prefix-format'.")
1679 (defcustom org-agenda-todo-keyword-format "%-1s"
1680 "Format for the TODO keyword in agenda lines.
1681 Set this to something like \"%-12s\" if you want all TODO keywords
1682 to occupy a fixed space in the agenda display."
1683 :group 'org-agenda-line-format
1684 :type 'string)
1686 (defcustom org-agenda-diary-sexp-prefix nil
1687 "A regexp that matches part of a diary sexp entry
1688 which should be treated as scheduling/deadline information in
1689 `org-agenda'.
1691 For example, you can use this to extract the `diary-remind-message' from
1692 `diary-remind' entries."
1693 :group 'org-agenda-line-format
1694 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1696 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1697 "Text preceding timerange entries in the agenda view.
1698 This is a list with two strings. The first applies when the range
1699 is entirely on one day. The second applies if the range spans several days.
1700 The strings may have two \"%d\" format specifiers which will be filled
1701 with the sequence number of the days, and the total number of days in the
1702 range, respectively."
1703 :group 'org-agenda-line-format
1704 :type '(list
1705 (string :tag "Deadline today ")
1706 (choice :tag "Deadline relative"
1707 (string :tag "Format string")
1708 (function))))
1710 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1711 "Text preceding scheduled items in the agenda view.
1712 This is a list with two strings. The first applies when the item is
1713 scheduled on the current day. The second applies when it has been scheduled
1714 previously, it may contain a %d indicating that this is the nth time that
1715 this item is scheduled, due to automatic rescheduling of unfinished items
1716 for the following day. So this number is one larger than the number of days
1717 that passed since this item was scheduled first."
1718 :group 'org-agenda-line-format
1719 :version "24.4"
1720 :package-version '(Org . "8.0")
1721 :type '(list
1722 (string :tag "Scheduled today ")
1723 (string :tag "Scheduled previously")))
1725 (defcustom org-agenda-inactive-leader "["
1726 "Text preceding item pulled into the agenda by inactive time stamps.
1727 These entries are added to the agenda when pressing \"[\"."
1728 :group 'org-agenda-line-format
1729 :version "24.1"
1730 :type 'string)
1732 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: " "%2d d. ago: ")
1733 "Text preceding deadline items in the agenda view.
1734 This is a list with three strings. The first applies when the item has its
1735 deadline on the current day. The second applies when the deadline is in the
1736 future, the third one when it is in the past. The strings may contain %d
1737 to capture the number of days."
1738 :group 'org-agenda-line-format
1739 :version "24.4"
1740 :package-version '(Org . "8.0")
1741 :type '(list
1742 (string :tag "Deadline today ")
1743 (string :tag "Deadline in the future ")
1744 (string :tag "Deadline in the past ")))
1746 (defcustom org-agenda-remove-times-when-in-prefix t
1747 "Non-nil means remove duplicate time specifications in agenda items.
1748 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1749 time-of-day specification in a headline or diary entry is extracted and
1750 placed into the prefix. If this option is non-nil, the original specification
1751 \(a timestamp or -range, or just a plain time(range) specification like
1752 11:30-4pm) will be removed for agenda display. This makes the agenda less
1753 cluttered.
1754 The option can be t or nil. It may also be the symbol `beg', indicating
1755 that the time should only be removed when it is located at the beginning of
1756 the headline/diary entry."
1757 :group 'org-agenda-line-format
1758 :type '(choice
1759 (const :tag "Always" t)
1760 (const :tag "Never" nil)
1761 (const :tag "When at beginning of entry" beg)))
1763 (defcustom org-agenda-remove-timeranges-from-blocks nil
1764 "Non-nil means remove time ranges specifications in agenda
1765 items that span on several days."
1766 :group 'org-agenda-line-format
1767 :version "24.1"
1768 :type 'boolean)
1770 (defcustom org-agenda-default-appointment-duration nil
1771 "Default duration for appointments that only have a starting time.
1772 When nil, no duration is specified in such cases.
1773 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1774 :group 'org-agenda-line-format
1775 :type '(choice
1776 (integer :tag "Minutes")
1777 (const :tag "No default duration")))
1779 (defcustom org-agenda-show-inherited-tags t
1780 "Non-nil means show inherited tags in each agenda line.
1782 When this option is set to `always', it take precedences over
1783 `org-agenda-use-tag-inheritance' and inherited tags are shown
1784 in every agenda.
1786 When this option is set to t (the default), inherited tags are
1787 shown when they are available, i.e. when the value of
1788 `org-agenda-use-tag-inheritance' has been taken into account.
1790 This can be set to a list of agenda types in which the agenda
1791 must display the inherited tags. Available types are `todo',
1792 `agenda', `search' and `timeline'.
1794 When set to nil, never show inherited tags in agenda lines."
1795 :group 'org-agenda-line-format
1796 :group 'org-agenda
1797 :version "24.3"
1798 :type '(choice
1799 (const :tag "Show inherited tags when available" t)
1800 (const :tag "Always show inherited tags" always)
1801 (repeat :tag "Show inherited tags only in selected agenda types"
1802 (symbol :tag "Agenda type"))))
1804 (defcustom org-agenda-use-tag-inheritance '(todo search timeline agenda)
1805 "List of agenda view types where to use tag inheritance.
1807 In tags/tags-todo/tags-tree agenda views, tag inheritance is
1808 controlled by `org-use-tag-inheritance'. In other agenda types,
1809 `org-use-tag-inheritance' is not used for the selection of the
1810 agenda entries. Still, you may want the agenda to be aware of
1811 the inherited tags anyway, e.g. for later tag filtering.
1813 Allowed value are `todo', `search', `timeline' and `agenda'.
1815 This variable has no effect if `org-agenda-show-inherited-tags'
1816 is set to `always'. In that case, the agenda is aware of those
1817 tags.
1819 The default value sets tags in every agenda type. Setting this
1820 option to nil will speed up non-tags agenda view a lot."
1821 :group 'org-agenda
1822 :version "24.3"
1823 :type '(choice
1824 (const :tag "Use tag inheritance in all agenda types" t)
1825 (repeat :tag "Use tag inheritance in selected agenda types"
1826 (symbol :tag "Agenda type"))))
1828 (defcustom org-agenda-hide-tags-regexp nil
1829 "Regular expression used to filter away specific tags in agenda views.
1830 This means that these tags will be present, but not be shown in the agenda
1831 line. Secondary filtering will still work on the hidden tags.
1832 Nil means don't hide any tags."
1833 :group 'org-agenda-line-format
1834 :type '(choice
1835 (const :tag "Hide none" nil)
1836 (string :tag "Regexp ")))
1838 (defcustom org-agenda-remove-tags nil
1839 "Non-nil means remove the tags from the headline copy in the agenda.
1840 When this is the symbol `prefix', only remove tags when
1841 `org-agenda-prefix-format' contains a `%T' specifier."
1842 :group 'org-agenda-line-format
1843 :type '(choice
1844 (const :tag "Always" t)
1845 (const :tag "Never" nil)
1846 (const :tag "When prefix format contains %T" prefix)))
1848 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1849 'org-agenda-remove-tags)
1851 (defcustom org-agenda-tags-column -80
1852 "Shift tags in agenda items to this column.
1853 If this number is positive, it specifies the column. If it is negative,
1854 it means that the tags should be flushright to that column. For example,
1855 -80 works well for a normal 80 character screen."
1856 :group 'org-agenda-line-format
1857 :type 'integer)
1859 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column)
1861 (defcustom org-agenda-fontify-priorities 'cookies
1862 "Non-nil means highlight low and high priorities in agenda.
1863 When t, the highest priority entries are bold, lowest priority italic.
1864 However, settings in `org-priority-faces' will overrule these faces.
1865 When this variable is the symbol `cookies', only fontify the
1866 cookies, not the entire task.
1867 This may also be an association list of priority faces, whose
1868 keys are the character values of `org-highest-priority',
1869 `org-default-priority', and `org-lowest-priority' (the default values
1870 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1871 color as a string, or a list like `(:background \"Red\")'.
1872 If it is a color, the variable `org-faces-easy-properties'
1873 determines if it is a foreground or a background color."
1874 :group 'org-agenda-line-format
1875 :type '(choice
1876 (const :tag "Never" nil)
1877 (const :tag "Defaults" t)
1878 (const :tag "Cookies only" cookies)
1879 (repeat :tag "Specify"
1880 (list (character :tag "Priority" :value ?A)
1881 (choice :tag "Face "
1882 (string :tag "Color")
1883 (sexp :tag "Face"))))))
1885 (defcustom org-agenda-day-face-function nil
1886 "Function called to determine what face should be used to display a day.
1887 The only argument passed to that function is the day. It should
1888 returns a face, or nil if does not want to specify a face and let
1889 the normal rules apply."
1890 :group 'org-agenda-line-format
1891 :version "24.1"
1892 :type '(choice (const nil) (function)))
1894 (defcustom org-agenda-category-icon-alist nil
1895 "Alist of category icon to be displayed in agenda views.
1897 Each entry should have the following format:
1899 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1901 Where CATEGORY-REGEXP is a regexp matching the categories where
1902 the icon should be displayed.
1903 FILE-OR-DATA either a file path or a string containing image data.
1905 The other fields can be omitted safely if not needed:
1906 TYPE indicates the image type.
1907 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1908 image data.
1909 PROPS are additional image attributes to assign to the image,
1910 like, e.g. `:ascent center'.
1912 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1914 If you want to set the display properties yourself, just put a
1915 list as second element:
1917 (CATEGORY-REGEXP (MY PROPERTY LIST))
1919 For example, to display a 16px horizontal space for Emacs
1920 category, you can use:
1922 (\"Emacs\" \\='(space . (:width (16))))"
1923 :group 'org-agenda-line-format
1924 :version "24.1"
1925 :type '(alist :key-type (string :tag "Regexp matching category")
1926 :value-type (choice (list :tag "Icon"
1927 (string :tag "File or data")
1928 (symbol :tag "Type")
1929 (boolean :tag "Data?")
1930 (repeat :tag "Extra image properties" :inline t symbol))
1931 (list :tag "Display properties" sexp))))
1933 (defgroup org-agenda-column-view nil
1934 "Options concerning column view in the agenda."
1935 :tag "Org Agenda Column View"
1936 :group 'org-agenda)
1938 (defcustom org-agenda-view-columns-initially nil
1939 "When non-nil, switch to columns view right after creating the agenda."
1940 :group 'org-agenda-column-view
1941 :type 'boolean
1942 :version "25.1"
1943 :package-version '(Org . "9.0")
1944 :safe #'booleanp)
1946 (defcustom org-agenda-columns-show-summaries t
1947 "Non-nil means show summaries for columns displayed in the agenda view."
1948 :group 'org-agenda-column-view
1949 :type 'boolean)
1951 (defcustom org-agenda-columns-compute-summary-properties t
1952 "Non-nil means recompute all summary properties before column view.
1953 When column view in the agenda is listing properties that have a summary
1954 operator, it can go to all relevant buffers and recompute the summaries
1955 there. This can mean overhead for the agenda column view, but is necessary
1956 to have thing up to date.
1957 As a special case, a CLOCKSUM property also makes sure that the clock
1958 computations are current."
1959 :group 'org-agenda-column-view
1960 :type 'boolean)
1962 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1963 "Non-nil means the duration of an appointment will add to day effort.
1964 The property to which appointment durations will be added is the one given
1965 in the option `org-effort-property'. If an appointment does not have
1966 an end time, `org-agenda-default-appointment-duration' will be used. If that
1967 is not set, an appointment without end time will not contribute to the time
1968 estimate."
1969 :group 'org-agenda-column-view
1970 :type 'boolean)
1972 (defcustom org-agenda-auto-exclude-function nil
1973 "A function called with a tag to decide if it is filtered on \
1974 \\<org-agenda-mode-map>`\\[org-agenda-filter-by-tag] RET'.
1975 The sole argument to the function, which is called once for each
1976 possible tag, is a string giving the name of the tag. The
1977 function should return either nil if the tag should be included
1978 as normal, or \"-<TAG>\" to exclude the tag.
1979 Note that for the purpose of tag filtering, only the lower-case version of
1980 all tags will be considered, so that this function will only ever see
1981 the lower-case version of all tags."
1982 :group 'org-agenda
1983 :type '(choice (const nil) (function)))
1985 (defcustom org-agenda-bulk-custom-functions nil
1986 "Alist of characters and custom functions for bulk actions.
1987 For example, this value makes those two functions available:
1989 \\='((?R set-category)
1990 (?C bulk-cut))
1992 With selected entries in an agenda buffer, `B R' will call
1993 the custom function `set-category' on the selected entries.
1994 Note that functions in this alist don't need to be quoted."
1995 :type '(alist :key-type character :value-type (group function))
1996 :version "24.1"
1997 :group 'org-agenda)
1999 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
2000 "Execute BODY with point at location given by `org-hd-marker' property.
2001 If STRING is non-nil, the text property will be fetched from position 0
2002 in that string. If STRING is nil, it will be fetched from the beginning
2003 of the current line."
2004 (org-with-gensyms (marker)
2005 `(let ((,marker (get-text-property (if ,string 0 (point-at-bol))
2006 'org-hd-marker ,string)))
2007 (with-current-buffer (marker-buffer ,marker)
2008 (save-excursion
2009 (goto-char ,marker)
2010 ,@body)))))
2011 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
2013 (defun org-add-agenda-custom-command (entry)
2014 "Replace or add a command in `org-agenda-custom-commands'.
2015 This is mostly for hacking and trying a new command - once the command
2016 works you probably want to add it to `org-agenda-custom-commands' for good."
2017 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
2018 (if ass
2019 (setcdr ass (cdr entry))
2020 (push entry org-agenda-custom-commands))))
2022 ;;; Define the org-agenda-mode
2024 (defvar org-agenda-mode-map (make-sparse-keymap)
2025 "Keymap for `org-agenda-mode'.")
2026 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map)
2028 (defvar org-agenda-menu) ; defined later in this file.
2029 (defvar org-agenda-restrict nil) ; defined later in this file.
2030 (defvar org-agenda-follow-mode nil)
2031 (defvar org-agenda-entry-text-mode nil)
2032 (defvar org-agenda-clockreport-mode nil)
2033 (defvar org-agenda-show-log nil)
2034 (defvar org-agenda-redo-command nil)
2035 (defvar org-agenda-query-string nil)
2036 (defvar org-agenda-mode-hook nil
2037 "Hook run after `org-agenda-mode' is turned on.
2038 The buffer is still writable when this hook is called.")
2039 (defvar org-agenda-type nil)
2040 (defvar org-agenda-force-single-file nil)
2041 (defvar org-agenda-bulk-marked-entries nil
2042 "List of markers that refer to marked entries in the agenda.")
2043 (defvar org-agenda-current-date nil
2044 "Active date when building the agenda.")
2046 ;;; Multiple agenda buffers support
2048 (defcustom org-agenda-sticky nil
2049 "Non-nil means agenda q key will bury agenda buffers.
2050 Agenda commands will then show existing buffer instead of generating new ones.
2051 When nil, `q' will kill the single agenda buffer."
2052 :group 'org-agenda
2053 :version "24.3"
2054 :type 'boolean)
2057 ;;;###autoload
2058 (defun org-toggle-sticky-agenda (&optional arg)
2059 "Toggle `org-agenda-sticky'."
2060 (interactive "P")
2061 (let ((new-value (if arg
2062 (> (prefix-numeric-value arg) 0)
2063 (not org-agenda-sticky))))
2064 (if (equal new-value org-agenda-sticky)
2065 (and (called-interactively-p 'interactive)
2066 (message "Sticky agenda was already %s"
2067 (if org-agenda-sticky "enabled" "disabled")))
2068 (setq org-agenda-sticky new-value)
2069 (org-agenda-kill-all-agenda-buffers)
2070 (and (called-interactively-p 'interactive)
2071 (message "Sticky agenda %s"
2072 (if org-agenda-sticky "enabled" "disabled"))))))
2074 (defvar org-agenda-buffer nil
2075 "Agenda buffer currently being generated.")
2077 (defvar org-agenda-last-prefix-arg nil)
2078 (defvar org-agenda-this-buffer-name nil)
2079 (defvar org-agenda-doing-sticky-redo nil)
2080 (defvar org-agenda-this-buffer-is-sticky nil)
2081 (defvar org-agenda-last-indirect-buffer nil
2082 "Last buffer loaded by `org-agenda-tree-to-indirect-buffer'.")
2084 (defconst org-agenda-local-vars
2085 '(org-agenda-this-buffer-name
2086 org-agenda-undo-list
2087 org-agenda-pending-undo-list
2088 org-agenda-follow-mode
2089 org-agenda-entry-text-mode
2090 org-agenda-clockreport-mode
2091 org-agenda-show-log
2092 org-agenda-redo-command
2093 org-agenda-query-string
2094 org-agenda-type
2095 org-agenda-bulk-marked-entries
2096 org-agenda-undo-has-started-in
2097 org-agenda-info
2098 org-agenda-pre-window-conf
2099 org-agenda-columns-active
2100 org-agenda-tag-filter
2101 org-agenda-category-filter
2102 org-agenda-top-headline-filter
2103 org-agenda-regexp-filter
2104 org-agenda-effort-filter
2105 org-agenda-markers
2106 org-agenda-last-search-view-search-was-boolean
2107 org-agenda-last-indirect-buffer
2108 org-agenda-filtered-by-category
2109 org-agenda-filter-form
2110 org-agenda-cycle-counter
2111 org-agenda-last-prefix-arg)
2112 "Variables that must be local in agenda buffers to allow multiple buffers.")
2114 (defun org-agenda-mode ()
2115 "Mode for time-sorted view on action items in Org files.
2117 The following commands are available:
2119 \\{org-agenda-mode-map}"
2120 (interactive)
2121 (cond (org-agenda-doing-sticky-redo
2122 ;; Refreshing sticky agenda-buffer
2124 ;; Preserve the value of `org-agenda-local-vars' variables,
2125 ;; while letting `kill-all-local-variables' kill the rest
2126 (let ((save (buffer-local-variables)))
2127 (kill-all-local-variables)
2128 (mapc 'make-local-variable org-agenda-local-vars)
2129 (dolist (elem save)
2130 (let ((var (car elem))
2131 (val (cdr elem)))
2132 (when (and val
2133 (member var org-agenda-local-vars))
2134 (set var val)))))
2135 (setq-local org-agenda-this-buffer-is-sticky t))
2136 (org-agenda-sticky
2137 ;; Creating a sticky Agenda buffer for the first time
2138 (kill-all-local-variables)
2139 (mapc 'make-local-variable org-agenda-local-vars)
2140 (setq-local org-agenda-this-buffer-is-sticky t))
2142 ;; Creating a non-sticky agenda buffer
2143 (kill-all-local-variables)
2144 (setq-local org-agenda-this-buffer-is-sticky nil)))
2145 (setq org-agenda-undo-list nil
2146 org-agenda-pending-undo-list nil
2147 org-agenda-bulk-marked-entries nil)
2148 (setq major-mode 'org-agenda-mode)
2149 ;; Keep global-font-lock-mode from turning on font-lock-mode
2150 (setq-local font-lock-global-modes (list 'not major-mode))
2151 (setq mode-name "Org-Agenda")
2152 (setq indent-tabs-mode nil)
2153 (use-local-map org-agenda-mode-map)
2154 (easy-menu-add org-agenda-menu)
2155 (if org-startup-truncated (setq truncate-lines t))
2156 (setq-local line-move-visual nil)
2157 (add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
2158 (add-hook 'pre-command-hook 'org-unhighlight nil 'local)
2159 ;; Make sure properties are removed when copying text
2160 (add-hook 'filter-buffer-substring-functions
2161 (lambda (fun start end delete)
2162 (substring-no-properties (funcall fun start end delete)))
2163 nil t)
2164 (unless org-agenda-keep-modes
2165 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
2166 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode))
2167 (setq org-agenda-show-log org-agenda-start-with-log-mode)
2168 (setq org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode)
2169 (add-to-invisibility-spec '(org-filtered))
2170 (add-to-invisibility-spec '(org-link))
2171 (easy-menu-change
2172 '("Agenda") "Agenda Files"
2173 (append
2174 (list
2175 (vector
2176 (if (get 'org-agenda-files 'org-restrict)
2177 "Restricted to single file"
2178 "Edit File List")
2179 '(org-edit-agenda-file-list)
2180 (not (get 'org-agenda-files 'org-restrict)))
2181 "--")
2182 (mapcar 'org-file-menu-entry (org-agenda-files))))
2183 (org-agenda-set-mode-name)
2184 (apply
2185 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2186 (list 'org-agenda-mode-hook)))
2188 (substitute-key-definition 'undo 'org-agenda-undo
2189 org-agenda-mode-map global-map)
2190 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2191 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2192 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2193 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2194 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2195 (org-defkey org-agenda-mode-map [(meta down)] 'org-agenda-drag-line-forward)
2196 (org-defkey org-agenda-mode-map [(meta up)] 'org-agenda-drag-line-backward)
2197 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2198 (org-defkey org-agenda-mode-map "\M-m" 'org-agenda-bulk-toggle)
2199 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2200 (org-defkey org-agenda-mode-map "\M-*" 'org-agenda-bulk-toggle-all)
2201 (org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
2202 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2203 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2204 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2205 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2206 (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2207 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2208 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2209 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2210 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2211 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2212 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2213 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2214 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2215 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2216 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2217 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2218 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2219 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2220 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2221 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2222 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2223 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2224 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2225 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2226 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2227 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2228 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2229 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2230 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2231 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2232 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2233 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2234 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2235 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2236 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2237 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2238 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2239 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2241 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2242 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2243 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2244 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2245 (while l (org-defkey org-agenda-mode-map
2246 (int-to-string (pop l)) 'digit-argument)))
2248 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2249 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2250 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2251 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2252 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2253 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2254 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2255 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2256 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2257 (org-defkey org-agenda-mode-map "g" (lambda () (interactive) (org-agenda-redo t)))
2258 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2259 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2260 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2261 'org-clock-modify-effort-estimate)
2262 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2263 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2264 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2265 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2266 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2267 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2268 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2269 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2270 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2271 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2272 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2273 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2274 (substitute-key-definition 'next-line 'org-agenda-next-line
2275 org-agenda-mode-map global-map)
2276 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2277 org-agenda-mode-map global-map)
2278 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2279 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2280 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2281 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2282 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2283 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2284 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2285 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2286 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2287 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2288 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2289 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2290 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2291 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2292 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2293 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2294 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2295 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2296 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2297 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2298 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2299 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2300 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2301 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2302 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2303 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2304 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2305 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2306 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2307 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2309 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2310 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2311 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2312 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2313 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2314 (org-defkey org-agenda-mode-map "_" 'org-agenda-filter-by-effort)
2315 (org-defkey org-agenda-mode-map "=" 'org-agenda-filter-by-regexp)
2316 (org-defkey org-agenda-mode-map "|" 'org-agenda-filter-remove-all)
2317 (org-defkey org-agenda-mode-map "~" 'org-agenda-limit-interactively)
2318 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2319 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-headline)
2320 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2321 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2322 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2323 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2325 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2326 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2328 (define-key org-agenda-mode-map [remap forward-paragraph] 'org-agenda-forward-block)
2329 (define-key org-agenda-mode-map [remap backward-paragraph] 'org-agenda-backward-block)
2331 (when org-agenda-mouse-1-follows-link
2332 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2333 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2334 '("Agenda"
2335 ("Agenda Files")
2336 "--"
2337 ("Agenda Dates"
2338 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2339 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2340 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2341 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2342 "--"
2343 ("View"
2344 ["Day View" org-agenda-day-view
2345 :active (org-agenda-check-type nil 'agenda)
2346 :style radio :selected (eq org-agenda-current-span 'day)
2347 :keys "v d (or just d)"]
2348 ["Week View" org-agenda-week-view
2349 :active (org-agenda-check-type nil 'agenda)
2350 :style radio :selected (eq org-agenda-current-span 'week)
2351 :keys "v w"]
2352 ["Fortnight View" org-agenda-fortnight-view
2353 :active (org-agenda-check-type nil 'agenda)
2354 :style radio :selected (eq org-agenda-current-span 'fortnight)
2355 :keys "v f"]
2356 ["Month View" org-agenda-month-view
2357 :active (org-agenda-check-type nil 'agenda)
2358 :style radio :selected (eq org-agenda-current-span 'month)
2359 :keys "v m"]
2360 ["Year View" org-agenda-year-view
2361 :active (org-agenda-check-type nil 'agenda)
2362 :style radio :selected (eq org-agenda-current-span 'year)
2363 :keys "v y"]
2364 "--"
2365 ["Include Diary" org-agenda-toggle-diary
2366 :style toggle :selected org-agenda-include-diary
2367 :active (org-agenda-check-type nil 'agenda)]
2368 ["Include Deadlines" org-agenda-toggle-deadlines
2369 :style toggle :selected org-agenda-include-deadlines
2370 :active (org-agenda-check-type nil 'agenda)]
2371 ["Use Time Grid" org-agenda-toggle-time-grid
2372 :style toggle :selected org-agenda-use-time-grid
2373 :active (org-agenda-check-type nil 'agenda)]
2374 "--"
2375 ["Show clock report" org-agenda-clockreport-mode
2376 :style toggle :selected org-agenda-clockreport-mode
2377 :active (org-agenda-check-type nil 'agenda)]
2378 ["Show some entry text" org-agenda-entry-text-mode
2379 :style toggle :selected org-agenda-entry-text-mode
2380 :active t]
2381 "--"
2382 ["Show Logbook entries" org-agenda-log-mode
2383 :style toggle :selected org-agenda-show-log
2384 :active (org-agenda-check-type nil 'agenda 'timeline)
2385 :keys "v l (or just l)"]
2386 ["Include archived trees" org-agenda-archives-mode
2387 :style toggle :selected org-agenda-archives-mode :active t
2388 :keys "v a"]
2389 ["Include archive files" (org-agenda-archives-mode t)
2390 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2391 :keys "v A"]
2392 "--"
2393 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2394 ["Write view to file" org-agenda-write t]
2395 ["Rebuild buffer" org-agenda-redo t]
2396 ["Save all Org buffers" org-save-all-org-buffers t]
2397 "--"
2398 ["Show original entry" org-agenda-show t]
2399 ["Go To (other window)" org-agenda-goto t]
2400 ["Go To (this window)" org-agenda-switch-to t]
2401 ["Capture with cursor date" org-agenda-capture t]
2402 ["Follow Mode" org-agenda-follow-mode
2403 :style toggle :selected org-agenda-follow-mode :active t]
2404 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2405 "--"
2406 ("TODO"
2407 ["Cycle TODO" org-agenda-todo t]
2408 ["Next TODO set" org-agenda-todo-nextset t]
2409 ["Previous TODO set" org-agenda-todo-previousset t]
2410 ["Add note" org-agenda-add-note t])
2411 ("Archive/Refile/Delete"
2412 ["Archive default" org-agenda-archive-default t]
2413 ["Archive default" org-agenda-archive-default-with-confirmation t]
2414 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2415 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2416 ["Archive subtree" org-agenda-archive t]
2417 "--"
2418 ["Refile" org-agenda-refile t]
2419 "--"
2420 ["Delete subtree" org-agenda-kill t])
2421 ("Bulk action"
2422 ["Mark entry" org-agenda-bulk-mark t]
2423 ["Mark all" org-agenda-bulk-mark-all t]
2424 ["Unmark entry" org-agenda-bulk-unmark t]
2425 ["Unmark all" org-agenda-bulk-unmark-all :active t :keys "U"]
2426 ["Toggle mark" org-agenda-bulk-toggle t]
2427 ["Toggle all" org-agenda-bulk-toggle-all t]
2428 ["Mark regexp" org-agenda-bulk-mark-regexp t])
2429 ["Act on all marked" org-agenda-bulk-action t]
2430 "--"
2431 ("Tags and Properties"
2432 ["Show all Tags" org-agenda-show-tags t]
2433 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2434 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2435 "--"
2436 ["Column View" org-columns t])
2437 ("Deadline/Schedule"
2438 ["Schedule" org-agenda-schedule t]
2439 ["Set Deadline" org-agenda-deadline t]
2440 "--"
2441 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2442 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2443 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2444 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2445 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2446 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2447 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2448 ("Clock and Effort"
2449 ["Clock in" org-agenda-clock-in t]
2450 ["Clock out" org-agenda-clock-out t]
2451 ["Clock cancel" org-agenda-clock-cancel t]
2452 ["Goto running clock" org-clock-goto t]
2453 "--"
2454 ["Set Effort" org-agenda-set-effort t]
2455 ["Change clocked effort" org-clock-modify-effort-estimate
2456 (org-clock-is-active)])
2457 ("Priority"
2458 ["Set Priority" org-agenda-priority t]
2459 ["Increase Priority" org-agenda-priority-up t]
2460 ["Decrease Priority" org-agenda-priority-down t]
2461 ["Show Priority" org-show-priority t])
2462 ("Calendar/Diary"
2463 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2464 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2465 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2466 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2467 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2468 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2469 "--"
2470 ["Create iCalendar File" org-icalendar-combine-agenda-files t])
2471 "--"
2472 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2473 "--"
2474 ("MobileOrg"
2475 ["Push Files and Views" org-mobile-push t]
2476 ["Get Captured and Flagged" org-mobile-pull t]
2477 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2478 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2479 "--"
2480 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2481 "--"
2482 ["Quit" org-agenda-quit t]
2483 ["Exit and Release Buffers" org-agenda-exit t]
2486 ;;; Agenda undo
2488 (defvar org-agenda-allow-remote-undo t
2489 "Non-nil means allow remote undo from the agenda buffer.")
2490 (defvar org-agenda-undo-has-started-in nil
2491 "Buffers that have already seen `undo-start' in the current undo sequence.")
2493 (defun org-agenda-undo ()
2494 "Undo a remote editing step in the agenda.
2495 This undoes changes both in the agenda buffer and in the remote buffer
2496 that have been changed along."
2497 (interactive)
2498 (or org-agenda-allow-remote-undo
2499 (user-error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2500 (if (not (eq this-command last-command))
2501 (setq org-agenda-undo-has-started-in nil
2502 org-agenda-pending-undo-list org-agenda-undo-list))
2503 (if (not org-agenda-pending-undo-list)
2504 (user-error "No further undo information"))
2505 (let* ((entry (pop org-agenda-pending-undo-list))
2506 buf line cmd rembuf)
2507 (setq cmd (pop entry) line (pop entry))
2508 (setq rembuf (nth 2 entry))
2509 (org-with-remote-undo rembuf
2510 (while (bufferp (setq buf (pop entry)))
2511 (if (pop entry)
2512 (with-current-buffer buf
2513 (let ((last-undo-buffer buf)
2514 (inhibit-read-only t))
2515 (unless (memq buf org-agenda-undo-has-started-in)
2516 (push buf org-agenda-undo-has-started-in)
2517 (make-local-variable 'pending-undo-list)
2518 (undo-start))
2519 (while (and pending-undo-list
2520 (listp pending-undo-list)
2521 (not (car pending-undo-list)))
2522 (pop pending-undo-list))
2523 (undo-more 1))))))
2524 (org-goto-line line)
2525 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2527 (defun org-verify-change-for-undo (l1 l2)
2528 "Verify that a real change occurred between the undo lists L1 and L2."
2529 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2530 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2531 (not (eq l1 l2)))
2533 ;;; Agenda dispatch
2535 (defvar org-agenda-restrict-begin (make-marker))
2536 (defvar org-agenda-restrict-end (make-marker))
2537 (defvar org-agenda-last-dispatch-buffer nil)
2538 (defvar org-agenda-overriding-restriction nil)
2540 (defcustom org-agenda-custom-commands-contexts nil
2541 "Alist of custom agenda keys and contextual rules.
2543 For example, if you have a custom agenda command \"p\" and you
2544 want this command to be accessible only from plain text files,
2545 use this:
2547 \\='((\"p\" ((in-file . \"\\\\.txt\\\\'\"))))
2549 Here are the available contexts definitions:
2551 in-file: command displayed only in matching files
2552 in-mode: command displayed only in matching modes
2553 not-in-file: command not displayed in matching files
2554 not-in-mode: command not displayed in matching modes
2555 in-buffer: command displayed only in matching buffers
2556 not-in-buffer: command not displayed in matching buffers
2557 [function]: a custom function taking no argument
2559 If you define several checks, the agenda command will be
2560 accessible if there is at least one valid check.
2562 You can also bind a key to another agenda custom command
2563 depending on contextual rules.
2565 \\='((\"p\" \"q\" ((in-file . \"\\\\.txt\\\\'\"))))
2567 Here it means: in .txt files, use \"p\" as the key for the
2568 agenda command otherwise associated with \"q\". (The command
2569 originally associated with \"q\" is not displayed to avoid
2570 duplicates.)"
2571 :version "24.3"
2572 :group 'org-agenda-custom-commands
2573 :type '(repeat (list :tag "Rule"
2574 (string :tag " Agenda key")
2575 (string :tag "Replace by command")
2576 (repeat :tag "Available when"
2577 (choice
2578 (cons :tag "Condition"
2579 (choice
2580 (const :tag "In file" in-file)
2581 (const :tag "Not in file" not-in-file)
2582 (const :tag "In buffer" in-buffer)
2583 (const :tag "Not in buffer" not-in-buffer)
2584 (const :tag "In mode" in-mode)
2585 (const :tag "Not in mode" not-in-mode))
2586 (regexp))
2587 (function :tag "Custom function"))))))
2589 (defcustom org-agenda-max-entries nil
2590 "Maximum number of entries to display in an agenda.
2591 This can be nil (no limit) or an integer or an alist of agenda
2592 types with an associated number of entries to display in this
2593 type."
2594 :version "24.4"
2595 :package-version '(Org . "8.0")
2596 :group 'org-agenda-custom-commands
2597 :type '(choice (symbol :tag "No limit" nil)
2598 (integer :tag "Max number of entries")
2599 (repeat
2600 (cons (choice :tag "Agenda type"
2601 (const agenda)
2602 (const todo)
2603 (const tags)
2604 (const search)
2605 (const timeline))
2606 (integer :tag "Max number of entries")))))
2608 (defcustom org-agenda-max-todos nil
2609 "Maximum number of TODOs to display in an agenda.
2610 This can be nil (no limit) or an integer or an alist of agenda
2611 types with an associated number of entries to display in this
2612 type."
2613 :version "24.4"
2614 :package-version '(Org . "8.0")
2615 :group 'org-agenda-custom-commands
2616 :type '(choice (symbol :tag "No limit" nil)
2617 (integer :tag "Max number of TODOs")
2618 (repeat
2619 (cons (choice :tag "Agenda type"
2620 (const agenda)
2621 (const todo)
2622 (const tags)
2623 (const search)
2624 (const timeline))
2625 (integer :tag "Max number of TODOs")))))
2627 (defcustom org-agenda-max-tags nil
2628 "Maximum number of tagged entries to display in an agenda.
2629 This can be nil (no limit) or an integer or an alist of agenda
2630 types with an associated number of entries to display in this
2631 type."
2632 :version "24.4"
2633 :package-version '(Org . "8.0")
2634 :group 'org-agenda-custom-commands
2635 :type '(choice (symbol :tag "No limit" nil)
2636 (integer :tag "Max number of tagged entries")
2637 (repeat
2638 (cons (choice :tag "Agenda type"
2639 (const agenda)
2640 (const todo)
2641 (const tags)
2642 (const search)
2643 (const timeline))
2644 (integer :tag "Max number of tagged entries")))))
2646 (defcustom org-agenda-max-effort nil
2647 "Maximum cumulated effort duration for the agenda.
2648 This can be nil (no limit) or a number of minutes (as an integer)
2649 or an alist of agenda types with an associated number of minutes
2650 to limit entries to in this type."
2651 :version "24.4"
2652 :package-version '(Org . "8.0")
2653 :group 'org-agenda-custom-commands
2654 :type '(choice (symbol :tag "No limit" nil)
2655 (integer :tag "Max number of minutes")
2656 (repeat
2657 (cons (choice :tag "Agenda type"
2658 (const agenda)
2659 (const todo)
2660 (const tags)
2661 (const search)
2662 (const timeline))
2663 (integer :tag "Max number of minutes")))))
2665 (defvar org-agenda-keep-restricted-file-list nil)
2666 (defvar org-keys nil)
2667 (defvar org-match nil)
2668 ;;;###autoload
2669 (defun org-agenda (&optional arg org-keys restriction)
2670 "Dispatch agenda commands to collect entries to the agenda buffer.
2671 Prompts for a command to execute. Any prefix arg will be passed
2672 on to the selected command. The default selections are:
2674 a Call `org-agenda-list' to display the agenda for current day or week.
2675 t Call `org-todo-list' to display the global todo list.
2676 T Call `org-todo-list' to display the global todo list, select only
2677 entries with a specific TODO keyword (the user gets a prompt).
2678 m Call `org-tags-view' to display headlines with tags matching
2679 a condition (the user is prompted for the condition).
2680 M Like `m', but select only TODO entries, no ordinary headlines.
2681 L Create a timeline for the current buffer.
2682 e Export views to associated files.
2683 s Search entries for keywords.
2684 S Search entries for keywords, only with TODO keywords.
2685 / Multi occur across all agenda files and also files listed
2686 in `org-agenda-text-search-extra-files'.
2687 < Restrict agenda commands to buffer, subtree, or region.
2688 Press several times to get the desired effect.
2689 > Remove a previous restriction.
2690 # List \"stuck\" projects.
2691 ! Configure what \"stuck\" means.
2692 C Configure custom agenda commands.
2694 More commands can be added by configuring the variable
2695 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2696 searches can be pre-defined in this way.
2698 If the current buffer is in Org mode and visiting a file, you can also
2699 first press `<' once to indicate that the agenda should be temporarily
2700 \(until the next use of \\[org-agenda]) restricted to the current file.
2701 Pressing `<' twice means to restrict to the current subtree or region
2702 \(if active)."
2703 (interactive "P")
2704 (catch 'exit
2705 (let* ((prefix-descriptions nil)
2706 (org-agenda-buffer-name org-agenda-buffer-name)
2707 (org-agenda-window-setup (if (equal (buffer-name)
2708 org-agenda-buffer-name)
2709 'current-window
2710 org-agenda-window-setup))
2711 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2712 (org-agenda-custom-commands
2713 ;; normalize different versions
2714 (delq nil
2715 (mapcar
2716 (lambda (x)
2717 (cond ((stringp (cdr x))
2718 (push x prefix-descriptions)
2719 nil)
2720 ((stringp (nth 1 x)) x)
2721 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2722 (t (cons (car x) (cons "" (cdr x))))))
2723 org-agenda-custom-commands)))
2724 (org-agenda-custom-commands
2725 (org-contextualize-keys
2726 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2727 (buf (current-buffer))
2728 (bfn (buffer-file-name (buffer-base-buffer)))
2729 entry key type org-match lprops ans)
2730 ;; Turn off restriction unless there is an overriding one,
2731 (unless org-agenda-overriding-restriction
2732 (unless org-agenda-keep-restricted-file-list
2733 ;; There is a request to keep the file list in place
2734 (put 'org-agenda-files 'org-restrict nil))
2735 (setq org-agenda-restrict nil)
2736 (move-marker org-agenda-restrict-begin nil)
2737 (move-marker org-agenda-restrict-end nil))
2738 ;; Delete old local properties
2739 (put 'org-agenda-redo-command 'org-lprops nil)
2740 ;; Delete previously set last-arguments
2741 (put 'org-agenda-redo-command 'last-args nil)
2742 ;; Remember where this call originated
2743 (setq org-agenda-last-dispatch-buffer (current-buffer))
2744 (unless org-keys
2745 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2746 org-keys (car ans)
2747 restriction (cdr ans)))
2748 ;; If we have sticky agenda buffers, set a name for the buffer,
2749 ;; depending on the invoking keys. The user may still set this
2750 ;; as a command option, which will overwrite what we do here.
2751 (if org-agenda-sticky
2752 (setq org-agenda-buffer-name
2753 (format "*Org Agenda(%s)*" org-keys)))
2754 ;; Establish the restriction, if any
2755 (when (and (not org-agenda-overriding-restriction) restriction)
2756 (put 'org-agenda-files 'org-restrict (list bfn))
2757 (cond
2758 ((eq restriction 'region)
2759 (setq org-agenda-restrict (current-buffer))
2760 (move-marker org-agenda-restrict-begin (region-beginning))
2761 (move-marker org-agenda-restrict-end (region-end)))
2762 ((eq restriction 'subtree)
2763 (save-excursion
2764 (setq org-agenda-restrict (current-buffer))
2765 (org-back-to-heading t)
2766 (move-marker org-agenda-restrict-begin (point))
2767 (move-marker org-agenda-restrict-end
2768 (progn (org-end-of-subtree t)))))))
2770 ;; For example the todo list should not need it (but does...)
2771 (cond
2772 ((setq entry (assoc org-keys org-agenda-custom-commands))
2773 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2774 (progn
2775 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
2776 lprops (nth 4 entry))
2777 (if org-agenda-sticky
2778 (setq org-agenda-buffer-name
2779 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2780 (format "*Org Agenda(%s)*" org-keys))))
2781 (put 'org-agenda-redo-command 'org-lprops lprops)
2782 (cond
2783 ((eq type 'agenda)
2784 (org-let lprops '(org-agenda-list current-prefix-arg)))
2785 ((eq type 'agenda*)
2786 (org-let lprops '(org-agenda-list current-prefix-arg nil nil t)))
2787 ((eq type 'alltodo)
2788 (org-let lprops '(org-todo-list current-prefix-arg)))
2789 ((eq type 'search)
2790 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
2791 ((eq type 'stuck)
2792 (org-let lprops '(org-agenda-list-stuck-projects
2793 current-prefix-arg)))
2794 ((eq type 'tags)
2795 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2796 ((eq type 'tags-todo)
2797 (org-let lprops '(org-tags-view '(4) org-match)))
2798 ((eq type 'todo)
2799 (org-let lprops '(org-todo-list org-match)))
2800 ((eq type 'tags-tree)
2801 (org-check-for-org-mode)
2802 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
2803 ((eq type 'todo-tree)
2804 (org-check-for-org-mode)
2805 (org-let lprops
2806 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2807 (regexp-quote org-match) "\\>"))))
2808 ((eq type 'occur-tree)
2809 (org-check-for-org-mode)
2810 (org-let lprops '(org-occur org-match)))
2811 ((functionp type)
2812 (org-let lprops '(funcall type org-match)))
2813 ((fboundp type)
2814 (org-let lprops '(funcall type org-match)))
2815 (t (user-error "Invalid custom agenda command type %s" type))))
2816 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2817 ((equal org-keys "C")
2818 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2819 (customize-variable 'org-agenda-custom-commands))
2820 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2821 ((equal org-keys "s") (call-interactively 'org-search-view))
2822 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2823 ((equal org-keys "t") (call-interactively 'org-todo-list))
2824 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2825 ((equal org-keys "m") (call-interactively 'org-tags-view))
2826 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2827 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2828 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
2829 (add-hook
2830 'post-command-hook
2831 (lambda ()
2832 (unless (current-message)
2833 (let* ((m (org-agenda-get-any-marker))
2834 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2835 (when note
2836 (message (concat
2837 "FLAGGING-NOTE ([?] for more info): "
2838 (org-add-props
2839 (replace-regexp-in-string
2840 "\\\\n" "//"
2841 (copy-sequence note))
2842 nil 'face 'org-warning)))))))
2843 t t))
2844 ((equal org-keys "L")
2845 (unless (derived-mode-p 'org-mode)
2846 (user-error "This is not an Org file"))
2847 (unless restriction
2848 (put 'org-agenda-files 'org-restrict (list bfn))
2849 (org-call-with-arg 'org-timeline arg)))
2850 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2851 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2852 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
2853 (t (user-error "Invalid agenda key"))))))
2855 (defvar org-agenda-multi)
2857 (defun org-agenda-append-agenda ()
2858 "Append another agenda view to the current one.
2859 This function allows interactive building of block agendas.
2860 Agenda views are separated by `org-agenda-block-separator'."
2861 (interactive)
2862 (unless (derived-mode-p 'org-agenda-mode)
2863 (user-error "Can only append from within agenda buffer"))
2864 (let ((org-agenda-multi t))
2865 (org-agenda)
2866 (widen)
2867 (org-agenda-finalize)
2868 (setq buffer-read-only t)
2869 (org-agenda-fit-window-to-buffer)))
2871 (defun org-agenda-normalize-custom-commands (cmds)
2872 "Normalize custom commands CMDS."
2873 (delq nil
2874 (mapcar
2875 (lambda (x)
2876 (cond ((stringp (cdr x)) nil)
2877 ((stringp (nth 1 x)) x)
2878 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2879 (t (cons (car x) (cons "" (cdr x))))))
2880 cmds)))
2882 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2883 "The user interface for selecting an agenda command."
2884 (catch 'exit
2885 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2886 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2887 (region-p (org-region-active-p))
2888 (custom org-agenda-custom-commands)
2889 (selstring "")
2890 restriction second-time
2891 c entry key type match prefixes rmheader header-end custom1 desc
2892 line lines left right n n1)
2893 (save-window-excursion
2894 (delete-other-windows)
2895 (org-switch-to-buffer-other-window " *Agenda Commands*")
2896 (erase-buffer)
2897 (insert (eval-when-compile
2898 (let ((header
2899 "Press key for an agenda command: < Buffer, subtree/region restriction
2900 -------------------------------- > Remove restriction
2901 a Agenda for current week or day e Export agenda views
2902 t List of all TODO entries T Entries with special TODO kwd
2903 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2904 s Search for keywords S Like s, but only TODO entries
2905 L Timeline for current buffer # List stuck projects (!=configure)
2906 / Multi-occur C Configure custom agenda commands
2907 ? Find :FLAGGED: entries * Toggle sticky agenda views
2909 (start 0))
2910 (while (string-match
2911 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2912 header start)
2913 (setq start (match-end 0))
2914 (add-text-properties (match-beginning 2) (match-end 2)
2915 '(face bold) header))
2916 header)))
2917 (setq header-end (point-marker))
2918 (while t
2919 (setq custom1 custom)
2920 (when (eq rmheader t)
2921 (org-goto-line 1)
2922 (re-search-forward ":" nil t)
2923 (delete-region (match-end 0) (point-at-eol))
2924 (forward-char 1)
2925 (looking-at "-+")
2926 (delete-region (match-end 0) (point-at-eol))
2927 (move-marker header-end (match-end 0)))
2928 (goto-char header-end)
2929 (delete-region (point) (point-max))
2931 ;; Produce all the lines that describe custom commands and prefixes
2932 (setq lines nil)
2933 (while (setq entry (pop custom1))
2934 (setq key (car entry) desc (nth 1 entry)
2935 type (nth 2 entry)
2936 match (nth 3 entry))
2937 (if (> (length key) 1)
2938 (add-to-list 'prefixes (string-to-char key))
2939 (setq line
2940 (format
2941 "%-4s%-14s"
2942 (org-add-props (copy-sequence key)
2943 '(face bold))
2944 (cond
2945 ((string-match "\\S-" desc) desc)
2946 ((eq type 'agenda) "Agenda for current week or day")
2947 ((eq type 'agenda*) "Appointments for current week or day")
2948 ((eq type 'alltodo) "List of all TODO entries")
2949 ((eq type 'search) "Word search")
2950 ((eq type 'stuck) "List of stuck projects")
2951 ((eq type 'todo) "TODO keyword")
2952 ((eq type 'tags) "Tags query")
2953 ((eq type 'tags-todo) "Tags (TODO)")
2954 ((eq type 'tags-tree) "Tags tree")
2955 ((eq type 'todo-tree) "TODO kwd tree")
2956 ((eq type 'occur-tree) "Occur tree")
2957 ((functionp type) (if (symbolp type)
2958 (symbol-name type)
2959 "Lambda expression"))
2960 (t "???"))))
2961 (if org-agenda-menu-show-matcher
2962 (setq line
2963 (concat line ": "
2964 (cond
2965 ((stringp match)
2966 (setq match (copy-sequence match))
2967 (org-add-props match nil 'face 'org-warning))
2968 ((listp type)
2969 (format "set of %d commands" (length type))))))
2970 (if (org-string-nw-p match)
2971 (add-text-properties
2972 0 (length line) (list 'help-echo
2973 (concat "Matcher: " match)) line)))
2974 (push line lines)))
2975 (setq lines (nreverse lines))
2976 (when prefixes
2977 (mapc (lambda (x)
2978 (push
2979 (format "%s %s"
2980 (org-add-props (char-to-string x)
2981 nil 'face 'bold)
2982 (or (cdr (assoc (concat selstring
2983 (char-to-string x))
2984 prefix-descriptions))
2985 "Prefix key"))
2986 lines))
2987 prefixes))
2989 ;; Check if we should display in two columns
2990 (if org-agenda-menu-two-columns
2991 (progn
2992 (setq n (length lines)
2993 n1 (+ (/ n 2) (mod n 2))
2994 right (nthcdr n1 lines)
2995 left (copy-sequence lines))
2996 (setcdr (nthcdr (1- n1) left) nil))
2997 (setq left lines right nil))
2998 (while left
2999 (insert "\n" (pop left))
3000 (when right
3001 (if (< (current-column) 40)
3002 (move-to-column 40 t)
3003 (insert " "))
3004 (insert (pop right))))
3006 ;; Make the window the right size
3007 (goto-char (point-min))
3008 (if second-time
3009 (if (not (pos-visible-in-window-p (point-max)))
3010 (org-fit-window-to-buffer))
3011 (setq second-time t)
3012 (org-fit-window-to-buffer))
3014 ;; Ask for selection
3015 (message "Press key for agenda command%s:"
3016 (if (or restrict-ok org-agenda-overriding-restriction)
3017 (if org-agenda-overriding-restriction
3018 " (restriction lock active)"
3019 (if restriction
3020 (format " (restricted to %s)" restriction)
3021 " (unrestricted)"))
3022 ""))
3023 (setq c (read-char-exclusive))
3024 (message "")
3025 (cond
3026 ((assoc (char-to-string c) custom)
3027 (setq selstring (concat selstring (char-to-string c)))
3028 (throw 'exit (cons selstring restriction)))
3029 ((memq c prefixes)
3030 (setq selstring (concat selstring (char-to-string c))
3031 prefixes nil
3032 rmheader (or rmheader t)
3033 custom (delq nil (mapcar
3034 (lambda (x)
3035 (if (or (= (length (car x)) 1)
3036 (/= (string-to-char (car x)) c))
3038 (cons (substring (car x) 1) (cdr x))))
3039 custom))))
3040 ((eq c ?*)
3041 (call-interactively 'org-toggle-sticky-agenda)
3042 (sit-for 2))
3043 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
3044 (message "Restriction is only possible in Org buffers")
3045 (ding) (sit-for 1))
3046 ((eq c ?1)
3047 (org-agenda-remove-restriction-lock 'noupdate)
3048 (setq restriction 'buffer))
3049 ((eq c ?0)
3050 (org-agenda-remove-restriction-lock 'noupdate)
3051 (setq restriction (if region-p 'region 'subtree)))
3052 ((eq c ?<)
3053 (org-agenda-remove-restriction-lock 'noupdate)
3054 (setq restriction
3055 (cond
3056 ((eq restriction 'buffer)
3057 (if region-p 'region 'subtree))
3058 ((memq restriction '(subtree region))
3059 nil)
3060 (t 'buffer))))
3061 ((eq c ?>)
3062 (org-agenda-remove-restriction-lock 'noupdate)
3063 (setq restriction nil))
3064 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
3065 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
3066 ((and (> (length selstring) 0) (eq c ?\d))
3067 (delete-window)
3068 (org-agenda-get-restriction-and-command prefix-descriptions))
3070 ((equal c ?q) (error "Abort"))
3071 (t (user-error "Invalid key %c" c))))))))
3073 (defun org-agenda-fit-window-to-buffer ()
3074 "Fit the window to the buffer size."
3075 (and (memq org-agenda-window-setup '(reorganize-frame))
3076 (fboundp 'fit-window-to-buffer)
3077 (if (and (= (cdr org-agenda-window-frame-fractions) 1.0)
3078 (= (car org-agenda-window-frame-fractions) 1.0))
3079 (delete-other-windows)
3080 (org-fit-window-to-buffer
3082 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3083 (floor (* (frame-height) (car org-agenda-window-frame-fractions)))))))
3085 (defvar org-cmd nil)
3086 (defvar org-agenda-overriding-cmd nil)
3087 (defvar org-agenda-overriding-arguments nil)
3088 (defvar org-agenda-overriding-cmd-arguments nil)
3089 (defun org-agenda-run-series (name series)
3090 "Run agenda NAME as a SERIES of agenda commands."
3091 (org-let (nth 1 series) '(org-agenda-prepare name))
3092 ;; We need to reset agenda markers here, because when constructing a
3093 ;; block agenda, the individual blocks do not do that.
3094 (org-agenda-reset-markers)
3095 (let* ((org-agenda-multi t)
3096 (redo (list 'org-agenda-run-series name (list 'quote series)))
3097 (cmds (car series))
3098 (gprops (nth 1 series))
3099 match ;; The byte compiler incorrectly complains about this. Keep it!
3100 org-cmd type lprops)
3101 (while (setq org-cmd (pop cmds))
3102 (setq type (car org-cmd))
3103 (setq match (eval (nth 1 org-cmd)))
3104 (setq lprops (nth 2 org-cmd))
3105 (let ((org-agenda-overriding-arguments
3106 (if (eq org-agenda-overriding-cmd org-cmd)
3107 (or org-agenda-overriding-arguments
3108 org-agenda-overriding-cmd-arguments))))
3109 (cond
3110 ((eq type 'agenda)
3111 (org-let2 gprops lprops
3112 '(call-interactively 'org-agenda-list)))
3113 ((eq type 'agenda*)
3114 (org-let2 gprops lprops
3115 '(funcall 'org-agenda-list nil nil t)))
3116 ((eq type 'alltodo)
3117 (org-let2 gprops lprops
3118 '(call-interactively 'org-todo-list)))
3119 ((eq type 'search)
3120 (org-let2 gprops lprops
3121 '(org-search-view current-prefix-arg match nil)))
3122 ((eq type 'stuck)
3123 (org-let2 gprops lprops
3124 '(call-interactively 'org-agenda-list-stuck-projects)))
3125 ((eq type 'tags)
3126 (org-let2 gprops lprops
3127 '(org-tags-view current-prefix-arg match)))
3128 ((eq type 'tags-todo)
3129 (org-let2 gprops lprops
3130 '(org-tags-view '(4) match)))
3131 ((eq type 'todo)
3132 (org-let2 gprops lprops
3133 '(org-todo-list match)))
3134 ((fboundp type)
3135 (org-let2 gprops lprops
3136 '(funcall type match)))
3137 (t (error "Invalid type in command series")))))
3138 (widen)
3139 (let ((inhibit-read-only t))
3140 (add-text-properties (point-min) (point-max)
3141 `(org-series t org-series-redo-cmd ,redo)))
3142 (setq org-agenda-redo-command redo)
3143 (goto-char (point-min)))
3144 (org-agenda-fit-window-to-buffer)
3145 (org-let (nth 1 series) '(org-agenda-finalize)))
3147 ;;;###autoload
3148 (defmacro org-batch-agenda (cmd-key &rest parameters)
3149 "Run an agenda command in batch mode and send the result to STDOUT.
3150 If CMD-KEY is a string of length 1, it is used as a key in
3151 `org-agenda-custom-commands' and triggers this command. If it is a
3152 longer string it is used as a tags/todo match string.
3153 Parameters are alternating variable names and values that will be bound
3154 before running the agenda command."
3155 (org-eval-in-environment (org-make-parameter-alist parameters)
3156 (let (org-agenda-sticky)
3157 (if (> (length cmd-key) 1)
3158 (org-tags-view nil cmd-key)
3159 (org-agenda nil cmd-key))))
3160 (set-buffer org-agenda-buffer-name)
3161 (princ (buffer-string)))
3163 (defvar org-agenda-info nil)
3165 ;;;###autoload
3166 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
3167 "Run an agenda command in batch mode and send the result to STDOUT.
3168 If CMD-KEY is a string of length 1, it is used as a key in
3169 `org-agenda-custom-commands' and triggers this command. If it is a
3170 longer string it is used as a tags/todo match string.
3171 Parameters are alternating variable names and values that will be bound
3172 before running the agenda command.
3174 The output gives a line for each selected agenda item. Each
3175 item is a list of comma-separated values, like this:
3177 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3179 category The category of the item
3180 head The headline, without TODO kwd, TAGS and PRIORITY
3181 type The type of the agenda entry, can be
3182 todo selected in TODO match
3183 tagsmatch selected in tags match
3184 diary imported from diary
3185 deadline a deadline on given date
3186 scheduled scheduled on given date
3187 timestamp entry has timestamp on given date
3188 closed entry was closed on given date
3189 upcoming-deadline warning about deadline
3190 past-scheduled forwarded scheduled item
3191 block entry has date block including g. date
3192 todo The todo keyword, if any
3193 tags All tags including inherited ones, separated by colons
3194 date The relevant date, like 2007-2-14
3195 time The time, like 15:00-16:50
3196 extra Sting with extra planning info
3197 priority-l The priority letter if any was given
3198 priority-n The computed numerical priority
3199 agenda-day The day in the agenda where this is listed"
3200 (org-eval-in-environment (append '((org-agenda-remove-tags t))
3201 (org-make-parameter-alist parameters))
3202 (if (> (length cmd-key) 2)
3203 (org-tags-view nil cmd-key)
3204 (org-agenda nil cmd-key)))
3205 (set-buffer org-agenda-buffer-name)
3206 (let* ((lines (org-split-string (buffer-string) "\n"))
3207 line)
3208 (while (setq line (pop lines))
3209 (catch 'next
3210 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
3211 (setq org-agenda-info
3212 (org-fix-agenda-info (text-properties-at 0 line)))
3213 (princ
3214 (mapconcat 'org-agenda-export-csv-mapper
3215 '(org-category txt type todo tags date time extra
3216 priority-letter priority agenda-day)
3217 ","))
3218 (princ "\n")))))
3220 (defun org-fix-agenda-info (props)
3221 "Make sure all properties on an agenda item have a canonical form.
3222 This ensures the export commands can easily use it."
3223 (let (tmp re)
3224 (when (setq tmp (plist-get props 'tags))
3225 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3226 (when (setq tmp (plist-get props 'date))
3227 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3228 (let ((calendar-date-display-form '(year "-" month "-" day)))
3229 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3231 (setq tmp (calendar-date-string tmp)))
3232 (setq props (plist-put props 'date tmp)))
3233 (when (setq tmp (plist-get props 'day))
3234 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3235 (let ((calendar-date-display-form '(year "-" month "-" day)))
3236 (setq tmp (calendar-date-string tmp)))
3237 (setq props (plist-put props 'day tmp))
3238 (setq props (plist-put props 'agenda-day tmp)))
3239 (when (setq tmp (plist-get props 'txt))
3240 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3241 (plist-put props 'priority-letter (match-string 1 tmp))
3242 (setq tmp (replace-match "" t t tmp)))
3243 (when (and (setq re (plist-get props 'org-todo-regexp))
3244 (setq re (concat "\\`\\.*" re " ?"))
3245 (string-match re tmp))
3246 (plist-put props 'todo (match-string 1 tmp))
3247 (setq tmp (replace-match "" t t tmp)))
3248 (plist-put props 'txt tmp)))
3249 props)
3251 (defun org-agenda-export-csv-mapper (prop)
3252 (let ((res (plist-get org-agenda-info prop)))
3253 (setq res
3254 (cond
3255 ((not res) "")
3256 ((stringp res) res)
3257 (t (prin1-to-string res))))
3258 (org-trim (replace-regexp-in-string "," ";" res nil t))))
3260 ;;;###autoload
3261 (defun org-store-agenda-views (&rest parameters)
3262 "Store agenda views."
3263 (interactive)
3264 (eval (list 'org-batch-store-agenda-views)))
3266 ;;;###autoload
3267 (defmacro org-batch-store-agenda-views (&rest parameters)
3268 "Run all custom agenda commands that have a file argument."
3269 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3270 (pop-up-frames nil)
3271 (dir default-directory)
3272 (pars (org-make-parameter-alist parameters))
3273 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
3274 (save-window-excursion
3275 (while cmds
3276 (setq cmd (pop cmds)
3277 thiscmdkey (car cmd)
3278 thiscmdcmd (cdr cmd)
3279 match (nth 2 thiscmdcmd)
3280 bufname (if org-agenda-sticky
3281 (or (and (stringp match)
3282 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3283 (format "*Org Agenda(%s)*" thiscmdkey))
3284 org-agenda-buffer-name)
3285 cmd-or-set (nth 2 cmd)
3286 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3287 files (nth (if (listp cmd-or-set) 4 5) cmd))
3288 (if (stringp files) (setq files (list files)))
3289 (when files
3290 (org-eval-in-environment (append org-agenda-exporter-settings
3291 opts pars)
3292 (org-agenda nil thiscmdkey))
3293 (set-buffer bufname)
3294 (while files
3295 (org-eval-in-environment (append org-agenda-exporter-settings
3296 opts pars)
3297 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3298 (and (get-buffer bufname)
3299 (kill-buffer bufname)))))))
3301 (defvar org-agenda-current-span nil
3302 "The current span used in the agenda view.") ; local variable in the agenda buffer
3303 (defun org-agenda-mark-header-line (pos)
3304 "Mark the line at POS as an agenda structure header."
3305 (save-excursion
3306 (goto-char pos)
3307 (put-text-property (point-at-bol) (point-at-eol)
3308 'org-agenda-structural-header t)
3309 (when org-agenda-title-append
3310 (put-text-property (point-at-bol) (point-at-eol)
3311 'org-agenda-title-append org-agenda-title-append))))
3313 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3314 (defvar org-agenda-write-buffer-name "Agenda View")
3315 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3316 "Write the current buffer (an agenda view) as a file.
3318 Depending on the extension of the file name, plain text (.txt),
3319 HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
3320 If the extension is .ics, translate visible agenda into iCalendar
3321 format. If the extension is .org, collect all subtrees
3322 corresponding to the agenda entries and add them in an .org file.
3324 With prefix argument OPEN, open the new file immediately. If
3325 NOSETTINGS is given, do not scope the settings of
3326 `org-agenda-exporter-settings' into the export commands. This is
3327 used when the settings have already been scoped and we do not
3328 wish to overrule other, higher priority settings. If
3329 AGENDA-BUFFER-NAME is provided, use this as the buffer name for
3330 the agenda to write."
3331 (interactive "FWrite agenda to file: \nP")
3332 (if (or (not (file-writable-p file))
3333 (and (file-exists-p file)
3334 (if (called-interactively-p 'any)
3335 (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
3336 (user-error "Cannot write agenda to file %s" file))
3337 (org-let (if nosettings nil org-agenda-exporter-settings)
3338 '(save-excursion
3339 (save-window-excursion
3340 (let ((bs (copy-sequence (buffer-string)))
3341 (extension (file-name-extension file))
3342 beg content)
3343 (with-temp-buffer
3344 (rename-buffer org-agenda-write-buffer-name t)
3345 (set-buffer-modified-p nil)
3346 (insert bs)
3347 (org-agenda-remove-marked-text 'invisible 'org-filtered)
3348 (run-hooks 'org-agenda-before-write-hook)
3349 (cond
3350 ((bound-and-true-p org-mobile-creating-agendas)
3351 (org-mobile-write-agenda-for-mobile file))
3352 ((string= "org" extension)
3353 (let (content p m message-log-max)
3354 (goto-char (point-min))
3355 (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
3356 (goto-char p)
3357 (setq m (get-text-property (point) 'org-hd-marker))
3358 (when m
3359 (push (save-excursion
3360 (set-buffer (marker-buffer m))
3361 (goto-char m)
3362 (org-copy-subtree 1 nil t t)
3363 org-subtree-clip)
3364 content)))
3365 (find-file file)
3366 (erase-buffer)
3367 (dolist (s content) (org-paste-subtree 1 s))
3368 (write-file file)
3369 (kill-buffer (current-buffer))
3370 (message "Org file written to %s" file)))
3371 ((member extension '("html" "htm"))
3372 (require 'htmlize)
3373 (set-buffer (htmlize-buffer (current-buffer)))
3374 (when org-agenda-export-html-style
3375 ;; replace <style> section with org-agenda-export-html-style
3376 (goto-char (point-min))
3377 (kill-region (- (search-forward "<style") 6)
3378 (search-forward "</style>"))
3379 (insert org-agenda-export-html-style))
3380 (write-file file)
3381 (kill-buffer (current-buffer))
3382 (message "HTML written to %s" file))
3383 ((string= "ps" extension)
3384 (require 'ps-print)
3385 (ps-print-buffer-with-faces file)
3386 (message "Postscript written to %s" file))
3387 ((string= "pdf" extension)
3388 (require 'ps-print)
3389 (ps-print-buffer-with-faces
3390 (concat (file-name-sans-extension file) ".ps"))
3391 (call-process "ps2pdf" nil nil nil
3392 (expand-file-name
3393 (concat (file-name-sans-extension file) ".ps"))
3394 (expand-file-name file))
3395 (delete-file (concat (file-name-sans-extension file) ".ps"))
3396 (message "PDF written to %s" file))
3397 ((string= "ics" extension)
3398 (require 'ox-icalendar)
3399 (org-icalendar-export-current-agenda (expand-file-name file)))
3401 (let ((bs (buffer-string)))
3402 (find-file file)
3403 (erase-buffer)
3404 (insert bs)
3405 (save-buffer 0)
3406 (kill-buffer (current-buffer))
3407 (message "Plain text written to %s" file))))))))
3408 (set-buffer (or agenda-bufname
3409 (and (called-interactively-p 'any) (buffer-name))
3410 org-agenda-buffer-name)))
3411 (when open (org-open-file file)))
3413 (defun org-agenda-remove-marked-text (property &optional value)
3414 "Delete all text marked with VALUE of PROPERTY.
3415 VALUE defaults to t."
3416 (let (beg)
3417 (setq value (or value t))
3418 (while (setq beg (text-property-any (point-min) (point-max)
3419 property value))
3420 (delete-region
3421 beg (or (next-single-property-change beg property)
3422 (point-max))))))
3424 (defun org-agenda-add-entry-text ()
3425 "Add entry text to agenda lines.
3426 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3427 entry text following headings shown in the agenda.
3428 Drawers will be excluded, also the line with scheduling/deadline info."
3429 (when (and (> org-agenda-add-entry-text-maxlines 0)
3430 (not (bound-and-true-p org-mobile-creating-agendas)))
3431 (let (m txt)
3432 (goto-char (point-min))
3433 (while (not (eobp))
3434 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3435 (beginning-of-line 2)
3436 (setq txt (org-agenda-get-some-entry-text
3437 m org-agenda-add-entry-text-maxlines " > "))
3438 (end-of-line 1)
3439 (if (string-match "\\S-" txt)
3440 (insert "\n" txt)
3441 (or (eobp) (forward-char 1))))))))
3443 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3444 &rest keep)
3445 "Extract entry text from MARKER, at most N-LINES lines.
3446 This will ignore drawers etc, just get the text.
3447 If INDENT is given, prefix every line with this string. If KEEP is
3448 given, it is a list of symbols, defining stuff that should not be
3449 removed from the entry content. Currently only `planning' is allowed here."
3450 (let (txt drawer-re kwd-time-re ind)
3451 (save-excursion
3452 (with-current-buffer (marker-buffer marker)
3453 (if (not (derived-mode-p 'org-mode))
3454 (setq txt "")
3455 (org-with-wide-buffer
3456 (goto-char marker)
3457 (end-of-line 1)
3458 (setq txt (buffer-substring
3459 (min (1+ (point)) (point-max))
3460 (progn (outline-next-heading) (point)))
3461 drawer-re org-drawer-regexp
3462 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3463 ".*\n?"))
3464 (with-temp-buffer
3465 (insert txt)
3466 (when org-agenda-add-entry-text-descriptive-links
3467 (goto-char (point-min))
3468 (while (org-activate-bracket-links (point-max))
3469 (add-text-properties (match-beginning 0) (match-end 0)
3470 '(face org-link))))
3471 (goto-char (point-min))
3472 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3473 (set-text-properties (match-beginning 0) (match-end 0)
3474 nil))
3475 (goto-char (point-min))
3476 (while (re-search-forward drawer-re nil t)
3477 (delete-region
3478 (match-beginning 0)
3479 (progn (re-search-forward
3480 "^[ \t]*:END:.*\n?" nil 'move)
3481 (point))))
3482 (unless (member 'planning keep)
3483 (goto-char (point-min))
3484 (while (re-search-forward kwd-time-re nil t)
3485 (replace-match "")))
3486 (goto-char (point-min))
3487 (when org-agenda-entry-text-exclude-regexps
3488 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3489 (while (setq re (pop re-list))
3490 (goto-char (point-min))
3491 (while (re-search-forward re nil t)
3492 (replace-match "")))))
3493 (goto-char (point-max))
3494 (skip-chars-backward " \t\n")
3495 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3497 ;; find and remove min common indentation
3498 (goto-char (point-min))
3499 (untabify (point-min) (point-max))
3500 (setq ind (org-get-indentation))
3501 (while (not (eobp))
3502 (unless (looking-at "[ \t]*$")
3503 (setq ind (min ind (org-get-indentation))))
3504 (beginning-of-line 2))
3505 (goto-char (point-min))
3506 (while (not (eobp))
3507 (unless (looking-at "[ \t]*$")
3508 (move-to-column ind)
3509 (delete-region (point-at-bol) (point)))
3510 (beginning-of-line 2))
3512 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3514 (goto-char (point-min))
3515 (when indent
3516 (while (and (not (eobp)) (re-search-forward "^" nil t))
3517 (replace-match indent t t)))
3518 (goto-char (point-min))
3519 (while (looking-at "[ \t]*\n") (replace-match ""))
3520 (goto-char (point-max))
3521 (when (> (org-current-line)
3522 n-lines)
3523 (org-goto-line (1+ n-lines))
3524 (backward-char 1))
3525 (setq txt (buffer-substring (point-min) (point))))))))
3526 txt))
3528 (defun org-check-for-org-mode ()
3529 "Make sure current buffer is in org-mode. Error if not."
3530 (or (derived-mode-p 'org-mode)
3531 (error "Cannot execute Org agenda command on buffer in %s"
3532 major-mode)))
3534 ;;; Agenda prepare and finalize
3536 (defvar org-agenda-multi nil) ; dynamically scoped
3537 (defvar org-agenda-pre-window-conf nil)
3538 (defvar org-agenda-columns-active nil)
3539 (defvar org-agenda-name nil)
3540 (defvar org-agenda-tag-filter nil)
3541 (defvar org-agenda-category-filter nil)
3542 (defvar org-agenda-regexp-filter nil)
3543 (defvar org-agenda-effort-filter nil)
3544 (defvar org-agenda-top-headline-filter nil)
3545 (defvar org-agenda-tag-filter-preset nil
3546 "A preset of the tags filter used for secondary agenda filtering.
3547 This must be a list of strings, each string must be a single tag preceded
3548 by \"+\" or \"-\".
3549 This variable should not be set directly, but agenda custom commands can
3550 bind it in the options section. The preset filter is a global property of
3551 the entire agenda view. In a block agenda, it will not work reliably to
3552 define a filter for one of the individual blocks. You need to set it in
3553 the global options and expect it to be applied to the entire view.")
3555 (defvar org-agenda-category-filter-preset nil
3556 "A preset of the category filter used for secondary agenda filtering.
3557 This must be a list of strings, each string must be a single category
3558 preceded by \"+\" or \"-\".
3559 This variable should not be set directly, but agenda custom commands can
3560 bind it in the options section. The preset filter is a global property of
3561 the entire agenda view. In a block agenda, it will not work reliably to
3562 define a filter for one of the individual blocks. You need to set it in
3563 the global options and expect it to be applied to the entire view.")
3565 (defvar org-agenda-regexp-filter-preset nil
3566 "A preset of the regexp filter used for secondary agenda filtering.
3567 This must be a list of strings, each string must be a single regexp
3568 preceded by \"+\" or \"-\".
3569 This variable should not be set directly, but agenda custom commands can
3570 bind it in the options section. The preset filter is a global property of
3571 the entire agenda view. In a block agenda, it will not work reliably to
3572 define a filter for one of the individual blocks. You need to set it in
3573 the global options and expect it to be applied to the entire view.")
3575 (defvar org-agenda-effort-filter-preset nil
3576 "A preset of the effort condition used for secondary agenda filtering.
3577 This must be a list of strings, each string must be a single regexp
3578 preceded by \"+\" or \"-\".
3579 This variable should not be set directly, but agenda custom commands can
3580 bind it in the options section. The preset filter is a global property of
3581 the entire agenda view. In a block agenda, it will not work reliably to
3582 define a filter for one of the individual blocks. You need to set it in
3583 the global options and expect it to be applied to the entire view.")
3585 (defun org-agenda-use-sticky-p ()
3586 "Return non-nil if an agenda buffer named
3587 `org-agenda-buffer-name' exists and should be shown instead of
3588 generating a new one."
3589 (and
3590 ;; turned off by user
3591 org-agenda-sticky
3592 ;; For multi-agenda buffer already exists
3593 (not org-agenda-multi)
3594 ;; buffer found
3595 (get-buffer org-agenda-buffer-name)
3596 ;; C-u parameter is same as last call
3597 (with-current-buffer (get-buffer org-agenda-buffer-name)
3598 (and
3599 (equal current-prefix-arg
3600 org-agenda-last-prefix-arg)
3601 ;; In case user turned stickiness on, while having existing
3602 ;; Agenda buffer active, don't reuse that buffer, because it
3603 ;; does not have org variables local
3604 org-agenda-this-buffer-is-sticky))))
3606 (defun org-agenda-prepare-window (abuf filter-alist)
3607 "Setup agenda buffer in the window.
3608 ABUF is the buffer for the agenda window.
3609 FILTER-ALIST is an alist of filters we need to apply when
3610 `org-agenda-persistent-filter' is non-nil."
3611 (let* ((awin (get-buffer-window abuf)) wconf)
3612 (cond
3613 ((equal (current-buffer) abuf) nil)
3614 (awin (select-window awin))
3615 ((not (setq wconf (current-window-configuration))))
3616 ((eq org-agenda-window-setup 'current-window)
3617 (pop-to-buffer-same-window abuf))
3618 ((eq org-agenda-window-setup 'other-window)
3619 (org-switch-to-buffer-other-window abuf))
3620 ((eq org-agenda-window-setup 'other-frame)
3621 (switch-to-buffer-other-frame abuf))
3622 ((eq org-agenda-window-setup 'only-window)
3623 (delete-other-windows)
3624 (pop-to-buffer-same-window abuf))
3625 ((eq org-agenda-window-setup 'reorganize-frame)
3626 (delete-other-windows)
3627 (org-switch-to-buffer-other-window abuf)))
3628 (setq org-agenda-tag-filter (cdr (assq 'tag filter-alist)))
3629 (setq org-agenda-category-filter (cdr (assq 'cat filter-alist)))
3630 (setq org-agenda-effort-filter (cdr (assq 'effort filter-alist)))
3631 (setq org-agenda-regexp-filter (cdr (assq 're filter-alist)))
3632 ;; Additional test in case agenda is invoked from within agenda
3633 ;; buffer via elisp link.
3634 (unless (equal (current-buffer) abuf)
3635 (pop-to-buffer-same-window abuf))
3636 (setq org-agenda-pre-window-conf
3637 (or wconf org-agenda-pre-window-conf))))
3639 (defun org-agenda-prepare (&optional name)
3640 (let ((filter-alist (if org-agenda-persistent-filter
3641 (with-current-buffer
3642 (get-buffer-create org-agenda-buffer-name)
3643 (list `(tag . ,org-agenda-tag-filter)
3644 `(re . ,org-agenda-regexp-filter)
3645 `(effort . ,org-agenda-effort-filter)
3646 `(cat . ,org-agenda-category-filter))))))
3647 (if (org-agenda-use-sticky-p)
3648 (progn
3649 (put 'org-agenda-tag-filter :preset-filter nil)
3650 (put 'org-agenda-category-filter :preset-filter nil)
3651 (put 'org-agenda-regexp-filter :preset-filter nil)
3652 ;; Popup existing buffer
3653 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name)
3654 filter-alist)
3655 (message "Sticky Agenda buffer, use `r' to refresh")
3656 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3657 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3658 (setq org-todo-keywords-for-agenda nil)
3659 (put 'org-agenda-tag-filter :preset-filter
3660 org-agenda-tag-filter-preset)
3661 (put 'org-agenda-category-filter :preset-filter
3662 org-agenda-category-filter-preset)
3663 (put 'org-agenda-regexp-filter :preset-filter
3664 org-agenda-regexp-filter-preset)
3665 (put 'org-agenda-effort-filter :preset-filter
3666 org-agenda-effort-filter-preset)
3667 (if org-agenda-multi
3668 (progn
3669 (setq buffer-read-only nil)
3670 (goto-char (point-max))
3671 (unless (or (bobp) org-agenda-compact-blocks
3672 (not org-agenda-block-separator))
3673 (insert "\n"
3674 (if (stringp org-agenda-block-separator)
3675 org-agenda-block-separator
3676 (make-string (window-width) org-agenda-block-separator))
3677 "\n"))
3678 (narrow-to-region (point) (point-max)))
3679 (setq org-done-keywords-for-agenda nil)
3680 ;; Setting any org variables that are in org-agenda-local-vars
3681 ;; list need to be done after the prepare call
3682 (org-agenda-prepare-window
3683 (get-buffer-create org-agenda-buffer-name) filter-alist)
3684 (setq buffer-read-only nil)
3685 (org-agenda-reset-markers)
3686 (let ((inhibit-read-only t)) (erase-buffer))
3687 (org-agenda-mode)
3688 (setq org-agenda-buffer (current-buffer))
3689 (setq org-agenda-contributing-files nil)
3690 (setq org-agenda-columns-active nil)
3691 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3692 (setq org-todo-keywords-for-agenda
3693 (org-uniquify org-todo-keywords-for-agenda))
3694 (setq org-done-keywords-for-agenda
3695 (org-uniquify org-done-keywords-for-agenda))
3696 (setq org-agenda-last-prefix-arg current-prefix-arg)
3697 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3698 (and name (not org-agenda-name)
3699 (setq-local org-agenda-name name)))
3700 (setq buffer-read-only nil))))
3702 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3703 (defun org-agenda-finalize ()
3704 "Finishing touch for the agenda buffer, called just before displaying it."
3705 (unless org-agenda-multi
3706 (save-excursion
3707 (let ((inhibit-read-only t))
3708 (goto-char (point-min))
3709 (save-excursion
3710 (while (org-activate-bracket-links (point-max))
3711 (add-text-properties (match-beginning 0) (match-end 0)
3712 '(face org-link))))
3713 (save-excursion
3714 (while (org-activate-plain-links (point-max))
3715 (add-text-properties (match-beginning 0) (match-end 0)
3716 '(face org-link))))
3717 (unless (eq org-agenda-remove-tags t)
3718 (org-agenda-align-tags))
3719 (unless org-agenda-with-colors
3720 (remove-text-properties (point-min) (point-max) '(face nil)))
3721 (if (and (boundp 'org-agenda-overriding-columns-format)
3722 org-agenda-overriding-columns-format)
3723 (setq-local org-agenda-overriding-columns-format
3724 org-agenda-overriding-columns-format))
3725 (if (and (boundp 'org-agenda-view-columns-initially)
3726 org-agenda-view-columns-initially)
3727 (org-agenda-columns))
3728 (when org-agenda-fontify-priorities
3729 (org-agenda-fontify-priorities))
3730 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3731 (org-agenda-dim-blocked-tasks))
3732 (org-agenda-mark-clocking-task)
3733 (when org-agenda-entry-text-mode
3734 (org-agenda-entry-text-hide)
3735 (org-agenda-entry-text-show))
3736 (if (and (functionp 'org-habit-insert-consistency-graphs)
3737 (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
3738 (org-habit-insert-consistency-graphs))
3739 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3740 (unless (or (eq org-agenda-show-inherited-tags 'always)
3741 (and (listp org-agenda-show-inherited-tags)
3742 (memq org-agenda-type org-agenda-show-inherited-tags))
3743 (and (eq org-agenda-show-inherited-tags t)
3744 (or (eq org-agenda-use-tag-inheritance t)
3745 (and (listp org-agenda-use-tag-inheritance)
3746 (not (memq org-agenda-type
3747 org-agenda-use-tag-inheritance))))))
3748 (let (mrk)
3749 (save-excursion
3750 (goto-char (point-min))
3751 (while (equal (forward-line) 0)
3752 (when (setq mrk (get-text-property (point) 'org-hd-marker))
3753 (put-text-property (point-at-bol) (point-at-eol)
3754 'tags (org-with-point-at mrk
3755 (delete-dups
3756 (mapcar 'downcase (org-get-tags-at))))))))))
3757 (run-hooks 'org-agenda-finalize-hook)
3758 (when org-agenda-top-headline-filter
3759 (org-agenda-filter-top-headline-apply
3760 org-agenda-top-headline-filter))
3761 (when org-agenda-tag-filter
3762 (org-agenda-filter-apply org-agenda-tag-filter 'tag t))
3763 (when (get 'org-agenda-tag-filter :preset-filter)
3764 (org-agenda-filter-apply
3765 (get 'org-agenda-tag-filter :preset-filter) 'tag t))
3766 (when org-agenda-category-filter
3767 (org-agenda-filter-apply org-agenda-category-filter 'category))
3768 (when (get 'org-agenda-category-filter :preset-filter)
3769 (org-agenda-filter-apply
3770 (get 'org-agenda-category-filter :preset-filter) 'category))
3771 (when org-agenda-regexp-filter
3772 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
3773 (when (get 'org-agenda-regexp-filter :preset-filter)
3774 (org-agenda-filter-apply
3775 (get 'org-agenda-regexp-filter :preset-filter) 'regexp))
3776 (when org-agenda-effort-filter
3777 (org-agenda-filter-apply org-agenda-effort-filter 'effort))
3778 (when (get 'org-agenda-effort-filter :preset-filter)
3779 (org-agenda-filter-apply
3780 (get 'org-agenda-effort-filter :preset-filter) 'effort))
3781 (add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)))))
3783 (defun org-agenda-mark-clocking-task ()
3784 "Mark the current clock entry in the agenda if it is present."
3785 ;; We need to widen when `org-agenda-finalize' is called from
3786 ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
3787 (when (bound-and-true-p org-clock-current-task)
3788 (save-restriction
3789 (widen)
3790 (org-agenda-unmark-clocking-task)
3791 (when (marker-buffer org-clock-hd-marker)
3792 (save-excursion
3793 (goto-char (point-min))
3794 (let (s ov)
3795 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3796 (goto-char s)
3797 (when (equal (org-get-at-bol 'org-hd-marker)
3798 org-clock-hd-marker)
3799 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3800 (overlay-put ov 'type 'org-agenda-clocking)
3801 (overlay-put ov 'face 'org-agenda-clocking)
3802 (overlay-put ov 'help-echo
3803 "The clock is running in this item")))))))))
3805 (defun org-agenda-unmark-clocking-task ()
3806 "Unmark the current clocking task."
3807 (mapc (lambda (o)
3808 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3809 (delete-overlay o)))
3810 (overlays-in (point-min) (point-max))))
3812 (defun org-agenda-fontify-priorities ()
3813 "Make highest priority lines bold, and lowest italic."
3814 (interactive)
3815 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3816 (delete-overlay o)))
3817 (overlays-in (point-min) (point-max)))
3818 (save-excursion
3819 (let (b e p ov h l)
3820 (goto-char (point-min))
3821 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3822 (setq h (or (get-char-property (point) 'org-highest-priority)
3823 org-highest-priority)
3824 l (or (get-char-property (point) 'org-lowest-priority)
3825 org-lowest-priority)
3826 p (string-to-char (match-string 1))
3827 b (match-beginning 0)
3828 e (if (eq org-agenda-fontify-priorities 'cookies)
3829 (match-end 0)
3830 (point-at-eol))
3831 ov (make-overlay b e))
3832 (overlay-put
3833 ov 'face
3834 (let ((special-face
3835 (cond ((org-face-from-face-or-color
3836 'priority nil
3837 (cdr (assoc p org-priority-faces))))
3838 ((and (listp org-agenda-fontify-priorities)
3839 (org-face-from-face-or-color
3840 'priority nil
3841 (cdr (assoc p org-agenda-fontify-priorities)))))
3842 ((equal p l) 'italic)
3843 ((equal p h) 'bold))))
3844 (if special-face (list special-face 'org-priority) 'org-priority)))
3845 (overlay-put ov 'org-type 'org-priority)))))
3847 (defvar org-depend-tag-blocked)
3849 (defun org-agenda-dim-blocked-tasks (&optional invisible)
3850 "Dim currently blocked TODOs in the agenda display.
3851 When INVISIBLE is non-nil, hide currently blocked TODO instead of
3852 dimming them."
3853 (interactive "P")
3854 (when (called-interactively-p 'interactive)
3855 (message "Dim or hide blocked tasks..."))
3856 (dolist (o (overlays-in (point-min) (point-max)))
3857 (when (eq (overlay-get o 'org-type) 'org-blocked-todo)
3858 (delete-overlay o)))
3859 (save-excursion
3860 (let ((inhibit-read-only t)
3861 (org-depend-tag-blocked nil)
3862 org-blocked-by-checkboxes)
3863 (goto-char (point-min))
3864 (while (let ((pos (text-property-not-all
3865 (point) (point-max) 'todo-state nil)))
3866 (when pos (goto-char pos)))
3867 (setq org-blocked-by-checkboxes nil)
3868 (let ((marker (org-get-at-bol 'org-hd-marker)))
3869 (when (and (markerp marker)
3870 (with-current-buffer (marker-buffer marker)
3871 (save-excursion (goto-char marker)
3872 (org-entry-blocked-p))))
3873 ;; Entries blocked by checkboxes cannot be made invisible.
3874 ;; See `org-agenda-dim-blocked-tasks' for details.
3875 (let* ((really-invisible
3876 (and (not org-blocked-by-checkboxes)
3877 (or invisible (eq org-agenda-dim-blocked-tasks
3878 'invisible))))
3879 (ov (make-overlay (if really-invisible (line-end-position 0)
3880 (line-beginning-position))
3881 (line-end-position))))
3882 (if really-invisible (overlay-put ov 'invisible t)
3883 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3884 (overlay-put ov 'org-type 'org-blocked-todo))))
3885 (forward-line))))
3886 (when (called-interactively-p 'interactive)
3887 (message "Dim or hide blocked tasks...done")))
3889 (defvar org-agenda-skip-function nil
3890 "Function to be called at each match during agenda construction.
3891 If this function returns nil, the current match should not be skipped.
3892 Otherwise, the function must return a position from where the search
3893 should be continued.
3894 This may also be a Lisp form, it will be evaluated.
3895 Never set this variable using `setq' or so, because then it will apply
3896 to all future agenda commands. If you do want a global skipping condition,
3897 use the option `org-agenda-skip-function-global' instead.
3898 The correct usage for `org-agenda-skip-function' is to bind it with
3899 `let' to scope it dynamically into the agenda-constructing command.
3900 A good way to set it is through options in `org-agenda-custom-commands'.")
3902 (defun org-agenda-skip ()
3903 "Throw to `:skip' in places that should be skipped.
3904 Also moves point to the end of the skipped region, so that search can
3905 continue from there."
3906 (let ((p (point-at-bol)) to)
3907 (when (or
3908 (save-excursion (goto-char p) (looking-at comment-start-skip))
3909 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3910 (get-text-property p :org-archived)
3911 (org-end-of-subtree t))
3912 (and org-agenda-skip-comment-trees
3913 (get-text-property p :org-comment)
3914 (org-end-of-subtree t))
3915 (and (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3916 (org-agenda-skip-eval org-agenda-skip-function)))
3917 (goto-char to))
3918 (org-in-src-block-p t))
3919 (throw :skip t))))
3921 (defun org-agenda-skip-eval (form)
3922 "If FORM is a function or a list, call (or eval) it and return the result.
3923 `save-excursion' and `save-match-data' are wrapped around the call, so point
3924 and match data are returned to the previous state no matter what these
3925 functions do."
3926 (let (fp)
3927 (and form
3928 (or (setq fp (functionp form))
3929 (consp form))
3930 (save-excursion
3931 (save-match-data
3932 (if fp
3933 (funcall form)
3934 (eval form)))))))
3936 (defvar org-agenda-markers nil
3937 "List of all currently active markers created by `org-agenda'.")
3938 (defvar org-agenda-last-marker-time (float-time)
3939 "Creation time of the last agenda marker.")
3941 (defun org-agenda-new-marker (&optional pos)
3942 "Return a new agenda marker.
3943 Maker is at point, or at POS if non-nil. Org mode keeps a list of
3944 these markers and resets them when they are no longer in use."
3945 (let ((m (copy-marker (or pos (point)) t)))
3946 (setq org-agenda-last-marker-time (float-time))
3947 (if org-agenda-buffer
3948 (with-current-buffer org-agenda-buffer
3949 (push m org-agenda-markers))
3950 (push m org-agenda-markers))
3953 (defun org-agenda-reset-markers ()
3954 "Reset markers created by `org-agenda'."
3955 (while org-agenda-markers
3956 (move-marker (pop org-agenda-markers) nil)))
3958 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3959 "Save relative positions of markers in region.
3960 This check for agenda markers in all agenda buffers currently active."
3961 (dolist (buf (buffer-list))
3962 (with-current-buffer buf
3963 (when (eq major-mode 'org-agenda-mode)
3964 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3965 org-agenda-markers)))))
3967 ;;; Entry text mode
3969 (defun org-agenda-entry-text-show-here ()
3970 "Add some text from the entry as context to the current line."
3971 (let (m txt o)
3972 (setq m (org-get-at-bol 'org-hd-marker))
3973 (unless (marker-buffer m)
3974 (error "No marker points to an entry here"))
3975 (setq txt (concat "\n" (org-no-properties
3976 (org-agenda-get-some-entry-text
3977 m org-agenda-entry-text-maxlines
3978 org-agenda-entry-text-leaders))))
3979 (when (string-match "\\S-" txt)
3980 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3981 (overlay-put o 'evaporate t)
3982 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3983 (overlay-put o 'after-string txt))))
3985 (defun org-agenda-entry-text-show ()
3986 "Add entry context for all agenda lines."
3987 (interactive)
3988 (save-excursion
3989 (goto-char (point-max))
3990 (beginning-of-line 1)
3991 (while (not (bobp))
3992 (when (org-get-at-bol 'org-hd-marker)
3993 (org-agenda-entry-text-show-here))
3994 (beginning-of-line 0))))
3996 (defun org-agenda-entry-text-hide ()
3997 "Remove any shown entry context."
3998 (delq nil
3999 (mapcar (lambda (o)
4000 (if (eq (overlay-get o 'org-overlay-type)
4001 'agenda-entry-content)
4002 (progn (delete-overlay o) t)))
4003 (overlays-in (point-min) (point-max)))))
4005 (defun org-agenda-get-day-face (date)
4006 "Return the face DATE should be displayed with."
4007 (cond ((and (functionp org-agenda-day-face-function)
4008 (funcall org-agenda-day-face-function date)))
4009 ((org-agenda-today-p date) 'org-agenda-date-today)
4010 ((memq (calendar-day-of-week date) org-agenda-weekend-days)
4011 'org-agenda-date-weekend)
4012 (t 'org-agenda-date)))
4014 ;;; Agenda timeline
4016 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
4017 (defvar org-agenda-show-log-scoped) ;; dynamically scope in `org-timeline' or `org-agenda-list'
4019 (defun org-timeline (&optional dotodo)
4020 "Show a time-sorted view of the entries in the current org file.
4021 Only entries with a time stamp of today or later will be listed. With
4022 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
4023 under the current date.
4024 If the buffer contains an active region, only check the region for
4025 dates."
4026 (interactive "P")
4027 (let* ((dopast t)
4028 (org-agenda-show-log-scoped org-agenda-show-log)
4029 (org-agenda-show-log org-agenda-show-log-scoped)
4030 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
4031 (current-buffer))))
4032 (date (calendar-current-date))
4033 (beg (if (org-region-active-p) (region-beginning) (point-min)))
4034 (end (if (org-region-active-p) (region-end) (point-max)))
4035 (day-numbers (org-get-all-dates
4036 beg end 'no-ranges
4037 t org-agenda-show-log-scoped ; always include today
4038 org-timeline-show-empty-dates))
4039 (org-deadline-warning-days 0)
4040 (org-agenda-only-exact-dates t)
4041 (today (org-today))
4042 (past t)
4043 args
4044 s e rtn d emptyp)
4045 (setq org-agenda-redo-command
4046 (list 'let
4047 (list (list 'org-agenda-show-log 'org-agenda-show-log))
4048 (list 'org-switch-to-buffer-other-window (current-buffer))
4049 (list 'org-timeline (list 'quote dotodo))))
4050 (put 'org-agenda-redo-command 'org-lprops nil)
4051 (if (not dopast)
4052 ;; Remove past dates from the list of dates.
4053 (setq day-numbers (delq nil (mapcar (lambda(x)
4054 (if (>= x today) x nil))
4055 day-numbers))))
4056 (org-agenda-prepare (concat "Timeline " (file-name-nondirectory entry)))
4057 (org-compile-prefix-format 'timeline)
4058 (org-set-sorting-strategy 'timeline)
4059 (if org-agenda-show-log-scoped (push :closed args))
4060 (push :timestamp args)
4061 (push :deadline args)
4062 (push :scheduled args)
4063 (push :sexp args)
4064 (if dotodo (push :todo args))
4065 (insert "Timeline of file " entry "\n")
4066 (add-text-properties (point-min) (point)
4067 (list 'face 'org-agenda-structure))
4068 (org-agenda-mark-header-line (point-min))
4069 (while (setq d (pop day-numbers))
4070 (if (and (listp d) (eq (car d) :omitted))
4071 (progn
4072 (setq s (point))
4073 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
4074 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
4075 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
4076 (if (and (>= d today)
4077 dopast
4078 past)
4079 (progn
4080 (setq past nil)
4081 (insert (make-string 79 ?-) "\n")))
4082 (setq date (calendar-gregorian-from-absolute d))
4083 (setq s (point))
4084 (setq rtn (and (not emptyp)
4085 (apply 'org-agenda-get-day-entries entry
4086 date args)))
4087 (if (or rtn (equal d today) org-timeline-show-empty-dates)
4088 (progn
4089 (insert
4090 (if (stringp org-agenda-format-date)
4091 (format-time-string org-agenda-format-date
4092 (org-time-from-absolute date))
4093 (funcall org-agenda-format-date date))
4094 "\n")
4095 (put-text-property s (1- (point)) 'face
4096 (org-agenda-get-day-face date))
4097 (put-text-property s (1- (point)) 'org-date-line t)
4098 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4099 (if (equal d today)
4100 (put-text-property s (1- (point)) 'org-today t))
4101 (and rtn (insert (org-agenda-finalize-entries rtn 'timeline) "\n"))
4102 (put-text-property s (1- (point)) 'day d)))))
4103 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
4104 (point-min)))
4105 (add-text-properties
4106 (point-min) (point-max)
4107 `(org-agenda-type timeline org-redo-cmd ,org-agenda-redo-command))
4108 (org-agenda-finalize)
4109 (setq buffer-read-only t)))
4111 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
4112 "Return a list of all relevant day numbers from BEG to END buffer positions.
4113 If NO-RANGES is non-nil, include only the start and end dates of a range,
4114 not every single day in the range. If FORCE-TODAY is non-nil, make
4115 sure that TODAY is included in the list. If INACTIVE is non-nil, also
4116 inactive time stamps (those in square brackets) are included.
4117 When EMPTY is non-nil, also include days without any entries."
4118 (let ((re (concat
4119 (if pre-re pre-re "")
4120 (if inactive org-ts-regexp-both org-ts-regexp)))
4121 dates dates1 date day day1 day2 ts1 ts2 pos)
4122 (if force-today
4123 (setq dates (list (org-today))))
4124 (save-excursion
4125 (goto-char beg)
4126 (while (re-search-forward re end t)
4127 (setq day (time-to-days (org-time-string-to-time
4128 (substring (match-string 1) 0 10)
4129 (current-buffer) (match-beginning 0))))
4130 (or (memq day dates) (push day dates)))
4131 (unless no-ranges
4132 (goto-char beg)
4133 (while (re-search-forward org-tr-regexp end t)
4134 (setq pos (match-beginning 0))
4135 (setq ts1 (substring (match-string 1) 0 10)
4136 ts2 (substring (match-string 2) 0 10)
4137 day1 (time-to-days (org-time-string-to-time
4138 ts1 (current-buffer) pos))
4139 day2 (time-to-days (org-time-string-to-time
4140 ts2 (current-buffer) pos)))
4141 (while (< (setq day1 (1+ day1)) day2)
4142 (or (memq day1 dates) (push day1 dates)))))
4143 (setq dates (sort dates '<))
4144 (when empty
4145 (while (setq day (pop dates))
4146 (setq day2 (car dates))
4147 (push day dates1)
4148 (when (and day2 empty)
4149 (if (or (eq empty t)
4150 (and (numberp empty) (<= (- day2 day) empty)))
4151 (while (< (setq day (1+ day)) day2)
4152 (push (list day) dates1))
4153 (push (cons :omitted (- day2 day)) dates1))))
4154 (setq dates (nreverse dates1)))
4155 dates)))
4157 ;;; Agenda Daily/Weekly
4159 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4160 "Start day for the agenda view.
4161 Custom commands can set this variable in the options section.
4162 This is usually a string like \"2007-11-01\", \"+2d\" or any other
4163 input allowed when reading a date through the Org calendar.
4164 See the docstring of `org-read-date' for details.")
4165 (defvar org-starting-day nil) ; local variable in the agenda buffer
4166 (defvar org-arg-loc nil) ; local variable
4168 (defvar org-agenda-buffer-tmp-name nil)
4169 ;;;###autoload
4170 (defun org-agenda-list (&optional arg start-day span with-hour)
4171 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4172 The view will be for the current day or week, but from the overview buffer
4173 you will be able to go to other days/weeks.
4175 With a numeric prefix argument in an interactive call, the agenda will
4176 span ARG days. Lisp programs should instead specify SPAN to change
4177 the number of days. SPAN defaults to `org-agenda-span'.
4179 START-DAY defaults to TODAY, or to the most recent match for the weekday
4180 given in `org-agenda-start-on-weekday'.
4182 When WITH-HOUR is non-nil, only include scheduled and deadline
4183 items if they have an hour specification like [h]h:mm."
4184 (interactive "P")
4185 (if org-agenda-overriding-arguments
4186 (setq arg (car org-agenda-overriding-arguments)
4187 start-day (nth 1 org-agenda-overriding-arguments)
4188 span (nth 2 org-agenda-overriding-arguments)))
4189 (if (and (integerp arg) (> arg 0))
4190 (setq span arg arg nil))
4191 (catch 'exit
4192 (setq org-agenda-buffer-name
4193 (or org-agenda-buffer-tmp-name
4194 (if org-agenda-sticky
4195 (cond ((and org-keys (stringp org-match))
4196 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4197 (org-keys
4198 (format "*Org Agenda(%s)*" org-keys))
4199 (t "*Org Agenda(a)*")))
4200 org-agenda-buffer-name))
4201 (org-agenda-prepare "Day/Week")
4202 (setq start-day (or start-day org-agenda-start-day))
4203 (if (stringp start-day)
4204 ;; Convert to an absolute day number
4205 (setq start-day (time-to-days (org-read-date nil t start-day))))
4206 (org-compile-prefix-format 'agenda)
4207 (org-set-sorting-strategy 'agenda)
4208 (let* ((span (org-agenda-ndays-to-span (or span org-agenda-span)))
4209 (today (org-today))
4210 (sd (or start-day today))
4211 (ndays (org-agenda-span-to-ndays span sd))
4212 (org-agenda-start-on-weekday
4213 (if (or (eq ndays 7) (eq ndays 14))
4214 org-agenda-start-on-weekday))
4215 (thefiles (org-agenda-files nil 'ifmode))
4216 (files thefiles)
4217 (start (if (or (null org-agenda-start-on-weekday)
4218 (< ndays 7))
4220 (let* ((nt (calendar-day-of-week
4221 (calendar-gregorian-from-absolute sd)))
4222 (n1 org-agenda-start-on-weekday)
4223 (d (- nt n1)))
4224 (- sd (+ (if (< d 0) 7 0) d)))))
4225 (day-numbers (list start))
4226 (day-cnt 0)
4227 (inhibit-redisplay (not debug-on-error))
4228 (org-agenda-show-log-scoped org-agenda-show-log)
4229 s e rtn rtnall file date d start-pos end-pos todayp
4230 clocktable-start clocktable-end filter)
4231 (setq org-agenda-redo-command
4232 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4233 (dotimes (n (1- ndays))
4234 (push (1+ (car day-numbers)) day-numbers))
4235 (setq day-numbers (nreverse day-numbers))
4236 (setq clocktable-start (car day-numbers)
4237 clocktable-end (1+ (or (org-last day-numbers) 0)))
4238 (setq-local org-starting-day (car day-numbers))
4239 (setq-local org-arg-loc arg)
4240 (setq-local org-agenda-current-span (org-agenda-ndays-to-span span))
4241 (unless org-agenda-compact-blocks
4242 (let* ((d1 (car day-numbers))
4243 (d2 (org-last day-numbers))
4244 (w1 (org-days-to-iso-week d1))
4245 (w2 (org-days-to-iso-week d2)))
4246 (setq s (point))
4247 (if org-agenda-overriding-header
4248 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4249 nil 'face 'org-agenda-structure) "\n")
4250 (insert (org-agenda-span-name span)
4251 "-agenda"
4252 (if (< (- d2 d1) 350)
4253 (if (= w1 w2)
4254 (format " (W%02d)" w1)
4255 (format " (W%02d-W%02d)" w1 w2))
4257 ":\n")))
4258 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4259 'org-date-line t))
4260 (org-agenda-mark-header-line s))
4261 (while (setq d (pop day-numbers))
4262 (setq date (calendar-gregorian-from-absolute d)
4263 s (point))
4264 (if (or (setq todayp (= d today))
4265 (and (not start-pos) (= d sd)))
4266 (setq start-pos (point))
4267 (if (and start-pos (not end-pos))
4268 (setq end-pos (point))))
4269 (setq files thefiles
4270 rtnall nil)
4271 (while (setq file (pop files))
4272 (catch 'nextfile
4273 (org-check-agenda-file file)
4274 (let ((org-agenda-entry-types org-agenda-entry-types))
4275 ;; Starred types override non-starred equivalents
4276 (when (member :deadline* org-agenda-entry-types)
4277 (setq org-agenda-entry-types
4278 (delq :deadline org-agenda-entry-types)))
4279 (when (member :scheduled* org-agenda-entry-types)
4280 (setq org-agenda-entry-types
4281 (delq :scheduled org-agenda-entry-types)))
4282 ;; Honor with-hour
4283 (when with-hour
4284 (when (member :deadline org-agenda-entry-types)
4285 (setq org-agenda-entry-types
4286 (delq :deadline org-agenda-entry-types))
4287 (push :deadline* org-agenda-entry-types))
4288 (when (member :scheduled org-agenda-entry-types)
4289 (setq org-agenda-entry-types
4290 (delq :scheduled org-agenda-entry-types))
4291 (push :scheduled* org-agenda-entry-types)))
4292 (unless org-agenda-include-deadlines
4293 (setq org-agenda-entry-types
4294 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4295 (cond
4296 ((memq org-agenda-show-log-scoped '(only clockcheck))
4297 (setq rtn (org-agenda-get-day-entries
4298 file date :closed)))
4299 (org-agenda-show-log-scoped
4300 (setq rtn (apply 'org-agenda-get-day-entries
4301 file date
4302 (append '(:closed) org-agenda-entry-types))))
4304 (setq rtn (apply 'org-agenda-get-day-entries
4305 file date
4306 org-agenda-entry-types)))))
4307 (setq rtnall (append rtnall rtn)))) ;; all entries
4308 (if org-agenda-include-diary
4309 (let ((org-agenda-search-headline-for-time t))
4310 (require 'diary-lib)
4311 (setq rtn (org-get-entries-from-diary date))
4312 (setq rtnall (append rtnall rtn))))
4313 (if (or rtnall org-agenda-show-all-dates)
4314 (progn
4315 (setq day-cnt (1+ day-cnt))
4316 (insert
4317 (if (stringp org-agenda-format-date)
4318 (format-time-string org-agenda-format-date
4319 (org-time-from-absolute date))
4320 (funcall org-agenda-format-date date))
4321 "\n")
4322 (put-text-property s (1- (point)) 'face
4323 (org-agenda-get-day-face date))
4324 (put-text-property s (1- (point)) 'org-date-line t)
4325 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4326 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4327 (when todayp
4328 (put-text-property s (1- (point)) 'org-today t))
4329 (setq rtnall
4330 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4331 (if rtnall (insert ;; all entries
4332 (org-agenda-finalize-entries rtnall 'agenda)
4333 "\n"))
4334 (put-text-property s (1- (point)) 'day d)
4335 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4336 (when (and org-agenda-clockreport-mode clocktable-start)
4337 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4338 ;; the above line is to ensure the restricted range!
4339 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4340 tbl)
4341 (setq p (org-plist-delete p :block))
4342 (setq p (plist-put p :tstart clocktable-start))
4343 (setq p (plist-put p :tend clocktable-end))
4344 (setq p (plist-put p :scope 'agenda))
4345 (setq tbl (apply 'org-clock-get-clocktable p))
4346 (insert tbl)))
4347 (goto-char (point-min))
4348 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4349 (unless (and (pos-visible-in-window-p (point-min))
4350 (pos-visible-in-window-p (point-max)))
4351 (goto-char (1- (point-max)))
4352 (recenter -1)
4353 (if (not (pos-visible-in-window-p (or start-pos 1)))
4354 (progn
4355 (goto-char (or start-pos 1))
4356 (recenter 1))))
4357 (goto-char (or start-pos 1))
4358 (add-text-properties (point-min) (point-max)
4359 `(org-agenda-type agenda
4360 org-last-args (,arg ,start-day ,span)
4361 org-redo-cmd ,org-agenda-redo-command
4362 org-series-cmd ,org-cmd))
4363 (if (eq org-agenda-show-log-scoped 'clockcheck)
4364 (org-agenda-show-clocking-issues))
4365 (org-agenda-finalize)
4366 (setq buffer-read-only t)
4367 (message ""))))
4369 (defun org-agenda-ndays-to-span (n)
4370 "Return a span symbol for a span of N days, or N if none matches."
4371 (cond ((symbolp n) n)
4372 ((= n 1) 'day)
4373 ((= n 7) 'week)
4374 ((= n 14) 'fortnight)
4375 (t n)))
4377 (defun org-agenda-span-to-ndays (span &optional start-day)
4378 "Return ndays from SPAN, possibly starting at START-DAY.
4379 START-DAY is an absolute time value."
4380 (cond ((numberp span) span)
4381 ((eq span 'day) 1)
4382 ((eq span 'week) 7)
4383 ((eq span 'fortnight) 14)
4384 ((eq span 'month)
4385 (let ((date (calendar-gregorian-from-absolute start-day)))
4386 (calendar-last-day-of-month (car date) (cl-caddr date))))
4387 ((eq span 'year)
4388 (let ((date (calendar-gregorian-from-absolute start-day)))
4389 (if (calendar-leap-year-p (cl-caddr date)) 366 365)))))
4391 (defun org-agenda-span-name (span)
4392 "Return a SPAN name."
4393 (if (null span)
4395 (if (symbolp span)
4396 (capitalize (symbol-name span))
4397 (format "%d days" span))))
4399 ;;; Agenda word search
4401 (defvar org-agenda-search-history nil)
4403 (defvar org-search-syntax-table nil
4404 "Special syntax table for Org search.
4405 In this table, we have single quotes not as word constituents, to
4406 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4408 (defvar org-mode-syntax-table) ; From org.el
4409 (defun org-search-syntax-table ()
4410 (unless org-search-syntax-table
4411 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4412 (modify-syntax-entry ?' "." org-search-syntax-table)
4413 (modify-syntax-entry ?` "." org-search-syntax-table))
4414 org-search-syntax-table)
4416 (defvar org-agenda-last-search-view-search-was-boolean nil)
4418 ;;;###autoload
4419 (defun org-search-view (&optional todo-only string edit-at)
4420 "Show all entries that contain a phrase or words or regular expressions.
4422 With optional prefix argument TODO-ONLY, only consider entries that are
4423 TODO entries. The argument STRING can be used to pass a default search
4424 string into this function. If EDIT-AT is non-nil, it means that the
4425 user should get a chance to edit this string, with cursor at position
4426 EDIT-AT.
4428 The search string can be viewed either as a phrase that should be found as
4429 is, or it can be broken into a number of snippets, each of which must match
4430 in a Boolean way to select an entry. The default depends on the variable
4431 `org-agenda-search-view-always-boolean'.
4432 Even if this is turned off (the default) you can always switch to
4433 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4435 The default is a direct search of the whole phrase, where each space in
4436 the search string can expand to an arbitrary amount of whitespace,
4437 including newlines.
4439 If using a Boolean search, the search string is split on whitespace and
4440 each snippet is searched separately, with logical AND to select an entry.
4441 Words prefixed with a minus must *not* occur in the entry. Words without
4442 a prefix or prefixed with a plus must occur in the entry. Matching is
4443 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4444 match whole words, not parts of a word) if
4445 `org-agenda-search-view-force-full-words' is set (default is nil).
4447 Boolean search snippets enclosed by curly braces are interpreted as
4448 regular expressions that must or (when preceded with \"-\") must not
4449 match in the entry. Snippets enclosed into double quotes will be taken
4450 as a whole, to include whitespace.
4452 - If the search string starts with an asterisk, search only in headlines.
4453 - If (possibly after the leading star) the search string starts with an
4454 exclamation mark, this also means to look at TODO entries only, an effect
4455 that can also be achieved with a prefix argument.
4456 - If (possibly after star and exclamation mark) the search string starts
4457 with a colon, this will mean that the (non-regexp) snippets of the
4458 Boolean search must match as full words.
4460 This command searches the agenda files, and in addition the files listed
4461 in `org-agenda-text-search-extra-files'."
4462 (interactive "P")
4463 (if org-agenda-overriding-arguments
4464 (setq todo-only (car org-agenda-overriding-arguments)
4465 string (nth 1 org-agenda-overriding-arguments)
4466 edit-at (nth 2 org-agenda-overriding-arguments)))
4467 (let* ((props (list 'face nil
4468 'done-face 'org-agenda-done
4469 'org-not-done-regexp org-not-done-regexp
4470 'org-todo-regexp org-todo-regexp
4471 'org-complex-heading-regexp org-complex-heading-regexp
4472 'mouse-face 'highlight
4473 'help-echo (format "mouse-2 or RET jump to location")))
4474 (full-words org-agenda-search-view-force-full-words)
4475 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4476 regexp rtn rtnall files file pos inherited-tags
4477 marker category level tags c neg re boolean
4478 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4479 (unless (and (not edit-at)
4480 (stringp string)
4481 (string-match "\\S-" string))
4482 (setq string (read-string
4483 (if org-agenda-search-view-always-boolean
4484 "[+-]Word/{Regexp} ...: "
4485 "Phrase or [+-]Word/{Regexp} ...: ")
4486 (cond
4487 ((integerp edit-at) (cons string edit-at))
4488 (edit-at string))
4489 'org-agenda-search-history)))
4490 (catch 'exit
4491 (if org-agenda-sticky
4492 (setq org-agenda-buffer-name
4493 (if (stringp string)
4494 (format "*Org Agenda(%s:%s)*"
4495 (or org-keys (or (and todo-only "S") "s")) string)
4496 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4497 (org-agenda-prepare "SEARCH")
4498 (org-compile-prefix-format 'search)
4499 (org-set-sorting-strategy 'search)
4500 (setq org-agenda-redo-command
4501 (list 'org-search-view (if todo-only t nil)
4502 (list 'if 'current-prefix-arg nil string)))
4503 (setq org-agenda-query-string string)
4504 (if (equal (string-to-char string) ?*)
4505 (setq hdl-only t
4506 words (substring string 1))
4507 (setq words string))
4508 (when (equal (string-to-char words) ?!)
4509 (setq todo-only t
4510 words (substring words 1)))
4511 (when (equal (string-to-char words) ?:)
4512 (setq full-words t
4513 words (substring words 1)))
4514 (if (or org-agenda-search-view-always-boolean
4515 (member (string-to-char words) '(?- ?+ ?\{)))
4516 (setq boolean t))
4517 (setq words (org-split-string words))
4518 (let (www w)
4519 (while (setq w (pop words))
4520 (while (and (string-match "\\\\\\'" w) words)
4521 (setq w (concat (substring w 0 -1) " " (pop words))))
4522 (push w www))
4523 (setq words (nreverse www) www nil)
4524 (while (setq w (pop words))
4525 (when (and (string-match "\\`[-+]?{" w)
4526 (not (string-match "}\\'" w)))
4527 (while (and words (not (string-match "}\\'" (car words))))
4528 (setq w (concat w " " (pop words))))
4529 (setq w (concat w " " (pop words))))
4530 (push w www))
4531 (setq words (nreverse www)))
4532 (setq org-agenda-last-search-view-search-was-boolean boolean)
4533 (when boolean
4534 (let (wds w)
4535 (while (setq w (pop words))
4536 (if (or (equal (substring w 0 1) "\"")
4537 (and (> (length w) 1)
4538 (member (substring w 0 1) '("+" "-"))
4539 (equal (substring w 1 2) "\"")))
4540 (while (and words (not (equal (substring w -1) "\"")))
4541 (setq w (concat w " " (pop words)))))
4542 (and (string-match "\\`\\([-+]?\\)\"" w)
4543 (setq w (replace-match "\\1" nil nil w)))
4544 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4545 (push w wds))
4546 (setq words (nreverse wds))))
4547 (if boolean
4548 (mapc (lambda (w)
4549 (setq c (string-to-char w))
4550 (if (equal c ?-)
4551 (setq neg t w (substring w 1))
4552 (if (equal c ?+)
4553 (setq neg nil w (substring w 1))
4554 (setq neg nil)))
4555 (if (string-match "\\`{.*}\\'" w)
4556 (setq re (substring w 1 -1))
4557 (if full-words
4558 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4559 (setq re (regexp-quote (downcase w)))))
4560 (if neg (push re regexps-) (push re regexps+)))
4561 words)
4562 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4563 regexps+))
4564 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4565 (if (not regexps+)
4566 (setq regexp org-outline-regexp-bol)
4567 (setq regexp (pop regexps+))
4568 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4569 regexp))))
4570 (setq files (org-agenda-files nil 'ifmode))
4571 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4572 (pop org-agenda-text-search-extra-files)
4573 (setq files (org-add-archive-files files)))
4574 (setq files (append files org-agenda-text-search-extra-files)
4575 rtnall nil)
4576 (while (setq file (pop files))
4577 (setq ee nil)
4578 (catch 'nextfile
4579 (org-check-agenda-file file)
4580 (setq buffer (if (file-exists-p file)
4581 (org-get-agenda-file-buffer file)
4582 (error "No such file %s" file)))
4583 (if (not buffer)
4584 ;; If file does not exist, make sure an error message is sent
4585 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4586 file))))
4587 (with-current-buffer buffer
4588 (with-syntax-table (org-search-syntax-table)
4589 (unless (derived-mode-p 'org-mode)
4590 (error "Agenda file %s is not in `org-mode'" file))
4591 (let ((case-fold-search t))
4592 (save-excursion
4593 (save-restriction
4594 (if (eq buffer org-agenda-restrict)
4595 (narrow-to-region org-agenda-restrict-begin
4596 org-agenda-restrict-end)
4597 (widen))
4598 (goto-char (point-min))
4599 (unless (or (org-at-heading-p)
4600 (outline-next-heading))
4601 (throw 'nextfile t))
4602 (goto-char (max (point-min) (1- (point))))
4603 (while (re-search-forward regexp nil t)
4604 (org-back-to-heading t)
4605 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4606 (> (org-reduced-level (org-outline-level))
4607 org-agenda-search-view-max-outline-level)
4608 (forward-line -1)
4609 (org-back-to-heading t)))
4610 (skip-chars-forward "* ")
4611 (setq beg (point-at-bol)
4612 beg1 (point)
4613 end (progn
4614 (outline-next-heading)
4615 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4616 (> (org-reduced-level (org-outline-level))
4617 org-agenda-search-view-max-outline-level)
4618 (forward-line 1)
4619 (outline-next-heading)))
4620 (point)))
4622 (catch :skip
4623 (goto-char beg)
4624 (org-agenda-skip)
4625 (setq str (buffer-substring-no-properties
4626 (point-at-bol)
4627 (if hdl-only (point-at-eol) end)))
4628 (mapc (lambda (wr) (when (string-match wr str)
4629 (goto-char (1- end))
4630 (throw :skip t)))
4631 regexps-)
4632 (mapc (lambda (wr) (unless (string-match wr str)
4633 (goto-char (1- end))
4634 (throw :skip t)))
4635 (if todo-only
4636 (cons (concat "^\\*+[ \t]+"
4637 org-not-done-regexp)
4638 regexps+)
4639 regexps+))
4640 (goto-char beg)
4641 (setq marker (org-agenda-new-marker (point))
4642 category (org-get-category)
4643 level (make-string (org-reduced-level (org-outline-level)) ? )
4644 inherited-tags
4645 (or (eq org-agenda-show-inherited-tags 'always)
4646 (and (listp org-agenda-show-inherited-tags)
4647 (memq 'todo org-agenda-show-inherited-tags))
4648 (and (eq org-agenda-show-inherited-tags t)
4649 (or (eq org-agenda-use-tag-inheritance t)
4650 (memq 'todo org-agenda-use-tag-inheritance))))
4651 tags (org-get-tags-at nil (not inherited-tags))
4652 txt (org-agenda-format-item
4654 (buffer-substring-no-properties
4655 beg1 (point-at-eol))
4656 level category tags t))
4657 (org-add-props txt props
4658 'org-marker marker 'org-hd-marker marker
4659 'org-todo-regexp org-todo-regexp
4660 'level level
4661 'org-complex-heading-regexp org-complex-heading-regexp
4662 'priority 1000
4663 'type "search")
4664 (push txt ee)
4665 (goto-char (1- end))))))))))
4666 (setq rtn (nreverse ee))
4667 (setq rtnall (append rtnall rtn)))
4668 (if org-agenda-overriding-header
4669 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4670 nil 'face 'org-agenda-structure) "\n")
4671 (insert "Search words: ")
4672 (add-text-properties (point-min) (1- (point))
4673 (list 'face 'org-agenda-structure))
4674 (setq pos (point))
4675 (insert string "\n")
4676 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4677 (setq pos (point))
4678 (unless org-agenda-multi
4679 (insert (substitute-command-keys "\
4680 Press `\\[org-agenda-manipulate-query-add]', \
4681 `\\[org-agenda-manipulate-query-subtract]' to add/sub word, \
4682 `\\[org-agenda-manipulate-query-add-re]', \
4683 `\\[org-agenda-manipulate-query-subtract-re]' to add/sub regexp, \
4684 `\\[universal-argument] \\[org-agenda-redo]' to edit\n"))
4685 (add-text-properties pos (1- (point))
4686 (list 'face 'org-agenda-structure))))
4687 (org-agenda-mark-header-line (point-min))
4688 (when rtnall
4689 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4690 (goto-char (point-min))
4691 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4692 (add-text-properties (point-min) (point-max)
4693 `(org-agenda-type search
4694 org-last-args (,todo-only ,string ,edit-at)
4695 org-redo-cmd ,org-agenda-redo-command
4696 org-series-cmd ,org-cmd))
4697 (org-agenda-finalize)
4698 (setq buffer-read-only t))))
4700 ;;; Agenda TODO list
4702 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4703 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4704 (concat
4705 (if (or (equal keywords "ALL") (not keywords))
4706 (propertize "ALL" 'face 'warning)
4707 (mapconcat
4708 (lambda (kw)
4709 (propertize kw 'face (org-get-todo-face kw)))
4710 (org-split-string keywords "|")
4711 "|"))
4712 "\n"))
4714 (defvar org-select-this-todo-keyword nil)
4715 (defvar org-last-arg nil)
4717 ;;;###autoload
4718 (defun org-todo-list (&optional arg)
4719 "Show all (not done) TODO entries from all agenda file in a single list.
4720 The prefix arg can be used to select a specific TODO keyword and limit
4721 the list to these. When using \\[universal-argument], you will be prompted
4722 for a keyword. A numeric prefix directly selects the Nth keyword in
4723 `org-todo-keywords-1'."
4724 (interactive "P")
4725 (if org-agenda-overriding-arguments
4726 (setq arg org-agenda-overriding-arguments))
4727 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4728 (let* ((today (org-today))
4729 (date (calendar-gregorian-from-absolute today))
4730 (kwds org-todo-keywords-for-agenda)
4731 (completion-ignore-case t)
4732 (org-select-this-todo-keyword
4733 (if (stringp arg) arg
4734 (and arg (integerp arg) (> arg 0)
4735 (nth (1- arg) kwds))))
4736 rtn rtnall files file pos)
4737 (when (equal arg '(4))
4738 (setq org-select-this-todo-keyword
4739 (completing-read "Keyword (or KWD1|K2D2|...): "
4740 (mapcar #'list kwds) nil nil)))
4741 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4742 (catch 'exit
4743 (if org-agenda-sticky
4744 (setq org-agenda-buffer-name
4745 (if (stringp org-select-this-todo-keyword)
4746 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4747 org-select-this-todo-keyword)
4748 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4749 (org-agenda-prepare "TODO")
4750 (org-compile-prefix-format 'todo)
4751 (org-set-sorting-strategy 'todo)
4752 (setq org-agenda-redo-command
4753 `(org-todo-list (or (and (numberp current-prefix-arg)
4754 current-prefix-arg)
4755 ,org-select-this-todo-keyword
4756 current-prefix-arg ,arg)))
4757 (setq files (org-agenda-files nil 'ifmode)
4758 rtnall nil)
4759 (while (setq file (pop files))
4760 (catch 'nextfile
4761 (org-check-agenda-file file)
4762 (setq rtn (org-agenda-get-day-entries file date :todo))
4763 (setq rtnall (append rtnall rtn))))
4764 (if org-agenda-overriding-header
4765 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4766 nil 'face 'org-agenda-structure) "\n")
4767 (insert "Global list of TODO items of type: ")
4768 (add-text-properties (point-min) (1- (point))
4769 (list 'face 'org-agenda-structure
4770 'short-heading
4771 (concat "ToDo: "
4772 (or org-select-this-todo-keyword "ALL"))))
4773 (org-agenda-mark-header-line (point-min))
4774 (insert (org-agenda-propertize-selected-todo-keywords
4775 org-select-this-todo-keyword))
4776 (setq pos (point))
4777 (unless org-agenda-multi
4778 (insert (substitute-command-keys "Available with \
4779 `N \\[org-agenda-redo]': (0)[ALL]"))
4780 (let ((n 0) s)
4781 (mapc (lambda (x)
4782 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4783 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4784 (insert "\n "))
4785 (insert " " s))
4786 kwds))
4787 (insert "\n"))
4788 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4789 (org-agenda-mark-header-line (point-min))
4790 (when rtnall
4791 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4792 (goto-char (point-min))
4793 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4794 (add-text-properties (point-min) (point-max)
4795 `(org-agenda-type todo
4796 org-last-args ,arg
4797 org-redo-cmd ,org-agenda-redo-command
4798 org-series-cmd ,org-cmd))
4799 (org-agenda-finalize)
4800 (setq buffer-read-only t))))
4802 ;;; Agenda tags match
4804 ;;;###autoload
4805 (defun org-tags-view (&optional todo-only match)
4806 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4807 The prefix arg TODO-ONLY limits the search to TODO entries."
4808 (interactive "P")
4809 (if org-agenda-overriding-arguments
4810 (setq todo-only (car org-agenda-overriding-arguments)
4811 match (nth 1 org-agenda-overriding-arguments)))
4812 (let* ((org-tags-match-list-sublevels
4813 org-tags-match-list-sublevels)
4814 (completion-ignore-case t)
4815 rtn rtnall files file pos matcher
4816 buffer)
4817 (when (and (stringp match) (not (string-match "\\S-" match)))
4818 (setq match nil))
4819 (catch 'exit
4820 (if org-agenda-sticky
4821 (setq org-agenda-buffer-name
4822 (if (stringp match)
4823 (format "*Org Agenda(%s:%s)*"
4824 (or org-keys (or (and todo-only "M") "m")) match)
4825 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4826 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4827 ;; expanding tags within `org-make-tags-matcher'
4828 (org-agenda-prepare (concat "TAGS " match))
4829 (setq org--matcher-tags-todo-only todo-only
4830 matcher (org-make-tags-matcher match)
4831 match (car matcher)
4832 matcher (cdr matcher))
4833 (org-compile-prefix-format 'tags)
4834 (org-set-sorting-strategy 'tags)
4835 (setq org-agenda-query-string match)
4836 (setq org-agenda-redo-command
4837 (list 'org-tags-view
4838 `(quote ,org--matcher-tags-todo-only)
4839 `(if current-prefix-arg nil ,org-agenda-query-string)))
4840 (setq files (org-agenda-files nil 'ifmode)
4841 rtnall nil)
4842 (while (setq file (pop files))
4843 (catch 'nextfile
4844 (org-check-agenda-file file)
4845 (setq buffer (if (file-exists-p file)
4846 (org-get-agenda-file-buffer file)
4847 (error "No such file %s" file)))
4848 (if (not buffer)
4849 ;; If file does not exist, error message to agenda
4850 (setq rtn (list
4851 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4852 rtnall (append rtnall rtn))
4853 (with-current-buffer buffer
4854 (unless (derived-mode-p 'org-mode)
4855 (error "Agenda file %s is not in `org-mode'" file))
4856 (save-excursion
4857 (save-restriction
4858 (if (eq buffer org-agenda-restrict)
4859 (narrow-to-region org-agenda-restrict-begin
4860 org-agenda-restrict-end)
4861 (widen))
4862 (setq rtn (org-scan-tags 'agenda
4863 matcher
4864 org--matcher-tags-todo-only))
4865 (setq rtnall (append rtnall rtn))))))))
4866 (if org-agenda-overriding-header
4867 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4868 nil 'face 'org-agenda-structure) "\n")
4869 (insert "Headlines with TAGS match: ")
4870 (add-text-properties (point-min) (1- (point))
4871 (list 'face 'org-agenda-structure
4872 'short-heading
4873 (concat "Match: " match)))
4874 (setq pos (point))
4875 (insert match "\n")
4876 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4877 (setq pos (point))
4878 (unless org-agenda-multi
4879 (insert (substitute-command-keys
4880 "Press `\\[universal-argument] \\[org-agenda-redo]' \
4881 to search again with new search string\n")))
4882 (add-text-properties pos (1- (point))
4883 (list 'face 'org-agenda-structure)))
4884 (org-agenda-mark-header-line (point-min))
4885 (when rtnall
4886 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4887 (goto-char (point-min))
4888 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4889 (add-text-properties
4890 (point-min) (point-max)
4891 `(org-agenda-type tags
4892 org-last-args (,org--matcher-tags-todo-only ,match)
4893 org-redo-cmd ,org-agenda-redo-command
4894 org-series-cmd ,org-cmd))
4895 (org-agenda-finalize)
4896 (setq buffer-read-only t))))
4898 ;;; Agenda Finding stuck projects
4900 (defvar org-agenda-skip-regexp nil
4901 "Regular expression used in skipping subtrees for the agenda.
4902 This is basically a temporary global variable that can be set and then
4903 used by user-defined selections using `org-agenda-skip-function'.")
4905 (defvar org-agenda-overriding-header nil
4906 "When set during agenda, todo and tags searches it replaces the header.
4907 This variable should not be set directly, but custom commands can bind it
4908 in the options section.")
4910 (defun org-agenda-skip-entry-when-regexp-matches ()
4911 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4912 If yes, it returns the end position of this entry, causing agenda commands
4913 to skip the entry but continuing the search in the subtree. This is a
4914 function that can be put into `org-agenda-skip-function' for the duration
4915 of a command."
4916 (let ((end (save-excursion (org-end-of-subtree t)))
4917 skip)
4918 (save-excursion
4919 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4920 (and skip end)))
4922 (defun org-agenda-skip-subtree-when-regexp-matches ()
4923 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4924 If yes, it returns the end position of this tree, causing agenda commands
4925 to skip this subtree. This is a function that can be put into
4926 `org-agenda-skip-function' for the duration of a command."
4927 (let ((end (save-excursion (org-end-of-subtree t)))
4928 skip)
4929 (save-excursion
4930 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4931 (and skip end)))
4933 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4934 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4935 If yes, it returns the end position of the current entry (NOT the tree),
4936 causing agenda commands to skip the entry but continuing the search in
4937 the subtree. This is a function that can be put into
4938 `org-agenda-skip-function' for the duration of a command. An important
4939 use of this function is for the stuck project list."
4940 (let ((end (save-excursion (org-end-of-subtree t)))
4941 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4942 skip)
4943 (save-excursion
4944 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4945 (and skip entry-end)))
4947 (defun org-agenda-skip-entry-if (&rest conditions)
4948 "Skip entry if any of CONDITIONS is true.
4949 See `org-agenda-skip-if' for details."
4950 (org-agenda-skip-if nil conditions))
4952 (defun org-agenda-skip-subtree-if (&rest conditions)
4953 "Skip subtree if any of CONDITIONS is true.
4954 See `org-agenda-skip-if' for details."
4955 (org-agenda-skip-if t conditions))
4957 (defun org-agenda-skip-if (subtree conditions)
4958 "Checks current entity for CONDITIONS.
4959 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4960 the entry (i.e. the text before the next heading) is checked.
4962 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4963 from different tests. Valid conditions are:
4965 scheduled Check if there is a scheduled cookie
4966 notscheduled Check if there is no scheduled cookie
4967 deadline Check if there is a deadline
4968 notdeadline Check if there is no deadline
4969 timestamp Check if there is a timestamp (also deadline or scheduled)
4970 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4971 regexp Check if regexp matches
4972 notregexp Check if regexp does not match.
4973 todo Check if TODO keyword matches
4974 nottodo Check if TODO keyword does not match
4976 The regexp is taken from the conditions list, it must come right after
4977 the `regexp' or `notregexp' element.
4979 `todo' and `nottodo' accept as an argument a list of todo
4980 keywords, which may include \"*\" to match any todo keyword.
4982 (org-agenda-skip-entry-if \\='todo \\='(\"TODO\" \"WAITING\"))
4984 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4986 Instead of a list, a keyword class may be given. For example:
4988 (org-agenda-skip-entry-if \\='nottodo \\='done)
4990 would skip entries that haven't been marked with any of \"DONE\"
4991 keywords. Possible classes are: `todo', `done', `any'.
4993 If any of these conditions is met, this function returns the end point of
4994 the entity, causing the search to continue from there. This is a function
4995 that can be put into `org-agenda-skip-function' for the duration of a command."
4996 (let (beg end m)
4997 (org-back-to-heading t)
4998 (setq beg (point)
4999 end (if subtree
5000 (progn (org-end-of-subtree t) (point))
5001 (progn (outline-next-heading) (1- (point)))))
5002 (goto-char beg)
5003 (and
5005 (and (memq 'scheduled conditions)
5006 (re-search-forward org-scheduled-time-regexp end t))
5007 (and (memq 'notscheduled conditions)
5008 (not (re-search-forward org-scheduled-time-regexp end t)))
5009 (and (memq 'deadline conditions)
5010 (re-search-forward org-deadline-time-regexp end t))
5011 (and (memq 'notdeadline conditions)
5012 (not (re-search-forward org-deadline-time-regexp end t)))
5013 (and (memq 'timestamp conditions)
5014 (re-search-forward org-ts-regexp end t))
5015 (and (memq 'nottimestamp conditions)
5016 (not (re-search-forward org-ts-regexp end t)))
5017 (and (setq m (memq 'regexp conditions))
5018 (stringp (nth 1 m))
5019 (re-search-forward (nth 1 m) end t))
5020 (and (setq m (memq 'notregexp conditions))
5021 (stringp (nth 1 m))
5022 (not (re-search-forward (nth 1 m) end t)))
5023 (and (or
5024 (setq m (memq 'nottodo conditions))
5025 (setq m (memq 'todo-unblocked conditions))
5026 (setq m (memq 'nottodo-unblocked conditions))
5027 (setq m (memq 'todo conditions)))
5028 (org-agenda-skip-if-todo m end)))
5029 end)))
5031 (defun org-agenda-skip-if-todo (args end)
5032 "Helper function for `org-agenda-skip-if', do not use it directly.
5033 ARGS is a list with first element either `todo', `nottodo',
5034 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
5035 a list of TODO keywords, or a state symbol `todo' or `done' or
5036 `any'."
5037 (let ((kw (car args))
5038 (arg (cadr args))
5039 todo-wds todo-re)
5040 (setq todo-wds
5041 (org-uniquify
5042 (cond
5043 ((listp arg) ;; list of keywords
5044 (if (member "*" arg)
5045 (mapcar 'substring-no-properties org-todo-keywords-1)
5046 arg))
5047 ((symbolp arg) ;; keyword class name
5048 (cond
5049 ((eq arg 'todo)
5050 (org-delete-all org-done-keywords
5051 (mapcar 'substring-no-properties
5052 org-todo-keywords-1)))
5053 ((eq arg 'done) org-done-keywords)
5054 ((eq arg 'any)
5055 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
5056 (setq todo-re
5057 (concat "^\\*+[ \t]+\\<\\("
5058 (mapconcat 'identity todo-wds "\\|")
5059 "\\)\\>"))
5060 (cond
5061 ((eq kw 'todo) (re-search-forward todo-re end t))
5062 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
5063 ((eq kw 'todo-unblocked)
5064 (catch 'unblocked
5065 (while (re-search-forward todo-re end t)
5066 (or (org-entry-blocked-p) (throw 'unblocked t)))
5067 nil))
5068 ((eq kw 'nottodo-unblocked)
5069 (catch 'unblocked
5070 (while (re-search-forward todo-re end t)
5071 (or (org-entry-blocked-p) (throw 'unblocked nil)))
5075 ;;;###autoload
5076 (defun org-agenda-list-stuck-projects (&rest ignore)
5077 "Create agenda view for projects that are stuck.
5078 Stuck projects are project that have no next actions. For the definitions
5079 of what a project is and how to check if it stuck, customize the variable
5080 `org-stuck-projects'."
5081 (interactive)
5082 (let* ((org-agenda-skip-function
5083 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
5084 ;; We could have used org-agenda-skip-if here.
5085 (org-agenda-overriding-header
5086 (or org-agenda-overriding-header "List of stuck projects: "))
5087 (matcher (nth 0 org-stuck-projects))
5088 (todo (nth 1 org-stuck-projects))
5089 (todo-wds (if (member "*" todo)
5090 (progn
5091 (org-agenda-prepare-buffers (org-agenda-files
5092 nil 'ifmode))
5093 (org-delete-all
5094 org-done-keywords-for-agenda
5095 (copy-sequence org-todo-keywords-for-agenda)))
5096 todo))
5097 (todo-re (concat "^\\*+[ \t]+\\("
5098 (mapconcat 'identity todo-wds "\\|")
5099 "\\)\\>"))
5100 (tags (nth 2 org-stuck-projects))
5101 (tags-re (if (member "*" tags)
5102 (concat org-outline-regexp-bol
5103 ".*:[[:alnum:]_@#%]+:[ \t]*$")
5104 (if tags
5105 (concat org-outline-regexp-bol
5106 ".*:\\("
5107 (mapconcat #'identity tags "\\|")
5108 "\\):[[:alnum:]_@#%:]*[ \t]*$"))))
5109 (gen-re (nth 3 org-stuck-projects))
5110 (re-list
5111 (delq nil
5112 (list
5113 (if todo todo-re)
5114 (if tags tags-re)
5115 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
5116 gen-re)))))
5117 (setq org-agenda-skip-regexp
5118 (if re-list
5119 (mapconcat 'identity re-list "\\|")
5120 (error "No information how to identify unstuck projects")))
5121 (org-tags-view nil matcher)
5122 (setq org-agenda-buffer-name (buffer-name))
5123 (with-current-buffer org-agenda-buffer-name
5124 (setq org-agenda-redo-command
5125 `(org-agenda-list-stuck-projects ,current-prefix-arg)))))
5127 ;;; Diary integration
5129 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
5130 (defvar diary-list-entries-hook)
5131 (defvar diary-time-regexp)
5132 (defun org-get-entries-from-diary (date)
5133 "Get the (Emacs Calendar) diary entries for DATE."
5134 (require 'diary-lib)
5135 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
5136 (diary-display-function 'diary-fancy-display)
5137 (pop-up-frames nil)
5138 (diary-list-entries-hook
5139 (cons 'org-diary-default-entry diary-list-entries-hook))
5140 (diary-file-name-prefix nil) ; turn this feature off
5141 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
5142 entries
5143 (org-disable-agenda-to-diary t))
5144 (save-excursion
5145 (save-window-excursion
5146 (funcall (if (fboundp 'diary-list-entries)
5147 'diary-list-entries 'list-diary-entries)
5148 date 1)))
5149 (if (not (get-buffer diary-fancy-buffer))
5150 (setq entries nil)
5151 (with-current-buffer diary-fancy-buffer
5152 (setq buffer-read-only nil)
5153 (if (zerop (buffer-size))
5154 ;; No entries
5155 (setq entries nil)
5156 ;; Omit the date and other unnecessary stuff
5157 (org-agenda-cleanup-fancy-diary)
5158 ;; Add prefix to each line and extend the text properties
5159 (if (zerop (buffer-size))
5160 (setq entries nil)
5161 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
5162 (setq entries
5163 (with-temp-buffer
5164 (insert entries) (goto-char (point-min))
5165 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5166 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5167 (replace-match (concat "; " (match-string 1)))))
5168 (buffer-string)))))
5169 (set-buffer-modified-p nil)
5170 (kill-buffer diary-fancy-buffer)))
5171 (when entries
5172 (setq entries (org-split-string entries "\n"))
5173 (setq entries
5174 (mapcar
5175 (lambda (x)
5176 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5177 ;; Extend the text properties to the beginning of the line
5178 (org-add-props x (text-properties-at (1- (length x)) x)
5179 'type "diary" 'date date 'face 'org-agenda-diary))
5180 entries)))))
5182 (defvar org-agenda-cleanup-fancy-diary-hook nil
5183 "Hook run when the fancy diary buffer is cleaned up.")
5185 (defun org-agenda-cleanup-fancy-diary ()
5186 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5187 This gets rid of the date, the underline under the date, and
5188 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5189 date. It also removes lines that contain only whitespace."
5190 (goto-char (point-min))
5191 (if (looking-at ".*?:[ \t]*")
5192 (progn
5193 (replace-match "")
5194 (re-search-forward "\n=+$" nil t)
5195 (replace-match "")
5196 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5197 (re-search-forward "\n=+$" nil t)
5198 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5199 (goto-char (point-min))
5200 (while (re-search-forward "^ +\n" nil t)
5201 (replace-match ""))
5202 (goto-char (point-min))
5203 (if (re-search-forward "^Org-mode dummy\n?" nil t)
5204 (replace-match ""))
5205 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5207 ;; Make sure entries from the diary have the right text properties.
5208 (eval-after-load "diary-lib"
5209 '(if (boundp 'diary-modify-entry-list-string-function)
5210 ;; We can rely on the hook, nothing to do
5212 ;; Hook not available, must use advice to make this work
5213 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5214 "Make the position visible."
5215 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5216 (stringp string)
5217 buffer-file-name)
5218 (setq string (org-modify-diary-entry-string string))))))
5220 (defun org-modify-diary-entry-string (string)
5221 "Add text properties to string, allowing Org to act on it."
5222 (org-add-props string nil
5223 'mouse-face 'highlight
5224 'help-echo (if buffer-file-name
5225 (format "mouse-2 or RET jump to diary file %s"
5226 (abbreviate-file-name buffer-file-name))
5228 'org-agenda-diary-link t
5229 'org-marker (org-agenda-new-marker (point-at-bol))))
5231 (defun org-diary-default-entry ()
5232 "Add a dummy entry to the diary.
5233 Needed to avoid empty dates which mess up holiday display."
5234 ;; Catch the error if dealing with the new add-to-diary-alist
5235 (when org-disable-agenda-to-diary
5236 (condition-case nil
5237 (org-add-to-diary-list original-date "Org-mode dummy" "")
5238 (error
5239 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
5241 (defun org-add-to-diary-list (&rest args)
5242 (if (fboundp 'diary-add-to-list)
5243 (apply 'diary-add-to-list args)
5244 (apply 'add-to-diary-list args)))
5246 (defvar org-diary-last-run-time nil)
5248 ;;;###autoload
5249 (defun org-diary (&rest args)
5250 "Return diary information from org files.
5251 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5252 It accesses org files and extracts information from those files to be
5253 listed in the diary. The function accepts arguments specifying what
5254 items should be listed. For a list of arguments allowed here, see the
5255 variable `org-agenda-entry-types'.
5257 The call in the diary file should look like this:
5259 &%%(org-diary) ~/path/to/some/orgfile.org
5261 Use a separate line for each org file to check. Or, if you omit the file name,
5262 all files listed in `org-agenda-files' will be checked automatically:
5264 &%%(org-diary)
5266 If you don't give any arguments (as in the example above), the default value
5267 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5268 So the example above may also be written as
5270 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5272 The function expects the lisp variables `entry' and `date' to be provided
5273 by the caller, because this is how the calendar works. Don't use this
5274 function from a program - use `org-agenda-get-day-entries' instead."
5275 (when (> (- (float-time)
5276 org-agenda-last-marker-time)
5278 ;; I am not sure if this works with sticky agendas, because the marker
5279 ;; list is then no longer a global variable.
5280 (org-agenda-reset-markers))
5281 (org-compile-prefix-format 'agenda)
5282 (org-set-sorting-strategy 'agenda)
5283 (setq args (or args org-agenda-entry-types))
5284 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5285 (list entry)
5286 (org-agenda-files t)))
5287 (time (float-time))
5288 file rtn results)
5289 (when (or (not org-diary-last-run-time)
5290 (> (- time
5291 org-diary-last-run-time)
5293 (org-agenda-prepare-buffers files))
5294 (setq org-diary-last-run-time time)
5295 ;; If this is called during org-agenda, don't return any entries to
5296 ;; the calendar. Org Agenda will list these entries itself.
5297 (if org-disable-agenda-to-diary (setq files nil))
5298 (while (setq file (pop files))
5299 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5300 (setq results (append results rtn)))
5301 (when results
5302 (setq results
5303 (mapcar (lambda (i) (replace-regexp-in-string
5304 org-bracket-link-regexp "\\3" i)) results))
5305 (concat (org-agenda-finalize-entries results) "\n"))))
5307 ;;; Agenda entry finders
5309 (defun org-agenda--timestamp-to-absolute (&rest args)
5310 "Call `org-time-string-to-absolute' with ARGS.
5311 However, throw `:skip' whenever an error is raised."
5312 (condition-case e
5313 (apply #'org-time-string-to-absolute args)
5314 (org-diary-sexp-no-match (throw :skip nil))
5315 (error
5316 (message "%s; Skipping entry" (error-message-string e))
5317 (throw :skip nil))))
5319 (defun org-agenda-get-day-entries (file date &rest args)
5320 "Does the work for `org-diary' and `org-agenda'.
5321 FILE is the path to a file to be checked for entries. DATE is date like
5322 the one returned by `calendar-current-date'. ARGS are symbols indicating
5323 which kind of entries should be extracted. For details about these, see
5324 the documentation of `org-diary'."
5325 (let* ((org-startup-folded nil)
5326 (org-startup-align-all-tables nil)
5327 (buffer (if (file-exists-p file) (org-get-agenda-file-buffer file)
5328 (error "No such file %s" file))))
5329 (if (not buffer)
5330 ;; If file does not exist, signal it in diary nonetheless.
5331 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5332 (with-current-buffer buffer
5333 (unless (derived-mode-p 'org-mode)
5334 (error "Agenda file %s is not in `org-mode'" file))
5335 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5336 (setf org-agenda-current-date date)
5337 (save-excursion
5338 (save-restriction
5339 (if (eq buffer org-agenda-restrict)
5340 (narrow-to-region org-agenda-restrict-begin
5341 org-agenda-restrict-end)
5342 (widen))
5343 ;; Rationalize ARGS. Also make sure `:deadline' comes
5344 ;; first in order to populate DEADLINES before passing it.
5346 ;; We use `delq' since `org-uniquify' duplicates ARGS,
5347 ;; guarding us from modifying `org-agenda-entry-types'.
5348 (setf args (org-uniquify (or args org-agenda-entry-types)))
5349 (when (and (memq :scheduled args) (memq :scheduled* args))
5350 (setf args (delq :scheduled* args)))
5351 (cond
5352 ((memq :deadline args)
5353 (setf args (cons :deadline
5354 (delq :deadline (delq :deadline* args)))))
5355 ((memq :deadline* args)
5356 (setf args (cons :deadline* (delq :deadline* args)))))
5357 ;; Collect list of headlines. Return them flattened.
5358 (let ((case-fold-search nil) results deadlines)
5359 (dolist (arg args (apply #'nconc (nreverse results)))
5360 (pcase arg
5361 ((and :todo (guard (org-agenda-today-p date)))
5362 (push (org-agenda-get-todos) results))
5363 (:timestamp
5364 (push (org-agenda-get-blocks) results)
5365 (push (org-agenda-get-timestamps deadlines) results))
5366 (:sexp
5367 (push (org-agenda-get-sexps) results))
5368 (:scheduled
5369 (push (org-agenda-get-scheduled deadlines) results))
5370 (:scheduled*
5371 (push (org-agenda-get-scheduled deadlines t) results))
5372 (:closed
5373 (push (org-agenda-get-progress) results))
5374 (:deadline
5375 (setf deadlines (org-agenda-get-deadlines))
5376 (push deadlines results))
5377 (:deadline*
5378 (setf deadlines (org-agenda-get-deadlines t))
5379 (push deadlines results)))))))))))
5381 (defsubst org-em (x y list)
5382 "Is X or Y a member of LIST?"
5383 (or (memq x list) (memq y list)))
5385 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5386 (defvar org-agenda-sorting-strategy-selected nil)
5388 (defun org-agenda-entry-get-agenda-timestamp (pom)
5389 "Retrieve timestamp information for sorting agenda views.
5390 Given a point or marker POM, returns a cons cell of the timestamp
5391 and the timestamp type relevant for the sorting strategy in
5392 `org-agenda-sorting-strategy-selected'."
5393 (let (ts ts-date-type)
5394 (save-match-data
5395 (cond ((org-em 'scheduled-up 'scheduled-down
5396 org-agenda-sorting-strategy-selected)
5397 (setq ts (org-entry-get pom "SCHEDULED")
5398 ts-date-type " scheduled"))
5399 ((org-em 'deadline-up 'deadline-down
5400 org-agenda-sorting-strategy-selected)
5401 (setq ts (org-entry-get pom "DEADLINE")
5402 ts-date-type " deadline"))
5403 ((org-em 'ts-up 'ts-down
5404 org-agenda-sorting-strategy-selected)
5405 (setq ts (org-entry-get pom "TIMESTAMP")
5406 ts-date-type " timestamp"))
5407 ((org-em 'tsia-up 'tsia-down
5408 org-agenda-sorting-strategy-selected)
5409 (setq ts (org-entry-get pom "TIMESTAMP_IA")
5410 ts-date-type " timestamp_ia"))
5411 ((org-em 'timestamp-up 'timestamp-down
5412 org-agenda-sorting-strategy-selected)
5413 (setq ts (or (org-entry-get pom "SCHEDULED")
5414 (org-entry-get pom "DEADLINE")
5415 (org-entry-get pom "TIMESTAMP")
5416 (org-entry-get pom "TIMESTAMP_IA"))
5417 ts-date-type ""))
5418 (t (setq ts-date-type "")))
5419 (cons (when ts (ignore-errors (org-time-string-to-absolute ts)))
5420 ts-date-type))))
5422 (defun org-agenda-get-todos ()
5423 "Return the TODO information for agenda display."
5424 (let* ((props (list 'face nil
5425 'done-face 'org-agenda-done
5426 'org-not-done-regexp org-not-done-regexp
5427 'org-todo-regexp org-todo-regexp
5428 'org-complex-heading-regexp org-complex-heading-regexp
5429 'mouse-face 'highlight
5430 'help-echo
5431 (format "mouse-2 or RET jump to org file %s"
5432 (abbreviate-file-name buffer-file-name))))
5433 (regexp (format org-heading-keyword-regexp-format
5434 (cond
5435 ((and org-select-this-todo-keyword
5436 (equal org-select-this-todo-keyword "*"))
5437 org-todo-regexp)
5438 (org-select-this-todo-keyword
5439 (concat "\\("
5440 (mapconcat 'identity
5441 (org-split-string
5442 org-select-this-todo-keyword
5443 "|")
5444 "\\|") "\\)"))
5445 (t org-not-done-regexp))))
5446 marker priority category level tags todo-state
5447 ts-date ts-date-type ts-date-pair
5448 ee txt beg end inherited-tags todo-state-end-pos)
5449 (goto-char (point-min))
5450 (while (re-search-forward regexp nil t)
5451 (catch :skip
5452 (save-match-data
5453 (beginning-of-line)
5454 (org-agenda-skip)
5455 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5456 (unless (and (setq todo-state (org-get-todo-state))
5457 (setq todo-state-end-pos (match-end 2)))
5458 (goto-char end)
5459 (throw :skip nil))
5460 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5461 (goto-char (1+ beg))
5462 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5463 (throw :skip nil)))
5464 (goto-char (match-beginning 2))
5465 (setq marker (org-agenda-new-marker (match-beginning 0))
5466 category (org-get-category)
5467 ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
5468 ts-date (car ts-date-pair)
5469 ts-date-type (cdr ts-date-pair)
5470 txt (org-trim (buffer-substring (match-beginning 2) (match-end 0)))
5471 inherited-tags
5472 (or (eq org-agenda-show-inherited-tags 'always)
5473 (and (listp org-agenda-show-inherited-tags)
5474 (memq 'todo org-agenda-show-inherited-tags))
5475 (and (eq org-agenda-show-inherited-tags t)
5476 (or (eq org-agenda-use-tag-inheritance t)
5477 (memq 'todo org-agenda-use-tag-inheritance))))
5478 tags (org-get-tags-at nil (not inherited-tags))
5479 level (make-string (org-reduced-level (org-outline-level)) ? )
5480 txt (org-agenda-format-item "" txt level category tags t)
5481 priority (1+ (org-get-priority txt)))
5482 (org-add-props txt props
5483 'org-marker marker 'org-hd-marker marker
5484 'priority priority
5485 'level level
5486 'ts-date ts-date
5487 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5488 (push txt ee)
5489 (if org-agenda-todo-list-sublevels
5490 (goto-char todo-state-end-pos)
5491 (org-end-of-subtree 'invisible))))
5492 (nreverse ee)))
5494 (defun org-agenda-todo-custom-ignore-p (time n)
5495 "Check whether timestamp is farther away than n number of days.
5496 This function is invoked if `org-agenda-todo-ignore-deadlines',
5497 `org-agenda-todo-ignore-scheduled' or
5498 `org-agenda-todo-ignore-timestamp' is set to an integer."
5499 (let ((days (org-time-stamp-to-now
5500 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5501 (if (>= n 0)
5502 (>= days n)
5503 (<= days n))))
5505 ;;;###autoload
5506 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5507 (&optional end)
5508 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5509 (when (or org-agenda-todo-ignore-with-date
5510 org-agenda-todo-ignore-scheduled
5511 org-agenda-todo-ignore-deadlines
5512 org-agenda-todo-ignore-timestamp)
5513 (setq end (or end (save-excursion (outline-next-heading) (point))))
5514 (save-excursion
5515 (or (and org-agenda-todo-ignore-with-date
5516 (re-search-forward org-ts-regexp end t))
5517 (and org-agenda-todo-ignore-scheduled
5518 (re-search-forward org-scheduled-time-regexp end t)
5519 (cond
5520 ((eq org-agenda-todo-ignore-scheduled 'future)
5521 (> (org-time-stamp-to-now
5522 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5523 ((eq org-agenda-todo-ignore-scheduled 'past)
5524 (<= (org-time-stamp-to-now
5525 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5526 ((numberp org-agenda-todo-ignore-scheduled)
5527 (org-agenda-todo-custom-ignore-p
5528 (match-string 1) org-agenda-todo-ignore-scheduled))
5529 (t)))
5530 (and org-agenda-todo-ignore-deadlines
5531 (re-search-forward org-deadline-time-regexp end t)
5532 (cond
5533 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5534 ((eq org-agenda-todo-ignore-deadlines 'far)
5535 (not (org-deadline-close-p (match-string 1))))
5536 ((eq org-agenda-todo-ignore-deadlines 'future)
5537 (> (org-time-stamp-to-now
5538 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5539 ((eq org-agenda-todo-ignore-deadlines 'past)
5540 (<= (org-time-stamp-to-now
5541 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5542 ((numberp org-agenda-todo-ignore-deadlines)
5543 (org-agenda-todo-custom-ignore-p
5544 (match-string 1) org-agenda-todo-ignore-deadlines))
5545 (t (org-deadline-close-p (match-string 1)))))
5546 (and org-agenda-todo-ignore-timestamp
5547 (let ((buffer (current-buffer))
5548 (regexp
5549 (concat
5550 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5551 (start (point)))
5552 ;; Copy current buffer into a temporary one
5553 (with-temp-buffer
5554 (insert-buffer-substring buffer start end)
5555 (goto-char (point-min))
5556 ;; Delete SCHEDULED and DEADLINE items
5557 (while (re-search-forward regexp end t)
5558 (delete-region (match-beginning 0) (match-end 0)))
5559 (goto-char (point-min))
5560 ;; No search for timestamp left
5561 (when (re-search-forward org-ts-regexp nil t)
5562 (cond
5563 ((eq org-agenda-todo-ignore-timestamp 'future)
5564 (> (org-time-stamp-to-now
5565 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5566 ((eq org-agenda-todo-ignore-timestamp 'past)
5567 (<= (org-time-stamp-to-now
5568 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5569 ((numberp org-agenda-todo-ignore-timestamp)
5570 (org-agenda-todo-custom-ignore-p
5571 (match-string 1) org-agenda-todo-ignore-timestamp))
5572 (t))))))))))
5574 (defun org-agenda-get-timestamps (&optional deadline-results)
5575 "Return the date stamp information for agenda display."
5576 (let* ((props (list 'face 'org-agenda-calendar-event
5577 'org-not-done-regexp org-not-done-regexp
5578 'org-todo-regexp org-todo-regexp
5579 'org-complex-heading-regexp org-complex-heading-regexp
5580 'mouse-face 'highlight
5581 'help-echo
5582 (format "mouse-2 or RET jump to org file %s"
5583 (abbreviate-file-name buffer-file-name))))
5584 (d1 (calendar-absolute-from-gregorian date))
5586 (deadline-position-alist
5587 (mapcar (lambda (a) (and (setq mm (get-text-property
5588 0 'org-hd-marker a))
5589 (cons (marker-position mm) a)))
5590 deadline-results))
5591 (remove-re org-ts-regexp)
5592 (regexp
5593 (concat
5594 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5595 (regexp-quote
5596 (substring
5597 (format-time-string
5598 (car org-time-stamp-formats)
5599 (apply 'encode-time ; DATE bound by calendar
5600 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5601 1 11))
5602 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5603 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5604 marker hdmarker deadlinep scheduledp clockp closedp inactivep
5605 donep tmp priority category level ee txt timestr tags
5606 b0 b3 e3 head todo-state end-of-match show-all warntime habitp
5607 inherited-tags ts-date)
5608 (goto-char (point-min))
5609 (while (setq end-of-match (re-search-forward regexp nil t))
5610 (setq b0 (match-beginning 0)
5611 b3 (match-beginning 3) e3 (match-end 3)
5612 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
5613 habitp (and (functionp 'org-is-habit-p) (save-match-data (org-is-habit-p)))
5614 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5615 (member todo-state
5616 org-agenda-repeating-timestamp-show-all)))
5617 (catch :skip
5618 (and (org-at-date-range-p) (throw :skip nil))
5619 (org-agenda-skip)
5620 (if (and (match-end 1)
5621 (not (= d1 (org-agenda--timestamp-to-absolute
5622 (match-string 1) d1 nil show-all
5623 (current-buffer) b0))))
5624 (throw :skip nil))
5625 (if (and e3
5626 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
5627 (throw :skip nil))
5628 (setq tmp (buffer-substring (max (point-min)
5629 (- b0 org-ds-keyword-length))
5631 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
5632 inactivep (= (char-after b0) ?\[)
5633 deadlinep (string-match org-deadline-regexp tmp)
5634 scheduledp (string-match org-scheduled-regexp tmp)
5635 closedp (and org-agenda-include-inactive-timestamps
5636 (string-match org-closed-string tmp))
5637 clockp (and org-agenda-include-inactive-timestamps
5638 (or (string-match org-clock-string tmp)
5639 (string-match "]-+\\'" tmp)))
5640 warntime (get-text-property (point) 'org-appt-warntime)
5641 donep (member todo-state org-done-keywords))
5642 (if (or scheduledp deadlinep closedp clockp
5643 (and donep org-agenda-skip-timestamp-if-done))
5644 (throw :skip t))
5645 (if (string-match ">" timestr)
5646 ;; substring should only run to end of time stamp
5647 (setq timestr (substring timestr 0 (match-end 0))))
5648 (setq marker (org-agenda-new-marker b0)
5649 category (org-get-category b0))
5650 (save-excursion
5651 (if (not (re-search-backward org-outline-regexp-bol nil t))
5652 (throw :skip nil)
5653 (goto-char (match-beginning 0))
5654 (if (and (eq t org-agenda-skip-timestamp-if-deadline-is-shown)
5655 (assoc (point) deadline-position-alist))
5656 (throw :skip nil))
5657 (setq hdmarker (org-agenda-new-marker)
5658 inherited-tags
5659 (or (eq org-agenda-show-inherited-tags 'always)
5660 (and (listp org-agenda-show-inherited-tags)
5661 (memq 'agenda org-agenda-show-inherited-tags))
5662 (and (eq org-agenda-show-inherited-tags t)
5663 (or (eq org-agenda-use-tag-inheritance t)
5664 (memq 'agenda org-agenda-use-tag-inheritance))))
5665 tags (org-get-tags-at nil (not inherited-tags))
5666 level (make-string (org-reduced-level (org-outline-level)) ? ))
5667 (looking-at "\\*+[ \t]+\\(.*\\)")
5668 (setq head (match-string 1))
5669 (setq txt (org-agenda-format-item
5670 (if inactivep org-agenda-inactive-leader nil)
5671 head level category tags timestr
5672 remove-re habitp)))
5673 (setq priority (org-get-priority txt))
5674 (org-add-props txt props 'priority priority
5675 'org-marker marker 'org-hd-marker hdmarker
5676 'date date
5677 'level level
5678 'ts-date
5679 (ignore-errors (org-time-string-to-absolute timestr))
5680 'todo-state todo-state
5681 'warntime warntime
5682 'type "timestamp")
5683 (push txt ee))
5684 (if org-agenda-skip-additional-timestamps-same-entry
5685 (outline-next-heading)
5686 (goto-char end-of-match))))
5687 (nreverse ee)))
5689 (defun org-agenda-get-sexps ()
5690 "Return the sexp information for agenda display."
5691 (require 'diary-lib)
5692 (let* ((props (list 'face 'org-agenda-calendar-sexp
5693 'mouse-face 'highlight
5694 'help-echo
5695 (format "mouse-2 or RET jump to org file %s"
5696 (abbreviate-file-name buffer-file-name))))
5697 (regexp "^&?%%(")
5698 marker category extra level ee txt tags entry
5699 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5700 (goto-char (point-min))
5701 (while (re-search-forward regexp nil t)
5702 (catch :skip
5703 (org-agenda-skip)
5704 (setq beg (match-beginning 0))
5705 (goto-char (1- (match-end 0)))
5706 (setq b (point))
5707 (forward-sexp 1)
5708 (setq sexp (buffer-substring b (point)))
5709 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5710 (org-trim (match-string 1))
5711 ""))
5712 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5713 (when result
5714 (setq marker (org-agenda-new-marker beg)
5715 level (make-string (org-reduced-level (org-outline-level)) ? )
5716 category (org-get-category beg)
5717 inherited-tags
5718 (or (eq org-agenda-show-inherited-tags 'always)
5719 (and (listp org-agenda-show-inherited-tags)
5720 (memq 'agenda org-agenda-show-inherited-tags))
5721 (and (eq org-agenda-show-inherited-tags t)
5722 (or (eq org-agenda-use-tag-inheritance t)
5723 (memq 'agenda org-agenda-use-tag-inheritance))))
5724 tags (org-get-tags-at nil (not inherited-tags))
5725 todo-state (org-get-todo-state)
5726 warntime (get-text-property (point) 'org-appt-warntime)
5727 extra nil)
5729 (dolist (r (if (stringp result)
5730 (list result)
5731 result)) ;; we expect a list here
5732 (when (and org-agenda-diary-sexp-prefix
5733 (string-match org-agenda-diary-sexp-prefix r))
5734 (setq extra (match-string 0 r)
5735 r (replace-match "" nil nil r)))
5736 (if (string-match "\\S-" r)
5737 (setq txt r)
5738 (setq txt "SEXP entry returned empty string"))
5739 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5740 (org-add-props txt props 'org-marker marker
5741 'date date 'todo-state todo-state
5742 'level level 'type "sexp" 'warntime warntime)
5743 (push txt ee)))))
5744 (nreverse ee)))
5746 ;; Calendar sanity: define some functions that are independent of
5747 ;; `calendar-date-style'.
5748 (defun org-anniversary (year month day &optional mark)
5749 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5750 (with-no-warnings
5751 (let ((calendar-date-style 'iso))
5752 (diary-anniversary year month day mark))))
5753 (defun org-cyclic (N year month day &optional mark)
5754 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5755 (with-no-warnings
5756 (let ((calendar-date-style 'iso))
5757 (diary-cyclic N year month day mark))))
5758 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5759 "Like `diary-block', but with fixed (ISO) order of arguments."
5760 (with-no-warnings
5761 (let ((calendar-date-style 'iso))
5762 (diary-block Y1 M1 D1 Y2 M2 D2 mark))))
5763 (defun org-date (year month day &optional mark)
5764 "Like `diary-date', but with fixed (ISO) order of arguments."
5765 (with-no-warnings
5766 (let ((calendar-date-style 'iso))
5767 (diary-date year month day mark))))
5769 ;; Define the `org-class' function
5770 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5771 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5772 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5773 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5774 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5775 `holidays', then any date that is known by the Emacs calendar to be a
5776 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5777 then those holidays will be skipped."
5778 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5779 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5780 (d (calendar-absolute-from-gregorian date))
5781 (h (when skip-weeks (calendar-check-holidays date))))
5782 (and
5783 (<= date1 d)
5784 (<= d date2)
5785 (= (calendar-day-of-week date) dayname)
5786 (or (not skip-weeks)
5787 (progn
5788 (require 'cal-iso)
5789 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5790 (not (or (and h (memq 'holidays skip-weeks))
5791 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5792 entry)))
5794 (defalias 'org-get-closed 'org-agenda-get-progress)
5795 (defun org-agenda-get-progress ()
5796 "Return the logged TODO entries for agenda display."
5797 (let* ((props (list 'mouse-face 'highlight
5798 'org-not-done-regexp org-not-done-regexp
5799 'org-todo-regexp org-todo-regexp
5800 'org-complex-heading-regexp org-complex-heading-regexp
5801 'help-echo
5802 (format "mouse-2 or RET jump to org file %s"
5803 (abbreviate-file-name buffer-file-name))))
5804 (items (if (consp org-agenda-show-log-scoped)
5805 org-agenda-show-log-scoped
5806 (if (eq org-agenda-show-log-scoped 'clockcheck)
5807 '(clock)
5808 org-agenda-log-mode-items)))
5809 (parts
5810 (delq nil
5811 (list
5812 (if (memq 'closed items) (concat "\\<" org-closed-string))
5813 (if (memq 'clock items) (concat "\\<" org-clock-string))
5814 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5815 (parts-re (if parts (mapconcat 'identity parts "\\|")
5816 (error "`org-agenda-log-mode-items' is empty")))
5817 (regexp (concat
5818 "\\(" parts-re "\\)"
5819 " *\\["
5820 (regexp-quote
5821 (substring
5822 (format-time-string
5823 (car org-time-stamp-formats)
5824 (apply 'encode-time ; DATE bound by calendar
5825 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5826 1 11))))
5827 (org-agenda-search-headline-for-time nil)
5828 marker hdmarker priority category level tags closedp
5829 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5830 (goto-char (point-min))
5831 (while (re-search-forward regexp nil t)
5832 (catch :skip
5833 (org-agenda-skip)
5834 (setq marker (org-agenda-new-marker (match-beginning 0))
5835 closedp (equal (match-string 1) org-closed-string)
5836 statep (equal (string-to-char (match-string 1)) ?-)
5837 clockp (not (or closedp statep))
5838 state (and statep (match-string 2))
5839 category (org-get-category (match-beginning 0))
5840 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5841 (when (string-match "\\]" timestr)
5842 ;; substring should only run to end of time stamp
5843 (setq rest (substring timestr (match-end 0))
5844 timestr (substring timestr 0 (match-end 0)))
5845 (if (and (not closedp) (not statep)
5846 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5847 rest))
5848 (progn (setq timestr (concat (substring timestr 0 -1)
5849 "-" (match-string 1 rest) "]"))
5850 (setq clocked (match-string 2 rest)))
5851 (setq clocked "-")))
5852 (save-excursion
5853 (setq extra
5854 (cond
5855 ((not org-agenda-log-mode-add-notes) nil)
5856 (statep
5857 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5858 (match-string 1)))
5859 (clockp
5860 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5861 (match-string 1)))))
5862 (if (not (re-search-backward org-outline-regexp-bol nil t))
5863 (throw :skip nil)
5864 (goto-char (match-beginning 0))
5865 (setq hdmarker (org-agenda-new-marker)
5866 inherited-tags
5867 (or (eq org-agenda-show-inherited-tags 'always)
5868 (and (listp org-agenda-show-inherited-tags)
5869 (memq 'todo org-agenda-show-inherited-tags))
5870 (and (eq org-agenda-show-inherited-tags t)
5871 (or (eq org-agenda-use-tag-inheritance t)
5872 (memq 'todo org-agenda-use-tag-inheritance))))
5873 tags (org-get-tags-at nil (not inherited-tags))
5874 level (make-string (org-reduced-level (org-outline-level)) ? ))
5875 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5876 (setq txt (match-string 1))
5877 (when extra
5878 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5879 (setq txt (concat (substring txt 0 (match-beginning 1))
5880 " - " extra " " (match-string 2 txt)))
5881 (setq txt (concat txt " - " extra))))
5882 (setq txt (org-agenda-format-item
5883 (cond
5884 (closedp "Closed: ")
5885 (statep (concat "State: (" state ")"))
5886 (t (concat "Clocked: (" clocked ")")))
5887 txt level category tags timestr)))
5888 (setq priority 100000)
5889 (org-add-props txt props
5890 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5891 'priority priority 'level level
5892 'type "closed" 'date date
5893 'undone-face 'org-warning 'done-face 'org-agenda-done)
5894 (push txt ee))
5895 (goto-char (point-at-eol))))
5896 (nreverse ee)))
5898 (defun org-agenda-show-clocking-issues ()
5899 "Add overlays, showing issues with clocking.
5900 See also the user option `org-agenda-clock-consistency-checks'."
5901 (interactive)
5902 (let* ((org-time-clocksum-use-effort-durations nil)
5903 (pl org-agenda-clock-consistency-checks)
5904 (re (concat "^[ \t]*"
5905 org-clock-string
5906 "[ \t]+"
5907 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5908 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5909 (tlstart 0.)
5910 (tlend 0.)
5911 (maxtime (org-hh:mm-string-to-minutes
5912 (or (plist-get pl :max-duration) "24:00")))
5913 (mintime (org-hh:mm-string-to-minutes
5914 (or (plist-get pl :min-duration) 0)))
5915 (maxgap (org-hh:mm-string-to-minutes
5916 ;; default 30:00 means never complain
5917 (or (plist-get pl :max-gap) "30:00")))
5918 (gapok (mapcar 'org-hh:mm-string-to-minutes
5919 (plist-get pl :gap-ok-around)))
5920 (def-face (or (plist-get pl :default-face)
5921 '((:background "DarkRed") (:foreground "white"))))
5922 issue face m te ts dt ov)
5923 (goto-char (point-min))
5924 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5925 (setq issue nil face def-face)
5926 (catch 'next
5927 (setq m (org-get-at-bol 'org-marker)
5928 te nil ts nil)
5929 (unless (and m (markerp m))
5930 (setq issue "No valid clock line") (throw 'next t))
5931 (org-with-point-at m
5932 (save-excursion
5933 (goto-char (point-at-bol))
5934 (unless (looking-at re)
5935 (error "No valid Clock line")
5936 (throw 'next t))
5937 (unless (match-end 3)
5938 (setq issue "No end time"
5939 face (or (plist-get pl :no-end-time-face) face))
5940 (throw 'next t))
5941 (setq ts (match-string 1)
5942 te (match-string 3)
5943 ts (float-time
5944 (apply #'encode-time (org-parse-time-string ts)))
5945 te (float-time
5946 (apply #'encode-time (org-parse-time-string te)))
5947 dt (- te ts))))
5948 (cond
5949 ((> dt (* 60 maxtime))
5950 ;; a very long clocking chunk
5951 (setq issue (format "Clocking interval is very long: %s"
5952 (org-minutes-to-clocksum-string
5953 (floor (/ (float dt) 60.))))
5954 face (or (plist-get pl :long-face) face)))
5955 ((< dt (* 60 mintime))
5956 ;; a very short clocking chunk
5957 (setq issue (format "Clocking interval is very short: %s"
5958 (org-minutes-to-clocksum-string
5959 (floor (/ (float dt) 60.))))
5960 face (or (plist-get pl :short-face) face)))
5961 ((and (> tlend 0) (< ts tlend))
5962 ;; Two clock entries are overlapping
5963 (setq issue (format "Clocking overlap: %d minutes"
5964 (/ (- tlend ts) 60))
5965 face (or (plist-get pl :overlap-face) face)))
5966 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5967 ;; There is a gap, lets see if we need to report it
5968 (unless (org-agenda-check-clock-gap tlend ts gapok)
5969 (setq issue (format "Clocking gap: %d minutes"
5970 (/ (- ts tlend) 60))
5971 face (or (plist-get pl :gap-face) face))))
5972 (t nil)))
5973 (setq tlend (or te tlend) tlstart (or ts tlstart))
5974 (when issue
5975 ;; OK, there was some issue, add an overlay to show the issue
5976 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5977 (overlay-put ov 'before-string
5978 (concat
5979 (org-add-props
5980 (format "%-43s" (concat " " issue))
5982 'face face)
5983 "\n"))
5984 (overlay-put ov 'evaporate t)))))
5986 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5987 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5988 (catch 'exit
5989 (unless ok-list
5990 ;; there are no OK times for gaps...
5991 (throw 'exit nil))
5992 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5993 ;; This is more than 24 hours, so it is OK.
5994 ;; because we have at least one OK time, that must be in the
5995 ;; 24 hour interval.
5996 (throw 'exit t))
5997 ;; We have a shorter gap.
5998 ;; Now we have to get the minute of the day when these times are
5999 (let* ((t1dec (decode-time (seconds-to-time t1)))
6000 (t2dec (decode-time (seconds-to-time t2)))
6001 ;; compute the minute on the day
6002 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
6003 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
6004 (when (< min2 min1)
6005 ;; if min2 is smaller than min1, this means it is on the next day.
6006 ;; Wrap it to after midnight.
6007 (setq min2 (+ min2 1440)))
6008 ;; Now check if any of the OK times is in the gap
6009 (mapc (lambda (x)
6010 ;; Wrap the time to after midnight if necessary
6011 (if (< x min1) (setq x (+ x 1440)))
6012 ;; Check if in interval
6013 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
6014 ok-list)
6015 ;; Nope, this gap is not OK
6016 nil)))
6018 (defun org-agenda-get-deadlines (&optional with-hour)
6019 "Return the deadline information for agenda display.
6020 When WITH-HOUR is non-nil, only return deadlines with an hour
6021 specification like [h]h:mm."
6022 (let* ((props (list 'mouse-face 'highlight
6023 'org-not-done-regexp org-not-done-regexp
6024 'org-todo-regexp org-todo-regexp
6025 'org-complex-heading-regexp org-complex-heading-regexp
6026 'help-echo
6027 (format "mouse-2 or RET jump to org file %s"
6028 (abbreviate-file-name buffer-file-name))))
6029 (regexp (if with-hour
6030 org-deadline-time-hour-regexp
6031 org-deadline-time-regexp))
6032 (todayp (org-agenda-today-p date)) ; DATE bound by calendar.
6033 (current (calendar-absolute-from-gregorian date))
6034 deadline-items)
6035 (goto-char (point-min))
6036 (while (re-search-forward regexp nil t)
6037 (catch :skip
6038 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
6039 (org-agenda-skip)
6040 (let* ((s (match-string 1))
6041 (pos (1- (match-beginning 1)))
6042 (todo-state (save-match-data (org-get-todo-state)))
6043 (donep (member todo-state org-done-keywords))
6044 (show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6045 (member todo-state
6046 org-agenda-repeating-timestamp-show-all)))
6047 ;; DEADLINE is the current scheduled date. When it
6048 ;; contains a repeater and SHOW-ALL is non-nil,
6049 ;; LAST-REPEAT is the repeat closest to CURRENT.
6050 ;; Otherwise, LAST-REPEAT is equal to DEADLINE.
6051 (last-repeat (org-agenda--timestamp-to-absolute
6052 s current 'past show-all (current-buffer) pos))
6053 (deadline (org-agenda--timestamp-to-absolute s current))
6054 (diff (- last-repeat current))
6055 (suppress-prewarning
6056 (let ((scheduled
6057 (and org-agenda-skip-deadline-prewarning-if-scheduled
6058 (org-entry-get nil "SCHEDULED"))))
6059 (cond
6060 ((not scheduled) nil)
6061 ;; The current item has a scheduled date, so
6062 ;; evaluate its prewarning lead time.
6063 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
6064 ;; Use global prewarning-restart lead time.
6065 org-agenda-skip-deadline-prewarning-if-scheduled)
6066 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
6067 'pre-scheduled)
6068 ;; Set pre-warning to no earlier than SCHEDULED.
6069 (min (- last-repeat
6070 (org-agenda--timestamp-to-absolute
6071 scheduled current 'past show-all
6072 (current-buffer)
6073 (save-excursion
6074 (beginning-of-line)
6075 (1+ (search-forward org-deadline-string)))))
6076 org-deadline-warning-days))
6077 ;; Set pre-warning to deadline.
6078 (t 0))))
6079 (wdays (if suppress-prewarning
6080 (let ((org-deadline-warning-days suppress-prewarning))
6081 (org-get-wdays s))
6082 (org-get-wdays s))))
6083 ;; When to show a deadline in the calendar: if the
6084 ;; expiration is within WDAYS warning time. Past-due
6085 ;; deadlines are only shown on the current date
6086 (unless (or (and (<= diff wdays)
6087 (and todayp (not org-agenda-only-exact-dates)))
6088 (= diff 0))
6089 (throw :skip nil))
6090 ;; Skip done tasks if `org-agenda-skip-deadline-if-done' is
6091 ;; non-nil or if it isn't applicable to CURRENT deadline.
6092 (when (and donep
6093 (or org-agenda-skip-deadline-if-done
6094 (/= deadline current)))
6095 (throw :skip nil))
6096 (save-excursion
6097 (re-search-backward "^\\*+[ \t]+" nil t)
6098 (goto-char (match-end 0))
6099 (let* ((category (org-get-category))
6100 (level
6101 (make-string (org-reduced-level (org-outline-level)) ?\s))
6102 (head (buffer-substring (point) (line-end-position)))
6103 (inherited-tags
6104 (or (eq org-agenda-show-inherited-tags 'always)
6105 (and (listp org-agenda-show-inherited-tags)
6106 (memq 'agenda org-agenda-show-inherited-tags))
6107 (and (eq org-agenda-show-inherited-tags t)
6108 (or (eq org-agenda-use-tag-inheritance t)
6109 (memq 'agenda
6110 org-agenda-use-tag-inheritance)))))
6111 (tags (org-get-tags-at nil (not inherited-tags)))
6112 (timestr
6113 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6114 (concat (substring s (match-beginning 1)) " ")
6115 'time))
6116 (item
6117 (org-agenda-format-item
6118 ;; For past deadlines, make sure to report time
6119 ;; difference since date S, not since closest
6120 ;; repeater.
6121 (let ((diff (if (< (org-today) current) diff
6122 (- deadline current))))
6123 (if (= diff 0) (car org-agenda-deadline-leaders)
6124 (let ((future (nth 1 org-agenda-deadline-leaders))
6125 (past (nth 2 org-agenda-deadline-leaders)))
6126 (cond ((> diff 0) (format future diff))
6127 ((string= future past) (format past diff))
6128 (t (format past (abs diff)))))))
6129 head level category tags
6130 (and (= diff 0) timestr)))
6131 (face (org-agenda-deadline-face
6132 (- 1 (/ (float (- deadline current)) (max wdays 1)))))
6133 (upcomingp (and todayp (> diff 0)))
6134 (warntime (get-text-property (point) 'org-appt-warntime)))
6135 (org-add-props item props
6136 'org-marker (org-agenda-new-marker pos)
6137 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
6138 'warntime warntime
6139 'level level
6140 'ts-date deadline
6141 'priority (- (org-get-priority item) diff)
6142 'todo-state todo-state
6143 'type (if upcomingp "upcoming-deadline" "deadline")
6144 'date (if upcomingp date deadline)
6145 'face (if donep 'org-agenda-done face)
6146 'undone-face face
6147 'done-face 'org-agenda-done)
6148 (push item deadline-items))))))
6149 (nreverse deadline-items)))
6151 (defun org-agenda-deadline-face (fraction)
6152 "Return the face to displaying a deadline item.
6153 FRACTION is what fraction of the head-warning time has passed."
6154 (let ((faces org-agenda-deadline-faces) f)
6155 (catch 'exit
6156 (while (setq f (pop faces))
6157 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
6159 (defun org-agenda-get-scheduled (&optional deadlines with-hour)
6160 "Return the scheduled information for agenda display.
6161 Optional argument DEADLINES is a list of deadline items to be
6162 displayed in agenda view. When WITH-HOUR is non-nil, only return
6163 scheduled items with an hour specification like [h]h:mm."
6164 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
6165 'org-todo-regexp org-todo-regexp
6166 'org-complex-heading-regexp org-complex-heading-regexp
6167 'done-face 'org-agenda-done
6168 'mouse-face 'highlight
6169 'help-echo
6170 (format "mouse-2 or RET jump to org file %s"
6171 (abbreviate-file-name buffer-file-name))))
6172 (regexp (if with-hour
6173 org-scheduled-time-hour-regexp
6174 org-scheduled-time-regexp))
6175 (todayp (org-agenda-today-p date)) ; DATE bound by calendar.
6176 (current (calendar-absolute-from-gregorian date))
6177 (deadline-pos
6178 (mapcar (lambda (d)
6179 (let ((m (get-text-property 0 'org-hd-marker d)))
6180 (and m (marker-position m))))
6181 deadlines))
6182 scheduled-items)
6183 (goto-char (point-min))
6184 (while (re-search-forward regexp nil t)
6185 (catch :skip
6186 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
6187 (org-agenda-skip)
6188 (let* ((s (match-string 1))
6189 (pos (1- (match-beginning 1)))
6190 (todo-state (save-match-data (org-get-todo-state)))
6191 (donep (member todo-state org-done-keywords))
6192 (show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6193 (member todo-state
6194 org-agenda-repeating-timestamp-show-all)))
6195 ;; SCHEDULE is the current scheduled date. When it
6196 ;; contains a repeater and SHOW-ALL is non-nil,
6197 ;; LAST-REPEAT is the repeat closest to CURRENT.
6198 ;; Otherwise, LAST-REPEAT is equal to SCHEDULE.
6199 (last-repeat (org-agenda--timestamp-to-absolute
6200 s current 'past show-all (current-buffer) pos))
6201 (schedule (org-agenda--timestamp-to-absolute s current))
6202 (diff (- last-repeat current))
6203 (warntime (get-text-property (point) 'org-appt-warntime))
6204 (pastschedp (< schedule (org-today)))
6205 (habitp (and (fboundp 'org-is-habit-p) (org-is-habit-p)))
6206 (suppress-delay
6207 (let ((deadline (and org-agenda-skip-scheduled-delay-if-deadline
6208 (org-entry-get nil "DEADLINE"))))
6209 (cond
6210 ((not deadline) nil)
6211 ;; The current item has a deadline date, so
6212 ;; evaluate its delay time.
6213 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6214 ;; Use global delay time.
6215 (- org-agenda-skip-scheduled-delay-if-deadline))
6216 ((eq org-agenda-skip-scheduled-delay-if-deadline
6217 'post-deadline)
6218 ;; Set delay to no later than DEADLINE. If
6219 ;; DEADLINE has a repeater, compare last schedule
6220 ;; repeat and last deadline repeat.
6221 (min (- last-repeat
6222 (org-agenda--timestamp-to-absolute
6223 deadline current 'past show-all
6224 (current-buffer)
6225 (save-excursion
6226 (beginning-of-line)
6227 (1+ (search-forward org-deadline-string)))))
6228 org-scheduled-delay-days))
6229 (t 0))))
6230 (ddays
6231 (cond
6232 ;; Nullify delay when a repeater triggered already
6233 ;; and the delay is of the form --Xd.
6234 ((and (save-match-data (string-match "--[0-9]+[hdwmy]" s))
6235 (/= schedule last-repeat))
6237 (suppress-delay
6238 (let ((org-scheduled-delay-days suppress-delay))
6239 (org-get-wdays s t t)))
6240 (t (org-get-wdays s t)))))
6241 ;; Only show a scheduled item in the calendar if it is on or
6242 ;; past the current date. Skip it if it has been displayed
6243 ;; for more than `org-scheduled-past-days'.
6244 (unless (or (and (>= ddays 0) (= diff (- ddays)))
6245 (and (< (+ diff ddays) 0)
6246 (< (abs diff) org-scheduled-past-days)
6247 (and todayp (not org-agenda-only-exact-dates)))
6248 (and todayp
6249 habitp
6250 (bound-and-true-p org-habit-show-all-today)))
6251 (throw :skip nil))
6252 ;; Skip done habits, or tasks if
6253 ;; `org-agenda-skip-deadline-if-done' is non-nil or if it
6254 ;; was scheduled in the past anyway.
6255 (when (and donep
6256 (or org-agenda-skip-scheduled-if-done
6257 (/= schedule current)
6258 habitp))
6259 (throw :skip nil))
6260 ;; Skip entry if it already appears as a deadline, per
6261 ;; `org-agenda-skip-scheduled-if-deadline-is-shown'. This
6262 ;; doesn't apply to habits.
6263 (when (pcase org-agenda-skip-scheduled-if-deadline-is-shown
6264 ((guard
6265 (or (not (assq (line-beginning-position 0) deadline-pos))
6266 habitp))
6267 nil)
6268 (`repeated-after-deadline
6269 (>= last-repeat
6270 (time-to-days (org-get-deadline-time (point)))))
6271 (`not-today pastschedp)
6272 (`t t)
6273 (_ nil))
6274 (throw :skip nil))
6275 ;; Skip habits if `org-habit-show-habits' is nil, or if we
6276 ;; only show them for today.
6277 (when (and habitp
6278 (or (not (bound-and-true-p org-habit-show-habits))
6279 (and (not todayp)
6280 (bound-and-true-p
6281 org-habit-show-habits-only-for-today))))
6282 (throw :skip nil))
6283 (save-excursion
6284 (re-search-backward "^\\*+[ \t]+" nil t)
6285 (goto-char (match-end 0))
6286 (let* ((category (org-get-category))
6287 (inherited-tags
6288 (or (eq org-agenda-show-inherited-tags 'always)
6289 (and (listp org-agenda-show-inherited-tags)
6290 (memq 'agenda org-agenda-show-inherited-tags))
6291 (and (eq org-agenda-show-inherited-tags t)
6292 (or (eq org-agenda-use-tag-inheritance t)
6293 (memq 'agenda
6294 org-agenda-use-tag-inheritance)))))
6295 (tags (org-get-tags-at nil (not inherited-tags)))
6296 (level
6297 (make-string (org-reduced-level (org-outline-level)) ?\s))
6298 (head (buffer-substring (point) (line-end-position)))
6299 (timestr
6300 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6301 (concat (substring s (match-beginning 1)) " ")
6302 'time))
6303 (item (org-agenda-format-item
6304 ;; For past scheduled dates, make sure to
6305 ;; report time difference since SCHEDULE,
6306 ;; not since closest repeater.
6307 (let ((diff (if (< (org-today) current) diff
6308 (- schedule current))))
6309 (if (= diff 0) (car org-agenda-scheduled-leaders)
6310 (format (nth 1 org-agenda-scheduled-leaders)
6311 (- 1 diff))))
6312 head level category tags
6313 (and (= diff 0) timestr)
6314 nil habitp))
6315 (face (cond ((and (not habitp) pastschedp)
6316 'org-scheduled-previously)
6317 (todayp 'org-scheduled-today)
6318 (t 'org-scheduled)))
6319 (habitp (and habitp (org-habit-parse-todo))))
6320 (org-add-props item props
6321 'undone-face face
6322 'face (if donep 'org-agenda-done face)
6323 'org-marker (org-agenda-new-marker pos)
6324 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
6325 'type (if pastschedp "past-scheduled" "scheduled")
6326 'date (if pastschedp schedule date)
6327 'ts-date schedule
6328 'warntime warntime
6329 'level level
6330 'priority (if habitp (org-habit-get-priority habitp)
6331 (+ 94 (- 5 diff) (org-get-priority item)))
6332 'org-habit-p habitp
6333 'todo-state todo-state)
6334 (push item scheduled-items))))))
6335 (nreverse scheduled-items)))
6337 (defun org-agenda-get-blocks ()
6338 "Return the date-range information for agenda display."
6339 (let* ((props (list 'face nil
6340 'org-not-done-regexp org-not-done-regexp
6341 'org-todo-regexp org-todo-regexp
6342 'org-complex-heading-regexp org-complex-heading-regexp
6343 'mouse-face 'highlight
6344 'help-echo
6345 (format "mouse-2 or RET jump to org file %s"
6346 (abbreviate-file-name buffer-file-name))))
6347 (regexp org-tr-regexp)
6348 (d0 (calendar-absolute-from-gregorian date))
6349 marker hdmarker ee txt d1 d2 s1 s2 category
6350 level todo-state tags pos head donep inherited-tags)
6351 (goto-char (point-min))
6352 (while (re-search-forward regexp nil t)
6353 (catch :skip
6354 (org-agenda-skip)
6355 (setq pos (point))
6356 (let ((start-time (match-string 1))
6357 (end-time (match-string 2)))
6358 (setq s1 (match-string 1)
6359 s2 (match-string 2)
6360 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
6361 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
6362 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6363 ;; Only allow days between the limits, because the normal
6364 ;; date stamps will catch the limits.
6365 (save-excursion
6366 (setq todo-state (org-get-todo-state))
6367 (setq donep (member todo-state org-done-keywords))
6368 (if (and donep org-agenda-skip-timestamp-if-done)
6369 (throw :skip t))
6370 (setq marker (org-agenda-new-marker (point))
6371 category (org-get-category))
6372 (if (not (re-search-backward org-outline-regexp-bol nil t))
6373 (throw :skip nil)
6374 (goto-char (match-beginning 0))
6375 (setq hdmarker (org-agenda-new-marker (point))
6376 inherited-tags
6377 (or (eq org-agenda-show-inherited-tags 'always)
6378 (and (listp org-agenda-show-inherited-tags)
6379 (memq 'agenda org-agenda-show-inherited-tags))
6380 (and (eq org-agenda-show-inherited-tags t)
6381 (or (eq org-agenda-use-tag-inheritance t)
6382 (memq 'agenda org-agenda-use-tag-inheritance))))
6384 tags (org-get-tags-at nil (not inherited-tags)))
6385 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6386 (looking-at "\\*+[ \t]+\\(.*\\)")
6387 (setq head (match-string 1))
6388 (let ((remove-re
6389 (if org-agenda-remove-timeranges-from-blocks
6390 (concat
6391 "<" (regexp-quote s1) ".*?>"
6392 "--"
6393 "<" (regexp-quote s2) ".*?>")
6394 nil)))
6395 (setq txt (org-agenda-format-item
6396 (format
6397 (nth (if (= d1 d2) 0 1)
6398 org-agenda-timerange-leaders)
6399 (1+ (- d0 d1)) (1+ (- d2 d1)))
6400 head level category tags
6401 (cond ((and (= d1 d0) (= d2 d0))
6402 (concat "<" start-time ">--<" end-time ">"))
6403 ((= d1 d0)
6404 (concat "<" start-time ">"))
6405 ((= d2 d0)
6406 (concat "<" end-time ">")))
6407 remove-re))))
6408 (org-add-props txt props
6409 'org-marker marker 'org-hd-marker hdmarker
6410 'type "block" 'date date
6411 'level level
6412 'todo-state todo-state
6413 'priority (org-get-priority txt))
6414 (push txt ee))))
6415 (goto-char pos)))
6416 ;; Sort the entries by expiration date.
6417 (nreverse ee)))
6419 ;;; Agenda presentation and sorting
6421 (defvar org-prefix-has-time nil
6422 "A flag, set by `org-compile-prefix-format'.
6423 The flag is set if the currently compiled format contains a `%t'.")
6424 (defvar org-prefix-has-tag nil
6425 "A flag, set by `org-compile-prefix-format'.
6426 The flag is set if the currently compiled format contains a `%T'.")
6427 (defvar org-prefix-has-effort nil
6428 "A flag, set by `org-compile-prefix-format'.
6429 The flag is set if the currently compiled format contains a `%e'.")
6430 (defvar org-prefix-has-breadcrumbs nil
6431 "A flag, set by `org-compile-prefix-format'.
6432 The flag is set if the currently compiled format contains a `%b'.")
6433 (defvar org-prefix-category-length nil
6434 "Used by `org-compile-prefix-format' to remember the category field width.")
6435 (defvar org-prefix-category-max-length nil
6436 "Used by `org-compile-prefix-format' to remember the category field width.")
6438 (defun org-agenda-get-category-icon (category)
6439 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6440 (dolist (entry org-agenda-category-icon-alist)
6441 (when (string-match-p (car entry) category)
6442 (if (listp (cadr entry))
6443 (cl-return (cadr entry))
6444 (cl-return (apply #'create-image (cdr entry)))))))
6446 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6447 remove-re habitp)
6448 "Format TXT to be inserted into the agenda buffer.
6449 In particular, add the prefix and corresponding text properties.
6451 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6452 LEVEL may be a string to replace the `%l' specifier.
6453 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6454 category taken from local variable or file name. It will replace the `%c'
6455 specifier in the format.
6456 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6457 TXT for sorting of this entry, and for the `%t' specifier in the format.
6458 When DOTIME is a string, this string is searched for a time before TXT is.
6459 TAGS can be the tags of the headline.
6460 Any match of REMOVE-RE will be removed from TXT."
6461 ;; We keep the org-prefix-* variable values along with a compiled
6462 ;; formatter, so that multiple agendas existing at the same time do
6463 ;; not step on each other toes.
6465 ;; It was inconvenient to make these variables buffer local in
6466 ;; Agenda buffers, because this function expects to be called with
6467 ;; the buffer where item comes from being current, and not agenda
6468 ;; buffer
6469 (let* ((bindings (car org-prefix-format-compiled))
6470 (formatter (cadr org-prefix-format-compiled)))
6471 (cl-loop for (var value) in bindings
6472 do (set var value))
6473 (save-match-data
6474 ;; Diary entries sometimes have extra whitespace at the beginning
6475 (setq txt (org-trim txt))
6477 ;; Fix the tags part in txt
6478 (setq txt (org-agenda-fix-displayed-tags
6479 txt tags
6480 org-agenda-show-inherited-tags
6481 org-agenda-hide-tags-regexp))
6483 (let* ((category (or category
6484 (if buffer-file-name
6485 (file-name-sans-extension
6486 (file-name-nondirectory buffer-file-name))
6487 "")))
6488 (category-icon (org-agenda-get-category-icon category))
6489 (category-icon (if category-icon
6490 (propertize " " 'display category-icon)
6491 ""))
6492 (effort (and (not (string= txt ""))
6493 (get-text-property 1 'effort txt)))
6494 ;; time, tag, effort are needed for the eval of the prefix format
6495 (tag (if tags (nth (1- (length tags)) tags) ""))
6496 time
6497 (ts (if dotime (concat
6498 (if (stringp dotime) dotime "")
6499 (and org-agenda-search-headline-for-time txt))))
6500 (time-of-day (and dotime (org-get-time-of-day ts)))
6501 stamp plain s0 s1 s2 rtn srp l
6502 duration breadcrumbs)
6503 (and (derived-mode-p 'org-mode) buffer-file-name
6504 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6505 (when (and dotime time-of-day)
6506 ;; Extract starting and ending time and move them to prefix
6507 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6508 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6509 (setq s0 (match-string 0 ts)
6510 srp (and stamp (match-end 3))
6511 s1 (match-string (if plain 1 2) ts)
6512 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6514 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6515 ;; them, we might want to remove them there to avoid duplication.
6516 ;; The user can turn this off with a variable.
6517 (if (and org-prefix-has-time
6518 org-agenda-remove-times-when-in-prefix (or stamp plain)
6519 (string-match (concat (regexp-quote s0) " *") txt)
6520 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6521 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6522 (= (match-beginning 0) 0)
6524 (setq txt (replace-match "" nil nil txt))))
6525 ;; Normalize the time(s) to 24 hour
6526 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6527 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6529 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
6530 (let (org-time-clocksum-use-effort-durations)
6531 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6532 (setq s2
6533 (org-minutes-to-clocksum-string
6534 (+ (org-hh:mm-string-to-minutes s1)
6535 org-agenda-default-appointment-duration)))))
6537 ;; Compute the duration
6538 (when s2
6539 (setq duration (- (org-hh:mm-string-to-minutes s2)
6540 (org-hh:mm-string-to-minutes s1)))))
6542 (when (string-match "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" txt)
6543 ;; Tags are in the string
6544 (if (or (eq org-agenda-remove-tags t)
6545 (and org-agenda-remove-tags
6546 org-prefix-has-tag))
6547 (setq txt (replace-match "" t t txt))
6548 (setq txt (replace-match
6549 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6550 (match-string 2 txt))
6551 t t txt))))
6553 (when remove-re
6554 (while (string-match remove-re txt)
6555 (setq txt (replace-match "" t t txt))))
6557 ;; Set org-heading property on `txt' to mark the start of the
6558 ;; heading.
6559 (add-text-properties 0 (length txt) '(org-heading t) txt)
6561 ;; Prepare the variables needed in the eval of the compiled format
6562 (if org-prefix-has-breadcrumbs
6563 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6564 (let ((s (org-display-outline-path nil nil "->" t)))
6565 (if (eq "" s) "" (concat s "->"))))))
6566 (setq time (cond (s2 (concat
6567 (org-agenda-time-of-day-to-ampm-maybe s1)
6568 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6569 (if org-agenda-timegrid-use-ampm " ")))
6570 (s1 (concat
6571 (org-agenda-time-of-day-to-ampm-maybe s1)
6572 (if org-agenda-timegrid-use-ampm
6573 "........ "
6574 "......")))
6575 (t ""))
6576 extra (or (and (not habitp) extra) "")
6577 category (if (symbolp category) (symbol-name category) category)
6578 level (or level ""))
6579 (if (string-match org-bracket-link-regexp category)
6580 (progn
6581 (setq l (if (match-end 3)
6582 (- (match-end 3) (match-beginning 3))
6583 (- (match-end 1) (match-beginning 1))))
6584 (when (< l (or org-prefix-category-length 0))
6585 (setq category (copy-sequence category))
6586 (org-add-props category nil
6587 'extra-space (make-string
6588 (- org-prefix-category-length l 1) ?\ ))))
6589 (if (and org-prefix-category-max-length
6590 (>= (length category) org-prefix-category-max-length))
6591 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6592 ;; Evaluate the compiled format
6593 (setq rtn (concat (eval formatter) txt))
6595 ;; And finally add the text properties
6596 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6597 (org-add-props rtn nil
6598 'org-category category
6599 'tags (mapcar 'org-downcase-keep-props tags)
6600 'org-highest-priority org-highest-priority
6601 'org-lowest-priority org-lowest-priority
6602 'time-of-day time-of-day
6603 'duration duration
6604 'breadcrumbs breadcrumbs
6605 'txt txt
6606 'level level
6607 'time time
6608 'extra extra
6609 'format org-prefix-format-compiled
6610 'dotime dotime)))))
6612 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6613 "Remove tags string from TXT, and add a modified list of tags.
6614 The modified list may contain inherited tags, and tags matched by
6615 `org-agenda-hide-tags-regexp' will be removed."
6616 (when (or add-inherited hide-re)
6617 (if (string-match "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" txt)
6618 (setq txt (substring txt 0 (match-beginning 0))))
6619 (setq tags
6620 (delq nil
6621 (mapcar (lambda (tg)
6622 (if (or (and hide-re (string-match hide-re tg))
6623 (and (not add-inherited)
6624 (get-text-property 0 'inherited tg)))
6626 tg))
6627 tags)))
6628 (when tags
6629 (let ((have-i (get-text-property 0 'inherited (car tags)))
6631 (setq txt (concat txt " :"
6632 (mapconcat
6633 (lambda (x)
6634 (setq i (get-text-property 0 'inherited x))
6635 (if (and have-i (not i))
6636 (progn
6637 (setq have-i nil)
6638 (concat ":" x))
6640 tags ":")
6641 (if have-i "::" ":"))))))
6642 txt)
6644 (defun org-downcase-keep-props (s)
6645 (let ((props (text-properties-at 0 s)))
6646 (setq s (downcase s))
6647 (add-text-properties 0 (length s) props s)
6650 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6652 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6653 "Add a time-grid for agenda items which need it.
6655 LIST is the list of agenda items formatted by `org-agenda-list'.
6656 NDAYS is the span of the current agenda view.
6657 TODAYP is t when the current agenda view is on today."
6658 (catch 'exit
6659 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6660 ((and todayp (member 'today (car org-agenda-time-grid))))
6661 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6662 ((member 'weekly (car org-agenda-time-grid)))
6663 (t (throw 'exit list)))
6664 (let* ((have (delq nil (mapcar
6665 (lambda (x) (get-text-property 1 'time-of-day x))
6666 list)))
6667 (string (nth 1 org-agenda-time-grid))
6668 (gridtimes (nth 2 org-agenda-time-grid))
6669 (req (car org-agenda-time-grid))
6670 (remove (member 'remove-match req))
6671 new time)
6672 (if (and (member 'require-timed req) (not have))
6673 ;; don't show empty grid
6674 (throw 'exit list))
6675 (while (setq time (pop gridtimes))
6676 (unless (and remove (member time have))
6677 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6678 (push (org-agenda-format-item
6679 nil string nil "" nil
6680 (concat (substring time 0 -2) ":" (substring time -2)))
6681 new)
6682 (put-text-property
6683 2 (length (car new)) 'face 'org-time-grid (car new))))
6684 (when (and todayp org-agenda-show-current-time-in-grid)
6685 (push (org-agenda-format-item
6686 nil org-agenda-current-time-string nil "" nil
6687 (format-time-string "%H:%M "))
6688 new)
6689 (put-text-property
6690 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6692 (if (member 'time-up org-agenda-sorting-strategy-selected)
6693 (append new list)
6694 (append list new)))))
6696 (defun org-compile-prefix-format (key)
6697 "Compile the prefix format into a Lisp form that can be evaluated.
6698 The resulting form and associated variable bindings is returned
6699 and stored in the variable `org-prefix-format-compiled'."
6700 (setq org-prefix-has-time nil
6701 org-prefix-has-tag nil
6702 org-prefix-category-length nil
6703 org-prefix-has-effort nil
6704 org-prefix-has-breadcrumbs nil)
6705 (let ((s (cond
6706 ((stringp org-agenda-prefix-format)
6707 org-agenda-prefix-format)
6708 ((assq key org-agenda-prefix-format)
6709 (cdr (assq key org-agenda-prefix-format)))
6710 (t " %-12:c%?-12t% s")))
6711 (start 0)
6712 varform vars var e c f opt)
6713 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6714 s start)
6715 (setq var (or (cdr (assoc (match-string 4 s)
6716 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6717 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6718 'eval)
6719 c (or (match-string 3 s) "")
6720 opt (match-beginning 1)
6721 start (1+ (match-beginning 0)))
6722 (if (eq var 'time) (setq org-prefix-has-time t))
6723 (if (eq var 'tag) (setq org-prefix-has-tag t))
6724 (if (eq var 'effort) (setq org-prefix-has-effort t))
6725 (if (eq var 'breadcrumbs) (setq org-prefix-has-breadcrumbs t))
6726 (setq f (concat "%" (match-string 2 s) "s"))
6727 (when (eq var 'category)
6728 (setq org-prefix-category-length
6729 (floor (abs (string-to-number (match-string 2 s)))))
6730 (setq org-prefix-category-max-length
6731 (let ((x (match-string 2 s)))
6732 (save-match-data
6733 (if (string-match "\\.[0-9]+" x)
6734 (string-to-number (substring (match-string 0 x) 1)))))))
6735 (if (eq var 'eval)
6736 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6737 (if opt
6738 (setq varform
6739 `(if (or (equal "" ,var) (equal nil ,var))
6741 (format ,f (concat ,var ,c))))
6742 (setq varform
6743 `(format ,f (if (or (equal ,var "")
6744 (equal ,var nil)) ""
6745 (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6746 (setq s (replace-match "%s" t nil s))
6747 (push varform vars))
6748 (setq vars (nreverse vars))
6749 (with-current-buffer (or org-agenda-buffer (current-buffer))
6750 (setq org-prefix-format-compiled
6751 (list
6752 `((org-prefix-has-time ,org-prefix-has-time)
6753 (org-prefix-has-tag ,org-prefix-has-tag)
6754 (org-prefix-category-length ,org-prefix-category-length)
6755 (org-prefix-has-effort ,org-prefix-has-effort)
6756 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6757 `(format ,s ,@vars))))))
6759 (defun org-set-sorting-strategy (key)
6760 (if (symbolp (car org-agenda-sorting-strategy))
6761 ;; the old format
6762 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6763 (setq org-agenda-sorting-strategy-selected
6764 (or (cdr (assq key org-agenda-sorting-strategy))
6765 (cdr (assq 'agenda org-agenda-sorting-strategy))
6766 '(time-up category-keep priority-down)))))
6768 (defun org-get-time-of-day (s &optional string mod24)
6769 "Check string S for a time of day.
6770 If found, return it as a military time number between 0 and 2400.
6771 If not found, return nil.
6772 The optional STRING argument forces conversion into a 5 character wide string
6773 HH:MM."
6774 (save-match-data
6775 (when
6776 (and
6777 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6778 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6779 (not (eq (get-text-property 1 'face s) 'org-link)))
6780 (let* ((h (string-to-number (match-string 1 s)))
6781 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6782 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6783 (am-p (equal ampm "am"))
6784 (h1 (cond ((not ampm) h)
6785 ((= h 12) (if am-p 0 12))
6786 (t (+ h (if am-p 0 12)))))
6787 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6788 (mod h1 24) h1))
6789 (t0 (+ (* 100 h2) m))
6790 (t1 (concat (if (>= h1 24) "+" " ")
6791 (if (and org-agenda-time-leading-zero
6792 (< t0 1000)) "0" "")
6793 (if (< t0 100) "0" "")
6794 (if (< t0 10) "0" "")
6795 (int-to-string t0))))
6796 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6798 (defvar org-agenda-before-sorting-filter-function nil
6799 "Function to be applied to agenda items prior to sorting.
6800 Prior to sorting also means just before they are inserted into the agenda.
6802 To aid sorting, you may revisit the original entries and add more text
6803 properties which will later be used by the sorting functions.
6805 The function should take a string argument, an agenda line.
6806 It has access to the text properties in that line, which contain among
6807 other things, the property `org-hd-marker' that points to the entry
6808 where the line comes from. Note that not all lines going into the agenda
6809 have this property, only most.
6811 The function should return the modified string. It is probably best
6812 to ONLY change text properties.
6814 You can also use this function as a filter, by returning nil for lines
6815 you don't want to have in the agenda at all. For this application, you
6816 could bind the variable in the options section of a custom command.")
6818 (defun org-agenda-finalize-entries (list &optional type)
6819 "Sort, limit and concatenate the LIST of agenda items.
6820 The optional argument TYPE tells the agenda type."
6821 (let ((max-effort (cond ((listp org-agenda-max-effort)
6822 (cdr (assoc type org-agenda-max-effort)))
6823 (t org-agenda-max-effort)))
6824 (max-todo (cond ((listp org-agenda-max-todos)
6825 (cdr (assoc type org-agenda-max-todos)))
6826 (t org-agenda-max-todos)))
6827 (max-tags (cond ((listp org-agenda-max-tags)
6828 (cdr (assoc type org-agenda-max-tags)))
6829 (t org-agenda-max-tags)))
6830 (max-entries (cond ((listp org-agenda-max-entries)
6831 (cdr (assoc type org-agenda-max-entries)))
6832 (t org-agenda-max-entries))))
6833 (when org-agenda-before-sorting-filter-function
6834 (setq list
6835 (delq nil
6836 (mapcar
6837 org-agenda-before-sorting-filter-function list))))
6838 (setq list (mapcar 'org-agenda-highlight-todo list)
6839 list (mapcar 'identity (sort list 'org-entries-lessp)))
6840 (when max-effort
6841 (setq list (org-agenda-limit-entries
6842 list 'effort-minutes max-effort
6843 (lambda (e) (or e (if org-sort-agenda-noeffort-is-high
6844 32767 -1))))))
6845 (when max-todo
6846 (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
6847 (when max-tags
6848 (setq list (org-agenda-limit-entries list 'tags max-tags)))
6849 (when max-entries
6850 (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
6851 (mapconcat 'identity list "\n")))
6853 (defun org-agenda-limit-entries (list prop limit &optional fn)
6854 "Limit the number of agenda entries."
6855 (let ((include (and limit (< limit 0))))
6856 (if limit
6857 (let ((fun (or fn (lambda (p) (if p 1))))
6858 (lim 0))
6859 (delq nil
6860 (mapcar
6861 (lambda (e)
6862 (let ((pval (funcall
6863 fun (get-text-property (1- (length e))
6864 prop e))))
6865 (if pval (setq lim (+ lim pval)))
6866 (cond ((and pval (<= lim (abs limit))) e)
6867 ((and include (not pval)) e))))
6868 list)))
6869 list)))
6871 (defun org-agenda-limit-interactively (remove)
6872 "In agenda, interactively limit entries to various maximums."
6873 (interactive "P")
6874 (if remove
6875 (progn (setq org-agenda-max-entries nil
6876 org-agenda-max-todos nil
6877 org-agenda-max-tags nil
6878 org-agenda-max-effort nil)
6879 (org-agenda-redo))
6880 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6881 (msg (cond ((= max ?E) "How many minutes? ")
6882 ((= max ?e) "How many entries? ")
6883 ((= max ?t) "How many TODO entries? ")
6884 ((= max ?T) "How many tagged entries? ")
6885 (t (user-error "Wrong input"))))
6886 (num (string-to-number (read-from-minibuffer msg))))
6887 (cond ((equal max ?e)
6888 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6889 ((equal max ?t)
6890 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6891 ((equal max ?T)
6892 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6893 ((equal max ?E)
6894 (let ((org-agenda-max-effort num)) (org-agenda-redo))))))
6895 (org-agenda-fit-window-to-buffer))
6897 (defun org-agenda-highlight-todo (x)
6898 (let ((org-done-keywords org-done-keywords-for-agenda)
6899 (case-fold-search nil)
6901 (if (eq x 'line)
6902 (save-excursion
6903 (beginning-of-line 1)
6904 (setq re (org-get-at-bol 'org-todo-regexp))
6905 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6906 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6907 (add-text-properties (match-beginning 0) (match-end 1)
6908 (list 'face (org-get-todo-face 1)))
6909 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6910 (delete-region (match-beginning 1) (1- (match-end 0)))
6911 (goto-char (match-beginning 1))
6912 (insert (format org-agenda-todo-keyword-format s)))))
6913 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6914 (setq re (get-text-property 0 'org-todo-regexp x))
6915 (when (and re
6916 ;; Test `pl' because if there's no heading content,
6917 ;; there's no point matching to highlight. Note
6918 ;; that if we didn't test `pl' first, and there
6919 ;; happened to be no keyword from `org-todo-regexp'
6920 ;; on this heading line, then the `equal' comparison
6921 ;; afterwards would spuriously succeed in the case
6922 ;; where `pl' is nil -- causing an args-out-of-range
6923 ;; error when we try to add text properties to text
6924 ;; that isn't there.
6926 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6927 x pl) pl))
6928 (add-text-properties
6929 (or (match-end 1) (match-end 0)) (match-end 0)
6930 (list 'face (org-get-todo-face (match-string 2 x)))
6932 (when (match-end 1)
6933 (setq x (concat (substring x 0 (match-end 1))
6934 (format org-agenda-todo-keyword-format
6935 (match-string 2 x))
6936 (org-add-props " " (text-properties-at 0 x))
6937 (substring x (match-end 3)))))))
6938 x)))
6940 (defsubst org-cmp-values (a b property)
6941 "Compare the numeric value of text PROPERTY for string A and B."
6942 (let ((pa (or (get-text-property (1- (length a)) property a) 0))
6943 (pb (or (get-text-property (1- (length b)) property b) 0)))
6944 (cond ((> pa pb) +1)
6945 ((< pa pb) -1))))
6947 (defsubst org-cmp-effort (a b)
6948 "Compare the effort values of string A and B."
6949 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6950 (ea (or (get-text-property (1- (length a)) 'effort-minutes a) def))
6951 (eb (or (get-text-property (1- (length b)) 'effort-minutes b) def)))
6952 (cond ((> ea eb) +1)
6953 ((< ea eb) -1))))
6955 (defsubst org-cmp-category (a b)
6956 "Compare the string values of categories of strings A and B."
6957 (let ((ca (or (get-text-property (1- (length a)) 'org-category a) ""))
6958 (cb (or (get-text-property (1- (length b)) 'org-category b) "")))
6959 (cond ((string-lessp ca cb) -1)
6960 ((string-lessp cb ca) +1))))
6962 (defsubst org-cmp-todo-state (a b)
6963 "Compare the todo states of strings A and B."
6964 (let* ((ma (or (get-text-property 1 'org-marker a)
6965 (get-text-property 1 'org-hd-marker a)))
6966 (mb (or (get-text-property 1 'org-marker b)
6967 (get-text-property 1 'org-hd-marker b)))
6968 (fa (and ma (marker-buffer ma)))
6969 (fb (and mb (marker-buffer mb)))
6970 (todo-kwds
6971 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6972 (and fb (with-current-buffer fb org-todo-keywords-1))))
6973 (ta (or (get-text-property 1 'todo-state a) ""))
6974 (tb (or (get-text-property 1 'todo-state b) ""))
6975 (la (- (length (member ta todo-kwds))))
6976 (lb (- (length (member tb todo-kwds))))
6977 (donepa (member ta org-done-keywords-for-agenda))
6978 (donepb (member tb org-done-keywords-for-agenda)))
6979 (cond ((and donepa (not donepb)) -1)
6980 ((and (not donepa) donepb) +1)
6981 ((< la lb) -1)
6982 ((< lb la) +1))))
6984 (defsubst org-cmp-alpha (a b)
6985 "Compare the headlines, alphabetically."
6986 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6987 (plb (text-property-any 0 (length b) 'org-heading t b))
6988 (ta (and pla (substring a pla)))
6989 (tb (and plb (substring b plb))))
6990 (when pla
6991 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6992 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6993 (setq ta (substring ta (match-end 0))))
6994 (setq ta (downcase ta)))
6995 (when plb
6996 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6997 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6998 (setq tb (substring tb (match-end 0))))
6999 (setq tb (downcase tb)))
7000 (cond ((not ta) +1)
7001 ((not tb) -1)
7002 ((string-lessp ta tb) -1)
7003 ((string-lessp tb ta) +1))))
7005 (defsubst org-cmp-tag (a b)
7006 "Compare the string values of the first tags of A and B."
7007 (let ((ta (car (last (get-text-property 1 'tags a))))
7008 (tb (car (last (get-text-property 1 'tags b)))))
7009 (cond ((not ta) +1)
7010 ((not tb) -1)
7011 ((string-lessp ta tb) -1)
7012 ((string-lessp tb ta) +1))))
7014 (defsubst org-cmp-time (a b)
7015 "Compare the time-of-day values of strings A and B."
7016 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
7017 (ta (or (get-text-property 1 'time-of-day a) def))
7018 (tb (or (get-text-property 1 'time-of-day b) def)))
7019 (cond ((< ta tb) -1)
7020 ((< tb ta) +1))))
7022 (defsubst org-cmp-ts (a b type)
7023 "Compare the timestamps values of entries A and B.
7024 When TYPE is \"scheduled\", \"deadline\", \"timestamp\" or
7025 \"timestamp_ia\", compare within each of these type. When TYPE
7026 is the empty string, compare all timestamps without respect of
7027 their type."
7028 (let* ((def (if org-sort-agenda-notime-is-late most-positive-fixnum -1))
7029 (ta (or (and (string-match type (or (get-text-property 1 'type a) ""))
7030 (get-text-property 1 'ts-date a))
7031 def))
7032 (tb (or (and (string-match type (or (get-text-property 1 'type b) ""))
7033 (get-text-property 1 'ts-date b))
7034 def)))
7035 (cond ((< ta tb) -1)
7036 ((< tb ta) +1))))
7038 (defsubst org-cmp-habit-p (a b)
7039 "Compare the todo states of strings A and B."
7040 (let ((ha (get-text-property 1 'org-habit-p a))
7041 (hb (get-text-property 1 'org-habit-p b)))
7042 (cond ((and ha (not hb)) -1)
7043 ((and (not ha) hb) +1))))
7045 (defun org-entries-lessp (a b)
7046 "Predicate for sorting agenda entries."
7047 ;; The following variables will be used when the form is evaluated.
7048 ;; So even though the compiler complains, keep them.
7049 (let* ((ss org-agenda-sorting-strategy-selected)
7050 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
7051 (org-cmp-ts a b "")))
7052 (timestamp-down (if timestamp-up (- timestamp-up) nil))
7053 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
7054 (org-cmp-ts a b "scheduled")))
7055 (scheduled-down (if scheduled-up (- scheduled-up) nil))
7056 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
7057 (org-cmp-ts a b "deadline")))
7058 (deadline-down (if deadline-up (- deadline-up) nil))
7059 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
7060 (org-cmp-ts a b "timestamp_ia")))
7061 (tsia-down (if tsia-up (- tsia-up) nil))
7062 (ts-up (and (org-em 'ts-up 'ts-down ss)
7063 (org-cmp-ts a b "timestamp")))
7064 (ts-down (if ts-up (- ts-up) nil))
7065 (time-up (and (org-em 'time-up 'time-down ss)
7066 (org-cmp-time a b)))
7067 (time-down (if time-up (- time-up) nil))
7068 (stats-up (and (org-em 'stats-up 'stats-down ss)
7069 (org-cmp-values a b 'org-stats)))
7070 (stats-down (if stats-up (- stats-up) nil))
7071 (priority-up (and (org-em 'priority-up 'priority-down ss)
7072 (org-cmp-values a b 'priority)))
7073 (priority-down (if priority-up (- priority-up) nil))
7074 (effort-up (and (org-em 'effort-up 'effort-down ss)
7075 (org-cmp-effort a b)))
7076 (effort-down (if effort-up (- effort-up) nil))
7077 (category-up (and (or (org-em 'category-up 'category-down ss)
7078 (memq 'category-keep ss))
7079 (org-cmp-category a b)))
7080 (category-down (if category-up (- category-up) nil))
7081 (category-keep (if category-up +1 nil))
7082 (tag-up (and (org-em 'tag-up 'tag-down ss)
7083 (org-cmp-tag a b)))
7084 (tag-down (if tag-up (- tag-up) nil))
7085 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
7086 (org-cmp-todo-state a b)))
7087 (todo-state-down (if todo-state-up (- todo-state-up) nil))
7088 (habit-up (and (org-em 'habit-up 'habit-down ss)
7089 (org-cmp-habit-p a b)))
7090 (habit-down (if habit-up (- habit-up) nil))
7091 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
7092 (org-cmp-alpha a b)))
7093 (alpha-down (if alpha-up (- alpha-up) nil))
7094 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
7095 user-defined-up user-defined-down)
7096 (if (and need-user-cmp org-agenda-cmp-user-defined
7097 (functionp org-agenda-cmp-user-defined))
7098 (setq user-defined-up
7099 (funcall org-agenda-cmp-user-defined a b)
7100 user-defined-down (if user-defined-up (- user-defined-up) nil)))
7101 (cdr (assoc
7102 (eval (cons 'or org-agenda-sorting-strategy-selected))
7103 '((-1 . t) (1 . nil) (nil . nil))))))
7105 ;;; Agenda restriction lock
7107 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
7108 "Overlay to mark the headline to which agenda commands are restricted.")
7109 (overlay-put org-agenda-restriction-lock-overlay
7110 'face 'org-agenda-restriction-lock)
7111 (overlay-put org-agenda-restriction-lock-overlay
7112 'help-echo "Agendas are currently limited to this subtree.")
7113 (delete-overlay org-agenda-restriction-lock-overlay)
7115 ;;;###autoload
7116 (defun org-agenda-set-restriction-lock (&optional type)
7117 "Set restriction lock for agenda, to current subtree or file.
7118 Restriction will be the file if TYPE is `file', or if type is the
7119 universal prefix \\='(4), or if the cursor is before the first headline
7120 in the file. Otherwise, restriction will be to the current subtree."
7121 (interactive "P")
7122 (org-agenda-remove-restriction-lock 'noupdate)
7123 (and (equal type '(4)) (setq type 'file))
7124 (setq type (cond
7125 (type type)
7126 ((org-at-heading-p) 'subtree)
7127 ((condition-case nil (org-back-to-heading t) (error nil))
7128 'subtree)
7129 (t 'file)))
7130 (if (eq type 'subtree)
7131 (progn
7132 (setq org-agenda-restrict (current-buffer))
7133 (setq org-agenda-overriding-restriction 'subtree)
7134 (put 'org-agenda-files 'org-restrict
7135 (list (buffer-file-name (buffer-base-buffer))))
7136 (org-back-to-heading t)
7137 (move-overlay org-agenda-restriction-lock-overlay
7138 (point)
7139 (if org-agenda-restriction-lock-highlight-subtree
7140 (save-excursion (org-end-of-subtree t t) (point))
7141 (point-at-eol)))
7142 (move-marker org-agenda-restrict-begin (point))
7143 (move-marker org-agenda-restrict-end
7144 (save-excursion (org-end-of-subtree t t)))
7145 (message "Locking agenda restriction to subtree"))
7146 (put 'org-agenda-files 'org-restrict
7147 (list (buffer-file-name (buffer-base-buffer))))
7148 (setq org-agenda-restrict nil)
7149 (setq org-agenda-overriding-restriction 'file)
7150 (move-marker org-agenda-restrict-begin nil)
7151 (move-marker org-agenda-restrict-end nil)
7152 (message "Locking agenda restriction to file"))
7153 (setq current-prefix-arg nil)
7154 (org-agenda-maybe-redo))
7156 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7157 "Remove the agenda restriction lock."
7158 (interactive "P")
7159 (delete-overlay org-agenda-restriction-lock-overlay)
7160 (delete-overlay org-speedbar-restriction-lock-overlay)
7161 (setq org-agenda-overriding-restriction nil)
7162 (setq org-agenda-restrict nil)
7163 (put 'org-agenda-files 'org-restrict nil)
7164 (move-marker org-agenda-restrict-begin nil)
7165 (move-marker org-agenda-restrict-end nil)
7166 (setq current-prefix-arg nil)
7167 (message "Agenda restriction lock removed")
7168 (or noupdate (org-agenda-maybe-redo)))
7170 (defun org-agenda-maybe-redo ()
7171 "If there is any window showing the agenda view, update it."
7172 (let ((w (get-buffer-window (or org-agenda-this-buffer-name
7173 org-agenda-buffer-name)
7175 (w0 (selected-window)))
7176 (when w
7177 (select-window w)
7178 (org-agenda-redo)
7179 (select-window w0)
7180 (if org-agenda-overriding-restriction
7181 (message "Agenda view shifted to new %s restriction"
7182 org-agenda-overriding-restriction)
7183 (message "Agenda restriction lock removed")))))
7185 ;;; Agenda commands
7187 (defun org-agenda-check-type (error &rest types)
7188 "Check if agenda buffer is of allowed type.
7189 If ERROR is non-nil, throw an error, otherwise just return nil.
7190 Allowed types are `agenda' `timeline' `todo' `tags' `search'."
7191 (if (not org-agenda-type)
7192 (error "No Org agenda currently displayed")
7193 (if (memq org-agenda-type types)
7195 (if error
7196 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
7197 nil))))
7199 (defun org-agenda-Quit ()
7200 "Exit the agenda, killing the agenda buffer.
7201 Like `org-agenda-quit', but kill the buffer even when
7202 `org-agenda-sticky' is non-nil."
7203 (interactive)
7204 (org-agenda--quit))
7206 (defun org-agenda-quit ()
7207 "Exit the agenda.
7209 When `org-agenda-sticky' is non-nil, bury the agenda buffer
7210 instead of killing it.
7212 When `org-agenda-restore-windows-after-quit' is non-nil, restore
7213 the pre-agenda window configuration.
7215 When column view is active, exit column view instead of the
7216 agenda."
7217 (interactive)
7218 (org-agenda--quit org-agenda-sticky))
7220 (defun org-agenda--quit (&optional bury)
7221 (if org-agenda-columns-active
7222 (org-columns-quit)
7223 (let ((wconf org-agenda-pre-window-conf)
7224 (buf (current-buffer))
7225 (org-agenda-last-indirect-window
7226 (and (eq org-indirect-buffer-display 'other-window)
7227 org-agenda-last-indirect-buffer
7228 (get-buffer-window org-agenda-last-indirect-buffer))))
7229 (cond
7230 ((eq org-agenda-window-setup 'other-frame)
7231 (delete-frame))
7232 ((and org-agenda-restore-windows-after-quit
7233 wconf)
7234 ;; Maybe restore the pre-agenda window configuration. Reset
7235 ;; `org-agenda-pre-window-conf' before running
7236 ;; `set-window-configuration', which loses the current buffer.
7237 (setq org-agenda-pre-window-conf nil)
7238 (set-window-configuration wconf))
7240 (when org-agenda-last-indirect-window
7241 (delete-window org-agenda-last-indirect-window))
7242 (and (not (eq org-agenda-window-setup 'current-window))
7243 (not (one-window-p))
7244 (delete-window))))
7245 (if bury
7246 ;; Set the agenda buffer as the current buffer instead of
7247 ;; passing it as an argument to `bury-buffer' so that
7248 ;; `bury-buffer' removes it from the window.
7249 (with-current-buffer buf
7250 (bury-buffer))
7251 (kill-buffer buf)
7252 (setq org-agenda-archives-mode nil
7253 org-agenda-buffer nil)))))
7255 (defun org-agenda-exit ()
7256 "Exit the agenda, killing Org buffers loaded by the agenda.
7257 Like `org-agenda-Quit', but kill any buffers that were created by
7258 the agenda. Org buffers visited directly by the user will not be
7259 touched. Also, exit the agenda even if it is in column view."
7260 (interactive)
7261 (when org-agenda-columns-active
7262 (org-columns-quit))
7263 (org-release-buffers org-agenda-new-buffers)
7264 (setq org-agenda-new-buffers nil)
7265 (org-agenda-Quit))
7267 (defun org-agenda-kill-all-agenda-buffers ()
7268 "Kill all buffers in `org-agenda-mode'.
7269 This is used when toggling sticky agendas."
7270 (interactive)
7271 (let (blist)
7272 (dolist (buf (buffer-list))
7273 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7274 (push buf blist)))
7275 (mapc 'kill-buffer blist)))
7277 (defun org-agenda-execute (arg)
7278 "Execute another agenda command, keeping same window.
7279 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7280 in the agenda."
7281 (interactive "P")
7282 (let ((org-agenda-window-setup 'current-window))
7283 (org-agenda arg)))
7285 (defun org-agenda-redo (&optional all)
7286 "Rebuild possibly ALL agenda view(s) in the current buffer."
7287 (interactive "P")
7288 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7289 (cpa (unless (eq all t) current-prefix-arg))
7290 (org-agenda-doing-sticky-redo org-agenda-sticky)
7291 (org-agenda-sticky nil)
7292 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7293 org-agenda-buffer-name))
7294 (org-agenda-keep-modes t)
7295 (tag-filter org-agenda-tag-filter)
7296 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7297 (top-hl-filter org-agenda-top-headline-filter)
7298 (cat-filter org-agenda-category-filter)
7299 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7300 (re-filter org-agenda-regexp-filter)
7301 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7302 (effort-filter org-agenda-effort-filter)
7303 (effort-preset (get 'org-agenda-effort-filter :preset-filter))
7304 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7305 (cols org-agenda-columns-active)
7306 (line (org-current-line))
7307 (window-line (- line (org-current-line (window-start))))
7308 (lprops (get 'org-agenda-redo-command 'org-lprops))
7309 (redo-cmd (get-text-property p 'org-redo-cmd))
7310 (last-args (get-text-property p 'org-last-args))
7311 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7312 (org-agenda-overriding-cmd-arguments
7313 (unless (eq all t)
7314 (cond ((listp last-args)
7315 (cons (or cpa (car last-args)) (cdr last-args)))
7316 ((stringp last-args)
7317 last-args))))
7318 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7319 (put 'org-agenda-tag-filter :preset-filter nil)
7320 (put 'org-agenda-category-filter :preset-filter nil)
7321 (put 'org-agenda-regexp-filter :preset-filter nil)
7322 (put 'org-agenda-effort-filter :preset-filter nil)
7323 (and cols (org-columns-quit))
7324 (message "Rebuilding agenda buffer...")
7325 (if series-redo-cmd
7326 (eval series-redo-cmd)
7327 (org-let lprops redo-cmd))
7328 (setq org-agenda-undo-list nil
7329 org-agenda-pending-undo-list nil
7330 org-agenda-tag-filter tag-filter
7331 org-agenda-category-filter cat-filter
7332 org-agenda-regexp-filter re-filter
7333 org-agenda-effort-filter effort-filter
7334 org-agenda-top-headline-filter top-hl-filter)
7335 (message "Rebuilding agenda buffer...done")
7336 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7337 (put 'org-agenda-category-filter :preset-filter cat-preset)
7338 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7339 (put 'org-agenda-effort-filter :preset-filter effort-preset)
7340 (let ((tag (or tag-filter tag-preset))
7341 (cat (or cat-filter cat-preset))
7342 (effort (or effort-filter effort-preset))
7343 (re (or re-filter re-preset)))
7344 (when tag (org-agenda-filter-apply tag 'tag t))
7345 (when cat (org-agenda-filter-apply cat 'category))
7346 (when effort (org-agenda-filter-apply effort 'effort))
7347 (when re (org-agenda-filter-apply re 'regexp)))
7348 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7349 (and cols (called-interactively-p 'any) (org-agenda-columns))
7350 (org-goto-line line)
7351 (recenter window-line)))
7353 (defvar org-global-tags-completion-table nil)
7354 (defvar org-agenda-filter-form nil)
7355 (defvar org-agenda-filtered-by-category nil)
7357 (defun org-agenda-filter-by-category (strip)
7358 "Filter lines in the agenda buffer that have a specific category.
7359 The category is that of the current line.
7360 Without prefix argument, keep only the lines of that category.
7361 With a prefix argument, exclude the lines of that category.
7363 (interactive "P")
7364 (if (and org-agenda-filtered-by-category
7365 org-agenda-category-filter)
7366 (org-agenda-filter-show-all-cat)
7367 (let ((cat (org-no-properties (org-get-at-eol 'org-category 1))))
7368 (cond
7369 ((and cat strip)
7370 (org-agenda-filter-apply
7371 (push (concat "-" cat) org-agenda-category-filter) 'category))
7372 (cat
7373 (org-agenda-filter-apply
7374 (setq org-agenda-category-filter
7375 (list (concat "+" cat))) 'category))
7376 (t (error "No category at point"))))))
7378 (defun org-find-top-headline (&optional pos)
7379 "Find the topmost parent headline and return it."
7380 (save-excursion
7381 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
7382 (if pos (goto-char pos))
7383 ;; Skip up to the topmost parent
7384 (while (ignore-errors (outline-up-heading 1) t))
7385 (ignore-errors
7386 (nth 4 (org-heading-components))))))
7388 (defvar org-agenda-filtered-by-top-headline nil)
7389 (defun org-agenda-filter-by-top-headline (strip)
7390 "Keep only those lines that are descendants from the same top headline.
7391 The top headline is that of the current line."
7392 (interactive "P")
7393 (if org-agenda-filtered-by-top-headline
7394 (progn
7395 (setq org-agenda-filtered-by-top-headline nil
7396 org-agenda-top-headline-filter nil)
7397 (org-agenda-filter-show-all-top-filter))
7398 (let ((toph (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7399 (if toph (org-agenda-filter-top-headline-apply toph strip)
7400 (error "No top-level headline at point")))))
7402 (defvar org-agenda-regexp-filter nil)
7403 (defun org-agenda-filter-by-regexp (strip)
7404 "Filter agenda entries by a regular expression.
7405 Regexp filters are cumulative.
7406 With no prefix argument, keep entries matching the regexp.
7407 With one prefix argument, filter out entries matching the regexp.
7408 With two prefix arguments, remove the regexp filters."
7409 (interactive "P")
7410 (if (not (equal strip '(16)))
7411 (let ((flt (concat (if (equal strip '(4)) "-" "+")
7412 (read-from-minibuffer
7413 (if (equal strip '(4))
7414 "Filter out entries matching regexp: "
7415 "Narrow to entries matching regexp: ")))))
7416 (push flt org-agenda-regexp-filter)
7417 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
7418 (org-agenda-filter-show-all-re)
7419 (message "Regexp filter removed")))
7421 (defvar org-agenda-effort-filter nil)
7422 (defun org-agenda-filter-by-effort (strip)
7423 "Filter agenda entries by effort.
7424 With no prefix argument, keep entries matching the effort condition.
7425 With one prefix argument, filter out entries matching the condition.
7426 With two prefix arguments, remove the effort filters."
7427 (interactive "P")
7428 (cond ((member strip '(nil 4))
7429 (let ((efforts (org-split-string
7430 (or (cdr (assoc (concat org-effort-property "_ALL")
7431 org-global-properties))
7432 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
7433 "")))
7434 (eff -1)
7435 effort-prompt op)
7436 (while (not (member op '(?< ?> ?=)))
7437 (setq op (read-char-exclusive "Effort operator? (> = or <)")))
7438 (cl-loop for i from 0 to 9 do
7439 (setq effort-prompt
7440 (concat
7441 effort-prompt " ["
7442 (if (= i 9) "0" (int-to-string (1+ i)))
7443 "]" (nth i efforts))))
7444 (message "Effort %s%s" (char-to-string op) effort-prompt)
7445 (while (or (< eff 0) (> eff 9))
7446 (setq eff (string-to-number (char-to-string (read-char-exclusive)))))
7447 (setq org-agenda-effort-filter
7448 (list (concat (if strip "-" "+")
7449 (char-to-string op) (nth (1- eff) efforts))))
7450 (org-agenda-filter-apply org-agenda-effort-filter 'effort)))
7451 (t (org-agenda-filter-show-all-effort)
7452 (message "Effort filter removed"))))
7454 (defun org-agenda-filter-remove-all ()
7455 "Remove all filters from the current agenda buffer."
7456 (interactive)
7457 (when org-agenda-tag-filter
7458 (org-agenda-filter-show-all-tag))
7459 (when org-agenda-category-filter
7460 (org-agenda-filter-show-all-cat))
7461 (when org-agenda-regexp-filter
7462 (org-agenda-filter-show-all-re))
7463 (when org-agenda-top-headline-filter
7464 (org-agenda-filter-show-all-top-filter))
7465 (when org-agenda-effort-filter
7466 (org-agenda-filter-show-all-effort))
7467 (org-agenda-finalize))
7469 (defun org-agenda-filter-by-tag (arg &optional char exclude)
7470 "Keep only those lines in the agenda buffer that have a specific tag.
7471 The tag is selected with its fast selection letter, as
7472 configured. With a single \\[universal-argument] prefix ARG,
7473 exclude the agenda search. With a double \\[universal-argument]
7474 prefix ARG, filter the literal tag. I.e. don't filter on all its
7475 group members.
7477 A lisp caller can specify CHAR. EXCLUDE means that the new tag should be
7478 used to exclude the search - the interactive user can also press `-' or `+'
7479 to switch between filtering and excluding."
7480 (interactive "P")
7481 (let* ((alist org-tag-alist-for-agenda)
7482 (tag-chars (mapconcat
7483 (lambda (x) (if (and (not (symbolp (car x)))
7484 (cdr x))
7485 (char-to-string (cdr x))
7486 ""))
7487 org-tag-alist-for-agenda ""))
7488 (valid-char-list (append '(?\t ?\r ?/ ?. ?\s ?q)
7489 (string-to-list tag-chars)))
7490 (exclude (or exclude (equal arg '(4))))
7491 (expand (not (equal arg '(16))))
7492 (inhibit-read-only t)
7493 (current org-agenda-tag-filter)
7494 a n tag)
7495 (unless char
7496 (while (not (memq char valid-char-list))
7497 (message
7498 "%s by tag [%s ], [TAB], %s[/]:off, [+/-]:filter/exclude%s, [q]:quit"
7499 (if exclude "Exclude" "Filter") tag-chars
7500 (if org-agenda-auto-exclude-function "[RET], " "")
7501 (if expand "" ", no grouptag expand"))
7502 (setq char (read-char-exclusive))
7503 ;; Excluding or filtering down
7504 (cond ((eq char ?-) (setq exclude t))
7505 ((eq char ?+) (setq exclude nil)))))
7506 (when (eq char ?\t)
7507 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7508 (setq-local org-global-tags-completion-table
7509 (org-global-tags-completion-table)))
7510 (let ((completion-ignore-case t))
7511 (setq tag (completing-read
7512 "Tag: " org-global-tags-completion-table))))
7513 (cond
7514 ((eq char ?\r)
7515 (org-agenda-filter-show-all-tag)
7516 (when org-agenda-auto-exclude-function
7517 (setq org-agenda-tag-filter nil)
7518 (dolist (tag (org-agenda-get-represented-tags))
7519 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7520 (if modifier
7521 (push modifier org-agenda-tag-filter))))
7522 (if (not (null org-agenda-tag-filter))
7523 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))))
7524 ((eq char ?/)
7525 (org-agenda-filter-show-all-tag)
7526 (when (get 'org-agenda-tag-filter :preset-filter)
7527 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand)))
7528 ((eq char ?.)
7529 (setq org-agenda-tag-filter
7530 (mapcar (lambda(tag) (concat "+" tag))
7531 (org-get-at-bol 'tags)))
7532 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7533 ((eq char ?q)) ;If q, abort (even if there is a q-key for a tag...)
7534 ((or (eq char ?\s)
7535 (setq a (rassoc char alist))
7536 (and tag (setq a (cons tag nil))))
7537 (org-agenda-filter-show-all-tag)
7538 (setq tag (car a))
7539 (setq org-agenda-tag-filter
7540 (cons (concat (if exclude "-" "+") tag)
7541 current))
7542 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7543 (t (error "Invalid tag selection character %c" char)))))
7545 (defun org-agenda-get-represented-tags ()
7546 "Get a list of all tags currently represented in the agenda."
7547 (let (p tags)
7548 (save-excursion
7549 (goto-char (point-min))
7550 (while (setq p (next-single-property-change (point) 'tags))
7551 (goto-char p)
7552 (mapc (lambda (x) (add-to-list 'tags x))
7553 (get-text-property (point) 'tags))))
7554 tags))
7557 (defun org-agenda-filter-make-matcher (filter type &optional expand)
7558 "Create the form that tests a line for agenda filter. Optional
7559 argument EXPAND can be used for the TYPE tag and will expand the
7560 tags in the FILTER if any of the tags in FILTER are grouptags."
7561 (let (f f1)
7562 (cond
7563 ;; Tag filter
7564 ((eq type 'tag)
7565 (setq filter
7566 (delete-dups
7567 (append (get 'org-agenda-tag-filter :preset-filter)
7568 filter)))
7569 (dolist (x filter)
7570 (let ((op (string-to-char x)))
7571 (if expand (setq x (org-agenda-filter-expand-tags (list x) t))
7572 (setq x (list x)))
7573 (setq f1 (org-agenda-filter-make-matcher-tag-exp x op))
7574 (push f1 f))))
7575 ;; Category filter
7576 ((eq type 'category)
7577 (setq filter
7578 (delete-dups
7579 (append (get 'org-agenda-category-filter :preset-filter)
7580 filter)))
7581 (dolist (x filter)
7582 (if (equal "-" (substring x 0 1))
7583 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7584 (setq f1 (list 'equal (substring x 1) 'cat)))
7585 (push f1 f)))
7586 ;; Regexp filter
7587 ((eq type 'regexp)
7588 (setq filter
7589 (delete-dups
7590 (append (get 'org-agenda-regexp-filter :preset-filter)
7591 filter)))
7592 (dolist (x filter)
7593 (if (equal "-" (substring x 0 1))
7594 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7595 (setq f1 (list 'string-match (substring x 1) 'txt)))
7596 (push f1 f)))
7597 ;; Effort filter
7598 ((eq type 'effort)
7599 (setq filter
7600 (delete-dups
7601 (append (get 'org-agenda-effort-filter :preset-filter)
7602 filter)))
7603 (dolist (x filter)
7604 (push (org-agenda-filter-effort-form x) f))))
7605 (cons 'and (nreverse f))))
7607 (defun org-agenda-filter-make-matcher-tag-exp (tags op)
7608 "Return a form associated to tag-expression TAGS.
7609 Build a form testing a line for agenda filter for
7610 tag-expressions. OP is an operator of type CHAR that allows the
7611 function to set the right switches in the returned form."
7612 (let (form)
7613 ;; Any of the expressions can match if OP is +, all must match if
7614 ;; the operator is -.
7615 (dolist (x tags (cons (if (eq op ?-) 'and 'or) form))
7616 (let* ((tag (substring x 1))
7617 (f (cond
7618 ((string= "" tag) '(not tags))
7619 ((and (string-match-p "\\`{" tag) (string-match-p "}\\'" tag))
7620 ;; TAG is a regexp.
7621 (list 'org-match-any-p (substring tag 1 -1) 'tags))
7622 (t (list 'member (downcase tag) 'tags)))))
7623 (push (if (eq op ?-) (list 'not f) f) form)))))
7625 (defun org-agenda-filter-effort-form (e)
7626 "Return the form to compare the effort of the current line with what E says.
7627 E looks like \"+<2:25\"."
7628 (let (op)
7629 (setq e (substring e 1))
7630 (setq op (string-to-char e) e (substring e 1))
7631 (setq op (cond ((equal op ?<) '<=)
7632 ((equal op ?>) '>=)
7633 ((equal op ??) op)
7634 (t '=)))
7635 (list 'org-agenda-compare-effort (list 'quote op)
7636 (org-duration-string-to-minutes e))))
7638 (defun org-agenda-compare-effort (op value)
7639 "Compare the effort of the current line with VALUE, using OP.
7640 If the line does not have an effort defined, return nil."
7641 (let ((eff (org-get-at-eol 'effort-minutes 1)))
7642 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 -1))
7643 value)))
7645 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7646 "Expand group tags in FILTER for the agenda.
7647 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7648 (if org-group-tags
7649 (let ((case-fold-search t) rtn)
7650 (mapc
7651 (lambda (f)
7652 (let (f0 dir)
7653 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7654 (setq dir (match-string 1 f) f0 (match-string 2 f))
7655 (setq dir (if no-operator "" "+") f0 f))
7656 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7657 (org-tags-expand f0 t t))
7658 rtn))))
7659 filter)
7660 (reverse rtn))
7661 filter))
7663 (defun org-agenda-filter-apply (filter type &optional expand)
7664 "Set FILTER as the new agenda filter and apply it. Optional
7665 argument EXPAND can be used for the TYPE tag and will expand the
7666 tags in the FILTER if any of the tags in FILTER are grouptags."
7667 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7668 (if org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7669 (let (tags cat txt)
7670 (setq org-agenda-filter-form (org-agenda-filter-make-matcher filter type expand))
7671 ;; Only set `org-agenda-filtered-by-category' to t when a unique
7672 ;; category is used as the filter:
7673 (setq org-agenda-filtered-by-category
7674 (and (eq type 'category)
7675 (not (equal (substring (car filter) 0 1) "-"))))
7676 (org-agenda-set-mode-name)
7677 (save-excursion
7678 (goto-char (point-min))
7679 (while (not (eobp))
7680 (if (org-get-at-bol 'org-marker)
7681 (progn
7682 (setq tags (org-get-at-bol 'tags)
7683 cat (org-get-at-eol 'org-category 1)
7684 txt (org-get-at-bol 'txt))
7685 (if (not (eval org-agenda-filter-form))
7686 (org-agenda-filter-hide-line type))
7687 (beginning-of-line 2))
7688 (beginning-of-line 2))))
7689 (if (get-char-property (point) 'invisible)
7690 (ignore-errors (org-agenda-previous-line)))))
7692 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7693 "Filter by top headline HL."
7694 (org-agenda-set-mode-name)
7695 (save-excursion
7696 (goto-char (point-min))
7697 (while (not (eobp))
7698 (let* ((pos (org-get-at-bol 'org-hd-marker))
7699 (tophl (and pos (org-find-top-headline pos))))
7700 (if (and tophl (funcall (if negative 'identity 'not)
7701 (string= hl tophl)))
7702 (org-agenda-filter-hide-line 'top-headline)))
7703 (beginning-of-line 2)))
7704 (if (get-char-property (point) 'invisible)
7705 (org-agenda-previous-line))
7706 (setq org-agenda-top-headline-filter hl
7707 org-agenda-filtered-by-top-headline t))
7709 (defun org-agenda-filter-hide-line (type)
7710 "Hide lines with TYPE in the agenda buffer."
7711 (let* ((b (max (point-min) (1- (point-at-bol))))
7712 (e (point-at-eol)))
7713 (let ((inhibit-read-only t))
7714 (add-text-properties
7715 b e `(invisible org-filtered org-filter-type ,type)))))
7717 (defun org-agenda-remove-filter (type)
7718 (interactive)
7719 "Remove filter of type TYPE from the agenda buffer."
7720 (save-excursion
7721 (goto-char (point-min))
7722 (let ((inhibit-read-only t) pos)
7723 (while (setq pos (text-property-any (point) (point-max) 'org-filter-type type))
7724 (goto-char pos)
7725 (remove-text-properties
7726 (point) (next-single-property-change (point) 'org-filter-type)
7727 `(invisible org-filtered org-filter-type ,type))))
7728 (set (intern (format "org-agenda-%s-filter" (intern-soft type))) nil)
7729 (setq org-agenda-filter-form nil)
7730 (org-agenda-set-mode-name)
7731 (org-agenda-finalize)))
7733 (defun org-agenda-filter-show-all-tag nil
7734 (org-agenda-remove-filter 'tag))
7735 (defun org-agenda-filter-show-all-re nil
7736 (org-agenda-remove-filter 'regexp))
7737 (defun org-agenda-filter-show-all-effort nil
7738 (org-agenda-remove-filter 'effort))
7739 (defun org-agenda-filter-show-all-cat nil
7740 (org-agenda-remove-filter 'category))
7741 (defun org-agenda-filter-show-all-top-filter nil
7742 (org-agenda-remove-filter 'top-headline))
7744 (defun org-agenda-manipulate-query-add ()
7745 "Manipulate the query by adding a search term with positive selection.
7746 Positive selection means the term must be matched for selection of an entry."
7747 (interactive)
7748 (org-agenda-manipulate-query ?\[))
7749 (defun org-agenda-manipulate-query-subtract ()
7750 "Manipulate the query by adding a search term with negative selection.
7751 Negative selection means term must not be matched for selection of an entry."
7752 (interactive)
7753 (org-agenda-manipulate-query ?\]))
7754 (defun org-agenda-manipulate-query-add-re ()
7755 "Manipulate the query by adding a search regexp with positive selection.
7756 Positive selection means the regexp must match for selection of an entry."
7757 (interactive)
7758 (org-agenda-manipulate-query ?\{))
7759 (defun org-agenda-manipulate-query-subtract-re ()
7760 "Manipulate the query by adding a search regexp with negative selection.
7761 Negative selection means regexp must not match for selection of an entry."
7762 (interactive)
7763 (org-agenda-manipulate-query ?\}))
7764 (defun org-agenda-manipulate-query (char)
7765 (cond
7766 ((memq org-agenda-type '(timeline agenda))
7767 (let ((org-agenda-include-inactive-timestamps t))
7768 (org-agenda-redo))
7769 (message "Display now includes inactive timestamps as well"))
7770 ((eq org-agenda-type 'search)
7771 (org-add-to-string
7772 'org-agenda-query-string
7773 (if org-agenda-last-search-view-search-was-boolean
7774 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7775 (?\{ . " +{}") (?\} . " -{}"))))
7776 " "))
7777 (setq org-agenda-redo-command
7778 (list 'org-search-view
7779 (car (get-text-property (min (1- (point-max)) (point))
7780 'org-last-args))
7781 org-agenda-query-string
7782 (+ (length org-agenda-query-string)
7783 (if (member char '(?\{ ?\})) 0 1))))
7784 (set-register org-agenda-query-register org-agenda-query-string)
7785 (let ((org-agenda-overriding-arguments
7786 (cdr org-agenda-redo-command)))
7787 (org-agenda-redo)))
7788 (t (error "Cannot manipulate query for %s-type agenda buffers"
7789 org-agenda-type))))
7791 (defun org-add-to-string (var string)
7792 (set var (concat (symbol-value var) string)))
7794 (defun org-agenda-goto-date (span)
7795 "Jump to DATE in agenda."
7796 (interactive "P")
7797 (let* ((org-read-date-prefer-future
7798 (eval org-agenda-jump-prefer-future))
7799 (date (org-read-date))
7800 (day (time-to-days (org-time-string-to-time date)))
7801 (org-agenda-sticky-orig org-agenda-sticky)
7802 (org-agenda-buffer-tmp-name (buffer-name))
7803 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7804 (0-arg (or current-prefix-arg (car args)))
7805 (2-arg (nth 2 args))
7806 (with-hour-p (nth 4 org-agenda-redo-command))
7807 (newcmd (list 'org-agenda-list 0-arg date
7808 (org-agenda-span-to-ndays
7809 2-arg (org-time-string-to-absolute date))
7810 with-hour-p))
7811 (newargs (cdr newcmd))
7812 (inhibit-read-only t)
7813 org-agenda-sticky)
7814 (if (not (org-agenda-check-type t 'agenda))
7815 (error "Not available in non-agenda views")
7816 (add-text-properties (point-min) (point-max)
7817 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7818 (org-agenda-redo)
7819 (goto-char (point-min))
7820 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7821 (save-excursion (move-beginning-of-line 2) (eobp))))
7822 (move-beginning-of-line 2))
7823 (setq org-agenda-sticky org-agenda-sticky-orig
7824 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7826 (defun org-agenda-goto-today ()
7827 "Go to today."
7828 (interactive)
7829 (org-agenda-check-type t 'timeline 'agenda)
7830 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7831 (curspan (nth 2 args))
7832 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7833 (cond
7834 (tdpos (goto-char tdpos))
7835 ((eq org-agenda-type 'agenda)
7836 (let* ((sd (org-agenda-compute-starting-span
7837 (org-today) (or curspan org-agenda-span)))
7838 (org-agenda-overriding-arguments args))
7839 (setf (nth 1 org-agenda-overriding-arguments) sd)
7840 (org-agenda-redo)
7841 (org-agenda-find-same-or-today-or-agenda)))
7842 (t (error "Cannot find today")))))
7844 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7845 (goto-char
7846 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7847 (text-property-any (point-min) (point-max) 'org-today t)
7848 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7849 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7850 (org-agenda-backward-block))
7851 (point-min))))
7853 (defun org-agenda-backward-block ()
7854 "Move backward by one agenda block."
7855 (interactive)
7856 (org-agenda-forward-block 'backward))
7858 (defun org-agenda-forward-block (&optional backward)
7859 "Move forward by one agenda block.
7860 When optional argument BACKWARD is set, go backward"
7861 (interactive)
7862 (cond ((not (derived-mode-p 'org-agenda-mode))
7863 (user-error
7864 "Cannot execute this command outside of org-agenda-mode buffers"))
7865 ((looking-at (if backward "\\`" "\\'"))
7866 (message "Already at the %s block" (if backward "first" "last")))
7867 (t (let ((pos (prog1 (point)
7868 (ignore-errors (if backward (backward-char 1)
7869 (move-end-of-line 1)))))
7870 (f (if backward
7871 'previous-single-property-change
7872 'next-single-property-change))
7873 moved dest)
7874 (while (and (setq dest (funcall
7875 f (point) 'org-agenda-structural-header))
7876 (not (get-text-property
7877 (point) 'org-agenda-structural-header)))
7878 (setq moved t)
7879 (goto-char dest))
7880 (if moved (move-beginning-of-line 1)
7881 (goto-char (if backward (point-min) (point-max)))
7882 (move-beginning-of-line 1)
7883 (message "No %s block" (if backward "previous" "further")))))))
7885 (defun org-agenda-later (arg)
7886 "Go forward in time by the current span.
7887 With prefix ARG, go forward that many times the current span."
7888 (interactive "p")
7889 (org-agenda-check-type t 'agenda)
7890 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7891 (span (or (nth 2 args) org-agenda-current-span))
7892 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7893 (greg (calendar-gregorian-from-absolute sd))
7894 (cnt (org-get-at-bol 'org-day-cnt))
7895 greg2)
7896 (cond
7897 ((numberp span)
7898 (setq sd (+ (* span arg) sd)))
7899 ((eq span 'day)
7900 (setq sd (+ arg sd)))
7901 ((eq span 'week)
7902 (setq sd (+ (* 7 arg) sd)))
7903 ((eq span 'fortnight)
7904 (setq sd (+ (* 14 arg) sd)))
7905 ((eq span 'month)
7906 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7907 sd (calendar-absolute-from-gregorian greg2))
7908 (setcar greg2 (1+ (car greg2))))
7909 ((eq span 'year)
7910 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7911 sd (calendar-absolute-from-gregorian greg2))
7912 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7914 (setq sd (+ (* span arg) sd))))
7915 (let ((org-agenda-overriding-cmd
7916 ;; `cmd' may have been set by `org-agenda-run-series' which
7917 ;; uses `org-agenda-overriding-cmd' to decide whether
7918 ;; overriding is allowed for `cmd'
7919 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7920 (org-agenda-overriding-arguments
7921 (list (car args) sd span)))
7922 (org-agenda-redo)
7923 (org-agenda-find-same-or-today-or-agenda cnt))))
7925 (defun org-agenda-earlier (arg)
7926 "Go backward in time by the current span.
7927 With prefix ARG, go backward that many times the current span."
7928 (interactive "p")
7929 (org-agenda-later (- arg)))
7931 (defun org-agenda-view-mode-dispatch ()
7932 "Call one of the view mode commands."
7933 (interactive)
7934 (message "View: [d]ay [w]eek for[t]night [m]onth [y]ear [SPC]reset [q]uit/abort
7935 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7936 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7937 (pcase (read-char-exclusive)
7938 (?\ (call-interactively 'org-agenda-reset-view))
7939 (?d (call-interactively 'org-agenda-day-view))
7940 (?w (call-interactively 'org-agenda-week-view))
7941 (?t (call-interactively 'org-agenda-fortnight-view))
7942 (?m (call-interactively 'org-agenda-month-view))
7943 (?y (call-interactively 'org-agenda-year-view))
7944 (?l (call-interactively 'org-agenda-log-mode))
7945 (?L (org-agenda-log-mode '(4)))
7946 (?c (org-agenda-log-mode 'clockcheck))
7947 ((or ?F ?f) (call-interactively 'org-agenda-follow-mode))
7948 (?a (call-interactively 'org-agenda-archives-mode))
7949 (?A (org-agenda-archives-mode 'files))
7950 ((or ?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7951 ((or ?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7952 (?G (call-interactively 'org-agenda-toggle-time-grid))
7953 (?D (call-interactively 'org-agenda-toggle-diary))
7954 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7955 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7956 (org-agenda-check-type t 'timeline 'agenda)
7957 (org-agenda-redo))
7958 (message "Display now includes inactive timestamps as well"))
7959 (?q (message "Abort"))
7960 (key (user-error "Invalid key: %s" key))))
7962 (defun org-agenda-reset-view ()
7963 "Switch to default view for agenda."
7964 (interactive)
7965 (org-agenda-change-time-span org-agenda-span))
7966 (defun org-agenda-day-view (&optional day-of-month)
7967 "Switch to daily view for agenda.
7968 With argument DAY-OF-MONTH, switch to that day of the month."
7969 (interactive "P")
7970 (org-agenda-change-time-span 'day day-of-month))
7971 (defun org-agenda-week-view (&optional iso-week)
7972 "Switch to daily view for agenda.
7973 With argument ISO-WEEK, switch to the corresponding ISO week.
7974 If ISO-WEEK has more then 2 digits, only the last two encode the
7975 week. Any digits before this encode a year. So 200712 means
7976 week 12 of year 2007. Years in the range 1938-2037 can also be
7977 written as 2-digit years."
7978 (interactive "P")
7979 (org-agenda-change-time-span 'week iso-week))
7980 (defun org-agenda-fortnight-view (&optional iso-week)
7981 "Switch to daily view for agenda.
7982 With argument ISO-WEEK, switch to the corresponding ISO week.
7983 If ISO-WEEK has more then 2 digits, only the last two encode the
7984 week. Any digits before this encode a year. So 200712 means
7985 week 12 of year 2007. Years in the range 1938-2037 can also be
7986 written as 2-digit years."
7987 (interactive "P")
7988 (org-agenda-change-time-span 'fortnight iso-week))
7989 (defun org-agenda-month-view (&optional month)
7990 "Switch to monthly view for agenda.
7991 With argument MONTH, switch to that month."
7992 (interactive "P")
7993 (org-agenda-change-time-span 'month month))
7994 (defun org-agenda-year-view (&optional year)
7995 "Switch to yearly view for agenda.
7996 With argument YEAR, switch to that year.
7997 If MONTH has more then 2 digits, only the last two encode the
7998 month. Any digits before this encode a year. So 200712 means
7999 December year 2007. Years in the range 1938-2037 can also be
8000 written as 2-digit years."
8001 (interactive "P")
8002 (when year
8003 (setq year (org-small-year-to-year year)))
8004 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
8005 (org-agenda-change-time-span 'year year)
8006 (error "Abort")))
8008 (defun org-agenda-change-time-span (span &optional n)
8009 "Change the agenda view to SPAN.
8010 SPAN may be `day', `week', `fortnight', `month', `year'."
8011 (org-agenda-check-type t 'agenda)
8012 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
8013 (curspan (nth 2 args)))
8014 (if (and (not n) (equal curspan span))
8015 (error "Viewing span is already \"%s\"" span))
8016 (let* ((sd (or (org-get-at-bol 'day)
8017 (nth 1 args)
8018 org-starting-day))
8019 (sd (org-agenda-compute-starting-span sd span n))
8020 (org-agenda-overriding-cmd
8021 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
8022 (org-agenda-overriding-arguments
8023 (list (car args) sd span)))
8024 (org-agenda-redo)
8025 (org-agenda-find-same-or-today-or-agenda))
8026 (org-agenda-set-mode-name)
8027 (message "Switched to %s view" span)))
8029 (defun org-agenda-compute-starting-span (sd span &optional n)
8030 "Compute starting date for agenda.
8031 SPAN may be `day', `week', `fortnight', `month', `year'. The return value
8032 is a cons cell with the starting date and the number of days,
8033 so that the date SD will be in that range."
8034 (let* ((greg (calendar-gregorian-from-absolute sd))
8035 (dg (nth 1 greg))
8036 (mg (car greg))
8037 (yg (nth 2 greg)))
8038 (cond
8039 ((eq span 'day)
8040 (when n
8041 (setq sd (+ (calendar-absolute-from-gregorian
8042 (list mg 1 yg))
8043 n -1))))
8044 ((or (eq span 'week) (eq span 'fortnight))
8045 (let* ((nt (calendar-day-of-week
8046 (calendar-gregorian-from-absolute sd)))
8047 (d (if org-agenda-start-on-weekday
8048 (- nt org-agenda-start-on-weekday)
8051 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
8052 (when n
8053 (require 'cal-iso)
8054 (when (> n 99)
8055 (setq y1 (org-small-year-to-year (/ n 100))
8056 n (mod n 100)))
8057 (setq sd
8058 (calendar-iso-to-absolute
8059 (list n 1
8060 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
8061 ((eq span 'month)
8062 (let (y1)
8063 (when (and n (> n 99))
8064 (setq y1 (org-small-year-to-year (/ n 100))
8065 n (mod n 100)))
8066 (setq sd (calendar-absolute-from-gregorian
8067 (list (or n mg) 1 (or y1 yg))))))
8068 ((eq span 'year)
8069 (setq sd (calendar-absolute-from-gregorian
8070 (list 1 1 (or n yg))))))
8071 sd))
8073 (defun org-agenda-next-date-line (&optional arg)
8074 "Jump to the next line indicating a date in agenda buffer."
8075 (interactive "p")
8076 (org-agenda-check-type t 'agenda 'timeline)
8077 (beginning-of-line 1)
8078 ;; This does not work if user makes date format that starts with a blank
8079 (if (looking-at "^\\S-") (forward-char 1))
8080 (if (not (re-search-forward "^\\S-" nil t arg))
8081 (progn
8082 (backward-char 1)
8083 (error "No next date after this line in this buffer")))
8084 (goto-char (match-beginning 0)))
8086 (defun org-agenda-previous-date-line (&optional arg)
8087 "Jump to the previous line indicating a date in agenda buffer."
8088 (interactive "p")
8089 (org-agenda-check-type t 'agenda 'timeline)
8090 (beginning-of-line 1)
8091 (if (not (re-search-backward "^\\S-" nil t arg))
8092 (error "No previous date before this line in this buffer")))
8094 ;; Initialize the highlight
8095 (defvar org-hl (make-overlay 1 1))
8096 (overlay-put org-hl 'face 'highlight)
8098 (defun org-highlight (begin end &optional buffer)
8099 "Highlight a region with overlay."
8100 (move-overlay org-hl begin end (or buffer (current-buffer))))
8102 (defun org-unhighlight ()
8103 "Detach overlay INDEX."
8104 (delete-overlay org-hl))
8106 (defun org-unhighlight-once ()
8107 "Remove the highlight from its position, and this function from the hook."
8108 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8109 (org-unhighlight))
8111 (defvar org-agenda-pre-follow-window-conf nil)
8112 (defun org-agenda-follow-mode ()
8113 "Toggle follow mode in an agenda buffer."
8114 (interactive)
8115 (unless org-agenda-follow-mode
8116 (setq org-agenda-pre-follow-window-conf
8117 (current-window-configuration)))
8118 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8119 (unless org-agenda-follow-mode
8120 (set-window-configuration org-agenda-pre-follow-window-conf))
8121 (org-agenda-set-mode-name)
8122 (org-agenda-do-context-action)
8123 (message "Follow mode is %s"
8124 (if org-agenda-follow-mode "on" "off")))
8126 (defun org-agenda-entry-text-mode (&optional arg)
8127 "Toggle entry text mode in an agenda buffer."
8128 (interactive "P")
8129 (if (or org-agenda-tag-filter
8130 org-agenda-category-filter
8131 org-agenda-regexp-filter
8132 org-agenda-top-headline-filter)
8133 (user-error "Can't show entry text in filtered views")
8134 (setq org-agenda-entry-text-mode (or (integerp arg)
8135 (not org-agenda-entry-text-mode)))
8136 (org-agenda-entry-text-hide)
8137 (and org-agenda-entry-text-mode
8138 (let ((org-agenda-entry-text-maxlines
8139 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8140 (org-agenda-entry-text-show)))
8141 (org-agenda-set-mode-name)
8142 (message "Entry text mode is %s%s"
8143 (if org-agenda-entry-text-mode "on" "off")
8144 (if (not org-agenda-entry-text-mode) ""
8145 (format " (maximum number of lines is %d)"
8146 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8148 (defun org-agenda-clockreport-mode ()
8149 "Toggle clocktable mode in an agenda buffer."
8150 (interactive)
8151 (org-agenda-check-type t 'agenda)
8152 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
8153 (setq org-agenda-start-with-clockreport-mode org-agenda-clockreport-mode)
8154 (org-agenda-set-mode-name)
8155 (org-agenda-redo)
8156 (message "Clocktable mode is %s"
8157 (if org-agenda-clockreport-mode "on" "off")))
8159 (defun org-agenda-log-mode (&optional special)
8160 "Toggle log mode in an agenda buffer.
8161 With argument SPECIAL, show all possible log items, not only the ones
8162 configured in `org-agenda-log-mode-items'.
8163 With a double \\[universal-argument] prefix arg, show *only* \
8164 log items, nothing else."
8165 (interactive "P")
8166 (org-agenda-check-type t 'agenda 'timeline)
8167 (setq org-agenda-show-log
8168 (cond
8169 ((equal special '(16)) 'only)
8170 ((eq special 'clockcheck)
8171 (if (eq org-agenda-show-log 'clockcheck)
8172 nil 'clockcheck))
8173 (special '(closed clock state))
8174 (t (not org-agenda-show-log))))
8175 (setq org-agenda-start-with-log-mode org-agenda-show-log)
8176 (org-agenda-set-mode-name)
8177 (org-agenda-redo)
8178 (message "Log mode is %s"
8179 (if org-agenda-show-log "on" "off")))
8181 (defun org-agenda-archives-mode (&optional with-files)
8182 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8183 When called with a prefix argument, include all archive files as well."
8184 (interactive "P")
8185 (setq org-agenda-archives-mode
8186 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8187 (org-agenda-set-mode-name)
8188 (org-agenda-redo)
8189 (message
8190 "%s"
8191 (cond
8192 ((eq org-agenda-archives-mode nil)
8193 "No archives are included")
8194 ((eq org-agenda-archives-mode 'trees)
8195 (format "Trees with :%s: tag are included" org-archive-tag))
8196 ((eq org-agenda-archives-mode t)
8197 (format "Trees with :%s: tag and all active archive files are included"
8198 org-archive-tag)))))
8200 (defun org-agenda-toggle-diary ()
8201 "Toggle diary inclusion in an agenda buffer."
8202 (interactive)
8203 (org-agenda-check-type t 'agenda)
8204 (setq org-agenda-include-diary (not org-agenda-include-diary))
8205 (org-agenda-redo)
8206 (org-agenda-set-mode-name)
8207 (message "Diary inclusion turned %s"
8208 (if org-agenda-include-diary "on" "off")))
8210 (defun org-agenda-toggle-deadlines ()
8211 "Toggle inclusion of entries with a deadline in an agenda buffer."
8212 (interactive)
8213 (org-agenda-check-type t 'agenda)
8214 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8215 (org-agenda-redo)
8216 (org-agenda-set-mode-name)
8217 (message "Deadlines inclusion turned %s"
8218 (if org-agenda-include-deadlines "on" "off")))
8220 (defun org-agenda-toggle-time-grid ()
8221 "Toggle time grid in an agenda buffer."
8222 (interactive)
8223 (org-agenda-check-type t 'agenda)
8224 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8225 (org-agenda-redo)
8226 (org-agenda-set-mode-name)
8227 (message "Time-grid turned %s"
8228 (if org-agenda-use-time-grid "on" "off")))
8230 (defun org-agenda-set-mode-name ()
8231 "Set the mode name to indicate all the small mode settings."
8232 (setq mode-name
8233 (list "Org-Agenda"
8234 (if (get 'org-agenda-files 'org-restrict) " []" "")
8236 '(:eval (org-agenda-span-name org-agenda-current-span))
8237 (if org-agenda-follow-mode " Follow" "")
8238 (if org-agenda-entry-text-mode " ETxt" "")
8239 (if org-agenda-include-diary " Diary" "")
8240 (if org-agenda-include-deadlines " Ddl" "")
8241 (if org-agenda-use-time-grid " Grid" "")
8242 (if (and (boundp 'org-habit-show-habits)
8243 org-habit-show-habits) " Habit" "")
8244 (cond
8245 ((consp org-agenda-show-log) " LogAll")
8246 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8247 (org-agenda-show-log " Log")
8248 (t ""))
8249 (if (or org-agenda-category-filter
8250 (get 'org-agenda-category-filter :preset-filter))
8251 '(:eval (propertize
8252 (concat " <"
8253 (mapconcat
8254 'identity
8255 (append
8256 (get 'org-agenda-category-filter :preset-filter)
8257 org-agenda-category-filter)
8259 ">")
8260 'face 'org-agenda-filter-category
8261 'help-echo "Category used in filtering")) "")
8262 (if (or org-agenda-tag-filter
8263 (get 'org-agenda-tag-filter :preset-filter))
8264 '(:eval (propertize
8265 (concat " {"
8266 (mapconcat
8267 'identity
8268 (append
8269 (get 'org-agenda-tag-filter :preset-filter)
8270 org-agenda-tag-filter)
8272 "}")
8273 'face 'org-agenda-filter-tags
8274 'help-echo "Tags used in filtering")) "")
8275 (if (or org-agenda-effort-filter
8276 (get 'org-agenda-effort-filter :preset-filter))
8277 '(:eval (propertize
8278 (concat " {"
8279 (mapconcat
8280 'identity
8281 (append
8282 (get 'org-agenda-effort-filter :preset-filter)
8283 org-agenda-effort-filter)
8285 "}")
8286 'face 'org-agenda-filter-effort
8287 'help-echo "Effort conditions used in filtering")) "")
8288 (if (or org-agenda-regexp-filter
8289 (get 'org-agenda-regexp-filter :preset-filter))
8290 '(:eval (propertize
8291 (concat " ["
8292 (mapconcat
8293 'identity
8294 (append
8295 (get 'org-agenda-regexp-filter :preset-filter)
8296 org-agenda-regexp-filter)
8298 "]")
8299 'face 'org-agenda-filter-regexp
8300 'help-echo "Regexp used in filtering")) "")
8301 (if org-agenda-archives-mode
8302 (if (eq org-agenda-archives-mode t)
8303 " Archives"
8304 (format " :%s:" org-archive-tag))
8306 (if org-agenda-clockreport-mode " Clock" "")))
8307 (force-mode-line-update))
8309 (defun org-agenda-update-agenda-type ()
8310 "Update the agenda type after each command."
8311 (setq org-agenda-type
8312 (or (get-text-property (point) 'org-agenda-type)
8313 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8315 (defun org-agenda-next-line ()
8316 "Move cursor to the next line, and show if follow mode is active."
8317 (interactive)
8318 (call-interactively 'next-line)
8319 (org-agenda-do-context-action))
8321 (defun org-agenda-previous-line ()
8322 "Move cursor to the previous line, and show if follow-mode is active."
8323 (interactive)
8324 (call-interactively 'previous-line)
8325 (org-agenda-do-context-action))
8327 (defun org-agenda-next-item (n)
8328 "Move cursor to next agenda item."
8329 (interactive "p")
8330 (let ((col (current-column)))
8331 (dotimes (c n)
8332 (when (next-single-property-change (point-at-eol) 'org-marker)
8333 (move-end-of-line 1)
8334 (goto-char (next-single-property-change (point) 'org-marker))))
8335 (org-move-to-column col))
8336 (org-agenda-do-context-action))
8338 (defun org-agenda-previous-item (n)
8339 "Move cursor to next agenda item."
8340 (interactive "p")
8341 (dotimes (c n)
8342 (let ((col (current-column))
8343 (goto (save-excursion
8344 (move-end-of-line 0)
8345 (previous-single-property-change (point) 'org-marker))))
8346 (if goto (goto-char goto))
8347 (org-move-to-column col)))
8348 (org-agenda-do-context-action))
8350 (defun org-agenda-do-context-action ()
8351 "Show outline path and, maybe, follow mode window."
8352 (let ((m (org-get-at-bol 'org-marker)))
8353 (when (and (markerp m) (marker-buffer m))
8354 (and org-agenda-follow-mode
8355 (if org-agenda-follow-indirect
8356 (org-agenda-tree-to-indirect-buffer nil)
8357 (org-agenda-show)))
8358 (and org-agenda-show-outline-path
8359 (org-with-point-at m (org-display-outline-path t))))))
8361 (defun org-agenda-show-tags ()
8362 "Show the tags applicable to the current item."
8363 (interactive)
8364 (let* ((tags (org-get-at-bol 'tags)))
8365 (if tags
8366 (message "Tags are :%s:"
8367 (org-no-properties (mapconcat 'identity tags ":")))
8368 (message "No tags associated with this line"))))
8370 (defun org-agenda-goto (&optional highlight)
8371 "Go to the entry at point in the corresponding Org file."
8372 (interactive)
8373 (let* ((marker (or (org-get-at-bol 'org-marker)
8374 (org-agenda-error)))
8375 (buffer (marker-buffer marker))
8376 (pos (marker-position marker)))
8377 (switch-to-buffer-other-window buffer)
8378 (widen)
8379 (push-mark)
8380 (goto-char pos)
8381 (when (derived-mode-p 'org-mode)
8382 (org-show-context 'agenda)
8383 (recenter (/ (window-height) 2))
8384 (org-back-to-heading t)
8385 (if (re-search-forward org-complex-heading-regexp nil t)
8386 (goto-char (match-beginning 4))))
8387 (run-hooks 'org-agenda-after-show-hook)
8388 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8390 (defvar org-agenda-after-show-hook nil
8391 "Normal hook run after an item has been shown from the agenda.
8392 Point is in the buffer where the item originated.")
8394 (defun org-agenda-kill ()
8395 "Kill the entry or subtree belonging to the current agenda entry."
8396 (interactive)
8397 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8398 (let* ((bufname-orig (buffer-name))
8399 (marker (or (org-get-at-bol 'org-marker)
8400 (org-agenda-error)))
8401 (buffer (marker-buffer marker))
8402 (pos (marker-position marker))
8403 (type (org-get-at-bol 'type))
8404 dbeg dend (n 0) conf)
8405 (org-with-remote-undo buffer
8406 (with-current-buffer buffer
8407 (save-excursion
8408 (goto-char pos)
8409 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8410 (setq dbeg (progn (org-back-to-heading t) (point))
8411 dend (org-end-of-subtree t t))
8412 (setq dbeg (point-at-bol)
8413 dend (min (point-max) (1+ (point-at-eol)))))
8414 (goto-char dbeg)
8415 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8416 (setq conf (or (eq t org-agenda-confirm-kill)
8417 (and (numberp org-agenda-confirm-kill)
8418 (> n org-agenda-confirm-kill))))
8419 (and conf
8420 (not (y-or-n-p
8421 (format "Delete entry with %d lines in buffer \"%s\"? "
8422 n (buffer-name buffer))))
8423 (error "Abort"))
8424 (let ((org-agenda-buffer-name bufname-orig))
8425 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8426 (with-current-buffer buffer (delete-region dbeg dend))
8427 (message "Agenda item and source killed"))))
8429 (defvar org-archive-default-command) ; defined in org-archive.el
8430 (defun org-agenda-archive-default ()
8431 "Archive the entry or subtree belonging to the current agenda entry."
8432 (interactive)
8433 (require 'org-archive)
8434 (org-agenda-archive-with org-archive-default-command))
8436 (defun org-agenda-archive-default-with-confirmation ()
8437 "Archive the entry or subtree belonging to the current agenda entry."
8438 (interactive)
8439 (require 'org-archive)
8440 (org-agenda-archive-with org-archive-default-command 'confirm))
8442 (defun org-agenda-archive ()
8443 "Archive the entry or subtree belonging to the current agenda entry."
8444 (interactive)
8445 (org-agenda-archive-with 'org-archive-subtree))
8447 (defun org-agenda-archive-to-archive-sibling ()
8448 "Move the entry to the archive sibling."
8449 (interactive)
8450 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8452 (defun org-agenda-archive-with (cmd &optional confirm)
8453 "Move the entry to the archive sibling."
8454 (interactive)
8455 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8456 (let* ((bufname-orig (buffer-name))
8457 (marker (or (org-get-at-bol 'org-marker)
8458 (org-agenda-error)))
8459 (buffer (marker-buffer marker))
8460 (pos (marker-position marker)))
8461 (org-with-remote-undo buffer
8462 (with-current-buffer buffer
8463 (if (derived-mode-p 'org-mode)
8464 (if (and confirm
8465 (not (y-or-n-p "Archive this subtree or entry? ")))
8466 (error "Abort")
8467 (save-window-excursion
8468 (goto-char pos)
8469 (let ((org-agenda-buffer-name bufname-orig))
8470 (org-remove-subtree-entries-from-agenda))
8471 (org-back-to-heading t)
8472 (funcall cmd)))
8473 (error "Archiving works only in Org files"))))))
8475 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8476 "Remove all lines in the agenda that correspond to a given subtree.
8477 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8478 If this information is not given, the function uses the tree at point."
8479 (let ((buf (or buf (current-buffer))) m p)
8480 (save-excursion
8481 (unless (and beg end)
8482 (org-back-to-heading t)
8483 (setq beg (point))
8484 (org-end-of-subtree t)
8485 (setq end (point)))
8486 (set-buffer (get-buffer org-agenda-buffer-name))
8487 (save-excursion
8488 (goto-char (point-max))
8489 (beginning-of-line 1)
8490 (while (not (bobp))
8491 (when (and (setq m (org-get-at-bol 'org-marker))
8492 (equal buf (marker-buffer m))
8493 (setq p (marker-position m))
8494 (>= p beg)
8495 (< p end))
8496 (let ((inhibit-read-only t))
8497 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8498 (beginning-of-line 0))))))
8500 (defun org-agenda-refile (&optional goto rfloc no-update)
8501 "Refile the item at point.
8503 When GOTO is 0 or \\='(64) or a triple \\[universal-argument] prefix argument,
8504 clear the refile cache.
8505 When GOTO is \\='(16) or a double \\[universal-argument] prefix argument,
8506 go to the location of the last refiled item.
8507 RFLOC can be a refile location obtained in a different way.
8508 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8509 (interactive "P")
8510 (cond
8511 ((member goto '(0 (64)))
8512 (org-refile-cache-clear))
8513 ((equal goto '(16))
8514 (org-refile-goto-last-stored))
8516 (let* ((buffer-orig (buffer-name))
8517 (marker (or (org-get-at-bol 'org-hd-marker)
8518 (org-agenda-error)))
8519 (buffer (marker-buffer marker))
8520 (pos (marker-position marker))
8521 (rfloc (or rfloc
8522 (org-refile-get-location
8523 (if goto "Goto" "Refile to") buffer
8524 org-refile-allow-creating-parent-nodes))))
8525 (with-current-buffer buffer
8526 (org-with-wide-buffer
8527 (goto-char marker)
8528 (let ((org-agenda-buffer-name buffer-orig))
8529 (org-remove-subtree-entries-from-agenda))
8530 (org-refile goto buffer rfloc))))
8531 (unless no-update (org-agenda-redo)))))
8533 (defun org-agenda-open-link (&optional arg)
8534 "Open the link(s) in the current entry, if any.
8535 This looks for a link in the displayed line in the agenda.
8536 It also looks at the text of the entry itself."
8537 (interactive "P")
8538 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8539 (org-get-at-bol 'org-marker)))
8540 (buffer (and marker (marker-buffer marker)))
8541 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8542 (lkall (and buffer (org-offer-links-in-entry
8543 buffer marker arg prefix)))
8544 (lk0 (car lkall))
8545 (lk (if (stringp lk0) (list lk0) lk0))
8546 (lkend (cdr lkall))
8547 trg)
8548 (cond
8549 ((and buffer lk)
8550 (mapcar (lambda(l)
8551 (with-current-buffer buffer
8552 (setq trg (and (string-match org-bracket-link-regexp l)
8553 (match-string 1 l)))
8554 (if (or (not trg) (string-match org-any-link-re trg))
8555 (org-with-wide-buffer
8556 (goto-char marker)
8557 (when (search-forward l nil lkend)
8558 (goto-char (match-beginning 0))
8559 (org-open-at-point)))
8560 ;; This is an internal link, widen the buffer
8561 (switch-to-buffer-other-window buffer)
8562 (widen)
8563 (goto-char marker)
8564 (when (search-forward l nil lkend)
8565 (goto-char (match-beginning 0))
8566 (org-open-at-point)))))
8567 lk))
8568 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8569 (save-excursion
8570 (beginning-of-line 1)
8571 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8572 (org-open-link-from-string (match-string 1)))
8573 (t (message "No link to open here")))))
8575 (defun org-agenda-copy-local-variable (var)
8576 "Get a variable from a referenced buffer and install it here."
8577 (let ((m (org-get-at-bol 'org-marker)))
8578 (when (and m (buffer-live-p (marker-buffer m)))
8579 (set (make-local-variable var)
8580 (with-current-buffer (marker-buffer m)
8581 (symbol-value var))))))
8583 (defun org-agenda-switch-to (&optional delete-other-windows)
8584 "Go to the Org mode file which contains the item at point.
8585 When optional argument DELETE-OTHER-WINDOWS is non-nil, the
8586 displayed Org file fills the frame."
8587 (interactive)
8588 (if (and org-return-follows-link
8589 (not (org-get-at-bol 'org-marker))
8590 (org-in-regexp org-bracket-link-regexp))
8591 (org-open-link-from-string (match-string 0))
8592 (let* ((marker (or (org-get-at-bol 'org-marker)
8593 (org-agenda-error)))
8594 (buffer (marker-buffer marker))
8595 (pos (marker-position marker)))
8596 (unless buffer (user-error "Trying to switch to non-existent buffer"))
8597 (pop-to-buffer-same-window buffer)
8598 (when delete-other-windows (delete-other-windows))
8599 (widen)
8600 (goto-char pos)
8601 (when (derived-mode-p 'org-mode)
8602 (org-show-context 'agenda)
8603 (run-hooks 'org-agenda-after-show-hook)))))
8605 (defun org-agenda-goto-mouse (ev)
8606 "Go to the Org file which contains the item at the mouse click."
8607 (interactive "e")
8608 (mouse-set-point ev)
8609 (org-agenda-goto))
8611 (defun org-agenda-show (&optional full-entry)
8612 "Display the Org file which contains the item at point.
8613 With prefix argument FULL-ENTRY, make the entire entry visible
8614 if it was hidden in the outline."
8615 (interactive "P")
8616 (let ((win (selected-window)))
8617 (org-agenda-goto t)
8618 (when full-entry (org-show-entry))
8619 (select-window win)))
8621 (defvar org-agenda-show-window nil)
8622 (defun org-agenda-show-and-scroll-up (&optional arg)
8623 "Display the Org file which contains the item at point.
8624 When called repeatedly, scroll the window that is displaying the buffer.
8625 With a \\[universal-argument] prefix, use `org-show-entry' instead of
8626 `show-subtree' to display the item, so that drawers and logbooks stay
8627 folded."
8628 (interactive "P")
8629 (let ((win (selected-window)))
8630 (if (and (window-live-p org-agenda-show-window)
8631 (eq this-command last-command))
8632 (progn
8633 (select-window org-agenda-show-window)
8634 (ignore-errors (scroll-up)))
8635 (org-agenda-goto t)
8636 (if arg (org-show-entry) (outline-show-subtree))
8637 (setq org-agenda-show-window (selected-window)))
8638 (select-window win)))
8640 (defun org-agenda-show-scroll-down ()
8641 "Scroll down the window showing the agenda."
8642 (interactive)
8643 (let ((win (selected-window)))
8644 (when (window-live-p org-agenda-show-window)
8645 (select-window org-agenda-show-window)
8646 (ignore-errors (scroll-down))
8647 (select-window win))))
8649 (defun org-agenda-show-1 (&optional more)
8650 "Display the Org file which contains the item at point.
8651 The prefix arg selects the amount of information to display:
8653 0 hide the subtree
8654 1 just show the entry according to defaults.
8655 2 show the children view
8656 3 show the subtree view
8657 4 show the entire subtree and any LOGBOOK drawers
8658 5 show the entire subtree and any drawers
8659 With prefix argument FULL-ENTRY, make the entire entry visible
8660 if it was hidden in the outline."
8661 (interactive "p")
8662 (let ((win (selected-window)))
8663 (org-agenda-goto t)
8664 (org-back-to-heading)
8665 (set-window-start (selected-window) (point-at-bol))
8666 (cond
8667 ((= more 0)
8668 (outline-hide-subtree)
8669 (save-excursion
8670 (org-back-to-heading)
8671 (run-hook-with-args 'org-cycle-hook 'folded))
8672 (message "Remote: FOLDED"))
8673 ((and (called-interactively-p 'any) (= more 1))
8674 (message "Remote: show with default settings"))
8675 ((= more 2)
8676 (outline-show-entry)
8677 (org-show-children)
8678 (save-excursion
8679 (org-back-to-heading)
8680 (run-hook-with-args 'org-cycle-hook 'children))
8681 (message "Remote: CHILDREN"))
8682 ((= more 3)
8683 (outline-show-subtree)
8684 (save-excursion
8685 (org-back-to-heading)
8686 (run-hook-with-args 'org-cycle-hook 'subtree))
8687 (message "Remote: SUBTREE"))
8688 ((= more 4)
8689 (outline-show-subtree)
8690 (save-excursion
8691 (org-back-to-heading)
8692 (org-cycle-hide-drawers 'subtree '("LOGBOOK")))
8693 (message "Remote: SUBTREE AND LOGBOOK"))
8694 ((> more 4)
8695 (outline-show-subtree)
8696 (message "Remote: SUBTREE AND ALL DRAWERS")))
8697 (select-window win)))
8699 (defvar org-agenda-cycle-counter nil)
8700 (defun org-agenda-cycle-show (&optional n)
8701 "Show the current entry in another window, with default settings.
8703 Default settings are taken from `org-show-context-detail'. When
8704 use repeatedly in immediate succession, the remote entry will
8705 cycle through visibility
8707 children -> subtree -> folded
8709 When called with a numeric prefix arg, that arg will be passed through to
8710 `org-agenda-show-1'. For the interpretation of that argument, see the
8711 docstring of `org-agenda-show-1'."
8712 (interactive "P")
8713 (if (integerp n)
8714 (setq org-agenda-cycle-counter n)
8715 (if (not (eq last-command this-command))
8716 (setq org-agenda-cycle-counter 1)
8717 (if (equal org-agenda-cycle-counter 0)
8718 (setq org-agenda-cycle-counter 2)
8719 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8720 (if (> org-agenda-cycle-counter 3)
8721 (setq org-agenda-cycle-counter 0)))))
8722 (org-agenda-show-1 org-agenda-cycle-counter))
8724 (defun org-agenda-recenter (arg)
8725 "Display the Org file which contains the item at point and recenter."
8726 (interactive "P")
8727 (let ((win (selected-window)))
8728 (org-agenda-goto t)
8729 (recenter arg)
8730 (select-window win)))
8732 (defun org-agenda-show-mouse (ev)
8733 "Display the Org file which contains the item at the mouse click."
8734 (interactive "e")
8735 (mouse-set-point ev)
8736 (org-agenda-show))
8738 (defun org-agenda-check-no-diary ()
8739 "Check if the entry is a diary link and abort if yes."
8740 (if (org-get-at-bol 'org-agenda-diary-link)
8741 (org-agenda-error)))
8743 (defun org-agenda-error ()
8744 "Throw an error when a command is not allowed in the agenda."
8745 (user-error "Command not allowed in this line"))
8747 (defun org-agenda-tree-to-indirect-buffer (arg)
8748 "Show the subtree corresponding to the current entry in an indirect buffer.
8749 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8751 With a numerical prefix ARG, go up to this level and then take that tree.
8752 With a negative numeric ARG, go up by this number of levels.
8753 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
8754 use the dedicated frame)."
8755 (interactive "P")
8756 (if current-prefix-arg
8757 (org-agenda-do-tree-to-indirect-buffer arg)
8758 (let ((agenda-buffer (buffer-name))
8759 (agenda-window (selected-window))
8760 (indirect-window
8761 (and org-last-indirect-buffer
8762 (get-buffer-window org-last-indirect-buffer))))
8763 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8764 (unless (or (eq org-indirect-buffer-display 'new-frame)
8765 (eq org-indirect-buffer-display 'dedicated-frame))
8766 (unwind-protect
8767 (unless (and indirect-window (window-live-p indirect-window))
8768 (setq indirect-window (split-window agenda-window)))
8769 (and indirect-window (select-window indirect-window))
8770 (switch-to-buffer org-last-indirect-buffer :norecord)
8771 (fit-window-to-buffer indirect-window)))
8772 (select-window (get-buffer-window agenda-buffer))
8773 (setq org-agenda-last-indirect-buffer org-last-indirect-buffer))))
8775 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8776 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8777 (org-agenda-check-no-diary)
8778 (let* ((marker (or (org-get-at-bol 'org-marker)
8779 (org-agenda-error)))
8780 (buffer (marker-buffer marker))
8781 (pos (marker-position marker)))
8782 (with-current-buffer buffer
8783 (save-excursion
8784 (goto-char pos)
8785 (funcall 'org-tree-to-indirect-buffer arg)))))
8787 (defvar org-last-heading-marker (make-marker)
8788 "Marker pointing to the headline that last changed its TODO state
8789 by a remote command from the agenda.")
8791 (defun org-agenda-todo-nextset ()
8792 "Switch TODO entry to next sequence."
8793 (interactive)
8794 (org-agenda-todo 'nextset))
8796 (defun org-agenda-todo-previousset ()
8797 "Switch TODO entry to previous sequence."
8798 (interactive)
8799 (org-agenda-todo 'previousset))
8801 (defun org-agenda-todo (&optional arg)
8802 "Cycle TODO state of line at point, also in Org file.
8803 This changes the line at point, all other lines in the agenda referring to
8804 the same tree node, and the headline of the tree node in the Org file."
8805 (interactive "P")
8806 (org-agenda-check-no-diary)
8807 (let* ((col (current-column))
8808 (marker (or (org-get-at-bol 'org-marker)
8809 (org-agenda-error)))
8810 (buffer (marker-buffer marker))
8811 (pos (marker-position marker))
8812 (hdmarker (org-get-at-bol 'org-hd-marker))
8813 (todayp (org-agenda-today-p (org-get-at-bol 'day)))
8814 (inhibit-read-only t)
8815 org-agenda-headline-snapshot-before-repeat newhead just-one)
8816 (org-with-remote-undo buffer
8817 (with-current-buffer buffer
8818 (widen)
8819 (goto-char pos)
8820 (org-show-context 'agenda)
8821 (let ((current-prefix-arg arg))
8822 (call-interactively 'org-todo))
8823 (and (bolp) (forward-char 1))
8824 (setq newhead (org-get-heading))
8825 (when (and (bound-and-true-p
8826 org-agenda-headline-snapshot-before-repeat)
8827 (not (equal org-agenda-headline-snapshot-before-repeat
8828 newhead))
8829 todayp)
8830 (setq newhead org-agenda-headline-snapshot-before-repeat
8831 just-one t))
8832 (save-excursion
8833 (org-back-to-heading)
8834 (move-marker org-last-heading-marker (point))))
8835 (beginning-of-line 1)
8836 (save-window-excursion
8837 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8838 (when (bound-and-true-p org-clock-out-when-done)
8839 (string-match (concat "^" (regexp-opt org-done-keywords-for-agenda))
8840 newhead)
8841 (org-agenda-unmark-clocking-task))
8842 (org-move-to-column col)
8843 (org-agenda-mark-clocking-task))))
8845 (defun org-agenda-add-note (&optional arg)
8846 "Add a time-stamped note to the entry at point."
8847 (interactive "P")
8848 (org-agenda-check-no-diary)
8849 (let* ((marker (or (org-get-at-bol 'org-marker)
8850 (org-agenda-error)))
8851 (buffer (marker-buffer marker))
8852 (pos (marker-position marker))
8853 (hdmarker (org-get-at-bol 'org-hd-marker))
8854 (inhibit-read-only t))
8855 (with-current-buffer buffer
8856 (widen)
8857 (goto-char pos)
8858 (org-show-context 'agenda)
8859 (org-add-note))))
8861 (defun org-agenda-change-all-lines (newhead hdmarker
8862 &optional fixface just-this)
8863 "Change all lines in the agenda buffer which match HDMARKER.
8864 The new content of the line will be NEWHEAD (as modified by
8865 `org-agenda-format-item'). HDMARKER is checked with
8866 `equal' against all `org-hd-marker' text properties in the file.
8867 If FIXFACE is non-nil, the face of each item is modified according to
8868 the new TODO state.
8869 If JUST-THIS is non-nil, change just the current line, not all.
8870 If FORCE-TAGS is non nil, the car of it returns the new tags."
8871 (let* ((inhibit-read-only t)
8872 (line (org-current-line))
8873 (org-agenda-buffer (current-buffer))
8874 (thetags (with-current-buffer (marker-buffer hdmarker)
8875 (org-with-wide-buffer
8876 (goto-char hdmarker)
8877 (org-get-tags-at))))
8878 props m pl undone-face done-face finish new dotime level cat tags)
8879 (save-excursion
8880 (goto-char (point-max))
8881 (beginning-of-line 1)
8882 (while (not finish)
8883 (setq finish (bobp))
8884 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8885 (or (not just-this) (= (org-current-line) line))
8886 (equal m hdmarker))
8887 (setq props (text-properties-at (point))
8888 dotime (org-get-at-bol 'dotime)
8889 cat (org-get-at-eol 'org-category 1)
8890 level (org-get-at-bol 'level)
8891 tags thetags
8893 (let ((org-prefix-format-compiled
8894 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8895 org-prefix-format-compiled))
8896 (extra (org-get-at-bol 'extra)))
8897 (with-current-buffer (marker-buffer hdmarker)
8898 (org-with-wide-buffer
8899 (org-agenda-format-item extra newhead level cat tags dotime))))
8900 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8901 undone-face (org-get-at-bol 'undone-face)
8902 done-face (org-get-at-bol 'done-face))
8903 (beginning-of-line 1)
8904 (cond
8905 ((equal new "") (delete-region (point) (line-beginning-position 2)))
8906 ((looking-at ".*")
8907 ;; When replacing the whole line, preserve bulk mark
8908 ;; overlay, if any.
8909 (let ((mark (catch :overlay
8910 (dolist (o (overlays-in (point) (+ 2 (point))))
8911 (when (eq (overlay-get o 'type)
8912 'org-marked-entry-overlay)
8913 (throw :overlay o))))))
8914 (replace-match new t t)
8915 (beginning-of-line)
8916 (when mark (move-overlay mark (point) (+ 2 (point)))))
8917 (add-text-properties (point-at-bol) (point-at-eol) props)
8918 (when fixface
8919 (add-text-properties
8920 (point-at-bol) (point-at-eol)
8921 (list 'face
8922 (if org-last-todo-state-is-todo
8923 undone-face done-face))))
8924 (org-agenda-highlight-todo 'line)
8925 (beginning-of-line 1))
8926 (t (error "Line update did not work")))
8927 (save-restriction
8928 (narrow-to-region (point-at-bol) (point-at-eol))
8929 (org-agenda-finalize)))
8930 (beginning-of-line 0)))))
8932 (defun org-agenda-align-tags (&optional line)
8933 "Align all tags in agenda items to `org-agenda-tags-column'."
8934 (let ((inhibit-read-only t) l c)
8935 (save-excursion
8936 (goto-char (if line (point-at-bol) (point-min)))
8937 (while (re-search-forward "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"
8938 (if line (point-at-eol) nil) t)
8939 (add-text-properties
8940 (match-beginning 2) (match-end 2)
8941 (list 'face (delq nil (let ((prop (get-text-property
8942 (match-beginning 2) 'face)))
8943 (or (listp prop) (setq prop (list prop)))
8944 (if (memq 'org-tag prop)
8945 prop
8946 (cons 'org-tag prop))))))
8947 (setq l (- (match-end 2) (match-beginning 2))
8948 c (if (< org-agenda-tags-column 0)
8949 (- (abs org-agenda-tags-column) l)
8950 org-agenda-tags-column))
8951 (delete-region (match-beginning 1) (match-end 1))
8952 (goto-char (match-beginning 1))
8953 (insert (org-add-props
8954 (make-string (max 1 (- c (current-column))) ?\ )
8955 (plist-put (copy-sequence (text-properties-at (point)))
8956 'face nil))))
8957 (goto-char (point-min))
8958 (org-font-lock-add-tag-faces (point-max)))))
8960 (defun org-agenda-priority-up ()
8961 "Increase the priority of line at point, also in Org file."
8962 (interactive)
8963 (org-agenda-priority 'up))
8965 (defun org-agenda-priority-down ()
8966 "Decrease the priority of line at point, also in Org file."
8967 (interactive)
8968 (org-agenda-priority 'down))
8970 (defun org-agenda-priority (&optional force-direction)
8971 "Set the priority of line at point, also in Org file.
8972 This changes the line at point, all other lines in the agenda referring to
8973 the same tree node, and the headline of the tree node in the Org file.
8974 Called with a universal prefix arg, show the priority instead of setting it."
8975 (interactive "P")
8976 (if (equal force-direction '(4))
8977 (org-show-priority)
8978 (unless org-enable-priority-commands
8979 (error "Priority commands are disabled"))
8980 (org-agenda-check-no-diary)
8981 (let* ((col (current-column))
8982 (marker (or (org-get-at-bol 'org-marker)
8983 (org-agenda-error)))
8984 (hdmarker (org-get-at-bol 'org-hd-marker))
8985 (buffer (marker-buffer hdmarker))
8986 (pos (marker-position hdmarker))
8987 (inhibit-read-only t)
8988 newhead)
8989 (org-with-remote-undo buffer
8990 (with-current-buffer buffer
8991 (widen)
8992 (goto-char pos)
8993 (org-show-context 'agenda)
8994 (funcall 'org-priority force-direction)
8995 (end-of-line 1)
8996 (setq newhead (org-get-heading)))
8997 (org-agenda-change-all-lines newhead hdmarker)
8998 (org-move-to-column col)))))
9000 ;; FIXME: should fix the tags property of the agenda line.
9001 (defun org-agenda-set-tags (&optional tag onoff)
9002 "Set tags for the current headline."
9003 (interactive)
9004 (org-agenda-check-no-diary)
9005 (if (and (org-region-active-p) (called-interactively-p 'any))
9006 (call-interactively 'org-change-tag-in-region)
9007 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9008 (org-agenda-error)))
9009 (buffer (marker-buffer hdmarker))
9010 (pos (marker-position hdmarker))
9011 (inhibit-read-only t)
9012 newhead)
9013 (org-with-remote-undo buffer
9014 (with-current-buffer buffer
9015 (widen)
9016 (goto-char pos)
9017 (org-show-context 'agenda)
9018 (if tag
9019 (org-toggle-tag tag onoff)
9020 (call-interactively 'org-set-tags))
9021 (end-of-line 1)
9022 (setq newhead (org-get-heading)))
9023 (org-agenda-change-all-lines newhead hdmarker)
9024 (beginning-of-line 1)))))
9026 (defun org-agenda-set-property ()
9027 "Set a property for the current headline."
9028 (interactive)
9029 (org-agenda-check-no-diary)
9030 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9031 (org-agenda-error)))
9032 (buffer (marker-buffer hdmarker))
9033 (pos (marker-position hdmarker))
9034 (inhibit-read-only t)
9035 newhead)
9036 (org-with-remote-undo buffer
9037 (with-current-buffer buffer
9038 (widen)
9039 (goto-char pos)
9040 (org-show-context 'agenda)
9041 (call-interactively 'org-set-property)))))
9043 (defun org-agenda-set-effort ()
9044 "Set the effort property for the current headline."
9045 (interactive)
9046 (org-agenda-check-no-diary)
9047 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9048 (org-agenda-error)))
9049 (buffer (marker-buffer hdmarker))
9050 (pos (marker-position hdmarker))
9051 (inhibit-read-only t)
9052 newhead)
9053 (org-with-remote-undo buffer
9054 (with-current-buffer buffer
9055 (widen)
9056 (goto-char pos)
9057 (org-show-context 'agenda)
9058 (call-interactively 'org-set-effort)
9059 (end-of-line 1)
9060 (setq newhead (org-get-heading)))
9061 (org-agenda-change-all-lines newhead hdmarker))))
9063 (defun org-agenda-toggle-archive-tag ()
9064 "Toggle the archive tag for the current entry."
9065 (interactive)
9066 (org-agenda-check-no-diary)
9067 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9068 (org-agenda-error)))
9069 (buffer (marker-buffer hdmarker))
9070 (pos (marker-position hdmarker))
9071 (inhibit-read-only t)
9072 newhead)
9073 (org-with-remote-undo buffer
9074 (with-current-buffer buffer
9075 (widen)
9076 (goto-char pos)
9077 (org-show-context 'agenda)
9078 (call-interactively 'org-toggle-archive-tag)
9079 (end-of-line 1)
9080 (setq newhead (org-get-heading)))
9081 (org-agenda-change-all-lines newhead hdmarker)
9082 (beginning-of-line 1))))
9084 (defun org-agenda-do-date-later (arg)
9085 (interactive "P")
9086 (cond
9087 ((or (equal arg '(16))
9088 (memq last-command
9089 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9090 (setq this-command 'org-agenda-date-later-minutes)
9091 (org-agenda-date-later-minutes 1))
9092 ((or (equal arg '(4))
9093 (memq last-command
9094 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9095 (setq this-command 'org-agenda-date-later-hours)
9096 (org-agenda-date-later-hours 1))
9098 (org-agenda-date-later (prefix-numeric-value arg)))))
9100 (defun org-agenda-do-date-earlier (arg)
9101 (interactive "P")
9102 (cond
9103 ((or (equal arg '(16))
9104 (memq last-command
9105 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9106 (setq this-command 'org-agenda-date-earlier-minutes)
9107 (org-agenda-date-earlier-minutes 1))
9108 ((or (equal arg '(4))
9109 (memq last-command
9110 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9111 (setq this-command 'org-agenda-date-earlier-hours)
9112 (org-agenda-date-earlier-hours 1))
9114 (org-agenda-date-earlier (prefix-numeric-value arg)))))
9116 (defun org-agenda-date-later (arg &optional what)
9117 "Change the date of this item to ARG day(s) later."
9118 (interactive "p")
9119 (org-agenda-check-type t 'agenda 'timeline)
9120 (org-agenda-check-no-diary)
9121 (let* ((marker (or (org-get-at-bol 'org-marker)
9122 (org-agenda-error)))
9123 (buffer (marker-buffer marker))
9124 (pos (marker-position marker))
9125 cdate today)
9126 (org-with-remote-undo buffer
9127 (with-current-buffer buffer
9128 (widen)
9129 (goto-char pos)
9130 (if (not (org-at-timestamp-p))
9131 (error "Cannot find time stamp"))
9132 (when (and org-agenda-move-date-from-past-immediately-to-today
9133 (equal arg 1)
9134 (or (not what) (eq what 'day))
9135 (not (save-match-data (org-at-date-range-p))))
9136 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9137 cdate (calendar-absolute-from-gregorian
9138 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9139 today (org-today))
9140 (if (> today cdate)
9141 ;; immediately shift to today
9142 (setq arg (- today cdate))))
9143 (org-timestamp-change arg (or what 'day))
9144 (when (and (org-at-date-range-p)
9145 (re-search-backward org-tr-regexp-both (point-at-bol)))
9146 (let ((end org-last-changed-timestamp))
9147 (org-timestamp-change arg (or what 'day))
9148 (setq org-last-changed-timestamp
9149 (concat org-last-changed-timestamp "--" end)))))
9150 (org-agenda-show-new-time marker org-last-changed-timestamp))
9151 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9153 (defun org-agenda-date-earlier (arg &optional what)
9154 "Change the date of this item to ARG day(s) earlier."
9155 (interactive "p")
9156 (org-agenda-date-later (- arg) what))
9158 (defun org-agenda-date-later-minutes (arg)
9159 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9160 (interactive "p")
9161 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9162 (org-agenda-date-later arg 'minute))
9164 (defun org-agenda-date-earlier-minutes (arg)
9165 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9166 (interactive "p")
9167 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9168 (org-agenda-date-earlier arg 'minute))
9170 (defun org-agenda-date-later-hours (arg)
9171 "Change the time of this item, in hour steps."
9172 (interactive "p")
9173 (org-agenda-date-later arg 'hour))
9175 (defun org-agenda-date-earlier-hours (arg)
9176 "Change the time of this item, in hour steps."
9177 (interactive "p")
9178 (org-agenda-date-earlier arg 'hour))
9180 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9181 "Show new date stamp via text properties."
9182 ;; We use text properties to make this undoable
9183 (let ((inhibit-read-only t))
9184 (setq stamp (concat prefix " => " stamp " "))
9185 (save-excursion
9186 (goto-char (point-max))
9187 (while (not (bobp))
9188 (when (equal marker (org-get-at-bol 'org-marker))
9189 (remove-text-properties (point-at-bol) (point-at-eol) '(display))
9190 (org-move-to-column (- (window-width) (length stamp)) t)
9191 (add-text-properties
9192 (1- (point)) (point-at-eol)
9193 (list 'display (org-add-props stamp nil
9194 'face '(secondary-selection default))))
9195 (beginning-of-line 1))
9196 (beginning-of-line 0)))))
9198 (defun org-agenda-date-prompt (arg)
9199 "Change the date of this item. Date is prompted for, with default today.
9200 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9201 be used to request time specification in the time stamp."
9202 (interactive "P")
9203 (org-agenda-check-type t 'agenda 'timeline)
9204 (org-agenda-check-no-diary)
9205 (let* ((marker (or (org-get-at-bol 'org-marker)
9206 (org-agenda-error)))
9207 (buffer (marker-buffer marker))
9208 (pos (marker-position marker)))
9209 (org-with-remote-undo buffer
9210 (with-current-buffer buffer
9211 (widen)
9212 (goto-char pos)
9213 (if (not (org-at-timestamp-p t))
9214 (error "Cannot find time stamp"))
9215 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
9216 (org-agenda-show-new-time marker org-last-changed-timestamp))
9217 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9219 (defun org-agenda-schedule (arg &optional time)
9220 "Schedule the item at point.
9221 ARG is passed through to `org-schedule'."
9222 (interactive "P")
9223 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9224 (org-agenda-check-no-diary)
9225 (let* ((marker (or (org-get-at-bol 'org-marker)
9226 (org-agenda-error)))
9227 (type (marker-insertion-type marker))
9228 (buffer (marker-buffer marker))
9229 (pos (marker-position marker))
9231 (set-marker-insertion-type marker t)
9232 (org-with-remote-undo buffer
9233 (with-current-buffer buffer
9234 (widen)
9235 (goto-char pos)
9236 (setq ts (org-schedule arg time)))
9237 (org-agenda-show-new-time marker ts " S"))
9238 (message "%s" ts)))
9240 (defun org-agenda-deadline (arg &optional time)
9241 "Schedule the item at point.
9242 ARG is passed through to `org-deadline'."
9243 (interactive "P")
9244 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9245 (org-agenda-check-no-diary)
9246 (let* ((marker (or (org-get-at-bol 'org-marker)
9247 (org-agenda-error)))
9248 (buffer (marker-buffer marker))
9249 (pos (marker-position marker))
9251 (org-with-remote-undo buffer
9252 (with-current-buffer buffer
9253 (widen)
9254 (goto-char pos)
9255 (setq ts (org-deadline arg time)))
9256 (org-agenda-show-new-time marker ts " D"))
9257 (message "%s" ts)))
9259 (defun org-agenda-clock-in (&optional arg)
9260 "Start the clock on the currently selected item."
9261 (interactive "P")
9262 (org-agenda-check-no-diary)
9263 (if (equal arg '(4))
9264 (org-clock-in arg)
9265 (let* ((marker (or (org-get-at-bol 'org-marker)
9266 (org-agenda-error)))
9267 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9268 (pos (marker-position marker))
9269 (col (current-column))
9270 newhead)
9271 (org-with-remote-undo (marker-buffer marker)
9272 (with-current-buffer (marker-buffer marker)
9273 (widen)
9274 (goto-char pos)
9275 (org-show-context 'agenda)
9276 (org-cycle-hide-drawers 'children)
9277 (org-clock-in arg)
9278 (setq newhead (org-get-heading)))
9279 (org-agenda-change-all-lines newhead hdmarker))
9280 (org-move-to-column col))))
9282 (defun org-agenda-clock-out ()
9283 "Stop the currently running clock."
9284 (interactive)
9285 (unless (marker-buffer org-clock-marker)
9286 (error "No running clock"))
9287 (let ((marker (make-marker)) (col (current-column)) newhead)
9288 (org-with-remote-undo (marker-buffer org-clock-marker)
9289 (with-current-buffer (marker-buffer org-clock-marker)
9290 (org-with-wide-buffer
9291 (goto-char org-clock-marker)
9292 (org-back-to-heading t)
9293 (move-marker marker (point))
9294 (org-clock-out)
9295 (setq newhead (org-get-heading)))))
9296 (org-agenda-change-all-lines newhead marker)
9297 (move-marker marker nil)
9298 (org-move-to-column col)
9299 (org-agenda-unmark-clocking-task)))
9301 (defun org-agenda-clock-cancel (&optional arg)
9302 "Cancel the currently running clock."
9303 (interactive "P")
9304 (unless (marker-buffer org-clock-marker)
9305 (user-error "No running clock"))
9306 (org-with-remote-undo (marker-buffer org-clock-marker)
9307 (org-clock-cancel)))
9309 (defun org-agenda-clock-goto ()
9310 "Jump to the currently clocked in task within the agenda.
9311 If the currently clocked in task is not listed in the agenda
9312 buffer, display it in another window."
9313 (interactive)
9314 (let (pos)
9315 (mapc (lambda (o)
9316 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
9317 (setq pos (overlay-start o))))
9318 (overlays-in (point-min) (point-max)))
9319 (cond (pos (goto-char pos))
9320 ;; If the currently clocked entry is not in the agenda
9321 ;; buffer, we visit it in another window:
9322 ((bound-and-true-p org-clock-current-task)
9323 (org-switch-to-buffer-other-window (org-clock-goto)))
9324 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9326 (defun org-agenda-diary-entry-in-org-file ()
9327 "Make a diary entry in the file `org-agenda-diary-file'."
9328 (let (d1 d2 char (text "") dp1 dp2)
9329 (if (equal (buffer-name) "*Calendar*")
9330 (setq d1 (calendar-cursor-to-date t)
9331 d2 (car calendar-mark-ring))
9332 (setq dp1 (get-text-property (point-at-bol) 'day))
9333 (unless dp1 (user-error "No date defined in current line"))
9334 (setq d1 (calendar-gregorian-from-absolute dp1)
9335 d2 (and (ignore-errors (mark))
9336 (save-excursion
9337 (goto-char (mark))
9338 (setq dp2 (get-text-property (point-at-bol) 'day)))
9339 (calendar-gregorian-from-absolute dp2))))
9340 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9341 (setq char (read-char-exclusive))
9342 (cond
9343 ((equal char ?d)
9344 (setq text (read-string "Day entry: "))
9345 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9346 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9347 ((equal char ?a)
9348 (setq d1 (list (car d1) (nth 1 d1)
9349 (read-number (format "Reference year [%d]: " (nth 2 d1))
9350 (nth 2 d1))))
9351 (setq text (read-string "Anniversary (use %d to show years): "))
9352 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9353 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9354 ((equal char ?b)
9355 (setq text (read-string "Block entry: "))
9356 (unless (and d1 d2 (not (equal d1 d2)))
9357 (user-error "No block of days selected"))
9358 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9359 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9360 ((equal char ?j)
9361 (org-switch-to-buffer-other-window
9362 (find-file-noselect org-agenda-diary-file))
9363 (require 'org-datetree)
9364 (org-datetree-find-date-create d1)
9365 (org-reveal t))
9366 (t (user-error "Invalid selection character `%c'" char)))))
9368 (defcustom org-agenda-insert-diary-strategy 'date-tree
9369 "Where in `org-agenda-diary-file' should new entries be added?
9370 Valid values:
9372 date-tree in the date tree, as first child of the date
9373 date-tree-last in the date tree, as last child of the date
9374 top-level as top-level entries at the end of the file."
9375 :group 'org-agenda
9376 :type '(choice
9377 (const :tag "first in a date tree" date-tree)
9378 (const :tag "last in a date tree" date-tree-last)
9379 (const :tag "as top level at end of file" top-level)))
9381 (defcustom org-agenda-insert-diary-extract-time nil
9382 "Non-nil means extract any time specification from the diary entry."
9383 :group 'org-agenda
9384 :version "24.1"
9385 :type 'boolean)
9387 (defcustom org-agenda-bulk-mark-char ">"
9388 "A single-character string to be used as the bulk mark."
9389 :group 'org-agenda
9390 :version "24.1"
9391 :type 'string)
9393 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9394 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9395 If TEXT is not empty, it will become the headline of the new entry, and
9396 the resulting entry will not be shown. When TEXT is empty, switch to
9397 `org-agenda-diary-file' and let the user finish the entry there."
9398 (let ((cw (current-window-configuration)))
9399 (org-switch-to-buffer-other-window
9400 (find-file-noselect org-agenda-diary-file))
9401 (widen)
9402 (goto-char (point-min))
9403 (cond
9404 ((eq type 'anniversary)
9405 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
9406 (progn
9407 (or (org-at-heading-p t)
9408 (progn
9409 (outline-next-heading)
9410 (insert "* Anniversaries\n\n")
9411 (beginning-of-line -1)))))
9412 (outline-next-heading)
9413 (org-back-over-empty-lines)
9414 (backward-char 1)
9415 (insert "\n")
9416 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9417 (nth 2 d1) (car d1) (nth 1 d1) text)))
9418 ((eq type 'day)
9419 (let ((org-prefix-has-time t)
9420 (org-agenda-time-leading-zero t)
9421 fmt time time2)
9422 (if org-agenda-insert-diary-extract-time
9423 ;; Use org-agenda-format-item to parse text for a time-range and
9424 ;; remove it. FIXME: This is a hack, we should refactor
9425 ;; that function to make time extraction available separately
9426 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9427 time (get-text-property 0 'time fmt)
9428 time2 (if (> (length time) 0)
9429 ;; split-string removes trailing ...... if
9430 ;; no end time given. First space
9431 ;; separates time from date.
9432 (concat " " (car (split-string time "\\.")))
9433 nil)
9434 text (get-text-property 0 'txt fmt)))
9435 (if (eq org-agenda-insert-diary-strategy 'top-level)
9436 (org-agenda-insert-diary-as-top-level text)
9437 (require 'org-datetree)
9438 (org-datetree-find-date-create d1)
9439 (org-agenda-insert-diary-make-new-entry text))
9440 (org-insert-time-stamp (org-time-from-absolute
9441 (calendar-absolute-from-gregorian d1))
9442 nil nil nil nil time2))
9443 (end-of-line 0))
9444 ((eq type 'block)
9445 (if (> (calendar-absolute-from-gregorian d1)
9446 (calendar-absolute-from-gregorian d2))
9447 (setq d1 (prog1 d2 (setq d2 d1))))
9448 (if (eq org-agenda-insert-diary-strategy 'top-level)
9449 (org-agenda-insert-diary-as-top-level text)
9450 (require 'org-datetree)
9451 (org-datetree-find-date-create d1)
9452 (org-agenda-insert-diary-make-new-entry text))
9453 (org-insert-time-stamp (org-time-from-absolute
9454 (calendar-absolute-from-gregorian d1)))
9455 (insert "--")
9456 (org-insert-time-stamp (org-time-from-absolute
9457 (calendar-absolute-from-gregorian d2)))
9458 (end-of-line 0)))
9459 (if (string-match "\\S-" text)
9460 (progn
9461 (set-window-configuration cw)
9462 (message "%s entry added to %s"
9463 (capitalize (symbol-name type))
9464 (abbreviate-file-name org-agenda-diary-file)))
9465 (org-reveal t)
9466 (message "Please finish entry here"))))
9468 (defun org-agenda-insert-diary-as-top-level (text)
9469 "Make new entry as a top-level entry at the end of the file.
9470 Add TEXT as headline, and position the cursor in the second line so that
9471 a timestamp can be added there."
9472 (widen)
9473 (goto-char (point-max))
9474 (unless (bolp) (insert "\n"))
9475 (org-insert-heading nil t t)
9476 (insert text)
9477 (org-end-of-meta-data)
9478 (unless (bolp) (insert "\n"))
9479 (when org-adapt-indentation (indent-to-column 2)))
9481 (defun org-agenda-insert-diary-make-new-entry (text)
9482 "Make a new entry with TEXT as a child of the current subtree.
9483 Position the point in the heading's first body line so that
9484 a timestamp can be added there."
9485 (cond
9486 ((eq org-agenda-insert-diary-strategy 'date-tree-last)
9487 (end-of-line)
9488 (org-insert-heading '(4) t)
9489 (org-do-demote))
9491 (outline-next-heading)
9492 (org-back-over-empty-lines)
9493 (unless (looking-at "[ \t]*$") (save-excursion (insert "\n")))
9494 (org-insert-heading nil t)
9495 (org-do-demote)))
9496 (let ((col (current-column)))
9497 (insert text)
9498 (org-end-of-meta-data)
9499 ;; Ensure point is left on a blank line, at proper indentation.
9500 (unless (bolp) (insert "\n"))
9501 (unless (looking-at-p "^[ \t]*$") (save-excursion (insert "\n")))
9502 (when org-adapt-indentation (indent-to-column col)))
9503 (org-show-set-visibility 'lineage))
9505 (defun org-agenda-diary-entry ()
9506 "Make a diary entry, like the `i' command from the calendar.
9507 All the standard commands work: block, weekly etc.
9508 When `org-agenda-diary-file' points to a file,
9509 `org-agenda-diary-entry-in-org-file' is called instead to create
9510 entries in that Org file."
9511 (interactive)
9512 (if (not (eq org-agenda-diary-file 'diary-file))
9513 (org-agenda-diary-entry-in-org-file)
9514 (require 'diary-lib)
9515 (let* ((char (progn
9516 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
9517 (read-char-exclusive)))
9518 (cmd (cdr (assoc char
9519 '((?d . diary-insert-entry)
9520 (?w . diary-insert-weekly-entry)
9521 (?m . diary-insert-monthly-entry)
9522 (?y . diary-insert-yearly-entry)
9523 (?a . diary-insert-anniversary-entry)
9524 (?b . diary-insert-block-entry)
9525 (?c . diary-insert-cyclic-entry)))))
9526 (oldf (symbol-function 'calendar-cursor-to-date))
9527 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9528 (point (point))
9529 (mark (or (mark t) (point))))
9530 (unless cmd
9531 (user-error "No command associated with <%c>" char))
9532 (unless (and (get-text-property point 'day)
9533 (or (not (equal ?b char))
9534 (get-text-property mark 'day)))
9535 (user-error "Don't know which date to use for diary entry"))
9536 ;; We implement this by hacking the `calendar-cursor-to-date' function
9537 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9538 (let ((calendar-mark-ring
9539 (list (calendar-gregorian-from-absolute
9540 (or (get-text-property mark 'day)
9541 (get-text-property point 'day))))))
9542 (unwind-protect
9543 (progn
9544 (fset 'calendar-cursor-to-date
9545 (lambda (&optional error dummy)
9546 (calendar-gregorian-from-absolute
9547 (get-text-property point 'day))))
9548 (call-interactively cmd))
9549 (fset 'calendar-cursor-to-date oldf))))))
9551 (defun org-agenda-execute-calendar-command (cmd)
9552 "Execute a calendar command from the agenda with date from cursor."
9553 (org-agenda-check-type t 'agenda 'timeline)
9554 (require 'diary-lib)
9555 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9556 (user-error "Don't know which date to use for the calendar command"))
9557 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9558 (point (point))
9559 (date (calendar-gregorian-from-absolute
9560 (get-text-property point 'day)))
9561 ;; the following 2 vars are needed in the calendar
9562 (displayed-month (car date))
9563 (displayed-year (nth 2 date)))
9564 (unwind-protect
9565 (progn
9566 (fset 'calendar-cursor-to-date
9567 (lambda (&optional error dummy)
9568 (calendar-gregorian-from-absolute
9569 (get-text-property point 'day))))
9570 (call-interactively cmd))
9571 (fset 'calendar-cursor-to-date oldf))))
9573 (defun org-agenda-phases-of-moon ()
9574 "Display the phases of the moon for the 3 months around the cursor date."
9575 (interactive)
9576 (org-agenda-execute-calendar-command 'calendar-lunar-phases))
9578 (defun org-agenda-holidays ()
9579 "Display the holidays for the 3 months around the cursor date."
9580 (interactive)
9581 (org-agenda-execute-calendar-command 'calendar-list-holidays))
9583 (defvar calendar-longitude) ; defined in calendar.el
9584 (defvar calendar-latitude) ; defined in calendar.el
9585 (defvar calendar-location-name) ; defined in calendar.el
9587 (defun org-agenda-sunrise-sunset (arg)
9588 "Display sunrise and sunset for the cursor date.
9589 Latitude and longitude can be specified with the variables
9590 `calendar-latitude' and `calendar-longitude'. When called with prefix
9591 argument, latitude and longitude will be prompted for."
9592 (interactive "P")
9593 (require 'solar)
9594 (let ((calendar-longitude (if arg nil calendar-longitude))
9595 (calendar-latitude (if arg nil calendar-latitude))
9596 (calendar-location-name
9597 (if arg "the given coordinates" calendar-location-name)))
9598 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9600 (defun org-agenda-goto-calendar ()
9601 "Open the Emacs calendar with the date at the cursor."
9602 (interactive)
9603 (org-agenda-check-type t 'agenda 'timeline)
9604 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9605 (user-error "Don't know which date to open in calendar")))
9606 (date (calendar-gregorian-from-absolute day))
9607 (calendar-move-hook nil)
9608 (calendar-view-holidays-initially-flag nil)
9609 (calendar-view-diary-initially-flag nil))
9610 (calendar)
9611 (calendar-goto-date date)))
9613 ;;;###autoload
9614 (defun org-calendar-goto-agenda ()
9615 "Compute the Org agenda for the calendar date displayed at the cursor.
9616 This is a command that has to be installed in `calendar-mode-map'."
9617 (interactive)
9618 ;; Temporarily disable sticky agenda since user clearly wants to
9619 ;; refresh view anyway.
9620 (let ((org-agenda-buffer-tmp-name "*Org Agenda(a)*")
9621 (org-agenda-sticky nil))
9622 (org-agenda-list nil (calendar-absolute-from-gregorian
9623 (calendar-cursor-to-date))
9624 nil)))
9626 (defun org-agenda-convert-date ()
9627 (interactive)
9628 (org-agenda-check-type t 'agenda 'timeline)
9629 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9630 date s)
9631 (unless day
9632 (user-error "Don't know which date to convert"))
9633 (setq date (calendar-gregorian-from-absolute day))
9634 (setq s (concat
9635 "Gregorian: " (calendar-date-string date) "\n"
9636 "ISO: " (calendar-iso-date-string date) "\n"
9637 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9638 "Julian: " (calendar-julian-date-string date) "\n"
9639 "Astron. JD: " (calendar-astro-date-string date)
9640 " (Julian date number at noon UTC)\n"
9641 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9642 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9643 "French: " (calendar-french-date-string date) "\n"
9644 "Bahá’í: " (calendar-bahai-date-string date) " (until sunset)\n"
9645 "Mayan: " (calendar-mayan-date-string date) "\n"
9646 "Coptic: " (calendar-coptic-date-string date) "\n"
9647 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9648 "Persian: " (calendar-persian-date-string date) "\n"
9649 "Chinese: " (calendar-chinese-date-string date) "\n"))
9650 (with-output-to-temp-buffer "*Dates*"
9651 (princ s))
9652 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9654 ;;; Bulk commands
9656 (defun org-agenda-bulk-marked-p ()
9657 "Non-nil when current entry is marked for bulk action."
9658 (eq (get-char-property (point-at-bol) 'type)
9659 'org-marked-entry-overlay))
9661 (defun org-agenda-bulk-mark (&optional arg)
9662 "Mark the entry at point for future bulk action."
9663 (interactive "p")
9664 (dotimes (i (or arg 1))
9665 (unless (org-get-at-bol 'org-agenda-diary-link)
9666 (let* ((m (org-get-at-bol 'org-hd-marker))
9668 (unless (org-agenda-bulk-marked-p)
9669 (unless m (user-error "Nothing to mark at point"))
9670 (push m org-agenda-bulk-marked-entries)
9671 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9672 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9673 (org-get-todo-face "TODO")
9674 'evaporate)
9675 (overlay-put ov 'type 'org-marked-entry-overlay))
9676 (end-of-line 1)
9677 (or (ignore-errors
9678 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9679 (beginning-of-line 2))
9680 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9681 (beginning-of-line 2))
9682 (message "%d entries marked for bulk action"
9683 (length org-agenda-bulk-marked-entries))))))
9685 (defun org-agenda-bulk-mark-all ()
9686 "Mark all entries for future agenda bulk action."
9687 (interactive)
9688 (org-agenda-bulk-mark-regexp "."))
9690 (defun org-agenda-bulk-mark-regexp (regexp)
9691 "Mark entries matching REGEXP for future agenda bulk action."
9692 (interactive "sMark entries matching regexp: ")
9693 (let ((entries-marked 0) txt-at-point)
9694 (save-excursion
9695 (goto-char (point-min))
9696 (goto-char (next-single-property-change (point) 'org-hd-marker))
9697 (while (and (re-search-forward regexp nil t)
9698 (setq txt-at-point (get-text-property (point) 'txt)))
9699 (if (get-char-property (point) 'invisible)
9700 (beginning-of-line 2)
9701 (when (string-match regexp txt-at-point)
9702 (setq entries-marked (1+ entries-marked))
9703 (call-interactively 'org-agenda-bulk-mark)))))
9705 (if (not entries-marked)
9706 (message "No entry matching this regexp."))))
9708 (defun org-agenda-bulk-unmark (&optional arg)
9709 "Unmark the entry at point for future bulk action."
9710 (interactive "P")
9711 (if arg
9712 (org-agenda-bulk-unmark-all)
9713 (cond ((org-agenda-bulk-marked-p)
9714 (org-agenda-bulk-remove-overlays
9715 (point-at-bol) (+ 2 (point-at-bol)))
9716 (setq org-agenda-bulk-marked-entries
9717 (delete (org-get-at-bol 'org-hd-marker)
9718 org-agenda-bulk-marked-entries))
9719 (end-of-line 1)
9720 (or (ignore-errors
9721 (goto-char (next-single-property-change (point) 'txt)))
9722 (beginning-of-line 2))
9723 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9724 (beginning-of-line 2))
9725 (message "%d entries left marked for bulk action"
9726 (length org-agenda-bulk-marked-entries)))
9727 (t (message "No entry to unmark here")))))
9729 (defun org-agenda-bulk-toggle-all ()
9730 "Toggle all marks for bulk action."
9731 (interactive)
9732 (save-excursion
9733 (goto-char (point-min))
9734 (while (ignore-errors
9735 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9736 (org-agenda-bulk-toggle))))
9738 (defun org-agenda-bulk-toggle ()
9739 "Toggle the mark at point for bulk action."
9740 (interactive)
9741 (if (org-agenda-bulk-marked-p)
9742 (org-agenda-bulk-unmark)
9743 (org-agenda-bulk-mark)))
9745 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9746 "Remove the mark overlays between BEG and END in the agenda buffer.
9747 BEG and END default to the buffer limits.
9749 This only removes the overlays, it does not remove the markers
9750 from the list in `org-agenda-bulk-marked-entries'."
9751 (interactive)
9752 (mapc (lambda (ov)
9753 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9754 (delete-overlay ov)))
9755 (overlays-in (or beg (point-min)) (or end (point-max)))))
9757 (defun org-agenda-bulk-unmark-all ()
9758 "Remove all marks in the agenda buffer.
9759 This will remove the markers and the overlays."
9760 (interactive)
9761 (if (null org-agenda-bulk-marked-entries)
9762 (message "No entry to unmark")
9763 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
9764 (setq org-agenda-bulk-marked-entries nil)
9765 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9767 (defcustom org-agenda-persistent-marks nil
9768 "Non-nil means marked items will stay marked after a bulk action.
9769 You can toggle this interactively by typing `p' when prompted for a
9770 bulk action."
9771 :group 'org-agenda
9772 :version "24.1"
9773 :type 'boolean)
9775 (defun org-agenda-bulk-action (&optional arg)
9776 "Execute an remote-editing action on all marked entries.
9777 The prefix arg is passed through to the command if possible."
9778 (interactive "P")
9779 ;; Make sure we have markers, and only valid ones
9780 (unless org-agenda-bulk-marked-entries (user-error "No entries are marked"))
9781 (mapc
9782 (lambda (m)
9783 (unless (and (markerp m)
9784 (marker-buffer m)
9785 (buffer-live-p (marker-buffer m))
9786 (marker-position m))
9787 (user-error "Marker %s for bulk command is invalid" m)))
9788 org-agenda-bulk-marked-entries)
9790 ;; Prompt for the bulk command
9791 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
9792 (message (concat msg "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9793 "[S]catter [f]unction "
9794 (when org-agenda-bulk-custom-functions
9795 (concat " Custom: ["
9796 (mapconcat (lambda(f) (char-to-string (car f)))
9797 org-agenda-bulk-custom-functions "")
9798 "]"))))
9799 (catch 'exit
9800 (let* ((action (read-char-exclusive))
9801 (org-log-refile (if org-log-refile 'time nil))
9802 (entries (reverse org-agenda-bulk-marked-entries))
9803 (org-overriding-default-time
9804 (if (get-text-property (point) 'org-agenda-date-header)
9805 (org-get-cursor-date)))
9806 redo-at-end
9807 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
9808 (cond
9809 ((equal action ?p)
9810 (let ((org-agenda-persistent-marks
9811 (not org-agenda-persistent-marks)))
9812 (org-agenda-bulk-action)
9813 (throw 'exit nil)))
9815 ((equal action ?$)
9816 (setq cmd '(org-agenda-archive)))
9818 ((equal action ?A)
9819 (setq cmd '(org-agenda-archive-to-archive-sibling)))
9821 ((member action '(?r ?w))
9822 (setq rfloc (org-refile-get-location
9823 "Refile to"
9824 (marker-buffer (car entries))
9825 org-refile-allow-creating-parent-nodes))
9826 (if (nth 3 rfloc)
9827 (setcar (nthcdr 3 rfloc)
9828 (move-marker (make-marker) (nth 3 rfloc)
9829 (or (get-file-buffer (nth 1 rfloc))
9830 (find-buffer-visiting (nth 1 rfloc))
9831 (error "This should not happen")))))
9833 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
9834 redo-at-end t))
9836 ((equal action ?t)
9837 (setq state (completing-read
9838 "Todo state: "
9839 (with-current-buffer (marker-buffer (car entries))
9840 (mapcar #'list org-todo-keywords-1))))
9841 (setq cmd `(let ((org-inhibit-blocking t)
9842 (org-inhibit-logging 'note))
9843 (org-agenda-todo ,state))))
9845 ((memq action '(?- ?+))
9846 (setq tag (completing-read
9847 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9848 (with-current-buffer (marker-buffer (car entries))
9849 (delq nil
9850 (mapcar (lambda (x) (and (stringp (car x)) x))
9851 org-current-tag-alist)))))
9852 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
9854 ((memq action '(?s ?d))
9855 (let* ((time
9856 (unless arg
9857 (org-read-date
9858 nil nil nil
9859 (if (eq action ?s) "(Re)Schedule to" "(Re)Set Deadline to")
9860 org-overriding-default-time)))
9861 (c1 (if (eq action ?s) 'org-agenda-schedule
9862 'org-agenda-deadline)))
9863 ;; Make sure to not prompt for a note when bulk
9864 ;; rescheduling as Org cannot cope with simultaneous Org.
9865 ;; Besides, it could be annoying depending on the number
9866 ;; of items re-scheduled.
9867 (setq cmd `(eval '(let ((org-log-reschedule
9868 (and org-log-reschedule 'time)))
9869 (,c1 arg ,time))))))
9871 ((equal action ?S)
9872 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
9873 (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
9874 (let ((days (read-number
9875 (format "Scatter tasks across how many %sdays: "
9876 (if arg "week" "")) 7)))
9877 (setq cmd
9878 `(let ((distance (1+ (random ,days))))
9879 (if arg
9880 (let ((dist distance)
9881 (day-of-week
9882 (calendar-day-of-week
9883 (calendar-gregorian-from-absolute (org-today)))))
9884 (dotimes (i (1+ dist))
9885 (while (member day-of-week org-agenda-weekend-days)
9886 (cl-incf distance)
9887 (cl-incf day-of-week)
9888 (when (= day-of-week 7)
9889 (setq day-of-week 0)))
9890 (cl-incf day-of-week)
9891 (when (= day-of-week 7)
9892 (setq day-of-week 0)))))
9893 ;; silently fail when try to replan a sexp entry
9894 (condition-case nil
9895 (let* ((date (calendar-gregorian-from-absolute
9896 (+ (org-today) distance)))
9897 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9898 (nth 2 date))))
9899 (org-agenda-schedule nil time))
9900 (error nil)))))))
9902 ((assoc action org-agenda-bulk-custom-functions)
9903 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
9904 redo-at-end t))
9906 ((equal action ?f)
9907 (setq cmd (list (intern
9908 (completing-read "Function: "
9909 obarray 'fboundp t nil nil)))))
9911 (t (user-error "Invalid bulk action")))
9913 ;; Sort the markers, to make sure that parents are handled before children
9914 (setq entries (sort entries
9915 (lambda (a b)
9916 (cond
9917 ((equal (marker-buffer a) (marker-buffer b))
9918 (< (marker-position a) (marker-position b)))
9920 (string< (buffer-name (marker-buffer a))
9921 (buffer-name (marker-buffer b))))))))
9923 ;; Now loop over all markers and apply cmd
9924 (while (setq e (pop entries))
9925 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
9926 (if (not pos)
9927 (progn (message "Skipping removed entry at %s" e)
9928 (setq cntskip (1+ cntskip)))
9929 (goto-char pos)
9930 (let (org-loop-over-headlines-in-active-region)
9931 (eval cmd))
9932 ;; `post-command-hook' is not run yet. We make sure any
9933 ;; pending log note is processed.
9934 (when (or (memq 'org-add-log-note (default-value 'post-command-hook))
9935 (memq 'org-add-log-note post-command-hook))
9936 (org-add-log-note))
9937 (setq cnt (1+ cnt))))
9938 (when redo-at-end (org-agenda-redo))
9939 (unless org-agenda-persistent-marks
9940 (org-agenda-bulk-unmark-all))
9941 (message "Acted on %d entries%s%s"
9943 (if (= cntskip 0)
9945 (format ", skipped %d (disappeared before their turn)"
9946 cntskip))
9947 (if (not org-agenda-persistent-marks)
9948 "" " (kept marked)"))))))
9950 (defun org-agenda-capture (&optional with-time)
9951 "Call `org-capture' with the date at point.
9952 With a `C-1' prefix, use the HH:MM value at point (if any) or the
9953 current HH:MM time."
9954 (interactive "P")
9955 (if (not (eq major-mode 'org-agenda-mode))
9956 (user-error "You cannot do this outside of agenda buffers")
9957 (let ((org-overriding-default-time
9958 (org-get-cursor-date (equal with-time 1))))
9959 (call-interactively 'org-capture))))
9961 ;;; Dragging agenda lines forward/backward
9963 (defun org-agenda-reapply-filters ()
9964 "Re-apply all agenda filters."
9965 (mapcar
9966 (lambda(f) (when (car f) (org-agenda-filter-apply (car f) (cadr f) t)))
9967 `((,org-agenda-tag-filter tag)
9968 (,org-agenda-category-filter category)
9969 (,org-agenda-regexp-filter regexp)
9970 (,org-agenda-effort-filter effort)
9971 (,(get 'org-agenda-tag-filter :preset-filter) tag)
9972 (,(get 'org-agenda-category-filter :preset-filter) category)
9973 (,(get 'org-agenda-effort-filter :preset-filter) effort)
9974 (,(get 'org-agenda-regexp-filter :preset-filter) regexp))))
9976 (defun org-agenda-drag-line-forward (arg &optional backward)
9977 "Drag an agenda line forward by ARG lines.
9978 When the optional argument `backward' is non-nil, move backward."
9979 (interactive "p")
9980 (let ((inhibit-read-only t) lst line)
9981 (if (or (not (get-text-property (point) 'txt))
9982 (save-excursion
9983 (dotimes (n arg)
9984 (move-beginning-of-line (if backward 0 2))
9985 (push (not (get-text-property (point) 'txt)) lst))
9986 (delq nil lst)))
9987 (message "Cannot move line forward")
9988 (let ((end (save-excursion (move-beginning-of-line 2) (point))))
9989 (move-beginning-of-line 1)
9990 (setq line (buffer-substring (point) end))
9991 (delete-region (point) end)
9992 (move-beginning-of-line (funcall (if backward '1- '1+) arg))
9993 (insert line)
9994 (org-agenda-reapply-filters)
9995 (org-agenda-mark-clocking-task)
9996 (move-beginning-of-line 0)))))
9998 (defun org-agenda-drag-line-backward (arg)
9999 "Drag an agenda line backward by ARG lines."
10000 (interactive "p")
10001 (org-agenda-drag-line-forward arg t))
10003 ;;; Flagging notes
10005 (defun org-agenda-show-the-flagging-note ()
10006 "Display the flagging note in the other window.
10007 When called a second time in direct sequence, offer to remove the FLAGGING
10008 tag and (if present) the flagging note."
10009 (interactive)
10010 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
10011 (win (selected-window))
10012 note heading newhead)
10013 (unless hdmarker
10014 (user-error "No linked entry at point"))
10015 (if (and (eq this-command last-command)
10016 (y-or-n-p "Unflag and remove any flagging note? "))
10017 (progn
10018 (org-agenda-remove-flag hdmarker)
10019 (let ((win (get-buffer-window "*Flagging Note*")))
10020 (and win (delete-window win)))
10021 (message "Entry unflagged"))
10022 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
10023 (unless note
10024 (user-error "No flagging note"))
10025 (org-kill-new note)
10026 (org-switch-to-buffer-other-window "*Flagging Note*")
10027 (erase-buffer)
10028 (insert note)
10029 (goto-char (point-min))
10030 (while (re-search-forward "\\\\n" nil t)
10031 (replace-match "\n" t t))
10032 (goto-char (point-min))
10033 (select-window win)
10034 (message "%s" (substitute-command-keys "Flagging note pushed to \
10035 kill ring. Press \\[org-agenda-show-the-flagging-note] again to remove \
10036 tag and note")))))
10038 (defun org-agenda-remove-flag (marker)
10039 "Remove the FLAGGED tag and any flagging note in the entry."
10040 (let (newhead)
10041 (org-with-point-at marker
10042 (org-toggle-tag "FLAGGED" 'off)
10043 (org-entry-delete nil "THEFLAGGINGNOTE")
10044 (setq newhead (org-get-heading)))
10045 (org-agenda-change-all-lines newhead marker)
10046 (message "Entry unflagged")))
10048 (defun org-agenda-get-any-marker (&optional pos)
10049 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
10050 (get-text-property (or pos (point-at-bol)) 'org-marker)))
10052 ;;; Appointment reminders
10054 (defvar appt-time-msg-list) ; defined in appt.el
10056 ;;;###autoload
10057 (defun org-agenda-to-appt (&optional refresh filter &rest args)
10058 "Activate appointments found in `org-agenda-files'.
10059 With a \\[universal-argument] prefix, refresh the list of
10060 appointments.
10062 If FILTER is t, interactively prompt the user for a regular
10063 expression, and filter out entries that don't match it.
10065 If FILTER is a string, use this string as a regular expression
10066 for filtering entries out.
10068 If FILTER is a function, filter out entries against which
10069 calling the function returns nil. This function takes one
10070 argument: an entry from `org-agenda-get-day-entries'.
10072 FILTER can also be an alist with the car of each cell being
10073 either `headline' or `category'. For example:
10075 \\='((headline \"IMPORTANT\")
10076 (category \"Work\"))
10078 will only add headlines containing IMPORTANT or headlines
10079 belonging to the \"Work\" category.
10081 ARGS are symbols indicating what kind of entries to consider.
10082 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
10083 \(i.e., deadlines and scheduled items with a hh:mm specification)
10084 and :timestamp entries. See the docstring of `org-diary' for
10085 details and examples.
10087 If an entry has a APPT_WARNTIME property, its value will be used
10088 to override `appt-message-warning-time'."
10089 (interactive "P")
10090 (if refresh (setq appt-time-msg-list nil))
10091 (if (eq filter t)
10092 (setq filter (read-from-minibuffer "Regexp filter: ")))
10093 (let* ((cnt 0) ; count added events
10094 (scope (or args '(:deadline* :scheduled* :timestamp)))
10095 (org-agenda-new-buffers nil)
10096 (org-deadline-warning-days 0)
10097 ;; Do not use `org-today' here because appt only takes
10098 ;; time and without date as argument, so it may pass wrong
10099 ;; information otherwise
10100 (today (org-date-to-gregorian
10101 (time-to-days (current-time))))
10102 (org-agenda-restrict nil)
10103 (files (org-agenda-files 'unrestricted)) entries file
10104 (org-agenda-buffer nil))
10105 ;; Get all entries which may contain an appt
10106 (org-agenda-prepare-buffers files)
10107 (while (setq file (pop files))
10108 (setq entries
10109 (delq nil
10110 (append entries
10111 (apply 'org-agenda-get-day-entries
10112 file today scope)))))
10113 ;; Map thru entries and find if we should filter them out
10114 (mapc
10115 (lambda (x)
10116 (let* ((evt (org-trim
10117 (replace-regexp-in-string
10118 org-bracket-link-regexp "\\3"
10119 (or (get-text-property 1 'txt x) ""))))
10120 (cat (get-text-property (1- (length x)) 'org-category x))
10121 (tod (get-text-property 1 'time-of-day x))
10122 (ok (or (null filter)
10123 (and (stringp filter) (string-match filter evt))
10124 (and (functionp filter) (funcall filter x))
10125 (and (listp filter)
10126 (let ((cat-filter (cadr (assq 'category filter)))
10127 (evt-filter (cadr (assq 'headline filter))))
10128 (or (and (stringp cat-filter)
10129 (string-match cat-filter cat))
10130 (and (stringp evt-filter)
10131 (string-match evt-filter evt)))))))
10132 (wrn (get-text-property 1 'warntime x)))
10133 ;; FIXME: Shall we remove text-properties for the appt text?
10134 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
10135 (when (and ok tod (not (string-match "\\`DONE\\|CANCELLED" evt)))
10136 (setq tod (concat "00" (number-to-string tod)))
10137 (setq tod (when (string-match
10138 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
10139 (concat (match-string 1 tod) ":"
10140 (match-string 2 tod))))
10141 (when (if (version< emacs-version "23.3")
10142 (appt-add tod evt)
10143 (appt-add tod evt wrn))
10144 (setq cnt (1+ cnt))))))
10145 entries)
10146 (org-release-buffers org-agenda-new-buffers)
10147 (if (eq cnt 0)
10148 (message "No event to add")
10149 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
10151 (defun org-agenda-today-p (date)
10152 "Non nil when DATE means today.
10153 DATE is either a list of the form (month day year) or a number of
10154 days as returned by `calendar-absolute-from-gregorian' or
10155 `org-today'. This function considers `org-extend-today-until'
10156 when defining today."
10157 (eq (org-today)
10158 (if (consp date) (calendar-absolute-from-gregorian date) date)))
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* ((org-use-effective-time t)
10164 (hour (nth 2 (decode-time (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