lisp/org-table.el: fix table alignment
[org-mode/org-tableheadings.git] / lisp / org-agenda.el
blobfbf520c5e61cfae17c751dc3249b5f6ff45fd274
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004-2019 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: https://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 <https://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file contains the code for creating and using the Agenda for Org.
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 'ol)
50 (require 'org)
51 (require 'org-macs)
53 (declare-function diary-add-to-list "diary-lib"
54 (date string specifier &optional marker globcolor literal))
55 (declare-function calendar-iso-to-absolute "cal-iso" (date))
56 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
57 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
58 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
59 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
60 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
61 (declare-function calendar-french-date-string "cal-french" (&optional date))
62 (declare-function calendar-goto-date "cal-move" (date))
63 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
64 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
65 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
66 (declare-function calendar-iso-from-absolute "cal-iso" (date))
67 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
68 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
69 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
70 (declare-function calendar-check-holidays "holidays" (date))
72 (declare-function org-columns-remove-overlays "org-colview" ())
73 (declare-function org-datetree-find-date-create "org-datetree"
74 (date &optional keep-restriction))
75 (declare-function org-columns-quit "org-colview" ())
76 (declare-function diary-date-display-form "diary-lib" (&optional type))
77 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
78 (declare-function org-habit-insert-consistency-graphs
79 "org-habit" (&optional line))
80 (declare-function org-is-habit-p "org-habit" (&optional pom))
81 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
82 (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
83 (declare-function org-agenda-columns "org-colview" ())
84 (declare-function org-add-archive-files "org-archive" (files))
85 (declare-function org-capture "org-capture" (&optional goto keys))
87 (defvar calendar-mode-map)
88 (defvar org-clock-current-task)
89 (defvar org-current-tag-alist)
90 (defvar org-mobile-force-id-on-agenda-items)
91 (defvar org-habit-show-habits)
92 (defvar org-habit-show-habits-only-for-today)
93 (defvar org-habit-show-all-today)
94 (defvar org-habit-scheduled-past-days)
96 ;; Defined somewhere in this file, but used before definition.
97 (defvar org-agenda-buffer-name "*Org Agenda*")
98 (defvar org-agenda-overriding-header nil)
99 (defvar org-agenda-title-append nil)
100 (with-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
101 (with-no-warnings (defvar date)) ;; unprefixed, from calendar.el
102 (defvar original-date) ; dynamically scoped, calendar.el does scope this
104 (defvar org-agenda-undo-list nil
105 "List of undoable operations in the agenda since last refresh.")
106 (defvar org-agenda-pending-undo-list nil
107 "In a series of undo commands, this is the list of remaining undo items.")
109 (defcustom org-agenda-confirm-kill 1
110 "When set, remote killing from the agenda buffer needs confirmation.
111 When t, a confirmation is always needed. When a number N, confirmation is
112 only needed when the text to be killed contains more than N non-white lines."
113 :group 'org-agenda
114 :type '(choice
115 (const :tag "Never" nil)
116 (const :tag "Always" t)
117 (integer :tag "When more than N lines")))
119 (defcustom org-agenda-compact-blocks nil
120 "Non-nil means make the block agenda more compact.
121 This is done globally by leaving out lines like the agenda span
122 name and week number or the separator lines."
123 :group 'org-agenda
124 :type 'boolean)
126 (defcustom org-agenda-block-separator ?=
127 "The separator between blocks in the agenda.
128 If this is a string, it will be used as the separator, with a newline added.
129 If it is a character, it will be repeated to fill the window width.
130 If nil the separator is disabled. In `org-agenda-custom-commands' this
131 addresses the separator between the current and the previous block."
132 :group 'org-agenda
133 :type '(choice
134 (const :tag "Disabled" nil)
135 (character)
136 (string)))
138 (defgroup org-agenda-export nil
139 "Options concerning exporting agenda views in Org mode."
140 :tag "Org Agenda Export"
141 :group 'org-agenda)
143 (defcustom org-agenda-with-colors t
144 "Non-nil means use colors in agenda views."
145 :group 'org-agenda-export
146 :type 'boolean)
148 (defcustom org-agenda-exporter-settings nil
149 "Alist of variable/value pairs that should be active during agenda export.
150 This is a good place to set options for ps-print and for htmlize.
151 Note that the way this is implemented, the values will be evaluated
152 before assigned to the variables. So make sure to quote values you do
153 *not* want evaluated, for example
155 (setq org-agenda-exporter-settings
156 \\='((ps-print-color-p \\='black-white)))"
157 :group 'org-agenda-export
158 :type '(repeat
159 (list
160 (variable)
161 (sexp :tag "Value"))))
163 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
164 "Hook run in a temporary buffer before writing the agenda to an export file.
165 A useful function for this hook is `org-agenda-add-entry-text'."
166 :group 'org-agenda-export
167 :type 'hook
168 :options '(org-agenda-add-entry-text))
170 (defcustom org-agenda-add-entry-text-maxlines 0
171 "Maximum number of entry text lines to be added to agenda.
172 This is only relevant when `org-agenda-add-entry-text' is part of
173 `org-agenda-before-write-hook', which is the default.
174 When this is 0, nothing will happen. When it is greater than 0, it
175 specifies the maximum number of lines that will be added for each entry
176 that is listed in the agenda view.
178 Note that this variable is not used during display, only when exporting
179 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
180 and `org-agenda-entry-text-maxlines'."
181 :group 'org-agenda
182 :type 'integer)
184 (defcustom org-agenda-add-entry-text-descriptive-links t
185 "Non-nil means export org-links as descriptive links in agenda added text.
186 This variable applies to the text added to the agenda when
187 `org-agenda-add-entry-text-maxlines' is larger than 0.
188 When this variable nil, the URL will (also) be shown."
189 :group 'org-agenda
190 :type 'boolean)
192 (defcustom org-agenda-export-html-style nil
193 "The style specification for exported HTML Agenda files.
194 If this variable contains a string, it will replace the default <style>
195 section as produced by `htmlize'.
196 Since there are different ways of setting style information, this variable
197 needs to contain the full HTML structure to provide a style, including the
198 surrounding HTML tags. The style specifications should include definitions
199 the fonts used by the agenda, here is an example:
201 <style type=\"text/css\">
202 p { font-weight: normal; color: gray; }
203 .org-agenda-structure {
204 font-size: 110%;
205 color: #003399;
206 font-weight: 600;
208 .org-todo {
209 color: #cc6666;
210 font-weight: bold;
212 .org-agenda-done {
213 color: #339933;
215 .org-done {
216 color: #339933;
218 .title { text-align: center; }
219 .todo, .deadline { color: red; }
220 .done { color: green; }
221 </style>
223 or, if you want to keep the style in a file,
225 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
227 As the value of this option simply gets inserted into the HTML <head> header,
228 you can \"misuse\" it to also add other text to the header."
229 :group 'org-agenda-export
230 :group 'org-export-html
231 :type '(choice
232 (const nil)
233 (string)))
235 (defcustom org-agenda-persistent-filter nil
236 "When set, keep filters from one agenda view to the next."
237 :group 'org-agenda
238 :type 'boolean)
240 (defgroup org-agenda-custom-commands nil
241 "Options concerning agenda views in Org mode."
242 :tag "Org Agenda Custom Commands"
243 :group 'org-agenda)
245 (defconst org-sorting-choice
246 '(choice
247 (const time-up) (const time-down)
248 (const timestamp-up) (const timestamp-down)
249 (const scheduled-up) (const scheduled-down)
250 (const deadline-up) (const deadline-down)
251 (const ts-up) (const ts-down)
252 (const tsia-up) (const tsia-down)
253 (const category-keep) (const category-up) (const category-down)
254 (const tag-down) (const tag-up)
255 (const priority-up) (const priority-down)
256 (const todo-state-up) (const todo-state-down)
257 (const effort-up) (const effort-down)
258 (const habit-up) (const habit-down)
259 (const alpha-up) (const alpha-down)
260 (const user-defined-up) (const user-defined-down))
261 "Sorting choices.")
263 ;; Keep custom values for `org-agenda-filter-preset' compatible with
264 ;; the new variable `org-agenda-tag-filter-preset'.
265 (defvaralias 'org-agenda-filter-preset 'org-agenda-tag-filter-preset)
266 (defvaralias 'org-agenda-filter 'org-agenda-tag-filter)
268 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
269 "List of types searched for when creating the daily/weekly agenda.
270 This variable is a list of symbols that controls the types of
271 items that appear in the daily/weekly agenda. Allowed symbols in this
272 list are are
274 :timestamp List items containing a date stamp or date range matching
275 the selected date. This includes sexp entries in angular
276 brackets.
278 :sexp List entries resulting from plain diary-like sexps.
280 :deadline List deadline due on that date. When the date is today,
281 also list any deadlines past due, or due within
282 `org-deadline-warning-days'.
284 :deadline* Same as above, but only include the deadline if it has an
285 hour specification as [h]h:mm.
287 :scheduled List all items which are scheduled for the given date.
288 The diary for *today* also contains items which were
289 scheduled earlier and are not yet marked DONE.
291 :scheduled* Same as above, but only include the scheduled item if it
292 has an hour specification as [h]h:mm.
294 By default, all four non-starred types are turned on.
296 When :scheduled* or :deadline* are included, :schedule or :deadline
297 will be ignored.
299 Never set this variable globally using `setq', because then it
300 will apply to all future agenda commands. Instead, bind it with
301 `let' to scope it dynamically into the agenda-constructing
302 command. A good way to set it is through options in
303 `org-agenda-custom-commands'. For a more flexible (though
304 somewhat less efficient) way of determining what is included in
305 the daily/weekly agenda, see `org-agenda-skip-function'.")
307 (defconst org-agenda-custom-commands-local-options
308 `(repeat :tag "Local settings for this command. Remember to quote values"
309 (choice :tag "Setting"
310 (list :tag "Heading for this block"
311 (const org-agenda-overriding-header)
312 (string :tag "Headline"))
313 (list :tag "Files to be searched"
314 (const org-agenda-files)
315 (list
316 (const :format "" quote)
317 (repeat (file))))
318 (list :tag "Sorting strategy"
319 (const org-agenda-sorting-strategy)
320 (list
321 (const :format "" quote)
322 (repeat
323 ,org-sorting-choice)))
324 (list :tag "Prefix format"
325 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
326 (string))
327 (list :tag "Number of days in agenda"
328 (const org-agenda-span)
329 (list
330 (const :format "" quote)
331 (choice (const :tag "Day" day)
332 (const :tag "Week" week)
333 (const :tag "Fortnight" fortnight)
334 (const :tag "Month" month)
335 (const :tag "Year" year)
336 (integer :tag "Custom"))))
337 (list :tag "Fixed starting date"
338 (const org-agenda-start-day)
339 (string :value "2007-11-01"))
340 (list :tag "Start on day of week"
341 (const org-agenda-start-on-weekday)
342 (choice :value 1
343 (const :tag "Today" nil)
344 (integer :tag "Weekday No.")))
345 (list :tag "Include data from diary"
346 (const org-agenda-include-diary)
347 (boolean))
348 (list :tag "Deadline Warning days"
349 (const org-deadline-warning-days)
350 (integer :value 1))
351 (list :tag "Category filter preset"
352 (const org-agenda-category-filter-preset)
353 (list
354 (const :format "" quote)
355 (repeat
356 (string :tag "+category or -category"))))
357 (list :tag "Tags filter preset"
358 (const org-agenda-tag-filter-preset)
359 (list
360 (const :format "" quote)
361 (repeat
362 (string :tag "+tag or -tag"))))
363 (list :tag "Effort filter preset"
364 (const org-agenda-effort-filter-preset)
365 (list
366 (const :format "" quote)
367 (repeat
368 (string :tag "+=10 or -=10 or +<10 or ->10"))))
369 (list :tag "Regexp filter preset"
370 (const org-agenda-regexp-filter-preset)
371 (list
372 (const :format "" quote)
373 (repeat
374 (string :tag "+regexp or -regexp"))))
375 (list :tag "Set daily/weekly entry types"
376 (const org-agenda-entry-types)
377 (list
378 (const :format "" quote)
379 (set :greedy t :value ,org-agenda-entry-types
380 (const :deadline)
381 (const :scheduled)
382 (const :deadline*)
383 (const :scheduled*)
384 (const :timestamp)
385 (const :sexp))))
386 (list :tag "Standard skipping condition"
387 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
388 (const org-agenda-skip-function)
389 (list
390 (const :format "" quote)
391 (list
392 (choice
393 :tag "Skipping range"
394 (const :tag "Skip entry" org-agenda-skip-entry-if)
395 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
396 (repeat :inline t :tag "Conditions for skipping"
397 (choice
398 :tag "Condition type"
399 (list :tag "Regexp matches" :inline t
400 (const :format "" regexp)
401 (regexp))
402 (list :tag "Regexp does not match" :inline t
403 (const :format "" notregexp)
404 (regexp))
405 (list :tag "TODO state is" :inline t
406 (const todo)
407 (choice
408 (const :tag "Any not-done state" todo)
409 (const :tag "Any done state" done)
410 (const :tag "Any state" any)
411 (list :tag "Keyword list"
412 (const :format "" quote)
413 (repeat (string :tag "Keyword")))))
414 (list :tag "TODO state is not" :inline t
415 (const nottodo)
416 (choice
417 (const :tag "Any not-done state" todo)
418 (const :tag "Any done state" done)
419 (const :tag "Any state" any)
420 (list :tag "Keyword list"
421 (const :format "" quote)
422 (repeat (string :tag "Keyword")))))
423 (const :tag "scheduled" scheduled)
424 (const :tag "not scheduled" notscheduled)
425 (const :tag "deadline" deadline)
426 (const :tag "no deadline" notdeadline)
427 (const :tag "timestamp" timestamp)
428 (const :tag "no timestamp" nottimestamp))))))
429 (list :tag "Non-standard skipping condition"
430 :value (org-agenda-skip-function)
431 (const org-agenda-skip-function)
432 (sexp :tag "Function or form (quoted!)"))
433 (list :tag "Any variable"
434 (variable :tag "Variable")
435 (sexp :tag "Value (sexp)"))))
436 "Selection of examples for agenda command settings.
437 This will be spliced into the custom type of
438 `org-agenda-custom-commands'.")
441 (defcustom org-agenda-custom-commands
442 '(("n" "Agenda and all TODOs" ((agenda "") (alltodo ""))))
443 "Custom commands for the agenda.
444 \\<org-mode-map>
445 These commands will be offered on the splash screen displayed by the
446 agenda dispatcher `\\[org-agenda]'. Each entry is a list like this:
448 (key desc type match settings files)
450 key The key (one or more characters as a string) to be associated
451 with the command.
452 desc A description of the command, when omitted or nil, a default
453 description is built using MATCH.
454 type The command type, any of the following symbols:
455 agenda The daily/weekly agenda.
456 todo Entries with a specific TODO keyword, in all agenda files.
457 search Entries containing search words entry or headline.
458 tags Tags/Property/TODO match in all agenda files.
459 tags-todo Tags/P/T match in all agenda files, TODO entries only.
460 todo-tree Sparse tree of specific TODO keyword in *current* file.
461 tags-tree Sparse tree with all tags matches in *current* file.
462 occur-tree Occur sparse tree for *current* file.
463 ... A user-defined function.
464 match What to search for:
465 - a single keyword for TODO keyword searches
466 - a tags match expression for tags searches
467 - a word search expression for text searches.
468 - a regular expression for occur searches
469 For all other commands, this should be the empty string.
470 settings A list of option settings, similar to that in a let form, so like
471 this: ((opt1 val1) (opt2 val2) ...). The values will be
472 evaluated at the moment of execution, so quote them when needed.
473 files A list of files to write the produced agenda buffer to with
474 the command `org-store-agenda-views'.
475 If a file name ends in \".html\", an HTML version of the buffer
476 is written out. If it ends in \".ps\", a postscript version is
477 produced. Otherwise, only the plain text is written to the file.
479 You can also define a set of commands, to create a composite agenda buffer.
480 In this case, an entry looks like this:
482 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
484 where
486 desc A description string to be displayed in the dispatcher menu.
487 cmd An agenda command, similar to the above. However, tree commands
488 are not allowed, but instead you can get agenda and global todo list.
489 So valid commands for a set are:
490 (agenda \"\" settings)
491 (alltodo \"\" settings)
492 (stuck \"\" settings)
493 (todo \"match\" settings files)
494 (search \"match\" settings files)
495 (tags \"match\" settings files)
496 (tags-todo \"match\" settings files)
498 Each command can carry a list of options, and another set of options can be
499 given for the whole set of commands. Individual command options take
500 precedence over the general options.
502 When using several characters as key to a command, the first characters
503 are prefix commands. For the dispatcher to display useful information, you
504 should provide a description for the prefix, like
506 (setq org-agenda-custom-commands
507 \\='((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
508 (\"hl\" tags \"+HOME+Lisa\")
509 (\"hp\" tags \"+HOME+Peter\")
510 (\"hk\" tags \"+HOME+Kim\")))"
511 :group 'org-agenda-custom-commands
512 :type `(repeat
513 (choice :value ("x" "Describe command here" tags "" nil)
514 (list :tag "Single command"
515 (string :tag "Access Key(s) ")
516 (option (string :tag "Description"))
517 (choice
518 (const :tag "Agenda" agenda)
519 (const :tag "TODO list" alltodo)
520 (const :tag "Search words" search)
521 (const :tag "Stuck projects" stuck)
522 (const :tag "Tags/Property match (all agenda files)" tags)
523 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
524 (const :tag "TODO keyword search (all agenda files)" todo)
525 (const :tag "Tags sparse tree (current buffer)" tags-tree)
526 (const :tag "TODO keyword tree (current buffer)" todo-tree)
527 (const :tag "Occur tree (current buffer)" occur-tree)
528 (sexp :tag "Other, user-defined function"))
529 (string :tag "Match (only for some commands)")
530 ,org-agenda-custom-commands-local-options
531 (option (repeat :tag "Export" (file :tag "Export to"))))
532 (list :tag "Command series, all agenda files"
533 (string :tag "Access Key(s)")
534 (string :tag "Description ")
535 (repeat :tag "Component"
536 (choice
537 (list :tag "Agenda"
538 (const :format "" agenda)
539 (const :tag "" :format "" "")
540 ,org-agenda-custom-commands-local-options)
541 (list :tag "TODO list (all keywords)"
542 (const :format "" alltodo)
543 (const :tag "" :format "" "")
544 ,org-agenda-custom-commands-local-options)
545 (list :tag "Search words"
546 (const :format "" search)
547 (string :tag "Match")
548 ,org-agenda-custom-commands-local-options)
549 (list :tag "Stuck projects"
550 (const :format "" stuck)
551 (const :tag "" :format "" "")
552 ,org-agenda-custom-commands-local-options)
553 (list :tag "Tags search"
554 (const :format "" tags)
555 (string :tag "Match")
556 ,org-agenda-custom-commands-local-options)
557 (list :tag "Tags search, TODO entries only"
558 (const :format "" tags-todo)
559 (string :tag "Match")
560 ,org-agenda-custom-commands-local-options)
561 (list :tag "TODO keyword search"
562 (const :format "" todo)
563 (string :tag "Match")
564 ,org-agenda-custom-commands-local-options)
565 (list :tag "Other, user-defined function"
566 (symbol :tag "function")
567 (string :tag "Match")
568 ,org-agenda-custom-commands-local-options)))
570 (repeat :tag "Settings for entire command set"
571 (list (variable :tag "Any variable")
572 (sexp :tag "Value")))
573 (option (repeat :tag "Export" (file :tag "Export to"))))
574 (cons :tag "Prefix key documentation"
575 (string :tag "Access Key(s)")
576 (string :tag "Description ")))))
578 (defcustom org-agenda-query-register ?o
579 "The register holding the current query string.
580 The purpose of this is that if you construct a query string interactively,
581 you can then use it to define a custom command."
582 :group 'org-agenda-custom-commands
583 :type 'character)
585 (defcustom org-stuck-projects
586 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
587 "How to identify stuck projects.
588 This is a list of four items:
589 1. A tags/todo/property matcher string that is used to identify a project.
590 See the manual for a description of tag and property searches.
591 The entire tree below a headline matched by this is considered one project.
592 2. A list of TODO keywords identifying non-stuck projects.
593 If the project subtree contains any headline with one of these todo
594 keywords, the project is considered to be not stuck. If you specify
595 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
596 3. A list of tags identifying non-stuck projects.
597 If the project subtree contains any headline with one of these tags,
598 the project is considered to be not stuck. If you specify \"*\" as
599 a tag, any tag will mark the project unstuck. Note that this is about
600 the explicit presence of a tag somewhere in the subtree, inherited
601 tags do not count here. If inherited tags make a project not stuck,
602 use \"-TAG\" in the tags part of the matcher under (1.) above.
603 4. An arbitrary regular expression matching non-stuck projects.
605 If the project turns out to be not stuck, search continues also in the
606 subtree to see if any of the subtasks have project status.
608 See also the variable `org-tags-match-list-sublevels' which applies
609 to projects matched by this search as well.
611 After defining this variable, you may use `org-agenda-list-stuck-projects'
612 \(bound to `\\[org-agenda] #') to produce the list."
613 :group 'org-agenda-custom-commands
614 :type '(list
615 (string :tag "Tags/TODO match to identify a project")
616 (repeat :tag "Projects are *not* stuck if they have an entry with \
617 TODO keyword any of" (string))
618 (repeat :tag "Projects are *not* stuck if they have an entry with \
619 TAG being any of" (string))
620 (regexp :tag "Projects are *not* stuck if this regexp matches inside \
621 the subtree")))
623 (defgroup org-agenda-skip nil
624 "Options concerning skipping parts of agenda files."
625 :tag "Org Agenda Skip"
626 :group 'org-agenda)
628 (defcustom org-agenda-skip-function-global nil
629 "Function to be called at each match during agenda construction.
630 If this function returns nil, the current match should not be skipped.
631 If the function decided to skip an agenda match, is must return the
632 buffer position from which the search should be continued.
633 This may also be a Lisp form, which will be evaluated.
635 This variable will be applied to every agenda match, including
636 tags/property searches and TODO lists. So try to make the test function
637 do its checking as efficiently as possible. To implement a skipping
638 condition just for specific agenda commands, use the variable
639 `org-agenda-skip-function' which can be set in the options section
640 of custom agenda commands."
641 :group 'org-agenda-skip
642 :type 'sexp)
644 (defgroup org-agenda-daily/weekly nil
645 "Options concerning the daily/weekly agenda."
646 :tag "Org Agenda Daily/Weekly"
647 :group 'org-agenda)
648 (defgroup org-agenda-todo-list nil
649 "Options concerning the global todo list agenda view."
650 :tag "Org Agenda Todo List"
651 :group 'org-agenda)
652 (defgroup org-agenda-match-view nil
653 "Options concerning the general tags/property/todo match agenda view."
654 :tag "Org Agenda Match View"
655 :group 'org-agenda)
656 (defgroup org-agenda-search-view nil
657 "Options concerning the search agenda view."
658 :tag "Org Agenda Search View"
659 :group 'org-agenda)
661 (defvar org-agenda-archives-mode nil
662 "Non-nil means the agenda will include archived items.
663 If this is the symbol `trees', trees in the selected agenda scope
664 that are marked with the ARCHIVE tag will be included anyway. When this is
665 t, also all archive files associated with the current selection of agenda
666 files will be included.")
668 (defcustom org-agenda-restriction-lock-highlight-subtree t
669 "Non-nil means highlight the whole subtree when restriction is active.
670 Otherwise only highlight the headline. Highlighting the whole subtree is
671 useful to ensure no edits happen beyond the restricted region."
672 :group 'org-agenda
673 :type 'boolean)
675 (defcustom org-agenda-skip-comment-trees t
676 "Non-nil means skip trees that start with the COMMENT keyword.
677 When nil, these trees are also scanned by agenda commands."
678 :group 'org-agenda-skip
679 :type 'boolean)
681 (defcustom org-agenda-todo-list-sublevels t
682 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
683 When nil, the sublevels of a TODO entry are not checked, resulting in
684 potentially much shorter TODO lists."
685 :group 'org-agenda-skip
686 :group 'org-agenda-todo-list
687 :type 'boolean)
689 (defcustom org-agenda-todo-ignore-with-date nil
690 "Non-nil means don't show entries with a date in the global todo list.
691 You can use this if you prefer to mark mere appointments with a TODO keyword,
692 but don't want them to show up in the TODO list.
693 When this is set, it also covers deadlines and scheduled items, the settings
694 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
695 will be ignored.
696 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
697 :group 'org-agenda-skip
698 :group 'org-agenda-todo-list
699 :type 'boolean)
701 (defcustom org-agenda-todo-ignore-timestamp nil
702 "Non-nil means don't show entries with a timestamp.
703 This applies when creating the global todo list.
704 Valid values are:
706 past Don't show entries for today or in the past.
708 future Don't show entries with a timestamp in the future.
709 The idea behind this is that if it has a future
710 timestamp, you don't want to think about it until the
711 date.
713 all Don't show any entries with a timestamp in the global todo list.
714 The idea behind this is that by setting a timestamp, you
715 have already \"taken care\" of this item.
717 This variable can also have an integer as a value. If positive (N),
718 todos with a timestamp N or more days in the future will be ignored. If
719 negative (-N), todos with a timestamp N or more days in the past will be
720 ignored. If 0, todos with a timestamp either today or in the future will
721 be ignored. For example, a value of -1 will exclude todos with a
722 timestamp in the past (yesterday or earlier), while a value of 7 will
723 exclude todos with a timestamp a week or more in the future.
725 See also `org-agenda-todo-ignore-with-date'.
726 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
727 to make his option also apply to the tags-todo list."
728 :group 'org-agenda-skip
729 :group 'org-agenda-todo-list
730 :version "24.1"
731 :type '(choice
732 (const :tag "Ignore future timestamp todos" future)
733 (const :tag "Ignore past or present timestamp todos" past)
734 (const :tag "Ignore all timestamp todos" all)
735 (const :tag "Show timestamp todos" nil)
736 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
738 (defcustom org-agenda-todo-ignore-scheduled nil
739 "Non-nil means, ignore some scheduled TODO items when making TODO list.
740 This applies when creating the global todo list.
741 Valid values are:
743 past Don't show entries scheduled today or in the past.
745 future Don't show entries scheduled in the future.
746 The idea behind this is that by scheduling it, you don't want to
747 think about it until the scheduled date.
749 all Don't show any scheduled entries in the global todo list.
750 The idea behind this is that by scheduling it, you have already
751 \"taken care\" of this item.
753 t Same as `all', for backward compatibility.
755 This variable can also have an integer as a value. See
756 `org-agenda-todo-ignore-timestamp' for more details.
758 See also `org-agenda-todo-ignore-with-date'.
759 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
760 to make his option also apply to the tags-todo list."
761 :group 'org-agenda-skip
762 :group 'org-agenda-todo-list
763 :type '(choice
764 (const :tag "Ignore future-scheduled todos" future)
765 (const :tag "Ignore past- or present-scheduled todos" past)
766 (const :tag "Ignore all scheduled todos" all)
767 (const :tag "Ignore all scheduled todos (compatibility)" t)
768 (const :tag "Show scheduled todos" nil)
769 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
771 (defcustom org-agenda-todo-ignore-deadlines nil
772 "Non-nil means ignore some deadline TODO items when making TODO list.
774 There are different motivations for using different values, please think
775 carefully when configuring this variable.
777 This applies when creating the global TODO list.
779 Valid values are:
781 near Don't show near deadline entries. A deadline is near when it is
782 closer than `org-deadline-warning-days' days. The idea behind this
783 is that such items will appear in the agenda anyway.
785 far Don't show TODO entries where a deadline has been defined, but
786 is not going to happen anytime soon. This is useful if you want to use
787 the TODO list to figure out what to do now.
789 past Don't show entries with a deadline timestamp for today or in the past.
791 future Don't show entries with a deadline timestamp in the future, not even
792 when they become `near' ones. Use it with caution.
794 all Ignore all TODO entries that do have a deadline.
796 t Same as `near', for backward compatibility.
798 This variable can also have an integer as a value. See
799 `org-agenda-todo-ignore-timestamp' for more details.
801 See also `org-agenda-todo-ignore-with-date'.
802 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
803 to make his option also apply to the tags-todo list."
804 :group 'org-agenda-skip
805 :group 'org-agenda-todo-list
806 :type '(choice
807 (const :tag "Ignore near deadlines" near)
808 (const :tag "Ignore near deadlines (compatibility)" t)
809 (const :tag "Ignore far deadlines" far)
810 (const :tag "Ignore all TODOs with a deadlines" all)
811 (const :tag "Show all TODOs, even if they have a deadline" nil)
812 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
814 (defcustom org-agenda-todo-ignore-time-comparison-use-seconds nil
815 "Time unit to use when possibly ignoring an agenda item.
817 See the docstring of various `org-agenda-todo-ignore-*' options.
818 The default is to compare time stamps using days. An item is thus
819 considered to be in the future if it is at least one day after today.
820 Non-nil means to compare time stamps using seconds. An item is then
821 considered future if it has a time value later than current time."
822 :group 'org-agenda-skip
823 :group 'org-agenda-todo-list
824 :version "24.4"
825 :package-version '(Org . "8.0")
826 :type '(choice
827 (const :tag "Compare time with days" nil)
828 (const :tag "Compare time with seconds" t)))
830 (defcustom org-agenda-tags-todo-honor-ignore-options nil
831 "Non-nil means honor todo-list ignores options also in tags-todo search.
832 The variables
833 `org-agenda-todo-ignore-with-date',
834 `org-agenda-todo-ignore-timestamp',
835 `org-agenda-todo-ignore-scheduled',
836 `org-agenda-todo-ignore-deadlines'
837 make the global TODO list skip entries that have time stamps of certain
838 kinds. If this option is set, the same options will also apply for the
839 tags-todo search, which is the general tags/property matcher
840 restricted to unfinished TODO entries only."
841 :group 'org-agenda-skip
842 :group 'org-agenda-todo-list
843 :group 'org-agenda-match-view
844 :type 'boolean)
846 (defcustom org-agenda-skip-scheduled-if-done nil
847 "Non-nil means don't show scheduled items in agenda when they are done.
848 This is relevant for the daily/weekly agenda, not for the TODO list. It
849 applies only to the actual date of the scheduling. Warnings about an item
850 with a past scheduling dates are always turned off when the item is DONE."
851 :group 'org-agenda-skip
852 :group 'org-agenda-daily/weekly
853 :type 'boolean)
855 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
856 "Non-nil means skip scheduling line if same entry shows because of deadline.
858 In the agenda of today, an entry can show up multiple times
859 because it is both scheduled and has a nearby deadline, and maybe
860 a plain time stamp as well.
862 When this variable is nil, the entry will be shown several times.
864 When set to t, then only the deadline is shown and the fact that
865 the entry is scheduled today or was scheduled previously is not
866 shown.
868 When set to the symbol `not-today', skip scheduled previously,
869 but not scheduled today.
871 When set to the symbol `repeated-after-deadline', skip scheduled
872 items if they are repeated beyond the current deadline."
873 :group 'org-agenda-skip
874 :group 'org-agenda-daily/weekly
875 :type '(choice
876 (const :tag "Never" nil)
877 (const :tag "Always" t)
878 (const :tag "Not when scheduled today" not-today)
879 (const :tag "When repeated past deadline" repeated-after-deadline)))
881 (defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
882 "Non-nil means skip timestamp line if same entry shows because of deadline.
883 In the agenda of today, an entry can show up multiple times
884 because it has both a plain timestamp and has a nearby deadline.
885 When this variable is t, then only the deadline is shown and the
886 fact that the entry has a timestamp for or including today is not
887 shown. When this variable is nil, the entry will be shown
888 several times."
889 :group 'org-agenda-skip
890 :group 'org-agenda-daily/weekly
891 :version "24.1"
892 :type '(choice
893 (const :tag "Never" nil)
894 (const :tag "Always" t)))
896 (defcustom org-agenda-skip-deadline-if-done nil
897 "Non-nil means don't show deadlines when the corresponding item is done.
898 When nil, the deadline is still shown and should give you a happy feeling.
899 This is relevant for the daily/weekly agenda. It applies only to the
900 actual date of the deadline. Warnings about approaching and past-due
901 deadlines are always turned off when the item is DONE."
902 :group 'org-agenda-skip
903 :group 'org-agenda-daily/weekly
904 :type 'boolean)
906 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
907 "Non-nil means skip deadline prewarning when entry is also scheduled.
908 This will apply on all days where a prewarning for the deadline would
909 be shown, but not at the day when the entry is actually due. On that day,
910 the deadline will be shown anyway.
911 This variable may be set to nil, t, the symbol `pre-scheduled',
912 or a number which will then give the number of days before the actual
913 deadline when the prewarnings should resume. The symbol `pre-scheduled'
914 eliminates the deadline prewarning only prior to the scheduled date.
915 This can be used in a workflow where the first showing of the deadline will
916 trigger you to schedule it, and then you don't want to be reminded of it
917 because you will take care of it on the day when scheduled."
918 :group 'org-agenda-skip
919 :group 'org-agenda-daily/weekly
920 :version "24.1"
921 :type '(choice
922 (const :tag "Always show prewarning" nil)
923 (const :tag "Remove prewarning prior to scheduled date" pre-scheduled)
924 (const :tag "Remove prewarning if entry is scheduled" t)
925 (integer :tag "Restart prewarning N days before deadline")))
927 (defcustom org-agenda-skip-scheduled-delay-if-deadline nil
928 "Non-nil means skip scheduled delay when entry also has a deadline.
929 This variable may be set to nil, t, the symbol `post-deadline',
930 or a number which will then give the number of days after the actual
931 scheduled date when the delay should expire. The symbol `post-deadline'
932 eliminates the schedule delay when the date is posterior to the deadline."
933 :group 'org-agenda-skip
934 :group 'org-agenda-daily/weekly
935 :version "24.4"
936 :package-version '(Org . "8.0")
937 :type '(choice
938 (const :tag "Always honor delay" nil)
939 (const :tag "Ignore delay if posterior to the deadline" post-deadline)
940 (const :tag "Ignore delay if entry has a deadline" t)
941 (integer :tag "Honor delay up until N days after the scheduled date")))
943 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
944 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
945 When non-nil, after the search for timestamps has matched once in an
946 entry, the rest of the entry will not be searched."
947 :group 'org-agenda-skip
948 :type 'boolean)
950 (defcustom org-agenda-skip-timestamp-if-done nil
951 "Non-nil means don't select item by timestamp or -range if it is DONE."
952 :group 'org-agenda-skip
953 :group 'org-agenda-daily/weekly
954 :type 'boolean)
956 (defcustom org-agenda-dim-blocked-tasks t
957 "Non-nil means dim blocked tasks in the agenda display.
958 This causes some overhead during agenda construction, but if you
959 have turned on `org-enforce-todo-dependencies',
960 `org-enforce-todo-checkbox-dependencies', or any other blocking
961 mechanism, this will create useful feedback in the agenda.
963 Instead of t, this variable can also have the value `invisible'.
964 Then blocked tasks will be invisible and only become visible when
965 they become unblocked. An exemption to this behavior is when a task is
966 blocked because of unchecked checkboxes below it. Since checkboxes do
967 not show up in the agenda views, making this task invisible you remove any
968 trace from agenda views that there is something to do. Therefore, a task
969 that is blocked because of checkboxes will never be made invisible, it
970 will only be dimmed."
971 :group 'org-agenda-daily/weekly
972 :group 'org-agenda-todo-list
973 :version "24.3"
974 :type '(choice
975 (const :tag "Do not dim" nil)
976 (const :tag "Dim to a gray face" t)
977 (const :tag "Make invisible" invisible)))
979 (defgroup org-agenda-startup nil
980 "Options concerning initial settings in the Agenda in Org Mode."
981 :tag "Org Agenda Startup"
982 :group 'org-agenda)
984 (defcustom org-agenda-menu-show-matcher t
985 "Non-nil means show the match string in the agenda dispatcher menu.
986 When nil, the matcher string is not shown, but is put into the help-echo
987 property so than moving the mouse over the command shows it.
988 Setting it to nil is good if matcher strings are very long and/or if
989 you want to use two-columns display (see `org-agenda-menu-two-columns')."
990 :group 'org-agenda
991 :version "24.1"
992 :type 'boolean)
994 (defcustom org-agenda-menu-two-columns nil
995 "Non-nil means, use two columns to show custom commands in the dispatcher.
996 If you use this, you probably want to set `org-agenda-menu-show-matcher'
997 to nil."
998 :group 'org-agenda
999 :version "24.1"
1000 :type 'boolean)
1002 (defcustom org-agenda-finalize-hook nil
1003 "Hook run just before displaying an agenda buffer.
1004 The buffer is still writable when the hook is called.
1006 You can modify some of the buffer substrings but you should be
1007 extra careful not to modify the text properties of the agenda
1008 headlines as the agenda display heavily relies on them."
1009 :group 'org-agenda-startup
1010 :type 'hook)
1012 (defcustom org-agenda-mouse-1-follows-link nil
1013 "Non-nil means mouse-1 on a link will follow the link in the agenda.
1014 A longer mouse click will still set point. Needs to be set
1015 before org.el is loaded."
1016 :group 'org-agenda-startup
1017 :type 'boolean)
1019 (defcustom org-agenda-start-with-follow-mode nil
1020 "The initial value of follow mode in a newly created agenda window."
1021 :group 'org-agenda-startup
1022 :type 'boolean)
1024 (defcustom org-agenda-follow-indirect nil
1025 "Non-nil means `org-agenda-follow-mode' displays only the
1026 current item's tree, in an indirect buffer."
1027 :group 'org-agenda
1028 :version "24.1"
1029 :type 'boolean)
1031 (defcustom org-agenda-show-outline-path t
1032 "Non-nil means show outline path in echo area after line motion."
1033 :group 'org-agenda-startup
1034 :type 'boolean)
1036 (defcustom org-agenda-start-with-entry-text-mode nil
1037 "The initial value of entry-text-mode in a newly created agenda window."
1038 :group 'org-agenda-startup
1039 :type 'boolean)
1041 (defcustom org-agenda-entry-text-maxlines 5
1042 "Number of text lines to be added when `E' is pressed in the agenda.
1044 Note that this variable only used during agenda display. To add entry text
1045 when exporting the agenda, configure the variable
1046 `org-agenda-add-entry-text-maxlines'."
1047 :group 'org-agenda
1048 :type 'integer)
1050 (defcustom org-agenda-entry-text-exclude-regexps nil
1051 "List of regular expressions to clean up entry text.
1052 The complete matches of all regular expressions in this list will be
1053 removed from entry text before it is shown in the agenda."
1054 :group 'org-agenda
1055 :type '(repeat (regexp)))
1057 (defcustom org-agenda-entry-text-leaders " > "
1058 "Text prepended to the entry text in agenda buffers."
1059 :version "24.4"
1060 :package-version '(Org . "8.0")
1061 :group 'org-agenda
1062 :type 'string)
1064 (defvar org-agenda-entry-text-cleanup-hook nil
1065 "Hook that is run after basic cleanup of entry text to be shown in agenda.
1066 This cleanup is done in a temporary buffer, so the function may inspect and
1067 change the entire buffer.
1068 Some default stuff like drawers and scheduling/deadline dates will already
1069 have been removed when this is called, as will any matches for regular
1070 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
1072 (defvar org-agenda-include-inactive-timestamps nil
1073 "Non-nil means include inactive time stamps in agenda.
1074 Dynamically scoped.")
1076 (defgroup org-agenda-windows nil
1077 "Options concerning the windows used by the Agenda in Org Mode."
1078 :tag "Org Agenda Windows"
1079 :group 'org-agenda)
1081 (defcustom org-agenda-window-setup 'reorganize-frame
1082 "How the agenda buffer should be displayed.
1083 Possible values for this option are:
1085 current-window Show agenda in the current window, keeping all other windows.
1086 other-window Use `switch-to-buffer-other-window' to display agenda.
1087 only-window Show agenda, deleting all other windows.
1088 reorganize-frame Show only two windows on the current frame, the current
1089 window and the agenda.
1090 other-frame Use `switch-to-buffer-other-frame' to display agenda.
1091 Also, when exiting the agenda, kill that frame.
1092 See also the variable `org-agenda-restore-windows-after-quit'."
1093 :group 'org-agenda-windows
1094 :type '(choice
1095 (const current-window)
1096 (const other-frame)
1097 (const other-window)
1098 (const only-window)
1099 (const reorganize-frame)))
1101 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
1102 "The min and max height of the agenda window as a fraction of frame height.
1103 The value of the variable is a cons cell with two numbers between 0 and 1.
1104 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
1105 :group 'org-agenda-windows
1106 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
1108 (defcustom org-agenda-restore-windows-after-quit nil
1109 "Non-nil means restore window configuration upon exiting agenda.
1110 Before the window configuration is changed for displaying the agenda,
1111 the current status is recorded. When the agenda is exited with
1112 `q' or `x' and this option is set, the old state is restored. If
1113 `org-agenda-window-setup' is `other-frame', the value of this
1114 option will be ignored."
1115 :group 'org-agenda-windows
1116 :type 'boolean)
1118 (defcustom org-agenda-span 'week
1119 "Number of days to include in overview display.
1120 Can be day, week, month, year, or any number of days.
1121 Custom commands can set this variable in the options section."
1122 :group 'org-agenda-daily/weekly
1123 :type '(choice (const :tag "Day" day)
1124 (const :tag "Week" week)
1125 (const :tag "Fortnight" fortnight)
1126 (const :tag "Month" month)
1127 (const :tag "Year" year)
1128 (integer :tag "Custom")))
1130 (defcustom org-agenda-start-on-weekday 1
1131 "Non-nil means start the overview always on the specified weekday.
1132 0 denotes Sunday, 1 denotes Monday, etc.
1133 When nil, always start on the current day.
1134 Custom commands can set this variable in the options section."
1135 :group 'org-agenda-daily/weekly
1136 :type '(choice (const :tag "Today" nil)
1137 (integer :tag "Weekday No.")))
1139 (defcustom org-agenda-show-all-dates t
1140 "Non-nil means `org-agenda' shows every day in the selected range.
1141 When nil, only the days which actually have entries are shown."
1142 :group 'org-agenda-daily/weekly
1143 :type 'boolean)
1145 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1146 "Format string for displaying dates in the agenda.
1147 Used by the daily/weekly agenda. This should be a format string
1148 understood by `format-time-string', or a function returning the
1149 formatted date as a string. The function must take a single
1150 argument, a calendar-style date list like (month day year)."
1151 :group 'org-agenda-daily/weekly
1152 :type '(choice
1153 (string :tag "Format string")
1154 (function :tag "Function")))
1156 (defun org-agenda-format-date-aligned (date)
1157 "Format a DATE string for display in the daily/weekly agenda.
1158 This function makes sure that dates are aligned for easy reading."
1159 (require 'cal-iso)
1160 (let* ((dayname (calendar-day-name date))
1161 (day (cadr date))
1162 (day-of-week (calendar-day-of-week date))
1163 (month (car date))
1164 (monthname (calendar-month-name month))
1165 (year (nth 2 date))
1166 (iso-week (org-days-to-iso-week
1167 (calendar-absolute-from-gregorian date)))
1168 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1169 (1- year))
1170 ((and (= month 12) (<= iso-week 1))
1171 (1+ year))
1172 (t year)))
1173 (weekstring (if (= day-of-week 1)
1174 (format " W%02d" iso-week)
1175 "")))
1176 (format "%-10s %2d %s %4d%s"
1177 dayname day monthname year weekstring)))
1179 (defcustom org-agenda-time-leading-zero nil
1180 "Non-nil means use leading zero for military times in agenda.
1181 For example, 9:30am would become 09:30 rather than 9:30."
1182 :group 'org-agenda-daily/weekly
1183 :version "24.1"
1184 :type 'boolean)
1186 (defcustom org-agenda-timegrid-use-ampm nil
1187 "When set, show AM/PM style timestamps on the timegrid."
1188 :group 'org-agenda
1189 :version "24.1"
1190 :type 'boolean)
1192 (defun org-agenda-time-of-day-to-ampm (time)
1193 "Convert TIME of a string like \"13:45\" to an AM/PM style time string."
1194 (let* ((hour-number (string-to-number (substring time 0 -3)))
1195 (minute (substring time -2))
1196 (ampm "am"))
1197 (cond
1198 ((equal hour-number 12)
1199 (setq ampm "pm"))
1200 ((> hour-number 12)
1201 (setq ampm "pm")
1202 (setq hour-number (- hour-number 12))))
1203 (concat
1204 (if org-agenda-time-leading-zero
1205 (format "%02d" hour-number)
1206 (format "%02s" (number-to-string hour-number)))
1207 ":" minute ampm)))
1209 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1210 "Conditionally convert TIME to AM/PM format based on `org-agenda-timegrid-use-ampm'."
1211 (if org-agenda-timegrid-use-ampm
1212 (org-agenda-time-of-day-to-ampm time)
1213 time))
1215 (defcustom org-agenda-weekend-days '(6 0)
1216 "Which days are weekend?
1217 These days get the special face `org-agenda-date-weekend' in the agenda."
1218 :group 'org-agenda-daily/weekly
1219 :type '(set :greedy t
1220 (const :tag "Monday" 1)
1221 (const :tag "Tuesday" 2)
1222 (const :tag "Wednesday" 3)
1223 (const :tag "Thursday" 4)
1224 (const :tag "Friday" 5)
1225 (const :tag "Saturday" 6)
1226 (const :tag "Sunday" 0)))
1228 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1229 "Non-nil means jump to today when moving a past date forward in time.
1230 When using S-right in the agenda to move a a date forward, and the date
1231 stamp currently points to the past, the first key press will move it
1232 to today. When nil, just move one day forward even if the date stays
1233 in the past."
1234 :group 'org-agenda-daily/weekly
1235 :version "24.1"
1236 :type 'boolean)
1238 (defcustom org-agenda-include-diary nil
1239 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1240 Custom commands can set this variable in the options section."
1241 :group 'org-agenda-daily/weekly
1242 :type 'boolean)
1244 (defcustom org-agenda-include-deadlines t
1245 "If non-nil, include entries within their deadline warning period.
1246 Custom commands can set this variable in the options section."
1247 :group 'org-agenda-daily/weekly
1248 :version "24.1"
1249 :type 'boolean)
1251 (defcustom org-agenda-show-future-repeats t
1252 "Non-nil shows repeated entries in the future part of the agenda.
1253 When set to the symbol `next' only the first future repeat is shown."
1254 :group 'org-agenda-daily/weekly
1255 :type '(choice
1256 (const :tag "Show all repeated entries" t)
1257 (const :tag "Show next repeated entry" next)
1258 (const :tag "Do not show repeated entries" nil))
1259 :version "26.1"
1260 :package-version '(Org . "9.1")
1261 :safe #'symbolp)
1263 (defcustom org-agenda-prefer-last-repeat nil
1264 "Non-nil sets date for repeated entries to their last repeat.
1266 When nil, display SCHEDULED and DEADLINE dates at their base
1267 date, and in today's agenda, as a reminder. Display plain
1268 time-stamps, on the other hand, at every repeat date in the past
1269 in addition to the base date.
1271 When non-nil, show a repeated entry at its latest repeat date,
1272 possibly being today even if it wasn't marked as done. This
1273 setting is useful if you do not always mark repeated entries as
1274 done and, yet, consider that reaching repeat date starts the task
1275 anew.
1277 When set to a list of strings, prefer last repeats only for
1278 entries with these TODO keywords."
1279 :group 'org-agenda-daily/weekly
1280 :type '(choice
1281 (const :tag "Prefer last repeat" t)
1282 (const :tag "Prefer base date" nil)
1283 (repeat :tag "Prefer last repeat for entries with these TODO keywords"
1284 (string :tag "TODO keyword")))
1285 :version "26.1"
1286 :package-version '(Org . "9.1")
1287 :safe (lambda (x) (or (booleanp x) (consp x))))
1289 (defcustom org-scheduled-past-days 10000
1290 "Number of days to continue listing scheduled items not marked DONE.
1291 When an item is scheduled on a date, it shows up in the agenda on
1292 this day and will be listed until it is marked done or for the
1293 number of days given here."
1294 :group 'org-agenda-daily/weekly
1295 :type 'integer
1296 :safe 'integerp)
1298 (defcustom org-deadline-past-days 10000
1299 "Number of days to warn about missed deadlines.
1300 When an item has deadline on a date, it shows up in the agenda on
1301 this day and will appear as a reminder until it is marked DONE or
1302 for the number of days given here."
1303 :group 'org-agenda-daily/weekly
1304 :type 'integer
1305 :version "26.1"
1306 :package-version '(Org . "9.1")
1307 :safe 'integerp)
1309 (defcustom org-agenda-log-mode-items '(closed clock)
1310 "List of items that should be shown in agenda log mode.
1311 \\<org-agenda-mode-map>\
1312 This list may contain the following symbols:
1314 closed Show entries that have been closed on that day.
1315 clock Show entries that have received clocked time on that day.
1316 state Show all logged state changes.
1317 Note that instead of changing this variable, you can also press \
1318 `\\[universal-argument] \\[org-agenda-log-mode]' in
1319 the agenda to display all available LOG items temporarily."
1320 :group 'org-agenda-daily/weekly
1321 :type '(set :greedy t (const closed) (const clock) (const state)))
1323 (defcustom org-agenda-clock-consistency-checks
1324 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1325 :gap-ok-around ("4:00")
1326 :default-face ((:background "DarkRed") (:foreground "white"))
1327 :overlap-face nil :gap-face nil :no-end-time-face nil
1328 :long-face nil :short-face nil)
1329 "This is a property list, with the following keys:
1331 :max-duration Mark clocking chunks that are longer than this time.
1332 This is a time string like \"HH:MM\", or the number
1333 of minutes as an integer.
1335 :min-duration Mark clocking chunks that are shorter that this.
1336 This is a time string like \"HH:MM\", or the number
1337 of minutes as an integer.
1339 :max-gap Mark gaps between clocking chunks that are longer than
1340 this duration. A number of minutes, or a string
1341 like \"HH:MM\".
1343 :gap-ok-around List of times during the day which are usually not working
1344 times. When a gap is detected, but the gap contains any
1345 of these times, the gap is *not* reported. For example,
1346 if this is (\"4:00\" \"13:00\") then gaps that contain
1347 4:00 in the morning (i.e. the night) and 13:00
1348 (i.e. a typical lunch time) do not cause a warning.
1349 You should have at least one time during the night in this
1350 list, or otherwise the first task each morning will trigger
1351 a warning because it follows a long gap.
1353 Furthermore, the following properties can be used to define faces for
1354 issue display.
1356 :default-face the default face, if the specific face is undefined
1357 :overlap-face face for overlapping clocks
1358 :gap-face face for gaps between clocks
1359 :no-end-time-face face for incomplete clocks
1360 :long-face face for clock intervals that are too long
1361 :short-face face for clock intervals that are too short"
1362 :group 'org-agenda-daily/weekly
1363 :group 'org-clock
1364 :version "24.1"
1365 :type 'plist)
1367 (defcustom org-agenda-log-mode-add-notes t
1368 "Non-nil means add first line of notes to log entries in agenda views.
1369 If a log item like a state change or a clock entry is associated with
1370 notes, the first line of these notes will be added to the entry in the
1371 agenda display."
1372 :group 'org-agenda-daily/weekly
1373 :type 'boolean)
1375 (defcustom org-agenda-start-with-log-mode nil
1376 "The initial value of log-mode in a newly created agenda window.
1377 See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
1378 explanations on the possible values."
1379 :group 'org-agenda-startup
1380 :group 'org-agenda-daily/weekly
1381 :type '(choice (const :tag "Don't show log items" nil)
1382 (const :tag "Show only log items" only)
1383 (const :tag "Show all possible log items" clockcheck)
1384 (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
1385 (choice (const :tag "Show closed log items" closed)
1386 (const :tag "Show clocked log items" clock)
1387 (const :tag "Show all logged state changes" state)))))
1389 (defcustom org-agenda-start-with-clockreport-mode nil
1390 "The initial value of clockreport-mode in a newly created agenda window."
1391 :group 'org-agenda-startup
1392 :group 'org-agenda-daily/weekly
1393 :type 'boolean)
1395 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1396 "Property list with parameters for the clocktable in clockreport mode.
1397 This is the display mode that shows a clock table in the daily/weekly
1398 agenda, the properties for this dynamic block can be set here.
1399 The usual clocktable parameters are allowed here, but you cannot set
1400 the properties :name, :tstart, :tend, :block, and :scope - these will
1401 be overwritten to make sure the content accurately reflects the
1402 current display in the agenda."
1403 :group 'org-agenda-daily/weekly
1404 :type 'plist)
1406 (defvaralias 'org-agenda-search-view-search-words-only
1407 'org-agenda-search-view-always-boolean)
1409 (defcustom org-agenda-search-view-always-boolean nil
1410 "Non-nil means the search string is interpreted as individual parts.
1412 The search string for search view can either be interpreted as a phrase,
1413 or as a list of snippets that define a boolean search for a number of
1414 strings.
1416 When this is non-nil, the string will be split on whitespace, and each
1417 snippet will be searched individually, and all must match in order to
1418 select an entry. A snippet is then a single string of non-white
1419 characters, or a string in double quotes, or a regexp in {} braces.
1420 If a snippet is preceded by \"-\", the snippet must *not* match.
1421 \"+\" is syntactic sugar for positive selection. Each snippet may
1422 be found as a full word or a partial word, but see the variable
1423 `org-agenda-search-view-force-full-words'.
1425 When this is nil, search will look for the entire search phrase as one,
1426 with each space character matching any amount of whitespace, including
1427 line breaks.
1429 Even when this is nil, you can still switch to Boolean search dynamically
1430 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1431 is a regexp marked with braces like \"{abc}\", this will also switch to
1432 boolean search."
1433 :group 'org-agenda-search-view
1434 :version "24.1"
1435 :type 'boolean)
1437 (defcustom org-agenda-search-view-force-full-words nil
1438 "Non-nil means, search words must be matches as complete words.
1439 When nil, they may also match part of a word."
1440 :group 'org-agenda-search-view
1441 :version "24.1"
1442 :type 'boolean)
1444 (defcustom org-agenda-search-view-max-outline-level 0
1445 "Maximum outline level to display in search view.
1446 E.g. when this is set to 1, the search view will only
1447 show headlines of level 1. When set to 0, the default
1448 value, don't limit agenda view by outline level."
1449 :group 'org-agenda-search-view
1450 :version "26.1"
1451 :package-version '(Org . "8.3")
1452 :type 'integer)
1454 (defgroup org-agenda-time-grid nil
1455 "Options concerning the time grid in the Org Agenda."
1456 :tag "Org Agenda Time Grid"
1457 :group 'org-agenda)
1459 (defcustom org-agenda-search-headline-for-time t
1460 "Non-nil means search headline for a time-of-day.
1461 If the headline contains a time-of-day in one format or another, it will
1462 be used to sort the entry into the time sequence of items for a day.
1463 Some people have time stamps in the headline that refer to the creation
1464 time or so, and then this produces an unwanted side effect. If this is
1465 the case for your, use this variable to turn off searching the headline
1466 for a time."
1467 :group 'org-agenda-time-grid
1468 :type 'boolean)
1470 (defcustom org-agenda-use-time-grid t
1471 "Non-nil means show a time grid in the agenda schedule.
1472 A time grid is a set of lines for specific times (like every two hours between
1473 8:00 and 20:00). The items scheduled for a day at specific times are
1474 sorted in between these lines.
1475 For details about when the grid will be shown, and what it will look like, see
1476 the variable `org-agenda-time-grid'."
1477 :group 'org-agenda-time-grid
1478 :type 'boolean)
1480 (defcustom org-agenda-time-grid
1481 '((daily today require-timed)
1482 (800 1000 1200 1400 1600 1800 2000)
1483 "......"
1484 "----------------")
1486 "The settings for time grid for agenda display.
1487 This is a list of four items. The first item is again a list. It contains
1488 symbols specifying conditions when the grid should be displayed:
1490 daily if the agenda shows a single day
1491 weekly if the agenda shows an entire week
1492 today show grid on current date, independent of daily/weekly display
1493 require-timed show grid only if at least one item has a time specification
1494 remove-match skip grid times already present in an entry
1496 The second item is a list of integers, indicating the times that
1497 should have a grid line.
1499 The third item is a string which will be placed right after the
1500 times that have a grid line.
1502 The fourth item is a string placed after the grid times. This
1503 will align with agenda items"
1504 :group 'org-agenda-time-grid
1505 :type
1506 '(list
1507 (set :greedy t :tag "Grid Display Options"
1508 (const :tag "Show grid in single day agenda display" daily)
1509 (const :tag "Show grid in weekly agenda display" weekly)
1510 (const :tag "Always show grid for today" today)
1511 (const :tag "Show grid only if any timed entries are present"
1512 require-timed)
1513 (const :tag "Skip grid times already present in an entry"
1514 remove-match))
1515 (repeat :tag "Grid Times" (integer :tag "Time"))
1516 (string :tag "Grid String (after agenda times)")
1517 (string :tag "Grid String (aligns with agenda items)")))
1519 (defcustom org-agenda-show-current-time-in-grid t
1520 "Non-nil means show the current time in the time grid."
1521 :group 'org-agenda-time-grid
1522 :version "24.1"
1523 :type 'boolean)
1525 (defcustom org-agenda-current-time-string
1526 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1527 "The string for the current time marker in the agenda."
1528 :group 'org-agenda-time-grid
1529 :version "24.1"
1530 :type 'string)
1532 (defgroup org-agenda-sorting nil
1533 "Options concerning sorting in the Org Agenda."
1534 :tag "Org Agenda Sorting"
1535 :group 'org-agenda)
1537 (defcustom org-agenda-sorting-strategy
1538 '((agenda habit-down time-up priority-down category-keep)
1539 (todo priority-down category-keep)
1540 (tags priority-down category-keep)
1541 (search category-keep))
1542 "Sorting structure for the agenda items of a single day.
1543 This is a list of symbols which will be used in sequence to determine
1544 if an entry should be listed before another entry. The following
1545 symbols are recognized:
1547 time-up Put entries with time-of-day indications first, early first
1548 time-down Put entries with time-of-day indications first, late first
1549 timestamp-up Sort by any timestamp, early first
1550 timestamp-down Sort by any timestamp, late first
1551 scheduled-up Sort by scheduled timestamp, early first
1552 scheduled-down Sort by scheduled timestamp, late first
1553 deadline-up Sort by deadline timestamp, early first
1554 deadline-down Sort by deadline timestamp, late first
1555 ts-up Sort by active timestamp, early first
1556 ts-down Sort by active timestamp, late first
1557 tsia-up Sort by inactive timestamp, early first
1558 tsia-down Sort by inactive timestamp, late first
1559 category-keep Keep the default order of categories, corresponding to the
1560 sequence in `org-agenda-files'.
1561 category-up Sort alphabetically by category, A-Z.
1562 category-down Sort alphabetically by category, Z-A.
1563 tag-up Sort alphabetically by last tag, A-Z.
1564 tag-down Sort alphabetically by last tag, Z-A.
1565 priority-up Sort numerically by priority, high priority last.
1566 priority-down Sort numerically by priority, high priority first.
1567 todo-state-up Sort by todo state, tasks that are done last.
1568 todo-state-down Sort by todo state, tasks that are done first.
1569 effort-up Sort numerically by estimated effort, high effort last.
1570 effort-down Sort numerically by estimated effort, high effort first.
1571 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1572 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1573 habit-up Put entries that are habits first
1574 habit-down Put entries that are habits last
1575 alpha-up Sort headlines alphabetically
1576 alpha-down Sort headlines alphabetically, reversed
1578 The different possibilities will be tried in sequence, and testing stops
1579 if one comparison returns a \"not-equal\". For example, the default
1580 '(time-up category-keep priority-down)
1581 means: Pull out all entries having a specified time of day and sort them,
1582 in order to make a time schedule for the current day the first thing in the
1583 agenda listing for the day. Of the entries without a time indication, keep
1584 the grouped in categories, don't sort the categories, but keep them in
1585 the sequence given in `org-agenda-files'. Within each category sort by
1586 priority.
1588 Leaving out `category-keep' would mean that items will be sorted across
1589 categories by priority.
1591 Instead of a single list, this can also be a set of list for specific
1592 contents, with a context symbol in the car of the list, any of
1593 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1595 Custom commands can bind this variable in the options section."
1596 :group 'org-agenda-sorting
1597 :type `(choice
1598 (repeat :tag "General" ,org-sorting-choice)
1599 (list :tag "Individually"
1600 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1601 (repeat ,org-sorting-choice))
1602 (cons (const :tag "Strategy for TODO lists" todo)
1603 (repeat ,org-sorting-choice))
1604 (cons (const :tag "Strategy for Tags matches" tags)
1605 (repeat ,org-sorting-choice))
1606 (cons (const :tag "Strategy for search matches" search)
1607 (repeat ,org-sorting-choice)))))
1609 (defcustom org-agenda-cmp-user-defined nil
1610 "A function to define the comparison `user-defined'.
1611 This function must receive two arguments, agenda entry a and b.
1612 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1613 the user comparison, return nil.
1614 When this is defined, you can make `user-defined-up' and `user-defined-down'
1615 part of an agenda sorting strategy."
1616 :group 'org-agenda-sorting
1617 :type 'symbol)
1619 (defcustom org-sort-agenda-notime-is-late t
1620 "Non-nil means items without time are considered late.
1621 This is only relevant for sorting. When t, items which have no explicit
1622 time like 15:30 will be considered as 99:01, i.e. later than any items which
1623 do have a time. When nil, the default time is before 0:00. You can use this
1624 option to decide if the schedule for today should come before or after timeless
1625 agenda entries."
1626 :group 'org-agenda-sorting
1627 :type 'boolean)
1629 (defcustom org-sort-agenda-noeffort-is-high t
1630 "Non-nil means items without effort estimate are sorted as high effort.
1631 This also applies when filtering an agenda view with respect to the
1632 < or > effort operator. Then, tasks with no effort defined will be treated
1633 as tasks with high effort.
1634 When nil, such items are sorted as 0 minutes effort."
1635 :group 'org-agenda-sorting
1636 :type 'boolean)
1638 (defgroup org-agenda-line-format nil
1639 "Options concerning the entry prefix in the Org agenda display."
1640 :tag "Org Agenda Line Format"
1641 :group 'org-agenda)
1643 (defcustom org-agenda-prefix-format
1644 '((agenda . " %i %-12:c%?-12t% s")
1645 (todo . " %i %-12:c")
1646 (tags . " %i %-12:c")
1647 (search . " %i %-12:c"))
1648 "Format specifications for the prefix of items in the agenda views.
1650 An alist with one entry per agenda type. The keys of the
1651 sublists are `agenda', `todo', `search' and `tags'. The values
1652 are format strings.
1654 This format works similar to a printf format, with the following meaning:
1656 %c the category of the item, \"Diary\" for entries from the diary,
1657 or as given by the CATEGORY keyword or derived from the file name
1658 %e the effort required by the item
1659 %l the level of the item (insert X space(s) if item is of level X)
1660 %i the icon category of the item, see `org-agenda-category-icon-alist'
1661 %T the last tag of the item (ignore inherited tags, which come first)
1662 %t the HH:MM time-of-day specification if one applies to the entry
1663 %s Scheduling/Deadline information, a short string
1664 %b show breadcrumbs, i.e., the names of the higher levels
1665 %(expression) Eval EXPRESSION and replace the control string
1666 by the result
1668 All specifiers work basically like the standard `%s' of printf, but may
1669 contain two additional characters: a question mark just after the `%'
1670 and a whitespace/punctuation character just before the final letter.
1672 If the first character after `%' is a question mark, the entire field
1673 will only be included if the corresponding value applies to the current
1674 entry. This is useful for fields which should have fixed width when
1675 present, but zero width when absent. For example, \"%?-12t\" will
1676 result in a 12 character time field if a time of the day is specified,
1677 but will completely disappear in entries which do not contain a time.
1679 If there is punctuation or whitespace character just before the
1680 final format letter, this character will be appended to the field
1681 value if the value is not empty. For example, the format
1682 \"%-12:c\" leads to \"Diary: \" if the category is \"Diary\". If
1683 the category is empty, no additional colon is inserted.
1685 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1686 which means:
1688 - Indent the line with two space characters
1689 - Give the category a 12 chars wide field, padded with whitespace on
1690 the right (because of `-'). Append a colon if there is a category
1691 (because of `:').
1692 - If there is a time-of-day, put it into a 12 chars wide field. If no
1693 time, don't put in an empty field, just skip it (because of '?').
1694 - Finally, put the scheduling information.
1696 See also the variables `org-agenda-remove-times-when-in-prefix' and
1697 `org-agenda-remove-tags'.
1699 Custom commands can set this variable in the options section."
1700 :type '(choice
1701 (string :tag "General format")
1702 (list :greedy t :tag "View dependent"
1703 (cons (const agenda) (string :tag "Format"))
1704 (cons (const todo) (string :tag "Format"))
1705 (cons (const tags) (string :tag "Format"))
1706 (cons (const search) (string :tag "Format"))))
1707 :group 'org-agenda-line-format
1708 :version "26.1"
1709 :package-version '(Org . "9.1"))
1711 (defcustom org-agenda-breadcrumbs-separator "->"
1712 "The separator of breadcrumbs in agenda lines."
1713 :group 'org-agenda-line-format
1714 :package-version '(Org . "9.3")
1715 :type 'string
1716 :safe #'stringp)
1718 (defvar org-prefix-format-compiled nil
1719 "The compiled prefix format and associated variables.
1720 This is a list where first element is a list of variable bindings, and second
1721 element is the compiled format expression. See the variable
1722 `org-agenda-prefix-format'.")
1724 (defcustom org-agenda-todo-keyword-format "%-1s"
1725 "Format for the TODO keyword in agenda lines.
1726 Set this to something like \"%-12s\" if you want all TODO keywords
1727 to occupy a fixed space in the agenda display."
1728 :group 'org-agenda-line-format
1729 :type 'string)
1731 (defcustom org-agenda-diary-sexp-prefix nil
1732 "A regexp that matches part of a diary sexp entry
1733 which should be treated as scheduling/deadline information in
1734 `org-agenda'.
1736 For example, you can use this to extract the `diary-remind-message' from
1737 `diary-remind' entries."
1738 :group 'org-agenda-line-format
1739 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1741 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1742 "Text preceding timerange entries in the agenda view.
1743 This is a list with two strings. The first applies when the range
1744 is entirely on one day. The second applies if the range spans several days.
1745 The strings may have two \"%d\" format specifiers which will be filled
1746 with the sequence number of the days, and the total number of days in the
1747 range, respectively."
1748 :group 'org-agenda-line-format
1749 :type '(list
1750 (string :tag "Deadline today ")
1751 (choice :tag "Deadline relative"
1752 (string :tag "Format string")
1753 (function))))
1755 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1756 "Text preceding scheduled items in the agenda view.
1757 This is a list with two strings. The first applies when the item is
1758 scheduled on the current day. The second applies when it has been scheduled
1759 previously, it may contain a %d indicating that this is the nth time that
1760 this item is scheduled, due to automatic rescheduling of unfinished items
1761 for the following day. So this number is one larger than the number of days
1762 that passed since this item was scheduled first."
1763 :group 'org-agenda-line-format
1764 :version "24.4"
1765 :package-version '(Org . "8.0")
1766 :type '(list
1767 (string :tag "Scheduled today ")
1768 (string :tag "Scheduled previously")))
1770 (defcustom org-agenda-inactive-leader "["
1771 "Text preceding item pulled into the agenda by inactive time stamps.
1772 These entries are added to the agenda when pressing \"[\"."
1773 :group 'org-agenda-line-format
1774 :version "24.1"
1775 :type 'string)
1777 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: " "%2d d. ago: ")
1778 "Text preceding deadline items in the agenda view.
1779 This is a list with three strings. The first applies when the item has its
1780 deadline on the current day. The second applies when the deadline is in the
1781 future, the third one when it is in the past. The strings may contain %d
1782 to capture the number of days."
1783 :group 'org-agenda-line-format
1784 :version "24.4"
1785 :package-version '(Org . "8.0")
1786 :type '(list
1787 (string :tag "Deadline today ")
1788 (string :tag "Deadline in the future ")
1789 (string :tag "Deadline in the past ")))
1791 (defcustom org-agenda-remove-times-when-in-prefix t
1792 "Non-nil means remove duplicate time specifications in agenda items.
1793 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1794 time-of-day specification in a headline or diary entry is extracted and
1795 placed into the prefix. If this option is non-nil, the original specification
1796 \(a timestamp or -range, or just a plain time(range) specification like
1797 11:30-4pm) will be removed for agenda display. This makes the agenda less
1798 cluttered.
1799 The option can be t or nil. It may also be the symbol `beg', indicating
1800 that the time should only be removed when it is located at the beginning of
1801 the headline/diary entry."
1802 :group 'org-agenda-line-format
1803 :type '(choice
1804 (const :tag "Always" t)
1805 (const :tag "Never" nil)
1806 (const :tag "When at beginning of entry" beg)))
1808 (defcustom org-agenda-remove-timeranges-from-blocks nil
1809 "Non-nil means remove time ranges specifications in agenda
1810 items that span on several days."
1811 :group 'org-agenda-line-format
1812 :version "24.1"
1813 :type 'boolean)
1815 (defcustom org-agenda-default-appointment-duration nil
1816 "Default duration for appointments that only have a starting time.
1817 When nil, no duration is specified in such cases.
1818 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1819 :group 'org-agenda-line-format
1820 :type '(choice
1821 (integer :tag "Minutes")
1822 (const :tag "No default duration")))
1824 (defcustom org-agenda-show-inherited-tags t
1825 "Non-nil means show inherited tags in each agenda line.
1827 When this option is set to `always', it takes precedence over
1828 `org-agenda-use-tag-inheritance' and inherited tags are shown
1829 in every agenda.
1831 When this option is set to t (the default), inherited tags are
1832 shown when they are available, i.e. when the value of
1833 `org-agenda-use-tag-inheritance' enables tag inheritance for the
1834 given agenda type.
1836 This can be set to a list of agenda types in which the agenda
1837 must display the inherited tags. Available types are `todo',
1838 `agenda' and `search'.
1840 When set to nil, never show inherited tags in agenda lines."
1841 :group 'org-agenda-line-format
1842 :group 'org-agenda
1843 :version "24.3"
1844 :type '(choice
1845 (const :tag "Show inherited tags when available" t)
1846 (const :tag "Always show inherited tags" always)
1847 (repeat :tag "Show inherited tags only in selected agenda types"
1848 (symbol :tag "Agenda type"))))
1850 (defcustom org-agenda-use-tag-inheritance '(todo search agenda)
1851 "List of agenda view types where to use tag inheritance.
1853 In tags/tags-todo/tags-tree agenda views, tag inheritance is
1854 controlled by `org-use-tag-inheritance'. In other agenda types,
1855 `org-use-tag-inheritance' is not used for the selection of the
1856 agenda entries. Still, you may want the agenda to be aware of
1857 the inherited tags anyway, e.g. for later tag filtering.
1859 Allowed value are `todo', `search' and `agenda'.
1861 This variable has no effect if `org-agenda-show-inherited-tags'
1862 is set to `always'. In that case, the agenda is aware of those
1863 tags.
1865 The default value sets tags in every agenda type. Setting this
1866 option to nil will speed up non-tags agenda view a lot."
1867 :group 'org-agenda
1868 :version "26.1"
1869 :package-version '(Org . "9.1")
1870 :type '(choice
1871 (const :tag "Use tag inheritance in all agenda types" t)
1872 (repeat :tag "Use tag inheritance in selected agenda types"
1873 (symbol :tag "Agenda type"))))
1875 (defcustom org-agenda-hide-tags-regexp nil
1876 "Regular expression used to filter away specific tags in agenda views.
1877 This means that these tags will be present, but not be shown in the agenda
1878 line. Secondary filtering will still work on the hidden tags.
1879 Nil means don't hide any tags."
1880 :group 'org-agenda-line-format
1881 :type '(choice
1882 (const :tag "Hide none" nil)
1883 (string :tag "Regexp ")))
1885 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1886 'org-agenda-remove-tags)
1888 (defcustom org-agenda-remove-tags nil
1889 "Non-nil means remove the tags from the headline copy in the agenda.
1890 When this is the symbol `prefix', only remove tags when
1891 `org-agenda-prefix-format' contains a `%T' specifier."
1892 :group 'org-agenda-line-format
1893 :type '(choice
1894 (const :tag "Always" t)
1895 (const :tag "Never" nil)
1896 (const :tag "When prefix format contains %T" prefix)))
1898 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column)
1900 (defcustom org-agenda-tags-column 'auto
1901 "Shift tags in agenda items to this column.
1902 If set to `auto', tags will be automatically aligned to the right
1903 edge of the window.
1905 If set to a positive number, tags will be left-aligned to that
1906 column. If set to a negative number, tags will be right-aligned
1907 to that column. For example, -80 works well for a normal 80
1908 character screen."
1909 :group 'org-agenda-line-format
1910 :type '(choice
1911 (const :tag "Automatically align to right edge of window" auto)
1912 (integer :tag "Specific column" -80))
1913 :package-version '(Org . "9.1")
1914 :version "26.1")
1916 (defcustom org-agenda-fontify-priorities 'cookies
1917 "Non-nil means highlight low and high priorities in agenda.
1918 When t, the highest priority entries are bold, lowest priority italic.
1919 However, settings in `org-priority-faces' will overrule these faces.
1920 When this variable is the symbol `cookies', only fontify the
1921 cookies, not the entire task.
1922 This may also be an association list of priority faces, whose
1923 keys are the character values of `org-highest-priority',
1924 `org-default-priority', and `org-lowest-priority' (the default values
1925 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1926 color as a string, or a list like `(:background \"Red\")'.
1927 If it is a color, the variable `org-faces-easy-properties'
1928 determines if it is a foreground or a background color."
1929 :group 'org-agenda-line-format
1930 :type '(choice
1931 (const :tag "Never" nil)
1932 (const :tag "Defaults" t)
1933 (const :tag "Cookies only" cookies)
1934 (repeat :tag "Specify"
1935 (list (character :tag "Priority" :value ?A)
1936 (choice :tag "Face "
1937 (string :tag "Color")
1938 (sexp :tag "Face"))))))
1940 (defcustom org-agenda-day-face-function nil
1941 "Function called to determine what face should be used to display a day.
1942 The only argument passed to that function is the day. It should
1943 returns a face, or nil if does not want to specify a face and let
1944 the normal rules apply."
1945 :group 'org-agenda-line-format
1946 :version "24.1"
1947 :type '(choice (const nil) (function)))
1949 (defcustom org-agenda-category-icon-alist nil
1950 "Alist of category icon to be displayed in agenda views.
1952 Each entry should have the following format:
1954 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1956 Where CATEGORY-REGEXP is a regexp matching the categories where
1957 the icon should be displayed.
1958 FILE-OR-DATA either a file path or a string containing image data.
1960 The other fields can be omitted safely if not needed:
1961 TYPE indicates the image type.
1962 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1963 image data.
1964 PROPS are additional image attributes to assign to the image,
1965 like, e.g. `:ascent center'.
1967 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1969 If you want to set the display properties yourself, just put a
1970 list as second element:
1972 (CATEGORY-REGEXP (MY PROPERTY LIST))
1974 For example, to display a 16px horizontal space for Emacs
1975 category, you can use:
1977 (\"Emacs\" \\='(space . (:width (16))))"
1978 :group 'org-agenda-line-format
1979 :version "24.1"
1980 :type '(alist :key-type (string :tag "Regexp matching category")
1981 :value-type (choice (list :tag "Icon"
1982 (string :tag "File or data")
1983 (symbol :tag "Type")
1984 (boolean :tag "Data?")
1985 (repeat :tag "Extra image properties" :inline t symbol))
1986 (list :tag "Display properties" sexp))))
1988 (defgroup org-agenda-column-view nil
1989 "Options concerning column view in the agenda."
1990 :tag "Org Agenda Column View"
1991 :group 'org-agenda)
1993 (defcustom org-agenda-view-columns-initially nil
1994 "When non-nil, switch to columns view right after creating the agenda."
1995 :group 'org-agenda-column-view
1996 :type 'boolean
1997 :version "26.1"
1998 :package-version '(Org . "9.0")
1999 :safe #'booleanp)
2001 (defcustom org-agenda-columns-show-summaries t
2002 "Non-nil means show summaries for columns displayed in the agenda view."
2003 :group 'org-agenda-column-view
2004 :type 'boolean)
2006 (defcustom org-agenda-columns-compute-summary-properties t
2007 "Non-nil means recompute all summary properties before column view.
2008 When column view in the agenda is listing properties that have a summary
2009 operator, it can go to all relevant buffers and recompute the summaries
2010 there. This can mean overhead for the agenda column view, but is necessary
2011 to have thing up to date.
2012 As a special case, a CLOCKSUM property also makes sure that the clock
2013 computations are current."
2014 :group 'org-agenda-column-view
2015 :type 'boolean)
2017 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
2018 "Non-nil means the duration of an appointment will add to day effort.
2019 The property to which appointment durations will be added is the one given
2020 in the option `org-effort-property'. If an appointment does not have
2021 an end time, `org-agenda-default-appointment-duration' will be used. If that
2022 is not set, an appointment without end time will not contribute to the time
2023 estimate."
2024 :group 'org-agenda-column-view
2025 :type 'boolean)
2027 (defcustom org-agenda-auto-exclude-function nil
2028 "A function called with a tag to decide if it is filtered on \
2029 \\<org-agenda-mode-map>`\\[org-agenda-filter-by-tag] RET'.
2030 The sole argument to the function, which is called once for each
2031 possible tag, is a string giving the name of the tag. The
2032 function should return either nil if the tag should be included
2033 as normal, or \"-<TAG>\" to exclude the tag.
2034 Note that for the purpose of tag filtering, only the lower-case version of
2035 all tags will be considered, so that this function will only ever see
2036 the lower-case version of all tags."
2037 :group 'org-agenda
2038 :type '(choice (const nil) (function)))
2040 (defcustom org-agenda-bulk-custom-functions nil
2041 "Alist of characters and custom functions for bulk actions.
2042 For example, this value makes those two functions available:
2044 \\='((?R set-category)
2045 (?C bulk-cut))
2047 With selected entries in an agenda buffer, `B R' will call
2048 the custom function `set-category' on the selected entries.
2049 Note that functions in this alist don't need to be quoted."
2050 :type '(alist :key-type character :value-type (group function))
2051 :version "24.1"
2052 :group 'org-agenda)
2054 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
2055 "Execute BODY with point at location given by `org-hd-marker' property.
2056 If STRING is non-nil, the text property will be fetched from position 0
2057 in that string. If STRING is nil, it will be fetched from the beginning
2058 of the current line."
2059 (org-with-gensyms (marker)
2060 `(let ((,marker (get-text-property (if ,string 0 (point-at-bol))
2061 'org-hd-marker ,string)))
2062 (with-current-buffer (marker-buffer ,marker)
2063 (save-excursion
2064 (goto-char ,marker)
2065 ,@body)))))
2066 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
2068 (defun org-add-agenda-custom-command (entry)
2069 "Replace or add a command in `org-agenda-custom-commands'.
2070 This is mostly for hacking and trying a new command - once the command
2071 works you probably want to add it to `org-agenda-custom-commands' for good."
2072 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
2073 (if ass
2074 (setcdr ass (cdr entry))
2075 (push entry org-agenda-custom-commands))))
2077 (defmacro org-agenda--insert-overriding-header (default)
2078 "Insert header into agenda view.
2079 The inserted header depends on `org-agenda-overriding-header'.
2080 If the empty string, don't insert a header. If any other string,
2081 insert it as a header. If nil, insert DEFAULT, which should
2082 evaluate to a string."
2083 (declare (debug (form)) (indent defun))
2084 `(cond
2085 ((not org-agenda-overriding-header) (insert ,default))
2086 ((equal org-agenda-overriding-header "") nil)
2087 ((stringp org-agenda-overriding-header)
2088 (insert (propertize org-agenda-overriding-header
2089 'face 'org-agenda-structure)
2090 "\n"))
2091 (t (user-error "Invalid value for `org-agenda-overriding-header': %S"
2092 org-agenda-overriding-header))))
2094 ;;; Define the org-agenda-mode
2096 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map)
2097 (defvar org-agenda-mode-map (make-sparse-keymap)
2098 "Keymap for `org-agenda-mode'.")
2100 (defvar org-agenda-menu) ; defined later in this file.
2101 (defvar org-agenda-restrict nil) ; defined later in this file.
2102 (defvar org-agenda-follow-mode nil)
2103 (defvar org-agenda-entry-text-mode nil)
2104 (defvar org-agenda-clockreport-mode nil)
2105 (defvar org-agenda-show-log nil
2106 "When non-nil, show the log in the agenda.
2107 Do not set this directly; instead use
2108 `org-agenda-start-with-log-mode', which see.")
2109 (defvar org-agenda-redo-command nil)
2110 (defvar org-agenda-query-string nil)
2111 (defvar org-agenda-mode-hook nil
2112 "Hook run after `org-agenda-mode' is turned on.
2113 The buffer is still writable when this hook is called.")
2114 (defvar org-agenda-type nil)
2115 (defvar org-agenda-force-single-file nil)
2116 (defvar org-agenda-bulk-marked-entries nil
2117 "List of markers that refer to marked entries in the agenda.")
2118 (defvar org-agenda-current-date nil
2119 "Active date when building the agenda.")
2121 ;;; Multiple agenda buffers support
2123 (defcustom org-agenda-sticky nil
2124 "Non-nil means agenda q key will bury agenda buffers.
2125 Agenda commands will then show existing buffer instead of generating new ones.
2126 When nil, `q' will kill the single agenda buffer."
2127 :group 'org-agenda
2128 :version "24.3"
2129 :type 'boolean)
2132 ;;;###autoload
2133 (defun org-toggle-sticky-agenda (&optional arg)
2134 "Toggle `org-agenda-sticky'."
2135 (interactive "P")
2136 (let ((new-value (if arg
2137 (> (prefix-numeric-value arg) 0)
2138 (not org-agenda-sticky))))
2139 (if (equal new-value org-agenda-sticky)
2140 (and (called-interactively-p 'interactive)
2141 (message "Sticky agenda was already %s"
2142 (if org-agenda-sticky "enabled" "disabled")))
2143 (setq org-agenda-sticky new-value)
2144 (org-agenda-kill-all-agenda-buffers)
2145 (and (called-interactively-p 'interactive)
2146 (message "Sticky agenda %s"
2147 (if org-agenda-sticky "enabled" "disabled"))))))
2149 (defvar org-agenda-buffer nil
2150 "Agenda buffer currently being generated.")
2152 (defvar org-agenda-last-prefix-arg nil)
2153 (defvar org-agenda-this-buffer-name nil)
2154 (defvar org-agenda-doing-sticky-redo nil)
2155 (defvar org-agenda-this-buffer-is-sticky nil)
2156 (defvar org-agenda-last-indirect-buffer nil
2157 "Last buffer loaded by `org-agenda-tree-to-indirect-buffer'.")
2159 (defconst org-agenda-local-vars
2160 '(org-agenda-this-buffer-name
2161 org-agenda-undo-list
2162 org-agenda-pending-undo-list
2163 org-agenda-follow-mode
2164 org-agenda-entry-text-mode
2165 org-agenda-clockreport-mode
2166 org-agenda-show-log
2167 org-agenda-redo-command
2168 org-agenda-query-string
2169 org-agenda-type
2170 org-agenda-bulk-marked-entries
2171 org-agenda-undo-has-started-in
2172 org-agenda-info
2173 org-agenda-pre-window-conf
2174 org-agenda-columns-active
2175 org-agenda-tag-filter
2176 org-agenda-category-filter
2177 org-agenda-top-headline-filter
2178 org-agenda-regexp-filter
2179 org-agenda-effort-filter
2180 org-agenda-markers
2181 org-agenda-last-search-view-search-was-boolean
2182 org-agenda-last-indirect-buffer
2183 org-agenda-filtered-by-category
2184 org-agenda-filter-form
2185 org-agenda-cycle-counter
2186 org-agenda-last-prefix-arg)
2187 "Variables that must be local in agenda buffers to allow multiple buffers.")
2189 (defun org-agenda-mode ()
2190 "Mode for time-sorted view on action items in Org files.
2192 The following commands are available:
2194 \\{org-agenda-mode-map}"
2195 (interactive)
2196 (let ((agenda-local-vars-to-keep
2197 '(text-scale-mode-amount
2198 text-scale-mode
2199 text-scale-mode-lighter
2200 face-remapping-alist))
2201 (save (buffer-local-variables)))
2202 (kill-all-local-variables)
2203 (cl-flet ((reset-saved (var-set)
2204 "Reset variables in VAR-SET to possibly stored value in SAVE."
2205 (dolist (elem save)
2206 (pcase elem
2207 (`(,var . ,val) ;ignore unbound variables
2208 (when (and val (memq var var-set))
2209 (set var val)))))))
2210 (cond (org-agenda-doing-sticky-redo
2211 ;; Refreshing sticky agenda-buffer
2213 ;; Preserve the value of `org-agenda-local-vars' variables.
2214 (mapc #'make-local-variable org-agenda-local-vars)
2215 (reset-saved org-agenda-local-vars)
2216 (setq-local org-agenda-this-buffer-is-sticky t))
2217 (org-agenda-sticky
2218 ;; Creating a sticky Agenda buffer for the first time
2219 (mapc 'make-local-variable org-agenda-local-vars)
2220 (setq-local org-agenda-this-buffer-is-sticky t))
2222 ;; Creating a non-sticky agenda buffer
2223 (setq-local org-agenda-this-buffer-is-sticky nil)))
2224 (mapc #'make-local-variable agenda-local-vars-to-keep)
2225 (reset-saved agenda-local-vars-to-keep)))
2226 (setq org-agenda-undo-list nil
2227 org-agenda-pending-undo-list nil
2228 org-agenda-bulk-marked-entries nil)
2229 (setq major-mode 'org-agenda-mode)
2230 ;; Keep global-font-lock-mode from turning on font-lock-mode
2231 (setq-local font-lock-global-modes (list 'not major-mode))
2232 (setq mode-name "Org-Agenda")
2233 (setq indent-tabs-mode nil)
2234 (use-local-map org-agenda-mode-map)
2235 (easy-menu-add org-agenda-menu)
2236 (when org-startup-truncated (setq truncate-lines t))
2237 (setq-local line-move-visual nil)
2238 (add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
2239 (add-hook 'pre-command-hook 'org-unhighlight nil 'local)
2240 ;; Make sure properties are removed when copying text
2241 (if (boundp 'filter-buffer-substring-functions)
2242 (add-hook 'filter-buffer-substring-functions
2243 (lambda (fun start end delete)
2244 (substring-no-properties (funcall fun start end delete)))
2245 nil t)
2246 ;; Emacs >= 24.4.
2247 (add-function :filter-return (local 'filter-buffer-substring-function)
2248 #'substring-no-properties))
2249 (unless org-agenda-keep-modes
2250 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
2251 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
2252 org-agenda-show-log org-agenda-start-with-log-mode
2253 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode))
2254 (add-to-invisibility-spec '(org-filtered))
2255 (add-to-invisibility-spec '(org-link))
2256 (easy-menu-change
2257 '("Agenda") "Agenda Files"
2258 (append
2259 (list
2260 (vector
2261 (if (get 'org-agenda-files 'org-restrict)
2262 "Restricted to single file"
2263 "Edit File List")
2264 '(org-edit-agenda-file-list)
2265 (not (get 'org-agenda-files 'org-restrict)))
2266 "--")
2267 (mapcar 'org-file-menu-entry (org-agenda-files))))
2268 (org-agenda-set-mode-name)
2269 (apply
2270 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2271 (list 'org-agenda-mode-hook)))
2273 (substitute-key-definition 'undo 'org-agenda-undo
2274 org-agenda-mode-map global-map)
2275 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2276 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2277 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2278 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2279 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2280 (org-defkey org-agenda-mode-map [(meta down)] 'org-agenda-drag-line-forward)
2281 (org-defkey org-agenda-mode-map [(meta up)] 'org-agenda-drag-line-backward)
2282 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2283 (org-defkey org-agenda-mode-map "\M-m" 'org-agenda-bulk-toggle)
2284 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2285 (org-defkey org-agenda-mode-map "\M-*" 'org-agenda-bulk-toggle-all)
2286 (org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
2287 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2288 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2289 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2290 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2291 (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2292 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2293 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2294 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2295 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2296 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2297 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2298 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2299 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2300 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2301 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2302 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2303 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2304 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2305 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2306 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2307 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2308 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2309 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2310 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2311 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2312 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2313 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2314 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2315 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2316 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2317 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2318 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2319 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2320 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2321 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2322 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2323 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2324 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2326 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2327 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2328 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2329 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2330 (while l (org-defkey org-agenda-mode-map
2331 (int-to-string (pop l)) 'digit-argument)))
2333 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2334 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2335 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2336 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2337 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2338 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2339 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2340 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2341 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2342 (org-defkey org-agenda-mode-map "g" 'org-agenda-redo-all)
2343 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2344 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2345 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2346 'org-clock-modify-effort-estimate)
2347 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2348 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2349 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2350 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2351 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2352 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2353 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2354 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2355 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2356 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2357 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2358 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2359 (substitute-key-definition 'next-line 'org-agenda-next-line
2360 org-agenda-mode-map global-map)
2361 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2362 org-agenda-mode-map global-map)
2363 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2364 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2365 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2366 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2367 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2368 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2369 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2370 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2371 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2372 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2373 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2374 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2375 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2376 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2377 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2378 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2379 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2380 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2381 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2382 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2383 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2384 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2385 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2386 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2387 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2388 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2389 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2390 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2391 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2392 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2393 (org-defkey org-agenda-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock-from-agenda)
2395 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2396 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2397 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2398 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2399 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2400 (org-defkey org-agenda-mode-map "_" 'org-agenda-filter-by-effort)
2401 (org-defkey org-agenda-mode-map "=" 'org-agenda-filter-by-regexp)
2402 (org-defkey org-agenda-mode-map "|" 'org-agenda-filter-remove-all)
2403 (org-defkey org-agenda-mode-map "~" 'org-agenda-limit-interactively)
2404 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2405 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-headline)
2406 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2407 (org-defkey org-agenda-mode-map "\C-c\C-x_" 'org-timer-stop)
2408 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2409 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2410 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2411 (org-defkey org-agenda-mode-map "\C-c\C-xI" 'org-info-find-node)
2413 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2414 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2416 (define-key org-agenda-mode-map [remap forward-paragraph] 'org-agenda-forward-block)
2417 (define-key org-agenda-mode-map [remap backward-paragraph] 'org-agenda-backward-block)
2419 (when org-agenda-mouse-1-follows-link
2420 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2421 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2422 '("Agenda"
2423 ("Agenda Files")
2424 "--"
2425 ("Agenda Dates"
2426 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda)]
2427 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2428 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2429 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2430 "--"
2431 ("View"
2432 ["Day View" org-agenda-day-view
2433 :active (org-agenda-check-type nil 'agenda)
2434 :style radio :selected (eq org-agenda-current-span 'day)
2435 :keys "v d (or just d)"]
2436 ["Week View" org-agenda-week-view
2437 :active (org-agenda-check-type nil 'agenda)
2438 :style radio :selected (eq org-agenda-current-span 'week)
2439 :keys "v w"]
2440 ["Fortnight View" org-agenda-fortnight-view
2441 :active (org-agenda-check-type nil 'agenda)
2442 :style radio :selected (eq org-agenda-current-span 'fortnight)
2443 :keys "v t"]
2444 ["Month View" org-agenda-month-view
2445 :active (org-agenda-check-type nil 'agenda)
2446 :style radio :selected (eq org-agenda-current-span 'month)
2447 :keys "v m"]
2448 ["Year View" org-agenda-year-view
2449 :active (org-agenda-check-type nil 'agenda)
2450 :style radio :selected (eq org-agenda-current-span 'year)
2451 :keys "v y"]
2452 "--"
2453 ["Include Diary" org-agenda-toggle-diary
2454 :style toggle :selected org-agenda-include-diary
2455 :active (org-agenda-check-type nil 'agenda)]
2456 ["Include Deadlines" org-agenda-toggle-deadlines
2457 :style toggle :selected org-agenda-include-deadlines
2458 :active (org-agenda-check-type nil 'agenda)]
2459 ["Use Time Grid" org-agenda-toggle-time-grid
2460 :style toggle :selected org-agenda-use-time-grid
2461 :active (org-agenda-check-type nil 'agenda)]
2462 "--"
2463 ["Show clock report" org-agenda-clockreport-mode
2464 :style toggle :selected org-agenda-clockreport-mode
2465 :active (org-agenda-check-type nil 'agenda)]
2466 ["Show some entry text" org-agenda-entry-text-mode
2467 :style toggle :selected org-agenda-entry-text-mode
2468 :active t]
2469 "--"
2470 ["Show Logbook entries" org-agenda-log-mode
2471 :style toggle :selected org-agenda-show-log
2472 :active (org-agenda-check-type nil 'agenda)
2473 :keys "v l (or just l)"]
2474 ["Include archived trees" org-agenda-archives-mode
2475 :style toggle :selected org-agenda-archives-mode :active t
2476 :keys "v a"]
2477 ["Include archive files" (org-agenda-archives-mode t)
2478 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2479 :keys "v A"]
2480 "--"
2481 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2482 ["Write view to file" org-agenda-write t]
2483 ["Rebuild buffer" org-agenda-redo t]
2484 ["Save all Org buffers" org-save-all-org-buffers t]
2485 "--"
2486 ["Show original entry" org-agenda-show t]
2487 ["Go To (other window)" org-agenda-goto t]
2488 ["Go To (this window)" org-agenda-switch-to t]
2489 ["Capture with cursor date" org-agenda-capture t]
2490 ["Follow Mode" org-agenda-follow-mode
2491 :style toggle :selected org-agenda-follow-mode :active t]
2492 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2493 "--"
2494 ("TODO"
2495 ["Cycle TODO" org-agenda-todo t]
2496 ["Next TODO set" org-agenda-todo-nextset t]
2497 ["Previous TODO set" org-agenda-todo-previousset t]
2498 ["Add note" org-agenda-add-note t])
2499 ("Archive/Refile/Delete"
2500 ["Archive default" org-agenda-archive-default t]
2501 ["Archive default" org-agenda-archive-default-with-confirmation t]
2502 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2503 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2504 ["Archive subtree" org-agenda-archive t]
2505 "--"
2506 ["Refile" org-agenda-refile t]
2507 "--"
2508 ["Delete subtree" org-agenda-kill t])
2509 ("Bulk action"
2510 ["Mark entry" org-agenda-bulk-mark t]
2511 ["Mark all" org-agenda-bulk-mark-all t]
2512 ["Unmark entry" org-agenda-bulk-unmark t]
2513 ["Unmark all" org-agenda-bulk-unmark-all :active t :keys "U"]
2514 ["Toggle mark" org-agenda-bulk-toggle t]
2515 ["Toggle all" org-agenda-bulk-toggle-all t]
2516 ["Mark regexp" org-agenda-bulk-mark-regexp t])
2517 ["Act on all marked" org-agenda-bulk-action t]
2518 "--"
2519 ("Tags and Properties"
2520 ["Show all Tags" org-agenda-show-tags t]
2521 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2522 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2523 "--"
2524 ["Column View" org-columns t])
2525 ("Deadline/Schedule"
2526 ["Schedule" org-agenda-schedule t]
2527 ["Set Deadline" org-agenda-deadline t]
2528 "--"
2529 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda)]
2530 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda)]
2531 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda) :keys "C-u S-right"]
2532 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda) :keys "C-u S-left"]
2533 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda) :keys "C-u C-u S-right"]
2534 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda) :keys "C-u C-u S-left"]
2535 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda)])
2536 ("Clock and Effort"
2537 ["Clock in" org-agenda-clock-in t]
2538 ["Clock out" org-agenda-clock-out t]
2539 ["Clock cancel" org-agenda-clock-cancel t]
2540 ["Goto running clock" org-clock-goto t]
2541 "--"
2542 ["Set Effort" org-agenda-set-effort t]
2543 ["Change clocked effort" org-clock-modify-effort-estimate
2544 (org-clock-is-active)])
2545 ("Priority"
2546 ["Set Priority" org-agenda-priority t]
2547 ["Increase Priority" org-agenda-priority-up t]
2548 ["Decrease Priority" org-agenda-priority-down t]
2549 ["Show Priority" org-show-priority t])
2550 ("Calendar/Diary"
2551 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda)]
2552 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda)]
2553 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda)]
2554 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda)]
2555 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda)]
2556 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda)]
2557 "--"
2558 ["Create iCalendar File" org-icalendar-combine-agenda-files t])
2559 "--"
2560 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2561 "--"
2562 ("MobileOrg"
2563 ["Push Files and Views" org-mobile-push t]
2564 ["Get Captured and Flagged" org-mobile-pull t]
2565 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "\\[org-agenda] ?"]
2566 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2567 "--"
2568 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2569 "--"
2570 ["Quit" org-agenda-quit t]
2571 ["Exit and Release Buffers" org-agenda-exit t]
2574 ;;; Agenda undo
2576 (defvar org-agenda-allow-remote-undo t
2577 "Non-nil means allow remote undo from the agenda buffer.")
2578 (defvar org-agenda-undo-has-started-in nil
2579 "Buffers that have already seen `undo-start' in the current undo sequence.")
2581 (defun org-agenda-undo ()
2582 "Undo a remote editing step in the agenda.
2583 This undoes changes both in the agenda buffer and in the remote buffer
2584 that have been changed along."
2585 (interactive)
2586 (or org-agenda-allow-remote-undo
2587 (user-error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2588 (when (not (eq this-command last-command))
2589 (setq org-agenda-undo-has-started-in nil
2590 org-agenda-pending-undo-list org-agenda-undo-list))
2591 (when (not org-agenda-pending-undo-list)
2592 (user-error "No further undo information"))
2593 (let* ((entry (pop org-agenda-pending-undo-list))
2594 buf line cmd rembuf)
2595 (setq cmd (pop entry) line (pop entry))
2596 (setq rembuf (nth 2 entry))
2597 (org-with-remote-undo rembuf
2598 (while (bufferp (setq buf (pop entry)))
2599 (when (pop entry)
2600 (with-current-buffer buf
2601 (let ((last-undo-buffer buf)
2602 (inhibit-read-only t))
2603 (unless (memq buf org-agenda-undo-has-started-in)
2604 (push buf org-agenda-undo-has-started-in)
2605 (make-local-variable 'pending-undo-list)
2606 (undo-start))
2607 (while (and pending-undo-list
2608 (listp pending-undo-list)
2609 (not (car pending-undo-list)))
2610 (pop pending-undo-list))
2611 (undo-more 1))))))
2612 (org-goto-line line)
2613 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2615 (defun org-verify-change-for-undo (l1 l2)
2616 "Verify that a real change occurred between the undo lists L1 and L2."
2617 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2618 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2619 (not (eq l1 l2)))
2621 ;;; Agenda dispatch
2623 (defvar org-agenda-restrict-begin (make-marker))
2624 (defvar org-agenda-restrict-end (make-marker))
2625 (defvar org-agenda-last-dispatch-buffer nil)
2626 (defvar org-agenda-overriding-restriction nil)
2628 (defcustom org-agenda-custom-commands-contexts nil
2629 "Alist of custom agenda keys and contextual rules.
2631 For example, if you have a custom agenda command \"p\" and you
2632 want this command to be accessible only from plain text files,
2633 use this:
2635 \\='((\"p\" ((in-file . \"\\\\.txt\\\\'\"))))
2637 Here are the available contexts definitions:
2639 in-file: command displayed only in matching files
2640 in-mode: command displayed only in matching modes
2641 not-in-file: command not displayed in matching files
2642 not-in-mode: command not displayed in matching modes
2643 in-buffer: command displayed only in matching buffers
2644 not-in-buffer: command not displayed in matching buffers
2645 [function]: a custom function taking no argument
2647 If you define several checks, the agenda command will be
2648 accessible if there is at least one valid check.
2650 You can also bind a key to another agenda custom command
2651 depending on contextual rules.
2653 \\='((\"p\" \"q\" ((in-file . \"\\\\.txt\\\\'\"))))
2655 Here it means: in .txt files, use \"p\" as the key for the
2656 agenda command otherwise associated with \"q\". (The command
2657 originally associated with \"q\" is not displayed to avoid
2658 duplicates.)"
2659 :version "24.3"
2660 :group 'org-agenda-custom-commands
2661 :type '(repeat (list :tag "Rule"
2662 (string :tag " Agenda key")
2663 (string :tag "Replace by command")
2664 (repeat :tag "Available when"
2665 (choice
2666 (cons :tag "Condition"
2667 (choice
2668 (const :tag "In file" in-file)
2669 (const :tag "Not in file" not-in-file)
2670 (const :tag "In buffer" in-buffer)
2671 (const :tag "Not in buffer" not-in-buffer)
2672 (const :tag "In mode" in-mode)
2673 (const :tag "Not in mode" not-in-mode))
2674 (regexp))
2675 (function :tag "Custom function"))))))
2677 (defcustom org-agenda-max-entries nil
2678 "Maximum number of entries to display in an agenda.
2679 This can be nil (no limit) or an integer or an alist of agenda
2680 types with an associated number of entries to display in this
2681 type."
2682 :version "24.4"
2683 :package-version '(Org . "8.0")
2684 :group 'org-agenda-custom-commands
2685 :type '(choice (symbol :tag "No limit" nil)
2686 (integer :tag "Max number of entries")
2687 (repeat
2688 (cons (choice :tag "Agenda type"
2689 (const agenda)
2690 (const todo)
2691 (const tags)
2692 (const search))
2693 (integer :tag "Max number of entries")))))
2695 (defcustom org-agenda-max-todos nil
2696 "Maximum number of TODOs to display in an agenda.
2697 This can be nil (no limit) or an integer or an alist of agenda
2698 types with an associated number of entries to display in this
2699 type."
2700 :version "24.4"
2701 :package-version '(Org . "8.0")
2702 :group 'org-agenda-custom-commands
2703 :type '(choice (symbol :tag "No limit" nil)
2704 (integer :tag "Max number of TODOs")
2705 (repeat
2706 (cons (choice :tag "Agenda type"
2707 (const agenda)
2708 (const todo)
2709 (const tags)
2710 (const search))
2711 (integer :tag "Max number of TODOs")))))
2713 (defcustom org-agenda-max-tags nil
2714 "Maximum number of tagged entries to display in an agenda.
2715 This can be nil (no limit) or an integer or an alist of agenda
2716 types with an associated number of entries to display in this
2717 type."
2718 :version "24.4"
2719 :package-version '(Org . "8.0")
2720 :group 'org-agenda-custom-commands
2721 :type '(choice (symbol :tag "No limit" nil)
2722 (integer :tag "Max number of tagged entries")
2723 (repeat
2724 (cons (choice :tag "Agenda type"
2725 (const agenda)
2726 (const todo)
2727 (const tags)
2728 (const search))
2729 (integer :tag "Max number of tagged entries")))))
2731 (defcustom org-agenda-max-effort nil
2732 "Maximum cumulated effort duration for the agenda.
2733 This can be nil (no limit) or a number of minutes (as an integer)
2734 or an alist of agenda types with an associated number of minutes
2735 to limit entries to in this type."
2736 :version "24.4"
2737 :package-version '(Org . "8.0")
2738 :group 'org-agenda-custom-commands
2739 :type '(choice (symbol :tag "No limit" nil)
2740 (integer :tag "Max number of minutes")
2741 (repeat
2742 (cons (choice :tag "Agenda type"
2743 (const agenda)
2744 (const todo)
2745 (const tags)
2746 (const search))
2747 (integer :tag "Max number of minutes")))))
2749 (defvar org-agenda-keep-restricted-file-list nil)
2750 (defvar org-keys nil)
2751 (defvar org-match nil)
2752 ;;;###autoload
2753 (defun org-agenda (&optional arg org-keys restriction)
2754 "Dispatch agenda commands to collect entries to the agenda buffer.
2755 Prompts for a command to execute. Any prefix arg will be passed
2756 on to the selected command. The default selections are:
2758 a Call `org-agenda-list' to display the agenda for current day or week.
2759 t Call `org-todo-list' to display the global todo list.
2760 T Call `org-todo-list' to display the global todo list, select only
2761 entries with a specific TODO keyword (the user gets a prompt).
2762 m Call `org-tags-view' to display headlines with tags matching
2763 a condition (the user is prompted for the condition).
2764 M Like `m', but select only TODO entries, no ordinary headlines.
2765 e Export views to associated files.
2766 s Search entries for keywords.
2767 S Search entries for keywords, only with TODO keywords.
2768 / Multi occur across all agenda files and also files listed
2769 in `org-agenda-text-search-extra-files'.
2770 < Restrict agenda commands to buffer, subtree, or region.
2771 Press several times to get the desired effect.
2772 > Remove a previous restriction.
2773 # List \"stuck\" projects.
2774 ! Configure what \"stuck\" means.
2775 C Configure custom agenda commands.
2777 More commands can be added by configuring the variable
2778 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2779 searches can be pre-defined in this way.
2781 If the current buffer is in Org mode and visiting a file, you can also
2782 first press `<' once to indicate that the agenda should be temporarily
2783 \(until the next use of `\\[org-agenda]') restricted to the current file.
2784 Pressing `<' twice means to restrict to the current subtree or region
2785 \(if active)."
2786 (interactive "P")
2787 (catch 'exit
2788 (let* ((prefix-descriptions nil)
2789 (org-agenda-buffer-name org-agenda-buffer-name)
2790 (org-agenda-window-setup (if (equal (buffer-name)
2791 org-agenda-buffer-name)
2792 'current-window
2793 org-agenda-window-setup))
2794 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2795 (org-agenda-custom-commands
2796 ;; normalize different versions
2797 (delq nil
2798 (mapcar
2799 (lambda (x)
2800 (cond ((stringp (cdr x))
2801 (push x prefix-descriptions)
2802 nil)
2803 ((stringp (nth 1 x)) x)
2804 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2805 (t (cons (car x) (cons "" (cdr x))))))
2806 org-agenda-custom-commands)))
2807 (org-agenda-custom-commands
2808 (org-contextualize-keys
2809 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2810 (buf (current-buffer))
2811 (bfn (buffer-file-name (buffer-base-buffer)))
2812 entry key type org-match lprops ans)
2813 ;; Turn off restriction unless there is an overriding one,
2814 (unless org-agenda-overriding-restriction
2815 (unless org-agenda-keep-restricted-file-list
2816 ;; There is a request to keep the file list in place
2817 (put 'org-agenda-files 'org-restrict nil))
2818 (setq org-agenda-restrict nil)
2819 (move-marker org-agenda-restrict-begin nil)
2820 (move-marker org-agenda-restrict-end nil))
2821 ;; Delete old local properties
2822 (put 'org-agenda-redo-command 'org-lprops nil)
2823 ;; Delete previously set last-arguments
2824 (put 'org-agenda-redo-command 'last-args nil)
2825 ;; Remember where this call originated
2826 (setq org-agenda-last-dispatch-buffer (current-buffer))
2827 (unless org-keys
2828 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2829 org-keys (car ans)
2830 restriction (cdr ans)))
2831 ;; If we have sticky agenda buffers, set a name for the buffer,
2832 ;; depending on the invoking keys. The user may still set this
2833 ;; as a command option, which will overwrite what we do here.
2834 (when org-agenda-sticky
2835 (setq org-agenda-buffer-name
2836 (format "*Org Agenda(%s)*" org-keys)))
2837 ;; Establish the restriction, if any
2838 (when (and (not org-agenda-overriding-restriction) restriction)
2839 (put 'org-agenda-files 'org-restrict (list bfn))
2840 (cond
2841 ((eq restriction 'region)
2842 (setq org-agenda-restrict (current-buffer))
2843 (move-marker org-agenda-restrict-begin (region-beginning))
2844 (move-marker org-agenda-restrict-end (region-end)))
2845 ((eq restriction 'subtree)
2846 (save-excursion
2847 (setq org-agenda-restrict (current-buffer))
2848 (org-back-to-heading t)
2849 (move-marker org-agenda-restrict-begin (point))
2850 (move-marker org-agenda-restrict-end
2851 (progn (org-end-of-subtree t)))))))
2853 ;; For example the todo list should not need it (but does...)
2854 (cond
2855 ((setq entry (assoc org-keys org-agenda-custom-commands))
2856 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2857 (progn
2858 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
2859 lprops (nth 4 entry))
2860 (when org-agenda-sticky
2861 (setq org-agenda-buffer-name
2862 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2863 (format "*Org Agenda(%s)*" org-keys))))
2864 (put 'org-agenda-redo-command 'org-lprops lprops)
2865 (cond
2866 ((eq type 'agenda)
2867 (org-let lprops '(org-agenda-list current-prefix-arg)))
2868 ((eq type 'agenda*)
2869 (org-let lprops '(org-agenda-list current-prefix-arg nil nil t)))
2870 ((eq type 'alltodo)
2871 (org-let lprops '(org-todo-list current-prefix-arg)))
2872 ((eq type 'search)
2873 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
2874 ((eq type 'stuck)
2875 (org-let lprops '(org-agenda-list-stuck-projects
2876 current-prefix-arg)))
2877 ((eq type 'tags)
2878 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2879 ((eq type 'tags-todo)
2880 (org-let lprops '(org-tags-view '(4) org-match)))
2881 ((eq type 'todo)
2882 (org-let lprops '(org-todo-list org-match)))
2883 ((eq type 'tags-tree)
2884 (org-check-for-org-mode)
2885 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
2886 ((eq type 'todo-tree)
2887 (org-check-for-org-mode)
2888 (org-let lprops
2889 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2890 (regexp-quote org-match) "\\>"))))
2891 ((eq type 'occur-tree)
2892 (org-check-for-org-mode)
2893 (org-let lprops '(org-occur org-match)))
2894 ((functionp type)
2895 (org-let lprops '(funcall type org-match)))
2896 ((fboundp type)
2897 (org-let lprops '(funcall type org-match)))
2898 (t (user-error "Invalid custom agenda command type %s" type))))
2899 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2900 ((equal org-keys "C")
2901 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2902 (customize-variable 'org-agenda-custom-commands))
2903 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2904 ((equal org-keys "s") (call-interactively 'org-search-view))
2905 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2906 ((equal org-keys "t") (call-interactively 'org-todo-list))
2907 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2908 ((equal org-keys "m") (call-interactively 'org-tags-view))
2909 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2910 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2911 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
2912 (add-hook
2913 'post-command-hook
2914 (lambda ()
2915 (unless (current-message)
2916 (let* ((m (org-agenda-get-any-marker))
2917 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2918 (when note
2919 (message "FLAGGING-NOTE ([?] for more info): %s"
2920 (org-add-props
2921 (replace-regexp-in-string
2922 "\\\\n" "//"
2923 (copy-sequence note))
2924 nil 'face 'org-warning))))))
2925 t t))
2926 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2927 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2928 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
2929 (t (user-error "Invalid agenda key"))))))
2931 (defvar org-agenda-multi)
2933 (defun org-agenda-append-agenda ()
2934 "Append another agenda view to the current one.
2935 This function allows interactive building of block agendas.
2936 Agenda views are separated by `org-agenda-block-separator'."
2937 (interactive)
2938 (unless (derived-mode-p 'org-agenda-mode)
2939 (user-error "Can only append from within agenda buffer"))
2940 (let ((org-agenda-multi t))
2941 (org-agenda)
2942 (widen)
2943 (org-agenda-finalize)
2944 (setq buffer-read-only t)
2945 (org-agenda-fit-window-to-buffer)))
2947 (defun org-agenda-normalize-custom-commands (cmds)
2948 "Normalize custom commands CMDS."
2949 (delq nil
2950 (mapcar
2951 (lambda (x)
2952 (cond ((stringp (cdr x)) nil)
2953 ((stringp (nth 1 x)) x)
2954 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2955 (t (cons (car x) (cons "" (cdr x))))))
2956 cmds)))
2958 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2959 "The user interface for selecting an agenda command."
2960 (catch 'exit
2961 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2962 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2963 (region-p (org-region-active-p))
2964 (custom org-agenda-custom-commands)
2965 (selstring "")
2966 restriction second-time
2967 c entry key type match prefixes rmheader header-end custom1 desc
2968 line lines left right n n1)
2969 (save-window-excursion
2970 (delete-other-windows)
2971 (org-switch-to-buffer-other-window " *Agenda Commands*")
2972 (erase-buffer)
2973 (insert (eval-when-compile
2974 (let ((header
2975 "Press key for an agenda command:
2976 -------------------------------- < Buffer, subtree/region restriction
2977 a Agenda for current week or day > Remove restriction
2978 t List of all TODO entries e Export agenda views
2979 m Match a TAGS/PROP/TODO query T Entries with special TODO kwd
2980 s Search for keywords M Like m, but only TODO entries
2981 / Multi-occur S Like s, but only TODO entries
2982 ? Find :FLAGGED: entries C Configure custom agenda commands
2983 * Toggle sticky agenda views # List stuck projects (!=configure)
2985 (start 0))
2986 (while (string-match
2987 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2988 header start)
2989 (setq start (match-end 0))
2990 (add-text-properties (match-beginning 2) (match-end 2)
2991 '(face bold) header))
2992 header)))
2993 (setq header-end (point-marker))
2994 (while t
2995 (setq custom1 custom)
2996 (when (eq rmheader t)
2997 (org-goto-line 1)
2998 (re-search-forward ":" nil t)
2999 (delete-region (match-end 0) (point-at-eol))
3000 (forward-char 1)
3001 (looking-at "-+")
3002 (delete-region (match-end 0) (point-at-eol))
3003 (move-marker header-end (match-end 0)))
3004 (goto-char header-end)
3005 (delete-region (point) (point-max))
3007 ;; Produce all the lines that describe custom commands and prefixes
3008 (setq lines nil)
3009 (while (setq entry (pop custom1))
3010 (setq key (car entry) desc (nth 1 entry)
3011 type (nth 2 entry)
3012 match (nth 3 entry))
3013 (if (> (length key) 1)
3014 (cl-pushnew (string-to-char key) prefixes :test #'equal)
3015 (setq line
3016 (format
3017 "%-4s%-14s"
3018 (org-add-props (copy-sequence key)
3019 '(face bold))
3020 (cond
3021 ((string-match "\\S-" desc) desc)
3022 ((eq type 'agenda) "Agenda for current week or day")
3023 ((eq type 'agenda*) "Appointments for current week or day")
3024 ((eq type 'alltodo) "List of all TODO entries")
3025 ((eq type 'search) "Word search")
3026 ((eq type 'stuck) "List of stuck projects")
3027 ((eq type 'todo) "TODO keyword")
3028 ((eq type 'tags) "Tags query")
3029 ((eq type 'tags-todo) "Tags (TODO)")
3030 ((eq type 'tags-tree) "Tags tree")
3031 ((eq type 'todo-tree) "TODO kwd tree")
3032 ((eq type 'occur-tree) "Occur tree")
3033 ((functionp type) (if (symbolp type)
3034 (symbol-name type)
3035 "Lambda expression"))
3036 (t "???"))))
3037 (cond
3038 ((not (org-string-nw-p match)) nil)
3039 (org-agenda-menu-show-matcher
3040 (setq line
3041 (concat line ": "
3042 (cond
3043 ((stringp match)
3044 (propertize match 'face 'org-warning))
3045 ((listp type)
3046 (format "set of %d commands" (length type)))))))
3048 (org-add-props line nil 'help-echo (concat "Matcher: " match))))
3049 (push line lines)))
3050 (setq lines (nreverse lines))
3051 (when prefixes
3052 (mapc (lambda (x)
3053 (push
3054 (format "%s %s"
3055 (org-add-props (char-to-string x)
3056 nil 'face 'bold)
3057 (or (cdr (assoc (concat selstring
3058 (char-to-string x))
3059 prefix-descriptions))
3060 "Prefix key"))
3061 lines))
3062 prefixes))
3064 ;; Check if we should display in two columns
3065 (if org-agenda-menu-two-columns
3066 (progn
3067 (setq n (length lines)
3068 n1 (+ (/ n 2) (mod n 2))
3069 right (nthcdr n1 lines)
3070 left (copy-sequence lines))
3071 (setcdr (nthcdr (1- n1) left) nil))
3072 (setq left lines right nil))
3073 (while left
3074 (insert "\n" (pop left))
3075 (when right
3076 (if (< (current-column) 40)
3077 (move-to-column 40 t)
3078 (insert " "))
3079 (insert (pop right))))
3081 ;; Make the window the right size
3082 (goto-char (point-min))
3083 (if second-time
3084 (when (not (pos-visible-in-window-p (point-max)))
3085 (org-fit-window-to-buffer))
3086 (setq second-time t)
3087 (org-fit-window-to-buffer))
3089 ;; Ask for selection
3090 (message "Press key for agenda command%s:"
3091 (if (or restrict-ok org-agenda-overriding-restriction)
3092 (if org-agenda-overriding-restriction
3093 " (restriction lock active)"
3094 (if restriction
3095 (format " (restricted to %s)" restriction)
3096 " (unrestricted)"))
3097 ""))
3098 (setq c (read-char-exclusive))
3099 (message "")
3100 (cond
3101 ((assoc (char-to-string c) custom)
3102 (setq selstring (concat selstring (char-to-string c)))
3103 (throw 'exit (cons selstring restriction)))
3104 ((memq c prefixes)
3105 (setq selstring (concat selstring (char-to-string c))
3106 prefixes nil
3107 rmheader (or rmheader t)
3108 custom (delq nil (mapcar
3109 (lambda (x)
3110 (if (or (= (length (car x)) 1)
3111 (/= (string-to-char (car x)) c))
3113 (cons (substring (car x) 1) (cdr x))))
3114 custom))))
3115 ((eq c ?*)
3116 (call-interactively 'org-toggle-sticky-agenda)
3117 (sit-for 2))
3118 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
3119 (message "Restriction is only possible in Org buffers")
3120 (ding) (sit-for 1))
3121 ((eq c ?1)
3122 (org-agenda-remove-restriction-lock 'noupdate)
3123 (setq restriction 'buffer))
3124 ((eq c ?0)
3125 (org-agenda-remove-restriction-lock 'noupdate)
3126 (setq restriction (if region-p 'region 'subtree)))
3127 ((eq c ?<)
3128 (org-agenda-remove-restriction-lock 'noupdate)
3129 (setq restriction
3130 (cond
3131 ((eq restriction 'buffer)
3132 (if region-p 'region 'subtree))
3133 ((memq restriction '(subtree region))
3134 nil)
3135 (t 'buffer))))
3136 ((eq c ?>)
3137 (org-agenda-remove-restriction-lock 'noupdate)
3138 (setq restriction nil))
3139 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
3140 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
3141 ((and (> (length selstring) 0) (eq c ?\d))
3142 (delete-window)
3143 (org-agenda-get-restriction-and-command prefix-descriptions))
3145 ((equal c ?q) (error "Abort"))
3146 (t (user-error "Invalid key %c" c))))))))
3148 (defun org-agenda-fit-window-to-buffer ()
3149 "Fit the window to the buffer size."
3150 (and (memq org-agenda-window-setup '(reorganize-frame))
3151 (fboundp 'fit-window-to-buffer)
3152 (if (and (= (cdr org-agenda-window-frame-fractions) 1.0)
3153 (= (car org-agenda-window-frame-fractions) 1.0))
3154 (delete-other-windows)
3155 (org-fit-window-to-buffer
3157 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3158 (floor (* (frame-height) (car org-agenda-window-frame-fractions)))))))
3160 (defvar org-cmd nil)
3161 (defvar org-agenda-overriding-cmd nil)
3162 (defvar org-agenda-overriding-arguments nil)
3163 (defvar org-agenda-overriding-cmd-arguments nil)
3164 (defun org-agenda-run-series (name series)
3165 "Run agenda NAME as a SERIES of agenda commands."
3166 (org-let (nth 1 series) '(org-agenda-prepare name))
3167 ;; We need to reset agenda markers here, because when constructing a
3168 ;; block agenda, the individual blocks do not do that.
3169 (org-agenda-reset-markers)
3170 (let* ((org-agenda-multi t)
3171 (redo (list 'org-agenda-run-series name (list 'quote series)))
3172 (cmds (car series))
3173 (gprops (nth 1 series))
3174 match ;; The byte compiler incorrectly complains about this. Keep it!
3175 org-cmd type lprops)
3176 (while (setq org-cmd (pop cmds))
3177 (setq type (car org-cmd))
3178 (setq match (eval (nth 1 org-cmd)))
3179 (setq lprops (nth 2 org-cmd))
3180 (let ((org-agenda-overriding-arguments
3181 (if (eq org-agenda-overriding-cmd org-cmd)
3182 (or org-agenda-overriding-arguments
3183 org-agenda-overriding-cmd-arguments))))
3184 (cond
3185 ((eq type 'agenda)
3186 (org-let2 gprops lprops
3187 '(call-interactively 'org-agenda-list)))
3188 ((eq type 'agenda*)
3189 (org-let2 gprops lprops
3190 '(funcall 'org-agenda-list nil nil t)))
3191 ((eq type 'alltodo)
3192 (org-let2 gprops lprops
3193 '(call-interactively 'org-todo-list)))
3194 ((eq type 'search)
3195 (org-let2 gprops lprops
3196 '(org-search-view current-prefix-arg match nil)))
3197 ((eq type 'stuck)
3198 (org-let2 gprops lprops
3199 '(call-interactively 'org-agenda-list-stuck-projects)))
3200 ((eq type 'tags)
3201 (org-let2 gprops lprops
3202 '(org-tags-view current-prefix-arg match)))
3203 ((eq type 'tags-todo)
3204 (org-let2 gprops lprops
3205 '(org-tags-view '(4) match)))
3206 ((eq type 'todo)
3207 (org-let2 gprops lprops
3208 '(org-todo-list match)))
3209 ((fboundp type)
3210 (org-let2 gprops lprops
3211 '(funcall type match)))
3212 (t (error "Invalid type in command series")))))
3213 (widen)
3214 (let ((inhibit-read-only t))
3215 (add-text-properties (point-min) (point-max)
3216 `(org-series t org-series-redo-cmd ,redo)))
3217 (setq org-agenda-redo-command redo)
3218 (goto-char (point-min)))
3219 (org-agenda-fit-window-to-buffer)
3220 (org-let (nth 1 series) '(org-agenda-finalize)))
3222 ;;;###autoload
3223 (defmacro org-batch-agenda (cmd-key &rest parameters)
3224 "Run an agenda command in batch mode and send the result to STDOUT.
3225 If CMD-KEY is a string of length 1, it is used as a key in
3226 `org-agenda-custom-commands' and triggers this command. If it is a
3227 longer string it is used as a tags/todo match string.
3228 Parameters are alternating variable names and values that will be bound
3229 before running the agenda command."
3230 (org-eval-in-environment (org-make-parameter-alist parameters)
3231 (let (org-agenda-sticky)
3232 (if (> (length cmd-key) 1)
3233 (org-tags-view nil cmd-key)
3234 (org-agenda nil cmd-key))))
3235 (set-buffer org-agenda-buffer-name)
3236 (princ (buffer-string)))
3238 (defvar org-agenda-info nil)
3240 ;;;###autoload
3241 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
3242 "Run an agenda command in batch mode and send the result to STDOUT.
3243 If CMD-KEY is a string of length 1, it is used as a key in
3244 `org-agenda-custom-commands' and triggers this command. If it is a
3245 longer string it is used as a tags/todo match string.
3246 Parameters are alternating variable names and values that will be bound
3247 before running the agenda command.
3249 The output gives a line for each selected agenda item. Each
3250 item is a list of comma-separated values, like this:
3252 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3254 category The category of the item
3255 head The headline, without TODO kwd, TAGS and PRIORITY
3256 type The type of the agenda entry, can be
3257 todo selected in TODO match
3258 tagsmatch selected in tags match
3259 diary imported from diary
3260 deadline a deadline on given date
3261 scheduled scheduled on given date
3262 timestamp entry has timestamp on given date
3263 closed entry was closed on given date
3264 upcoming-deadline warning about deadline
3265 past-scheduled forwarded scheduled item
3266 block entry has date block including g. date
3267 todo The todo keyword, if any
3268 tags All tags including inherited ones, separated by colons
3269 date The relevant date, like 2007-2-14
3270 time The time, like 15:00-16:50
3271 extra Sting with extra planning info
3272 priority-l The priority letter if any was given
3273 priority-n The computed numerical priority
3274 agenda-day The day in the agenda where this is listed"
3275 (org-eval-in-environment (append '((org-agenda-remove-tags t))
3276 (org-make-parameter-alist parameters))
3277 (if (> (length cmd-key) 2)
3278 (org-tags-view nil cmd-key)
3279 (org-agenda nil cmd-key)))
3280 (set-buffer org-agenda-buffer-name)
3281 (let ((lines (org-split-string (buffer-string) "\n")))
3282 (dolist (line lines)
3283 (when (get-text-property 0 'org-category line)
3284 (setq org-agenda-info
3285 (org-fix-agenda-info (text-properties-at 0 line)))
3286 (princ
3287 (mapconcat 'org-agenda-export-csv-mapper
3288 '(org-category txt type todo tags date time extra
3289 priority-letter priority agenda-day)
3290 ","))
3291 (princ "\n")))))
3293 (defun org-fix-agenda-info (props)
3294 "Make sure all properties on an agenda item have a canonical form.
3295 This ensures the export commands can easily use it."
3296 (let (tmp re)
3297 (when (setq tmp (plist-get props 'tags))
3298 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3299 (when (setq tmp (plist-get props 'date))
3300 (when (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3301 (let ((calendar-date-display-form '(year "-" month "-" day)))
3302 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3304 (setq tmp (calendar-date-string tmp)))
3305 (setq props (plist-put props 'date tmp)))
3306 (when (setq tmp (plist-get props 'day))
3307 (when (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3308 (let ((calendar-date-display-form '(year "-" month "-" day)))
3309 (setq tmp (calendar-date-string tmp)))
3310 (setq props (plist-put props 'day tmp))
3311 (setq props (plist-put props 'agenda-day tmp)))
3312 (when (setq tmp (plist-get props 'txt))
3313 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3314 (plist-put props 'priority-letter (match-string 1 tmp))
3315 (setq tmp (replace-match "" t t tmp)))
3316 (when (and (setq re (plist-get props 'org-todo-regexp))
3317 (setq re (concat "\\`\\.*" re " ?"))
3318 (let ((case-fold-search nil)) (string-match re tmp)))
3319 (plist-put props 'todo (match-string 1 tmp))
3320 (setq tmp (replace-match "" t t tmp)))
3321 (plist-put props 'txt tmp)))
3322 props)
3324 (defun org-agenda-export-csv-mapper (prop)
3325 (let ((res (plist-get org-agenda-info prop)))
3326 (setq res
3327 (cond
3328 ((not res) "")
3329 ((stringp res) res)
3330 (t (prin1-to-string res))))
3331 (org-trim (replace-regexp-in-string "," ";" res nil t))))
3333 ;;;###autoload
3334 (defun org-store-agenda-views (&rest parameters)
3335 "Store agenda views."
3336 (interactive)
3337 (eval (list 'org-batch-store-agenda-views)))
3339 ;;;###autoload
3340 (defmacro org-batch-store-agenda-views (&rest parameters)
3341 "Run all custom agenda commands that have a file argument."
3342 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3343 (pop-up-frames nil)
3344 (dir default-directory)
3345 (pars (org-make-parameter-alist parameters))
3346 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
3347 (save-window-excursion
3348 (while cmds
3349 (setq cmd (pop cmds)
3350 thiscmdkey (car cmd)
3351 thiscmdcmd (cdr cmd)
3352 match (nth 2 thiscmdcmd)
3353 bufname (if org-agenda-sticky
3354 (or (and (stringp match)
3355 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3356 (format "*Org Agenda(%s)*" thiscmdkey))
3357 org-agenda-buffer-name)
3358 cmd-or-set (nth 2 cmd)
3359 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3360 files (nth (if (listp cmd-or-set) 4 5) cmd))
3361 (if (stringp files) (setq files (list files)))
3362 (when files
3363 (org-eval-in-environment (append org-agenda-exporter-settings
3364 opts pars)
3365 (org-agenda nil thiscmdkey))
3366 (set-buffer bufname)
3367 (while files
3368 (org-eval-in-environment (append org-agenda-exporter-settings
3369 opts pars)
3370 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3371 (and (get-buffer bufname)
3372 (kill-buffer bufname)))))))
3374 (defvar org-agenda-current-span nil
3375 "The current span used in the agenda view.") ; local variable in the agenda buffer
3376 (defun org-agenda-mark-header-line (pos)
3377 "Mark the line at POS as an agenda structure header."
3378 (save-excursion
3379 (goto-char pos)
3380 (put-text-property (point-at-bol) (point-at-eol)
3381 'org-agenda-structural-header t)
3382 (when org-agenda-title-append
3383 (put-text-property (point-at-bol) (point-at-eol)
3384 'org-agenda-title-append org-agenda-title-append))))
3386 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3387 (defvar org-agenda-write-buffer-name "Agenda View")
3388 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3389 "Write the current buffer (an agenda view) as a file.
3391 Depending on the extension of the file name, plain text (.txt),
3392 HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
3393 If the extension is .ics, translate visible agenda into iCalendar
3394 format. If the extension is .org, collect all subtrees
3395 corresponding to the agenda entries and add them in an .org file.
3397 With prefix argument OPEN, open the new file immediately. If
3398 NOSETTINGS is given, do not scope the settings of
3399 `org-agenda-exporter-settings' into the export commands. This is
3400 used when the settings have already been scoped and we do not
3401 wish to overrule other, higher priority settings. If
3402 AGENDA-BUFFER-NAME is provided, use this as the buffer name for
3403 the agenda to write."
3404 (interactive "FWrite agenda to file: \nP")
3405 (if (or (not (file-writable-p file))
3406 (and (file-exists-p file)
3407 (if (called-interactively-p 'any)
3408 (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
3409 (user-error "Cannot write agenda to file %s" file))
3410 (org-let (if nosettings nil org-agenda-exporter-settings)
3411 '(save-excursion
3412 (save-window-excursion
3413 (let ((bs (copy-sequence (buffer-string)))
3414 (extension (file-name-extension file))
3415 (default-directory (file-name-directory file))
3416 beg content)
3417 (with-temp-buffer
3418 (rename-buffer org-agenda-write-buffer-name t)
3419 (set-buffer-modified-p nil)
3420 (insert bs)
3421 (org-agenda-remove-marked-text 'invisible 'org-filtered)
3422 (run-hooks 'org-agenda-before-write-hook)
3423 (cond
3424 ((bound-and-true-p org-mobile-creating-agendas)
3425 (org-mobile-write-agenda-for-mobile file))
3426 ((string= "org" extension)
3427 (let (content p m message-log-max)
3428 (goto-char (point-min))
3429 (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
3430 (goto-char p)
3431 (setq m (get-text-property (point) 'org-hd-marker))
3432 (when m
3433 (push (save-excursion
3434 (set-buffer (marker-buffer m))
3435 (goto-char m)
3436 (org-copy-subtree 1 nil t t)
3437 org-subtree-clip)
3438 content)))
3439 (find-file file)
3440 (erase-buffer)
3441 (dolist (s content) (org-paste-subtree 1 s))
3442 (write-file file)
3443 (kill-buffer (current-buffer))
3444 (message "Org file written to %s" file)))
3445 ((member extension '("html" "htm"))
3446 (or (require 'htmlize nil t)
3447 (error "Please install htmlize from https://github.com/hniksic/emacs-htmlize"))
3448 (set-buffer (htmlize-buffer (current-buffer)))
3449 (when org-agenda-export-html-style
3450 ;; replace <style> section with org-agenda-export-html-style
3451 (goto-char (point-min))
3452 (kill-region (- (search-forward "<style") 6)
3453 (search-forward "</style>"))
3454 (insert org-agenda-export-html-style))
3455 (write-file file)
3456 (kill-buffer (current-buffer))
3457 (message "HTML written to %s" file))
3458 ((string= "ps" extension)
3459 (require 'ps-print)
3460 (ps-print-buffer-with-faces file)
3461 (message "Postscript written to %s" file))
3462 ((string= "pdf" extension)
3463 (require 'ps-print)
3464 (ps-print-buffer-with-faces
3465 (concat (file-name-sans-extension file) ".ps"))
3466 (call-process "ps2pdf" nil nil nil
3467 (expand-file-name
3468 (concat (file-name-sans-extension file) ".ps"))
3469 (expand-file-name file))
3470 (delete-file (concat (file-name-sans-extension file) ".ps"))
3471 (message "PDF written to %s" file))
3472 ((string= "ics" extension)
3473 (require 'ox-icalendar)
3474 (org-icalendar-export-current-agenda (expand-file-name file)))
3476 (let ((bs (buffer-string)))
3477 (find-file file)
3478 (erase-buffer)
3479 (insert bs)
3480 (save-buffer 0)
3481 (kill-buffer (current-buffer))
3482 (message "Plain text written to %s" file))))))))
3483 (set-buffer (or agenda-bufname
3484 (and (called-interactively-p 'any) (buffer-name))
3485 org-agenda-buffer-name)))
3486 (when open (org-open-file file)))
3488 (defun org-agenda-remove-marked-text (property &optional value)
3489 "Delete all text marked with VALUE of PROPERTY.
3490 VALUE defaults to t."
3491 (let (beg)
3492 (setq value (or value t))
3493 (while (setq beg (text-property-any (point-min) (point-max)
3494 property value))
3495 (delete-region
3496 beg (or (next-single-property-change beg property)
3497 (point-max))))))
3499 (defun org-agenda-add-entry-text ()
3500 "Add entry text to agenda lines.
3501 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3502 entry text following headings shown in the agenda.
3503 Drawers will be excluded, also the line with scheduling/deadline info."
3504 (when (and (> org-agenda-add-entry-text-maxlines 0)
3505 (not (bound-and-true-p org-mobile-creating-agendas)))
3506 (let (m txt)
3507 (goto-char (point-min))
3508 (while (not (eobp))
3509 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3510 (beginning-of-line 2)
3511 (setq txt (org-agenda-get-some-entry-text
3512 m org-agenda-add-entry-text-maxlines " > "))
3513 (end-of-line 1)
3514 (if (string-match "\\S-" txt)
3515 (insert "\n" txt)
3516 (or (eobp) (forward-char 1))))))))
3518 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3519 &rest keep)
3520 "Extract entry text from MARKER, at most N-LINES lines.
3521 This will ignore drawers etc, just get the text.
3522 If INDENT is given, prefix every line with this string. If KEEP is
3523 given, it is a list of symbols, defining stuff that should not be
3524 removed from the entry content. Currently only `planning' is allowed here."
3525 (let (txt drawer-re kwd-time-re ind)
3526 (save-excursion
3527 (with-current-buffer (marker-buffer marker)
3528 (if (not (derived-mode-p 'org-mode))
3529 (setq txt "")
3530 (org-with-wide-buffer
3531 (goto-char marker)
3532 (end-of-line 1)
3533 (setq txt (buffer-substring
3534 (min (1+ (point)) (point-max))
3535 (progn (outline-next-heading) (point)))
3536 drawer-re org-drawer-regexp
3537 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3538 ".*\n?"))
3539 (with-temp-buffer
3540 (insert txt)
3541 (when org-agenda-add-entry-text-descriptive-links
3542 (goto-char (point-min))
3543 (while (org-activate-links (point-max))
3544 (add-text-properties (match-beginning 0) (match-end 0)
3545 '(face org-link))))
3546 (goto-char (point-min))
3547 (while (re-search-forward org-link-bracket-re (point-max) t)
3548 (set-text-properties (match-beginning 0) (match-end 0)
3549 nil))
3550 (goto-char (point-min))
3551 (while (re-search-forward drawer-re nil t)
3552 (delete-region
3553 (match-beginning 0)
3554 (progn (re-search-forward
3555 "^[ \t]*:END:.*\n?" nil 'move)
3556 (point))))
3557 (unless (member 'planning keep)
3558 (goto-char (point-min))
3559 (while (re-search-forward kwd-time-re nil t)
3560 (replace-match "")))
3561 (goto-char (point-min))
3562 (when org-agenda-entry-text-exclude-regexps
3563 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3564 (while (setq re (pop re-list))
3565 (goto-char (point-min))
3566 (while (re-search-forward re nil t)
3567 (replace-match "")))))
3568 (goto-char (point-max))
3569 (skip-chars-backward " \t\n")
3570 (when (looking-at "[ \t\n]+\\'") (replace-match ""))
3572 ;; find and remove min common indentation
3573 (goto-char (point-min))
3574 (untabify (point-min) (point-max))
3575 (setq ind (current-indentation))
3576 (while (not (eobp))
3577 (unless (looking-at "[ \t]*$")
3578 (setq ind (min ind (current-indentation))))
3579 (beginning-of-line 2))
3580 (goto-char (point-min))
3581 (while (not (eobp))
3582 (unless (looking-at "[ \t]*$")
3583 (move-to-column ind)
3584 (delete-region (point-at-bol) (point)))
3585 (beginning-of-line 2))
3587 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3589 (goto-char (point-min))
3590 (when indent
3591 (while (and (not (eobp)) (re-search-forward "^" nil t))
3592 (replace-match indent t t)))
3593 (goto-char (point-min))
3594 (while (looking-at "[ \t]*\n") (replace-match ""))
3595 (goto-char (point-max))
3596 (when (> (org-current-line)
3597 n-lines)
3598 (org-goto-line (1+ n-lines))
3599 (backward-char 1))
3600 (setq txt (buffer-substring (point-min) (point))))))))
3601 txt))
3603 (defun org-check-for-org-mode ()
3604 "Make sure current buffer is in Org mode. Error if not."
3605 (or (derived-mode-p 'org-mode)
3606 (error "Cannot execute Org agenda command on buffer in %s"
3607 major-mode)))
3609 ;;; Agenda prepare and finalize
3611 (defvar org-agenda-multi nil) ; dynamically scoped
3612 (defvar org-agenda-pre-window-conf nil)
3613 (defvar org-agenda-columns-active nil)
3614 (defvar org-agenda-name nil)
3615 (defvar org-agenda-tag-filter nil)
3616 (defvar org-agenda-category-filter nil)
3617 (defvar org-agenda-regexp-filter nil)
3618 (defvar org-agenda-effort-filter nil)
3619 (defvar org-agenda-top-headline-filter nil)
3620 (defvar org-agenda-tag-filter-preset nil
3621 "A preset of the tags filter used for secondary agenda filtering.
3622 This must be a list of strings, each string must be a single tag preceded
3623 by \"+\" or \"-\".
3624 This variable should not be set directly, but agenda custom commands can
3625 bind it in the options section. The preset filter is a global property of
3626 the entire agenda view. In a block agenda, it will not work reliably to
3627 define a filter for one of the individual blocks. You need to set it in
3628 the global options and expect it to be applied to the entire view.")
3630 (defvar org-agenda-category-filter-preset nil
3631 "A preset of the category filter used for secondary agenda filtering.
3632 This must be a list of strings, each string must be a single category
3633 preceded by \"+\" or \"-\".
3634 This variable should not be set directly, but agenda custom commands can
3635 bind it in the options section. The preset filter is a global property of
3636 the entire agenda view. In a block agenda, it will not work reliably to
3637 define a filter for one of the individual blocks. You need to set it in
3638 the global options and expect it to be applied to the entire view.")
3640 (defvar org-agenda-regexp-filter-preset nil
3641 "A preset of the regexp filter used for secondary agenda filtering.
3642 This must be a list of strings, each string must be a single regexp
3643 preceded by \"+\" or \"-\".
3644 This variable should not be set directly, but agenda custom commands can
3645 bind it in the options section. The preset filter is a global property of
3646 the entire agenda view. In a block agenda, it will not work reliably to
3647 define a filter for one of the individual blocks. You need to set it in
3648 the global options and expect it to be applied to the entire view.")
3650 (defvar org-agenda-effort-filter-preset nil
3651 "A preset of the effort condition used for secondary agenda filtering.
3652 This must be a list of strings, each string must be a single regexp
3653 preceded by \"+\" or \"-\".
3654 This variable should not be set directly, but agenda custom commands can
3655 bind it in the options section. The preset filter is a global property of
3656 the entire agenda view. In a block agenda, it will not work reliably to
3657 define a filter for one of the individual blocks. You need to set it in
3658 the global options and expect it to be applied to the entire view.")
3660 (defun org-agenda-use-sticky-p ()
3661 "Return non-nil if an agenda buffer named
3662 `org-agenda-buffer-name' exists and should be shown instead of
3663 generating a new one."
3664 (and
3665 ;; turned off by user
3666 org-agenda-sticky
3667 ;; For multi-agenda buffer already exists
3668 (not org-agenda-multi)
3669 ;; buffer found
3670 (get-buffer org-agenda-buffer-name)
3671 ;; C-u parameter is same as last call
3672 (with-current-buffer (get-buffer org-agenda-buffer-name)
3673 (and
3674 (equal current-prefix-arg
3675 org-agenda-last-prefix-arg)
3676 ;; In case user turned stickiness on, while having existing
3677 ;; Agenda buffer active, don't reuse that buffer, because it
3678 ;; does not have org variables local
3679 org-agenda-this-buffer-is-sticky))))
3681 (defun org-agenda-prepare-window (abuf filter-alist)
3682 "Setup agenda buffer in the window.
3683 ABUF is the buffer for the agenda window.
3684 FILTER-ALIST is an alist of filters we need to apply when
3685 `org-agenda-persistent-filter' is non-nil."
3686 (let* ((awin (get-buffer-window abuf)) wconf)
3687 (cond
3688 ((equal (current-buffer) abuf) nil)
3689 (awin (select-window awin))
3690 ((not (setq wconf (current-window-configuration))))
3691 ((eq org-agenda-window-setup 'current-window)
3692 (pop-to-buffer-same-window abuf))
3693 ((eq org-agenda-window-setup 'other-window)
3694 (org-switch-to-buffer-other-window abuf))
3695 ((eq org-agenda-window-setup 'other-frame)
3696 (switch-to-buffer-other-frame abuf))
3697 ((eq org-agenda-window-setup 'only-window)
3698 (delete-other-windows)
3699 (pop-to-buffer-same-window abuf))
3700 ((eq org-agenda-window-setup 'reorganize-frame)
3701 (delete-other-windows)
3702 (org-switch-to-buffer-other-window abuf)))
3703 (setq org-agenda-tag-filter (cdr (assq 'tag filter-alist)))
3704 (setq org-agenda-category-filter (cdr (assq 'cat filter-alist)))
3705 (setq org-agenda-effort-filter (cdr (assq 'effort filter-alist)))
3706 (setq org-agenda-regexp-filter (cdr (assq 're filter-alist)))
3707 ;; Additional test in case agenda is invoked from within agenda
3708 ;; buffer via elisp link.
3709 (unless (equal (current-buffer) abuf)
3710 (pop-to-buffer-same-window abuf))
3711 (setq org-agenda-pre-window-conf
3712 (or wconf org-agenda-pre-window-conf))))
3714 (defun org-agenda-prepare (&optional name)
3715 (let ((filter-alist (when org-agenda-persistent-filter
3716 (with-current-buffer
3717 (get-buffer-create org-agenda-buffer-name)
3718 `((tag . ,org-agenda-tag-filter)
3719 (re . ,org-agenda-regexp-filter)
3720 (effort . ,org-agenda-effort-filter)
3721 (cat . ,org-agenda-category-filter))))))
3722 (if (org-agenda-use-sticky-p)
3723 (progn
3724 (put 'org-agenda-tag-filter :preset-filter nil)
3725 (put 'org-agenda-category-filter :preset-filter nil)
3726 (put 'org-agenda-regexp-filter :preset-filter nil)
3727 ;; Popup existing buffer
3728 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name)
3729 filter-alist)
3730 (message "Sticky Agenda buffer, use `r' to refresh")
3731 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3732 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3733 (setq org-todo-keywords-for-agenda nil)
3734 (put 'org-agenda-tag-filter :preset-filter
3735 org-agenda-tag-filter-preset)
3736 (put 'org-agenda-category-filter :preset-filter
3737 org-agenda-category-filter-preset)
3738 (put 'org-agenda-regexp-filter :preset-filter
3739 org-agenda-regexp-filter-preset)
3740 (put 'org-agenda-effort-filter :preset-filter
3741 org-agenda-effort-filter-preset)
3742 (if org-agenda-multi
3743 (progn
3744 (setq buffer-read-only nil)
3745 (goto-char (point-max))
3746 (unless (or (bobp) org-agenda-compact-blocks
3747 (not org-agenda-block-separator))
3748 (insert "\n"
3749 (if (stringp org-agenda-block-separator)
3750 org-agenda-block-separator
3751 (make-string (window-width) org-agenda-block-separator))
3752 "\n"))
3753 (narrow-to-region (point) (point-max)))
3754 (setq org-done-keywords-for-agenda nil)
3755 ;; Setting any org variables that are in org-agenda-local-vars
3756 ;; list need to be done after the prepare call
3757 (org-agenda-prepare-window
3758 (get-buffer-create org-agenda-buffer-name) filter-alist)
3759 (setq buffer-read-only nil)
3760 (org-agenda-reset-markers)
3761 (let ((inhibit-read-only t)) (erase-buffer))
3762 (org-agenda-mode)
3763 (setq org-agenda-buffer (current-buffer))
3764 (setq org-agenda-contributing-files nil)
3765 (setq org-agenda-columns-active nil)
3766 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3767 (setq org-todo-keywords-for-agenda
3768 (org-uniquify org-todo-keywords-for-agenda))
3769 (setq org-done-keywords-for-agenda
3770 (org-uniquify org-done-keywords-for-agenda))
3771 (setq org-agenda-last-prefix-arg current-prefix-arg)
3772 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3773 (and name (not org-agenda-name)
3774 (setq-local org-agenda-name name)))
3775 (setq buffer-read-only nil))))
3777 (defvar org-overriding-columns-format)
3778 (defvar org-local-columns-format)
3779 (defun org-agenda-finalize ()
3780 "Finishing touch for the agenda buffer, called just before displaying it."
3781 (unless org-agenda-multi
3782 (save-excursion
3783 (let ((inhibit-read-only t))
3784 (goto-char (point-min))
3785 (save-excursion
3786 (while (org-activate-links (point-max))
3787 (add-text-properties (match-beginning 0) (match-end 0)
3788 '(face org-link))))
3789 (unless (eq org-agenda-remove-tags t)
3790 (org-agenda-align-tags))
3791 (unless org-agenda-with-colors
3792 (remove-text-properties (point-min) (point-max) '(face nil)))
3793 (when (bound-and-true-p org-overriding-columns-format)
3794 (setq-local org-local-columns-format
3795 org-overriding-columns-format))
3796 (when org-agenda-view-columns-initially
3797 (org-agenda-columns))
3798 (when org-agenda-fontify-priorities
3799 (org-agenda-fontify-priorities))
3800 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3801 (org-agenda-dim-blocked-tasks))
3802 (org-agenda-mark-clocking-task)
3803 (when org-agenda-entry-text-mode
3804 (org-agenda-entry-text-hide)
3805 (org-agenda-entry-text-show))
3806 (when (and (featurep 'org-habit)
3807 (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
3808 (org-habit-insert-consistency-graphs))
3809 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3810 (unless (or (eq org-agenda-show-inherited-tags 'always)
3811 (and (listp org-agenda-show-inherited-tags)
3812 (memq org-agenda-type org-agenda-show-inherited-tags))
3813 (and (eq org-agenda-show-inherited-tags t)
3814 (or (eq org-agenda-use-tag-inheritance t)
3815 (and (listp org-agenda-use-tag-inheritance)
3816 (not (memq org-agenda-type
3817 org-agenda-use-tag-inheritance))))))
3818 (let (mrk)
3819 (save-excursion
3820 (goto-char (point-min))
3821 (while (equal (forward-line) 0)
3822 (when (setq mrk (get-text-property (point) 'org-hd-marker))
3823 (put-text-property (point-at-bol) (point-at-eol)
3824 'tags
3825 (org-with-point-at mrk
3826 (mapcar #'downcase (org-get-tags)))))))))
3827 (run-hooks 'org-agenda-finalize-hook)
3828 (when org-agenda-top-headline-filter
3829 (org-agenda-filter-top-headline-apply
3830 org-agenda-top-headline-filter))
3831 (when org-agenda-tag-filter
3832 (org-agenda-filter-apply org-agenda-tag-filter 'tag t))
3833 (when (get 'org-agenda-tag-filter :preset-filter)
3834 (org-agenda-filter-apply
3835 (get 'org-agenda-tag-filter :preset-filter) 'tag t))
3836 (when org-agenda-category-filter
3837 (org-agenda-filter-apply org-agenda-category-filter 'category))
3838 (when (get 'org-agenda-category-filter :preset-filter)
3839 (org-agenda-filter-apply
3840 (get 'org-agenda-category-filter :preset-filter) 'category))
3841 (when org-agenda-regexp-filter
3842 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
3843 (when (get 'org-agenda-regexp-filter :preset-filter)
3844 (org-agenda-filter-apply
3845 (get 'org-agenda-regexp-filter :preset-filter) 'regexp))
3846 (when org-agenda-effort-filter
3847 (org-agenda-filter-apply org-agenda-effort-filter 'effort))
3848 (when (get 'org-agenda-effort-filter :preset-filter)
3849 (org-agenda-filter-apply
3850 (get 'org-agenda-effort-filter :preset-filter) 'effort))
3851 (add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)))))
3853 (defun org-agenda-mark-clocking-task ()
3854 "Mark the current clock entry in the agenda if it is present."
3855 ;; We need to widen when `org-agenda-finalize' is called from
3856 ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
3857 (when (bound-and-true-p org-clock-current-task)
3858 (save-restriction
3859 (widen)
3860 (org-agenda-unmark-clocking-task)
3861 (when (marker-buffer org-clock-hd-marker)
3862 (save-excursion
3863 (goto-char (point-min))
3864 (let (s ov)
3865 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3866 (goto-char s)
3867 (when (equal (org-get-at-bol 'org-hd-marker)
3868 org-clock-hd-marker)
3869 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3870 (overlay-put ov 'type 'org-agenda-clocking)
3871 (overlay-put ov 'face 'org-agenda-clocking)
3872 (overlay-put ov 'help-echo
3873 "The clock is running in this item")))))))))
3875 (defun org-agenda-unmark-clocking-task ()
3876 "Unmark the current clocking task."
3877 (mapc (lambda (o)
3878 (when (eq (overlay-get o 'type) 'org-agenda-clocking)
3879 (delete-overlay o)))
3880 (overlays-in (point-min) (point-max))))
3882 (defun org-agenda-fontify-priorities ()
3883 "Make highest priority lines bold, and lowest italic."
3884 (interactive)
3885 (mapc (lambda (o) (when (eq (overlay-get o 'org-type) 'org-priority)
3886 (delete-overlay o)))
3887 (overlays-in (point-min) (point-max)))
3888 (save-excursion
3889 (let (b e p ov h l)
3890 (goto-char (point-min))
3891 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3892 (setq h (or (get-char-property (point) 'org-highest-priority)
3893 org-highest-priority)
3894 l (or (get-char-property (point) 'org-lowest-priority)
3895 org-lowest-priority)
3896 p (string-to-char (match-string 1))
3897 b (match-beginning 0)
3898 e (if (eq org-agenda-fontify-priorities 'cookies)
3899 (match-end 0)
3900 (point-at-eol))
3901 ov (make-overlay b e))
3902 (overlay-put
3903 ov 'face
3904 (let ((special-face
3905 (cond ((org-face-from-face-or-color
3906 'priority 'org-priority
3907 (cdr (assoc p org-priority-faces))))
3908 ((and (listp org-agenda-fontify-priorities)
3909 (org-face-from-face-or-color
3910 'priority 'org-priority
3911 (cdr (assoc p org-agenda-fontify-priorities)))))
3912 ((equal p l) 'italic)
3913 ((equal p h) 'bold))))
3914 (if special-face (list special-face 'org-priority) 'org-priority)))
3915 (overlay-put ov 'org-type 'org-priority)))))
3917 (defvar org-depend-tag-blocked)
3919 (defun org-agenda-dim-blocked-tasks (&optional invisible)
3920 "Dim currently blocked TODOs in the agenda display.
3921 When INVISIBLE is non-nil, hide currently blocked TODO instead of
3922 dimming them."
3923 (interactive "P")
3924 (when (called-interactively-p 'interactive)
3925 (message "Dim or hide blocked tasks..."))
3926 (dolist (o (overlays-in (point-min) (point-max)))
3927 (when (eq (overlay-get o 'org-type) 'org-blocked-todo)
3928 (delete-overlay o)))
3929 (save-excursion
3930 (let ((inhibit-read-only t))
3931 (goto-char (point-min))
3932 (while (let ((pos (text-property-not-all
3933 (point) (point-max) 'org-todo-blocked nil)))
3934 (when pos (goto-char pos)))
3935 (let* ((invisible (eq (org-get-at-bol 'org-todo-blocked) 'invisible))
3936 (ov (make-overlay (if invisible
3937 (line-end-position 0)
3938 (line-beginning-position))
3939 (line-end-position))))
3940 (if invisible
3941 (overlay-put ov 'invisible t)
3942 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3943 (overlay-put ov 'org-type 'org-blocked-todo))
3944 (forward-line))))
3945 (when (called-interactively-p 'interactive)
3946 (message "Dim or hide blocked tasks...done")))
3948 (defun org-agenda--mark-blocked-entry (entry)
3949 "For ENTRY a string with the text property `org-hd-marker', if
3950 the header at `org-hd-marker' is blocked according to
3951 `org-entry-blocked-p', then if `org-agenda-dim-blocked-tasks' is
3952 'invisible and the header is not blocked by checkboxes, set the
3953 text property `org-todo-blocked' to 'invisible, otherwise set it
3954 to t."
3955 (when (get-text-property 0 'todo-state entry)
3956 (let ((entry-marker (get-text-property 0 'org-hd-marker entry))
3957 (org-blocked-by-checkboxes nil)
3958 ;; Necessary so that `org-entry-blocked-p' does not change
3959 ;; the buffer.
3960 (org-depend-tag-blocked nil))
3961 (when entry-marker
3962 (let ((blocked
3963 (with-current-buffer (marker-buffer entry-marker)
3964 (save-excursion
3965 (goto-char entry-marker)
3966 (org-entry-blocked-p)))))
3967 (when blocked
3968 (let ((really-invisible
3969 (and (not org-blocked-by-checkboxes)
3970 (eq org-agenda-dim-blocked-tasks 'invisible))))
3971 (put-text-property
3972 0 (length entry) 'org-todo-blocked
3973 (if really-invisible 'invisible t)
3974 entry)))))))
3975 entry)
3977 (defvar org-agenda-skip-function nil
3978 "Function to be called at each match during agenda construction.
3979 If this function returns nil, the current match should not be skipped.
3980 Otherwise, the function must return a position from where the search
3981 should be continued.
3982 This may also be a Lisp form, it will be evaluated.
3983 Never set this variable using `setq' or so, because then it will apply
3984 to all future agenda commands. If you do want a global skipping condition,
3985 use the option `org-agenda-skip-function-global' instead.
3986 The correct usage for `org-agenda-skip-function' is to bind it with
3987 `let' to scope it dynamically into the agenda-constructing command.
3988 A good way to set it is through options in `org-agenda-custom-commands'.")
3990 (defun org-agenda-skip ()
3991 "Throw to `:skip' in places that should be skipped.
3992 Also moves point to the end of the skipped region, so that search can
3993 continue from there."
3994 (let ((p (point-at-bol)) to)
3995 (when (or
3996 (save-excursion (goto-char p) (looking-at comment-start-skip))
3997 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3998 (get-text-property p :org-archived)
3999 (org-end-of-subtree t))
4000 (and org-agenda-skip-comment-trees
4001 (get-text-property p :org-comment)
4002 (org-end-of-subtree t))
4003 (and (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
4004 (org-agenda-skip-eval org-agenda-skip-function)))
4005 (goto-char to))
4006 (org-in-src-block-p t))
4007 (throw :skip t))))
4009 (defun org-agenda-skip-eval (form)
4010 "If FORM is a function or a list, call (or eval) it and return the result.
4011 `save-excursion' and `save-match-data' are wrapped around the call, so point
4012 and match data are returned to the previous state no matter what these
4013 functions do."
4014 (let (fp)
4015 (and form
4016 (or (setq fp (functionp form))
4017 (consp form))
4018 (save-excursion
4019 (save-match-data
4020 (if fp
4021 (funcall form)
4022 (eval form)))))))
4024 (defvar org-agenda-markers nil
4025 "List of all currently active markers created by `org-agenda'.")
4026 (defvar org-agenda-last-marker-time (float-time)
4027 "Creation time of the last agenda marker.")
4029 (defun org-agenda-new-marker (&optional pos)
4030 "Return a new agenda marker.
4031 Maker is at point, or at POS if non-nil. Org mode keeps a list of
4032 these markers and resets them when they are no longer in use."
4033 (let ((m (copy-marker (or pos (point)) t)))
4034 (setq org-agenda-last-marker-time (float-time))
4035 (if org-agenda-buffer
4036 (with-current-buffer org-agenda-buffer
4037 (push m org-agenda-markers))
4038 (push m org-agenda-markers))
4041 (defun org-agenda-reset-markers ()
4042 "Reset markers created by `org-agenda'."
4043 (while org-agenda-markers
4044 (move-marker (pop org-agenda-markers) nil)))
4046 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
4047 "Save relative positions of markers in region.
4048 This check for agenda markers in all agenda buffers currently active."
4049 (dolist (buf (buffer-list))
4050 (with-current-buffer buf
4051 (when (eq major-mode 'org-agenda-mode)
4052 (mapc (lambda (m) (org-check-and-save-marker m beg end))
4053 org-agenda-markers)))))
4055 ;;; Entry text mode
4057 (defun org-agenda-entry-text-show-here ()
4058 "Add some text from the entry as context to the current line."
4059 (let (m txt o)
4060 (setq m (org-get-at-bol 'org-hd-marker))
4061 (unless (marker-buffer m)
4062 (error "No marker points to an entry here"))
4063 (setq txt (concat "\n" (org-no-properties
4064 (org-agenda-get-some-entry-text
4065 m org-agenda-entry-text-maxlines
4066 org-agenda-entry-text-leaders))))
4067 (when (string-match "\\S-" txt)
4068 (setq o (make-overlay (point-at-bol) (point-at-eol)))
4069 (overlay-put o 'evaporate t)
4070 (overlay-put o 'org-overlay-type 'agenda-entry-content)
4071 (overlay-put o 'after-string txt))))
4073 (defun org-agenda-entry-text-show ()
4074 "Add entry context for all agenda lines."
4075 (interactive)
4076 (save-excursion
4077 (goto-char (point-max))
4078 (beginning-of-line 1)
4079 (while (not (bobp))
4080 (when (org-get-at-bol 'org-hd-marker)
4081 (org-agenda-entry-text-show-here))
4082 (beginning-of-line 0))))
4084 (defun org-agenda-entry-text-hide ()
4085 "Remove any shown entry context."
4086 (mapc (lambda (o)
4087 (when (eq (overlay-get o 'org-overlay-type)
4088 'agenda-entry-content)
4089 (delete-overlay o)))
4090 (overlays-in (point-min) (point-max))))
4092 (defun org-agenda-get-day-face (date)
4093 "Return the face DATE should be displayed with."
4094 (cond ((and (functionp org-agenda-day-face-function)
4095 (funcall org-agenda-day-face-function date)))
4096 ((org-agenda-today-p date) 'org-agenda-date-today)
4097 ((memq (calendar-day-of-week date) org-agenda-weekend-days)
4098 'org-agenda-date-weekend)
4099 (t 'org-agenda-date)))
4101 (defvar org-agenda-show-log-scoped)
4103 ;;; Agenda Daily/Weekly
4105 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4106 "Start day for the agenda view.
4107 Custom commands can set this variable in the options section.
4108 This is usually a string like \"2007-11-01\", \"+2d\" or any other
4109 input allowed when reading a date through the Org calendar.
4110 See the docstring of `org-read-date' for details.")
4111 (defvar org-starting-day nil) ; local variable in the agenda buffer
4112 (defvar org-arg-loc nil) ; local variable
4114 (defvar org-agenda-buffer-tmp-name nil)
4115 ;;;###autoload
4116 (defun org-agenda-list (&optional arg start-day span with-hour)
4117 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4118 The view will be for the current day or week, but from the overview buffer
4119 you will be able to go to other days/weeks.
4121 With a numeric prefix argument in an interactive call, the agenda will
4122 span ARG days. Lisp programs should instead specify SPAN to change
4123 the number of days. SPAN defaults to `org-agenda-span'.
4125 START-DAY defaults to TODAY, or to the most recent match for the weekday
4126 given in `org-agenda-start-on-weekday'.
4128 When WITH-HOUR is non-nil, only include scheduled and deadline
4129 items if they have an hour specification like [h]h:mm."
4130 (interactive "P")
4131 (when org-agenda-overriding-arguments
4132 (setq arg (car org-agenda-overriding-arguments)
4133 start-day (nth 1 org-agenda-overriding-arguments)
4134 span (nth 2 org-agenda-overriding-arguments)))
4135 (when (and (integerp arg) (> arg 0))
4136 (setq span arg arg nil))
4137 (when (numberp span)
4138 (unless (< 0 span)
4139 (user-error "Agenda creation impossible for this span(=%d days)." span)))
4140 (catch 'exit
4141 (setq org-agenda-buffer-name
4142 (or org-agenda-buffer-tmp-name
4143 (and org-agenda-doing-sticky-redo org-agenda-buffer-name)
4144 (when org-agenda-sticky
4145 (cond ((and org-keys (stringp org-match))
4146 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4147 (org-keys
4148 (format "*Org Agenda(%s)*" org-keys))
4149 (t "*Org Agenda(a)*")))
4150 "*Org Agenda*"))
4151 (org-agenda-prepare "Day/Week")
4152 (setq start-day (or start-day org-agenda-start-day))
4153 (when (stringp start-day)
4154 ;; Convert to an absolute day number
4155 (setq start-day (time-to-days (org-read-date nil t start-day))))
4156 (org-compile-prefix-format 'agenda)
4157 (org-set-sorting-strategy 'agenda)
4158 (let* ((span (org-agenda-ndays-to-span (or span org-agenda-span)))
4159 (today (org-today))
4160 (sd (or start-day today))
4161 (ndays (org-agenda-span-to-ndays span sd))
4162 (org-agenda-start-on-weekday
4163 (and (or (eq ndays 7) (eq ndays 14))
4164 org-agenda-start-on-weekday))
4165 (thefiles (org-agenda-files nil 'ifmode))
4166 (files thefiles)
4167 (start (if (or (null org-agenda-start-on-weekday)
4168 (< ndays 7))
4170 (let* ((nt (calendar-day-of-week
4171 (calendar-gregorian-from-absolute sd)))
4172 (n1 org-agenda-start-on-weekday)
4173 (d (- nt n1)))
4174 (- sd (+ (if (< d 0) 7 0) d)))))
4175 (day-numbers (list start))
4176 (day-cnt 0)
4177 (inhibit-redisplay (not debug-on-error))
4178 (org-agenda-show-log-scoped org-agenda-show-log)
4179 s e rtn rtnall file date d start-pos end-pos todayp
4180 clocktable-start clocktable-end filter)
4181 (setq org-agenda-redo-command
4182 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4183 (dotimes (n (1- ndays))
4184 (push (1+ (car day-numbers)) day-numbers))
4185 (setq day-numbers (nreverse day-numbers))
4186 (setq clocktable-start (car day-numbers)
4187 clocktable-end (1+ (or (org-last day-numbers) 0)))
4188 (setq-local org-starting-day (car day-numbers))
4189 (setq-local org-arg-loc arg)
4190 (setq-local org-agenda-current-span (org-agenda-ndays-to-span span))
4191 (unless org-agenda-compact-blocks
4192 (let* ((d1 (car day-numbers))
4193 (d2 (org-last day-numbers))
4194 (w1 (org-days-to-iso-week d1))
4195 (w2 (org-days-to-iso-week d2)))
4196 (setq s (point))
4197 (org-agenda--insert-overriding-header
4198 (concat (org-agenda-span-name span)
4199 "-agenda"
4200 (cond ((<= 350 (- d2 d1)) "")
4201 ((= w1 w2) (format " (W%02d)" w1))
4202 (t (format " (W%02d-W%02d)" w1 w2)))
4203 ":\n")))
4204 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4205 'org-date-line t))
4206 (org-agenda-mark-header-line s))
4207 (while (setq d (pop day-numbers))
4208 (setq date (calendar-gregorian-from-absolute d)
4209 s (point))
4210 (if (or (setq todayp (= d today))
4211 (and (not start-pos) (= d sd)))
4212 (setq start-pos (point))
4213 (when (and start-pos (not end-pos))
4214 (setq end-pos (point))))
4215 (setq files thefiles
4216 rtnall nil)
4217 (while (setq file (pop files))
4218 (catch 'nextfile
4219 (org-check-agenda-file file)
4220 (let ((org-agenda-entry-types org-agenda-entry-types))
4221 ;; Starred types override non-starred equivalents
4222 (when (member :deadline* org-agenda-entry-types)
4223 (setq org-agenda-entry-types
4224 (delq :deadline org-agenda-entry-types)))
4225 (when (member :scheduled* org-agenda-entry-types)
4226 (setq org-agenda-entry-types
4227 (delq :scheduled org-agenda-entry-types)))
4228 ;; Honor with-hour
4229 (when with-hour
4230 (when (member :deadline org-agenda-entry-types)
4231 (setq org-agenda-entry-types
4232 (delq :deadline org-agenda-entry-types))
4233 (push :deadline* org-agenda-entry-types))
4234 (when (member :scheduled org-agenda-entry-types)
4235 (setq org-agenda-entry-types
4236 (delq :scheduled org-agenda-entry-types))
4237 (push :scheduled* org-agenda-entry-types)))
4238 (unless org-agenda-include-deadlines
4239 (setq org-agenda-entry-types
4240 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4241 (cond
4242 ((memq org-agenda-show-log-scoped '(only clockcheck))
4243 (setq rtn (org-agenda-get-day-entries
4244 file date :closed)))
4245 (org-agenda-show-log-scoped
4246 (setq rtn (apply 'org-agenda-get-day-entries
4247 file date
4248 (append '(:closed) org-agenda-entry-types))))
4250 (setq rtn (apply 'org-agenda-get-day-entries
4251 file date
4252 org-agenda-entry-types)))))
4253 (setq rtnall (append rtnall rtn)))) ;; all entries
4254 (when org-agenda-include-diary
4255 (let ((org-agenda-search-headline-for-time t))
4256 (require 'diary-lib)
4257 (setq rtn (org-get-entries-from-diary date))
4258 (setq rtnall (append rtnall rtn))))
4259 (when (or rtnall org-agenda-show-all-dates)
4260 (setq day-cnt (1+ day-cnt))
4261 (insert
4262 (if (stringp org-agenda-format-date)
4263 (format-time-string org-agenda-format-date
4264 (org-time-from-absolute date))
4265 (funcall org-agenda-format-date date))
4266 "\n")
4267 (put-text-property s (1- (point)) 'face
4268 (org-agenda-get-day-face date))
4269 (put-text-property s (1- (point)) 'org-date-line t)
4270 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4271 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4272 (when todayp
4273 (put-text-property s (1- (point)) 'org-today t))
4274 (setq rtnall
4275 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4276 (when rtnall (insert ;; all entries
4277 (org-agenda-finalize-entries rtnall 'agenda)
4278 "\n"))
4279 (put-text-property s (1- (point)) 'day d)
4280 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)))
4281 (when (and org-agenda-clockreport-mode clocktable-start)
4282 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4283 ;; the above line is to ensure the restricted range!
4284 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4285 tbl)
4286 (setq p (org-plist-delete p :block))
4287 (setq p (plist-put p :tstart clocktable-start))
4288 (setq p (plist-put p :tend clocktable-end))
4289 (setq p (plist-put p :scope 'agenda))
4290 (setq tbl (apply 'org-clock-get-clocktable p))
4291 (insert tbl)))
4292 (goto-char (point-min))
4293 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4294 (unless (or (not (get-buffer-window))
4295 (and (pos-visible-in-window-p (point-min))
4296 (pos-visible-in-window-p (point-max))))
4297 (goto-char (1- (point-max)))
4298 (recenter -1)
4299 (when (not (pos-visible-in-window-p (or start-pos 1)))
4300 (goto-char (or start-pos 1))
4301 (recenter 1)))
4302 (goto-char (or start-pos 1))
4303 (add-text-properties (point-min) (point-max)
4304 `(org-agenda-type agenda
4305 org-last-args (,arg ,start-day ,span)
4306 org-redo-cmd ,org-agenda-redo-command
4307 org-series-cmd ,org-cmd))
4308 (when (eq org-agenda-show-log-scoped 'clockcheck)
4309 (org-agenda-show-clocking-issues))
4310 (org-agenda-finalize)
4311 (setq buffer-read-only t)
4312 (message ""))))
4314 (defun org-agenda-ndays-to-span (n)
4315 "Return a span symbol for a span of N days, or N if none matches."
4316 (cond ((symbolp n) n)
4317 ((= n 1) 'day)
4318 ((= n 7) 'week)
4319 ((= n 14) 'fortnight)
4320 (t n)))
4322 (defun org-agenda-span-to-ndays (span &optional start-day)
4323 "Return ndays from SPAN, possibly starting at START-DAY.
4324 START-DAY is an absolute time value."
4325 (cond ((numberp span) span)
4326 ((eq span 'day) 1)
4327 ((eq span 'week) 7)
4328 ((eq span 'fortnight) 14)
4329 ((eq span 'month)
4330 (let ((date (calendar-gregorian-from-absolute start-day)))
4331 (calendar-last-day-of-month (car date) (cl-caddr date))))
4332 ((eq span 'year)
4333 (let ((date (calendar-gregorian-from-absolute start-day)))
4334 (if (calendar-leap-year-p (cl-caddr date)) 366 365)))))
4336 (defun org-agenda-span-name (span)
4337 "Return a SPAN name."
4338 (if (null span)
4340 (if (symbolp span)
4341 (capitalize (symbol-name span))
4342 (format "%d days" span))))
4344 ;;; Agenda word search
4346 (defvar org-agenda-search-history nil)
4348 (defvar org-search-syntax-table nil
4349 "Special syntax table for Org search.
4350 In this table, we have single quotes not as word constituents, to
4351 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4353 (defvar org-mode-syntax-table) ; From org.el
4354 (defun org-search-syntax-table ()
4355 (unless org-search-syntax-table
4356 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4357 (modify-syntax-entry ?' "." org-search-syntax-table)
4358 (modify-syntax-entry ?` "." org-search-syntax-table))
4359 org-search-syntax-table)
4361 (defvar org-agenda-last-search-view-search-was-boolean nil)
4363 ;;;###autoload
4364 (defun org-search-view (&optional todo-only string edit-at)
4365 "Show all entries that contain a phrase or words or regular expressions.
4367 With optional prefix argument TODO-ONLY, only consider entries that are
4368 TODO entries. The argument STRING can be used to pass a default search
4369 string into this function. If EDIT-AT is non-nil, it means that the
4370 user should get a chance to edit this string, with cursor at position
4371 EDIT-AT.
4373 The search string can be viewed either as a phrase that should be found as
4374 is, or it can be broken into a number of snippets, each of which must match
4375 in a Boolean way to select an entry. The default depends on the variable
4376 `org-agenda-search-view-always-boolean'.
4377 Even if this is turned off (the default) you can always switch to
4378 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4380 The default is a direct search of the whole phrase, where each space in
4381 the search string can expand to an arbitrary amount of whitespace,
4382 including newlines.
4384 If using a Boolean search, the search string is split on whitespace and
4385 each snippet is searched separately, with logical AND to select an entry.
4386 Words prefixed with a minus must *not* occur in the entry. Words without
4387 a prefix or prefixed with a plus must occur in the entry. Matching is
4388 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4389 match whole words, not parts of a word) if
4390 `org-agenda-search-view-force-full-words' is set (default is nil).
4392 Boolean search snippets enclosed by curly braces are interpreted as
4393 regular expressions that must or (when preceded with \"-\") must not
4394 match in the entry. Snippets enclosed into double quotes will be taken
4395 as a whole, to include whitespace.
4397 - If the search string starts with an asterisk, search only in headlines.
4398 - If (possibly after the leading star) the search string starts with an
4399 exclamation mark, this also means to look at TODO entries only, an effect
4400 that can also be achieved with a prefix argument.
4401 - If (possibly after star and exclamation mark) the search string starts
4402 with a colon, this will mean that the (non-regexp) snippets of the
4403 Boolean search must match as full words.
4405 This command searches the agenda files, and in addition the files
4406 listed in `org-agenda-text-search-extra-files' unless a restriction lock
4407 is active."
4408 (interactive "P")
4409 (when org-agenda-overriding-arguments
4410 (setq todo-only (car org-agenda-overriding-arguments)
4411 string (nth 1 org-agenda-overriding-arguments)
4412 edit-at (nth 2 org-agenda-overriding-arguments)))
4413 (let* ((props (list 'face nil
4414 'done-face 'org-agenda-done
4415 'org-not-done-regexp org-not-done-regexp
4416 'org-todo-regexp org-todo-regexp
4417 'org-complex-heading-regexp org-complex-heading-regexp
4418 'mouse-face 'highlight
4419 'help-echo (format "mouse-2 or RET jump to location")))
4420 (full-words org-agenda-search-view-force-full-words)
4421 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4422 regexp rtn rtnall files file pos inherited-tags
4423 marker category level tags c neg re boolean
4424 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4425 (unless (and (not edit-at)
4426 (stringp string)
4427 (string-match "\\S-" string))
4428 (setq string (read-string
4429 (if org-agenda-search-view-always-boolean
4430 "[+-]Word/{Regexp} ...: "
4431 "Phrase or [+-]Word/{Regexp} ...: ")
4432 (cond
4433 ((integerp edit-at) (cons string edit-at))
4434 (edit-at string))
4435 'org-agenda-search-history)))
4436 (catch 'exit
4437 (when org-agenda-sticky
4438 (setq org-agenda-buffer-name
4439 (if (stringp string)
4440 (format "*Org Agenda(%s:%s)*"
4441 (or org-keys (or (and todo-only "S") "s")) string)
4442 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4443 (org-agenda-prepare "SEARCH")
4444 (org-compile-prefix-format 'search)
4445 (org-set-sorting-strategy 'search)
4446 (setq org-agenda-redo-command
4447 (list 'org-search-view (if todo-only t nil)
4448 (list 'if 'current-prefix-arg nil string)))
4449 (setq org-agenda-query-string string)
4450 (if (equal (string-to-char string) ?*)
4451 (setq hdl-only t
4452 words (substring string 1))
4453 (setq words string))
4454 (when (equal (string-to-char words) ?!)
4455 (setq todo-only t
4456 words (substring words 1)))
4457 (when (equal (string-to-char words) ?:)
4458 (setq full-words t
4459 words (substring words 1)))
4460 (when (or org-agenda-search-view-always-boolean
4461 (member (string-to-char words) '(?- ?+ ?\{)))
4462 (setq boolean t))
4463 (setq words (split-string words))
4464 (let (www w)
4465 (while (setq w (pop words))
4466 (while (and (string-match "\\\\\\'" w) words)
4467 (setq w (concat (substring w 0 -1) " " (pop words))))
4468 (push w www))
4469 (setq words (nreverse www) www nil)
4470 (while (setq w (pop words))
4471 (when (and (string-match "\\`[-+]?{" w)
4472 (not (string-match "}\\'" w)))
4473 (while (and words (not (string-match "}\\'" (car words))))
4474 (setq w (concat w " " (pop words))))
4475 (setq w (concat w " " (pop words))))
4476 (push w www))
4477 (setq words (nreverse www)))
4478 (setq org-agenda-last-search-view-search-was-boolean boolean)
4479 (when boolean
4480 (let (wds w)
4481 (while (setq w (pop words))
4482 (when (or (equal (substring w 0 1) "\"")
4483 (and (> (length w) 1)
4484 (member (substring w 0 1) '("+" "-"))
4485 (equal (substring w 1 2) "\"")))
4486 (while (and words (not (equal (substring w -1) "\"")))
4487 (setq w (concat w " " (pop words)))))
4488 (and (string-match "\\`\\([-+]?\\)\"" w)
4489 (setq w (replace-match "\\1" nil nil w)))
4490 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4491 (push w wds))
4492 (setq words (nreverse wds))))
4493 (if boolean
4494 (mapc (lambda (w)
4495 (setq c (string-to-char w))
4496 (if (equal c ?-)
4497 (setq neg t w (substring w 1))
4498 (if (equal c ?+)
4499 (setq neg nil w (substring w 1))
4500 (setq neg nil)))
4501 (if (string-match "\\`{.*}\\'" w)
4502 (setq re (substring w 1 -1))
4503 (if full-words
4504 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4505 (setq re (regexp-quote (downcase w)))))
4506 (if neg (push re regexps-) (push re regexps+)))
4507 words)
4508 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4509 regexps+))
4510 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4511 (if (not regexps+)
4512 (setq regexp org-outline-regexp-bol)
4513 (setq regexp (pop regexps+))
4514 (when hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4515 regexp))))
4516 (setq files (org-agenda-files nil 'ifmode))
4517 ;; Add `org-agenda-text-search-extra-files' unless there is some
4518 ;; restriction.
4519 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4520 (pop org-agenda-text-search-extra-files)
4521 (unless (get 'org-agenda-files 'org-restrict)
4522 (setq files (org-add-archive-files files))))
4523 ;; Uniquify files. However, let `org-check-agenda-file' handle
4524 ;; non-existent ones.
4525 (setq files (cl-remove-duplicates
4526 (append files org-agenda-text-search-extra-files)
4527 :test (lambda (a b)
4528 (and (file-exists-p a)
4529 (file-exists-p b)
4530 (file-equal-p a b))))
4531 rtnall nil)
4532 (while (setq file (pop files))
4533 (setq ee nil)
4534 (catch 'nextfile
4535 (org-check-agenda-file file)
4536 (setq buffer (if (file-exists-p file)
4537 (org-get-agenda-file-buffer file)
4538 (error "No such file %s" file)))
4539 (unless buffer
4540 ;; If file does not exist, make sure an error message is sent
4541 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4542 file))))
4543 (with-current-buffer buffer
4544 (with-syntax-table (org-search-syntax-table)
4545 (unless (derived-mode-p 'org-mode)
4546 (error "Agenda file %s is not in Org mode" file))
4547 (let ((case-fold-search t))
4548 (save-excursion
4549 (save-restriction
4550 (if (eq buffer org-agenda-restrict)
4551 (narrow-to-region org-agenda-restrict-begin
4552 org-agenda-restrict-end)
4553 (widen))
4554 (goto-char (point-min))
4555 (unless (or (org-at-heading-p)
4556 (outline-next-heading))
4557 (throw 'nextfile t))
4558 (goto-char (max (point-min) (1- (point))))
4559 (while (re-search-forward regexp nil t)
4560 (org-back-to-heading t)
4561 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4562 (> (org-reduced-level (org-outline-level))
4563 org-agenda-search-view-max-outline-level)
4564 (forward-line -1)
4565 (org-back-to-heading t)))
4566 (skip-chars-forward "* ")
4567 (setq beg (point-at-bol)
4568 beg1 (point)
4569 end (progn
4570 (outline-next-heading)
4571 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4572 (> (org-reduced-level (org-outline-level))
4573 org-agenda-search-view-max-outline-level)
4574 (forward-line 1)
4575 (outline-next-heading)))
4576 (point)))
4578 (catch :skip
4579 (goto-char beg)
4580 (org-agenda-skip)
4581 (setq str (buffer-substring-no-properties
4582 (point-at-bol)
4583 (if hdl-only (point-at-eol) end)))
4584 (mapc (lambda (wr) (when (string-match wr str)
4585 (goto-char (1- end))
4586 (throw :skip t)))
4587 regexps-)
4588 (mapc (lambda (wr) (unless (string-match wr str)
4589 (goto-char (1- end))
4590 (throw :skip t)))
4591 (if todo-only
4592 (cons (concat "^\\*+[ \t]+"
4593 org-not-done-regexp)
4594 regexps+)
4595 regexps+))
4596 (goto-char beg)
4597 (setq marker (org-agenda-new-marker (point))
4598 category (org-get-category)
4599 level (make-string (org-reduced-level (org-outline-level)) ? )
4600 inherited-tags
4601 (or (eq org-agenda-show-inherited-tags 'always)
4602 (and (listp org-agenda-show-inherited-tags)
4603 (memq 'todo org-agenda-show-inherited-tags))
4604 (and (eq org-agenda-show-inherited-tags t)
4605 (or (eq org-agenda-use-tag-inheritance t)
4606 (memq 'todo org-agenda-use-tag-inheritance))))
4607 tags (org-get-tags nil (not inherited-tags))
4608 txt (org-agenda-format-item
4610 (buffer-substring-no-properties
4611 beg1 (point-at-eol))
4612 level category tags t))
4613 (org-add-props txt props
4614 'org-marker marker 'org-hd-marker marker
4615 'org-todo-regexp org-todo-regexp
4616 'level level
4617 'org-complex-heading-regexp org-complex-heading-regexp
4618 'priority 1000
4619 'type "search")
4620 (push txt ee)
4621 (goto-char (1- end))))))))))
4622 (setq rtn (nreverse ee))
4623 (setq rtnall (append rtnall rtn)))
4624 (org-agenda--insert-overriding-header
4625 (with-temp-buffer
4626 (insert "Search words: ")
4627 (add-text-properties (point-min) (1- (point))
4628 (list 'face 'org-agenda-structure))
4629 (setq pos (point))
4630 (insert string "\n")
4631 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4632 (setq pos (point))
4633 (unless org-agenda-multi
4634 (insert (substitute-command-keys "\\<org-agenda-mode-map>\
4635 Press `\\[org-agenda-manipulate-query-add]', \
4636 `\\[org-agenda-manipulate-query-subtract]' to add/sub word, \
4637 `\\[org-agenda-manipulate-query-add-re]', \
4638 `\\[org-agenda-manipulate-query-subtract-re]' to add/sub regexp, \
4639 `\\[universal-argument] \\[org-agenda-redo]' to edit\n"))
4640 (add-text-properties pos (1- (point))
4641 (list 'face 'org-agenda-structure)))
4642 (buffer-string)))
4643 (org-agenda-mark-header-line (point-min))
4644 (when rtnall
4645 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4646 (goto-char (point-min))
4647 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4648 (add-text-properties (point-min) (point-max)
4649 `(org-agenda-type search
4650 org-last-args (,todo-only ,string ,edit-at)
4651 org-redo-cmd ,org-agenda-redo-command
4652 org-series-cmd ,org-cmd))
4653 (org-agenda-finalize)
4654 (setq buffer-read-only t))))
4656 ;;; Agenda TODO list
4658 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4659 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4660 (concat
4661 (if (or (equal keywords "ALL") (not keywords))
4662 (propertize "ALL" 'face 'warning)
4663 (mapconcat
4664 (lambda (kw)
4665 (propertize kw 'face (org-get-todo-face kw)))
4666 (org-split-string keywords "|")
4667 "|"))
4668 "\n"))
4670 (defvar org-select-this-todo-keyword nil)
4671 (defvar org-last-arg nil)
4673 ;;;###autoload
4674 (defun org-todo-list (&optional arg)
4675 "Show all (not done) TODO entries from all agenda file in a single list.
4676 The prefix arg can be used to select a specific TODO keyword and limit
4677 the list to these. When using `\\[universal-argument]', you will be prompted
4678 for a keyword. A numeric prefix directly selects the Nth keyword in
4679 `org-todo-keywords-1'."
4680 (interactive "P")
4681 (when org-agenda-overriding-arguments
4682 (setq arg org-agenda-overriding-arguments))
4683 (when (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4684 (let* ((today (org-today))
4685 (date (calendar-gregorian-from-absolute today))
4686 (completion-ignore-case t)
4687 kwds org-select-this-todo-keyword rtn rtnall files file pos)
4688 (catch 'exit
4689 (when org-agenda-sticky
4690 (setq org-agenda-buffer-name
4691 (if (stringp org-select-this-todo-keyword)
4692 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4693 org-select-this-todo-keyword)
4694 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4695 (org-agenda-prepare "TODO")
4696 (setq kwds org-todo-keywords-for-agenda
4697 org-select-this-todo-keyword (if (stringp arg) arg
4698 (and (integerp arg)
4699 (> arg 0)
4700 (nth (1- arg) kwds))))
4701 (when (equal arg '(4))
4702 (setq org-select-this-todo-keyword
4703 (completing-read "Keyword (or KWD1|K2D2|...): "
4704 (mapcar #'list kwds) nil nil)))
4705 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4706 (org-compile-prefix-format 'todo)
4707 (org-set-sorting-strategy 'todo)
4708 (setq org-agenda-redo-command
4709 `(org-todo-list (or (and (numberp current-prefix-arg)
4710 current-prefix-arg)
4711 ,org-select-this-todo-keyword
4712 current-prefix-arg ,arg)))
4713 (setq files (org-agenda-files nil 'ifmode)
4714 rtnall nil)
4715 (while (setq file (pop files))
4716 (catch 'nextfile
4717 (org-check-agenda-file file)
4718 (setq rtn (org-agenda-get-day-entries file date :todo))
4719 (setq rtnall (append rtnall rtn))))
4720 (org-agenda--insert-overriding-header
4721 (with-temp-buffer
4722 (insert "Global list of TODO items of type: ")
4723 (add-text-properties (point-min) (1- (point))
4724 (list 'face 'org-agenda-structure
4725 'short-heading
4726 (concat "ToDo: "
4727 (or org-select-this-todo-keyword "ALL"))))
4728 (org-agenda-mark-header-line (point-min))
4729 (insert (org-agenda-propertize-selected-todo-keywords
4730 org-select-this-todo-keyword))
4731 (setq pos (point))
4732 (unless org-agenda-multi
4733 (insert (substitute-command-keys "Press \
4734 \\<org-agenda-mode-map>`N \\[org-agenda-redo]' (e.g. `0 \\[org-agenda-redo]') \
4735 to search again: (0)[ALL]"))
4736 (let ((n 0))
4737 (dolist (k kwds)
4738 (let ((s (format "(%d)%s" (cl-incf n) k)))
4739 (when (> (+ (current-column) (string-width s) 1) (window-width))
4740 (insert "\n "))
4741 (insert " " s))))
4742 (insert "\n"))
4743 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure))
4744 (buffer-string)))
4745 (org-agenda-mark-header-line (point-min))
4746 (when rtnall
4747 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4748 (goto-char (point-min))
4749 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4750 (add-text-properties (point-min) (point-max)
4751 `(org-agenda-type todo
4752 org-last-args ,arg
4753 org-redo-cmd ,org-agenda-redo-command
4754 org-series-cmd ,org-cmd))
4755 (org-agenda-finalize)
4756 (setq buffer-read-only t))))
4758 ;;; Agenda tags match
4760 ;;;###autoload
4761 (defun org-tags-view (&optional todo-only match)
4762 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4763 The prefix arg TODO-ONLY limits the search to TODO entries."
4764 (interactive "P")
4765 (when org-agenda-overriding-arguments
4766 (setq todo-only (car org-agenda-overriding-arguments)
4767 match (nth 1 org-agenda-overriding-arguments)))
4768 (let* ((org-tags-match-list-sublevels
4769 org-tags-match-list-sublevels)
4770 (completion-ignore-case t)
4771 (org--matcher-tags-todo-only todo-only)
4772 rtn rtnall files file pos matcher
4773 buffer)
4774 (when (and (stringp match) (not (string-match "\\S-" match)))
4775 (setq match nil))
4776 (catch 'exit
4777 ;; TODO: this code is repeated a lot...
4778 (when org-agenda-sticky
4779 (setq org-agenda-buffer-name
4780 (if (stringp match)
4781 (format "*Org Agenda(%s:%s)*"
4782 (or org-keys (or (and todo-only "M") "m")) match)
4783 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4784 (setq matcher (org-make-tags-matcher match))
4785 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4786 ;; expanding tags within `org-make-tags-matcher'
4787 (org-agenda-prepare (concat "TAGS " match))
4788 (setq match (car matcher)
4789 matcher (cdr matcher))
4790 (org-compile-prefix-format 'tags)
4791 (org-set-sorting-strategy 'tags)
4792 (setq org-agenda-query-string match)
4793 (setq org-agenda-redo-command
4794 (list 'org-tags-view
4795 `(quote ,org--matcher-tags-todo-only)
4796 `(if current-prefix-arg nil ,org-agenda-query-string)))
4797 (setq files (org-agenda-files nil 'ifmode)
4798 rtnall nil)
4799 (while (setq file (pop files))
4800 (catch 'nextfile
4801 (org-check-agenda-file file)
4802 (setq buffer (if (file-exists-p file)
4803 (org-get-agenda-file-buffer file)
4804 (error "No such file %s" file)))
4805 (if (not buffer)
4806 ;; If file does not exist, error message to agenda
4807 (setq rtn (list
4808 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4809 rtnall (append rtnall rtn))
4810 (with-current-buffer buffer
4811 (unless (derived-mode-p 'org-mode)
4812 (error "Agenda file %s is not in Org mode" file))
4813 (save-excursion
4814 (save-restriction
4815 (if (eq buffer org-agenda-restrict)
4816 (narrow-to-region org-agenda-restrict-begin
4817 org-agenda-restrict-end)
4818 (widen))
4819 (setq rtn (org-scan-tags 'agenda
4820 matcher
4821 org--matcher-tags-todo-only))
4822 (setq rtnall (append rtnall rtn))))))))
4823 (org-agenda--insert-overriding-header
4824 (with-temp-buffer
4825 (insert "Headlines with TAGS match: ")
4826 (add-text-properties (point-min) (1- (point))
4827 (list 'face 'org-agenda-structure
4828 'short-heading
4829 (concat "Match: " match)))
4830 (setq pos (point))
4831 (insert match "\n")
4832 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4833 (setq pos (point))
4834 (unless org-agenda-multi
4835 (insert (substitute-command-keys
4836 "Press \
4837 \\<org-agenda-mode-map>`\\[universal-argument] \\[org-agenda-redo]' \
4838 to search again\n")))
4839 (add-text-properties pos (1- (point))
4840 (list 'face 'org-agenda-structure))
4841 (buffer-string)))
4842 (org-agenda-mark-header-line (point-min))
4843 (when rtnall
4844 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4845 (goto-char (point-min))
4846 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4847 (add-text-properties
4848 (point-min) (point-max)
4849 `(org-agenda-type tags
4850 org-last-args (,org--matcher-tags-todo-only ,match)
4851 org-redo-cmd ,org-agenda-redo-command
4852 org-series-cmd ,org-cmd))
4853 (org-agenda-finalize)
4854 (setq buffer-read-only t))))
4856 ;;; Agenda Finding stuck projects
4858 (defvar org-agenda-skip-regexp nil
4859 "Regular expression used in skipping subtrees for the agenda.
4860 This is basically a temporary global variable that can be set and then
4861 used by user-defined selections using `org-agenda-skip-function'.")
4863 (defvar org-agenda-overriding-header nil
4864 "When set during agenda, todo and tags searches it replaces the header.
4865 If an empty string, no header will be inserted. If any other
4866 string, it will be inserted as a header. If nil, a header will
4867 be generated automatically according to the command. This
4868 variable should not be set directly, but custom commands can bind
4869 it in the options section.")
4871 (defun org-agenda-skip-entry-if (&rest conditions)
4872 "Skip entry if any of CONDITIONS is true.
4873 See `org-agenda-skip-if' for details."
4874 (org-agenda-skip-if nil conditions))
4876 (defun org-agenda-skip-subtree-if (&rest conditions)
4877 "Skip subtree if any of CONDITIONS is true.
4878 See `org-agenda-skip-if' for details."
4879 (org-agenda-skip-if t conditions))
4881 (defun org-agenda-skip-if (subtree conditions)
4882 "Checks current entity for CONDITIONS.
4883 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4884 the entry (i.e. the text before the next heading) is checked.
4886 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4887 from different tests. Valid conditions are:
4889 scheduled Check if there is a scheduled cookie
4890 notscheduled Check if there is no scheduled cookie
4891 deadline Check if there is a deadline
4892 notdeadline Check if there is no deadline
4893 timestamp Check if there is a timestamp (also deadline or scheduled)
4894 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4895 regexp Check if regexp matches
4896 notregexp Check if regexp does not match.
4897 todo Check if TODO keyword matches
4898 nottodo Check if TODO keyword does not match
4900 The regexp is taken from the conditions list, it must come right after
4901 the `regexp' or `notregexp' element.
4903 `todo' and `nottodo' accept as an argument a list of todo
4904 keywords, which may include \"*\" to match any todo keyword.
4906 (org-agenda-skip-entry-if \\='todo \\='(\"TODO\" \"WAITING\"))
4908 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4910 Instead of a list, a keyword class may be given. For example:
4912 (org-agenda-skip-entry-if \\='nottodo \\='done)
4914 would skip entries that haven't been marked with any of \"DONE\"
4915 keywords. Possible classes are: `todo', `done', `any'.
4917 If any of these conditions is met, this function returns the end point of
4918 the entity, causing the search to continue from there. This is a function
4919 that can be put into `org-agenda-skip-function' for the duration of a command."
4920 (org-back-to-heading t)
4921 (let* ((beg (point))
4922 (end (if subtree (save-excursion (org-end-of-subtree t) (point))
4923 (org-entry-end-position)))
4924 (planning-end (if subtree end (line-end-position 2)))
4926 (and
4927 (or (and (memq 'scheduled conditions)
4928 (re-search-forward org-scheduled-time-regexp planning-end t))
4929 (and (memq 'notscheduled conditions)
4930 (not
4931 (save-excursion
4932 (re-search-forward org-scheduled-time-regexp planning-end t))))
4933 (and (memq 'deadline conditions)
4934 (re-search-forward org-deadline-time-regexp planning-end t))
4935 (and (memq 'notdeadline conditions)
4936 (not
4937 (save-excursion
4938 (re-search-forward org-deadline-time-regexp planning-end t))))
4939 (and (memq 'timestamp conditions)
4940 (re-search-forward org-ts-regexp end t))
4941 (and (memq 'nottimestamp conditions)
4942 (not (save-excursion (re-search-forward org-ts-regexp end t))))
4943 (and (setq m (memq 'regexp conditions))
4944 (stringp (nth 1 m))
4945 (re-search-forward (nth 1 m) end t))
4946 (and (setq m (memq 'notregexp conditions))
4947 (stringp (nth 1 m))
4948 (not (save-excursion (re-search-forward (nth 1 m) end t))))
4949 (and (or
4950 (setq m (memq 'nottodo conditions))
4951 (setq m (memq 'todo-unblocked conditions))
4952 (setq m (memq 'nottodo-unblocked conditions))
4953 (setq m (memq 'todo conditions)))
4954 (org-agenda-skip-if-todo m end)))
4955 end)))
4957 (defun org-agenda-skip-if-todo (args end)
4958 "Helper function for `org-agenda-skip-if', do not use it directly.
4959 ARGS is a list with first element either `todo', `nottodo',
4960 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
4961 a list of TODO keywords, or a state symbol `todo' or `done' or
4962 `any'."
4963 (let ((todo-re
4964 (concat "^\\*+[ \t]+"
4965 (regexp-opt
4966 (pcase args
4967 (`(,_ todo)
4968 (org-delete-all org-done-keywords
4969 (copy-sequence org-todo-keywords-1)))
4970 (`(,_ done) org-done-keywords)
4971 (`(,_ any) org-todo-keywords-1)
4972 (`(,_ ,(pred atom))
4973 (error "Invalid TODO class or type: %S" args))
4974 (`(,_ ,(pred (member "*"))) org-todo-keywords-1)
4975 (`(,_ ,todo-list) todo-list))
4976 'words))))
4977 (pcase args
4978 (`(todo . ,_)
4979 (let (case-fold-search) (re-search-forward todo-re end t)))
4980 (`(nottodo . ,_)
4981 (not (let (case-fold-search) (re-search-forward todo-re end t))))
4982 (`(todo-unblocked . ,_)
4983 (catch :unblocked
4984 (while (let (case-fold-search) (re-search-forward todo-re end t))
4985 (when (org-entry-blocked-p) (throw :unblocked t)))
4986 nil))
4987 (`(nottodo-unblocked . ,_)
4988 (catch :unblocked
4989 (while (let (case-fold-search) (re-search-forward todo-re end t))
4990 (when (org-entry-blocked-p) (throw :unblocked nil)))
4992 (`(,type . ,_) (error "Unknown TODO skip type: %S" type)))))
4994 ;;;###autoload
4995 (defun org-agenda-list-stuck-projects (&rest ignore)
4996 "Create agenda view for projects that are stuck.
4997 Stuck projects are project that have no next actions. For the definitions
4998 of what a project is and how to check if it stuck, customize the variable
4999 `org-stuck-projects'."
5000 (interactive)
5001 (let* ((org-agenda-overriding-header
5002 (or org-agenda-overriding-header "List of stuck projects: "))
5003 (matcher (nth 0 org-stuck-projects))
5004 (todo (nth 1 org-stuck-projects))
5005 (tags (nth 2 org-stuck-projects))
5006 (gen-re (org-string-nw-p (nth 3 org-stuck-projects)))
5007 (todo-wds
5008 (if (not (member "*" todo)) todo
5009 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
5010 (org-delete-all org-done-keywords-for-agenda
5011 (copy-sequence org-todo-keywords-for-agenda))))
5012 (todo-re (and todo
5013 (format "^\\*+[ \t]+\\(%s\\)\\>"
5014 (mapconcat #'identity todo-wds "\\|"))))
5015 (tags-re (cond ((null tags) nil)
5016 ((member "*" tags) org-tag-line-re)
5017 (tags
5018 (let ((other-tags (format "\\(?:%s:\\)*" org-tag-re)))
5019 (concat org-outline-regexp-bol
5020 ".*?[ \t]:"
5021 other-tags
5022 (regexp-opt tags t)
5023 ":" other-tags "[ \t]*$")))
5024 (t nil)))
5025 (re-list (delq nil (list todo-re tags-re gen-re)))
5026 (skip-re
5027 (if (null re-list)
5028 (error "Missing information to identify unstuck projects")
5029 (mapconcat #'identity re-list "\\|")))
5030 (org-agenda-skip-function
5031 ;; Skip entry if `org-agenda-skip-regexp' matches anywhere
5032 ;; in the subtree.
5033 `(lambda ()
5034 (and (save-excursion
5035 (let ((case-fold-search nil))
5036 (re-search-forward
5037 ,skip-re (save-excursion (org-end-of-subtree t)) t)))
5038 (progn (outline-next-heading) (point))))))
5039 (org-tags-view nil matcher)
5040 (setq org-agenda-buffer-name (buffer-name))
5041 (with-current-buffer org-agenda-buffer-name
5042 (setq org-agenda-redo-command
5043 `(org-agenda-list-stuck-projects ,current-prefix-arg))
5044 (let ((inhibit-read-only t))
5045 (add-text-properties
5046 (point-min) (point-max)
5047 `(org-redo-cmd ,org-agenda-redo-command))))))
5049 ;;; Diary integration
5051 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
5052 (defvar diary-list-entries-hook)
5053 (defvar diary-time-regexp)
5054 (defun org-get-entries-from-diary (date)
5055 "Get the (Emacs Calendar) diary entries for DATE."
5056 (require 'diary-lib)
5057 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
5058 (diary-display-function 'diary-fancy-display)
5059 (pop-up-frames nil)
5060 (diary-list-entries-hook
5061 (cons 'org-diary-default-entry diary-list-entries-hook))
5062 (diary-file-name-prefix nil) ; turn this feature off
5063 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
5064 entries
5065 (org-disable-agenda-to-diary t))
5066 (save-excursion
5067 (save-window-excursion
5068 (funcall (if (fboundp 'diary-list-entries)
5069 'diary-list-entries 'list-diary-entries)
5070 date 1)))
5071 (if (not (get-buffer diary-fancy-buffer))
5072 (setq entries nil)
5073 (with-current-buffer diary-fancy-buffer
5074 (setq buffer-read-only nil)
5075 (if (zerop (buffer-size))
5076 ;; No entries
5077 (setq entries nil)
5078 ;; Omit the date and other unnecessary stuff
5079 (org-agenda-cleanup-fancy-diary)
5080 ;; Add prefix to each line and extend the text properties
5081 (if (zerop (buffer-size))
5082 (setq entries nil)
5083 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
5084 (setq entries
5085 (with-temp-buffer
5086 (insert entries) (goto-char (point-min))
5087 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5088 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5089 (replace-match (concat "; " (match-string 1)))))
5090 (buffer-string)))))
5091 (set-buffer-modified-p nil)
5092 (kill-buffer diary-fancy-buffer)))
5093 (when entries
5094 (setq entries (org-split-string entries "\n"))
5095 (setq entries
5096 (mapcar
5097 (lambda (x)
5098 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5099 ;; Extend the text properties to the beginning of the line
5100 (org-add-props x (text-properties-at (1- (length x)) x)
5101 'type "diary" 'date date 'face 'org-agenda-diary))
5102 entries)))))
5104 (defvar org-agenda-cleanup-fancy-diary-hook nil
5105 "Hook run when the fancy diary buffer is cleaned up.")
5107 (defun org-agenda-cleanup-fancy-diary ()
5108 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5109 This gets rid of the date, the underline under the date, and the
5110 dummy entry installed by Org mode to ensure non-empty diary for
5111 each date. It also removes lines that contain only whitespace."
5112 (goto-char (point-min))
5113 (if (looking-at ".*?:[ \t]*")
5114 (progn
5115 (replace-match "")
5116 (re-search-forward "\n=+$" nil t)
5117 (replace-match "")
5118 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5119 (re-search-forward "\n=+$" nil t)
5120 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5121 (goto-char (point-min))
5122 (while (re-search-forward "^ +\n" nil t)
5123 (replace-match ""))
5124 (goto-char (point-min))
5125 (when (re-search-forward "^Org mode dummy\n?" nil t)
5126 (replace-match ""))
5127 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5129 (defun org-modify-diary-entry-string (string)
5130 "Add text properties to string, allowing Org to act on it."
5131 (org-add-props string nil
5132 'mouse-face 'highlight
5133 'help-echo (if buffer-file-name
5134 (format "mouse-2 or RET jump to diary file %s"
5135 (abbreviate-file-name buffer-file-name))
5137 'org-agenda-diary-link t
5138 'org-marker (org-agenda-new-marker (point-at-bol))))
5140 (defun org-diary-default-entry ()
5141 "Add a dummy entry to the diary.
5142 Needed to avoid empty dates which mess up holiday display."
5143 ;; Catch the error if dealing with the new add-to-diary-alist
5144 (when org-disable-agenda-to-diary
5145 (condition-case nil
5146 (org-add-to-diary-list original-date "Org mode dummy" "")
5147 (error
5148 (org-add-to-diary-list original-date "Org mode dummy" "" nil)))))
5150 (defun org-add-to-diary-list (&rest args)
5151 (if (fboundp 'diary-add-to-list)
5152 (apply 'diary-add-to-list args)
5153 (apply 'add-to-diary-list args)))
5155 (defvar org-diary-last-run-time nil)
5157 ;;;###autoload
5158 (defun org-diary (&rest args)
5159 "Return diary information from org files.
5160 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5161 It accesses org files and extracts information from those files to be
5162 listed in the diary. The function accepts arguments specifying what
5163 items should be listed. For a list of arguments allowed here, see the
5164 variable `org-agenda-entry-types'.
5166 The call in the diary file should look like this:
5168 &%%(org-diary) ~/path/to/some/orgfile.org
5170 Use a separate line for each org file to check. Or, if you omit the file name,
5171 all files listed in `org-agenda-files' will be checked automatically:
5173 &%%(org-diary)
5175 If you don't give any arguments (as in the example above), the default value
5176 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5177 So the example above may also be written as
5179 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5181 The function expects the lisp variables `entry' and `date' to be provided
5182 by the caller, because this is how the calendar works. Don't use this
5183 function from a program - use `org-agenda-get-day-entries' instead."
5184 (when (> (- (float-time)
5185 org-agenda-last-marker-time)
5187 ;; I am not sure if this works with sticky agendas, because the marker
5188 ;; list is then no longer a global variable.
5189 (org-agenda-reset-markers))
5190 (org-compile-prefix-format 'agenda)
5191 (org-set-sorting-strategy 'agenda)
5192 (setq args (or args org-agenda-entry-types))
5193 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5194 (list entry)
5195 (org-agenda-files t)))
5196 (time (float-time))
5197 file rtn results)
5198 (when (or (not org-diary-last-run-time)
5199 (> (- time
5200 org-diary-last-run-time)
5202 (org-agenda-prepare-buffers files))
5203 (setq org-diary-last-run-time time)
5204 ;; If this is called during org-agenda, don't return any entries to
5205 ;; the calendar. Org Agenda will list these entries itself.
5206 (when org-disable-agenda-to-diary (setq files nil))
5207 (while (setq file (pop files))
5208 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5209 (setq results (append results rtn)))
5210 (when results
5211 (setq results
5212 (mapcar (lambda (i) (replace-regexp-in-string
5213 org-link-bracket-re "\\2" i)) results))
5214 (concat (org-agenda-finalize-entries results) "\n"))))
5216 ;;; Agenda entry finders
5218 (defun org-agenda--timestamp-to-absolute (&rest args)
5219 "Call `org-time-string-to-absolute' with ARGS.
5220 However, throw `:skip' whenever an error is raised."
5221 (condition-case e
5222 (apply #'org-time-string-to-absolute args)
5223 (org-diary-sexp-no-match (throw :skip nil))
5224 (error
5225 (message "%s; Skipping entry" (error-message-string e))
5226 (throw :skip nil))))
5228 (defun org-agenda-get-day-entries (file date &rest args)
5229 "Does the work for `org-diary' and `org-agenda'.
5230 FILE is the path to a file to be checked for entries. DATE is date like
5231 the one returned by `calendar-current-date'. ARGS are symbols indicating
5232 which kind of entries should be extracted. For details about these, see
5233 the documentation of `org-diary'."
5234 (let* ((org-startup-folded nil)
5235 (org-startup-align-all-tables nil)
5236 (buffer (if (file-exists-p file) (org-get-agenda-file-buffer file)
5237 (error "No such file %s" file))))
5238 (if (not buffer)
5239 ;; If file does not exist, signal it in diary nonetheless.
5240 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5241 (with-current-buffer buffer
5242 (unless (derived-mode-p 'org-mode)
5243 (error "Agenda file %s is not in Org mode" file))
5244 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5245 (setf org-agenda-current-date date)
5246 (save-excursion
5247 (save-restriction
5248 (if (eq buffer org-agenda-restrict)
5249 (narrow-to-region org-agenda-restrict-begin
5250 org-agenda-restrict-end)
5251 (widen))
5252 ;; Rationalize ARGS. Also make sure `:deadline' comes
5253 ;; first in order to populate DEADLINES before passing it.
5255 ;; We use `delq' since `org-uniquify' duplicates ARGS,
5256 ;; guarding us from modifying `org-agenda-entry-types'.
5257 (setf args (org-uniquify (or args org-agenda-entry-types)))
5258 (when (and (memq :scheduled args) (memq :scheduled* args))
5259 (setf args (delq :scheduled* args)))
5260 (cond
5261 ((memq :deadline args)
5262 (setf args (cons :deadline
5263 (delq :deadline (delq :deadline* args)))))
5264 ((memq :deadline* args)
5265 (setf args (cons :deadline* (delq :deadline* args)))))
5266 ;; Collect list of headlines. Return them flattened.
5267 (let ((case-fold-search nil) results deadlines)
5268 (dolist (arg args (apply #'nconc (nreverse results)))
5269 (pcase arg
5270 ((and :todo (guard (org-agenda-today-p date)))
5271 (push (org-agenda-get-todos) results))
5272 (:timestamp
5273 (push (org-agenda-get-blocks) results)
5274 (push (org-agenda-get-timestamps deadlines) results))
5275 (:sexp
5276 (push (org-agenda-get-sexps) results))
5277 (:scheduled
5278 (push (org-agenda-get-scheduled deadlines) results))
5279 (:scheduled*
5280 (push (org-agenda-get-scheduled deadlines t) results))
5281 (:closed
5282 (push (org-agenda-get-progress) results))
5283 (:deadline
5284 (setf deadlines (org-agenda-get-deadlines))
5285 (push deadlines results))
5286 (:deadline*
5287 (setf deadlines (org-agenda-get-deadlines t))
5288 (push deadlines results)))))))))))
5290 (defsubst org-em (x y list)
5291 "Is X or Y a member of LIST?"
5292 (or (memq x list) (memq y list)))
5294 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5295 (defvar org-agenda-sorting-strategy-selected nil)
5297 (defun org-agenda-entry-get-agenda-timestamp (pom)
5298 "Retrieve timestamp information for sorting agenda views.
5299 Given a point or marker POM, returns a cons cell of the timestamp
5300 and the timestamp type relevant for the sorting strategy in
5301 `org-agenda-sorting-strategy-selected'."
5302 (let (ts ts-date-type)
5303 (save-match-data
5304 (cond ((org-em 'scheduled-up 'scheduled-down
5305 org-agenda-sorting-strategy-selected)
5306 (setq ts (org-entry-get pom "SCHEDULED")
5307 ts-date-type " scheduled"))
5308 ((org-em 'deadline-up 'deadline-down
5309 org-agenda-sorting-strategy-selected)
5310 (setq ts (org-entry-get pom "DEADLINE")
5311 ts-date-type " deadline"))
5312 ((org-em 'ts-up 'ts-down
5313 org-agenda-sorting-strategy-selected)
5314 (setq ts (org-entry-get pom "TIMESTAMP")
5315 ts-date-type " timestamp"))
5316 ((org-em 'tsia-up 'tsia-down
5317 org-agenda-sorting-strategy-selected)
5318 (setq ts (org-entry-get pom "TIMESTAMP_IA")
5319 ts-date-type " timestamp_ia"))
5320 ((org-em 'timestamp-up 'timestamp-down
5321 org-agenda-sorting-strategy-selected)
5322 (setq ts (or (org-entry-get pom "SCHEDULED")
5323 (org-entry-get pom "DEADLINE")
5324 (org-entry-get pom "TIMESTAMP")
5325 (org-entry-get pom "TIMESTAMP_IA"))
5326 ts-date-type ""))
5327 (t (setq ts-date-type "")))
5328 (cons (when ts (ignore-errors (org-time-string-to-absolute ts)))
5329 ts-date-type))))
5331 (defun org-agenda-get-todos ()
5332 "Return the TODO information for agenda display."
5333 (let* ((props (list 'face nil
5334 'done-face 'org-agenda-done
5335 'org-not-done-regexp org-not-done-regexp
5336 'org-todo-regexp org-todo-regexp
5337 'org-complex-heading-regexp org-complex-heading-regexp
5338 'mouse-face 'highlight
5339 'help-echo
5340 (format "mouse-2 or RET jump to org file %s"
5341 (abbreviate-file-name buffer-file-name))))
5342 (case-fold-search nil)
5343 (regexp (format org-heading-keyword-regexp-format
5344 (cond
5345 ((and org-select-this-todo-keyword
5346 (equal org-select-this-todo-keyword "*"))
5347 org-todo-regexp)
5348 (org-select-this-todo-keyword
5349 (concat "\\("
5350 (mapconcat 'identity
5351 (org-split-string
5352 org-select-this-todo-keyword
5353 "|")
5354 "\\|") "\\)"))
5355 (t org-not-done-regexp))))
5356 marker priority category level tags todo-state
5357 ts-date ts-date-type ts-date-pair
5358 ee txt beg end inherited-tags todo-state-end-pos)
5359 (goto-char (point-min))
5360 (while (re-search-forward regexp nil t)
5361 (catch :skip
5362 (save-match-data
5363 (beginning-of-line)
5364 (org-agenda-skip)
5365 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5366 (unless (and (setq todo-state (org-get-todo-state))
5367 (setq todo-state-end-pos (match-end 2)))
5368 (goto-char end)
5369 (throw :skip nil))
5370 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5371 (goto-char (1+ beg))
5372 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5373 (throw :skip nil)))
5374 (goto-char (match-beginning 2))
5375 (setq marker (org-agenda-new-marker (match-beginning 0))
5376 category (org-get-category)
5377 ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
5378 ts-date (car ts-date-pair)
5379 ts-date-type (cdr ts-date-pair)
5380 txt (org-trim (buffer-substring (match-beginning 2) (match-end 0)))
5381 inherited-tags
5382 (or (eq org-agenda-show-inherited-tags 'always)
5383 (and (listp org-agenda-show-inherited-tags)
5384 (memq 'todo org-agenda-show-inherited-tags))
5385 (and (eq org-agenda-show-inherited-tags t)
5386 (or (eq org-agenda-use-tag-inheritance t)
5387 (memq 'todo org-agenda-use-tag-inheritance))))
5388 tags (org-get-tags nil (not inherited-tags))
5389 level (make-string (org-reduced-level (org-outline-level)) ? )
5390 txt (org-agenda-format-item "" txt level category tags t)
5391 priority (1+ (org-get-priority txt)))
5392 (org-add-props txt props
5393 'org-marker marker 'org-hd-marker marker
5394 'priority priority
5395 'level level
5396 'ts-date ts-date
5397 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5398 (push txt ee)
5399 (if org-agenda-todo-list-sublevels
5400 (goto-char todo-state-end-pos)
5401 (org-end-of-subtree 'invisible))))
5402 (nreverse ee)))
5404 (defun org-agenda-todo-custom-ignore-p (time n)
5405 "Check whether timestamp is farther away than n number of days.
5406 This function is invoked if `org-agenda-todo-ignore-deadlines',
5407 `org-agenda-todo-ignore-scheduled' or
5408 `org-agenda-todo-ignore-timestamp' is set to an integer."
5409 (let ((days (org-time-stamp-to-now
5410 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5411 (if (>= n 0)
5412 (>= days n)
5413 (<= days n))))
5415 ;;;###autoload
5416 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5417 (&optional end)
5418 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5419 (when (or org-agenda-todo-ignore-with-date
5420 org-agenda-todo-ignore-scheduled
5421 org-agenda-todo-ignore-deadlines
5422 org-agenda-todo-ignore-timestamp)
5423 (setq end (or end (save-excursion (outline-next-heading) (point))))
5424 (save-excursion
5425 (or (and org-agenda-todo-ignore-with-date
5426 (re-search-forward org-ts-regexp end t))
5427 (and org-agenda-todo-ignore-scheduled
5428 (re-search-forward org-scheduled-time-regexp end t)
5429 (cond
5430 ((eq org-agenda-todo-ignore-scheduled 'future)
5431 (> (org-time-stamp-to-now
5432 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5433 ((eq org-agenda-todo-ignore-scheduled 'past)
5434 (<= (org-time-stamp-to-now
5435 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5436 ((numberp org-agenda-todo-ignore-scheduled)
5437 (org-agenda-todo-custom-ignore-p
5438 (match-string 1) org-agenda-todo-ignore-scheduled))
5439 (t)))
5440 (and org-agenda-todo-ignore-deadlines
5441 (re-search-forward org-deadline-time-regexp end t)
5442 (cond
5443 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5444 ((eq org-agenda-todo-ignore-deadlines 'far)
5445 (not (org-deadline-close-p (match-string 1))))
5446 ((eq org-agenda-todo-ignore-deadlines 'future)
5447 (> (org-time-stamp-to-now
5448 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5449 ((eq org-agenda-todo-ignore-deadlines 'past)
5450 (<= (org-time-stamp-to-now
5451 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5452 ((numberp org-agenda-todo-ignore-deadlines)
5453 (org-agenda-todo-custom-ignore-p
5454 (match-string 1) org-agenda-todo-ignore-deadlines))
5455 (t (org-deadline-close-p (match-string 1)))))
5456 (and org-agenda-todo-ignore-timestamp
5457 (let ((buffer (current-buffer))
5458 (regexp
5459 (concat
5460 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5461 (start (point)))
5462 ;; Copy current buffer into a temporary one
5463 (with-temp-buffer
5464 (insert-buffer-substring buffer start end)
5465 (goto-char (point-min))
5466 ;; Delete SCHEDULED and DEADLINE items
5467 (while (re-search-forward regexp end t)
5468 (delete-region (match-beginning 0) (match-end 0)))
5469 (goto-char (point-min))
5470 ;; No search for timestamp left
5471 (when (re-search-forward org-ts-regexp nil t)
5472 (cond
5473 ((eq org-agenda-todo-ignore-timestamp 'future)
5474 (> (org-time-stamp-to-now
5475 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5476 ((eq org-agenda-todo-ignore-timestamp 'past)
5477 (<= (org-time-stamp-to-now
5478 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5479 ((numberp org-agenda-todo-ignore-timestamp)
5480 (org-agenda-todo-custom-ignore-p
5481 (match-string 1) org-agenda-todo-ignore-timestamp))
5482 (t))))))))))
5484 (defun org-agenda-get-timestamps (&optional deadlines)
5485 "Return the date stamp information for agenda display.
5486 Optional argument DEADLINES is a list of deadline items to be
5487 displayed in agenda view."
5488 (let* ((props (list 'face 'org-agenda-calendar-event
5489 'org-not-done-regexp org-not-done-regexp
5490 'org-todo-regexp org-todo-regexp
5491 'org-complex-heading-regexp org-complex-heading-regexp
5492 'mouse-face 'highlight
5493 'help-echo
5494 (format "mouse-2 or RET jump to Org file %s"
5495 (abbreviate-file-name buffer-file-name))))
5496 (current (calendar-absolute-from-gregorian date))
5497 (today (org-today))
5498 (deadline-position-alist
5499 (mapcar (lambda (d)
5500 (let ((m (get-text-property 0 'org-hd-marker d)))
5501 (and m (marker-position m))))
5502 deadlines))
5503 ;; Match time-stamps set to current date, time-stamps with
5504 ;; a repeater, and S-exp time-stamps.
5505 (regexp
5506 (concat
5507 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5508 (regexp-quote
5509 (substring
5510 (format-time-string
5511 (car org-time-stamp-formats)
5512 (apply #'encode-time ; DATE bound by calendar
5513 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5514 1 11))
5515 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5516 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5517 timestamp-items)
5518 (goto-char (point-min))
5519 (while (re-search-forward regexp nil t)
5520 ;; Skip date ranges, scheduled and deadlines, which are handled
5521 ;; specially. Also skip time-stamps before first headline as
5522 ;; there would be no entry to add to the agenda. Eventually,
5523 ;; ignore clock entries.
5524 (catch :skip
5525 (save-match-data
5526 (when (or (org-at-date-range-p)
5527 (org-at-planning-p)
5528 (org-before-first-heading-p)
5529 (and org-agenda-include-inactive-timestamps
5530 (org-at-clock-log-p)))
5531 (throw :skip nil))
5532 (org-agenda-skip))
5533 (let* ((pos (match-beginning 0))
5534 (repeat (match-string 1))
5535 (sexp-entry (match-string 3))
5536 (time-stamp (if (or repeat sexp-entry) (match-string 0)
5537 (save-excursion
5538 (goto-char pos)
5539 (looking-at org-ts-regexp-both)
5540 (match-string 0))))
5541 (todo-state (org-get-todo-state))
5542 (warntime (get-text-property (point) 'org-appt-warntime))
5543 (done? (member todo-state org-done-keywords)))
5544 ;; Possibly skip done tasks.
5545 (when (and done? org-agenda-skip-timestamp-if-done)
5546 (throw :skip t))
5547 ;; S-exp entry doesn't match current day: skip it.
5548 (when (and sexp-entry (not (org-diary-sexp-entry sexp-entry "" date)))
5549 (throw :skip nil))
5550 (when repeat
5551 (let* ((past
5552 ;; A repeating time stamp is shown at its base
5553 ;; date and every repeated date up to TODAY. If
5554 ;; `org-agenda-prefer-last-repeat' is non-nil,
5555 ;; however, only the last repeat before today
5556 ;; (inclusive) is shown.
5557 (org-agenda--timestamp-to-absolute
5558 repeat
5559 (if (or (> current today)
5560 (eq org-agenda-prefer-last-repeat t)
5561 (member todo-state org-agenda-prefer-last-repeat))
5562 today
5563 current)
5564 'past (current-buffer) pos))
5565 (future
5566 ;; Display every repeated date past TODAY
5567 ;; (exclusive) unless
5568 ;; `org-agenda-show-future-repeats' is nil. If
5569 ;; this variable is set to `next', only display
5570 ;; the first repeated date after TODAY
5571 ;; (exclusive).
5572 (cond
5573 ((<= current today) past)
5574 ((not org-agenda-show-future-repeats) past)
5576 (let ((base (if (eq org-agenda-show-future-repeats 'next)
5577 (1+ today)
5578 current)))
5579 (org-agenda--timestamp-to-absolute
5580 repeat base 'future (current-buffer) pos))))))
5581 (when (and (/= current past) (/= current future))
5582 (throw :skip nil))))
5583 (save-excursion
5584 (re-search-backward org-outline-regexp-bol nil t)
5585 ;; Possibly skip time-stamp when a deadline is set.
5586 (when (and org-agenda-skip-timestamp-if-deadline-is-shown
5587 (assq (point) deadline-position-alist))
5588 (throw :skip nil))
5589 (let* ((category (org-get-category pos))
5590 (inherited-tags
5591 (or (eq org-agenda-show-inherited-tags 'always)
5592 (and (consp org-agenda-show-inherited-tags)
5593 (memq 'agenda org-agenda-show-inherited-tags))
5594 (and (eq org-agenda-show-inherited-tags t)
5595 (or (eq org-agenda-use-tag-inheritance t)
5596 (memq 'agenda
5597 org-agenda-use-tag-inheritance)))))
5598 (tags (org-get-tags nil (not inherited-tags)))
5599 (level (make-string (org-reduced-level (org-outline-level))
5600 ?\s))
5601 (head (and (looking-at "\\*+[ \t]+\\(.*\\)")
5602 (match-string 1)))
5603 (inactive? (= (char-after pos) ?\[))
5604 (habit? (and (fboundp 'org-is-habit-p) (org-is-habit-p)))
5605 (item
5606 (org-agenda-format-item
5607 (and inactive? org-agenda-inactive-leader)
5608 head level category tags time-stamp org-ts-regexp habit?)))
5609 (org-add-props item props
5610 'priority (if habit?
5611 (org-habit-get-priority (org-habit-parse-todo))
5612 (org-get-priority item))
5613 'org-marker (org-agenda-new-marker pos)
5614 'org-hd-marker (org-agenda-new-marker)
5615 'date date
5616 'level level
5617 'ts-date (if repeat (org-agenda--timestamp-to-absolute repeat)
5618 current)
5619 'todo-state todo-state
5620 'warntime warntime
5621 'type "timestamp")
5622 (push item timestamp-items))))
5623 (when org-agenda-skip-additional-timestamps-same-entry
5624 (outline-next-heading))))
5625 (nreverse timestamp-items)))
5627 (defun org-agenda-get-sexps ()
5628 "Return the sexp information for agenda display."
5629 (require 'diary-lib)
5630 (let* ((props (list 'face 'org-agenda-calendar-sexp
5631 'mouse-face 'highlight
5632 'help-echo
5633 (format "mouse-2 or RET jump to org file %s"
5634 (abbreviate-file-name buffer-file-name))))
5635 (regexp "^&?%%(")
5636 marker category extra level ee txt tags entry
5637 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5638 (goto-char (point-min))
5639 (while (re-search-forward regexp nil t)
5640 (catch :skip
5641 (org-agenda-skip)
5642 (setq beg (match-beginning 0))
5643 (goto-char (1- (match-end 0)))
5644 (setq b (point))
5645 (forward-sexp 1)
5646 (setq sexp (buffer-substring b (point)))
5647 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5648 (org-trim (match-string 1))
5649 ""))
5650 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5651 (when result
5652 (setq marker (org-agenda-new-marker beg)
5653 level (make-string (org-reduced-level (org-outline-level)) ? )
5654 category (org-get-category beg)
5655 inherited-tags
5656 (or (eq org-agenda-show-inherited-tags 'always)
5657 (and (listp org-agenda-show-inherited-tags)
5658 (memq 'agenda org-agenda-show-inherited-tags))
5659 (and (eq org-agenda-show-inherited-tags t)
5660 (or (eq org-agenda-use-tag-inheritance t)
5661 (memq 'agenda org-agenda-use-tag-inheritance))))
5662 tags (org-get-tags nil (not inherited-tags))
5663 todo-state (org-get-todo-state)
5664 warntime (get-text-property (point) 'org-appt-warntime)
5665 extra nil)
5667 (dolist (r (if (stringp result)
5668 (list result)
5669 result)) ;; we expect a list here
5670 (when (and org-agenda-diary-sexp-prefix
5671 (string-match org-agenda-diary-sexp-prefix r))
5672 (setq extra (match-string 0 r)
5673 r (replace-match "" nil nil r)))
5674 (if (string-match "\\S-" r)
5675 (setq txt r)
5676 (setq txt "SEXP entry returned empty string"))
5677 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5678 (org-add-props txt props 'org-marker marker
5679 'date date 'todo-state todo-state
5680 'level level 'type "sexp" 'warntime warntime)
5681 (push txt ee)))))
5682 (nreverse ee)))
5684 ;; Calendar sanity: define some functions that are independent of
5685 ;; `calendar-date-style'.
5686 (defun org-anniversary (year month day &optional mark)
5687 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5688 (with-no-warnings
5689 (let ((calendar-date-style 'iso))
5690 (diary-anniversary year month day mark))))
5691 (defun org-cyclic (N year month day &optional mark)
5692 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5693 (with-no-warnings
5694 (let ((calendar-date-style 'iso))
5695 (diary-cyclic N year month day mark))))
5696 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5697 "Like `diary-block', but with fixed (ISO) order of arguments."
5698 (with-no-warnings
5699 (let ((calendar-date-style 'iso))
5700 (diary-block Y1 M1 D1 Y2 M2 D2 mark))))
5701 (defun org-date (year month day &optional mark)
5702 "Like `diary-date', but with fixed (ISO) order of arguments."
5703 (with-no-warnings
5704 (let ((calendar-date-style 'iso))
5705 (diary-date year month day mark))))
5707 ;; Define the `org-class' function
5708 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5709 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5710 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5711 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5712 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5713 `holidays', then any date that is known by the Emacs calendar to be a
5714 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5715 then those holidays will be skipped."
5716 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5717 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5718 (d (calendar-absolute-from-gregorian date))
5719 (h (when skip-weeks (calendar-check-holidays date))))
5720 (and
5721 (<= date1 d)
5722 (<= d date2)
5723 (= (calendar-day-of-week date) dayname)
5724 (or (not skip-weeks)
5725 (progn
5726 (require 'cal-iso)
5727 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5728 (not (or (and h (memq 'holidays skip-weeks))
5729 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5730 entry)))
5732 (defalias 'org-get-closed 'org-agenda-get-progress)
5733 (defun org-agenda-get-progress ()
5734 "Return the logged TODO entries for agenda display."
5735 (let* ((props (list 'mouse-face 'highlight
5736 'org-not-done-regexp org-not-done-regexp
5737 'org-todo-regexp org-todo-regexp
5738 'org-complex-heading-regexp org-complex-heading-regexp
5739 'help-echo
5740 (format "mouse-2 or RET jump to org file %s"
5741 (abbreviate-file-name buffer-file-name))))
5742 (items (if (consp org-agenda-show-log-scoped)
5743 org-agenda-show-log-scoped
5744 (if (eq org-agenda-show-log-scoped 'clockcheck)
5745 '(clock)
5746 org-agenda-log-mode-items)))
5747 (parts
5748 (delq nil
5749 (list
5750 (when (memq 'closed items) (concat "\\<" org-closed-string))
5751 (when (memq 'clock items) (concat "\\<" org-clock-string))
5752 (when (memq 'state items)
5753 (format "- +State \"%s\".*?" org-todo-regexp)))))
5754 (parts-re (if parts (mapconcat 'identity parts "\\|")
5755 (error "`org-agenda-log-mode-items' is empty")))
5756 (regexp (concat
5757 "\\(" parts-re "\\)"
5758 " *\\["
5759 (regexp-quote
5760 (substring
5761 (format-time-string
5762 (car org-time-stamp-formats)
5763 (apply 'encode-time ; DATE bound by calendar
5764 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5765 1 11))))
5766 (org-agenda-search-headline-for-time nil)
5767 marker hdmarker priority category level tags closedp type
5768 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5769 (goto-char (point-min))
5770 (while (re-search-forward regexp nil t)
5771 (catch :skip
5772 (org-agenda-skip)
5773 (setq marker (org-agenda-new-marker (match-beginning 0))
5774 closedp (equal (match-string 1) org-closed-string)
5775 statep (equal (string-to-char (match-string 1)) ?-)
5776 clockp (not (or closedp statep))
5777 state (and statep (match-string 2))
5778 category (org-get-category (match-beginning 0))
5779 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5780 (when (string-match "\\]" timestr)
5781 ;; substring should only run to end of time stamp
5782 (setq rest (substring timestr (match-end 0))
5783 timestr (substring timestr 0 (match-end 0)))
5784 (if (and (not closedp) (not statep)
5785 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5786 rest))
5787 (progn (setq timestr (concat (substring timestr 0 -1)
5788 "-" (match-string 1 rest) "]"))
5789 (setq clocked (match-string 2 rest)))
5790 (setq clocked "-")))
5791 (save-excursion
5792 (setq extra
5793 (cond
5794 ((not org-agenda-log-mode-add-notes) nil)
5795 (statep
5796 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5797 (match-string 1)))
5798 (clockp
5799 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5800 (match-string 1)))))
5801 (if (not (re-search-backward org-outline-regexp-bol nil t))
5802 (throw :skip nil)
5803 (goto-char (match-beginning 0))
5804 (setq hdmarker (org-agenda-new-marker)
5805 inherited-tags
5806 (or (eq org-agenda-show-inherited-tags 'always)
5807 (and (listp org-agenda-show-inherited-tags)
5808 (memq 'todo org-agenda-show-inherited-tags))
5809 (and (eq org-agenda-show-inherited-tags t)
5810 (or (eq org-agenda-use-tag-inheritance t)
5811 (memq 'todo org-agenda-use-tag-inheritance))))
5812 tags (org-get-tags nil (not inherited-tags))
5813 level (make-string (org-reduced-level (org-outline-level)) ? ))
5814 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5815 (setq txt (match-string 1))
5816 (when extra
5817 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5818 (setq txt (concat (substring txt 0 (match-beginning 1))
5819 " - " extra " " (match-string 2 txt)))
5820 (setq txt (concat txt " - " extra))))
5821 (setq txt (org-agenda-format-item
5822 (cond
5823 (closedp "Closed: ")
5824 (statep (concat "State: (" state ")"))
5825 (t (concat "Clocked: (" clocked ")")))
5826 txt level category tags timestr)))
5827 (setq type (cond (closedp "closed")
5828 (statep "state")
5829 (t "clock")))
5830 (setq priority 100000)
5831 (org-add-props txt props
5832 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5833 'priority priority 'level level
5834 'type type 'date date
5835 'undone-face 'org-warning 'done-face 'org-agenda-done)
5836 (push txt ee))
5837 (goto-char (point-at-eol))))
5838 (nreverse ee)))
5840 (defun org-agenda-show-clocking-issues ()
5841 "Add overlays, showing issues with clocking.
5842 See also the user option `org-agenda-clock-consistency-checks'."
5843 (interactive)
5844 (let* ((pl org-agenda-clock-consistency-checks)
5845 (re (concat "^[ \t]*"
5846 org-clock-string
5847 "[ \t]+"
5848 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5849 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5850 (tlstart 0.)
5851 (tlend 0.)
5852 (maxtime (org-duration-to-minutes
5853 (or (plist-get pl :max-duration) "24:00")))
5854 (mintime (org-duration-to-minutes
5855 (or (plist-get pl :min-duration) 0)))
5856 (maxgap (org-duration-to-minutes
5857 ;; default 30:00 means never complain
5858 (or (plist-get pl :max-gap) "30:00")))
5859 (gapok (mapcar #'org-duration-to-minutes
5860 (plist-get pl :gap-ok-around)))
5861 (def-face (or (plist-get pl :default-face)
5862 '((:background "DarkRed") (:foreground "white"))))
5863 issue face m te ts dt ov)
5864 (goto-char (point-min))
5865 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5866 (setq issue nil face def-face)
5867 (catch 'next
5868 (setq m (org-get-at-bol 'org-marker)
5869 te nil ts nil)
5870 (unless (and m (markerp m))
5871 (setq issue "No valid clock line") (throw 'next t))
5872 (org-with-point-at m
5873 (save-excursion
5874 (goto-char (point-at-bol))
5875 (unless (looking-at re)
5876 (error "No valid Clock line")
5877 (throw 'next t))
5878 (unless (match-end 3)
5879 (setq issue "No end time"
5880 face (or (plist-get pl :no-end-time-face) face))
5881 (throw 'next t))
5882 (setq ts (match-string 1)
5883 te (match-string 3)
5884 ts (float-time
5885 (apply #'encode-time (org-parse-time-string ts)))
5886 te (float-time
5887 (apply #'encode-time (org-parse-time-string te)))
5888 dt (- te ts))))
5889 (cond
5890 ((> dt (* 60 maxtime))
5891 ;; a very long clocking chunk
5892 (setq issue (format "Clocking interval is very long: %s"
5893 (org-duration-from-minutes (floor dt 60)))
5894 face (or (plist-get pl :long-face) face)))
5895 ((< dt (* 60 mintime))
5896 ;; a very short clocking chunk
5897 (setq issue (format "Clocking interval is very short: %s"
5898 (org-duration-from-minutes (floor dt 60)))
5899 face (or (plist-get pl :short-face) face)))
5900 ((and (> tlend 0) (< ts tlend))
5901 ;; Two clock entries are overlapping
5902 (setq issue (format "Clocking overlap: %d minutes"
5903 (/ (- tlend ts) 60))
5904 face (or (plist-get pl :overlap-face) face)))
5905 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5906 ;; There is a gap, lets see if we need to report it
5907 (unless (org-agenda-check-clock-gap tlend ts gapok)
5908 (setq issue (format "Clocking gap: %d minutes"
5909 (/ (- ts tlend) 60))
5910 face (or (plist-get pl :gap-face) face))))
5911 (t nil)))
5912 (setq tlend (or te tlend) tlstart (or ts tlstart))
5913 (when issue
5914 ;; OK, there was some issue, add an overlay to show the issue
5915 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5916 (overlay-put ov 'before-string
5917 (concat
5918 (org-add-props
5919 (format "%-43s" (concat " " issue))
5921 'face face)
5922 "\n"))
5923 (overlay-put ov 'evaporate t)))))
5925 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5926 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5927 (catch 'exit
5928 (unless ok-list
5929 ;; there are no OK times for gaps...
5930 (throw 'exit nil))
5931 (when (> (- (/ t2 36000) (/ t1 36000)) 24)
5932 ;; This is more than 24 hours, so it is OK.
5933 ;; because we have at least one OK time, that must be in the
5934 ;; 24 hour interval.
5935 (throw 'exit t))
5936 ;; We have a shorter gap.
5937 ;; Now we have to get the minute of the day when these times are
5938 (let* ((t1dec (decode-time (seconds-to-time t1)))
5939 (t2dec (decode-time (seconds-to-time t2)))
5940 ;; compute the minute on the day
5941 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5942 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5943 (when (< min2 min1)
5944 ;; if min2 is smaller than min1, this means it is on the next day.
5945 ;; Wrap it to after midnight.
5946 (setq min2 (+ min2 1440)))
5947 ;; Now check if any of the OK times is in the gap
5948 (mapc (lambda (x)
5949 ;; Wrap the time to after midnight if necessary
5950 (when (< x min1) (setq x (+ x 1440)))
5951 ;; Check if in interval
5952 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5953 ok-list)
5954 ;; Nope, this gap is not OK
5955 nil)))
5957 (defun org-agenda-get-deadlines (&optional with-hour)
5958 "Return the deadline information for agenda display.
5959 When WITH-HOUR is non-nil, only return deadlines with an hour
5960 specification like [h]h:mm."
5961 (let* ((props (list 'mouse-face 'highlight
5962 'org-not-done-regexp org-not-done-regexp
5963 'org-todo-regexp org-todo-regexp
5964 'org-complex-heading-regexp org-complex-heading-regexp
5965 'help-echo
5966 (format "mouse-2 or RET jump to org file %s"
5967 (abbreviate-file-name buffer-file-name))))
5968 (regexp (if with-hour
5969 org-deadline-time-hour-regexp
5970 org-deadline-time-regexp))
5971 (today (org-today))
5972 (today? (org-agenda-today-p date)) ; DATE bound by calendar.
5973 (current (calendar-absolute-from-gregorian date))
5974 deadline-items)
5975 (goto-char (point-min))
5976 (while (re-search-forward regexp nil t)
5977 (catch :skip
5978 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
5979 (org-agenda-skip)
5980 (let* ((s (match-string 1))
5981 (pos (1- (match-beginning 1)))
5982 (todo-state (save-match-data (org-get-todo-state)))
5983 (done? (member todo-state org-done-keywords))
5984 (sexp? (string-prefix-p "%%" s))
5985 ;; DEADLINE is the deadline date for the entry. It is
5986 ;; either the base date or the last repeat, according
5987 ;; to `org-agenda-prefer-last-repeat'.
5988 (deadline
5989 (cond
5990 (sexp? (org-agenda--timestamp-to-absolute s current))
5991 ((or (eq org-agenda-prefer-last-repeat t)
5992 (member todo-state org-agenda-prefer-last-repeat))
5993 (org-agenda--timestamp-to-absolute
5994 s today 'past (current-buffer) pos))
5995 (t (org-agenda--timestamp-to-absolute s))))
5996 ;; REPEAT is the future repeat closest from CURRENT,
5997 ;; according to `org-agenda-show-future-repeats'. If
5998 ;; the latter is nil, or if the time stamp has no
5999 ;; repeat part, default to DEADLINE.
6000 (repeat
6001 (cond
6002 (sexp? deadline)
6003 ((<= current today) deadline)
6004 ((not org-agenda-show-future-repeats) deadline)
6006 (let ((base (if (eq org-agenda-show-future-repeats 'next)
6007 (1+ today)
6008 current)))
6009 (org-agenda--timestamp-to-absolute
6010 s base 'future (current-buffer) pos)))))
6011 (diff (- deadline current))
6012 (suppress-prewarning
6013 (let ((scheduled
6014 (and org-agenda-skip-deadline-prewarning-if-scheduled
6015 (org-entry-get nil "SCHEDULED"))))
6016 (cond
6017 ((not scheduled) nil)
6018 ;; The current item has a scheduled date, so
6019 ;; evaluate its prewarning lead time.
6020 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
6021 ;; Use global prewarning-restart lead time.
6022 org-agenda-skip-deadline-prewarning-if-scheduled)
6023 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
6024 'pre-scheduled)
6025 ;; Set pre-warning to no earlier than SCHEDULED.
6026 (min (- deadline
6027 (org-agenda--timestamp-to-absolute scheduled))
6028 org-deadline-warning-days))
6029 ;; Set pre-warning to deadline.
6030 (t 0))))
6031 (wdays (or suppress-prewarning (org-get-wdays s))))
6032 (cond
6033 ;; Only display deadlines at their base date, at future
6034 ;; repeat occurrences or in today agenda.
6035 ((= current deadline) nil)
6036 ((= current repeat) nil)
6037 ((not today?) (throw :skip nil))
6038 ;; Upcoming deadline: display within warning period WDAYS.
6039 ((> deadline current) (when (> diff wdays) (throw :skip nil)))
6040 ;; Overdue deadline: warn about it for
6041 ;; `org-deadline-past-days' duration.
6042 (t (when (< org-deadline-past-days (- diff)) (throw :skip nil))))
6043 ;; Possibly skip done tasks.
6044 (when (and done?
6045 (or org-agenda-skip-deadline-if-done
6046 (/= deadline current)))
6047 (throw :skip nil))
6048 (save-excursion
6049 (re-search-backward "^\\*+[ \t]+" nil t)
6050 (goto-char (match-end 0))
6051 (let* ((category (org-get-category))
6052 (level (make-string (org-reduced-level (org-outline-level))
6053 ?\s))
6054 (head (buffer-substring (point) (line-end-position)))
6055 (inherited-tags
6056 (or (eq org-agenda-show-inherited-tags 'always)
6057 (and (listp org-agenda-show-inherited-tags)
6058 (memq 'agenda org-agenda-show-inherited-tags))
6059 (and (eq org-agenda-show-inherited-tags t)
6060 (or (eq org-agenda-use-tag-inheritance t)
6061 (memq 'agenda
6062 org-agenda-use-tag-inheritance)))))
6063 (tags (org-get-tags nil (not inherited-tags)))
6064 (time
6065 (cond
6066 ;; No time of day designation if it is only
6067 ;; a reminder.
6068 ((and (/= current deadline) (/= current repeat)) nil)
6069 ((string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6070 (concat (substring s (match-beginning 1)) " "))
6071 (t 'time)))
6072 (item
6073 (org-agenda-format-item
6074 ;; Insert appropriate suffixes before deadlines.
6075 ;; Those only apply to today agenda.
6076 (pcase-let ((`(,now ,future ,past)
6077 org-agenda-deadline-leaders))
6078 (cond
6079 ((and today? (< deadline today)) (format past (- diff)))
6080 ((and today? (> deadline today)) (format future diff))
6081 (t now)))
6082 head level category tags time))
6083 (face (org-agenda-deadline-face
6084 (- 1 (/ (float diff) (max wdays 1)))))
6085 (upcoming? (and today? (> deadline today)))
6086 (warntime (get-text-property (point) 'org-appt-warntime)))
6087 (org-add-props item props
6088 'org-marker (org-agenda-new-marker pos)
6089 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
6090 'warntime warntime
6091 'level level
6092 'ts-date deadline
6093 'priority
6094 ;; Adjust priority to today reminders about deadlines.
6095 ;; Overdue deadlines get the highest priority
6096 ;; increase, then imminent deadlines and eventually
6097 ;; more distant deadlines.
6098 (let ((adjust (if today? (- diff) 0)))
6099 (+ adjust (org-get-priority item)))
6100 'todo-state todo-state
6101 'type (if upcoming? "upcoming-deadline" "deadline")
6102 'date (if upcoming? date deadline)
6103 'face (if done? 'org-agenda-done face)
6104 'undone-face face
6105 'done-face 'org-agenda-done)
6106 (push item deadline-items))))))
6107 (nreverse deadline-items)))
6109 (defun org-agenda-deadline-face (fraction)
6110 "Return the face to displaying a deadline item.
6111 FRACTION is what fraction of the head-warning time has passed."
6112 (assoc-default fraction org-agenda-deadline-faces #'<=))
6114 (defun org-agenda-get-scheduled (&optional deadlines with-hour)
6115 "Return the scheduled information for agenda display.
6116 Optional argument DEADLINES is a list of deadline items to be
6117 displayed in agenda view. When WITH-HOUR is non-nil, only return
6118 scheduled items with an hour specification like [h]h:mm."
6119 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
6120 'org-todo-regexp org-todo-regexp
6121 'org-complex-heading-regexp org-complex-heading-regexp
6122 'done-face 'org-agenda-done
6123 'mouse-face 'highlight
6124 'help-echo
6125 (format "mouse-2 or RET jump to Org file %s"
6126 (abbreviate-file-name buffer-file-name))))
6127 (regexp (if with-hour
6128 org-scheduled-time-hour-regexp
6129 org-scheduled-time-regexp))
6130 (today (org-today))
6131 (todayp (org-agenda-today-p date)) ; DATE bound by calendar.
6132 (current (calendar-absolute-from-gregorian date))
6133 (deadline-pos
6134 (mapcar (lambda (d)
6135 (let ((m (get-text-property 0 'org-hd-marker d)))
6136 (and m (marker-position m))))
6137 deadlines))
6138 scheduled-items)
6139 (goto-char (point-min))
6140 (while (re-search-forward regexp nil t)
6141 (catch :skip
6142 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
6143 (org-agenda-skip)
6144 (let* ((s (match-string 1))
6145 (pos (1- (match-beginning 1)))
6146 (todo-state (save-match-data (org-get-todo-state)))
6147 (donep (member todo-state org-done-keywords))
6148 (sexp? (string-prefix-p "%%" s))
6149 ;; SCHEDULE is the scheduled date for the entry. It is
6150 ;; either the bare date or the last repeat, according
6151 ;; to `org-agenda-prefer-last-repeat'.
6152 (schedule
6153 (cond
6154 (sexp? (org-agenda--timestamp-to-absolute s current))
6155 ((or (eq org-agenda-prefer-last-repeat t)
6156 (member todo-state org-agenda-prefer-last-repeat))
6157 (org-agenda--timestamp-to-absolute
6158 s today 'past (current-buffer) pos))
6159 (t (org-agenda--timestamp-to-absolute s))))
6160 ;; REPEAT is the future repeat closest from CURRENT,
6161 ;; according to `org-agenda-show-future-repeats'. If
6162 ;; the latter is nil, or if the time stamp has no
6163 ;; repeat part, default to SCHEDULE.
6164 (repeat
6165 (cond
6166 (sexp? schedule)
6167 ((<= current today) schedule)
6168 ((not org-agenda-show-future-repeats) schedule)
6170 (let ((base (if (eq org-agenda-show-future-repeats 'next)
6171 (1+ today)
6172 current)))
6173 (org-agenda--timestamp-to-absolute
6174 s base 'future (current-buffer) pos)))))
6175 (diff (- current schedule))
6176 (warntime (get-text-property (point) 'org-appt-warntime))
6177 (pastschedp (< schedule today))
6178 (futureschedp (> schedule today))
6179 (habitp (and (fboundp 'org-is-habit-p) (org-is-habit-p)))
6180 (suppress-delay
6181 (let ((deadline (and org-agenda-skip-scheduled-delay-if-deadline
6182 (org-entry-get nil "DEADLINE"))))
6183 (cond
6184 ((not deadline) nil)
6185 ;; The current item has a deadline date, so
6186 ;; evaluate its delay time.
6187 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6188 ;; Use global delay time.
6189 (- org-agenda-skip-scheduled-delay-if-deadline))
6190 ((eq org-agenda-skip-scheduled-delay-if-deadline
6191 'post-deadline)
6192 ;; Set delay to no later than DEADLINE.
6193 (min (- schedule
6194 (org-agenda--timestamp-to-absolute deadline))
6195 org-scheduled-delay-days))
6196 (t 0))))
6197 (ddays
6198 (cond
6199 ;; Nullify delay when a repeater triggered already
6200 ;; and the delay is of the form --Xd.
6201 ((and (string-match-p "--[0-9]+[hdwmy]" s)
6202 (> schedule (org-agenda--timestamp-to-absolute s)))
6204 (suppress-delay
6205 (let ((org-scheduled-delay-days suppress-delay))
6206 (org-get-wdays s t t)))
6207 (t (org-get-wdays s t)))))
6208 ;; Display scheduled items at base date (SCHEDULE), today if
6209 ;; scheduled before the current date, and at any repeat past
6210 ;; today. However, skip delayed items and items that have
6211 ;; been displayed for more than `org-scheduled-past-days'.
6212 (unless (and todayp
6213 habitp
6214 (bound-and-true-p org-habit-show-all-today))
6215 (when (or (and (> ddays 0) (< diff ddays))
6216 (> diff (or (and habitp org-habit-scheduled-past-days)
6217 org-scheduled-past-days))
6218 (> schedule current)
6219 (and (/= current schedule)
6220 (/= current today)
6221 (/= current repeat)))
6222 (throw :skip nil)))
6223 ;; Possibly skip done tasks.
6224 (when (and donep
6225 (or org-agenda-skip-scheduled-if-done
6226 (/= schedule current)))
6227 (throw :skip nil))
6228 ;; Skip entry if it already appears as a deadline, per
6229 ;; `org-agenda-skip-scheduled-if-deadline-is-shown'. This
6230 ;; doesn't apply to habits.
6231 (when (pcase org-agenda-skip-scheduled-if-deadline-is-shown
6232 ((guard
6233 (or (not (memq (line-beginning-position 0) deadline-pos))
6234 habitp))
6235 nil)
6236 (`repeated-after-deadline
6237 (let ((deadline (time-to-days
6238 (org-get-deadline-time (point)))))
6239 (and (<= schedule deadline) (> current deadline))))
6240 (`not-today pastschedp)
6241 (`t t)
6242 (_ nil))
6243 (throw :skip nil))
6244 ;; Skip habits if `org-habit-show-habits' is nil, or if we
6245 ;; only show them for today. Also skip done habits.
6246 (when (and habitp
6247 (or donep
6248 (not (bound-and-true-p org-habit-show-habits))
6249 (and (not todayp)
6250 (bound-and-true-p
6251 org-habit-show-habits-only-for-today))))
6252 (throw :skip nil))
6253 (save-excursion
6254 (re-search-backward "^\\*+[ \t]+" nil t)
6255 (goto-char (match-end 0))
6256 (let* ((category (org-get-category))
6257 (inherited-tags
6258 (or (eq org-agenda-show-inherited-tags 'always)
6259 (and (listp org-agenda-show-inherited-tags)
6260 (memq 'agenda org-agenda-show-inherited-tags))
6261 (and (eq org-agenda-show-inherited-tags t)
6262 (or (eq org-agenda-use-tag-inheritance t)
6263 (memq 'agenda
6264 org-agenda-use-tag-inheritance)))))
6265 (tags (org-get-tags nil (not inherited-tags)))
6266 (level (make-string (org-reduced-level (org-outline-level))
6267 ?\s))
6268 (head (buffer-substring (point) (line-end-position)))
6269 (time
6270 (cond
6271 ;; No time of day designation if it is only a
6272 ;; reminder, except for habits, which always show
6273 ;; the time of day. Habits are an exception
6274 ;; because if there is a time of day, that is
6275 ;; interpreted to mean they should usually happen
6276 ;; then, even if doing the habit was missed.
6277 ((and
6278 (not habitp)
6279 (/= current schedule)
6280 (/= current repeat))
6281 nil)
6282 ((string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6283 (concat (substring s (match-beginning 1)) " "))
6284 (t 'time)))
6285 (item
6286 (org-agenda-format-item
6287 (pcase-let ((`(,first ,past) org-agenda-scheduled-leaders))
6288 ;; Show a reminder of a past scheduled today.
6289 (if (and todayp pastschedp)
6290 (format past diff)
6291 first))
6292 head level category tags time nil habitp))
6293 (face (cond ((and (not habitp) pastschedp)
6294 'org-scheduled-previously)
6295 ((and habitp futureschedp)
6296 'org-agenda-done)
6297 (todayp 'org-scheduled-today)
6298 (t 'org-scheduled)))
6299 (habitp (and habitp (org-habit-parse-todo))))
6300 (org-add-props item props
6301 'undone-face face
6302 'face (if donep 'org-agenda-done face)
6303 'org-marker (org-agenda-new-marker pos)
6304 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
6305 'type (if pastschedp "past-scheduled" "scheduled")
6306 'date (if pastschedp schedule date)
6307 'ts-date schedule
6308 'warntime warntime
6309 'level level
6310 'priority (if habitp (org-habit-get-priority habitp)
6311 (+ 99 diff (org-get-priority item)))
6312 'org-habit-p habitp
6313 'todo-state todo-state)
6314 (push item scheduled-items))))))
6315 (nreverse scheduled-items)))
6317 (defun org-agenda-get-blocks ()
6318 "Return the date-range information for agenda display."
6319 (let* ((props (list 'face nil
6320 'org-not-done-regexp org-not-done-regexp
6321 'org-todo-regexp org-todo-regexp
6322 'org-complex-heading-regexp org-complex-heading-regexp
6323 'mouse-face 'highlight
6324 'help-echo
6325 (format "mouse-2 or RET jump to org file %s"
6326 (abbreviate-file-name buffer-file-name))))
6327 (regexp org-tr-regexp)
6328 (d0 (calendar-absolute-from-gregorian date))
6329 marker hdmarker ee txt d1 d2 s1 s2 category
6330 level todo-state tags pos head donep inherited-tags)
6331 (goto-char (point-min))
6332 (while (re-search-forward regexp nil t)
6333 (catch :skip
6334 (org-agenda-skip)
6335 (setq pos (point))
6336 (let ((start-time (match-string 1))
6337 (end-time (match-string 2)))
6338 (setq s1 (match-string 1)
6339 s2 (match-string 2)
6340 d1 (time-to-days
6341 (condition-case err
6342 (org-time-string-to-time s1)
6343 (error
6344 (error
6345 "Bad timestamp %S at %d in buffer %S\nError was: %s"
6348 (current-buffer)
6349 (error-message-string err)))))
6350 d2 (time-to-days
6351 (condition-case err
6352 (org-time-string-to-time s2)
6353 (error
6354 (error
6355 "Bad timestamp %S at %d in buffer %S\nError was: %s"
6358 (current-buffer)
6359 (error-message-string err))))))
6360 (when (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6361 ;; Only allow days between the limits, because the normal
6362 ;; date stamps will catch the limits.
6363 (save-excursion
6364 (setq todo-state (org-get-todo-state))
6365 (setq donep (member todo-state org-done-keywords))
6366 (when (and donep org-agenda-skip-timestamp-if-done)
6367 (throw :skip t))
6368 (setq marker (org-agenda-new-marker (point))
6369 category (org-get-category))
6370 (if (not (re-search-backward org-outline-regexp-bol nil t))
6371 (throw :skip nil)
6372 (goto-char (match-beginning 0))
6373 (setq hdmarker (org-agenda-new-marker (point))
6374 inherited-tags
6375 (or (eq org-agenda-show-inherited-tags 'always)
6376 (and (listp org-agenda-show-inherited-tags)
6377 (memq 'agenda org-agenda-show-inherited-tags))
6378 (and (eq org-agenda-show-inherited-tags t)
6379 (or (eq org-agenda-use-tag-inheritance t)
6380 (memq 'agenda org-agenda-use-tag-inheritance))))
6382 tags (org-get-tags nil (not inherited-tags)))
6383 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6384 (looking-at "\\*+[ \t]+\\(.*\\)")
6385 (setq head (match-string 1))
6386 (let ((remove-re
6387 (if org-agenda-remove-timeranges-from-blocks
6388 (concat
6389 "<" (regexp-quote s1) ".*?>"
6390 "--"
6391 "<" (regexp-quote s2) ".*?>")
6392 nil)))
6393 (setq txt (org-agenda-format-item
6394 (format
6395 (nth (if (= d1 d2) 0 1)
6396 org-agenda-timerange-leaders)
6397 (1+ (- d0 d1)) (1+ (- d2 d1)))
6398 head level category tags
6399 (cond ((and (= d1 d0) (= d2 d0))
6400 (concat "<" start-time ">--<" end-time ">"))
6401 ((= d1 d0)
6402 (concat "<" start-time ">"))
6403 ((= d2 d0)
6404 (concat "<" end-time ">")))
6405 remove-re))))
6406 (org-add-props txt props
6407 'org-marker marker 'org-hd-marker hdmarker
6408 'type "block" 'date date
6409 'level level
6410 'todo-state todo-state
6411 'priority (org-get-priority txt))
6412 (push txt ee))))
6413 (goto-char pos)))
6414 ;; Sort the entries by expiration date.
6415 (nreverse ee)))
6417 ;;; Agenda presentation and sorting
6419 (defvar org-prefix-has-time nil
6420 "A flag, set by `org-compile-prefix-format'.
6421 The flag is set if the currently compiled format contains a `%t'.")
6422 (defvar org-prefix-has-tag nil
6423 "A flag, set by `org-compile-prefix-format'.
6424 The flag is set if the currently compiled format contains a `%T'.")
6425 (defvar org-prefix-has-effort nil
6426 "A flag, set by `org-compile-prefix-format'.
6427 The flag is set if the currently compiled format contains a `%e'.")
6428 (defvar org-prefix-has-breadcrumbs nil
6429 "A flag, set by `org-compile-prefix-format'.
6430 The flag is set if the currently compiled format contains a `%b'.")
6431 (defvar org-prefix-category-length nil
6432 "Used by `org-compile-prefix-format' to remember the category field width.")
6433 (defvar org-prefix-category-max-length nil
6434 "Used by `org-compile-prefix-format' to remember the category field width.")
6436 (defun org-agenda-get-category-icon (category)
6437 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6438 (cl-dolist (entry org-agenda-category-icon-alist)
6439 (when (string-match-p (car entry) category)
6440 (if (listp (cadr entry))
6441 (cl-return (cadr entry))
6442 (cl-return (apply #'create-image (cdr entry)))))))
6444 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6445 remove-re habitp)
6446 "Format TXT to be inserted into the agenda buffer.
6447 In particular, add the prefix and corresponding text properties.
6449 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6450 LEVEL may be a string to replace the `%l' specifier.
6451 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6452 category taken from local variable or file name. It will replace the `%c'
6453 specifier in the format.
6454 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6455 TXT for sorting of this entry, and for the `%t' specifier in the format.
6456 When DOTIME is a string, this string is searched for a time before TXT is.
6457 TAGS can be the tags of the headline.
6458 Any match of REMOVE-RE will be removed from TXT."
6459 ;; We keep the org-prefix-* variable values along with a compiled
6460 ;; formatter, so that multiple agendas existing at the same time do
6461 ;; not step on each other toes.
6463 ;; It was inconvenient to make these variables buffer local in
6464 ;; Agenda buffers, because this function expects to be called with
6465 ;; the buffer where item comes from being current, and not agenda
6466 ;; buffer
6467 (let* ((bindings (car org-prefix-format-compiled))
6468 (formatter (cadr org-prefix-format-compiled)))
6469 (cl-loop for (var value) in bindings
6470 do (set var value))
6471 (save-match-data
6472 ;; Diary entries sometimes have extra whitespace at the beginning
6473 (setq txt (org-trim txt))
6475 ;; Fix the tags part in txt
6476 (setq txt (org-agenda-fix-displayed-tags
6477 txt tags
6478 org-agenda-show-inherited-tags
6479 org-agenda-hide-tags-regexp))
6481 (let* ((category (or category
6482 (if buffer-file-name
6483 (file-name-sans-extension
6484 (file-name-nondirectory buffer-file-name))
6485 "")))
6486 (category-icon (org-agenda-get-category-icon category))
6487 (category-icon (if category-icon
6488 (propertize " " 'display category-icon)
6489 ""))
6490 (effort (and (not (string= txt ""))
6491 (get-text-property 1 'effort txt)))
6492 ;; time, tag, effort are needed for the eval of the prefix format
6493 (tag (if tags (nth (1- (length tags)) tags) ""))
6494 (time-grid-trailing-characters (nth 2 org-agenda-time-grid))
6495 time
6496 (ts (when dotime (concat
6497 (if (stringp dotime) dotime "")
6498 (and org-agenda-search-headline-for-time txt))))
6499 (time-of-day (and dotime (org-get-time-of-day ts)))
6500 stamp plain s0 s1 s2 rtn srp l
6501 duration breadcrumbs)
6502 (and (derived-mode-p 'org-mode) buffer-file-name
6503 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6504 (when (and dotime time-of-day)
6505 ;; Extract starting and ending time and move them to prefix
6506 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6507 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6508 (setq s0 (match-string 0 ts)
6509 srp (and stamp (match-end 3))
6510 s1 (match-string (if plain 1 2) ts)
6511 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6513 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6514 ;; them, we might want to remove them there to avoid duplication.
6515 ;; The user can turn this off with a variable.
6516 (when (and org-prefix-has-time
6517 org-agenda-remove-times-when-in-prefix (or stamp plain)
6518 (string-match (concat (regexp-quote s0) " *") txt)
6519 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6520 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6521 (= (match-beginning 0) 0)
6523 (setq txt (replace-match "" nil nil txt))))
6524 ;; Normalize the time(s) to 24 hour
6525 (when s1 (setq s1 (org-get-time-of-day s1 'string t)))
6526 (when s2 (setq s2 (org-get-time-of-day s2 'string t)))
6528 ;; Try to set s2 if s1 and
6529 ;; `org-agenda-default-appointment-duration' are set
6530 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6531 (setq s2
6532 (org-duration-from-minutes
6533 (+ (org-duration-to-minutes s1 t)
6534 org-agenda-default-appointment-duration)
6535 nil t)))
6537 ;; Compute the duration
6538 (when s2
6539 (setq duration (- (org-duration-to-minutes s2)
6540 (org-duration-to-minutes s1)))))
6542 (when (string-match org-tag-group-re 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 1 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 (when org-prefix-has-breadcrumbs
6563 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6564 (let ((s (org-format-outline-path (org-get-outline-path)
6565 (1- (frame-width))
6566 nil org-agenda-breadcrumbs-separator)))
6567 (if (eq "" s) "" (concat s org-agenda-breadcrumbs-separator))))))
6568 (setq time (cond (s2 (concat
6569 (org-agenda-time-of-day-to-ampm-maybe s1)
6570 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6571 (when org-agenda-timegrid-use-ampm " ")))
6572 (s1 (concat
6573 (org-agenda-time-of-day-to-ampm-maybe s1)
6574 (if org-agenda-timegrid-use-ampm
6575 (concat time-grid-trailing-characters " ")
6576 time-grid-trailing-characters)))
6577 (t ""))
6578 extra (or (and (not habitp) extra) "")
6579 category (if (symbolp category) (symbol-name category) category)
6580 level (or level ""))
6581 (if (string-match org-link-bracket-re category)
6582 (progn
6583 (setq l (string-width (or (match-string 2) (match-string 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 (when (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 (when (string-match org-tag-group-re 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 3 org-agenda-time-grid))
6668 (gridtimes (nth 1 org-agenda-time-grid))
6669 (req (car org-agenda-time-grid))
6670 (remove (member 'remove-match req))
6671 new time)
6672 (when (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 (cl-case var
6723 (time (setq org-prefix-has-time t))
6724 (tag (setq org-prefix-has-tag t))
6725 (effort (setq org-prefix-has-effort t))
6726 (breadcrumbs (setq org-prefix-has-breadcrumbs t)))
6727 (setq f (concat "%" (match-string 2 s) "s"))
6728 (when (eq var 'category)
6729 (setq org-prefix-category-length
6730 (floor (abs (string-to-number (match-string 2 s)))))
6731 (setq org-prefix-category-max-length
6732 (let ((x (match-string 2 s)))
6733 (when (string-match-p "\\.[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 (when (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 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
6852 (setq list (mapcar #'org-agenda--mark-blocked-entry list)))
6853 (mapconcat 'identity list "\n")))
6855 (defun org-agenda-limit-entries (list prop limit &optional fn)
6856 "Limit the number of agenda entries."
6857 (let ((include (and limit (< limit 0))))
6858 (if limit
6859 (let ((fun (or fn (lambda (p) (when p 1))))
6860 (lim 0))
6861 (delq nil
6862 (mapcar
6863 (lambda (e)
6864 (let ((pval (funcall
6865 fun (get-text-property (1- (length e))
6866 prop e))))
6867 (when pval (setq lim (+ lim pval)))
6868 (cond ((and pval (<= lim (abs limit))) e)
6869 ((and include (not pval)) e))))
6870 list)))
6871 list)))
6873 (defun org-agenda-limit-interactively (remove)
6874 "In agenda, interactively limit entries to various maximums."
6875 (interactive "P")
6876 (if remove
6877 (progn (setq org-agenda-max-entries nil
6878 org-agenda-max-todos nil
6879 org-agenda-max-tags nil
6880 org-agenda-max-effort nil)
6881 (org-agenda-redo))
6882 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6883 (msg (cond ((= max ?E) "How many minutes? ")
6884 ((= max ?e) "How many entries? ")
6885 ((= max ?t) "How many TODO entries? ")
6886 ((= max ?T) "How many tagged entries? ")
6887 (t (user-error "Wrong input"))))
6888 (num (string-to-number (read-from-minibuffer msg))))
6889 (cond ((equal max ?e)
6890 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6891 ((equal max ?t)
6892 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6893 ((equal max ?T)
6894 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6895 ((equal max ?E)
6896 (let ((org-agenda-max-effort num)) (org-agenda-redo))))))
6897 (org-agenda-fit-window-to-buffer))
6899 (defun org-agenda-highlight-todo (x)
6900 (let ((org-done-keywords org-done-keywords-for-agenda)
6901 (case-fold-search nil)
6903 (if (eq x 'line)
6904 (save-excursion
6905 (beginning-of-line 1)
6906 (setq re (org-get-at-bol 'org-todo-regexp))
6907 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6908 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6909 (add-text-properties (match-beginning 0) (match-end 1)
6910 (list 'face (org-get-todo-face 1)))
6911 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6912 (delete-region (match-beginning 1) (1- (match-end 0)))
6913 (goto-char (match-beginning 1))
6914 (insert (format org-agenda-todo-keyword-format s)))))
6915 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6916 (setq re (get-text-property 0 'org-todo-regexp x))
6917 (when (and re
6918 ;; Test `pl' because if there's no heading content,
6919 ;; there's no point matching to highlight. Note
6920 ;; that if we didn't test `pl' first, and there
6921 ;; happened to be no keyword from `org-todo-regexp'
6922 ;; on this heading line, then the `equal' comparison
6923 ;; afterwards would spuriously succeed in the case
6924 ;; where `pl' is nil -- causing an args-out-of-range
6925 ;; error when we try to add text properties to text
6926 ;; that isn't there.
6928 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6929 x pl) pl))
6930 (add-text-properties
6931 (or (match-end 1) (match-end 0)) (match-end 0)
6932 (list 'face (org-get-todo-face (match-string 2 x)))
6934 (when (match-end 1)
6935 (setq x
6936 (concat
6937 (substring x 0 (match-end 1))
6938 (format org-agenda-todo-keyword-format
6939 (match-string 2 x))
6940 ;; Remove `display' property as the icon could leak
6941 ;; on the white space.
6942 (org-add-props " " (org-plist-delete (text-properties-at 0 x)
6943 'display))
6944 (substring x (match-end 3)))))))
6945 x)))
6947 (defsubst org-cmp-values (a b property)
6948 "Compare the numeric value of text PROPERTY for string A and B."
6949 (let ((pa (or (get-text-property (1- (length a)) property a) 0))
6950 (pb (or (get-text-property (1- (length b)) property b) 0)))
6951 (cond ((> pa pb) +1)
6952 ((< pa pb) -1))))
6954 (defsubst org-cmp-effort (a b)
6955 "Compare the effort values of string A and B."
6956 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6957 ;; `effort-minutes' property is not directly accessible from
6958 ;; the strings, but is stored as a property in `txt'.
6959 (ea (or (get-text-property
6960 0 'effort-minutes (get-text-property 0 'txt a))
6961 def))
6962 (eb (or (get-text-property
6963 0 'effort-minutes (get-text-property 0 'txt b))
6964 def)))
6965 (cond ((> ea eb) +1)
6966 ((< ea eb) -1))))
6968 (defsubst org-cmp-category (a b)
6969 "Compare the string values of categories of strings A and B."
6970 (let ((ca (or (get-text-property (1- (length a)) 'org-category a) ""))
6971 (cb (or (get-text-property (1- (length b)) 'org-category b) "")))
6972 (cond ((string-lessp ca cb) -1)
6973 ((string-lessp cb ca) +1))))
6975 (defsubst org-cmp-todo-state (a b)
6976 "Compare the todo states of strings A and B."
6977 (let* ((ma (or (get-text-property 1 'org-marker a)
6978 (get-text-property 1 'org-hd-marker a)))
6979 (mb (or (get-text-property 1 'org-marker b)
6980 (get-text-property 1 'org-hd-marker b)))
6981 (fa (and ma (marker-buffer ma)))
6982 (fb (and mb (marker-buffer mb)))
6983 (todo-kwds
6984 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6985 (and fb (with-current-buffer fb org-todo-keywords-1))))
6986 (ta (or (get-text-property 1 'todo-state a) ""))
6987 (tb (or (get-text-property 1 'todo-state b) ""))
6988 (la (- (length (member ta todo-kwds))))
6989 (lb (- (length (member tb todo-kwds))))
6990 (donepa (member ta org-done-keywords-for-agenda))
6991 (donepb (member tb org-done-keywords-for-agenda)))
6992 (cond ((and donepa (not donepb)) -1)
6993 ((and (not donepa) donepb) +1)
6994 ((< la lb) -1)
6995 ((< lb la) +1))))
6997 (defsubst org-cmp-alpha (a b)
6998 "Compare the headlines, alphabetically."
6999 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
7000 (plb (text-property-any 0 (length b) 'org-heading t b))
7001 (ta (and pla (substring a pla)))
7002 (tb (and plb (substring b plb)))
7003 (case-fold-search nil))
7004 (when pla
7005 (when (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
7006 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
7007 (setq ta (substring ta (match-end 0))))
7008 (setq ta (downcase ta)))
7009 (when plb
7010 (when (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
7011 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
7012 (setq tb (substring tb (match-end 0))))
7013 (setq tb (downcase tb)))
7014 (cond ((not (or ta tb)) nil)
7015 ((not ta) +1)
7016 ((not tb) -1)
7017 ((string-lessp ta tb) -1)
7018 ((string-lessp tb ta) +1))))
7020 (defsubst org-cmp-tag (a b)
7021 "Compare the string values of the first tags of A and B."
7022 (let ((ta (car (last (get-text-property 1 'tags a))))
7023 (tb (car (last (get-text-property 1 'tags b)))))
7024 (cond ((not (or ta tb)) nil)
7025 ((not ta) +1)
7026 ((not tb) -1)
7027 ((string-lessp ta tb) -1)
7028 ((string-lessp tb ta) +1))))
7030 (defsubst org-cmp-time (a b)
7031 "Compare the time-of-day values of strings A and B."
7032 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
7033 (ta (or (get-text-property 1 'time-of-day a) def))
7034 (tb (or (get-text-property 1 'time-of-day b) def)))
7035 (cond ((< ta tb) -1)
7036 ((< tb ta) +1))))
7038 (defsubst org-cmp-ts (a b type)
7039 "Compare the timestamps values of entries A and B.
7040 When TYPE is \"scheduled\", \"deadline\", \"timestamp\" or
7041 \"timestamp_ia\", compare within each of these type. When TYPE
7042 is the empty string, compare all timestamps without respect of
7043 their type."
7044 (let* ((def (and (not org-sort-agenda-notime-is-late) -1))
7045 (ta (or (and (string-match type (or (get-text-property 1 'type a) ""))
7046 (get-text-property 1 'ts-date a))
7047 def))
7048 (tb (or (and (string-match type (or (get-text-property 1 'type b) ""))
7049 (get-text-property 1 'ts-date b))
7050 def)))
7051 (cond ((if ta (and tb (< ta tb)) tb) -1)
7052 ((if tb (and ta (< tb ta)) ta) +1))))
7054 (defsubst org-cmp-habit-p (a b)
7055 "Compare the todo states of strings A and B."
7056 (let ((ha (get-text-property 1 'org-habit-p a))
7057 (hb (get-text-property 1 'org-habit-p b)))
7058 (cond ((and ha (not hb)) -1)
7059 ((and (not ha) hb) +1))))
7061 (defun org-entries-lessp (a b)
7062 "Predicate for sorting agenda entries."
7063 ;; The following variables will be used when the form is evaluated.
7064 ;; So even though the compiler complains, keep them.
7065 (let* ((ss org-agenda-sorting-strategy-selected)
7066 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
7067 (org-cmp-ts a b "")))
7068 (timestamp-down (if timestamp-up (- timestamp-up) nil))
7069 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
7070 (org-cmp-ts a b "scheduled")))
7071 (scheduled-down (if scheduled-up (- scheduled-up) nil))
7072 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
7073 (org-cmp-ts a b "deadline")))
7074 (deadline-down (if deadline-up (- deadline-up) nil))
7075 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
7076 (org-cmp-ts a b "timestamp_ia")))
7077 (tsia-down (if tsia-up (- tsia-up) nil))
7078 (ts-up (and (org-em 'ts-up 'ts-down ss)
7079 (org-cmp-ts a b "timestamp")))
7080 (ts-down (if ts-up (- ts-up) nil))
7081 (time-up (and (org-em 'time-up 'time-down ss)
7082 (org-cmp-time a b)))
7083 (time-down (if time-up (- time-up) nil))
7084 (stats-up (and (org-em 'stats-up 'stats-down ss)
7085 (org-cmp-values a b 'org-stats)))
7086 (stats-down (if stats-up (- stats-up) nil))
7087 (priority-up (and (org-em 'priority-up 'priority-down ss)
7088 (org-cmp-values a b 'priority)))
7089 (priority-down (if priority-up (- priority-up) nil))
7090 (effort-up (and (org-em 'effort-up 'effort-down ss)
7091 (org-cmp-effort a b)))
7092 (effort-down (if effort-up (- effort-up) nil))
7093 (category-up (and (or (org-em 'category-up 'category-down ss)
7094 (memq 'category-keep ss))
7095 (org-cmp-category a b)))
7096 (category-down (if category-up (- category-up) nil))
7097 (category-keep (if category-up +1 nil))
7098 (tag-up (and (org-em 'tag-up 'tag-down ss)
7099 (org-cmp-tag a b)))
7100 (tag-down (if tag-up (- tag-up) nil))
7101 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
7102 (org-cmp-todo-state a b)))
7103 (todo-state-down (if todo-state-up (- todo-state-up) nil))
7104 (habit-up (and (org-em 'habit-up 'habit-down ss)
7105 (org-cmp-habit-p a b)))
7106 (habit-down (if habit-up (- habit-up) nil))
7107 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
7108 (org-cmp-alpha a b)))
7109 (alpha-down (if alpha-up (- alpha-up) nil))
7110 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
7111 user-defined-up user-defined-down)
7112 (when (and need-user-cmp org-agenda-cmp-user-defined
7113 (functionp org-agenda-cmp-user-defined))
7114 (setq user-defined-up
7115 (funcall org-agenda-cmp-user-defined a b)
7116 user-defined-down (if user-defined-up (- user-defined-up) nil)))
7117 (cdr (assoc
7118 (eval (cons 'or org-agenda-sorting-strategy-selected))
7119 '((-1 . t) (1 . nil) (nil . nil))))))
7121 ;;; Agenda restriction lock
7123 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
7124 "Overlay to mark the headline to which agenda commands are restricted.")
7125 (overlay-put org-agenda-restriction-lock-overlay
7126 'face 'org-agenda-restriction-lock)
7127 (overlay-put org-agenda-restriction-lock-overlay
7128 'help-echo "Agendas are currently limited to this subtree.")
7129 (delete-overlay org-agenda-restriction-lock-overlay)
7131 (defun org-agenda-set-restriction-lock-from-agenda (arg)
7132 "Set the restriction lock to the agenda item at point from within the agenda.
7133 When called with a `\\[universal-argument]' prefix, restrict to
7134 the file which contains the item.
7135 Argument ARG is the prefix argument."
7136 (interactive "P")
7137 (unless (derived-mode-p 'org-agenda-mode)
7138 (user-error "Not in an Org agenda buffer"))
7139 (let* ((marker (or (org-get-at-bol 'org-marker)
7140 (org-agenda-error)))
7141 (buffer (marker-buffer marker))
7142 (pos (marker-position marker)))
7143 (with-current-buffer buffer
7144 (goto-char pos)
7145 (org-agenda-set-restriction-lock arg))))
7147 ;;;###autoload
7148 (defun org-agenda-set-restriction-lock (&optional type)
7149 "Set restriction lock for agenda to current subtree or file.
7150 When in a restricted subtree, remove it.
7152 The restriction will span over the entire file if TYPE is `file',
7153 or if type is '(4), or if the cursor is before the first headline
7154 in the file. Otherwise, only apply the restriction to the current
7155 subtree."
7156 (interactive "P")
7157 (if (and org-agenda-overriding-restriction
7158 (member org-agenda-restriction-lock-overlay
7159 (overlays-at (point)))
7160 (equal (overlay-start org-agenda-restriction-lock-overlay)
7161 (point)))
7162 (org-agenda-remove-restriction-lock 'noupdate)
7163 (org-agenda-remove-restriction-lock 'noupdate)
7164 (and (equal type '(4)) (setq type 'file))
7165 (setq type (cond
7166 (type type)
7167 ((org-at-heading-p) 'subtree)
7168 ((condition-case nil (org-back-to-heading t) (error nil))
7169 'subtree)
7170 (t 'file)))
7171 (if (eq type 'subtree)
7172 (progn
7173 (setq org-agenda-restrict (current-buffer))
7174 (setq org-agenda-overriding-restriction 'subtree)
7175 (put 'org-agenda-files 'org-restrict
7176 (list (buffer-file-name (buffer-base-buffer))))
7177 (org-back-to-heading t)
7178 (move-overlay org-agenda-restriction-lock-overlay
7179 (point)
7180 (if org-agenda-restriction-lock-highlight-subtree
7181 (save-excursion (org-end-of-subtree t t) (point))
7182 (point-at-eol)))
7183 (move-marker org-agenda-restrict-begin (point))
7184 (move-marker org-agenda-restrict-end
7185 (save-excursion (org-end-of-subtree t t)))
7186 (message "Locking agenda restriction to subtree"))
7187 (put 'org-agenda-files 'org-restrict
7188 (list (buffer-file-name (buffer-base-buffer))))
7189 (setq org-agenda-restrict nil)
7190 (setq org-agenda-overriding-restriction 'file)
7191 (move-marker org-agenda-restrict-begin nil)
7192 (move-marker org-agenda-restrict-end nil)
7193 (message "Locking agenda restriction to file"))
7194 (setq current-prefix-arg nil))
7195 (org-agenda-maybe-redo))
7197 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7198 "Remove agenda restriction lock."
7199 (interactive "P")
7200 (if (not org-agenda-restrict)
7201 (message "No agenda restriction to remove.")
7202 (delete-overlay org-agenda-restriction-lock-overlay)
7203 (delete-overlay org-speedbar-restriction-lock-overlay)
7204 (setq org-agenda-overriding-restriction nil)
7205 (setq org-agenda-restrict nil)
7206 (put 'org-agenda-files 'org-restrict nil)
7207 (move-marker org-agenda-restrict-begin nil)
7208 (move-marker org-agenda-restrict-end nil)
7209 (setq current-prefix-arg nil)
7210 (message "Agenda restriction lock removed")
7211 (or noupdate (org-agenda-maybe-redo))))
7213 (defun org-agenda-maybe-redo ()
7214 "If there is any window showing the agenda view, update it."
7215 (let ((w (get-buffer-window (or org-agenda-this-buffer-name
7216 org-agenda-buffer-name)
7218 (w0 (selected-window)))
7219 (when w
7220 (select-window w)
7221 (org-agenda-redo)
7222 (select-window w0)
7223 (if org-agenda-overriding-restriction
7224 (message "Agenda view shifted to new %s restriction"
7225 org-agenda-overriding-restriction)
7226 (message "Agenda restriction lock removed")))))
7228 ;;; Agenda commands
7230 (defun org-agenda-check-type (error &rest types)
7231 "Check if agenda buffer is of allowed type.
7232 If ERROR is non-nil, throw an error, otherwise just return nil.
7233 Allowed types are `agenda' `todo' `tags' `search'."
7234 (cond ((not org-agenda-type)
7235 (error "No Org agenda currently displayed"))
7236 ((memq org-agenda-type types) t)
7237 (error
7238 (error "Not allowed in %s-type agenda buffers" org-agenda-type))
7239 (t nil)))
7241 (defun org-agenda-Quit ()
7242 "Exit the agenda, killing the agenda buffer.
7243 Like `org-agenda-quit', but kill the buffer even when
7244 `org-agenda-sticky' is non-nil."
7245 (interactive)
7246 (org-agenda--quit))
7248 (defun org-agenda-quit ()
7249 "Exit the agenda.
7251 When `org-agenda-sticky' is non-nil, bury the agenda buffer
7252 instead of killing it.
7254 When `org-agenda-restore-windows-after-quit' is non-nil, restore
7255 the pre-agenda window configuration.
7257 When column view is active, exit column view instead of the
7258 agenda."
7259 (interactive)
7260 (org-agenda--quit org-agenda-sticky))
7262 (defun org-agenda--quit (&optional bury)
7263 (if org-agenda-columns-active
7264 (org-columns-quit)
7265 (let ((wconf org-agenda-pre-window-conf)
7266 (buf (current-buffer))
7267 (org-agenda-last-indirect-window
7268 (and (eq org-indirect-buffer-display 'other-window)
7269 org-agenda-last-indirect-buffer
7270 (get-buffer-window org-agenda-last-indirect-buffer))))
7271 (cond
7272 ((eq org-agenda-window-setup 'other-frame)
7273 (delete-frame))
7274 ((and org-agenda-restore-windows-after-quit
7275 wconf)
7276 ;; Maybe restore the pre-agenda window configuration. Reset
7277 ;; `org-agenda-pre-window-conf' before running
7278 ;; `set-window-configuration', which loses the current buffer.
7279 (setq org-agenda-pre-window-conf nil)
7280 (set-window-configuration wconf))
7282 (when org-agenda-last-indirect-window
7283 (delete-window org-agenda-last-indirect-window))
7284 (and (not (eq org-agenda-window-setup 'current-window))
7285 (not (one-window-p))
7286 (delete-window))))
7287 (if bury
7288 ;; Set the agenda buffer as the current buffer instead of
7289 ;; passing it as an argument to `bury-buffer' so that
7290 ;; `bury-buffer' removes it from the window.
7291 (with-current-buffer buf
7292 (bury-buffer))
7293 (kill-buffer buf)
7294 (setq org-agenda-archives-mode nil
7295 org-agenda-buffer nil)))))
7297 (defun org-agenda-exit ()
7298 "Exit the agenda, killing Org buffers loaded by the agenda.
7299 Like `org-agenda-Quit', but kill any buffers that were created by
7300 the agenda. Org buffers visited directly by the user will not be
7301 touched. Also, exit the agenda even if it is in column view."
7302 (interactive)
7303 (when org-agenda-columns-active
7304 (org-columns-quit))
7305 (org-release-buffers org-agenda-new-buffers)
7306 (setq org-agenda-new-buffers nil)
7307 (org-agenda-Quit))
7309 (defun org-agenda-kill-all-agenda-buffers ()
7310 "Kill all buffers in `org-agenda-mode'.
7311 This is used when toggling sticky agendas."
7312 (interactive)
7313 (let (blist)
7314 (dolist (buf (buffer-list))
7315 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7316 (push buf blist)))
7317 (mapc 'kill-buffer blist)))
7319 (defun org-agenda-execute (arg)
7320 "Execute another agenda command, keeping same window.
7321 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7322 in the agenda."
7323 (interactive "P")
7324 (let ((org-agenda-window-setup 'current-window))
7325 (org-agenda arg)))
7327 (defun org-agenda-redo (&optional all)
7328 "Rebuild possibly ALL agenda view(s) in the current buffer."
7329 (interactive "P")
7330 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7331 (cpa (unless (eq all t) current-prefix-arg))
7332 (org-agenda-doing-sticky-redo org-agenda-sticky)
7333 (org-agenda-sticky nil)
7334 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7335 org-agenda-buffer-name))
7336 (org-agenda-keep-modes t)
7337 (tag-filter org-agenda-tag-filter)
7338 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7339 (top-hl-filter org-agenda-top-headline-filter)
7340 (cat-filter org-agenda-category-filter)
7341 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7342 (re-filter org-agenda-regexp-filter)
7343 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7344 (effort-filter org-agenda-effort-filter)
7345 (effort-preset (get 'org-agenda-effort-filter :preset-filter))
7346 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7347 (cols org-agenda-columns-active)
7348 (line (org-current-line))
7349 (window-line (- line (org-current-line (window-start))))
7350 (lprops (get 'org-agenda-redo-command 'org-lprops))
7351 (redo-cmd (get-text-property p 'org-redo-cmd))
7352 (last-args (get-text-property p 'org-last-args))
7353 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7354 (org-agenda-overriding-cmd-arguments
7355 (unless (eq all t)
7356 (cond ((listp last-args)
7357 (cons (or cpa (car last-args)) (cdr last-args)))
7358 ((stringp last-args)
7359 last-args))))
7360 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7361 (put 'org-agenda-tag-filter :preset-filter nil)
7362 (put 'org-agenda-category-filter :preset-filter nil)
7363 (put 'org-agenda-regexp-filter :preset-filter nil)
7364 (put 'org-agenda-effort-filter :preset-filter nil)
7365 (and cols (org-columns-quit))
7366 (message "Rebuilding agenda buffer...")
7367 (if series-redo-cmd
7368 (eval series-redo-cmd)
7369 (org-let lprops redo-cmd))
7370 (setq org-agenda-undo-list nil
7371 org-agenda-pending-undo-list nil
7372 org-agenda-tag-filter tag-filter
7373 org-agenda-category-filter cat-filter
7374 org-agenda-regexp-filter re-filter
7375 org-agenda-effort-filter effort-filter
7376 org-agenda-top-headline-filter top-hl-filter)
7377 (message "Rebuilding agenda buffer...done")
7378 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7379 (put 'org-agenda-category-filter :preset-filter cat-preset)
7380 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7381 (put 'org-agenda-effort-filter :preset-filter effort-preset)
7382 (let ((tag (or tag-filter tag-preset))
7383 (cat (or cat-filter cat-preset))
7384 (effort (or effort-filter effort-preset))
7385 (re (or re-filter re-preset)))
7386 (when tag (org-agenda-filter-apply tag 'tag t))
7387 (when cat (org-agenda-filter-apply cat 'category))
7388 (when effort (org-agenda-filter-apply effort 'effort))
7389 (when re (org-agenda-filter-apply re 'regexp)))
7390 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7391 (and cols (called-interactively-p 'any) (org-agenda-columns))
7392 (org-goto-line line)
7393 (recenter window-line)))
7395 (defun org-agenda-redo-all (&optional exhaustive)
7396 "Rebuild all agenda views in the current buffer.
7397 With a prefix argument, do so in all agenda buffers."
7398 (interactive "P")
7399 (if exhaustive
7400 (dolist (buffer (buffer-list))
7401 (with-current-buffer buffer
7402 (when (derived-mode-p 'org-agenda-mode)
7403 (org-agenda-redo t))))
7404 (org-agenda-redo t)))
7406 (defvar org-global-tags-completion-table nil)
7407 (defvar org-agenda-filter-form nil)
7408 (defvar org-agenda-filtered-by-category nil)
7410 (defsubst org-agenda-get-category ()
7411 "Return the category of the agenda line."
7412 (org-get-at-bol 'org-category))
7414 (defun org-agenda-filter-by-category (strip)
7415 "Filter lines in the agenda buffer that have a specific category.
7416 The category is that of the current line.
7417 Without prefix argument, keep only the lines of that category.
7418 With a prefix argument, exclude the lines of that category.
7420 (interactive "P")
7421 (if (and org-agenda-filtered-by-category
7422 org-agenda-category-filter)
7423 (org-agenda-filter-show-all-cat)
7424 (let ((cat (org-no-properties (org-agenda-get-category))))
7425 (cond
7426 ((and cat strip)
7427 (org-agenda-filter-apply
7428 (push (concat "-" cat) org-agenda-category-filter) 'category))
7429 (cat
7430 (org-agenda-filter-apply
7431 (setq org-agenda-category-filter
7432 (list (concat "+" cat))) 'category))
7433 (t (error "No category at point"))))))
7435 (defun org-find-top-headline (&optional pos)
7436 "Find the topmost parent headline and return it.
7437 POS when non-nil is the marker or buffer position to start the
7438 search from."
7439 (save-excursion
7440 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
7441 (when pos (goto-char pos))
7442 ;; Skip up to the topmost parent.
7443 (while (org-up-heading-safe))
7444 (ignore-errors (nth 4 (org-heading-components))))))
7446 (defvar org-agenda-filtered-by-top-headline nil)
7447 (defun org-agenda-filter-by-top-headline (strip)
7448 "Keep only those lines that are descendants from the same top headline.
7449 The top headline is that of the current line."
7450 (interactive "P")
7451 (if org-agenda-filtered-by-top-headline
7452 (progn
7453 (setq org-agenda-filtered-by-top-headline nil
7454 org-agenda-top-headline-filter nil)
7455 (org-agenda-filter-show-all-top-filter))
7456 (let ((toph (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7457 (if toph (org-agenda-filter-top-headline-apply toph strip)
7458 (error "No top-level headline at point")))))
7460 (defvar org-agenda-regexp-filter nil)
7461 (defun org-agenda-filter-by-regexp (strip)
7462 "Filter agenda entries by regular expressions.
7464 With one prefix argument, filter out entries matching the regexp.
7465 If there is already a regexp filter, remove it unless called with
7466 two prefix arguments."
7467 (interactive "P")
7468 (cond
7469 ((and org-agenda-regexp-filter (not (equal strip '(16))))
7470 (org-agenda-filter-show-all-re)
7471 (message "Regexp filter removed"))
7472 (t (let ((flt (concat (if (equal strip '(4)) "-" "+")
7473 (read-from-minibuffer
7474 (if (equal strip '(4))
7475 "Filter out entries matching regexp: "
7476 "Narrow to entries matching regexp: ")))))
7477 (push flt org-agenda-regexp-filter)
7478 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp)))))
7480 (defvar org-agenda-effort-filter nil)
7481 (defun org-agenda-filter-by-effort (strip)
7482 "Filter agenda entries by effort.
7483 With no prefix argument, keep entries matching the effort condition.
7484 With one prefix argument, filter out entries matching the condition.
7485 With two prefix arguments, remove the effort filters."
7486 (interactive "P")
7487 (cond
7488 ((member strip '(nil 4))
7489 (let* ((efforts (split-string
7490 (or (cdr (assoc (concat org-effort-property "_ALL")
7491 org-global-properties))
7492 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00")))
7493 ;; XXX: the following handles only up to 10 different
7494 ;; effort values.
7495 (allowed-keys (if (null efforts) nil
7496 (mapcar (lambda (n) (mod n 10)) ;turn 10 into 0
7497 (number-sequence 1 (length efforts)))))
7498 (op nil))
7499 (while (not (memq op '(?< ?> ?=)))
7500 (setq op (read-char-exclusive "Effort operator? (> = or <)")))
7501 ;; Select appropriate duration. Ignore non-digit characters.
7502 (let ((prompt
7503 (apply #'format
7504 (concat "Effort %c "
7505 (mapconcat (lambda (s) (concat "[%d]" s))
7506 efforts
7507 " "))
7508 op allowed-keys))
7509 (eff -1))
7510 (while (not (memq eff allowed-keys))
7511 (message prompt)
7512 (setq eff (- (read-char-exclusive) 48)))
7513 (setq org-agenda-effort-filter
7514 (list (concat (if strip "-" "+")
7515 (char-to-string op)
7516 ;; Numbering is 1 2 3 ... 9 0, but we want
7517 ;; 0 1 2 ... 8 9.
7518 (nth (mod (1- eff) 10) efforts)))))
7519 (org-agenda-filter-apply org-agenda-effort-filter 'effort)))
7520 (t (org-agenda-filter-show-all-effort)
7521 (message "Effort filter removed"))))
7523 (defun org-agenda-filter-remove-all ()
7524 "Remove all filters from the current agenda buffer."
7525 (interactive)
7526 (when org-agenda-tag-filter
7527 (org-agenda-filter-show-all-tag))
7528 (when org-agenda-category-filter
7529 (org-agenda-filter-show-all-cat))
7530 (when org-agenda-regexp-filter
7531 (org-agenda-filter-show-all-re))
7532 (when org-agenda-top-headline-filter
7533 (org-agenda-filter-show-all-top-filter))
7534 (when org-agenda-effort-filter
7535 (org-agenda-filter-show-all-effort))
7536 (org-agenda-finalize))
7538 (defun org-agenda-filter-by-tag (arg &optional char exclude)
7539 "Keep only those lines in the agenda buffer that have a specific tag.
7541 The tag is selected with its fast selection letter, as configured.
7543 With a `\\[universal-argument]' prefix, exclude the agenda search.
7545 With a `\\[universal-argument] \\[universal-argument]' prefix, filter the literal tag, \
7546 i.e. don't
7547 filter on all its group members.
7549 A lisp caller can specify CHAR. EXCLUDE means that the new tag
7550 should be used to exclude the search - the interactive user can
7551 also press `-' or `+' to switch between filtering and excluding."
7552 (interactive "P")
7553 (let* ((alist org-tag-alist-for-agenda)
7554 (tag-chars (mapconcat
7555 (lambda (x) (if (and (not (symbolp (car x)))
7556 (cdr x))
7557 (char-to-string (cdr x))
7558 ""))
7559 org-tag-alist-for-agenda ""))
7560 (valid-char-list (append '(?\t ?\r ?/ ?. ?\s ?q)
7561 (string-to-list tag-chars)))
7562 (exclude (or exclude (equal arg '(4))))
7563 (expand (not (equal arg '(16))))
7564 (inhibit-read-only t)
7565 (current org-agenda-tag-filter)
7566 a n tag)
7567 (unless char
7568 (while (not (memq char valid-char-list))
7569 (org-unlogged-message
7570 "%s by tag [%s ]:tag-char, [TAB]:tag, %s[/]:off, [+/-]:filter/exclude%s, [q]:quit"
7571 (if exclude "Exclude" "Filter")
7572 tag-chars
7573 (if org-agenda-auto-exclude-function "[RET], " "")
7574 (if expand "" ", no grouptag expand"))
7575 (setq char (read-char-exclusive))
7576 ;; Excluding or filtering down
7577 (cond ((eq char ?-) (setq exclude t))
7578 ((eq char ?+) (setq exclude nil)))))
7579 (when (eq char ?\t)
7580 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7581 (setq-local org-global-tags-completion-table
7582 (org-global-tags-completion-table)))
7583 (let ((completion-ignore-case t))
7584 (setq tag (completing-read
7585 "Tag: " org-global-tags-completion-table nil t))))
7586 (cond
7587 ((eq char ?\r)
7588 (org-agenda-filter-show-all-tag)
7589 (when org-agenda-auto-exclude-function
7590 (setq org-agenda-tag-filter nil)
7591 (dolist (tag (org-agenda-get-represented-tags))
7592 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7593 (when modifier
7594 (push modifier org-agenda-tag-filter))))
7595 (unless (null org-agenda-tag-filter)
7596 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))))
7597 ((eq char ?/)
7598 (org-agenda-filter-show-all-tag)
7599 (when (get 'org-agenda-tag-filter :preset-filter)
7600 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand)))
7601 ((eq char ?.)
7602 (setq org-agenda-tag-filter
7603 (mapcar (lambda(tag) (concat "+" tag))
7604 (org-get-at-bol 'tags)))
7605 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7606 ((eq char ?q)) ;If q, abort (even if there is a q-key for a tag...)
7607 ((or (eq char ?\s)
7608 (setq a (rassoc char alist))
7609 (and tag (setq a (cons tag nil))))
7610 (org-agenda-filter-show-all-tag)
7611 (setq tag (car a))
7612 (setq org-agenda-tag-filter
7613 (cons (concat (if exclude "-" "+") tag)
7614 current))
7615 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7616 (t (error "Invalid tag selection character %c" char)))))
7618 (defun org-agenda-get-represented-tags ()
7619 "Get a list of all tags currently represented in the agenda."
7620 (let (p tags)
7621 (save-excursion
7622 (goto-char (point-min))
7623 (while (setq p (next-single-property-change (point) 'tags))
7624 (goto-char p)
7625 (mapc (lambda (x) (add-to-list 'tags x))
7626 (get-text-property (point) 'tags))))
7627 tags))
7630 (defun org-agenda-filter-make-matcher (filter type &optional expand)
7631 "Create the form that tests a line for agenda filter. Optional
7632 argument EXPAND can be used for the TYPE tag and will expand the
7633 tags in the FILTER if any of the tags in FILTER are grouptags."
7634 (let (f f1)
7635 (cond
7636 ;; Tag filter
7637 ((eq type 'tag)
7638 (setq filter
7639 (delete-dups
7640 (append (get 'org-agenda-tag-filter :preset-filter)
7641 filter)))
7642 (dolist (x filter)
7643 (let ((op (string-to-char x)))
7644 (if expand (setq x (org-agenda-filter-expand-tags (list x) t))
7645 (setq x (list x)))
7646 (setq f1 (org-agenda-filter-make-matcher-tag-exp x op))
7647 (push f1 f))))
7648 ;; Category filter
7649 ((eq type 'category)
7650 (setq filter
7651 (delete-dups
7652 (append (get 'org-agenda-category-filter :preset-filter)
7653 filter)))
7654 (dolist (x filter)
7655 (if (equal "-" (substring x 0 1))
7656 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7657 (setq f1 (list 'equal (substring x 1) 'cat)))
7658 (push f1 f)))
7659 ;; Regexp filter
7660 ((eq type 'regexp)
7661 (setq filter
7662 (delete-dups
7663 (append (get 'org-agenda-regexp-filter :preset-filter)
7664 filter)))
7665 (dolist (x filter)
7666 (if (equal "-" (substring x 0 1))
7667 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7668 (setq f1 (list 'string-match (substring x 1) 'txt)))
7669 (push f1 f)))
7670 ;; Effort filter
7671 ((eq type 'effort)
7672 (setq filter
7673 (delete-dups
7674 (append (get 'org-agenda-effort-filter :preset-filter)
7675 filter)))
7676 (dolist (x filter)
7677 (push (org-agenda-filter-effort-form x) f))))
7678 (cons 'and (nreverse f))))
7680 (defun org-agenda-filter-make-matcher-tag-exp (tags op)
7681 "Return a form associated to tag-expression TAGS.
7682 Build a form testing a line for agenda filter for
7683 tag-expressions. OP is an operator of type CHAR that allows the
7684 function to set the right switches in the returned form."
7685 (let (form)
7686 ;; Any of the expressions can match if OP is +, all must match if
7687 ;; the operator is -.
7688 (dolist (x tags (cons (if (eq op ?-) 'and 'or) form))
7689 (let* ((tag (substring x 1))
7690 (f (cond
7691 ((string= "" tag) '(not tags))
7692 ((and (string-match-p "\\`{" tag) (string-match-p "}\\'" tag))
7693 ;; TAG is a regexp.
7694 (list 'org-match-any-p (substring tag 1 -1) 'tags))
7695 (t (list 'member (downcase tag) 'tags)))))
7696 (push (if (eq op ?-) (list 'not f) f) form)))))
7698 (defun org-agenda-filter-effort-form (e)
7699 "Return the form to compare the effort of the current line with what E says.
7700 E looks like \"+<2:25\"."
7701 (let (op)
7702 (setq e (substring e 1))
7703 (setq op (string-to-char e) e (substring e 1))
7704 (setq op (cond ((equal op ?<) '<=)
7705 ((equal op ?>) '>=)
7706 ((equal op ??) op)
7707 (t '=)))
7708 (list 'org-agenda-compare-effort (list 'quote op)
7709 (org-duration-to-minutes e))))
7711 (defun org-agenda-compare-effort (op value)
7712 "Compare the effort of the current line with VALUE, using OP.
7713 If the line does not have an effort defined, return nil."
7714 ;; `effort-minutes' property cannot be extracted directly from
7715 ;; current line but is stored as a property in `txt'.
7716 (let ((effort (get-text-property 0 'effort-minutes (org-get-at-bol 'txt))))
7717 (funcall op
7718 (or effort (if org-sort-agenda-noeffort-is-high 32767 -1))
7719 value)))
7721 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7722 "Expand group tags in FILTER for the agenda.
7723 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7724 (if org-group-tags
7725 (let ((case-fold-search t) rtn)
7726 (mapc
7727 (lambda (f)
7728 (let (f0 dir)
7729 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7730 (setq dir (match-string 1 f) f0 (match-string 2 f))
7731 (setq dir (if no-operator "" "+") f0 f))
7732 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7733 (org-tags-expand f0 t t))
7734 rtn))))
7735 filter)
7736 (reverse rtn))
7737 filter))
7739 (defun org-agenda-filter-apply (filter type &optional expand)
7740 "Set FILTER as the new agenda filter and apply it. Optional
7741 argument EXPAND can be used for the TYPE tag and will expand the
7742 tags in the FILTER if any of the tags in FILTER are grouptags."
7743 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7744 (when org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7745 (let (tags cat txt)
7746 (setq org-agenda-filter-form (org-agenda-filter-make-matcher filter type expand))
7747 ;; Only set `org-agenda-filtered-by-category' to t when a unique
7748 ;; category is used as the filter:
7749 (setq org-agenda-filtered-by-category
7750 (and (eq type 'category)
7751 (not (equal (substring (car filter) 0 1) "-"))))
7752 (org-agenda-set-mode-name)
7753 (save-excursion
7754 (goto-char (point-min))
7755 (while (not (eobp))
7756 (if (org-get-at-bol 'org-marker)
7757 (progn
7758 (setq tags (org-get-at-bol 'tags)
7759 cat (org-agenda-get-category)
7760 txt (org-get-at-bol 'txt))
7761 (unless (eval org-agenda-filter-form)
7762 (org-agenda-filter-hide-line type))
7763 (beginning-of-line 2))
7764 (beginning-of-line 2))))
7765 (when (get-char-property (point) 'invisible)
7766 (ignore-errors (org-agenda-previous-line)))))
7768 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7769 "Filter by top headline HL."
7770 (org-agenda-set-mode-name)
7771 (save-excursion
7772 (goto-char (point-min))
7773 (while (not (eobp))
7774 (let* ((pos (org-get-at-bol 'org-hd-marker))
7775 (tophl (and pos (org-find-top-headline pos))))
7776 (when (and tophl (funcall (if negative 'identity 'not)
7777 (string= hl tophl)))
7778 (org-agenda-filter-hide-line 'top-headline)))
7779 (beginning-of-line 2)))
7780 (when (get-char-property (point) 'invisible)
7781 (org-agenda-previous-line))
7782 (setq org-agenda-top-headline-filter hl
7783 org-agenda-filtered-by-top-headline t))
7785 (defun org-agenda-filter-hide-line (type)
7786 "Hide lines with TYPE in the agenda buffer."
7787 (let* ((b (max (point-min) (1- (point-at-bol))))
7788 (e (point-at-eol)))
7789 (let ((inhibit-read-only t))
7790 (add-text-properties
7791 b e `(invisible org-filtered org-filter-type ,type)))))
7793 (defun org-agenda-remove-filter (type)
7794 (interactive)
7795 "Remove filter of type TYPE from the agenda buffer."
7796 (save-excursion
7797 (goto-char (point-min))
7798 (let ((inhibit-read-only t) pos)
7799 (while (setq pos (text-property-any (point) (point-max) 'org-filter-type type))
7800 (goto-char pos)
7801 (remove-text-properties
7802 (point) (next-single-property-change (point) 'org-filter-type)
7803 `(invisible org-filtered org-filter-type ,type))))
7804 (set (intern (format "org-agenda-%s-filter" (intern-soft type))) nil)
7805 (setq org-agenda-filter-form nil)
7806 (org-agenda-set-mode-name)
7807 (org-agenda-finalize)))
7809 (defun org-agenda-filter-show-all-tag nil
7810 (org-agenda-remove-filter 'tag))
7811 (defun org-agenda-filter-show-all-re nil
7812 (org-agenda-remove-filter 'regexp))
7813 (defun org-agenda-filter-show-all-effort nil
7814 (org-agenda-remove-filter 'effort))
7815 (defun org-agenda-filter-show-all-cat nil
7816 (org-agenda-remove-filter 'category))
7817 (defun org-agenda-filter-show-all-top-filter nil
7818 (org-agenda-remove-filter 'top-headline))
7820 (defun org-agenda-manipulate-query-add ()
7821 "Manipulate the query by adding a search term with positive selection.
7822 Positive selection means the term must be matched for selection of an entry."
7823 (interactive)
7824 (org-agenda-manipulate-query ?\[))
7825 (defun org-agenda-manipulate-query-subtract ()
7826 "Manipulate the query by adding a search term with negative selection.
7827 Negative selection means term must not be matched for selection of an entry."
7828 (interactive)
7829 (org-agenda-manipulate-query ?\]))
7830 (defun org-agenda-manipulate-query-add-re ()
7831 "Manipulate the query by adding a search regexp with positive selection.
7832 Positive selection means the regexp must match for selection of an entry."
7833 (interactive)
7834 (org-agenda-manipulate-query ?\{))
7835 (defun org-agenda-manipulate-query-subtract-re ()
7836 "Manipulate the query by adding a search regexp with negative selection.
7837 Negative selection means regexp must not match for selection of an entry."
7838 (interactive)
7839 (org-agenda-manipulate-query ?\}))
7840 (defun org-agenda-manipulate-query (char)
7841 (cond
7842 ((eq org-agenda-type 'agenda)
7843 (let ((org-agenda-include-inactive-timestamps t))
7844 (org-agenda-redo))
7845 (message "Display now includes inactive timestamps as well"))
7846 ((eq org-agenda-type 'search)
7847 (org-add-to-string
7848 'org-agenda-query-string
7849 (if org-agenda-last-search-view-search-was-boolean
7850 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7851 (?\{ . " +{}") (?\} . " -{}"))))
7852 " "))
7853 (setq org-agenda-redo-command
7854 (list 'org-search-view
7855 (car (get-text-property (min (1- (point-max)) (point))
7856 'org-last-args))
7857 org-agenda-query-string
7858 (+ (length org-agenda-query-string)
7859 (if (member char '(?\{ ?\})) 0 1))))
7860 (set-register org-agenda-query-register org-agenda-query-string)
7861 (let ((org-agenda-overriding-arguments
7862 (cdr org-agenda-redo-command)))
7863 (org-agenda-redo)))
7864 (t (error "Cannot manipulate query for %s-type agenda buffers"
7865 org-agenda-type))))
7867 (defun org-add-to-string (var string)
7868 (set var (concat (symbol-value var) string)))
7870 (defun org-agenda-goto-date (span)
7871 "Jump to DATE in agenda."
7872 (interactive "P")
7873 (let* ((org-read-date-prefer-future
7874 (eval org-agenda-jump-prefer-future))
7875 (date (org-read-date))
7876 (day (time-to-days (org-time-string-to-time date)))
7877 (org-agenda-sticky-orig org-agenda-sticky)
7878 (org-agenda-buffer-tmp-name (buffer-name))
7879 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7880 (0-arg (or current-prefix-arg (car args)))
7881 (2-arg (nth 2 args))
7882 (with-hour-p (nth 4 org-agenda-redo-command))
7883 (newcmd (list 'org-agenda-list 0-arg date
7884 (org-agenda-span-to-ndays
7885 2-arg (org-time-string-to-absolute date))
7886 with-hour-p))
7887 (newargs (cdr newcmd))
7888 (inhibit-read-only t)
7889 org-agenda-sticky)
7890 (if (not (org-agenda-check-type t 'agenda))
7891 (error "Not available in non-agenda views")
7892 (add-text-properties (point-min) (point-max)
7893 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7894 (org-agenda-redo)
7895 (goto-char (point-min))
7896 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7897 (save-excursion (move-beginning-of-line 2) (eobp))))
7898 (move-beginning-of-line 2))
7899 (setq org-agenda-sticky org-agenda-sticky-orig
7900 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7902 (defun org-agenda-goto-today ()
7903 "Go to today."
7904 (interactive)
7905 (org-agenda-check-type t 'agenda)
7906 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7907 (curspan (nth 2 args))
7908 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7909 (cond
7910 (tdpos (goto-char tdpos))
7911 ((eq org-agenda-type 'agenda)
7912 (let* ((sd (org-agenda-compute-starting-span
7913 (org-today) (or curspan org-agenda-span)))
7914 (org-agenda-overriding-arguments args))
7915 (setf (nth 1 org-agenda-overriding-arguments) sd)
7916 (org-agenda-redo)
7917 (org-agenda-find-same-or-today-or-agenda)))
7918 (t (error "Cannot find today")))))
7920 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7921 (goto-char
7922 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7923 (text-property-any (point-min) (point-max) 'org-today t)
7924 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7925 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7926 (org-agenda-backward-block))
7927 (point-min))))
7929 (defun org-agenda-backward-block ()
7930 "Move backward by one agenda block."
7931 (interactive)
7932 (org-agenda-forward-block 'backward))
7934 (defun org-agenda-forward-block (&optional backward)
7935 "Move forward by one agenda block.
7936 When optional argument BACKWARD is set, go backward"
7937 (interactive)
7938 (cond ((not (derived-mode-p 'org-agenda-mode))
7939 (user-error
7940 "Cannot execute this command outside of org-agenda-mode buffers"))
7941 ((looking-at (if backward "\\`" "\\'"))
7942 (message "Already at the %s block" (if backward "first" "last")))
7943 (t (let ((pos (prog1 (point)
7944 (ignore-errors (if backward (backward-char 1)
7945 (move-end-of-line 1)))))
7946 (f (if backward
7947 'previous-single-property-change
7948 'next-single-property-change))
7949 moved dest)
7950 (while (and (setq dest (funcall
7951 f (point) 'org-agenda-structural-header))
7952 (not (get-text-property
7953 (point) 'org-agenda-structural-header)))
7954 (setq moved t)
7955 (goto-char dest))
7956 (if moved (move-beginning-of-line 1)
7957 (goto-char (if backward (point-min) (point-max)))
7958 (move-beginning-of-line 1)
7959 (message "No %s block" (if backward "previous" "further")))))))
7961 (defun org-agenda-later (arg)
7962 "Go forward in time by the current span.
7963 With prefix ARG, go forward that many times the current span."
7964 (interactive "p")
7965 (org-agenda-check-type t 'agenda)
7966 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7967 (span (or (nth 2 args) org-agenda-current-span))
7968 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7969 (greg (calendar-gregorian-from-absolute sd))
7970 (cnt (org-get-at-bol 'org-day-cnt))
7971 greg2)
7972 (cond
7973 ((numberp span)
7974 (setq sd (+ (* span arg) sd)))
7975 ((eq span 'day)
7976 (setq sd (+ arg sd)))
7977 ((eq span 'week)
7978 (setq sd (+ (* 7 arg) sd)))
7979 ((eq span 'fortnight)
7980 (setq sd (+ (* 14 arg) sd)))
7981 ((eq span 'month)
7982 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7983 sd (calendar-absolute-from-gregorian greg2))
7984 (setcar greg2 (1+ (car greg2))))
7985 ((eq span 'year)
7986 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7987 sd (calendar-absolute-from-gregorian greg2))
7988 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7990 (setq sd (+ (* span arg) sd))))
7991 (let ((org-agenda-overriding-cmd
7992 ;; `cmd' may have been set by `org-agenda-run-series' which
7993 ;; uses `org-agenda-overriding-cmd' to decide whether
7994 ;; overriding is allowed for `cmd'
7995 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7996 (org-agenda-overriding-arguments
7997 (list (car args) sd span)))
7998 (org-agenda-redo)
7999 (org-agenda-find-same-or-today-or-agenda cnt))))
8001 (defun org-agenda-earlier (arg)
8002 "Go backward in time by the current span.
8003 With prefix ARG, go backward that many times the current span."
8004 (interactive "p")
8005 (org-agenda-later (- arg)))
8007 (defun org-agenda-view-mode-dispatch ()
8008 "Call one of the view mode commands."
8009 (interactive)
8010 (org-unlogged-message
8011 "View: [d]ay [w]eek for[t]night [m]onth [y]ear [SPC]reset [q]uit/abort
8012 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
8013 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
8014 (pcase (read-char-exclusive)
8015 (?\ (call-interactively 'org-agenda-reset-view))
8016 (?d (call-interactively 'org-agenda-day-view))
8017 (?w (call-interactively 'org-agenda-week-view))
8018 (?t (call-interactively 'org-agenda-fortnight-view))
8019 (?m (call-interactively 'org-agenda-month-view))
8020 (?y (call-interactively 'org-agenda-year-view))
8021 (?l (call-interactively 'org-agenda-log-mode))
8022 (?L (org-agenda-log-mode '(4)))
8023 (?c (org-agenda-log-mode 'clockcheck))
8024 ((or ?F ?f) (call-interactively 'org-agenda-follow-mode))
8025 (?a (call-interactively 'org-agenda-archives-mode))
8026 (?A (org-agenda-archives-mode 'files))
8027 ((or ?R ?r) (call-interactively 'org-agenda-clockreport-mode))
8028 ((or ?E ?e) (call-interactively 'org-agenda-entry-text-mode))
8029 (?G (call-interactively 'org-agenda-toggle-time-grid))
8030 (?D (call-interactively 'org-agenda-toggle-diary))
8031 (?\! (call-interactively 'org-agenda-toggle-deadlines))
8032 (?\[ (let ((org-agenda-include-inactive-timestamps t))
8033 (org-agenda-check-type t 'agenda)
8034 (org-agenda-redo))
8035 (message "Display now includes inactive timestamps as well"))
8036 (?q (message "Abort"))
8037 (key (user-error "Invalid key: %s" key))))
8039 (defun org-agenda-reset-view ()
8040 "Switch to default view for agenda."
8041 (interactive)
8042 (org-agenda-change-time-span org-agenda-span))
8044 (defun org-agenda-day-view (&optional day-of-month)
8045 "Switch to daily view for agenda.
8046 With argument DAY-OF-MONTH, switch to that day of the month."
8047 (interactive "P")
8048 (org-agenda-change-time-span 'day day-of-month))
8050 (defun org-agenda-week-view (&optional iso-week)
8051 "Switch to weekly view for agenda.
8052 With argument ISO-WEEK, switch to the corresponding ISO week.
8053 If ISO-WEEK has more then 2 digits, only the last two encode
8054 the week. Any digits before this encode a year. So 200712
8055 means week 12 of year 2007. Years ranging from 70 years ago
8056 to 30 years in the future can also be written as 2-digit years."
8057 (interactive "P")
8058 (org-agenda-change-time-span 'week iso-week))
8060 (defun org-agenda-fortnight-view (&optional iso-week)
8061 "Switch to fortnightly view for agenda.
8062 With argument ISO-WEEK, switch to the corresponding ISO week.
8063 If ISO-WEEK has more then 2 digits, only the last two encode
8064 the week. Any digits before this encode a year. So 200712
8065 means week 12 of year 2007. Years ranging from 70 years ago
8066 to 30 years in the future can also be written as 2-digit years."
8067 (interactive "P")
8068 (org-agenda-change-time-span 'fortnight iso-week))
8070 (defun org-agenda-month-view (&optional month)
8071 "Switch to monthly view for agenda.
8072 With argument MONTH, switch to that month. If MONTH has more
8073 then 2 digits, only the last two encode the month. Any digits
8074 before this encode a year. So 200712 means December year 2007.
8075 Years ranging from 70 years ago to 30 years in the future can
8076 also be written as 2-digit years."
8077 (interactive "P")
8078 (org-agenda-change-time-span 'month month))
8080 (defun org-agenda-year-view (&optional year)
8081 "Switch to yearly view for agenda.
8082 With argument YEAR, switch to that year. Years ranging from 70
8083 years ago to 30 years in the future can also be written as
8084 2-digit years."
8085 (interactive "P")
8086 (when year
8087 (setq year (org-small-year-to-year year)))
8088 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
8089 (org-agenda-change-time-span 'year year)
8090 (error "Abort")))
8092 (defun org-agenda-change-time-span (span &optional n)
8093 "Change the agenda view to SPAN.
8094 SPAN may be `day', `week', `fortnight', `month', `year'."
8095 (org-agenda-check-type t 'agenda)
8096 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
8097 (curspan (nth 2 args)))
8098 (when (and (not n) (equal curspan span))
8099 (error "Viewing span is already \"%s\"" span))
8100 (let* ((sd (or (org-get-at-bol 'day)
8101 (nth 1 args)
8102 org-starting-day))
8103 (sd (org-agenda-compute-starting-span sd span n))
8104 (org-agenda-overriding-cmd
8105 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
8106 (org-agenda-overriding-arguments
8107 (list (car args) sd span)))
8108 (org-agenda-redo)
8109 (org-agenda-find-same-or-today-or-agenda))
8110 (org-agenda-set-mode-name)
8111 (message "Switched to %s view" span)))
8113 (defun org-agenda-compute-starting-span (sd span &optional n)
8114 "Compute starting date for agenda.
8115 SPAN may be `day', `week', `fortnight', `month', `year'. The return value
8116 is a cons cell with the starting date and the number of days,
8117 so that the date SD will be in that range."
8118 (let* ((greg (calendar-gregorian-from-absolute sd))
8119 (dg (nth 1 greg))
8120 (mg (car greg))
8121 (yg (nth 2 greg)))
8122 (cond
8123 ((eq span 'day)
8124 (when n
8125 (setq sd (+ (calendar-absolute-from-gregorian
8126 (list mg 1 yg))
8127 n -1))))
8128 ((or (eq span 'week) (eq span 'fortnight))
8129 (let* ((nt (calendar-day-of-week
8130 (calendar-gregorian-from-absolute sd)))
8131 (d (if org-agenda-start-on-weekday
8132 (- nt org-agenda-start-on-weekday)
8135 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
8136 (when n
8137 (require 'cal-iso)
8138 (when (> n 99)
8139 (setq y1 (org-small-year-to-year (/ n 100))
8140 n (mod n 100)))
8141 (setq sd
8142 (calendar-iso-to-absolute
8143 (list n 1
8144 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
8145 ((eq span 'month)
8146 (let (y1)
8147 (when (and n (> n 99))
8148 (setq y1 (org-small-year-to-year (/ n 100))
8149 n (mod n 100)))
8150 (setq sd (calendar-absolute-from-gregorian
8151 (list (or n mg) 1 (or y1 yg))))))
8152 ((eq span 'year)
8153 (setq sd (calendar-absolute-from-gregorian
8154 (list 1 1 (or n yg))))))
8155 sd))
8157 (defun org-agenda-next-date-line (&optional arg)
8158 "Jump to the next line indicating a date in agenda buffer."
8159 (interactive "p")
8160 (org-agenda-check-type t 'agenda)
8161 (beginning-of-line 1)
8162 ;; This does not work if user makes date format that starts with a blank
8163 (when (looking-at-p "^\\S-") (forward-char 1))
8164 (unless (re-search-forward "^\\S-" nil t arg)
8165 (backward-char 1)
8166 (error "No next date after this line in this buffer"))
8167 (goto-char (match-beginning 0)))
8169 (defun org-agenda-previous-date-line (&optional arg)
8170 "Jump to the previous line indicating a date in agenda buffer."
8171 (interactive "p")
8172 (org-agenda-check-type t 'agenda)
8173 (beginning-of-line 1)
8174 (unless (re-search-backward "^\\S-" nil t arg)
8175 (error "No previous date before this line in this buffer")))
8177 ;; Initialize the highlight
8178 (defvar org-hl (make-overlay 1 1))
8179 (overlay-put org-hl 'face 'highlight)
8181 (defun org-highlight (begin end &optional buffer)
8182 "Highlight a region with overlay."
8183 (move-overlay org-hl begin end (or buffer (current-buffer))))
8185 (defun org-unhighlight ()
8186 "Detach overlay INDEX."
8187 (delete-overlay org-hl))
8189 (defun org-unhighlight-once ()
8190 "Remove the highlight from its position, and this function from the hook."
8191 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8192 (org-unhighlight))
8194 (defvar org-agenda-pre-follow-window-conf nil)
8195 (defun org-agenda-follow-mode ()
8196 "Toggle follow mode in an agenda buffer."
8197 (interactive)
8198 (unless org-agenda-follow-mode
8199 (setq org-agenda-pre-follow-window-conf
8200 (current-window-configuration)))
8201 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8202 (unless org-agenda-follow-mode
8203 (set-window-configuration org-agenda-pre-follow-window-conf))
8204 (org-agenda-set-mode-name)
8205 (org-agenda-do-context-action)
8206 (message "Follow mode is %s"
8207 (if org-agenda-follow-mode "on" "off")))
8209 (defun org-agenda-entry-text-mode (&optional arg)
8210 "Toggle entry text mode in an agenda buffer."
8211 (interactive "P")
8212 (if (or org-agenda-tag-filter
8213 org-agenda-category-filter
8214 org-agenda-regexp-filter
8215 org-agenda-top-headline-filter)
8216 (user-error "Can't show entry text in filtered views")
8217 (setq org-agenda-entry-text-mode (or (integerp arg)
8218 (not org-agenda-entry-text-mode)))
8219 (org-agenda-entry-text-hide)
8220 (and org-agenda-entry-text-mode
8221 (let ((org-agenda-entry-text-maxlines
8222 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8223 (org-agenda-entry-text-show)))
8224 (org-agenda-set-mode-name)
8225 (message "Entry text mode is %s%s"
8226 (if org-agenda-entry-text-mode "on" "off")
8227 (if (not org-agenda-entry-text-mode) ""
8228 (format " (maximum number of lines is %d)"
8229 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8231 (defun org-agenda-clockreport-mode ()
8232 "Toggle clocktable mode in an agenda buffer."
8233 (interactive)
8234 (org-agenda-check-type t 'agenda)
8235 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
8236 (org-agenda-set-mode-name)
8237 (org-agenda-redo)
8238 (message "Clocktable mode is %s"
8239 (if org-agenda-clockreport-mode "on" "off")))
8241 (defun org-agenda-log-mode (&optional special)
8242 "Toggle log mode in an agenda buffer.
8244 With argument SPECIAL, show all possible log items, not only the ones
8245 configured in `org-agenda-log-mode-items'.
8247 With a `\\[universal-argument] \\[universal-argument]' prefix, show *only* \
8248 log items, nothing else."
8249 (interactive "P")
8250 (org-agenda-check-type t 'agenda)
8251 (setq org-agenda-show-log
8252 (cond
8253 ((equal special '(16)) 'only)
8254 ((eq special 'clockcheck)
8255 (if (eq org-agenda-show-log 'clockcheck)
8256 nil 'clockcheck))
8257 (special '(closed clock state))
8258 (t (not org-agenda-show-log))))
8259 (org-agenda-set-mode-name)
8260 (org-agenda-redo)
8261 (message "Log mode is %s" (if org-agenda-show-log "on" "off")))
8263 (defun org-agenda-archives-mode (&optional with-files)
8264 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8265 When called with a prefix argument, include all archive files as well."
8266 (interactive "P")
8267 (setq org-agenda-archives-mode
8268 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8269 (org-agenda-set-mode-name)
8270 (org-agenda-redo)
8271 (message
8272 "%s"
8273 (cond
8274 ((eq org-agenda-archives-mode nil)
8275 "No archives are included")
8276 ((eq org-agenda-archives-mode 'trees)
8277 (format "Trees with :%s: tag are included" org-archive-tag))
8278 ((eq org-agenda-archives-mode t)
8279 (format "Trees with :%s: tag and all active archive files are included"
8280 org-archive-tag)))))
8282 (defun org-agenda-toggle-diary ()
8283 "Toggle diary inclusion in an agenda buffer."
8284 (interactive)
8285 (org-agenda-check-type t 'agenda)
8286 (setq org-agenda-include-diary (not org-agenda-include-diary))
8287 (org-agenda-redo)
8288 (org-agenda-set-mode-name)
8289 (message "Diary inclusion turned %s"
8290 (if org-agenda-include-diary "on" "off")))
8292 (defun org-agenda-toggle-deadlines ()
8293 "Toggle inclusion of entries with a deadline in an agenda buffer."
8294 (interactive)
8295 (org-agenda-check-type t 'agenda)
8296 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8297 (org-agenda-redo)
8298 (org-agenda-set-mode-name)
8299 (message "Deadlines inclusion turned %s"
8300 (if org-agenda-include-deadlines "on" "off")))
8302 (defun org-agenda-toggle-time-grid ()
8303 "Toggle time grid in an agenda buffer."
8304 (interactive)
8305 (org-agenda-check-type t 'agenda)
8306 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8307 (org-agenda-redo)
8308 (org-agenda-set-mode-name)
8309 (message "Time-grid turned %s"
8310 (if org-agenda-use-time-grid "on" "off")))
8312 (defun org-agenda-set-mode-name ()
8313 "Set the mode name to indicate all the small mode settings."
8314 (setq mode-name
8315 (list "Org-Agenda"
8316 (if (get 'org-agenda-files 'org-restrict) " []" "")
8318 '(:eval (org-agenda-span-name org-agenda-current-span))
8319 (if org-agenda-follow-mode " Follow" "")
8320 (if org-agenda-entry-text-mode " ETxt" "")
8321 (if org-agenda-include-diary " Diary" "")
8322 (if org-agenda-include-deadlines " Ddl" "")
8323 (if org-agenda-use-time-grid " Grid" "")
8324 (if (and (boundp 'org-habit-show-habits)
8325 org-habit-show-habits) " Habit" "")
8326 (cond
8327 ((consp org-agenda-show-log) " LogAll")
8328 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8329 (org-agenda-show-log " Log")
8330 (t ""))
8331 (if (or org-agenda-category-filter
8332 (get 'org-agenda-category-filter :preset-filter))
8333 '(:eval (propertize
8334 (concat " <"
8335 (mapconcat
8336 'identity
8337 (append
8338 (get 'org-agenda-category-filter :preset-filter)
8339 org-agenda-category-filter)
8341 ">")
8342 'face 'org-agenda-filter-category
8343 'help-echo "Category used in filtering")) "")
8344 (if (or org-agenda-tag-filter
8345 (get 'org-agenda-tag-filter :preset-filter))
8346 '(:eval (propertize
8347 (concat " {"
8348 (mapconcat
8349 'identity
8350 (append
8351 (get 'org-agenda-tag-filter :preset-filter)
8352 org-agenda-tag-filter)
8354 "}")
8355 'face 'org-agenda-filter-tags
8356 'help-echo "Tags used in filtering")) "")
8357 (if (or org-agenda-effort-filter
8358 (get 'org-agenda-effort-filter :preset-filter))
8359 '(:eval (propertize
8360 (concat " {"
8361 (mapconcat
8362 'identity
8363 (append
8364 (get 'org-agenda-effort-filter :preset-filter)
8365 org-agenda-effort-filter)
8367 "}")
8368 'face 'org-agenda-filter-effort
8369 'help-echo "Effort conditions used in filtering")) "")
8370 (if (or org-agenda-regexp-filter
8371 (get 'org-agenda-regexp-filter :preset-filter))
8372 '(:eval (propertize
8373 (concat " ["
8374 (mapconcat
8375 'identity
8376 (append
8377 (get 'org-agenda-regexp-filter :preset-filter)
8378 org-agenda-regexp-filter)
8380 "]")
8381 'face 'org-agenda-filter-regexp
8382 'help-echo "Regexp used in filtering")) "")
8383 (if org-agenda-archives-mode
8384 (if (eq org-agenda-archives-mode t)
8385 " Archives"
8386 (format " :%s:" org-archive-tag))
8388 (if org-agenda-clockreport-mode " Clock" "")))
8389 (force-mode-line-update))
8391 (defun org-agenda-update-agenda-type ()
8392 "Update the agenda type after each command."
8393 (setq org-agenda-type
8394 (or (get-text-property (point) 'org-agenda-type)
8395 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8397 (defun org-agenda-next-line ()
8398 "Move cursor to the next line, and show if follow mode is active."
8399 (interactive)
8400 (call-interactively 'next-line)
8401 (org-agenda-do-context-action))
8403 (defun org-agenda-previous-line ()
8404 "Move cursor to the previous line, and show if follow-mode is active."
8405 (interactive)
8406 (call-interactively 'previous-line)
8407 (org-agenda-do-context-action))
8409 (defun org-agenda-next-item (n)
8410 "Move cursor to next agenda item."
8411 (interactive "p")
8412 (let ((col (current-column)))
8413 (dotimes (c n)
8414 (when (next-single-property-change (point-at-eol) 'org-marker)
8415 (move-end-of-line 1)
8416 (goto-char (next-single-property-change (point) 'org-marker))))
8417 (org-move-to-column col))
8418 (org-agenda-do-context-action))
8420 (defun org-agenda-previous-item (n)
8421 "Move cursor to next agenda item."
8422 (interactive "p")
8423 (dotimes (c n)
8424 (let ((col (current-column))
8425 (goto (save-excursion
8426 (move-end-of-line 0)
8427 (previous-single-property-change (point) 'org-marker))))
8428 (when goto (goto-char goto))
8429 (org-move-to-column col)))
8430 (org-agenda-do-context-action))
8432 (defun org-agenda-do-context-action ()
8433 "Show outline path and, maybe, follow mode window."
8434 (let ((m (org-get-at-bol 'org-marker)))
8435 (when (and (markerp m) (marker-buffer m))
8436 (and org-agenda-follow-mode
8437 (if org-agenda-follow-indirect
8438 (org-agenda-tree-to-indirect-buffer nil)
8439 (org-agenda-show)))
8440 (and org-agenda-show-outline-path
8441 (org-with-point-at m (org-display-outline-path t))))))
8443 (defun org-agenda-show-tags ()
8444 "Show the tags applicable to the current item."
8445 (interactive)
8446 (let* ((tags (org-get-at-bol 'tags)))
8447 (if tags
8448 (message "Tags are :%s:"
8449 (org-no-properties (mapconcat 'identity tags ":")))
8450 (message "No tags associated with this line"))))
8452 (defun org-agenda-goto (&optional highlight)
8453 "Go to the entry at point in the corresponding Org file."
8454 (interactive)
8455 (let* ((marker (or (org-get-at-bol 'org-marker)
8456 (org-agenda-error)))
8457 (buffer (marker-buffer marker))
8458 (pos (marker-position marker)))
8459 (switch-to-buffer-other-window buffer)
8460 (widen)
8461 (push-mark)
8462 (goto-char pos)
8463 (when (derived-mode-p 'org-mode)
8464 (org-show-context 'agenda)
8465 (recenter (/ (window-height) 2))
8466 (org-back-to-heading t)
8467 (let ((case-fold-search nil))
8468 (when (re-search-forward org-complex-heading-regexp nil t)
8469 (goto-char (match-beginning 4)))))
8470 (run-hooks 'org-agenda-after-show-hook)
8471 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8473 (defvar org-agenda-after-show-hook nil
8474 "Normal hook run after an item has been shown from the agenda.
8475 Point is in the buffer where the item originated.")
8477 (defun org-agenda-kill ()
8478 "Kill the entry or subtree belonging to the current agenda entry."
8479 (interactive)
8480 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8481 (let* ((bufname-orig (buffer-name))
8482 (marker (or (org-get-at-bol 'org-marker)
8483 (org-agenda-error)))
8484 (buffer (marker-buffer marker))
8485 (pos (marker-position marker))
8486 (type (org-get-at-bol 'type))
8487 dbeg dend (n 0))
8488 (org-with-remote-undo buffer
8489 (with-current-buffer buffer
8490 (save-excursion
8491 (goto-char pos)
8492 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8493 (setq dbeg (progn (org-back-to-heading t) (point))
8494 dend (org-end-of-subtree t t))
8495 (setq dbeg (point-at-bol)
8496 dend (min (point-max) (1+ (point-at-eol)))))
8497 (goto-char dbeg)
8498 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8499 (when (or (eq t org-agenda-confirm-kill)
8500 (and (numberp org-agenda-confirm-kill)
8501 (> n org-agenda-confirm-kill)))
8502 (let ((win-conf (current-window-configuration)))
8503 (unwind-protect
8504 (and
8505 (prog2
8506 (org-agenda-tree-to-indirect-buffer nil)
8507 (not (y-or-n-p
8508 (format "Delete entry with %d lines in buffer \"%s\"? "
8509 n (buffer-name buffer))))
8510 (kill-buffer org-last-indirect-buffer))
8511 (error "Abort"))
8512 (set-window-configuration win-conf))))
8513 (let ((org-agenda-buffer-name bufname-orig))
8514 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8515 (with-current-buffer buffer (delete-region dbeg dend))
8516 (message "Agenda item and source killed"))))
8518 (defvar org-archive-default-command) ; defined in org-archive.el
8519 (defun org-agenda-archive-default ()
8520 "Archive the entry or subtree belonging to the current agenda entry."
8521 (interactive)
8522 (require 'org-archive)
8523 (org-agenda-archive-with org-archive-default-command))
8525 (defun org-agenda-archive-default-with-confirmation ()
8526 "Archive the entry or subtree belonging to the current agenda entry."
8527 (interactive)
8528 (require 'org-archive)
8529 (org-agenda-archive-with org-archive-default-command 'confirm))
8531 (defun org-agenda-archive ()
8532 "Archive the entry or subtree belonging to the current agenda entry."
8533 (interactive)
8534 (org-agenda-archive-with 'org-archive-subtree))
8536 (defun org-agenda-archive-to-archive-sibling ()
8537 "Move the entry to the archive sibling."
8538 (interactive)
8539 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8541 (defun org-agenda-archive-with (cmd &optional confirm)
8542 "Move the entry to the archive sibling."
8543 (interactive)
8544 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8545 (let* ((bufname-orig (buffer-name))
8546 (marker (or (org-get-at-bol 'org-marker)
8547 (org-agenda-error)))
8548 (buffer (marker-buffer marker))
8549 (pos (marker-position marker)))
8550 (org-with-remote-undo buffer
8551 (with-current-buffer buffer
8552 (if (derived-mode-p 'org-mode)
8553 (if (and confirm
8554 (not (y-or-n-p "Archive this subtree or entry? ")))
8555 (error "Abort")
8556 (save-window-excursion
8557 (goto-char pos)
8558 (let ((org-agenda-buffer-name bufname-orig))
8559 (org-remove-subtree-entries-from-agenda))
8560 (org-back-to-heading t)
8561 (funcall cmd)))
8562 (error "Archiving works only in Org files"))))))
8564 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8565 "Remove all lines in the agenda that correspond to a given subtree.
8566 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8567 If this information is not given, the function uses the tree at point."
8568 (let ((buf (or buf (current-buffer))) m p)
8569 (save-excursion
8570 (unless (and beg end)
8571 (org-back-to-heading t)
8572 (setq beg (point))
8573 (org-end-of-subtree t)
8574 (setq end (point)))
8575 (set-buffer (get-buffer org-agenda-buffer-name))
8576 (save-excursion
8577 (goto-char (point-max))
8578 (beginning-of-line 1)
8579 (while (not (bobp))
8580 (when (and (setq m (org-get-at-bol 'org-marker))
8581 (equal buf (marker-buffer m))
8582 (setq p (marker-position m))
8583 (>= p beg)
8584 (< p end))
8585 (let ((inhibit-read-only t))
8586 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8587 (beginning-of-line 0))))))
8589 (defun org-agenda-refile (&optional goto rfloc no-update)
8590 "Refile the item at point.
8592 When called with `\\[universal-argument] \\[universal-argument]', \
8593 go to the location of the last
8594 refiled item.
8596 When called with `\\[universal-argument] \\[universal-argument] \
8597 \\[universal-argument]' prefix or when GOTO is 0, clear
8598 the refile cache.
8600 RFLOC can be a refile location obtained in a different way.
8602 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8603 (interactive "P")
8604 (cond
8605 ((member goto '(0 (64)))
8606 (org-refile-cache-clear))
8607 ((equal goto '(16))
8608 (org-refile-goto-last-stored))
8610 (let* ((buffer-orig (buffer-name))
8611 (marker (or (org-get-at-bol 'org-hd-marker)
8612 (org-agenda-error)))
8613 (buffer (marker-buffer marker))
8614 (pos (marker-position marker))
8615 (rfloc (or rfloc
8616 (org-refile-get-location
8617 (if goto "Goto" "Refile to") buffer
8618 org-refile-allow-creating-parent-nodes))))
8619 (with-current-buffer buffer
8620 (org-with-wide-buffer
8621 (goto-char marker)
8622 (let ((org-agenda-buffer-name buffer-orig))
8623 (org-remove-subtree-entries-from-agenda))
8624 (org-refile goto buffer rfloc))))
8625 (unless no-update (org-agenda-redo)))))
8627 (defun org-agenda-open-link (&optional arg)
8628 "Open the link(s) in the current entry, if any.
8629 This looks for a link in the displayed line in the agenda.
8630 It also looks at the text of the entry itself."
8631 (interactive "P")
8632 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8633 (org-get-at-bol 'org-marker)))
8634 (buffer (and marker (marker-buffer marker)))
8635 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8636 (lkall (and buffer (org-offer-links-in-entry
8637 buffer marker arg prefix)))
8638 (lk0 (car lkall))
8639 (lk (if (stringp lk0) (list lk0) lk0))
8640 (lkend (cdr lkall))
8641 trg)
8642 (cond
8643 ((and buffer lk)
8644 (mapcar (lambda(l)
8645 (with-current-buffer buffer
8646 (setq trg (and (string-match org-link-bracket-re l)
8647 (match-string 1 l)))
8648 (if (or (not trg) (string-match org-link-any-re trg))
8649 (org-with-wide-buffer
8650 (goto-char marker)
8651 (when (search-forward l nil lkend)
8652 (goto-char (match-beginning 0))
8653 (org-open-at-point)))
8654 ;; This is an internal link, widen the buffer
8655 (switch-to-buffer-other-window buffer)
8656 (widen)
8657 (goto-char marker)
8658 (when (search-forward l nil lkend)
8659 (goto-char (match-beginning 0))
8660 (org-open-at-point)))))
8661 lk))
8662 ((or (org-in-regexp (concat "\\(" org-link-bracket-re "\\)"))
8663 (save-excursion
8664 (beginning-of-line 1)
8665 (looking-at (concat ".*?\\(" org-link-bracket-re "\\)"))))
8666 (org-link-open-from-string (match-string 1)))
8667 (t (message "No link to open here")))))
8669 (defun org-agenda-copy-local-variable (var)
8670 "Get a variable from a referenced buffer and install it here."
8671 (let ((m (org-get-at-bol 'org-marker)))
8672 (when (and m (buffer-live-p (marker-buffer m)))
8673 (set (make-local-variable var)
8674 (with-current-buffer (marker-buffer m)
8675 (symbol-value var))))))
8677 (defun org-agenda-switch-to (&optional delete-other-windows)
8678 "Go to the Org mode file which contains the item at point.
8679 When optional argument DELETE-OTHER-WINDOWS is non-nil, the
8680 displayed Org file fills the frame."
8681 (interactive)
8682 (if (and org-return-follows-link
8683 (not (org-get-at-bol 'org-marker))
8684 (org-in-regexp org-link-bracket-re))
8685 (org-link-open-from-string (match-string 0))
8686 (let* ((marker (or (org-get-at-bol 'org-marker)
8687 (org-agenda-error)))
8688 (buffer (marker-buffer marker))
8689 (pos (marker-position marker)))
8690 (unless buffer (user-error "Trying to switch to non-existent buffer"))
8691 (pop-to-buffer-same-window buffer)
8692 (when delete-other-windows (delete-other-windows))
8693 (widen)
8694 (goto-char pos)
8695 (when (derived-mode-p 'org-mode)
8696 (org-show-context 'agenda)
8697 (run-hooks 'org-agenda-after-show-hook)))))
8699 (defun org-agenda-goto-mouse (ev)
8700 "Go to the Org file which contains the item at the mouse click."
8701 (interactive "e")
8702 (mouse-set-point ev)
8703 (org-agenda-goto))
8705 (defun org-agenda-show (&optional full-entry)
8706 "Display the Org file which contains the item at point.
8707 With prefix argument FULL-ENTRY, make the entire entry visible
8708 if it was hidden in the outline."
8709 (interactive "P")
8710 (let ((win (selected-window)))
8711 (org-agenda-goto t)
8712 (when full-entry (org-show-entry))
8713 (select-window win)))
8715 (defvar org-agenda-show-window nil)
8716 (defun org-agenda-show-and-scroll-up (&optional arg)
8717 "Display the Org file which contains the item at point.
8719 When called repeatedly, scroll the window that is displaying the buffer.
8721 With a `\\[universal-argument]' prefix argument, display the item, but \
8722 fold drawers."
8723 (interactive "P")
8724 (let ((win (selected-window)))
8725 (if (and (window-live-p org-agenda-show-window)
8726 (eq this-command last-command))
8727 (progn
8728 (select-window org-agenda-show-window)
8729 (ignore-errors (scroll-up)))
8730 (org-agenda-goto t)
8731 (org-show-entry)
8732 (if arg (org-cycle-hide-drawers 'children)
8733 (org-with-wide-buffer
8734 (narrow-to-region (org-entry-beginning-position)
8735 (org-entry-end-position))
8736 (org-show-all '(drawers))))
8737 (when arg )
8738 (setq org-agenda-show-window (selected-window)))
8739 (select-window win)))
8741 (defun org-agenda-show-scroll-down ()
8742 "Scroll down the window showing the agenda."
8743 (interactive)
8744 (let ((win (selected-window)))
8745 (when (window-live-p org-agenda-show-window)
8746 (select-window org-agenda-show-window)
8747 (ignore-errors (scroll-down))
8748 (select-window win))))
8750 (defun org-agenda-show-1 (&optional more)
8751 "Display the Org file which contains the item at point.
8752 The prefix arg selects the amount of information to display:
8754 0 hide the subtree
8755 1 just show the entry according to defaults.
8756 2 show the children view
8757 3 show the subtree view
8758 4 show the entire subtree and any LOGBOOK drawers
8759 5 show the entire subtree and any drawers
8760 With prefix argument FULL-ENTRY, make the entire entry visible
8761 if it was hidden in the outline."
8762 (interactive "p")
8763 (let ((win (selected-window)))
8764 (org-agenda-goto t)
8765 (org-back-to-heading)
8766 (set-window-start (selected-window) (point-at-bol))
8767 (cond
8768 ((= more 0)
8769 (org-flag-subtree t)
8770 (save-excursion
8771 (org-back-to-heading)
8772 (run-hook-with-args 'org-cycle-hook 'folded))
8773 (message "Remote: FOLDED"))
8774 ((and (called-interactively-p 'any) (= more 1))
8775 (message "Remote: show with default settings"))
8776 ((= more 2)
8777 (outline-show-entry)
8778 (org-show-children)
8779 (save-excursion
8780 (org-back-to-heading)
8781 (run-hook-with-args 'org-cycle-hook 'children))
8782 (message "Remote: CHILDREN"))
8783 ((= more 3)
8784 (outline-show-subtree)
8785 (save-excursion
8786 (org-back-to-heading)
8787 (run-hook-with-args 'org-cycle-hook 'subtree))
8788 (message "Remote: SUBTREE"))
8789 ((= more 4)
8790 (outline-show-subtree)
8791 (save-excursion
8792 (org-back-to-heading)
8793 (org-cycle-hide-drawers 'subtree '("LOGBOOK")))
8794 (message "Remote: SUBTREE AND LOGBOOK"))
8795 ((> more 4)
8796 (outline-show-subtree)
8797 (message "Remote: SUBTREE AND ALL DRAWERS")))
8798 (select-window win)))
8800 (defvar org-agenda-cycle-counter nil)
8801 (defun org-agenda-cycle-show (&optional n)
8802 "Show the current entry in another window, with default settings.
8804 Default settings are taken from `org-show-context-detail'. When
8805 use repeatedly in immediate succession, the remote entry will
8806 cycle through visibility
8808 children -> subtree -> folded
8810 When called with a numeric prefix arg, that arg will be passed through to
8811 `org-agenda-show-1'. For the interpretation of that argument, see the
8812 docstring of `org-agenda-show-1'."
8813 (interactive "P")
8814 (if (integerp n)
8815 (setq org-agenda-cycle-counter n)
8816 (if (not (eq last-command this-command))
8817 (setq org-agenda-cycle-counter 1)
8818 (if (equal org-agenda-cycle-counter 0)
8819 (setq org-agenda-cycle-counter 2)
8820 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8821 (when (> org-agenda-cycle-counter 3)
8822 (setq org-agenda-cycle-counter 0)))))
8823 (org-agenda-show-1 org-agenda-cycle-counter))
8825 (defun org-agenda-recenter (arg)
8826 "Display the Org file which contains the item at point and recenter."
8827 (interactive "P")
8828 (let ((win (selected-window)))
8829 (org-agenda-goto t)
8830 (recenter arg)
8831 (select-window win)))
8833 (defun org-agenda-show-mouse (ev)
8834 "Display the Org file which contains the item at the mouse click."
8835 (interactive "e")
8836 (mouse-set-point ev)
8837 (org-agenda-show))
8839 (defun org-agenda-check-no-diary ()
8840 "Check if the entry is a diary link and abort if yes."
8841 (when (org-get-at-bol 'org-agenda-diary-link)
8842 (org-agenda-error)))
8844 (defun org-agenda-error ()
8845 "Throw an error when a command is not allowed in the agenda."
8846 (user-error "Command not allowed in this line"))
8848 (defun org-agenda-tree-to-indirect-buffer (arg)
8849 "Show the subtree corresponding to the current entry in an indirect buffer.
8850 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8852 With a numerical prefix ARG, go up to this level and then take that tree.
8853 With a negative numeric ARG, go up by this number of levels.
8855 With a `\\[universal-argument]' prefix, make a separate frame for this tree, \
8856 i.e. don't use
8857 the dedicated frame."
8858 (interactive "P")
8859 (if current-prefix-arg
8860 (org-agenda-do-tree-to-indirect-buffer arg)
8861 (let ((agenda-buffer (buffer-name))
8862 (agenda-window (selected-window))
8863 (indirect-window
8864 (and org-last-indirect-buffer
8865 (get-buffer-window org-last-indirect-buffer))))
8866 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8867 (unless (or (eq org-indirect-buffer-display 'new-frame)
8868 (eq org-indirect-buffer-display 'dedicated-frame))
8869 (unwind-protect
8870 (unless (and indirect-window (window-live-p indirect-window))
8871 (setq indirect-window (split-window agenda-window)))
8872 (and indirect-window (select-window indirect-window))
8873 (switch-to-buffer org-last-indirect-buffer :norecord)
8874 (fit-window-to-buffer indirect-window)))
8875 (select-window (get-buffer-window agenda-buffer))
8876 (setq org-agenda-last-indirect-buffer org-last-indirect-buffer))))
8878 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8879 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8880 (org-agenda-check-no-diary)
8881 (let* ((marker (or (org-get-at-bol 'org-marker)
8882 (org-agenda-error)))
8883 (buffer (marker-buffer marker))
8884 (pos (marker-position marker)))
8885 (with-current-buffer buffer
8886 (save-excursion
8887 (goto-char pos)
8888 (org-tree-to-indirect-buffer arg)))))
8890 (defvar org-last-heading-marker (make-marker)
8891 "Marker pointing to the headline that last changed its TODO state
8892 by a remote command from the agenda.")
8894 (defun org-agenda-todo-nextset ()
8895 "Switch TODO entry to next sequence."
8896 (interactive)
8897 (org-agenda-todo 'nextset))
8899 (defun org-agenda-todo-previousset ()
8900 "Switch TODO entry to previous sequence."
8901 (interactive)
8902 (org-agenda-todo 'previousset))
8904 (defun org-agenda-todo (&optional arg)
8905 "Cycle TODO state of line at point, also in Org file.
8906 This changes the line at point, all other lines in the agenda referring to
8907 the same tree node, and the headline of the tree node in the Org file."
8908 (interactive "P")
8909 (org-agenda-check-no-diary)
8910 (let* ((col (current-column))
8911 (marker (or (org-get-at-bol 'org-marker)
8912 (org-agenda-error)))
8913 (buffer (marker-buffer marker))
8914 (pos (marker-position marker))
8915 (hdmarker (org-get-at-bol 'org-hd-marker))
8916 (todayp (org-agenda-today-p (org-get-at-bol 'day)))
8917 (inhibit-read-only t)
8918 org-agenda-headline-snapshot-before-repeat newhead just-one)
8919 (org-with-remote-undo buffer
8920 (with-current-buffer buffer
8921 (widen)
8922 (goto-char pos)
8923 (org-show-context 'agenda)
8924 (let ((current-prefix-arg arg))
8925 (call-interactively 'org-todo))
8926 (and (bolp) (forward-char 1))
8927 (setq newhead (org-get-heading))
8928 (when (and (bound-and-true-p
8929 org-agenda-headline-snapshot-before-repeat)
8930 (not (equal org-agenda-headline-snapshot-before-repeat
8931 newhead))
8932 todayp)
8933 (setq newhead org-agenda-headline-snapshot-before-repeat
8934 just-one t))
8935 (save-excursion
8936 (org-back-to-heading)
8937 (move-marker org-last-heading-marker (point))))
8938 (beginning-of-line 1)
8939 (save-window-excursion
8940 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8941 (when (bound-and-true-p org-clock-out-when-done)
8942 (string-match (concat "^" (regexp-opt org-done-keywords-for-agenda))
8943 newhead)
8944 (org-agenda-unmark-clocking-task))
8945 (org-move-to-column col)
8946 (org-agenda-mark-clocking-task))))
8948 (defun org-agenda-add-note (&optional arg)
8949 "Add a time-stamped note to the entry at point."
8950 (interactive "P")
8951 (org-agenda-check-no-diary)
8952 (let* ((marker (or (org-get-at-bol 'org-marker)
8953 (org-agenda-error)))
8954 (buffer (marker-buffer marker))
8955 (pos (marker-position marker))
8956 (hdmarker (org-get-at-bol 'org-hd-marker))
8957 (inhibit-read-only t))
8958 (with-current-buffer buffer
8959 (widen)
8960 (goto-char pos)
8961 (org-show-context 'agenda)
8962 (org-add-note))))
8964 (defun org-agenda-change-all-lines (newhead hdmarker
8965 &optional fixface just-this)
8966 "Change all lines in the agenda buffer which match HDMARKER.
8967 The new content of the line will be NEWHEAD (as modified by
8968 `org-agenda-format-item'). HDMARKER is checked with
8969 `equal' against all `org-hd-marker' text properties in the file.
8970 If FIXFACE is non-nil, the face of each item is modified according to
8971 the new TODO state.
8972 If JUST-THIS is non-nil, change just the current line, not all.
8973 If FORCE-TAGS is non nil, the car of it returns the new tags."
8974 (let* ((inhibit-read-only t)
8975 (line (org-current-line))
8976 (org-agenda-buffer (current-buffer))
8977 (thetags (with-current-buffer (marker-buffer hdmarker)
8978 (org-get-tags hdmarker)))
8979 props m pl undone-face done-face finish new dotime level cat tags)
8980 (save-excursion
8981 (goto-char (point-max))
8982 (beginning-of-line 1)
8983 (while (not finish)
8984 (setq finish (bobp))
8985 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8986 (or (not just-this) (= (org-current-line) line))
8987 (equal m hdmarker))
8988 (setq props (text-properties-at (point))
8989 dotime (org-get-at-bol 'dotime)
8990 cat (org-agenda-get-category)
8991 level (org-get-at-bol 'level)
8992 tags thetags
8994 (let ((org-prefix-format-compiled
8995 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8996 org-prefix-format-compiled))
8997 (extra (org-get-at-bol 'extra)))
8998 (with-current-buffer (marker-buffer hdmarker)
8999 (org-with-wide-buffer
9000 (org-agenda-format-item extra newhead level cat tags dotime))))
9001 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
9002 undone-face (org-get-at-bol 'undone-face)
9003 done-face (org-get-at-bol 'done-face))
9004 (beginning-of-line 1)
9005 (cond
9006 ((equal new "") (delete-region (point) (line-beginning-position 2)))
9007 ((looking-at ".*")
9008 ;; When replacing the whole line, preserve bulk mark
9009 ;; overlay, if any.
9010 (let ((mark (catch :overlay
9011 (dolist (o (overlays-in (point) (+ 2 (point))))
9012 (when (eq (overlay-get o 'type)
9013 'org-marked-entry-overlay)
9014 (throw :overlay o))))))
9015 (replace-match new t t)
9016 (beginning-of-line)
9017 (when mark (move-overlay mark (point) (+ 2 (point)))))
9018 (add-text-properties (point-at-bol) (point-at-eol) props)
9019 (when fixface
9020 (add-text-properties
9021 (point-at-bol) (point-at-eol)
9022 (list 'face
9023 (if org-last-todo-state-is-todo
9024 undone-face done-face))))
9025 (org-agenda-highlight-todo 'line)
9026 (beginning-of-line 1))
9027 (t (error "Line update did not work")))
9028 (save-restriction
9029 (narrow-to-region (point-at-bol) (point-at-eol))
9030 (org-agenda-finalize)))
9031 (beginning-of-line 0)))))
9033 (defun org-agenda-align-tags (&optional line)
9034 "Align all tags in agenda items to `org-agenda-tags-column'.
9035 When optional argument LINE is non-nil, align tags only on the
9036 current line."
9037 (let ((inhibit-read-only t)
9038 (org-agenda-tags-column (if (eq 'auto org-agenda-tags-column)
9039 (- (window-text-width))
9040 org-agenda-tags-column))
9041 (end (and line (line-end-position)))
9042 l c)
9043 (save-excursion
9044 (goto-char (if line (line-beginning-position) (point-min)))
9045 (while (re-search-forward org-tag-group-re end t)
9046 (add-text-properties
9047 (match-beginning 1) (match-end 1)
9048 (list 'face (delq nil (let ((prop (get-text-property
9049 (match-beginning 1) 'face)))
9050 (or (listp prop) (setq prop (list prop)))
9051 (if (memq 'org-tag prop)
9052 prop
9053 (cons 'org-tag prop))))))
9054 (setq l (string-width (match-string 1))
9055 c (if (< org-agenda-tags-column 0)
9056 (- (abs org-agenda-tags-column) l)
9057 org-agenda-tags-column))
9058 (goto-char (match-beginning 1))
9059 (delete-region (save-excursion (skip-chars-backward " \t") (point))
9060 (point))
9061 (insert (org-add-props
9062 (make-string (max 1 (- c (current-column))) ?\s)
9063 (plist-put (copy-sequence (text-properties-at (point)))
9064 'face nil))))
9065 (goto-char (point-min))
9066 (org-font-lock-add-tag-faces (point-max)))))
9068 (defun org-agenda-priority-up ()
9069 "Increase the priority of line at point, also in Org file."
9070 (interactive)
9071 (org-agenda-priority 'up))
9073 (defun org-agenda-priority-down ()
9074 "Decrease the priority of line at point, also in Org file."
9075 (interactive)
9076 (org-agenda-priority 'down))
9078 (defun org-agenda-priority (&optional force-direction)
9079 "Set the priority of line at point, also in Org file.
9080 This changes the line at point, all other lines in the agenda referring to
9081 the same tree node, and the headline of the tree node in the Org file.
9082 Called with a universal prefix arg, show the priority instead of setting it."
9083 (interactive "P")
9084 (if (equal force-direction '(4))
9085 (org-show-priority)
9086 (unless org-enable-priority-commands
9087 (error "Priority commands are disabled"))
9088 (org-agenda-check-no-diary)
9089 (let* ((col (current-column))
9090 (marker (or (org-get-at-bol 'org-marker)
9091 (org-agenda-error)))
9092 (hdmarker (org-get-at-bol 'org-hd-marker))
9093 (buffer (marker-buffer hdmarker))
9094 (pos (marker-position hdmarker))
9095 (inhibit-read-only t)
9096 newhead)
9097 (org-with-remote-undo buffer
9098 (with-current-buffer buffer
9099 (widen)
9100 (goto-char pos)
9101 (org-show-context 'agenda)
9102 (org-priority force-direction)
9103 (end-of-line 1)
9104 (setq newhead (org-get-heading)))
9105 (org-agenda-change-all-lines newhead hdmarker)
9106 (org-move-to-column col)))))
9108 ;; FIXME: should fix the tags property of the agenda line.
9109 (defun org-agenda-set-tags (&optional tag onoff)
9110 "Set tags for the current headline."
9111 (interactive)
9112 (org-agenda-check-no-diary)
9113 (if (and (org-region-active-p) (called-interactively-p 'any))
9114 (call-interactively 'org-change-tag-in-region)
9115 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9116 (org-agenda-error)))
9117 (buffer (marker-buffer hdmarker))
9118 (pos (marker-position hdmarker))
9119 (inhibit-read-only t)
9120 newhead)
9121 (org-with-remote-undo buffer
9122 (with-current-buffer buffer
9123 (widen)
9124 (goto-char pos)
9125 (org-show-context 'agenda)
9126 (if tag
9127 (org-toggle-tag tag onoff)
9128 (call-interactively #'org-set-tags-command))
9129 (end-of-line 1)
9130 (setq newhead (org-get-heading)))
9131 (org-agenda-change-all-lines newhead hdmarker)
9132 (beginning-of-line 1)))))
9134 (defun org-agenda-set-property ()
9135 "Set a property for the current headline."
9136 (interactive)
9137 (org-agenda-check-no-diary)
9138 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9139 (org-agenda-error)))
9140 (buffer (marker-buffer hdmarker))
9141 (pos (marker-position hdmarker))
9142 (inhibit-read-only t)
9143 newhead)
9144 (org-with-remote-undo buffer
9145 (with-current-buffer buffer
9146 (widen)
9147 (goto-char pos)
9148 (org-show-context 'agenda)
9149 (call-interactively 'org-set-property)))))
9151 (defun org-agenda-set-effort ()
9152 "Set the effort property for the current headline."
9153 (interactive)
9154 (org-agenda-check-no-diary)
9155 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9156 (org-agenda-error)))
9157 (buffer (marker-buffer hdmarker))
9158 (pos (marker-position hdmarker))
9159 (inhibit-read-only t)
9160 newhead)
9161 (org-with-remote-undo buffer
9162 (with-current-buffer buffer
9163 (widen)
9164 (goto-char pos)
9165 (org-show-context 'agenda)
9166 (call-interactively 'org-set-effort)
9167 (end-of-line 1)
9168 (setq newhead (org-get-heading)))
9169 (org-agenda-change-all-lines newhead hdmarker))))
9171 (defun org-agenda-toggle-archive-tag ()
9172 "Toggle the archive tag for the current entry."
9173 (interactive)
9174 (org-agenda-check-no-diary)
9175 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9176 (org-agenda-error)))
9177 (buffer (marker-buffer hdmarker))
9178 (pos (marker-position hdmarker))
9179 (inhibit-read-only t)
9180 newhead)
9181 (org-with-remote-undo buffer
9182 (with-current-buffer buffer
9183 (widen)
9184 (goto-char pos)
9185 (org-show-context 'agenda)
9186 (call-interactively 'org-toggle-archive-tag)
9187 (end-of-line 1)
9188 (setq newhead (org-get-heading)))
9189 (org-agenda-change-all-lines newhead hdmarker)
9190 (beginning-of-line 1))))
9192 (defun org-agenda-do-date-later (arg)
9193 (interactive "P")
9194 (cond
9195 ((or (equal arg '(16))
9196 (memq last-command
9197 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9198 (setq this-command 'org-agenda-date-later-minutes)
9199 (org-agenda-date-later-minutes 1))
9200 ((or (equal arg '(4))
9201 (memq last-command
9202 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9203 (setq this-command 'org-agenda-date-later-hours)
9204 (org-agenda-date-later-hours 1))
9206 (org-agenda-date-later (prefix-numeric-value arg)))))
9208 (defun org-agenda-do-date-earlier (arg)
9209 (interactive "P")
9210 (cond
9211 ((or (equal arg '(16))
9212 (memq last-command
9213 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9214 (setq this-command 'org-agenda-date-earlier-minutes)
9215 (org-agenda-date-earlier-minutes 1))
9216 ((or (equal arg '(4))
9217 (memq last-command
9218 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9219 (setq this-command 'org-agenda-date-earlier-hours)
9220 (org-agenda-date-earlier-hours 1))
9222 (org-agenda-date-earlier (prefix-numeric-value arg)))))
9224 (defun org-agenda-date-later (arg &optional what)
9225 "Change the date of this item to ARG day(s) later."
9226 (interactive "p")
9227 (org-agenda-check-type t 'agenda)
9228 (org-agenda-check-no-diary)
9229 (let* ((marker (or (org-get-at-bol 'org-marker)
9230 (org-agenda-error)))
9231 (buffer (marker-buffer marker))
9232 (pos (marker-position marker))
9233 cdate today)
9234 (org-with-remote-undo buffer
9235 (with-current-buffer buffer
9236 (widen)
9237 (goto-char pos)
9238 (unless (org-at-timestamp-p 'lax) (error "Cannot find time stamp"))
9239 (when (and org-agenda-move-date-from-past-immediately-to-today
9240 (equal arg 1)
9241 (or (not what) (eq what 'day))
9242 (not (save-match-data (org-at-date-range-p))))
9243 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9244 cdate (calendar-absolute-from-gregorian
9245 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9246 today (org-today))
9247 (when (> today cdate)
9248 ;; immediately shift to today
9249 (setq arg (- today cdate))))
9250 (org-timestamp-change arg (or what 'day))
9251 (when (and (org-at-date-range-p)
9252 (re-search-backward org-tr-regexp-both (point-at-bol)))
9253 (let ((end org-last-changed-timestamp))
9254 (org-timestamp-change arg (or what 'day))
9255 (setq org-last-changed-timestamp
9256 (concat org-last-changed-timestamp "--" end)))))
9257 (org-agenda-show-new-time marker org-last-changed-timestamp))
9258 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9260 (defun org-agenda-date-earlier (arg &optional what)
9261 "Change the date of this item to ARG day(s) earlier."
9262 (interactive "p")
9263 (org-agenda-date-later (- arg) what))
9265 (defun org-agenda-date-later-minutes (arg)
9266 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9267 (interactive "p")
9268 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9269 (org-agenda-date-later arg 'minute))
9271 (defun org-agenda-date-earlier-minutes (arg)
9272 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9273 (interactive "p")
9274 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9275 (org-agenda-date-earlier arg 'minute))
9277 (defun org-agenda-date-later-hours (arg)
9278 "Change the time of this item, in hour steps."
9279 (interactive "p")
9280 (org-agenda-date-later arg 'hour))
9282 (defun org-agenda-date-earlier-hours (arg)
9283 "Change the time of this item, in hour steps."
9284 (interactive "p")
9285 (org-agenda-date-earlier arg 'hour))
9287 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9288 "Show new date stamp via text properties."
9289 ;; We use text properties to make this undoable
9290 (let ((inhibit-read-only t))
9291 (setq stamp (concat prefix " => " stamp " "))
9292 (save-excursion
9293 (goto-char (point-max))
9294 (while (not (bobp))
9295 (when (equal marker (org-get-at-bol 'org-marker))
9296 (remove-text-properties (point-at-bol) (point-at-eol) '(display))
9297 (org-move-to-column (- (window-width) (length stamp)) t)
9298 (add-text-properties
9299 (1- (point)) (point-at-eol)
9300 (list 'display (org-add-props stamp nil
9301 'face '(secondary-selection default))))
9302 (beginning-of-line 1))
9303 (beginning-of-line 0)))))
9305 (defun org-agenda-date-prompt (arg)
9306 "Change the date of this item. Date is prompted for, with default today.
9307 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9308 be used to request time specification in the time stamp."
9309 (interactive "P")
9310 (org-agenda-check-type t 'agenda)
9311 (org-agenda-check-no-diary)
9312 (let* ((marker (or (org-get-at-bol 'org-marker)
9313 (org-agenda-error)))
9314 (buffer (marker-buffer marker))
9315 (pos (marker-position marker)))
9316 (org-with-remote-undo buffer
9317 (with-current-buffer buffer
9318 (widen)
9319 (goto-char pos)
9320 (unless (org-at-timestamp-p 'lax) (error "Cannot find time stamp"))
9321 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
9322 (org-agenda-show-new-time marker org-last-changed-timestamp))
9323 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9325 (defun org-agenda-schedule (arg &optional time)
9326 "Schedule the item at point.
9327 ARG is passed through to `org-schedule'."
9328 (interactive "P")
9329 (org-agenda-check-type t 'agenda 'todo 'tags 'search)
9330 (org-agenda-check-no-diary)
9331 (let* ((marker (or (org-get-at-bol 'org-marker)
9332 (org-agenda-error)))
9333 (type (marker-insertion-type marker))
9334 (buffer (marker-buffer marker))
9335 (pos (marker-position marker))
9337 (set-marker-insertion-type marker t)
9338 (org-with-remote-undo buffer
9339 (with-current-buffer buffer
9340 (widen)
9341 (goto-char pos)
9342 (setq ts (org-schedule arg time)))
9343 (org-agenda-show-new-time marker ts " S"))
9344 (message "%s" ts)))
9346 (defun org-agenda-deadline (arg &optional time)
9347 "Schedule the item at point.
9348 ARG is passed through to `org-deadline'."
9349 (interactive "P")
9350 (org-agenda-check-type t 'agenda 'todo 'tags 'search)
9351 (org-agenda-check-no-diary)
9352 (let* ((marker (or (org-get-at-bol 'org-marker)
9353 (org-agenda-error)))
9354 (buffer (marker-buffer marker))
9355 (pos (marker-position marker))
9357 (org-with-remote-undo buffer
9358 (with-current-buffer buffer
9359 (widen)
9360 (goto-char pos)
9361 (setq ts (org-deadline arg time)))
9362 (org-agenda-show-new-time marker ts " D"))
9363 (message "%s" ts)))
9365 (defun org-agenda-clock-in (&optional arg)
9366 "Start the clock on the currently selected item."
9367 (interactive "P")
9368 (org-agenda-check-no-diary)
9369 (if (equal arg '(4))
9370 (org-clock-in arg)
9371 (let* ((marker (or (org-get-at-bol 'org-marker)
9372 (org-agenda-error)))
9373 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9374 (pos (marker-position marker))
9375 (col (current-column))
9376 newhead)
9377 (org-with-remote-undo (marker-buffer marker)
9378 (with-current-buffer (marker-buffer marker)
9379 (widen)
9380 (goto-char pos)
9381 (org-show-context 'agenda)
9382 (org-clock-in arg)
9383 (setq newhead (org-get-heading)))
9384 (org-agenda-change-all-lines newhead hdmarker))
9385 (org-move-to-column col))))
9387 (defun org-agenda-clock-out ()
9388 "Stop the currently running clock."
9389 (interactive)
9390 (unless (marker-buffer org-clock-marker)
9391 (error "No running clock"))
9392 (let ((marker (make-marker)) (col (current-column)) newhead)
9393 (org-with-remote-undo (marker-buffer org-clock-marker)
9394 (with-current-buffer (marker-buffer org-clock-marker)
9395 (org-with-wide-buffer
9396 (goto-char org-clock-marker)
9397 (org-back-to-heading t)
9398 (move-marker marker (point))
9399 (org-clock-out)
9400 (setq newhead (org-get-heading)))))
9401 (org-agenda-change-all-lines newhead marker)
9402 (move-marker marker nil)
9403 (org-move-to-column col)
9404 (org-agenda-unmark-clocking-task)))
9406 (defun org-agenda-clock-cancel (&optional arg)
9407 "Cancel the currently running clock."
9408 (interactive "P")
9409 (unless (marker-buffer org-clock-marker)
9410 (user-error "No running clock"))
9411 (org-with-remote-undo (marker-buffer org-clock-marker)
9412 (org-clock-cancel)))
9414 (defun org-agenda-clock-goto ()
9415 "Jump to the currently clocked in task within the agenda.
9416 If the currently clocked in task is not listed in the agenda
9417 buffer, display it in another window."
9418 (interactive)
9419 (let (pos)
9420 (mapc (lambda (o)
9421 (when (eq (overlay-get o 'type) 'org-agenda-clocking)
9422 (setq pos (overlay-start o))))
9423 (overlays-in (point-min) (point-max)))
9424 (cond (pos (goto-char pos))
9425 ;; If the currently clocked entry is not in the agenda
9426 ;; buffer, we visit it in another window:
9427 ((bound-and-true-p org-clock-current-task)
9428 (org-switch-to-buffer-other-window (org-clock-goto)))
9429 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9431 (defun org-agenda-diary-entry-in-org-file ()
9432 "Make a diary entry in the file `org-agenda-diary-file'."
9433 (let (d1 d2 char (text "") dp1 dp2)
9434 (if (equal (buffer-name) "*Calendar*")
9435 (setq d1 (calendar-cursor-to-date t)
9436 d2 (car calendar-mark-ring))
9437 (setq dp1 (get-text-property (point-at-bol) 'day))
9438 (unless dp1 (user-error "No date defined in current line"))
9439 (setq d1 (calendar-gregorian-from-absolute dp1)
9440 d2 (and (ignore-errors (mark))
9441 (save-excursion
9442 (goto-char (mark))
9443 (setq dp2 (get-text-property (point-at-bol) 'day)))
9444 (calendar-gregorian-from-absolute dp2))))
9445 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9446 (setq char (read-char-exclusive))
9447 (cond
9448 ((equal char ?d)
9449 (setq text (read-string "Day entry: "))
9450 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9451 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9452 ((equal char ?a)
9453 (setq d1 (list (car d1) (nth 1 d1)
9454 (read-number (format "Reference year [%d]: " (nth 2 d1))
9455 (nth 2 d1))))
9456 (setq text (read-string "Anniversary (use %d to show years): "))
9457 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9458 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9459 ((equal char ?b)
9460 (setq text (read-string "Block entry: "))
9461 (unless (and d1 d2 (not (equal d1 d2)))
9462 (user-error "No block of days selected"))
9463 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9464 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9465 ((equal char ?j)
9466 (org-switch-to-buffer-other-window
9467 (find-file-noselect org-agenda-diary-file))
9468 (require 'org-datetree)
9469 (org-datetree-find-date-create d1)
9470 (org-reveal t))
9471 (t (user-error "Invalid selection character `%c'" char)))))
9473 (defcustom org-agenda-insert-diary-strategy 'date-tree
9474 "Where in `org-agenda-diary-file' should new entries be added?
9475 Valid values:
9477 date-tree in the date tree, as first child of the date
9478 date-tree-last in the date tree, as last child of the date
9479 top-level as top-level entries at the end of the file."
9480 :group 'org-agenda
9481 :type '(choice
9482 (const :tag "first in a date tree" date-tree)
9483 (const :tag "last in a date tree" date-tree-last)
9484 (const :tag "as top level at end of file" top-level)))
9486 (defcustom org-agenda-insert-diary-extract-time nil
9487 "Non-nil means extract any time specification from the diary entry."
9488 :group 'org-agenda
9489 :version "24.1"
9490 :type 'boolean)
9492 (defcustom org-agenda-bulk-mark-char ">"
9493 "A single-character string to be used as the bulk mark."
9494 :group 'org-agenda
9495 :version "24.1"
9496 :type 'string)
9498 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9499 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9500 If TEXT is not empty, it will become the headline of the new entry, and
9501 the resulting entry will not be shown. When TEXT is empty, switch to
9502 `org-agenda-diary-file' and let the user finish the entry there."
9503 (let ((cw (current-window-configuration)))
9504 (org-switch-to-buffer-other-window
9505 (find-file-noselect org-agenda-diary-file))
9506 (widen)
9507 (goto-char (point-min))
9508 (cl-case type
9509 (anniversary
9510 (or (re-search-forward "^\\*[ \t]+Anniversaries" nil t)
9511 (progn
9512 (or (org-at-heading-p t)
9513 (progn
9514 (outline-next-heading)
9515 (insert "* Anniversaries\n\n")
9516 (beginning-of-line -1)))))
9517 (outline-next-heading)
9518 (org-back-over-empty-lines)
9519 (backward-char 1)
9520 (insert "\n")
9521 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9522 (nth 2 d1) (car d1) (nth 1 d1) text)))
9523 (day
9524 (let ((org-prefix-has-time t)
9525 (org-agenda-time-leading-zero t)
9526 fmt time time2)
9527 (when org-agenda-insert-diary-extract-time
9528 ;; Use org-agenda-format-item to parse text for a time-range and
9529 ;; remove it. FIXME: This is a hack, we should refactor
9530 ;; that function to make time extraction available separately
9531 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9532 time (get-text-property 0 'time fmt)
9533 time2 (if (> (length time) 0)
9534 ;; split-string removes trailing ...... if
9535 ;; no end time given. First space
9536 ;; separates time from date.
9537 (concat " " (car (split-string time "\\.")))
9538 nil)
9539 text (get-text-property 0 'txt fmt)))
9540 (if (eq org-agenda-insert-diary-strategy 'top-level)
9541 (org-agenda-insert-diary-as-top-level text)
9542 (require 'org-datetree)
9543 (org-datetree-find-date-create d1)
9544 (org-agenda-insert-diary-make-new-entry text))
9545 (org-insert-time-stamp (org-time-from-absolute
9546 (calendar-absolute-from-gregorian d1))
9547 nil nil nil nil time2))
9548 (end-of-line 0))
9549 ((block) ;; Wrap this in (strictly unnecessary) parens because
9550 ;; otherwise the indentation gets confused by the
9551 ;; special meaning of 'block
9552 (when (> (calendar-absolute-from-gregorian d1)
9553 (calendar-absolute-from-gregorian d2))
9554 (setq d1 (prog1 d2 (setq d2 d1))))
9555 (if (eq org-agenda-insert-diary-strategy 'top-level)
9556 (org-agenda-insert-diary-as-top-level text)
9557 (require 'org-datetree)
9558 (org-datetree-find-date-create d1)
9559 (org-agenda-insert-diary-make-new-entry text))
9560 (org-insert-time-stamp (org-time-from-absolute
9561 (calendar-absolute-from-gregorian d1)))
9562 (insert "--")
9563 (org-insert-time-stamp (org-time-from-absolute
9564 (calendar-absolute-from-gregorian d2)))
9565 (end-of-line 0)))
9566 (if (string-match "\\S-" text)
9567 (progn
9568 (set-window-configuration cw)
9569 (message "%s entry added to %s"
9570 (capitalize (symbol-name type))
9571 (abbreviate-file-name org-agenda-diary-file)))
9572 (org-reveal t)
9573 (message "Please finish entry here"))))
9575 (defun org-agenda-insert-diary-as-top-level (text)
9576 "Make new entry as a top-level entry at the end of the file.
9577 Add TEXT as headline, and position the cursor in the second line so that
9578 a timestamp can be added there."
9579 (widen)
9580 (goto-char (point-max))
9581 (unless (bolp) (insert "\n"))
9582 (org-insert-heading nil t t)
9583 (insert text)
9584 (org-end-of-meta-data)
9585 (unless (bolp) (insert "\n"))
9586 (when org-adapt-indentation (indent-to-column 2)))
9588 (defun org-agenda-insert-diary-make-new-entry (text)
9589 "Make a new entry with TEXT as a child of the current subtree.
9590 Position the point in the heading's first body line so that
9591 a timestamp can be added there."
9592 (cond
9593 ((eq org-agenda-insert-diary-strategy 'date-tree-last)
9594 (end-of-line)
9595 (org-insert-heading '(4) t)
9596 (org-do-demote))
9598 (outline-next-heading)
9599 (org-back-over-empty-lines)
9600 (unless (looking-at "[ \t]*$") (save-excursion (insert "\n")))
9601 (org-insert-heading nil t)
9602 (org-do-demote)))
9603 (let ((col (current-column)))
9604 (insert text)
9605 (org-end-of-meta-data)
9606 ;; Ensure point is left on a blank line, at proper indentation.
9607 (unless (bolp) (insert "\n"))
9608 (unless (looking-at-p "^[ \t]*$") (save-excursion (insert "\n")))
9609 (when org-adapt-indentation (indent-to-column col)))
9610 (org-show-set-visibility 'lineage))
9612 (defun org-agenda-diary-entry ()
9613 "Make a diary entry, like the `i' command from the calendar.
9614 All the standard commands work: block, weekly etc.
9615 When `org-agenda-diary-file' points to a file,
9616 `org-agenda-diary-entry-in-org-file' is called instead to create
9617 entries in that Org file."
9618 (interactive)
9619 (if (not (eq org-agenda-diary-file 'diary-file))
9620 (org-agenda-diary-entry-in-org-file)
9621 (require 'diary-lib)
9622 (let* ((char (read-char-exclusive
9623 "Diary entry: [d]ay [w]eekly [m]onthly [y]early\
9624 [a]nniversary [b]lock [c]yclic"))
9625 (cmd (cdr (assoc char
9626 '((?d . diary-insert-entry)
9627 (?w . diary-insert-weekly-entry)
9628 (?m . diary-insert-monthly-entry)
9629 (?y . diary-insert-yearly-entry)
9630 (?a . diary-insert-anniversary-entry)
9631 (?b . diary-insert-block-entry)
9632 (?c . diary-insert-cyclic-entry)))))
9633 (oldf (symbol-function 'calendar-cursor-to-date))
9634 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9635 (point (point))
9636 (mark (or (mark t) (point))))
9637 (unless cmd
9638 (user-error "No command associated with <%c>" char))
9639 (unless (and (get-text-property point 'day)
9640 (or (not (equal ?b char))
9641 (get-text-property mark 'day)))
9642 (user-error "Don't know which date to use for diary entry"))
9643 ;; We implement this by hacking the `calendar-cursor-to-date' function
9644 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9645 (let ((calendar-mark-ring
9646 (list (calendar-gregorian-from-absolute
9647 (or (get-text-property mark 'day)
9648 (get-text-property point 'day))))))
9649 (unwind-protect
9650 (progn
9651 (fset 'calendar-cursor-to-date
9652 (lambda (&optional error dummy)
9653 (calendar-gregorian-from-absolute
9654 (get-text-property point 'day))))
9655 (call-interactively cmd))
9656 (fset 'calendar-cursor-to-date oldf))))))
9658 (defun org-agenda-execute-calendar-command (cmd)
9659 "Execute a calendar command from the agenda with date from cursor."
9660 (org-agenda-check-type t 'agenda)
9661 (require 'diary-lib)
9662 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9663 (user-error "Don't know which date to use for the calendar command"))
9664 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9665 (point (point))
9666 (date (calendar-gregorian-from-absolute
9667 (get-text-property point 'day)))
9668 ;; the following 2 vars are needed in the calendar
9669 (displayed-month (car date))
9670 (displayed-year (nth 2 date)))
9671 (unwind-protect
9672 (progn
9673 (fset 'calendar-cursor-to-date
9674 (lambda (&optional error dummy)
9675 (calendar-gregorian-from-absolute
9676 (get-text-property point 'day))))
9677 (call-interactively cmd))
9678 (fset 'calendar-cursor-to-date oldf))))
9680 (defun org-agenda-phases-of-moon ()
9681 "Display the phases of the moon for the 3 months around the cursor date."
9682 (interactive)
9683 (org-agenda-execute-calendar-command 'calendar-lunar-phases))
9685 (defun org-agenda-holidays ()
9686 "Display the holidays for the 3 months around the cursor date."
9687 (interactive)
9688 (org-agenda-execute-calendar-command 'calendar-list-holidays))
9690 (defvar calendar-longitude) ; defined in calendar.el
9691 (defvar calendar-latitude) ; defined in calendar.el
9692 (defvar calendar-location-name) ; defined in calendar.el
9694 (defun org-agenda-sunrise-sunset (arg)
9695 "Display sunrise and sunset for the cursor date.
9696 Latitude and longitude can be specified with the variables
9697 `calendar-latitude' and `calendar-longitude'. When called with prefix
9698 argument, latitude and longitude will be prompted for."
9699 (interactive "P")
9700 (require 'solar)
9701 (let ((calendar-longitude (if arg nil calendar-longitude))
9702 (calendar-latitude (if arg nil calendar-latitude))
9703 (calendar-location-name
9704 (if arg "the given coordinates" calendar-location-name)))
9705 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9707 (defun org-agenda-goto-calendar ()
9708 "Open the Emacs calendar with the date at the cursor."
9709 (interactive)
9710 (org-agenda-check-type t 'agenda)
9711 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9712 (user-error "Don't know which date to open in calendar")))
9713 (date (calendar-gregorian-from-absolute day))
9714 (calendar-move-hook nil)
9715 (calendar-view-holidays-initially-flag nil)
9716 (calendar-view-diary-initially-flag nil))
9717 (calendar)
9718 (calendar-goto-date date)))
9720 ;;;###autoload
9721 (defun org-calendar-goto-agenda ()
9722 "Compute the Org agenda for the calendar date displayed at the cursor.
9723 This is a command that has to be installed in `calendar-mode-map'."
9724 (interactive)
9725 ;; Temporarily disable sticky agenda since user clearly wants to
9726 ;; refresh view anyway.
9727 (let ((org-agenda-buffer-tmp-name "*Org Agenda(a)*")
9728 (org-agenda-sticky nil))
9729 (org-agenda-list nil (calendar-absolute-from-gregorian
9730 (calendar-cursor-to-date))
9731 nil)))
9733 (defun org-agenda-convert-date ()
9734 (interactive)
9735 (org-agenda-check-type t 'agenda)
9736 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9737 date s)
9738 (unless day
9739 (user-error "Don't know which date to convert"))
9740 (setq date (calendar-gregorian-from-absolute day))
9741 (setq s (concat
9742 "Gregorian: " (calendar-date-string date) "\n"
9743 "ISO: " (calendar-iso-date-string date) "\n"
9744 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9745 "Julian: " (calendar-julian-date-string date) "\n"
9746 "Astron. JD: " (calendar-astro-date-string date)
9747 " (Julian date number at noon UTC)\n"
9748 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9749 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9750 "French: " (calendar-french-date-string date) "\n"
9751 "Bahá’í: " (calendar-bahai-date-string date) " (until sunset)\n"
9752 "Mayan: " (calendar-mayan-date-string date) "\n"
9753 "Coptic: " (calendar-coptic-date-string date) "\n"
9754 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9755 "Persian: " (calendar-persian-date-string date) "\n"
9756 "Chinese: " (calendar-chinese-date-string date) "\n"))
9757 (with-output-to-temp-buffer "*Dates*"
9758 (princ s))
9759 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9761 ;;; Bulk commands
9763 (defun org-agenda-bulk-marked-p ()
9764 "Non-nil when current entry is marked for bulk action."
9765 (eq (get-char-property (point-at-bol) 'type)
9766 'org-marked-entry-overlay))
9768 (defun org-agenda-bulk-mark (&optional arg)
9769 "Mark entries for future bulk action.
9771 When ARG is nil or one and region is not active then mark the
9772 entry at point.
9774 When ARG is nil or one and region is active then mark the entries
9775 in the region.
9777 When ARG is greater than one mark ARG lines."
9778 (interactive "p")
9779 (when (and (or (not arg) (= arg 1)) (use-region-p))
9780 (setq arg (count-lines (region-beginning) (region-end)))
9781 (goto-char (region-beginning))
9782 (deactivate-mark))
9783 (dotimes (i (or arg 1))
9784 (unless (org-get-at-bol 'org-agenda-diary-link)
9785 (let* ((m (org-get-at-bol 'org-hd-marker))
9787 (unless (org-agenda-bulk-marked-p)
9788 (unless m (user-error "Nothing to mark at point"))
9789 (push m org-agenda-bulk-marked-entries)
9790 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9791 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9792 (org-get-todo-face "TODO")
9793 'evaporate)
9794 (overlay-put ov 'type 'org-marked-entry-overlay))
9795 (end-of-line 1)
9796 (or (ignore-errors
9797 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9798 (beginning-of-line 2))
9799 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9800 (beginning-of-line 2)))))
9801 (message "%d entries marked for bulk action"
9802 (length org-agenda-bulk-marked-entries)))
9804 (defun org-agenda-bulk-mark-all ()
9805 "Mark all entries for future agenda bulk action."
9806 (interactive)
9807 (org-agenda-bulk-mark-regexp "."))
9809 (defun org-agenda-bulk-mark-regexp (regexp)
9810 "Mark entries matching REGEXP for future agenda bulk action."
9811 (interactive "sMark entries matching regexp: ")
9812 (let ((entries-marked 0) txt-at-point)
9813 (save-excursion
9814 (goto-char (point-min))
9815 (goto-char (next-single-property-change (point) 'org-hd-marker))
9816 (while (and (re-search-forward regexp nil t)
9817 (setq txt-at-point (get-text-property (point) 'txt)))
9818 (if (get-char-property (point) 'invisible)
9819 (beginning-of-line 2)
9820 (when (string-match regexp txt-at-point)
9821 (setq entries-marked (1+ entries-marked))
9822 (call-interactively 'org-agenda-bulk-mark)))))
9824 (unless entries-marked
9825 (message "No entry matching this regexp."))))
9827 (defun org-agenda-bulk-unmark (&optional arg)
9828 "Unmark the entry at point for future bulk action."
9829 (interactive "P")
9830 (if arg
9831 (org-agenda-bulk-unmark-all)
9832 (cond ((org-agenda-bulk-marked-p)
9833 (org-agenda-bulk-remove-overlays
9834 (point-at-bol) (+ 2 (point-at-bol)))
9835 (setq org-agenda-bulk-marked-entries
9836 (delete (org-get-at-bol 'org-hd-marker)
9837 org-agenda-bulk-marked-entries))
9838 (end-of-line 1)
9839 (or (ignore-errors
9840 (goto-char (next-single-property-change (point) 'txt)))
9841 (beginning-of-line 2))
9842 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9843 (beginning-of-line 2))
9844 (message "%d entries left marked for bulk action"
9845 (length org-agenda-bulk-marked-entries)))
9846 (t (message "No entry to unmark here")))))
9848 (defun org-agenda-bulk-toggle-all ()
9849 "Toggle all marks for bulk action."
9850 (interactive)
9851 (save-excursion
9852 (goto-char (point-min))
9853 (while (ignore-errors
9854 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9855 (org-agenda-bulk-toggle))))
9857 (defun org-agenda-bulk-toggle ()
9858 "Toggle the mark at point for bulk action."
9859 (interactive)
9860 (if (org-agenda-bulk-marked-p)
9861 (org-agenda-bulk-unmark)
9862 (org-agenda-bulk-mark)))
9864 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9865 "Remove the mark overlays between BEG and END in the agenda buffer.
9866 BEG and END default to the buffer limits.
9868 This only removes the overlays, it does not remove the markers
9869 from the list in `org-agenda-bulk-marked-entries'."
9870 (interactive)
9871 (mapc (lambda (ov)
9872 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9873 (delete-overlay ov)))
9874 (overlays-in (or beg (point-min)) (or end (point-max)))))
9876 (defun org-agenda-bulk-unmark-all ()
9877 "Remove all marks in the agenda buffer.
9878 This will remove the markers and the overlays."
9879 (interactive)
9880 (if (null org-agenda-bulk-marked-entries)
9881 (message "No entry to unmark")
9882 (setq org-agenda-bulk-marked-entries nil)
9883 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9885 (defcustom org-agenda-persistent-marks nil
9886 "Non-nil means marked items will stay marked after a bulk action.
9887 You can toggle this interactively by typing `p' when prompted for a
9888 bulk action."
9889 :group 'org-agenda
9890 :version "24.1"
9891 :type 'boolean)
9893 (defun org-agenda-bulk-action (&optional arg)
9894 "Execute an remote-editing action on all marked entries.
9895 The prefix arg is passed through to the command if possible."
9896 (interactive "P")
9897 ;; When there is no mark, act on the agenda entry at point.
9898 (if (not org-agenda-bulk-marked-entries)
9899 (save-excursion (org-agenda-bulk-mark)))
9900 (dolist (m org-agenda-bulk-marked-entries)
9901 (unless (and (markerp m)
9902 (marker-buffer m)
9903 (buffer-live-p (marker-buffer m))
9904 (marker-position m))
9905 (user-error "Marker %s for bulk command is invalid" m)))
9907 ;; Prompt for the bulk command.
9908 (org-unlogged-message
9909 (concat (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")
9910 "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9911 "[S]catter [f]unction [p]ersist-toggle "
9912 (and org-agenda-bulk-custom-functions
9913 (format " Custom: [%s]"
9914 (mapconcat (lambda (f) (char-to-string (car f)))
9915 org-agenda-bulk-custom-functions
9916 "")))))
9917 (catch 'exit
9918 (let* ((org-log-refile (if org-log-refile 'time nil))
9919 (entries (reverse org-agenda-bulk-marked-entries))
9920 (org-overriding-default-time
9921 (and (get-text-property (point) 'org-agenda-date-header)
9922 (org-get-cursor-date)))
9923 redo-at-end
9924 cmd)
9925 (pcase (read-char-exclusive)
9927 (let ((org-agenda-persistent-marks
9928 (not org-agenda-persistent-marks)))
9929 (org-agenda-bulk-action)
9930 (throw 'exit nil)))
9933 (setq cmd #'org-agenda-archive))
9936 (setq cmd #'org-agenda-archive-to-archive-sibling))
9938 ((or ?r ?w)
9939 (let ((refile-location
9940 (org-refile-get-location
9941 "Refile to"
9942 (marker-buffer (car entries))
9943 org-refile-allow-creating-parent-nodes)))
9944 (when (nth 3 refile-location)
9945 (setcar (nthcdr 3 refile-location)
9946 (move-marker
9947 (make-marker)
9948 (nth 3 refile-location)
9949 (or (get-file-buffer (nth 1 refile-location))
9950 (find-buffer-visiting (nth 1 refile-location))
9951 (error "This should not happen")))))
9953 (setq cmd `(lambda () (org-agenda-refile nil ',refile-location t)))
9954 (setq redo-at-end t)))
9957 (let ((state (completing-read
9958 "Todo state: "
9959 (with-current-buffer (marker-buffer (car entries))
9960 (mapcar #'list org-todo-keywords-1)))))
9961 (setq cmd `(lambda ()
9962 (let ((org-inhibit-blocking t)
9963 (org-inhibit-logging 'note))
9964 (org-agenda-todo ,state))))))
9966 ((and (or ?- ?+) action)
9967 (let ((tag (completing-read
9968 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9969 (with-current-buffer (marker-buffer (car entries))
9970 (delq nil
9971 (mapcar (lambda (x) (and (stringp (car x)) x))
9972 org-current-tag-alist))))))
9973 (setq cmd
9974 `(lambda ()
9975 (org-agenda-set-tags ,tag
9976 ,(if (eq action ?+) ''on ''off))))))
9978 ((and (or ?s ?d) c)
9979 (let* ((schedule? (eq c ?s))
9980 (prompt (if schedule? "(Re)Schedule to" "(Re)Set Deadline to"))
9981 (time
9982 (and (not arg)
9983 (let ((new (org-read-date
9984 nil nil nil prompt org-overriding-default-time)))
9985 ;; A "double plus" answer applies to every
9986 ;; scheduled time. Do not turn it into
9987 ;; a fixed date yet.
9988 (if (string-match-p "\\`[ \t]*\\+\\+"
9989 org-read-date-final-answer)
9990 org-read-date-final-answer
9991 new)))))
9992 ;; Make sure to not prompt for a note when bulk
9993 ;; rescheduling/resetting deadline as Org cannot cope with
9994 ;; simultaneous notes. Besides, it could be annoying
9995 ;; depending on the number of marked items.
9996 (setq cmd
9997 (if schedule?
9998 `(lambda ()
9999 (let ((org-log-reschedule
10000 (and org-log-reschedule 'time)))
10001 (org-agenda-schedule arg ,time)))
10002 `(lambda ()
10003 (let ((org-log-redeadline (and org-log-redeadline 'time)))
10004 (org-agenda-deadline arg ,time)))))))
10007 (unless (org-agenda-check-type nil 'agenda 'todo)
10008 (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type))
10009 (let ((days (read-number
10010 (format "Scatter tasks across how many %sdays: "
10011 (if arg "week" ""))
10012 7)))
10013 (setq cmd
10014 `(lambda ()
10015 (let ((distance (1+ (random ,days))))
10016 (when arg
10017 (let ((dist distance)
10018 (day-of-week
10019 (calendar-day-of-week
10020 (calendar-gregorian-from-absolute (org-today)))))
10021 (dotimes (i (1+ dist))
10022 (while (member day-of-week org-agenda-weekend-days)
10023 (cl-incf distance)
10024 (cl-incf day-of-week)
10025 (when (= day-of-week 7)
10026 (setq day-of-week 0)))
10027 (cl-incf day-of-week)
10028 (when (= day-of-week 7)
10029 (setq day-of-week 0)))))
10030 ;; Silently fail when try to replan a sexp entry.
10031 (ignore-errors
10032 (let* ((date (calendar-gregorian-from-absolute
10033 (+ (org-today) distance)))
10034 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
10035 (nth 2 date))))
10036 (org-agenda-schedule nil time))))))))
10039 (setq cmd
10040 (intern
10041 (completing-read "Function: " obarray #'fboundp t nil nil))))
10043 (action
10044 (pcase (assoc action org-agenda-bulk-custom-functions)
10045 (`(,_ ,f) (setq cmd f) (setq redo-at-end t))
10046 (_ (user-error "Invalid bulk action: %c" action)))))
10048 ;; Sort the markers, to make sure that parents are handled
10049 ;; before children.
10050 (setq entries (sort entries
10051 (lambda (a b)
10052 (cond
10053 ((eq (marker-buffer a) (marker-buffer b))
10054 (< (marker-position a) (marker-position b)))
10056 (string< (buffer-name (marker-buffer a))
10057 (buffer-name (marker-buffer b))))))))
10059 ;; Now loop over all markers and apply CMD.
10060 (let ((processed 0)
10061 (skipped 0))
10062 (dolist (e entries)
10063 (let ((pos (text-property-any (point-min) (point-max) 'org-hd-marker e)))
10064 (if (not pos)
10065 (progn (message "Skipping removed entry at %s" e)
10066 (cl-incf skipped))
10067 (goto-char pos)
10068 (let (org-loop-over-headlines-in-active-region) (funcall cmd))
10069 ;; `post-command-hook' is not run yet. We make sure any
10070 ;; pending log note is processed.
10071 (when (or (memq 'org-add-log-note (default-value 'post-command-hook))
10072 (memq 'org-add-log-note post-command-hook))
10073 (org-add-log-note))
10074 (cl-incf processed))))
10075 (when redo-at-end (org-agenda-redo))
10076 (unless org-agenda-persistent-marks (org-agenda-bulk-unmark-all))
10077 (message "Acted on %d entries%s%s"
10078 processed
10079 (if (= skipped 0)
10081 (format ", skipped %d (disappeared before their turn)"
10082 skipped))
10083 (if (not org-agenda-persistent-marks) "" " (kept marked)"))))))
10085 (defun org-agenda-capture (&optional with-time)
10086 "Call `org-capture' with the date at point.
10087 With a `C-1' prefix, use the HH:MM value at point (if any) or the
10088 current HH:MM time."
10089 (interactive "P")
10090 (if (not (eq major-mode 'org-agenda-mode))
10091 (user-error "You cannot do this outside of agenda buffers")
10092 (let ((org-overriding-default-time
10093 (org-get-cursor-date (equal with-time 1))))
10094 (call-interactively 'org-capture))))
10096 ;;; Dragging agenda lines forward/backward
10098 (defun org-agenda-reapply-filters ()
10099 "Re-apply all agenda filters."
10100 (mapcar
10101 (lambda(f) (when (car f) (org-agenda-filter-apply (car f) (cadr f) t)))
10102 `((,org-agenda-tag-filter tag)
10103 (,org-agenda-category-filter category)
10104 (,org-agenda-regexp-filter regexp)
10105 (,org-agenda-effort-filter effort)
10106 (,(get 'org-agenda-tag-filter :preset-filter) tag)
10107 (,(get 'org-agenda-category-filter :preset-filter) category)
10108 (,(get 'org-agenda-effort-filter :preset-filter) effort)
10109 (,(get 'org-agenda-regexp-filter :preset-filter) regexp))))
10111 (defun org-agenda-drag-line-forward (arg &optional backward)
10112 "Drag an agenda line forward by ARG lines.
10113 When the optional argument `backward' is non-nil, move backward."
10114 (interactive "p")
10115 (let ((inhibit-read-only t) lst line)
10116 (if (or (not (get-text-property (point) 'txt))
10117 (save-excursion
10118 (dotimes (n arg)
10119 (move-beginning-of-line (if backward 0 2))
10120 (push (not (get-text-property (point) 'txt)) lst))
10121 (delq nil lst)))
10122 (message "Cannot move line forward")
10123 (let ((end (save-excursion (move-beginning-of-line 2) (point))))
10124 (move-beginning-of-line 1)
10125 (setq line (buffer-substring (point) end))
10126 (delete-region (point) end)
10127 (move-beginning-of-line (funcall (if backward '1- '1+) arg))
10128 (insert line)
10129 (org-agenda-reapply-filters)
10130 (org-agenda-mark-clocking-task)
10131 (move-beginning-of-line 0)))))
10133 (defun org-agenda-drag-line-backward (arg)
10134 "Drag an agenda line backward by ARG lines."
10135 (interactive "p")
10136 (org-agenda-drag-line-forward arg t))
10138 ;;; Flagging notes
10140 (defun org-agenda-show-the-flagging-note ()
10141 "Display the flagging note in the other window.
10142 When called a second time in direct sequence, offer to remove the FLAGGING
10143 tag and (if present) the flagging note."
10144 (interactive)
10145 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
10146 (win (selected-window))
10147 note heading newhead)
10148 (unless hdmarker
10149 (user-error "No linked entry at point"))
10150 (if (and (eq this-command last-command)
10151 (y-or-n-p "Unflag and remove any flagging note? "))
10152 (progn
10153 (org-agenda-remove-flag hdmarker)
10154 (let ((win (get-buffer-window "*Flagging Note*")))
10155 (and win (delete-window win)))
10156 (message "Entry unflagged"))
10157 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
10158 (unless note
10159 (user-error "No flagging note"))
10160 (org-kill-new note)
10161 (org-switch-to-buffer-other-window "*Flagging Note*")
10162 (erase-buffer)
10163 (insert note)
10164 (goto-char (point-min))
10165 (while (re-search-forward "\\\\n" nil t)
10166 (replace-match "\n" t t))
10167 (goto-char (point-min))
10168 (select-window win)
10169 (message "%s" (substitute-command-keys "Flagging note pushed to \
10170 kill ring. Press `\\[org-agenda-show-the-flagging-note]' again to remove \
10171 tag and note")))))
10173 (defun org-agenda-remove-flag (marker)
10174 "Remove the FLAGGED tag and any flagging note in the entry."
10175 (let (newhead)
10176 (org-with-point-at marker
10177 (org-toggle-tag "FLAGGED" 'off)
10178 (org-entry-delete nil "THEFLAGGINGNOTE")
10179 (setq newhead (org-get-heading)))
10180 (org-agenda-change-all-lines newhead marker)
10181 (message "Entry unflagged")))
10183 (defun org-agenda-get-any-marker (&optional pos)
10184 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
10185 (get-text-property (or pos (point-at-bol)) 'org-marker)))
10187 ;;; Appointment reminders
10189 (defvar appt-time-msg-list) ; defined in appt.el
10191 ;;;###autoload
10192 (defun org-agenda-to-appt (&optional refresh filter &rest args)
10193 "Activate appointments found in `org-agenda-files'.
10195 With a `\\[universal-argument]' prefix, refresh the list of \
10196 appointments.
10198 If FILTER is t, interactively prompt the user for a regular
10199 expression, and filter out entries that don't match it.
10201 If FILTER is a string, use this string as a regular expression
10202 for filtering entries out.
10204 If FILTER is a function, filter out entries against which
10205 calling the function returns nil. This function takes one
10206 argument: an entry from `org-agenda-get-day-entries'.
10208 FILTER can also be an alist with the car of each cell being
10209 either `headline' or `category'. For example:
10211 \\='((headline \"IMPORTANT\")
10212 (category \"Work\"))
10214 will only add headlines containing IMPORTANT or headlines
10215 belonging to the \"Work\" category.
10217 ARGS are symbols indicating what kind of entries to consider.
10218 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
10219 \(i.e., deadlines and scheduled items with a hh:mm specification)
10220 and :timestamp entries. See the docstring of `org-diary' for
10221 details and examples.
10223 If an entry has a APPT_WARNTIME property, its value will be used
10224 to override `appt-message-warning-time'."
10225 (interactive "P")
10226 (when refresh (setq appt-time-msg-list nil))
10227 (when (eq filter t)
10228 (setq filter (read-from-minibuffer "Regexp filter: ")))
10229 (let* ((cnt 0) ; count added events
10230 (scope (or args '(:deadline* :scheduled* :timestamp)))
10231 (org-agenda-new-buffers nil)
10232 (org-deadline-warning-days 0)
10233 ;; Do not use `org-today' here because appt only takes
10234 ;; time and without date as argument, so it may pass wrong
10235 ;; information otherwise
10236 (today (org-date-to-gregorian
10237 (time-to-days nil)))
10238 (org-agenda-restrict nil)
10239 (files (org-agenda-files 'unrestricted)) entries file
10240 (org-agenda-buffer nil))
10241 ;; Get all entries which may contain an appt
10242 (org-agenda-prepare-buffers files)
10243 (while (setq file (pop files))
10244 (setq entries
10245 (delq nil
10246 (append entries
10247 (apply 'org-agenda-get-day-entries
10248 file today scope)))))
10249 ;; Map thru entries and find if we should filter them out
10250 (mapc
10251 (lambda (x)
10252 (let* ((evt (org-trim
10253 (replace-regexp-in-string
10254 org-link-bracket-re "\\2"
10255 (or (get-text-property 1 'txt x) ""))))
10256 (cat (get-text-property (1- (length x)) 'org-category x))
10257 (tod (get-text-property 1 'time-of-day x))
10258 (ok (or (null filter)
10259 (and (stringp filter) (string-match filter evt))
10260 (and (functionp filter) (funcall filter x))
10261 (and (listp filter)
10262 (let ((cat-filter (cadr (assq 'category filter)))
10263 (evt-filter (cadr (assq 'headline filter))))
10264 (or (and (stringp cat-filter)
10265 (string-match cat-filter cat))
10266 (and (stringp evt-filter)
10267 (string-match evt-filter evt)))))))
10268 (wrn (get-text-property 1 'warntime x)))
10269 ;; FIXME: Shall we remove text-properties for the appt text?
10270 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
10271 (when (and ok tod (not (string-match "\\`DONE\\|CANCELLED" evt)))
10272 (setq tod (concat "00" (number-to-string tod)))
10273 (setq tod (when (string-match
10274 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
10275 (concat (match-string 1 tod) ":"
10276 (match-string 2 tod))))
10277 (when (appt-add tod evt wrn)
10278 (setq cnt (1+ cnt))))))
10279 entries)
10280 (org-release-buffers org-agenda-new-buffers)
10281 (if (eq cnt 0)
10282 (message "No event to add")
10283 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
10285 (defun org-agenda-today-p (date)
10286 "Non nil when DATE means today.
10287 DATE is either a list of the form (month day year) or a number of
10288 days as returned by `calendar-absolute-from-gregorian' or
10289 `org-today'. This function considers `org-extend-today-until'
10290 when defining today."
10291 (eq (org-today)
10292 (if (consp date) (calendar-absolute-from-gregorian date) date)))
10294 (defun org-agenda-todo-yesterday (&optional arg)
10295 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
10296 (interactive "P")
10297 (let* ((org-use-effective-time t)
10298 (hour (nth 2 (decode-time (org-current-time))))
10299 (org-extend-today-until (1+ hour)))
10300 (org-agenda-todo arg)))
10302 (provide 'org-agenda)
10304 ;;; org-agenda.el ends here