test files: use cl- prefixed functions, and require cl-lib
[org-mode/org-tableheadings.git] / lisp / org-agenda.el
blob4f38e2e79fe50895db73e9aae80e98e6b134f138
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004-2018 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 'org)
50 (require 'org-macs)
52 (declare-function diary-add-to-list "diary-lib"
53 (date string specifier &optional marker globcolor literal))
54 (declare-function calendar-iso-to-absolute "cal-iso" (date))
55 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
56 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
57 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
58 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
59 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
60 (declare-function calendar-french-date-string "cal-french" (&optional date))
61 (declare-function calendar-goto-date "cal-move" (date))
62 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
63 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
64 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
65 (declare-function calendar-iso-from-absolute "cal-iso" (date))
66 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
67 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
68 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
69 (declare-function calendar-check-holidays "holidays" (date))
71 (declare-function org-columns-remove-overlays "org-colview" ())
72 (declare-function org-datetree-find-date-create "org-datetree"
73 (date &optional keep-restriction))
74 (declare-function org-columns-quit "org-colview" ())
75 (declare-function diary-date-display-form "diary-lib" (&optional type))
76 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
77 (declare-function org-habit-insert-consistency-graphs
78 "org-habit" (&optional line))
79 (declare-function org-is-habit-p "org-habit" (&optional pom))
80 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
81 (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
82 (declare-function org-agenda-columns "org-colview" ())
83 (declare-function org-add-archive-files "org-archive" (files))
84 (declare-function org-capture "org-capture" (&optional goto keys))
86 (defvar calendar-mode-map)
87 (defvar org-clock-current-task)
88 (defvar org-current-tag-alist)
89 (defvar org-mobile-force-id-on-agenda-items)
90 (defvar org-habit-show-habits)
91 (defvar org-habit-show-habits-only-for-today)
92 (defvar org-habit-show-all-today)
94 ;; Defined somewhere in this file, but used before definition.
95 (defvar org-agenda-buffer-name "*Org Agenda*")
96 (defvar org-agenda-overriding-header nil)
97 (defvar org-agenda-title-append nil)
98 (with-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
99 (with-no-warnings (defvar date)) ;; unprefixed, from calendar.el
100 (defvar original-date) ; dynamically scoped, calendar.el does scope this
102 (defvar org-agenda-undo-list nil
103 "List of undoable operations in the agenda since last refresh.")
104 (defvar org-agenda-pending-undo-list nil
105 "In a series of undo commands, this is the list of remaining undo items.")
107 (defcustom org-agenda-confirm-kill 1
108 "When set, remote killing from the agenda buffer needs confirmation.
109 When t, a confirmation is always needed. When a number N, confirmation is
110 only needed when the text to be killed contains more than N non-white lines."
111 :group 'org-agenda
112 :type '(choice
113 (const :tag "Never" nil)
114 (const :tag "Always" t)
115 (integer :tag "When more than N lines")))
117 (defcustom org-agenda-compact-blocks nil
118 "Non-nil means make the block agenda more compact.
119 This is done globally by leaving out lines like the agenda span
120 name and week number or the separator lines."
121 :group 'org-agenda
122 :type 'boolean)
124 (defcustom org-agenda-block-separator ?=
125 "The separator between blocks in the agenda.
126 If this is a string, it will be used as the separator, with a newline added.
127 If it is a character, it will be repeated to fill the window width.
128 If nil the separator is disabled. In `org-agenda-custom-commands' this
129 addresses the separator between the current and the previous block."
130 :group 'org-agenda
131 :type '(choice
132 (const :tag "Disabled" nil)
133 (character)
134 (string)))
136 (defgroup org-agenda-export nil
137 "Options concerning exporting agenda views in Org mode."
138 :tag "Org Agenda Export"
139 :group 'org-agenda)
141 (defcustom org-agenda-with-colors t
142 "Non-nil means use colors in agenda views."
143 :group 'org-agenda-export
144 :type 'boolean)
146 (defcustom org-agenda-exporter-settings nil
147 "Alist of variable/value pairs that should be active during agenda export.
148 This is a good place to set options for ps-print and for htmlize.
149 Note that the way this is implemented, the values will be evaluated
150 before assigned to the variables. So make sure to quote values you do
151 *not* want evaluated, for example
153 (setq org-agenda-exporter-settings
154 \\='((ps-print-color-p \\='black-white)))"
155 :group 'org-agenda-export
156 :type '(repeat
157 (list
158 (variable)
159 (sexp :tag "Value"))))
161 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
162 "Hook run in a temporary buffer before writing the agenda to an export file.
163 A useful function for this hook is `org-agenda-add-entry-text'."
164 :group 'org-agenda-export
165 :type 'hook
166 :options '(org-agenda-add-entry-text))
168 (defcustom org-agenda-add-entry-text-maxlines 0
169 "Maximum number of entry text lines to be added to agenda.
170 This is only relevant when `org-agenda-add-entry-text' is part of
171 `org-agenda-before-write-hook', which is the default.
172 When this is 0, nothing will happen. When it is greater than 0, it
173 specifies the maximum number of lines that will be added for each entry
174 that is listed in the agenda view.
176 Note that this variable is not used during display, only when exporting
177 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
178 and `org-agenda-entry-text-maxlines'."
179 :group 'org-agenda
180 :type 'integer)
182 (defcustom org-agenda-add-entry-text-descriptive-links t
183 "Non-nil means export org-links as descriptive links in agenda added text.
184 This variable applies to the text added to the agenda when
185 `org-agenda-add-entry-text-maxlines' is larger than 0.
186 When this variable nil, the URL will (also) be shown."
187 :group 'org-agenda
188 :type 'boolean)
190 (defcustom org-agenda-export-html-style nil
191 "The style specification for exported HTML Agenda files.
192 If this variable contains a string, it will replace the default <style>
193 section as produced by `htmlize'.
194 Since there are different ways of setting style information, this variable
195 needs to contain the full HTML structure to provide a style, including the
196 surrounding HTML tags. The style specifications should include definitions
197 the fonts used by the agenda, here is an example:
199 <style type=\"text/css\">
200 p { font-weight: normal; color: gray; }
201 .org-agenda-structure {
202 font-size: 110%;
203 color: #003399;
204 font-weight: 600;
206 .org-todo {
207 color: #cc6666;
208 font-weight: bold;
210 .org-agenda-done {
211 color: #339933;
213 .org-done {
214 color: #339933;
216 .title { text-align: center; }
217 .todo, .deadline { color: red; }
218 .done { color: green; }
219 </style>
221 or, if you want to keep the style in a file,
223 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
225 As the value of this option simply gets inserted into the HTML <head> header,
226 you can \"misuse\" it to also add other text to the header."
227 :group 'org-agenda-export
228 :group 'org-export-html
229 :type '(choice
230 (const nil)
231 (string)))
233 (defcustom org-agenda-persistent-filter nil
234 "When set, keep filters from one agenda view to the next."
235 :group 'org-agenda
236 :type 'boolean)
238 (defgroup org-agenda-custom-commands nil
239 "Options concerning agenda views in Org mode."
240 :tag "Org Agenda Custom Commands"
241 :group 'org-agenda)
243 (defconst org-sorting-choice
244 '(choice
245 (const time-up) (const time-down)
246 (const timestamp-up) (const timestamp-down)
247 (const scheduled-up) (const scheduled-down)
248 (const deadline-up) (const deadline-down)
249 (const ts-up) (const ts-down)
250 (const tsia-up) (const tsia-down)
251 (const category-keep) (const category-up) (const category-down)
252 (const tag-down) (const tag-up)
253 (const priority-up) (const priority-down)
254 (const todo-state-up) (const todo-state-down)
255 (const effort-up) (const effort-down)
256 (const habit-up) (const habit-down)
257 (const alpha-up) (const alpha-down)
258 (const user-defined-up) (const user-defined-down))
259 "Sorting choices.")
261 ;; Keep custom values for `org-agenda-filter-preset' compatible with
262 ;; the new variable `org-agenda-tag-filter-preset'.
263 (defvaralias 'org-agenda-filter-preset 'org-agenda-tag-filter-preset)
264 (defvaralias 'org-agenda-filter 'org-agenda-tag-filter)
266 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
267 "List of types searched for when creating the daily/weekly agenda.
268 This variable is a list of symbols that controls the types of
269 items that appear in the daily/weekly agenda. Allowed symbols in this
270 list are are
272 :timestamp List items containing a date stamp or date range matching
273 the selected date. This includes sexp entries in angular
274 brackets.
276 :sexp List entries resulting from plain diary-like sexps.
278 :deadline List deadline due on that date. When the date is today,
279 also list any deadlines past due, or due within
280 `org-deadline-warning-days'.
282 :deadline* Same as above, but only include the deadline if it has an
283 hour specification as [h]h:mm.
285 :scheduled List all items which are scheduled for the given date.
286 The diary for *today* also contains items which were
287 scheduled earlier and are not yet marked DONE.
289 :scheduled* Same as above, but only include the scheduled item if it
290 has an hour specification as [h]h:mm.
292 By default, all four non-starred types are turned on.
294 When :scheduled* or :deadline* are included, :schedule or :deadline
295 will be ignored.
297 Never set this variable globally using `setq', because then it
298 will apply to all future agenda commands. Instead, bind it with
299 `let' to scope it dynamically into the agenda-constructing
300 command. A good way to set it is through options in
301 `org-agenda-custom-commands'. For a more flexible (though
302 somewhat less efficient) way of determining what is included in
303 the daily/weekly agenda, see `org-agenda-skip-function'.")
305 (defconst org-agenda-custom-commands-local-options
306 `(repeat :tag "Local settings for this command. Remember to quote values"
307 (choice :tag "Setting"
308 (list :tag "Heading for this block"
309 (const org-agenda-overriding-header)
310 (string :tag "Headline"))
311 (list :tag "Files to be searched"
312 (const org-agenda-files)
313 (list
314 (const :format "" quote)
315 (repeat (file))))
316 (list :tag "Sorting strategy"
317 (const org-agenda-sorting-strategy)
318 (list
319 (const :format "" quote)
320 (repeat
321 ,org-sorting-choice)))
322 (list :tag "Prefix format"
323 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
324 (string))
325 (list :tag "Number of days in agenda"
326 (const org-agenda-span)
327 (list
328 (const :format "" quote)
329 (choice (const :tag "Day" day)
330 (const :tag "Week" week)
331 (const :tag "Fortnight" fortnight)
332 (const :tag "Month" month)
333 (const :tag "Year" year)
334 (integer :tag "Custom"))))
335 (list :tag "Fixed starting date"
336 (const org-agenda-start-day)
337 (string :value "2007-11-01"))
338 (list :tag "Start on day of week"
339 (const org-agenda-start-on-weekday)
340 (choice :value 1
341 (const :tag "Today" nil)
342 (integer :tag "Weekday No.")))
343 (list :tag "Include data from diary"
344 (const org-agenda-include-diary)
345 (boolean))
346 (list :tag "Deadline Warning days"
347 (const org-deadline-warning-days)
348 (integer :value 1))
349 (list :tag "Category filter preset"
350 (const org-agenda-category-filter-preset)
351 (list
352 (const :format "" quote)
353 (repeat
354 (string :tag "+category or -category"))))
355 (list :tag "Tags filter preset"
356 (const org-agenda-tag-filter-preset)
357 (list
358 (const :format "" quote)
359 (repeat
360 (string :tag "+tag or -tag"))))
361 (list :tag "Effort filter preset"
362 (const org-agenda-effort-filter-preset)
363 (list
364 (const :format "" quote)
365 (repeat
366 (string :tag "+=10 or -=10 or +<10 or ->10"))))
367 (list :tag "Regexp filter preset"
368 (const org-agenda-regexp-filter-preset)
369 (list
370 (const :format "" quote)
371 (repeat
372 (string :tag "+regexp or -regexp"))))
373 (list :tag "Set daily/weekly entry types"
374 (const org-agenda-entry-types)
375 (list
376 (const :format "" quote)
377 (set :greedy t :value ,org-agenda-entry-types
378 (const :deadline)
379 (const :scheduled)
380 (const :deadline*)
381 (const :scheduled*)
382 (const :timestamp)
383 (const :sexp))))
384 (list :tag "Standard skipping condition"
385 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
386 (const org-agenda-skip-function)
387 (list
388 (const :format "" quote)
389 (list
390 (choice
391 :tag "Skipping range"
392 (const :tag "Skip entry" org-agenda-skip-entry-if)
393 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
394 (repeat :inline t :tag "Conditions for skipping"
395 (choice
396 :tag "Condition type"
397 (list :tag "Regexp matches" :inline t
398 (const :format "" regexp)
399 (regexp))
400 (list :tag "Regexp does not match" :inline t
401 (const :format "" notregexp)
402 (regexp))
403 (list :tag "TODO state is" :inline t
404 (const todo)
405 (choice
406 (const :tag "Any not-done state" todo)
407 (const :tag "Any done state" done)
408 (const :tag "Any state" any)
409 (list :tag "Keyword list"
410 (const :format "" quote)
411 (repeat (string :tag "Keyword")))))
412 (list :tag "TODO state is not" :inline t
413 (const nottodo)
414 (choice
415 (const :tag "Any not-done state" todo)
416 (const :tag "Any done state" done)
417 (const :tag "Any state" any)
418 (list :tag "Keyword list"
419 (const :format "" quote)
420 (repeat (string :tag "Keyword")))))
421 (const :tag "scheduled" scheduled)
422 (const :tag "not scheduled" notscheduled)
423 (const :tag "deadline" deadline)
424 (const :tag "no deadline" notdeadline)
425 (const :tag "timestamp" timestamp)
426 (const :tag "no timestamp" nottimestamp))))))
427 (list :tag "Non-standard skipping condition"
428 :value (org-agenda-skip-function)
429 (const org-agenda-skip-function)
430 (sexp :tag "Function or form (quoted!)"))
431 (list :tag "Any variable"
432 (variable :tag "Variable")
433 (sexp :tag "Value (sexp)"))))
434 "Selection of examples for agenda command settings.
435 This will be spliced into the custom type of
436 `org-agenda-custom-commands'.")
439 (defcustom org-agenda-custom-commands
440 '(("n" "Agenda and all TODOs" ((agenda "") (alltodo ""))))
441 "Custom commands for the agenda.
442 \\<org-mode-map>
443 These commands will be offered on the splash screen displayed by the
444 agenda dispatcher `\\[org-agenda]'. Each entry is a list like this:
446 (key desc type match settings files)
448 key The key (one or more characters as a string) to be associated
449 with the command.
450 desc A description of the command, when omitted or nil, a default
451 description is built using MATCH.
452 type The command type, any of the following symbols:
453 agenda The daily/weekly agenda.
454 todo Entries with a specific TODO keyword, in all agenda files.
455 search Entries containing search words entry or headline.
456 tags Tags/Property/TODO match in all agenda files.
457 tags-todo Tags/P/T match in all agenda files, TODO entries only.
458 todo-tree Sparse tree of specific TODO keyword in *current* file.
459 tags-tree Sparse tree with all tags matches in *current* file.
460 occur-tree Occur sparse tree for *current* file.
461 ... A user-defined function.
462 match What to search for:
463 - a single keyword for TODO keyword searches
464 - a tags match expression for tags searches
465 - a word search expression for text searches.
466 - a regular expression for occur searches
467 For all other commands, this should be the empty string.
468 settings A list of option settings, similar to that in a let form, so like
469 this: ((opt1 val1) (opt2 val2) ...). The values will be
470 evaluated at the moment of execution, so quote them when needed.
471 files A list of files to write the produced agenda buffer to with
472 the command `org-store-agenda-views'.
473 If a file name ends in \".html\", an HTML version of the buffer
474 is written out. If it ends in \".ps\", a postscript version is
475 produced. Otherwise, only the plain text is written to the file.
477 You can also define a set of commands, to create a composite agenda buffer.
478 In this case, an entry looks like this:
480 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
482 where
484 desc A description string to be displayed in the dispatcher menu.
485 cmd An agenda command, similar to the above. However, tree commands
486 are not allowed, but instead you can get agenda and global todo list.
487 So valid commands for a set are:
488 (agenda \"\" settings)
489 (alltodo \"\" settings)
490 (stuck \"\" settings)
491 (todo \"match\" settings files)
492 (search \"match\" settings files)
493 (tags \"match\" settings files)
494 (tags-todo \"match\" settings files)
496 Each command can carry a list of options, and another set of options can be
497 given for the whole set of commands. Individual command options take
498 precedence over the general options.
500 When using several characters as key to a command, the first characters
501 are prefix commands. For the dispatcher to display useful information, you
502 should provide a description for the prefix, like
504 (setq org-agenda-custom-commands
505 \\='((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
506 (\"hl\" tags \"+HOME+Lisa\")
507 (\"hp\" tags \"+HOME+Peter\")
508 (\"hk\" tags \"+HOME+Kim\")))"
509 :group 'org-agenda-custom-commands
510 :type `(repeat
511 (choice :value ("x" "Describe command here" tags "" nil)
512 (list :tag "Single command"
513 (string :tag "Access Key(s) ")
514 (option (string :tag "Description"))
515 (choice
516 (const :tag "Agenda" agenda)
517 (const :tag "TODO list" alltodo)
518 (const :tag "Search words" search)
519 (const :tag "Stuck projects" stuck)
520 (const :tag "Tags/Property match (all agenda files)" tags)
521 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
522 (const :tag "TODO keyword search (all agenda files)" todo)
523 (const :tag "Tags sparse tree (current buffer)" tags-tree)
524 (const :tag "TODO keyword tree (current buffer)" todo-tree)
525 (const :tag "Occur tree (current buffer)" occur-tree)
526 (sexp :tag "Other, user-defined function"))
527 (string :tag "Match (only for some commands)")
528 ,org-agenda-custom-commands-local-options
529 (option (repeat :tag "Export" (file :tag "Export to"))))
530 (list :tag "Command series, all agenda files"
531 (string :tag "Access Key(s)")
532 (string :tag "Description ")
533 (repeat :tag "Component"
534 (choice
535 (list :tag "Agenda"
536 (const :format "" agenda)
537 (const :tag "" :format "" "")
538 ,org-agenda-custom-commands-local-options)
539 (list :tag "TODO list (all keywords)"
540 (const :format "" alltodo)
541 (const :tag "" :format "" "")
542 ,org-agenda-custom-commands-local-options)
543 (list :tag "Search words"
544 (const :format "" search)
545 (string :tag "Match")
546 ,org-agenda-custom-commands-local-options)
547 (list :tag "Stuck projects"
548 (const :format "" stuck)
549 (const :tag "" :format "" "")
550 ,org-agenda-custom-commands-local-options)
551 (list :tag "Tags search"
552 (const :format "" tags)
553 (string :tag "Match")
554 ,org-agenda-custom-commands-local-options)
555 (list :tag "Tags search, TODO entries only"
556 (const :format "" tags-todo)
557 (string :tag "Match")
558 ,org-agenda-custom-commands-local-options)
559 (list :tag "TODO keyword search"
560 (const :format "" todo)
561 (string :tag "Match")
562 ,org-agenda-custom-commands-local-options)
563 (list :tag "Other, user-defined function"
564 (symbol :tag "function")
565 (string :tag "Match")
566 ,org-agenda-custom-commands-local-options)))
568 (repeat :tag "Settings for entire command set"
569 (list (variable :tag "Any variable")
570 (sexp :tag "Value")))
571 (option (repeat :tag "Export" (file :tag "Export to"))))
572 (cons :tag "Prefix key documentation"
573 (string :tag "Access Key(s)")
574 (string :tag "Description ")))))
576 (defcustom org-agenda-query-register ?o
577 "The register holding the current query string.
578 The purpose of this is that if you construct a query string interactively,
579 you can then use it to define a custom command."
580 :group 'org-agenda-custom-commands
581 :type 'character)
583 (defcustom org-stuck-projects
584 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
585 "How to identify stuck projects.
586 This is a list of four items:
587 1. A tags/todo/property matcher string that is used to identify a project.
588 See the manual for a description of tag and property searches.
589 The entire tree below a headline matched by this is considered one project.
590 2. A list of TODO keywords identifying non-stuck projects.
591 If the project subtree contains any headline with one of these todo
592 keywords, the project is considered to be not stuck. If you specify
593 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
594 3. A list of tags identifying non-stuck projects.
595 If the project subtree contains any headline with one of these tags,
596 the project is considered to be not stuck. If you specify \"*\" as
597 a tag, any tag will mark the project unstuck. Note that this is about
598 the explicit presence of a tag somewhere in the subtree, inherited
599 tags do not count here. If inherited tags make a project not stuck,
600 use \"-TAG\" in the tags part of the matcher under (1.) above.
601 4. An arbitrary regular expression matching non-stuck projects.
603 If the project turns out to be not stuck, search continues also in the
604 subtree to see if any of the subtasks have project status.
606 See also the variable `org-tags-match-list-sublevels' which applies
607 to projects matched by this search as well.
609 After defining this variable, you may use `org-agenda-list-stuck-projects'
610 \(bound to `\\[org-agenda] #') to produce the list."
611 :group 'org-agenda-custom-commands
612 :type '(list
613 (string :tag "Tags/TODO match to identify a project")
614 (repeat :tag "Projects are *not* stuck if they have an entry with \
615 TODO keyword any of" (string))
616 (repeat :tag "Projects are *not* stuck if they have an entry with \
617 TAG being any of" (string))
618 (regexp :tag "Projects are *not* stuck if this regexp matches inside \
619 the subtree")))
621 (defgroup org-agenda-skip nil
622 "Options concerning skipping parts of agenda files."
623 :tag "Org Agenda Skip"
624 :group 'org-agenda)
626 (defcustom org-agenda-skip-function-global nil
627 "Function to be called at each match during agenda construction.
628 If this function returns nil, the current match should not be skipped.
629 If the function decided to skip an agenda match, is must return the
630 buffer position from which the search should be continued.
631 This may also be a Lisp form, which will be evaluated.
633 This variable will be applied to every agenda match, including
634 tags/property searches and TODO lists. So try to make the test function
635 do its checking as efficiently as possible. To implement a skipping
636 condition just for specific agenda commands, use the variable
637 `org-agenda-skip-function' which can be set in the options section
638 of custom agenda commands."
639 :group 'org-agenda-skip
640 :type 'sexp)
642 (defgroup org-agenda-daily/weekly nil
643 "Options concerning the daily/weekly agenda."
644 :tag "Org Agenda Daily/Weekly"
645 :group 'org-agenda)
646 (defgroup org-agenda-todo-list nil
647 "Options concerning the global todo list agenda view."
648 :tag "Org Agenda Todo List"
649 :group 'org-agenda)
650 (defgroup org-agenda-match-view nil
651 "Options concerning the general tags/property/todo match agenda view."
652 :tag "Org Agenda Match View"
653 :group 'org-agenda)
654 (defgroup org-agenda-search-view nil
655 "Options concerning the search agenda view."
656 :tag "Org Agenda Search View"
657 :group 'org-agenda)
659 (defvar org-agenda-archives-mode nil
660 "Non-nil means the agenda will include archived items.
661 If this is the symbol `trees', trees in the selected agenda scope
662 that are marked with the ARCHIVE tag will be included anyway. When this is
663 t, also all archive files associated with the current selection of agenda
664 files will be included.")
666 (defcustom org-agenda-restriction-lock-highlight-subtree t
667 "Non-nil means highlight the whole subtree when restriction is active.
668 Otherwise only highlight the headline. Highlighting the whole subtree is
669 useful to ensure no edits happen beyond the restricted region."
670 :group 'org-agenda
671 :type 'boolean)
673 (defcustom org-agenda-skip-comment-trees t
674 "Non-nil means skip trees that start with the COMMENT keyword.
675 When nil, these trees are also scanned by agenda commands."
676 :group 'org-agenda-skip
677 :type 'boolean)
679 (defcustom org-agenda-todo-list-sublevels t
680 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
681 When nil, the sublevels of a TODO entry are not checked, resulting in
682 potentially much shorter TODO lists."
683 :group 'org-agenda-skip
684 :group 'org-agenda-todo-list
685 :type 'boolean)
687 (defcustom org-agenda-todo-ignore-with-date nil
688 "Non-nil means don't show entries with a date in the global todo list.
689 You can use this if you prefer to mark mere appointments with a TODO keyword,
690 but don't want them to show up in the TODO list.
691 When this is set, it also covers deadlines and scheduled items, the settings
692 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
693 will be ignored.
694 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
695 :group 'org-agenda-skip
696 :group 'org-agenda-todo-list
697 :type 'boolean)
699 (defcustom org-agenda-todo-ignore-timestamp nil
700 "Non-nil means don't show entries with a timestamp.
701 This applies when creating the global todo list.
702 Valid values are:
704 past Don't show entries for today or in the past.
706 future Don't show entries with a timestamp in the future.
707 The idea behind this is that if it has a future
708 timestamp, you don't want to think about it until the
709 date.
711 all Don't show any entries with a timestamp in the global todo list.
712 The idea behind this is that by setting a timestamp, you
713 have already \"taken care\" of this item.
715 This variable can also have an integer as a value. If positive (N),
716 todos with a timestamp N or more days in the future will be ignored. If
717 negative (-N), todos with a timestamp N or more days in the past will be
718 ignored. If 0, todos with a timestamp either today or in the future will
719 be ignored. For example, a value of -1 will exclude todos with a
720 timestamp in the past (yesterday or earlier), while a value of 7 will
721 exclude todos with a timestamp a week or more in the future.
723 See also `org-agenda-todo-ignore-with-date'.
724 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
725 to make his option also apply to the tags-todo list."
726 :group 'org-agenda-skip
727 :group 'org-agenda-todo-list
728 :version "24.1"
729 :type '(choice
730 (const :tag "Ignore future timestamp todos" future)
731 (const :tag "Ignore past or present timestamp todos" past)
732 (const :tag "Ignore all timestamp todos" all)
733 (const :tag "Show timestamp todos" nil)
734 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
736 (defcustom org-agenda-todo-ignore-scheduled nil
737 "Non-nil means, ignore some scheduled TODO items when making TODO list.
738 This applies when creating the global todo list.
739 Valid values are:
741 past Don't show entries scheduled today or in the past.
743 future Don't show entries scheduled in the future.
744 The idea behind this is that by scheduling it, you don't want to
745 think about it until the scheduled date.
747 all Don't show any scheduled entries in the global todo list.
748 The idea behind this is that by scheduling it, you have already
749 \"taken care\" of this item.
751 t Same as `all', for backward compatibility.
753 This variable can also have an integer as a value. See
754 `org-agenda-todo-ignore-timestamp' for more details.
756 See also `org-agenda-todo-ignore-with-date'.
757 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
758 to make his option also apply to the tags-todo list."
759 :group 'org-agenda-skip
760 :group 'org-agenda-todo-list
761 :type '(choice
762 (const :tag "Ignore future-scheduled todos" future)
763 (const :tag "Ignore past- or present-scheduled todos" past)
764 (const :tag "Ignore all scheduled todos" all)
765 (const :tag "Ignore all scheduled todos (compatibility)" t)
766 (const :tag "Show scheduled todos" nil)
767 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
769 (defcustom org-agenda-todo-ignore-deadlines nil
770 "Non-nil means ignore some deadline TODO items when making TODO list.
772 There are different motivations for using different values, please think
773 carefully when configuring this variable.
775 This applies when creating the global TODO list.
777 Valid values are:
779 near Don't show near deadline entries. A deadline is near when it is
780 closer than `org-deadline-warning-days' days. The idea behind this
781 is that such items will appear in the agenda anyway.
783 far Don't show TODO entries where a deadline has been defined, but
784 is not going to happen anytime soon. This is useful if you want to use
785 the TODO list to figure out what to do now.
787 past Don't show entries with a deadline timestamp for today or in the past.
789 future Don't show entries with a deadline timestamp in the future, not even
790 when they become `near' ones. Use it with caution.
792 all Ignore all TODO entries that do have a deadline.
794 t Same as `near', for backward compatibility.
796 This variable can also have an integer as a value. See
797 `org-agenda-todo-ignore-timestamp' for more details.
799 See also `org-agenda-todo-ignore-with-date'.
800 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
801 to make his option also apply to the tags-todo list."
802 :group 'org-agenda-skip
803 :group 'org-agenda-todo-list
804 :type '(choice
805 (const :tag "Ignore near deadlines" near)
806 (const :tag "Ignore near deadlines (compatibility)" t)
807 (const :tag "Ignore far deadlines" far)
808 (const :tag "Ignore all TODOs with a deadlines" all)
809 (const :tag "Show all TODOs, even if they have a deadline" nil)
810 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
812 (defcustom org-agenda-todo-ignore-time-comparison-use-seconds nil
813 "Time unit to use when possibly ignoring an agenda item.
815 See the docstring of various `org-agenda-todo-ignore-*' options.
816 The default is to compare time stamps using days. An item is thus
817 considered to be in the future if it is at least one day after today.
818 Non-nil means to compare time stamps using seconds. An item is then
819 considered future if it has a time value later than current time."
820 :group 'org-agenda-skip
821 :group 'org-agenda-todo-list
822 :version "24.4"
823 :package-version '(Org . "8.0")
824 :type '(choice
825 (const :tag "Compare time with days" nil)
826 (const :tag "Compare time with seconds" t)))
828 (defcustom org-agenda-tags-todo-honor-ignore-options nil
829 "Non-nil means honor todo-list ignores options also in tags-todo search.
830 The variables
831 `org-agenda-todo-ignore-with-date',
832 `org-agenda-todo-ignore-timestamp',
833 `org-agenda-todo-ignore-scheduled',
834 `org-agenda-todo-ignore-deadlines'
835 make the global TODO list skip entries that have time stamps of certain
836 kinds. If this option is set, the same options will also apply for the
837 tags-todo search, which is the general tags/property matcher
838 restricted to unfinished TODO entries only."
839 :group 'org-agenda-skip
840 :group 'org-agenda-todo-list
841 :group 'org-agenda-match-view
842 :type 'boolean)
844 (defcustom org-agenda-skip-scheduled-if-done nil
845 "Non-nil means don't show scheduled items in agenda when they are done.
846 This is relevant for the daily/weekly agenda, not for the TODO list. It
847 applies only to the actual date of the scheduling. Warnings about an item
848 with a past scheduling dates are always turned off when the item is DONE."
849 :group 'org-agenda-skip
850 :group 'org-agenda-daily/weekly
851 :type 'boolean)
853 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
854 "Non-nil means skip scheduling line if same entry shows because of deadline.
856 In the agenda of today, an entry can show up multiple times
857 because it is both scheduled and has a nearby deadline, and maybe
858 a plain time stamp as well.
860 When this variable is nil, the entry will be shown several times.
862 When set to t, then only the deadline is shown and the fact that
863 the entry is scheduled today or was scheduled previously is not
864 shown.
866 When set to the symbol `not-today', skip scheduled previously,
867 but not scheduled today.
869 When set to the symbol `repeated-after-deadline', skip scheduled
870 items if they are repeated beyond the current deadline."
871 :group 'org-agenda-skip
872 :group 'org-agenda-daily/weekly
873 :type '(choice
874 (const :tag "Never" nil)
875 (const :tag "Always" t)
876 (const :tag "Not when scheduled today" not-today)
877 (const :tag "When repeated past deadline" repeated-after-deadline)))
879 (defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
880 "Non-nil means skip timestamp line if same entry shows because of deadline.
881 In the agenda of today, an entry can show up multiple times
882 because it has both a plain timestamp and has a nearby deadline.
883 When this variable is t, then only the deadline is shown and the
884 fact that the entry has a timestamp for or including today is not
885 shown. When this variable is nil, the entry will be shown
886 several times."
887 :group 'org-agenda-skip
888 :group 'org-agenda-daily/weekly
889 :version "24.1"
890 :type '(choice
891 (const :tag "Never" nil)
892 (const :tag "Always" t)))
894 (defcustom org-agenda-skip-deadline-if-done nil
895 "Non-nil means don't show deadlines when the corresponding item is done.
896 When nil, the deadline is still shown and should give you a happy feeling.
897 This is relevant for the daily/weekly agenda. It applies only to the
898 actual date of the deadline. Warnings about approaching and past-due
899 deadlines are always turned off when the item is DONE."
900 :group 'org-agenda-skip
901 :group 'org-agenda-daily/weekly
902 :type 'boolean)
904 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
905 "Non-nil means skip deadline prewarning when entry is also scheduled.
906 This will apply on all days where a prewarning for the deadline would
907 be shown, but not at the day when the entry is actually due. On that day,
908 the deadline will be shown anyway.
909 This variable may be set to nil, t, the symbol `pre-scheduled',
910 or a number which will then give the number of days before the actual
911 deadline when the prewarnings should resume. The symbol `pre-scheduled'
912 eliminates the deadline prewarning only prior to the scheduled date.
913 This can be used in a workflow where the first showing of the deadline will
914 trigger you to schedule it, and then you don't want to be reminded of it
915 because you will take care of it on the day when scheduled."
916 :group 'org-agenda-skip
917 :group 'org-agenda-daily/weekly
918 :version "24.1"
919 :type '(choice
920 (const :tag "Always show prewarning" nil)
921 (const :tag "Remove prewarning prior to scheduled date" pre-scheduled)
922 (const :tag "Remove prewarning if entry is scheduled" t)
923 (integer :tag "Restart prewarning N days before deadline")))
925 (defcustom org-agenda-skip-scheduled-delay-if-deadline nil
926 "Non-nil means skip scheduled delay when entry also has a deadline.
927 This variable may be set to nil, t, the symbol `post-deadline',
928 or a number which will then give the number of days after the actual
929 scheduled date when the delay should expire. The symbol `post-deadline'
930 eliminates the schedule delay when the date is posterior to the deadline."
931 :group 'org-agenda-skip
932 :group 'org-agenda-daily/weekly
933 :version "24.4"
934 :package-version '(Org . "8.0")
935 :type '(choice
936 (const :tag "Always honor delay" nil)
937 (const :tag "Ignore delay if posterior to the deadline" post-deadline)
938 (const :tag "Ignore delay if entry has a deadline" t)
939 (integer :tag "Honor delay up until N days after the scheduled date")))
941 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
942 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
943 When non-nil, after the search for timestamps has matched once in an
944 entry, the rest of the entry will not be searched."
945 :group 'org-agenda-skip
946 :type 'boolean)
948 (defcustom org-agenda-skip-timestamp-if-done nil
949 "Non-nil means don't select item by timestamp or -range if it is DONE."
950 :group 'org-agenda-skip
951 :group 'org-agenda-daily/weekly
952 :type 'boolean)
954 (defcustom org-agenda-dim-blocked-tasks t
955 "Non-nil means dim blocked tasks in the agenda display.
956 This causes some overhead during agenda construction, but if you
957 have turned on `org-enforce-todo-dependencies',
958 `org-enforce-todo-checkbox-dependencies', or any other blocking
959 mechanism, this will create useful feedback in the agenda.
961 Instead of t, this variable can also have the value `invisible'.
962 Then blocked tasks will be invisible and only become visible when
963 they become unblocked. An exemption to this behavior is when a task is
964 blocked because of unchecked checkboxes below it. Since checkboxes do
965 not show up in the agenda views, making this task invisible you remove any
966 trace from agenda views that there is something to do. Therefore, a task
967 that is blocked because of checkboxes will never be made invisible, it
968 will only be dimmed."
969 :group 'org-agenda-daily/weekly
970 :group 'org-agenda-todo-list
971 :version "24.3"
972 :type '(choice
973 (const :tag "Do not dim" nil)
974 (const :tag "Dim to a gray face" t)
975 (const :tag "Make invisible" invisible)))
977 (defgroup org-agenda-startup nil
978 "Options concerning initial settings in the Agenda in Org Mode."
979 :tag "Org Agenda Startup"
980 :group 'org-agenda)
982 (defcustom org-agenda-menu-show-matcher t
983 "Non-nil means show the match string in the agenda dispatcher menu.
984 When nil, the matcher string is not shown, but is put into the help-echo
985 property so than moving the mouse over the command shows it.
986 Setting it to nil is good if matcher strings are very long and/or if
987 you want to use two-columns display (see `org-agenda-menu-two-columns')."
988 :group 'org-agenda
989 :version "24.1"
990 :type 'boolean)
992 (defcustom org-agenda-menu-two-columns nil
993 "Non-nil means, use two columns to show custom commands in the dispatcher.
994 If you use this, you probably want to set `org-agenda-menu-show-matcher'
995 to nil."
996 :group 'org-agenda
997 :version "24.1"
998 :type 'boolean)
1000 (defcustom org-agenda-finalize-hook nil
1001 "Hook run just before displaying an agenda buffer.
1002 The buffer is still writable when the hook is called.
1004 You can modify some of the buffer substrings but you should be
1005 extra careful not to modify the text properties of the agenda
1006 headlines as the agenda display heavily relies on them."
1007 :group 'org-agenda-startup
1008 :type 'hook)
1010 (defcustom org-agenda-mouse-1-follows-link nil
1011 "Non-nil means mouse-1 on a link will follow the link in the agenda.
1012 A longer mouse click will still set point. Needs to be set
1013 before org.el is loaded."
1014 :group 'org-agenda-startup
1015 :type 'boolean)
1017 (defcustom org-agenda-start-with-follow-mode nil
1018 "The initial value of follow mode in a newly created agenda window."
1019 :group 'org-agenda-startup
1020 :type 'boolean)
1022 (defcustom org-agenda-follow-indirect nil
1023 "Non-nil means `org-agenda-follow-mode' displays only the
1024 current item's tree, in an indirect buffer."
1025 :group 'org-agenda
1026 :version "24.1"
1027 :type 'boolean)
1029 (defcustom org-agenda-show-outline-path t
1030 "Non-nil means show outline path in echo area after line motion."
1031 :group 'org-agenda-startup
1032 :type 'boolean)
1034 (defcustom org-agenda-start-with-entry-text-mode nil
1035 "The initial value of entry-text-mode in a newly created agenda window."
1036 :group 'org-agenda-startup
1037 :type 'boolean)
1039 (defcustom org-agenda-entry-text-maxlines 5
1040 "Number of text lines to be added when `E' is pressed in the agenda.
1042 Note that this variable only used during agenda display. To add entry text
1043 when exporting the agenda, configure the variable
1044 `org-agenda-add-entry-text-maxlines'."
1045 :group 'org-agenda
1046 :type 'integer)
1048 (defcustom org-agenda-entry-text-exclude-regexps nil
1049 "List of regular expressions to clean up entry text.
1050 The complete matches of all regular expressions in this list will be
1051 removed from entry text before it is shown in the agenda."
1052 :group 'org-agenda
1053 :type '(repeat (regexp)))
1055 (defcustom org-agenda-entry-text-leaders " > "
1056 "Text prepended to the entry text in agenda buffers."
1057 :version "24.4"
1058 :package-version '(Org . "8.0")
1059 :group 'org-agenda
1060 :type 'string)
1062 (defvar org-agenda-entry-text-cleanup-hook nil
1063 "Hook that is run after basic cleanup of entry text to be shown in agenda.
1064 This cleanup is done in a temporary buffer, so the function may inspect and
1065 change the entire buffer.
1066 Some default stuff like drawers and scheduling/deadline dates will already
1067 have been removed when this is called, as will any matches for regular
1068 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
1070 (defvar org-agenda-include-inactive-timestamps nil
1071 "Non-nil means include inactive time stamps in agenda.
1072 Dynamically scoped.")
1074 (defgroup org-agenda-windows nil
1075 "Options concerning the windows used by the Agenda in Org Mode."
1076 :tag "Org Agenda Windows"
1077 :group 'org-agenda)
1079 (defcustom org-agenda-window-setup 'reorganize-frame
1080 "How the agenda buffer should be displayed.
1081 Possible values for this option are:
1083 current-window Show agenda in the current window, keeping all other windows.
1084 other-window Use `switch-to-buffer-other-window' to display agenda.
1085 only-window Show agenda, deleting all other windows.
1086 reorganize-frame Show only two windows on the current frame, the current
1087 window and the agenda.
1088 other-frame Use `switch-to-buffer-other-frame' to display agenda.
1089 Also, when exiting the agenda, kill that frame.
1090 See also the variable `org-agenda-restore-windows-after-quit'."
1091 :group 'org-agenda-windows
1092 :type '(choice
1093 (const current-window)
1094 (const other-frame)
1095 (const other-window)
1096 (const only-window)
1097 (const reorganize-frame)))
1099 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
1100 "The min and max height of the agenda window as a fraction of frame height.
1101 The value of the variable is a cons cell with two numbers between 0 and 1.
1102 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
1103 :group 'org-agenda-windows
1104 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
1106 (defcustom org-agenda-restore-windows-after-quit nil
1107 "Non-nil means restore window configuration upon exiting agenda.
1108 Before the window configuration is changed for displaying the agenda,
1109 the current status is recorded. When the agenda is exited with
1110 `q' or `x' and this option is set, the old state is restored. If
1111 `org-agenda-window-setup' is `other-frame', the value of this
1112 option will be ignored."
1113 :group 'org-agenda-windows
1114 :type 'boolean)
1116 (defcustom org-agenda-span 'week
1117 "Number of days to include in overview display.
1118 Can be day, week, month, year, or any number of days.
1119 Custom commands can set this variable in the options section."
1120 :group 'org-agenda-daily/weekly
1121 :type '(choice (const :tag "Day" day)
1122 (const :tag "Week" week)
1123 (const :tag "Fortnight" fortnight)
1124 (const :tag "Month" month)
1125 (const :tag "Year" year)
1126 (integer :tag "Custom")))
1128 (defcustom org-agenda-start-on-weekday 1
1129 "Non-nil means start the overview always on the specified weekday.
1130 0 denotes Sunday, 1 denotes Monday, etc.
1131 When nil, always start on the current day.
1132 Custom commands can set this variable in the options section."
1133 :group 'org-agenda-daily/weekly
1134 :type '(choice (const :tag "Today" nil)
1135 (integer :tag "Weekday No.")))
1137 (defcustom org-agenda-show-all-dates t
1138 "Non-nil means `org-agenda' shows every day in the selected range.
1139 When nil, only the days which actually have entries are shown."
1140 :group 'org-agenda-daily/weekly
1141 :type 'boolean)
1143 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1144 "Format string for displaying dates in the agenda.
1145 Used by the daily/weekly agenda. This should be a format string
1146 understood by `format-time-string', or a function returning the
1147 formatted date as a string. The function must take a single
1148 argument, a calendar-style date list like (month day year)."
1149 :group 'org-agenda-daily/weekly
1150 :type '(choice
1151 (string :tag "Format string")
1152 (function :tag "Function")))
1154 (defun org-agenda-format-date-aligned (date)
1155 "Format a DATE string for display in the daily/weekly agenda.
1156 This function makes sure that dates are aligned for easy reading."
1157 (require 'cal-iso)
1158 (let* ((dayname (calendar-day-name date))
1159 (day (cadr date))
1160 (day-of-week (calendar-day-of-week date))
1161 (month (car date))
1162 (monthname (calendar-month-name month))
1163 (year (nth 2 date))
1164 (iso-week (org-days-to-iso-week
1165 (calendar-absolute-from-gregorian date)))
1166 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1167 (1- year))
1168 ((and (= month 12) (<= iso-week 1))
1169 (1+ year))
1170 (t year)))
1171 (weekstring (if (= day-of-week 1)
1172 (format " W%02d" iso-week)
1173 "")))
1174 (format "%-10s %2d %s %4d%s"
1175 dayname day monthname year weekstring)))
1177 (defcustom org-agenda-time-leading-zero nil
1178 "Non-nil means use leading zero for military times in agenda.
1179 For example, 9:30am would become 09:30 rather than 9:30."
1180 :group 'org-agenda-daily/weekly
1181 :version "24.1"
1182 :type 'boolean)
1184 (defcustom org-agenda-timegrid-use-ampm nil
1185 "When set, show AM/PM style timestamps on the timegrid."
1186 :group 'org-agenda
1187 :version "24.1"
1188 :type 'boolean)
1190 (defun org-agenda-time-of-day-to-ampm (time)
1191 "Convert TIME of a string like \"13:45\" to an AM/PM style time string."
1192 (let* ((hour-number (string-to-number (substring time 0 -3)))
1193 (minute (substring time -2))
1194 (ampm "am"))
1195 (cond
1196 ((equal hour-number 12)
1197 (setq ampm "pm"))
1198 ((> hour-number 12)
1199 (setq ampm "pm")
1200 (setq hour-number (- hour-number 12))))
1201 (concat
1202 (if org-agenda-time-leading-zero
1203 (format "%02d" hour-number)
1204 (format "%02s" (number-to-string hour-number)))
1205 ":" minute ampm)))
1207 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1208 "Conditionally convert TIME to AM/PM format based on `org-agenda-timegrid-use-ampm'."
1209 (if org-agenda-timegrid-use-ampm
1210 (org-agenda-time-of-day-to-ampm time)
1211 time))
1213 (defcustom org-agenda-weekend-days '(6 0)
1214 "Which days are weekend?
1215 These days get the special face `org-agenda-date-weekend' in the agenda."
1216 :group 'org-agenda-daily/weekly
1217 :type '(set :greedy t
1218 (const :tag "Monday" 1)
1219 (const :tag "Tuesday" 2)
1220 (const :tag "Wednesday" 3)
1221 (const :tag "Thursday" 4)
1222 (const :tag "Friday" 5)
1223 (const :tag "Saturday" 6)
1224 (const :tag "Sunday" 0)))
1226 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1227 "Non-nil means jump to today when moving a past date forward in time.
1228 When using S-right in the agenda to move a a date forward, and the date
1229 stamp currently points to the past, the first key press will move it
1230 to today. WHen nil, just move one day forward even if the date stays
1231 in the past."
1232 :group 'org-agenda-daily/weekly
1233 :version "24.1"
1234 :type 'boolean)
1236 (defcustom org-agenda-include-diary nil
1237 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1238 Custom commands can set this variable in the options section."
1239 :group 'org-agenda-daily/weekly
1240 :type 'boolean)
1242 (defcustom org-agenda-include-deadlines t
1243 "If non-nil, include entries within their deadline warning period.
1244 Custom commands can set this variable in the options section."
1245 :group 'org-agenda-daily/weekly
1246 :version "24.1"
1247 :type 'boolean)
1249 (defcustom org-agenda-show-future-repeats t
1250 "Non-nil shows repeated entries in the future part of the agenda.
1251 When set to the symbol `next' only the first future repeat is shown."
1252 :group 'org-agenda-daily/weekly
1253 :type '(choice
1254 (const :tag "Show all repeated entries" t)
1255 (const :tag "Show next repeated entry" next)
1256 (const :tag "Do not show repeated entries" nil))
1257 :version "26.1"
1258 :package-version '(Org . "9.1")
1259 :safe #'symbolp)
1261 (defcustom org-agenda-prefer-last-repeat nil
1262 "Non-nil sets date for repeated entries to their last repeat.
1264 When nil, display SCHEDULED and DEADLINE dates at their base
1265 date, and in today's agenda, as a reminder. Display plain
1266 time-stamps, on the other hand, at every repeat date in the past
1267 in addition to the base date.
1269 When non-nil, show a repeated entry at its latest repeat date,
1270 possibly being today even if it wasn't marked as done. This
1271 setting is useful if you do not always mark repeated entries as
1272 done and, yet, consider that reaching repeat date starts the task
1273 anew.
1275 When set to a list of strings, prefer last repeats only for
1276 entries with these TODO keywords."
1277 :group 'org-agenda-daily/weekly
1278 :type '(choice
1279 (const :tag "Prefer last repeat" t)
1280 (const :tag "Prefer base date" nil)
1281 (repeat :tag "Prefer last repeat for entries with these TODO keywords"
1282 (string :tag "TODO keyword")))
1283 :version "26.1"
1284 :package-version '(Org . "9.1")
1285 :safe (lambda (x) (or (booleanp x) (consp x))))
1287 (defcustom org-scheduled-past-days 10000
1288 "Number of days to continue listing scheduled items not marked DONE.
1289 When an item is scheduled on a date, it shows up in the agenda on
1290 this day and will be listed until it is marked done or for the
1291 number of days given here."
1292 :group 'org-agenda-daily/weekly
1293 :type 'integer
1294 :safe 'integerp)
1296 (defcustom org-deadline-past-days 10000
1297 "Number of days to warn about missed deadlines.
1298 When an item has deadline on a date, it shows up in the agenda on
1299 this day and will appear as a reminder until it is marked DONE or
1300 for the number of days given here."
1301 :group 'org-agenda-daily/weekly
1302 :type 'integer
1303 :version "26.1"
1304 :package-version '(Org . "9.1")
1305 :safe 'integerp)
1307 (defcustom org-agenda-log-mode-items '(closed clock)
1308 "List of items that should be shown in agenda log mode.
1309 \\<org-agenda-mode-map>\
1310 This list may contain the following symbols:
1312 closed Show entries that have been closed on that day.
1313 clock Show entries that have received clocked time on that day.
1314 state Show all logged state changes.
1315 Note that instead of changing this variable, you can also press \
1316 `\\[universal-argument] \\[org-agenda-log-mode]' in
1317 the agenda to display all available LOG items temporarily."
1318 :group 'org-agenda-daily/weekly
1319 :type '(set :greedy t (const closed) (const clock) (const state)))
1321 (defcustom org-agenda-clock-consistency-checks
1322 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1323 :gap-ok-around ("4:00")
1324 :default-face ((:background "DarkRed") (:foreground "white"))
1325 :overlap-face nil :gap-face nil :no-end-time-face nil
1326 :long-face nil :short-face nil)
1327 "This is a property list, with the following keys:
1329 :max-duration Mark clocking chunks that are longer than this time.
1330 This is a time string like \"HH:MM\", or the number
1331 of minutes as an integer.
1333 :min-duration Mark clocking chunks that are shorter that this.
1334 This is a time string like \"HH:MM\", or the number
1335 of minutes as an integer.
1337 :max-gap Mark gaps between clocking chunks that are longer than
1338 this duration. A number of minutes, or a string
1339 like \"HH:MM\".
1341 :gap-ok-around List of times during the day which are usually not working
1342 times. When a gap is detected, but the gap contains any
1343 of these times, the gap is *not* reported. For example,
1344 if this is (\"4:00\" \"13:00\") then gaps that contain
1345 4:00 in the morning (i.e. the night) and 13:00
1346 (i.e. a typical lunch time) do not cause a warning.
1347 You should have at least one time during the night in this
1348 list, or otherwise the first task each morning will trigger
1349 a warning because it follows a long gap.
1351 Furthermore, the following properties can be used to define faces for
1352 issue display.
1354 :default-face the default face, if the specific face is undefined
1355 :overlap-face face for overlapping clocks
1356 :gap-face face for gaps between clocks
1357 :no-end-time-face face for incomplete clocks
1358 :long-face face for clock intervals that are too long
1359 :short-face face for clock intervals that are too short"
1360 :group 'org-agenda-daily/weekly
1361 :group 'org-clock
1362 :version "24.1"
1363 :type 'plist)
1365 (defcustom org-agenda-log-mode-add-notes t
1366 "Non-nil means add first line of notes to log entries in agenda views.
1367 If a log item like a state change or a clock entry is associated with
1368 notes, the first line of these notes will be added to the entry in the
1369 agenda display."
1370 :group 'org-agenda-daily/weekly
1371 :type 'boolean)
1373 (defcustom org-agenda-start-with-log-mode nil
1374 "The initial value of log-mode in a newly created agenda window.
1375 See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
1376 explanations on the possible values."
1377 :group 'org-agenda-startup
1378 :group 'org-agenda-daily/weekly
1379 :type '(choice (const :tag "Don't show log items" nil)
1380 (const :tag "Show only log items" only)
1381 (const :tag "Show all possible log items" clockcheck)
1382 (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
1383 (choice (const :tag "Show closed log items" closed)
1384 (const :tag "Show clocked log items" clock)
1385 (const :tag "Show all logged state changes" state)))))
1387 (defcustom org-agenda-start-with-clockreport-mode nil
1388 "The initial value of clockreport-mode in a newly created agenda window."
1389 :group 'org-agenda-startup
1390 :group 'org-agenda-daily/weekly
1391 :type 'boolean)
1393 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1394 "Property list with parameters for the clocktable in clockreport mode.
1395 This is the display mode that shows a clock table in the daily/weekly
1396 agenda, the properties for this dynamic block can be set here.
1397 The usual clocktable parameters are allowed here, but you cannot set
1398 the properties :name, :tstart, :tend, :block, and :scope - these will
1399 be overwritten to make sure the content accurately reflects the
1400 current display in the agenda."
1401 :group 'org-agenda-daily/weekly
1402 :type 'plist)
1404 (defcustom org-agenda-search-view-always-boolean nil
1405 "Non-nil means the search string is interpreted as individual parts.
1407 The search string for search view can either be interpreted as a phrase,
1408 or as a list of snippets that define a boolean search for a number of
1409 strings.
1411 When this is non-nil, the string will be split on whitespace, and each
1412 snippet will be searched individually, and all must match in order to
1413 select an entry. A snippet is then a single string of non-white
1414 characters, or a string in double quotes, or a regexp in {} braces.
1415 If a snippet is preceded by \"-\", the snippet must *not* match.
1416 \"+\" is syntactic sugar for positive selection. Each snippet may
1417 be found as a full word or a partial word, but see the variable
1418 `org-agenda-search-view-force-full-words'.
1420 When this is nil, search will look for the entire search phrase as one,
1421 with each space character matching any amount of whitespace, including
1422 line breaks.
1424 Even when this is nil, you can still switch to Boolean search dynamically
1425 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1426 is a regexp marked with braces like \"{abc}\", this will also switch to
1427 boolean search."
1428 :group 'org-agenda-search-view
1429 :version "24.1"
1430 :type 'boolean)
1432 (defvaralias 'org-agenda-search-view-search-words-only
1433 'org-agenda-search-view-always-boolean)
1435 (defcustom org-agenda-search-view-force-full-words nil
1436 "Non-nil means, search words must be matches as complete words.
1437 When nil, they may also match part of a word."
1438 :group 'org-agenda-search-view
1439 :version "24.1"
1440 :type 'boolean)
1442 (defcustom org-agenda-search-view-max-outline-level 0
1443 "Maximum outline level to display in search view.
1444 E.g. when this is set to 1, the search view will only
1445 show headlines of level 1. When set to 0, the default
1446 value, don't limit agenda view by outline level."
1447 :group 'org-agenda-search-view
1448 :version "26.1"
1449 :package-version '(Org . "8.3")
1450 :type 'integer)
1452 (defgroup org-agenda-time-grid nil
1453 "Options concerning the time grid in the Org Agenda."
1454 :tag "Org Agenda Time Grid"
1455 :group 'org-agenda)
1457 (defcustom org-agenda-search-headline-for-time t
1458 "Non-nil means search headline for a time-of-day.
1459 If the headline contains a time-of-day in one format or another, it will
1460 be used to sort the entry into the time sequence of items for a day.
1461 Some people have time stamps in the headline that refer to the creation
1462 time or so, and then this produces an unwanted side effect. If this is
1463 the case for your, use this variable to turn off searching the headline
1464 for a time."
1465 :group 'org-agenda-time-grid
1466 :type 'boolean)
1468 (defcustom org-agenda-use-time-grid t
1469 "Non-nil means show a time grid in the agenda schedule.
1470 A time grid is a set of lines for specific times (like every two hours between
1471 8:00 and 20:00). The items scheduled for a day at specific times are
1472 sorted in between these lines.
1473 For details about when the grid will be shown, and what it will look like, see
1474 the variable `org-agenda-time-grid'."
1475 :group 'org-agenda-time-grid
1476 :type 'boolean)
1478 (defcustom org-agenda-time-grid
1479 '((daily today require-timed)
1480 (800 1000 1200 1400 1600 1800 2000)
1481 "......"
1482 "----------------")
1484 "The settings for time grid for agenda display.
1485 This is a list of four items. The first item is again a list. It contains
1486 symbols specifying conditions when the grid should be displayed:
1488 daily if the agenda shows a single day
1489 weekly if the agenda shows an entire week
1490 today show grid on current date, independent of daily/weekly display
1491 require-timed show grid only if at least one item has a time specification
1492 remove-match skip grid times already present in an entry
1494 The second item is a list of integers, indicating the times that
1495 should have a grid line.
1497 The third item is a string which will be placed right after the
1498 times that have a grid line.
1500 The fourth item is a string placed after the grid times. This
1501 will align with agenda items"
1502 :group 'org-agenda-time-grid
1503 :type
1504 '(list
1505 (set :greedy t :tag "Grid Display Options"
1506 (const :tag "Show grid in single day agenda display" daily)
1507 (const :tag "Show grid in weekly agenda display" weekly)
1508 (const :tag "Always show grid for today" today)
1509 (const :tag "Show grid only if any timed entries are present"
1510 require-timed)
1511 (const :tag "Skip grid times already present in an entry"
1512 remove-match))
1513 (repeat :tag "Grid Times" (integer :tag "Time"))
1514 (string :tag "Grid String (after agenda times)")
1515 (string :tag "Grid String (aligns with agenda items)")))
1517 (defcustom org-agenda-show-current-time-in-grid t
1518 "Non-nil means show the current time in the time grid."
1519 :group 'org-agenda-time-grid
1520 :version "24.1"
1521 :type 'boolean)
1523 (defcustom org-agenda-current-time-string
1524 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1525 "The string for the current time marker in the agenda."
1526 :group 'org-agenda-time-grid
1527 :version "24.1"
1528 :type 'string)
1530 (defgroup org-agenda-sorting nil
1531 "Options concerning sorting in the Org Agenda."
1532 :tag "Org Agenda Sorting"
1533 :group 'org-agenda)
1535 (defcustom org-agenda-sorting-strategy
1536 '((agenda habit-down time-up priority-down category-keep)
1537 (todo priority-down category-keep)
1538 (tags priority-down category-keep)
1539 (search category-keep))
1540 "Sorting structure for the agenda items of a single day.
1541 This is a list of symbols which will be used in sequence to determine
1542 if an entry should be listed before another entry. The following
1543 symbols are recognized:
1545 time-up Put entries with time-of-day indications first, early first
1546 time-down Put entries with time-of-day indications first, late first
1547 timestamp-up Sort by any timestamp, early first
1548 timestamp-down Sort by any timestamp, late first
1549 scheduled-up Sort by scheduled timestamp, early first
1550 scheduled-down Sort by scheduled timestamp, late first
1551 deadline-up Sort by deadline timestamp, early first
1552 deadline-down Sort by deadline timestamp, late first
1553 ts-up Sort by active timestamp, early first
1554 ts-down Sort by active timestamp, late first
1555 tsia-up Sort by inactive timestamp, early first
1556 tsia-down Sort by inactive timestamp, late first
1557 category-keep Keep the default order of categories, corresponding to the
1558 sequence in `org-agenda-files'.
1559 category-up Sort alphabetically by category, A-Z.
1560 category-down Sort alphabetically by category, Z-A.
1561 tag-up Sort alphabetically by last tag, A-Z.
1562 tag-down Sort alphabetically by last tag, Z-A.
1563 priority-up Sort numerically by priority, high priority last.
1564 priority-down Sort numerically by priority, high priority first.
1565 todo-state-up Sort by todo state, tasks that are done last.
1566 todo-state-down Sort by todo state, tasks that are done first.
1567 effort-up Sort numerically by estimated effort, high effort last.
1568 effort-down Sort numerically by estimated effort, high effort first.
1569 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1570 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1571 habit-up Put entries that are habits first
1572 habit-down Put entries that are habits last
1573 alpha-up Sort headlines alphabetically
1574 alpha-down Sort headlines alphabetically, reversed
1576 The different possibilities will be tried in sequence, and testing stops
1577 if one comparison returns a \"not-equal\". For example, the default
1578 '(time-up category-keep priority-down)
1579 means: Pull out all entries having a specified time of day and sort them,
1580 in order to make a time schedule for the current day the first thing in the
1581 agenda listing for the day. Of the entries without a time indication, keep
1582 the grouped in categories, don't sort the categories, but keep them in
1583 the sequence given in `org-agenda-files'. Within each category sort by
1584 priority.
1586 Leaving out `category-keep' would mean that items will be sorted across
1587 categories by priority.
1589 Instead of a single list, this can also be a set of list for specific
1590 contents, with a context symbol in the car of the list, any of
1591 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1593 Custom commands can bind this variable in the options section."
1594 :group 'org-agenda-sorting
1595 :type `(choice
1596 (repeat :tag "General" ,org-sorting-choice)
1597 (list :tag "Individually"
1598 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1599 (repeat ,org-sorting-choice))
1600 (cons (const :tag "Strategy for TODO lists" todo)
1601 (repeat ,org-sorting-choice))
1602 (cons (const :tag "Strategy for Tags matches" tags)
1603 (repeat ,org-sorting-choice))
1604 (cons (const :tag "Strategy for search matches" search)
1605 (repeat ,org-sorting-choice)))))
1607 (defcustom org-agenda-cmp-user-defined nil
1608 "A function to define the comparison `user-defined'.
1609 This function must receive two arguments, agenda entry a and b.
1610 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1611 the user comparison, return nil.
1612 When this is defined, you can make `user-defined-up' and `user-defined-down'
1613 part of an agenda sorting strategy."
1614 :group 'org-agenda-sorting
1615 :type 'symbol)
1617 (defcustom org-sort-agenda-notime-is-late t
1618 "Non-nil means items without time are considered late.
1619 This is only relevant for sorting. When t, items which have no explicit
1620 time like 15:30 will be considered as 99:01, i.e. later than any items which
1621 do have a time. When nil, the default time is before 0:00. You can use this
1622 option to decide if the schedule for today should come before or after timeless
1623 agenda entries."
1624 :group 'org-agenda-sorting
1625 :type 'boolean)
1627 (defcustom org-sort-agenda-noeffort-is-high t
1628 "Non-nil means items without effort estimate are sorted as high effort.
1629 This also applies when filtering an agenda view with respect to the
1630 < or > effort operator. Then, tasks with no effort defined will be treated
1631 as tasks with high effort.
1632 When nil, such items are sorted as 0 minutes effort."
1633 :group 'org-agenda-sorting
1634 :type 'boolean)
1636 (defgroup org-agenda-line-format nil
1637 "Options concerning the entry prefix in the Org agenda display."
1638 :tag "Org Agenda Line Format"
1639 :group 'org-agenda)
1641 (defcustom org-agenda-prefix-format
1642 '((agenda . " %i %-12:c%?-12t% s")
1643 (todo . " %i %-12:c")
1644 (tags . " %i %-12:c")
1645 (search . " %i %-12:c"))
1646 "Format specifications for the prefix of items in the agenda views.
1648 An alist with one entry per agenda type. The keys of the
1649 sublists are `agenda', `todo', `search' and `tags'. The values
1650 are format strings.
1652 This format works similar to a printf format, with the following meaning:
1654 %c the category of the item, \"Diary\" for entries from the diary,
1655 or as given by the CATEGORY keyword or derived from the file name
1656 %e the effort required by the item
1657 %l the level of the item (insert X space(s) if item is of level X)
1658 %i the icon category of the item, see `org-agenda-category-icon-alist'
1659 %T the last tag of the item (ignore inherited tags, which come first)
1660 %t the HH:MM time-of-day specification if one applies to the entry
1661 %s Scheduling/Deadline information, a short string
1662 %b show breadcrumbs, i.e., the names of the higher levels
1663 %(expression) Eval EXPRESSION and replace the control string
1664 by the result
1666 All specifiers work basically like the standard `%s' of printf, but may
1667 contain two additional characters: a question mark just after the `%'
1668 and a whitespace/punctuation character just before the final letter.
1670 If the first character after `%' is a question mark, the entire field
1671 will only be included if the corresponding value applies to the current
1672 entry. This is useful for fields which should have fixed width when
1673 present, but zero width when absent. For example, \"%?-12t\" will
1674 result in a 12 character time field if a time of the day is specified,
1675 but will completely disappear in entries which do not contain a time.
1677 If there is punctuation or whitespace character just before the
1678 final format letter, this character will be appended to the field
1679 value if the value is not empty. For example, the format
1680 \"%-12:c\" leads to \"Diary: \" if the category is \"Diary\". If
1681 the category is empty, no additional colon is inserted.
1683 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1684 which means:
1686 - Indent the line with two space characters
1687 - Give the category a 12 chars wide field, padded with whitespace on
1688 the right (because of `-'). Append a colon if there is a category
1689 (because of `:').
1690 - If there is a time-of-day, put it into a 12 chars wide field. If no
1691 time, don't put in an empty field, just skip it (because of '?').
1692 - Finally, put the scheduling information.
1694 See also the variables `org-agenda-remove-times-when-in-prefix' and
1695 `org-agenda-remove-tags'.
1697 Custom commands can set this variable in the options section."
1698 :type '(choice
1699 (string :tag "General format")
1700 (list :greedy t :tag "View dependent"
1701 (cons (const agenda) (string :tag "Format"))
1702 (cons (const todo) (string :tag "Format"))
1703 (cons (const tags) (string :tag "Format"))
1704 (cons (const search) (string :tag "Format"))))
1705 :group 'org-agenda-line-format
1706 :version "26.1"
1707 :package-version '(Org . "9.1"))
1709 (defvar org-prefix-format-compiled nil
1710 "The compiled prefix format and associated variables.
1711 This is a list where first element is a list of variable bindings, and second
1712 element is the compiled format expression. See the variable
1713 `org-agenda-prefix-format'.")
1715 (defcustom org-agenda-todo-keyword-format "%-1s"
1716 "Format for the TODO keyword in agenda lines.
1717 Set this to something like \"%-12s\" if you want all TODO keywords
1718 to occupy a fixed space in the agenda display."
1719 :group 'org-agenda-line-format
1720 :type 'string)
1722 (defcustom org-agenda-diary-sexp-prefix nil
1723 "A regexp that matches part of a diary sexp entry
1724 which should be treated as scheduling/deadline information in
1725 `org-agenda'.
1727 For example, you can use this to extract the `diary-remind-message' from
1728 `diary-remind' entries."
1729 :group 'org-agenda-line-format
1730 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1732 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1733 "Text preceding timerange entries in the agenda view.
1734 This is a list with two strings. The first applies when the range
1735 is entirely on one day. The second applies if the range spans several days.
1736 The strings may have two \"%d\" format specifiers which will be filled
1737 with the sequence number of the days, and the total number of days in the
1738 range, respectively."
1739 :group 'org-agenda-line-format
1740 :type '(list
1741 (string :tag "Deadline today ")
1742 (choice :tag "Deadline relative"
1743 (string :tag "Format string")
1744 (function))))
1746 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1747 "Text preceding scheduled items in the agenda view.
1748 This is a list with two strings. The first applies when the item is
1749 scheduled on the current day. The second applies when it has been scheduled
1750 previously, it may contain a %d indicating that this is the nth time that
1751 this item is scheduled, due to automatic rescheduling of unfinished items
1752 for the following day. So this number is one larger than the number of days
1753 that passed since this item was scheduled first."
1754 :group 'org-agenda-line-format
1755 :version "24.4"
1756 :package-version '(Org . "8.0")
1757 :type '(list
1758 (string :tag "Scheduled today ")
1759 (string :tag "Scheduled previously")))
1761 (defcustom org-agenda-inactive-leader "["
1762 "Text preceding item pulled into the agenda by inactive time stamps.
1763 These entries are added to the agenda when pressing \"[\"."
1764 :group 'org-agenda-line-format
1765 :version "24.1"
1766 :type 'string)
1768 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: " "%2d d. ago: ")
1769 "Text preceding deadline items in the agenda view.
1770 This is a list with three strings. The first applies when the item has its
1771 deadline on the current day. The second applies when the deadline is in the
1772 future, the third one when it is in the past. The strings may contain %d
1773 to capture the number of days."
1774 :group 'org-agenda-line-format
1775 :version "24.4"
1776 :package-version '(Org . "8.0")
1777 :type '(list
1778 (string :tag "Deadline today ")
1779 (string :tag "Deadline in the future ")
1780 (string :tag "Deadline in the past ")))
1782 (defcustom org-agenda-remove-times-when-in-prefix t
1783 "Non-nil means remove duplicate time specifications in agenda items.
1784 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1785 time-of-day specification in a headline or diary entry is extracted and
1786 placed into the prefix. If this option is non-nil, the original specification
1787 \(a timestamp or -range, or just a plain time(range) specification like
1788 11:30-4pm) will be removed for agenda display. This makes the agenda less
1789 cluttered.
1790 The option can be t or nil. It may also be the symbol `beg', indicating
1791 that the time should only be removed when it is located at the beginning of
1792 the headline/diary entry."
1793 :group 'org-agenda-line-format
1794 :type '(choice
1795 (const :tag "Always" t)
1796 (const :tag "Never" nil)
1797 (const :tag "When at beginning of entry" beg)))
1799 (defcustom org-agenda-remove-timeranges-from-blocks nil
1800 "Non-nil means remove time ranges specifications in agenda
1801 items that span on several days."
1802 :group 'org-agenda-line-format
1803 :version "24.1"
1804 :type 'boolean)
1806 (defcustom org-agenda-default-appointment-duration nil
1807 "Default duration for appointments that only have a starting time.
1808 When nil, no duration is specified in such cases.
1809 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1810 :group 'org-agenda-line-format
1811 :type '(choice
1812 (integer :tag "Minutes")
1813 (const :tag "No default duration")))
1815 (defcustom org-agenda-show-inherited-tags t
1816 "Non-nil means show inherited tags in each agenda line.
1818 When this option is set to `always', it takes precedence over
1819 `org-agenda-use-tag-inheritance' and inherited tags are shown
1820 in every agenda.
1822 When this option is set to t (the default), inherited tags are
1823 shown when they are available, i.e. when the value of
1824 `org-agenda-use-tag-inheritance' enables tag inheritance for the
1825 given agenda type.
1827 This can be set to a list of agenda types in which the agenda
1828 must display the inherited tags. Available types are `todo',
1829 `agenda' and `search'.
1831 When set to nil, never show inherited tags in agenda lines."
1832 :group 'org-agenda-line-format
1833 :group 'org-agenda
1834 :version "24.3"
1835 :type '(choice
1836 (const :tag "Show inherited tags when available" t)
1837 (const :tag "Always show inherited tags" always)
1838 (repeat :tag "Show inherited tags only in selected agenda types"
1839 (symbol :tag "Agenda type"))))
1841 (defcustom org-agenda-use-tag-inheritance '(todo search agenda)
1842 "List of agenda view types where to use tag inheritance.
1844 In tags/tags-todo/tags-tree agenda views, tag inheritance is
1845 controlled by `org-use-tag-inheritance'. In other agenda types,
1846 `org-use-tag-inheritance' is not used for the selection of the
1847 agenda entries. Still, you may want the agenda to be aware of
1848 the inherited tags anyway, e.g. for later tag filtering.
1850 Allowed value are `todo', `search' and `agenda'.
1852 This variable has no effect if `org-agenda-show-inherited-tags'
1853 is set to `always'. In that case, the agenda is aware of those
1854 tags.
1856 The default value sets tags in every agenda type. Setting this
1857 option to nil will speed up non-tags agenda view a lot."
1858 :group 'org-agenda
1859 :version "26.1"
1860 :package-version '(Org . "9.1")
1861 :type '(choice
1862 (const :tag "Use tag inheritance in all agenda types" t)
1863 (repeat :tag "Use tag inheritance in selected agenda types"
1864 (symbol :tag "Agenda type"))))
1866 (defcustom org-agenda-hide-tags-regexp nil
1867 "Regular expression used to filter away specific tags in agenda views.
1868 This means that these tags will be present, but not be shown in the agenda
1869 line. Secondary filtering will still work on the hidden tags.
1870 Nil means don't hide any tags."
1871 :group 'org-agenda-line-format
1872 :type '(choice
1873 (const :tag "Hide none" nil)
1874 (string :tag "Regexp ")))
1876 (defcustom org-agenda-remove-tags nil
1877 "Non-nil means remove the tags from the headline copy in the agenda.
1878 When this is the symbol `prefix', only remove tags when
1879 `org-agenda-prefix-format' contains a `%T' specifier."
1880 :group 'org-agenda-line-format
1881 :type '(choice
1882 (const :tag "Always" t)
1883 (const :tag "Never" nil)
1884 (const :tag "When prefix format contains %T" prefix)))
1886 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1887 'org-agenda-remove-tags)
1889 (defcustom org-agenda-tags-column 'auto
1890 "Shift tags in agenda items to this column.
1891 If set to `auto', tags will be automatically aligned to the right
1892 edge of the window.
1894 If set to a positive number, tags will be left-aligned to that
1895 column. If set to a negative number, tags will be right-aligned
1896 to that column. For example, -80 works well for a normal 80
1897 character screen."
1898 :group 'org-agenda-line-format
1899 :type '(choice
1900 (const :tag "Automatically align to right edge of window" auto)
1901 (integer :tag "Specific column" -80))
1902 :package-version '(Org . "9.1")
1903 :version "26.1")
1905 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column)
1907 (defcustom org-agenda-fontify-priorities 'cookies
1908 "Non-nil means highlight low and high priorities in agenda.
1909 When t, the highest priority entries are bold, lowest priority italic.
1910 However, settings in `org-priority-faces' will overrule these faces.
1911 When this variable is the symbol `cookies', only fontify the
1912 cookies, not the entire task.
1913 This may also be an association list of priority faces, whose
1914 keys are the character values of `org-highest-priority',
1915 `org-default-priority', and `org-lowest-priority' (the default values
1916 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1917 color as a string, or a list like `(:background \"Red\")'.
1918 If it is a color, the variable `org-faces-easy-properties'
1919 determines if it is a foreground or a background color."
1920 :group 'org-agenda-line-format
1921 :type '(choice
1922 (const :tag "Never" nil)
1923 (const :tag "Defaults" t)
1924 (const :tag "Cookies only" cookies)
1925 (repeat :tag "Specify"
1926 (list (character :tag "Priority" :value ?A)
1927 (choice :tag "Face "
1928 (string :tag "Color")
1929 (sexp :tag "Face"))))))
1931 (defcustom org-agenda-day-face-function nil
1932 "Function called to determine what face should be used to display a day.
1933 The only argument passed to that function is the day. It should
1934 returns a face, or nil if does not want to specify a face and let
1935 the normal rules apply."
1936 :group 'org-agenda-line-format
1937 :version "24.1"
1938 :type '(choice (const nil) (function)))
1940 (defcustom org-agenda-category-icon-alist nil
1941 "Alist of category icon to be displayed in agenda views.
1943 Each entry should have the following format:
1945 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1947 Where CATEGORY-REGEXP is a regexp matching the categories where
1948 the icon should be displayed.
1949 FILE-OR-DATA either a file path or a string containing image data.
1951 The other fields can be omitted safely if not needed:
1952 TYPE indicates the image type.
1953 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1954 image data.
1955 PROPS are additional image attributes to assign to the image,
1956 like, e.g. `:ascent center'.
1958 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1960 If you want to set the display properties yourself, just put a
1961 list as second element:
1963 (CATEGORY-REGEXP (MY PROPERTY LIST))
1965 For example, to display a 16px horizontal space for Emacs
1966 category, you can use:
1968 (\"Emacs\" \\='(space . (:width (16))))"
1969 :group 'org-agenda-line-format
1970 :version "24.1"
1971 :type '(alist :key-type (string :tag "Regexp matching category")
1972 :value-type (choice (list :tag "Icon"
1973 (string :tag "File or data")
1974 (symbol :tag "Type")
1975 (boolean :tag "Data?")
1976 (repeat :tag "Extra image properties" :inline t symbol))
1977 (list :tag "Display properties" sexp))))
1979 (defgroup org-agenda-column-view nil
1980 "Options concerning column view in the agenda."
1981 :tag "Org Agenda Column View"
1982 :group 'org-agenda)
1984 (defcustom org-agenda-view-columns-initially nil
1985 "When non-nil, switch to columns view right after creating the agenda."
1986 :group 'org-agenda-column-view
1987 :type 'boolean
1988 :version "26.1"
1989 :package-version '(Org . "9.0")
1990 :safe #'booleanp)
1992 (defcustom org-agenda-columns-show-summaries t
1993 "Non-nil means show summaries for columns displayed in the agenda view."
1994 :group 'org-agenda-column-view
1995 :type 'boolean)
1997 (defcustom org-agenda-columns-compute-summary-properties t
1998 "Non-nil means recompute all summary properties before column view.
1999 When column view in the agenda is listing properties that have a summary
2000 operator, it can go to all relevant buffers and recompute the summaries
2001 there. This can mean overhead for the agenda column view, but is necessary
2002 to have thing up to date.
2003 As a special case, a CLOCKSUM property also makes sure that the clock
2004 computations are current."
2005 :group 'org-agenda-column-view
2006 :type 'boolean)
2008 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
2009 "Non-nil means the duration of an appointment will add to day effort.
2010 The property to which appointment durations will be added is the one given
2011 in the option `org-effort-property'. If an appointment does not have
2012 an end time, `org-agenda-default-appointment-duration' will be used. If that
2013 is not set, an appointment without end time will not contribute to the time
2014 estimate."
2015 :group 'org-agenda-column-view
2016 :type 'boolean)
2018 (defcustom org-agenda-auto-exclude-function nil
2019 "A function called with a tag to decide if it is filtered on \
2020 \\<org-agenda-mode-map>`\\[org-agenda-filter-by-tag] RET'.
2021 The sole argument to the function, which is called once for each
2022 possible tag, is a string giving the name of the tag. The
2023 function should return either nil if the tag should be included
2024 as normal, or \"-<TAG>\" to exclude the tag.
2025 Note that for the purpose of tag filtering, only the lower-case version of
2026 all tags will be considered, so that this function will only ever see
2027 the lower-case version of all tags."
2028 :group 'org-agenda
2029 :type '(choice (const nil) (function)))
2031 (defcustom org-agenda-bulk-custom-functions nil
2032 "Alist of characters and custom functions for bulk actions.
2033 For example, this value makes those two functions available:
2035 \\='((?R set-category)
2036 (?C bulk-cut))
2038 With selected entries in an agenda buffer, `B R' will call
2039 the custom function `set-category' on the selected entries.
2040 Note that functions in this alist don't need to be quoted."
2041 :type '(alist :key-type character :value-type (group function))
2042 :version "24.1"
2043 :group 'org-agenda)
2045 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
2046 "Execute BODY with point at location given by `org-hd-marker' property.
2047 If STRING is non-nil, the text property will be fetched from position 0
2048 in that string. If STRING is nil, it will be fetched from the beginning
2049 of the current line."
2050 (org-with-gensyms (marker)
2051 `(let ((,marker (get-text-property (if ,string 0 (point-at-bol))
2052 'org-hd-marker ,string)))
2053 (with-current-buffer (marker-buffer ,marker)
2054 (save-excursion
2055 (goto-char ,marker)
2056 ,@body)))))
2057 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
2059 (defun org-add-agenda-custom-command (entry)
2060 "Replace or add a command in `org-agenda-custom-commands'.
2061 This is mostly for hacking and trying a new command - once the command
2062 works you probably want to add it to `org-agenda-custom-commands' for good."
2063 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
2064 (if ass
2065 (setcdr ass (cdr entry))
2066 (push entry org-agenda-custom-commands))))
2068 (defmacro org-agenda--insert-overriding-header (default)
2069 "Insert header into agenda view.
2070 The inserted header depends on `org-agenda-overriding-header'.
2071 If the empty string, don't insert a header. If any other string,
2072 insert it as a header. If nil, insert DEFAULT, which should
2073 evaluate to a string."
2074 (declare (debug (form)) (indent defun))
2075 `(cond
2076 ((not org-agenda-overriding-header) (insert ,default))
2077 ((equal org-agenda-overriding-header "") nil)
2078 ((stringp org-agenda-overriding-header)
2079 (insert (propertize org-agenda-overriding-header
2080 'face 'org-agenda-structure)
2081 "\n"))
2082 (t (user-error "Invalid value for `org-agenda-overriding-header': %S"
2083 org-agenda-overriding-header))))
2085 ;;; Define the org-agenda-mode
2087 (defvar org-agenda-mode-map (make-sparse-keymap)
2088 "Keymap for `org-agenda-mode'.")
2089 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map)
2091 (defvar org-agenda-menu) ; defined later in this file.
2092 (defvar org-agenda-restrict nil) ; defined later in this file.
2093 (defvar org-agenda-follow-mode nil)
2094 (defvar org-agenda-entry-text-mode nil)
2095 (defvar org-agenda-clockreport-mode nil)
2096 (defvar org-agenda-show-log nil
2097 "When non-nil, show the log in the agenda.
2098 Do not set this directly; instead use
2099 `org-agenda-start-with-log-mode', which see.")
2100 (defvar org-agenda-redo-command nil)
2101 (defvar org-agenda-query-string nil)
2102 (defvar org-agenda-mode-hook nil
2103 "Hook run after `org-agenda-mode' is turned on.
2104 The buffer is still writable when this hook is called.")
2105 (defvar org-agenda-type nil)
2106 (defvar org-agenda-force-single-file nil)
2107 (defvar org-agenda-bulk-marked-entries nil
2108 "List of markers that refer to marked entries in the agenda.")
2109 (defvar org-agenda-current-date nil
2110 "Active date when building the agenda.")
2112 ;;; Multiple agenda buffers support
2114 (defcustom org-agenda-sticky nil
2115 "Non-nil means agenda q key will bury agenda buffers.
2116 Agenda commands will then show existing buffer instead of generating new ones.
2117 When nil, `q' will kill the single agenda buffer."
2118 :group 'org-agenda
2119 :version "24.3"
2120 :type 'boolean)
2123 ;;;###autoload
2124 (defun org-toggle-sticky-agenda (&optional arg)
2125 "Toggle `org-agenda-sticky'."
2126 (interactive "P")
2127 (let ((new-value (if arg
2128 (> (prefix-numeric-value arg) 0)
2129 (not org-agenda-sticky))))
2130 (if (equal new-value org-agenda-sticky)
2131 (and (called-interactively-p 'interactive)
2132 (message "Sticky agenda was already %s"
2133 (if org-agenda-sticky "enabled" "disabled")))
2134 (setq org-agenda-sticky new-value)
2135 (org-agenda-kill-all-agenda-buffers)
2136 (and (called-interactively-p 'interactive)
2137 (message "Sticky agenda %s"
2138 (if org-agenda-sticky "enabled" "disabled"))))))
2140 (defvar org-agenda-buffer nil
2141 "Agenda buffer currently being generated.")
2143 (defvar org-agenda-last-prefix-arg nil)
2144 (defvar org-agenda-this-buffer-name nil)
2145 (defvar org-agenda-doing-sticky-redo nil)
2146 (defvar org-agenda-this-buffer-is-sticky nil)
2147 (defvar org-agenda-last-indirect-buffer nil
2148 "Last buffer loaded by `org-agenda-tree-to-indirect-buffer'.")
2150 (defconst org-agenda-local-vars
2151 '(org-agenda-this-buffer-name
2152 org-agenda-undo-list
2153 org-agenda-pending-undo-list
2154 org-agenda-follow-mode
2155 org-agenda-entry-text-mode
2156 org-agenda-clockreport-mode
2157 org-agenda-show-log
2158 org-agenda-redo-command
2159 org-agenda-query-string
2160 org-agenda-type
2161 org-agenda-bulk-marked-entries
2162 org-agenda-undo-has-started-in
2163 org-agenda-info
2164 org-agenda-pre-window-conf
2165 org-agenda-columns-active
2166 org-agenda-tag-filter
2167 org-agenda-category-filter
2168 org-agenda-top-headline-filter
2169 org-agenda-regexp-filter
2170 org-agenda-effort-filter
2171 org-agenda-markers
2172 org-agenda-last-search-view-search-was-boolean
2173 org-agenda-last-indirect-buffer
2174 org-agenda-filtered-by-category
2175 org-agenda-filter-form
2176 org-agenda-cycle-counter
2177 org-agenda-last-prefix-arg)
2178 "Variables that must be local in agenda buffers to allow multiple buffers.")
2180 (defun org-agenda-mode ()
2181 "Mode for time-sorted view on action items in Org files.
2183 The following commands are available:
2185 \\{org-agenda-mode-map}"
2186 (interactive)
2187 (cond (org-agenda-doing-sticky-redo
2188 ;; Refreshing sticky agenda-buffer
2190 ;; Preserve the value of `org-agenda-local-vars' variables,
2191 ;; while letting `kill-all-local-variables' kill the rest
2192 (let ((save (buffer-local-variables)))
2193 (kill-all-local-variables)
2194 (mapc #'make-local-variable org-agenda-local-vars)
2195 (dolist (elem save)
2196 (pcase elem
2197 (`(,var . ,val) ;ignore unbound variables
2198 (when (and val (memq var org-agenda-local-vars))
2199 (set var val))))))
2200 (setq-local org-agenda-this-buffer-is-sticky t))
2201 (org-agenda-sticky
2202 ;; Creating a sticky Agenda buffer for the first time
2203 (kill-all-local-variables)
2204 (mapc 'make-local-variable org-agenda-local-vars)
2205 (setq-local org-agenda-this-buffer-is-sticky t))
2207 ;; Creating a non-sticky agenda buffer
2208 (kill-all-local-variables)
2209 (setq-local org-agenda-this-buffer-is-sticky nil)))
2210 (setq org-agenda-undo-list nil
2211 org-agenda-pending-undo-list nil
2212 org-agenda-bulk-marked-entries nil)
2213 (setq major-mode 'org-agenda-mode)
2214 ;; Keep global-font-lock-mode from turning on font-lock-mode
2215 (setq-local font-lock-global-modes (list 'not major-mode))
2216 (setq mode-name "Org-Agenda")
2217 (setq indent-tabs-mode nil)
2218 (use-local-map org-agenda-mode-map)
2219 (easy-menu-add org-agenda-menu)
2220 (when org-startup-truncated (setq truncate-lines t))
2221 (setq-local line-move-visual nil)
2222 (add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
2223 (add-hook 'pre-command-hook 'org-unhighlight nil 'local)
2224 ;; Make sure properties are removed when copying text
2225 (add-hook 'filter-buffer-substring-functions
2226 (lambda (fun start end delete)
2227 (substring-no-properties (funcall fun start end delete)))
2228 nil t)
2229 (unless org-agenda-keep-modes
2230 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
2231 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
2232 org-agenda-show-log org-agenda-start-with-log-mode
2233 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode))
2234 (add-to-invisibility-spec '(org-filtered))
2235 (add-to-invisibility-spec '(org-link))
2236 (easy-menu-change
2237 '("Agenda") "Agenda Files"
2238 (append
2239 (list
2240 (vector
2241 (if (get 'org-agenda-files 'org-restrict)
2242 "Restricted to single file"
2243 "Edit File List")
2244 '(org-edit-agenda-file-list)
2245 (not (get 'org-agenda-files 'org-restrict)))
2246 "--")
2247 (mapcar 'org-file-menu-entry (org-agenda-files))))
2248 (org-agenda-set-mode-name)
2249 (apply
2250 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2251 (list 'org-agenda-mode-hook)))
2253 (substitute-key-definition 'undo 'org-agenda-undo
2254 org-agenda-mode-map global-map)
2255 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2256 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2257 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2258 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2259 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2260 (org-defkey org-agenda-mode-map [(meta down)] 'org-agenda-drag-line-forward)
2261 (org-defkey org-agenda-mode-map [(meta up)] 'org-agenda-drag-line-backward)
2262 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2263 (org-defkey org-agenda-mode-map "\M-m" 'org-agenda-bulk-toggle)
2264 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2265 (org-defkey org-agenda-mode-map "\M-*" 'org-agenda-bulk-toggle-all)
2266 (org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
2267 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2268 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2269 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2270 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2271 (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2272 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2273 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2274 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2275 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2276 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2277 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2278 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2279 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2280 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2281 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2282 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2283 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2284 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2285 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2286 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2287 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2288 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2289 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2290 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2291 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2292 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2293 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2294 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2295 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2296 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2297 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2298 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2299 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2300 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2301 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2302 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2303 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2304 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2306 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2307 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2308 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2309 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2310 (while l (org-defkey org-agenda-mode-map
2311 (int-to-string (pop l)) 'digit-argument)))
2313 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2314 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2315 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2316 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2317 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2318 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2319 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2320 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2321 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2322 (org-defkey org-agenda-mode-map "g" 'org-agenda-redo-all)
2323 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2324 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2325 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2326 'org-clock-modify-effort-estimate)
2327 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2328 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2329 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2330 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2331 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2332 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2333 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2334 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2335 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2336 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2337 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2338 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2339 (substitute-key-definition 'next-line 'org-agenda-next-line
2340 org-agenda-mode-map global-map)
2341 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2342 org-agenda-mode-map global-map)
2343 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2344 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2345 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2346 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2347 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2348 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2349 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2350 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2351 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2352 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2353 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2354 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2355 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2356 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2357 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2358 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2359 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2360 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2361 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2362 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2363 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2364 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2365 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2366 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2367 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2368 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2369 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2370 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2371 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2372 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2373 (org-defkey org-agenda-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock-from-agenda)
2375 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2376 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2377 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2378 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2379 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2380 (org-defkey org-agenda-mode-map "_" 'org-agenda-filter-by-effort)
2381 (org-defkey org-agenda-mode-map "=" 'org-agenda-filter-by-regexp)
2382 (org-defkey org-agenda-mode-map "|" 'org-agenda-filter-remove-all)
2383 (org-defkey org-agenda-mode-map "~" 'org-agenda-limit-interactively)
2384 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2385 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-headline)
2386 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2387 (org-defkey org-agenda-mode-map "\C-c\C-x_" 'org-timer-stop)
2388 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2389 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2390 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2392 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2393 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2395 (define-key org-agenda-mode-map [remap forward-paragraph] 'org-agenda-forward-block)
2396 (define-key org-agenda-mode-map [remap backward-paragraph] 'org-agenda-backward-block)
2398 (when org-agenda-mouse-1-follows-link
2399 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2400 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2401 '("Agenda"
2402 ("Agenda Files")
2403 "--"
2404 ("Agenda Dates"
2405 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda)]
2406 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2407 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2408 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2409 "--"
2410 ("View"
2411 ["Day View" org-agenda-day-view
2412 :active (org-agenda-check-type nil 'agenda)
2413 :style radio :selected (eq org-agenda-current-span 'day)
2414 :keys "v d (or just d)"]
2415 ["Week View" org-agenda-week-view
2416 :active (org-agenda-check-type nil 'agenda)
2417 :style radio :selected (eq org-agenda-current-span 'week)
2418 :keys "v w"]
2419 ["Fortnight View" org-agenda-fortnight-view
2420 :active (org-agenda-check-type nil 'agenda)
2421 :style radio :selected (eq org-agenda-current-span 'fortnight)
2422 :keys "v t"]
2423 ["Month View" org-agenda-month-view
2424 :active (org-agenda-check-type nil 'agenda)
2425 :style radio :selected (eq org-agenda-current-span 'month)
2426 :keys "v m"]
2427 ["Year View" org-agenda-year-view
2428 :active (org-agenda-check-type nil 'agenda)
2429 :style radio :selected (eq org-agenda-current-span 'year)
2430 :keys "v y"]
2431 "--"
2432 ["Include Diary" org-agenda-toggle-diary
2433 :style toggle :selected org-agenda-include-diary
2434 :active (org-agenda-check-type nil 'agenda)]
2435 ["Include Deadlines" org-agenda-toggle-deadlines
2436 :style toggle :selected org-agenda-include-deadlines
2437 :active (org-agenda-check-type nil 'agenda)]
2438 ["Use Time Grid" org-agenda-toggle-time-grid
2439 :style toggle :selected org-agenda-use-time-grid
2440 :active (org-agenda-check-type nil 'agenda)]
2441 "--"
2442 ["Show clock report" org-agenda-clockreport-mode
2443 :style toggle :selected org-agenda-clockreport-mode
2444 :active (org-agenda-check-type nil 'agenda)]
2445 ["Show some entry text" org-agenda-entry-text-mode
2446 :style toggle :selected org-agenda-entry-text-mode
2447 :active t]
2448 "--"
2449 ["Show Logbook entries" org-agenda-log-mode
2450 :style toggle :selected org-agenda-show-log
2451 :active (org-agenda-check-type nil 'agenda)
2452 :keys "v l (or just l)"]
2453 ["Include archived trees" org-agenda-archives-mode
2454 :style toggle :selected org-agenda-archives-mode :active t
2455 :keys "v a"]
2456 ["Include archive files" (org-agenda-archives-mode t)
2457 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2458 :keys "v A"]
2459 "--"
2460 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2461 ["Write view to file" org-agenda-write t]
2462 ["Rebuild buffer" org-agenda-redo t]
2463 ["Save all Org buffers" org-save-all-org-buffers t]
2464 "--"
2465 ["Show original entry" org-agenda-show t]
2466 ["Go To (other window)" org-agenda-goto t]
2467 ["Go To (this window)" org-agenda-switch-to t]
2468 ["Capture with cursor date" org-agenda-capture t]
2469 ["Follow Mode" org-agenda-follow-mode
2470 :style toggle :selected org-agenda-follow-mode :active t]
2471 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2472 "--"
2473 ("TODO"
2474 ["Cycle TODO" org-agenda-todo t]
2475 ["Next TODO set" org-agenda-todo-nextset t]
2476 ["Previous TODO set" org-agenda-todo-previousset t]
2477 ["Add note" org-agenda-add-note t])
2478 ("Archive/Refile/Delete"
2479 ["Archive default" org-agenda-archive-default t]
2480 ["Archive default" org-agenda-archive-default-with-confirmation t]
2481 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2482 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2483 ["Archive subtree" org-agenda-archive t]
2484 "--"
2485 ["Refile" org-agenda-refile t]
2486 "--"
2487 ["Delete subtree" org-agenda-kill t])
2488 ("Bulk action"
2489 ["Mark entry" org-agenda-bulk-mark t]
2490 ["Mark all" org-agenda-bulk-mark-all t]
2491 ["Unmark entry" org-agenda-bulk-unmark t]
2492 ["Unmark all" org-agenda-bulk-unmark-all :active t :keys "U"]
2493 ["Toggle mark" org-agenda-bulk-toggle t]
2494 ["Toggle all" org-agenda-bulk-toggle-all t]
2495 ["Mark regexp" org-agenda-bulk-mark-regexp t])
2496 ["Act on all marked" org-agenda-bulk-action t]
2497 "--"
2498 ("Tags and Properties"
2499 ["Show all Tags" org-agenda-show-tags t]
2500 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2501 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2502 "--"
2503 ["Column View" org-columns t])
2504 ("Deadline/Schedule"
2505 ["Schedule" org-agenda-schedule t]
2506 ["Set Deadline" org-agenda-deadline t]
2507 "--"
2508 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda)]
2509 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda)]
2510 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda) :keys "C-u S-right"]
2511 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda) :keys "C-u S-left"]
2512 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda) :keys "C-u C-u S-right"]
2513 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda) :keys "C-u C-u S-left"]
2514 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda)])
2515 ("Clock and Effort"
2516 ["Clock in" org-agenda-clock-in t]
2517 ["Clock out" org-agenda-clock-out t]
2518 ["Clock cancel" org-agenda-clock-cancel t]
2519 ["Goto running clock" org-clock-goto t]
2520 "--"
2521 ["Set Effort" org-agenda-set-effort t]
2522 ["Change clocked effort" org-clock-modify-effort-estimate
2523 (org-clock-is-active)])
2524 ("Priority"
2525 ["Set Priority" org-agenda-priority t]
2526 ["Increase Priority" org-agenda-priority-up t]
2527 ["Decrease Priority" org-agenda-priority-down t]
2528 ["Show Priority" org-show-priority t])
2529 ("Calendar/Diary"
2530 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda)]
2531 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda)]
2532 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda)]
2533 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda)]
2534 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda)]
2535 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda)]
2536 "--"
2537 ["Create iCalendar File" org-icalendar-combine-agenda-files t])
2538 "--"
2539 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2540 "--"
2541 ("MobileOrg"
2542 ["Push Files and Views" org-mobile-push t]
2543 ["Get Captured and Flagged" org-mobile-pull t]
2544 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "\\[org-agenda] ?"]
2545 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2546 "--"
2547 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2548 "--"
2549 ["Quit" org-agenda-quit t]
2550 ["Exit and Release Buffers" org-agenda-exit t]
2553 ;;; Agenda undo
2555 (defvar org-agenda-allow-remote-undo t
2556 "Non-nil means allow remote undo from the agenda buffer.")
2557 (defvar org-agenda-undo-has-started-in nil
2558 "Buffers that have already seen `undo-start' in the current undo sequence.")
2560 (defun org-agenda-undo ()
2561 "Undo a remote editing step in the agenda.
2562 This undoes changes both in the agenda buffer and in the remote buffer
2563 that have been changed along."
2564 (interactive)
2565 (or org-agenda-allow-remote-undo
2566 (user-error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2567 (when (not (eq this-command last-command))
2568 (setq org-agenda-undo-has-started-in nil
2569 org-agenda-pending-undo-list org-agenda-undo-list))
2570 (when (not org-agenda-pending-undo-list)
2571 (user-error "No further undo information"))
2572 (let* ((entry (pop org-agenda-pending-undo-list))
2573 buf line cmd rembuf)
2574 (setq cmd (pop entry) line (pop entry))
2575 (setq rembuf (nth 2 entry))
2576 (org-with-remote-undo rembuf
2577 (while (bufferp (setq buf (pop entry)))
2578 (when (pop entry)
2579 (with-current-buffer buf
2580 (let ((last-undo-buffer buf)
2581 (inhibit-read-only t))
2582 (unless (memq buf org-agenda-undo-has-started-in)
2583 (push buf org-agenda-undo-has-started-in)
2584 (make-local-variable 'pending-undo-list)
2585 (undo-start))
2586 (while (and pending-undo-list
2587 (listp pending-undo-list)
2588 (not (car pending-undo-list)))
2589 (pop pending-undo-list))
2590 (undo-more 1))))))
2591 (org-goto-line line)
2592 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2594 (defun org-verify-change-for-undo (l1 l2)
2595 "Verify that a real change occurred between the undo lists L1 and L2."
2596 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2597 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2598 (not (eq l1 l2)))
2600 ;;; Agenda dispatch
2602 (defvar org-agenda-restrict-begin (make-marker))
2603 (defvar org-agenda-restrict-end (make-marker))
2604 (defvar org-agenda-last-dispatch-buffer nil)
2605 (defvar org-agenda-overriding-restriction nil)
2607 (defcustom org-agenda-custom-commands-contexts nil
2608 "Alist of custom agenda keys and contextual rules.
2610 For example, if you have a custom agenda command \"p\" and you
2611 want this command to be accessible only from plain text files,
2612 use this:
2614 \\='((\"p\" ((in-file . \"\\\\.txt\\\\'\"))))
2616 Here are the available contexts definitions:
2618 in-file: command displayed only in matching files
2619 in-mode: command displayed only in matching modes
2620 not-in-file: command not displayed in matching files
2621 not-in-mode: command not displayed in matching modes
2622 in-buffer: command displayed only in matching buffers
2623 not-in-buffer: command not displayed in matching buffers
2624 [function]: a custom function taking no argument
2626 If you define several checks, the agenda command will be
2627 accessible if there is at least one valid check.
2629 You can also bind a key to another agenda custom command
2630 depending on contextual rules.
2632 \\='((\"p\" \"q\" ((in-file . \"\\\\.txt\\\\'\"))))
2634 Here it means: in .txt files, use \"p\" as the key for the
2635 agenda command otherwise associated with \"q\". (The command
2636 originally associated with \"q\" is not displayed to avoid
2637 duplicates.)"
2638 :version "24.3"
2639 :group 'org-agenda-custom-commands
2640 :type '(repeat (list :tag "Rule"
2641 (string :tag " Agenda key")
2642 (string :tag "Replace by command")
2643 (repeat :tag "Available when"
2644 (choice
2645 (cons :tag "Condition"
2646 (choice
2647 (const :tag "In file" in-file)
2648 (const :tag "Not in file" not-in-file)
2649 (const :tag "In buffer" in-buffer)
2650 (const :tag "Not in buffer" not-in-buffer)
2651 (const :tag "In mode" in-mode)
2652 (const :tag "Not in mode" not-in-mode))
2653 (regexp))
2654 (function :tag "Custom function"))))))
2656 (defcustom org-agenda-max-entries nil
2657 "Maximum number of entries to display in an agenda.
2658 This can be nil (no limit) or an integer or an alist of agenda
2659 types with an associated number of entries to display in this
2660 type."
2661 :version "24.4"
2662 :package-version '(Org . "8.0")
2663 :group 'org-agenda-custom-commands
2664 :type '(choice (symbol :tag "No limit" nil)
2665 (integer :tag "Max number of entries")
2666 (repeat
2667 (cons (choice :tag "Agenda type"
2668 (const agenda)
2669 (const todo)
2670 (const tags)
2671 (const search))
2672 (integer :tag "Max number of entries")))))
2674 (defcustom org-agenda-max-todos nil
2675 "Maximum number of TODOs to display in an agenda.
2676 This can be nil (no limit) or an integer or an alist of agenda
2677 types with an associated number of entries to display in this
2678 type."
2679 :version "24.4"
2680 :package-version '(Org . "8.0")
2681 :group 'org-agenda-custom-commands
2682 :type '(choice (symbol :tag "No limit" nil)
2683 (integer :tag "Max number of TODOs")
2684 (repeat
2685 (cons (choice :tag "Agenda type"
2686 (const agenda)
2687 (const todo)
2688 (const tags)
2689 (const search))
2690 (integer :tag "Max number of TODOs")))))
2692 (defcustom org-agenda-max-tags nil
2693 "Maximum number of tagged entries to display in an agenda.
2694 This can be nil (no limit) or an integer or an alist of agenda
2695 types with an associated number of entries to display in this
2696 type."
2697 :version "24.4"
2698 :package-version '(Org . "8.0")
2699 :group 'org-agenda-custom-commands
2700 :type '(choice (symbol :tag "No limit" nil)
2701 (integer :tag "Max number of tagged entries")
2702 (repeat
2703 (cons (choice :tag "Agenda type"
2704 (const agenda)
2705 (const todo)
2706 (const tags)
2707 (const search))
2708 (integer :tag "Max number of tagged entries")))))
2710 (defcustom org-agenda-max-effort nil
2711 "Maximum cumulated effort duration for the agenda.
2712 This can be nil (no limit) or a number of minutes (as an integer)
2713 or an alist of agenda types with an associated number of minutes
2714 to limit entries to in this type."
2715 :version "24.4"
2716 :package-version '(Org . "8.0")
2717 :group 'org-agenda-custom-commands
2718 :type '(choice (symbol :tag "No limit" nil)
2719 (integer :tag "Max number of minutes")
2720 (repeat
2721 (cons (choice :tag "Agenda type"
2722 (const agenda)
2723 (const todo)
2724 (const tags)
2725 (const search))
2726 (integer :tag "Max number of minutes")))))
2728 (defvar org-agenda-keep-restricted-file-list nil)
2729 (defvar org-keys nil)
2730 (defvar org-match nil)
2731 ;;;###autoload
2732 (defun org-agenda (&optional arg org-keys restriction)
2733 "Dispatch agenda commands to collect entries to the agenda buffer.
2734 Prompts for a command to execute. Any prefix arg will be passed
2735 on to the selected command. The default selections are:
2737 a Call `org-agenda-list' to display the agenda for current day or week.
2738 t Call `org-todo-list' to display the global todo list.
2739 T Call `org-todo-list' to display the global todo list, select only
2740 entries with a specific TODO keyword (the user gets a prompt).
2741 m Call `org-tags-view' to display headlines with tags matching
2742 a condition (the user is prompted for the condition).
2743 M Like `m', but select only TODO entries, no ordinary headlines.
2744 e Export views to associated files.
2745 s Search entries for keywords.
2746 S Search entries for keywords, only with TODO keywords.
2747 / Multi occur across all agenda files and also files listed
2748 in `org-agenda-text-search-extra-files'.
2749 < Restrict agenda commands to buffer, subtree, or region.
2750 Press several times to get the desired effect.
2751 > Remove a previous restriction.
2752 # List \"stuck\" projects.
2753 ! Configure what \"stuck\" means.
2754 C Configure custom agenda commands.
2756 More commands can be added by configuring the variable
2757 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2758 searches can be pre-defined in this way.
2760 If the current buffer is in Org mode and visiting a file, you can also
2761 first press `<' once to indicate that the agenda should be temporarily
2762 \(until the next use of `\\[org-agenda]') restricted to the current file.
2763 Pressing `<' twice means to restrict to the current subtree or region
2764 \(if active)."
2765 (interactive "P")
2766 (catch 'exit
2767 (let* ((prefix-descriptions nil)
2768 (org-agenda-buffer-name org-agenda-buffer-name)
2769 (org-agenda-window-setup (if (equal (buffer-name)
2770 org-agenda-buffer-name)
2771 'current-window
2772 org-agenda-window-setup))
2773 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2774 (org-agenda-custom-commands
2775 ;; normalize different versions
2776 (delq nil
2777 (mapcar
2778 (lambda (x)
2779 (cond ((stringp (cdr x))
2780 (push x prefix-descriptions)
2781 nil)
2782 ((stringp (nth 1 x)) x)
2783 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2784 (t (cons (car x) (cons "" (cdr x))))))
2785 org-agenda-custom-commands)))
2786 (org-agenda-custom-commands
2787 (org-contextualize-keys
2788 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2789 (buf (current-buffer))
2790 (bfn (buffer-file-name (buffer-base-buffer)))
2791 entry key type org-match lprops ans)
2792 ;; Turn off restriction unless there is an overriding one,
2793 (unless org-agenda-overriding-restriction
2794 (unless org-agenda-keep-restricted-file-list
2795 ;; There is a request to keep the file list in place
2796 (put 'org-agenda-files 'org-restrict nil))
2797 (setq org-agenda-restrict nil)
2798 (move-marker org-agenda-restrict-begin nil)
2799 (move-marker org-agenda-restrict-end nil))
2800 ;; Delete old local properties
2801 (put 'org-agenda-redo-command 'org-lprops nil)
2802 ;; Delete previously set last-arguments
2803 (put 'org-agenda-redo-command 'last-args nil)
2804 ;; Remember where this call originated
2805 (setq org-agenda-last-dispatch-buffer (current-buffer))
2806 (unless org-keys
2807 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2808 org-keys (car ans)
2809 restriction (cdr ans)))
2810 ;; If we have sticky agenda buffers, set a name for the buffer,
2811 ;; depending on the invoking keys. The user may still set this
2812 ;; as a command option, which will overwrite what we do here.
2813 (when org-agenda-sticky
2814 (setq org-agenda-buffer-name
2815 (format "*Org Agenda(%s)*" org-keys)))
2816 ;; Establish the restriction, if any
2817 (when (and (not org-agenda-overriding-restriction) restriction)
2818 (put 'org-agenda-files 'org-restrict (list bfn))
2819 (cond
2820 ((eq restriction 'region)
2821 (setq org-agenda-restrict (current-buffer))
2822 (move-marker org-agenda-restrict-begin (region-beginning))
2823 (move-marker org-agenda-restrict-end (region-end)))
2824 ((eq restriction 'subtree)
2825 (save-excursion
2826 (setq org-agenda-restrict (current-buffer))
2827 (org-back-to-heading t)
2828 (move-marker org-agenda-restrict-begin (point))
2829 (move-marker org-agenda-restrict-end
2830 (progn (org-end-of-subtree t)))))))
2832 ;; For example the todo list should not need it (but does...)
2833 (cond
2834 ((setq entry (assoc org-keys org-agenda-custom-commands))
2835 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2836 (progn
2837 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
2838 lprops (nth 4 entry))
2839 (when org-agenda-sticky
2840 (setq org-agenda-buffer-name
2841 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2842 (format "*Org Agenda(%s)*" org-keys))))
2843 (put 'org-agenda-redo-command 'org-lprops lprops)
2844 (cond
2845 ((eq type 'agenda)
2846 (org-let lprops '(org-agenda-list current-prefix-arg)))
2847 ((eq type 'agenda*)
2848 (org-let lprops '(org-agenda-list current-prefix-arg nil nil t)))
2849 ((eq type 'alltodo)
2850 (org-let lprops '(org-todo-list current-prefix-arg)))
2851 ((eq type 'search)
2852 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
2853 ((eq type 'stuck)
2854 (org-let lprops '(org-agenda-list-stuck-projects
2855 current-prefix-arg)))
2856 ((eq type 'tags)
2857 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2858 ((eq type 'tags-todo)
2859 (org-let lprops '(org-tags-view '(4) org-match)))
2860 ((eq type 'todo)
2861 (org-let lprops '(org-todo-list org-match)))
2862 ((eq type 'tags-tree)
2863 (org-check-for-org-mode)
2864 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
2865 ((eq type 'todo-tree)
2866 (org-check-for-org-mode)
2867 (org-let lprops
2868 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2869 (regexp-quote org-match) "\\>"))))
2870 ((eq type 'occur-tree)
2871 (org-check-for-org-mode)
2872 (org-let lprops '(org-occur org-match)))
2873 ((functionp type)
2874 (org-let lprops '(funcall type org-match)))
2875 ((fboundp type)
2876 (org-let lprops '(funcall type org-match)))
2877 (t (user-error "Invalid custom agenda command type %s" type))))
2878 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2879 ((equal org-keys "C")
2880 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2881 (customize-variable 'org-agenda-custom-commands))
2882 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2883 ((equal org-keys "s") (call-interactively 'org-search-view))
2884 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2885 ((equal org-keys "t") (call-interactively 'org-todo-list))
2886 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2887 ((equal org-keys "m") (call-interactively 'org-tags-view))
2888 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2889 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2890 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
2891 (add-hook
2892 'post-command-hook
2893 (lambda ()
2894 (unless (current-message)
2895 (let* ((m (org-agenda-get-any-marker))
2896 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2897 (when note
2898 (message (concat
2899 "FLAGGING-NOTE ([?] for more info): "
2900 (org-add-props
2901 (replace-regexp-in-string
2902 "\\\\n" "//"
2903 (copy-sequence note))
2904 nil 'face 'org-warning)))))))
2905 t t))
2906 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2907 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2908 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
2909 (t (user-error "Invalid agenda key"))))))
2911 (defvar org-agenda-multi)
2913 (defun org-agenda-append-agenda ()
2914 "Append another agenda view to the current one.
2915 This function allows interactive building of block agendas.
2916 Agenda views are separated by `org-agenda-block-separator'."
2917 (interactive)
2918 (unless (derived-mode-p 'org-agenda-mode)
2919 (user-error "Can only append from within agenda buffer"))
2920 (let ((org-agenda-multi t))
2921 (org-agenda)
2922 (widen)
2923 (org-agenda-finalize)
2924 (setq buffer-read-only t)
2925 (org-agenda-fit-window-to-buffer)))
2927 (defun org-agenda-normalize-custom-commands (cmds)
2928 "Normalize custom commands CMDS."
2929 (delq nil
2930 (mapcar
2931 (lambda (x)
2932 (cond ((stringp (cdr x)) nil)
2933 ((stringp (nth 1 x)) x)
2934 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2935 (t (cons (car x) (cons "" (cdr x))))))
2936 cmds)))
2938 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2939 "The user interface for selecting an agenda command."
2940 (catch 'exit
2941 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2942 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2943 (region-p (org-region-active-p))
2944 (custom org-agenda-custom-commands)
2945 (selstring "")
2946 restriction second-time
2947 c entry key type match prefixes rmheader header-end custom1 desc
2948 line lines left right n n1)
2949 (save-window-excursion
2950 (delete-other-windows)
2951 (org-switch-to-buffer-other-window " *Agenda Commands*")
2952 (erase-buffer)
2953 (insert (eval-when-compile
2954 (let ((header
2955 "Press key for an agenda command:
2956 -------------------------------- < Buffer, subtree/region restriction
2957 a Agenda for current week or day > Remove restriction
2958 t List of all TODO entries e Export agenda views
2959 m Match a TAGS/PROP/TODO query T Entries with special TODO kwd
2960 s Search for keywords M Like m, but only TODO entries
2961 / Multi-occur S Like s, but only TODO entries
2962 ? Find :FLAGGED: entries C Configure custom agenda commands
2963 * Toggle sticky agenda views # List stuck projects (!=configure)
2965 (start 0))
2966 (while (string-match
2967 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2968 header start)
2969 (setq start (match-end 0))
2970 (add-text-properties (match-beginning 2) (match-end 2)
2971 '(face bold) header))
2972 header)))
2973 (setq header-end (point-marker))
2974 (while t
2975 (setq custom1 custom)
2976 (when (eq rmheader t)
2977 (org-goto-line 1)
2978 (re-search-forward ":" nil t)
2979 (delete-region (match-end 0) (point-at-eol))
2980 (forward-char 1)
2981 (looking-at "-+")
2982 (delete-region (match-end 0) (point-at-eol))
2983 (move-marker header-end (match-end 0)))
2984 (goto-char header-end)
2985 (delete-region (point) (point-max))
2987 ;; Produce all the lines that describe custom commands and prefixes
2988 (setq lines nil)
2989 (while (setq entry (pop custom1))
2990 (setq key (car entry) desc (nth 1 entry)
2991 type (nth 2 entry)
2992 match (nth 3 entry))
2993 (if (> (length key) 1)
2994 (cl-pushnew (string-to-char key) prefixes :test #'equal)
2995 (setq line
2996 (format
2997 "%-4s%-14s"
2998 (org-add-props (copy-sequence key)
2999 '(face bold))
3000 (cond
3001 ((string-match "\\S-" desc) desc)
3002 ((eq type 'agenda) "Agenda for current week or day")
3003 ((eq type 'agenda*) "Appointments for current week or day")
3004 ((eq type 'alltodo) "List of all TODO entries")
3005 ((eq type 'search) "Word search")
3006 ((eq type 'stuck) "List of stuck projects")
3007 ((eq type 'todo) "TODO keyword")
3008 ((eq type 'tags) "Tags query")
3009 ((eq type 'tags-todo) "Tags (TODO)")
3010 ((eq type 'tags-tree) "Tags tree")
3011 ((eq type 'todo-tree) "TODO kwd tree")
3012 ((eq type 'occur-tree) "Occur tree")
3013 ((functionp type) (if (symbolp type)
3014 (symbol-name type)
3015 "Lambda expression"))
3016 (t "???"))))
3017 (if org-agenda-menu-show-matcher
3018 (setq line
3019 (concat line ": "
3020 (cond
3021 ((stringp match)
3022 (setq match (copy-sequence match))
3023 (org-add-props match nil 'face 'org-warning))
3024 ((listp type)
3025 (format "set of %d commands" (length type))))))
3026 (when (org-string-nw-p match)
3027 (add-text-properties
3028 0 (length line) (list 'help-echo
3029 (concat "Matcher: " match)) line)))
3030 (push line lines)))
3031 (setq lines (nreverse lines))
3032 (when prefixes
3033 (mapc (lambda (x)
3034 (push
3035 (format "%s %s"
3036 (org-add-props (char-to-string x)
3037 nil 'face 'bold)
3038 (or (cdr (assoc (concat selstring
3039 (char-to-string x))
3040 prefix-descriptions))
3041 "Prefix key"))
3042 lines))
3043 prefixes))
3045 ;; Check if we should display in two columns
3046 (if org-agenda-menu-two-columns
3047 (progn
3048 (setq n (length lines)
3049 n1 (+ (/ n 2) (mod n 2))
3050 right (nthcdr n1 lines)
3051 left (copy-sequence lines))
3052 (setcdr (nthcdr (1- n1) left) nil))
3053 (setq left lines right nil))
3054 (while left
3055 (insert "\n" (pop left))
3056 (when right
3057 (if (< (current-column) 40)
3058 (move-to-column 40 t)
3059 (insert " "))
3060 (insert (pop right))))
3062 ;; Make the window the right size
3063 (goto-char (point-min))
3064 (if second-time
3065 (when (not (pos-visible-in-window-p (point-max)))
3066 (org-fit-window-to-buffer))
3067 (setq second-time t)
3068 (org-fit-window-to-buffer))
3070 ;; Ask for selection
3071 (message "Press key for agenda command%s:"
3072 (if (or restrict-ok org-agenda-overriding-restriction)
3073 (if org-agenda-overriding-restriction
3074 " (restriction lock active)"
3075 (if restriction
3076 (format " (restricted to %s)" restriction)
3077 " (unrestricted)"))
3078 ""))
3079 (setq c (read-char-exclusive))
3080 (message "")
3081 (cond
3082 ((assoc (char-to-string c) custom)
3083 (setq selstring (concat selstring (char-to-string c)))
3084 (throw 'exit (cons selstring restriction)))
3085 ((memq c prefixes)
3086 (setq selstring (concat selstring (char-to-string c))
3087 prefixes nil
3088 rmheader (or rmheader t)
3089 custom (delq nil (mapcar
3090 (lambda (x)
3091 (if (or (= (length (car x)) 1)
3092 (/= (string-to-char (car x)) c))
3094 (cons (substring (car x) 1) (cdr x))))
3095 custom))))
3096 ((eq c ?*)
3097 (call-interactively 'org-toggle-sticky-agenda)
3098 (sit-for 2))
3099 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
3100 (message "Restriction is only possible in Org buffers")
3101 (ding) (sit-for 1))
3102 ((eq c ?1)
3103 (org-agenda-remove-restriction-lock 'noupdate)
3104 (setq restriction 'buffer))
3105 ((eq c ?0)
3106 (org-agenda-remove-restriction-lock 'noupdate)
3107 (setq restriction (if region-p 'region 'subtree)))
3108 ((eq c ?<)
3109 (org-agenda-remove-restriction-lock 'noupdate)
3110 (setq restriction
3111 (cond
3112 ((eq restriction 'buffer)
3113 (if region-p 'region 'subtree))
3114 ((memq restriction '(subtree region))
3115 nil)
3116 (t 'buffer))))
3117 ((eq c ?>)
3118 (org-agenda-remove-restriction-lock 'noupdate)
3119 (setq restriction nil))
3120 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
3121 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
3122 ((and (> (length selstring) 0) (eq c ?\d))
3123 (delete-window)
3124 (org-agenda-get-restriction-and-command prefix-descriptions))
3126 ((equal c ?q) (error "Abort"))
3127 (t (user-error "Invalid key %c" c))))))))
3129 (defun org-agenda-fit-window-to-buffer ()
3130 "Fit the window to the buffer size."
3131 (and (memq org-agenda-window-setup '(reorganize-frame))
3132 (fboundp 'fit-window-to-buffer)
3133 (if (and (= (cdr org-agenda-window-frame-fractions) 1.0)
3134 (= (car org-agenda-window-frame-fractions) 1.0))
3135 (delete-other-windows)
3136 (org-fit-window-to-buffer
3138 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3139 (floor (* (frame-height) (car org-agenda-window-frame-fractions)))))))
3141 (defvar org-cmd nil)
3142 (defvar org-agenda-overriding-cmd nil)
3143 (defvar org-agenda-overriding-arguments nil)
3144 (defvar org-agenda-overriding-cmd-arguments nil)
3145 (defun org-agenda-run-series (name series)
3146 "Run agenda NAME as a SERIES of agenda commands."
3147 (org-let (nth 1 series) '(org-agenda-prepare name))
3148 ;; We need to reset agenda markers here, because when constructing a
3149 ;; block agenda, the individual blocks do not do that.
3150 (org-agenda-reset-markers)
3151 (let* ((org-agenda-multi t)
3152 (redo (list 'org-agenda-run-series name (list 'quote series)))
3153 (cmds (car series))
3154 (gprops (nth 1 series))
3155 match ;; The byte compiler incorrectly complains about this. Keep it!
3156 org-cmd type lprops)
3157 (while (setq org-cmd (pop cmds))
3158 (setq type (car org-cmd))
3159 (setq match (eval (nth 1 org-cmd)))
3160 (setq lprops (nth 2 org-cmd))
3161 (let ((org-agenda-overriding-arguments
3162 (if (eq org-agenda-overriding-cmd org-cmd)
3163 (or org-agenda-overriding-arguments
3164 org-agenda-overriding-cmd-arguments))))
3165 (cond
3166 ((eq type 'agenda)
3167 (org-let2 gprops lprops
3168 '(call-interactively 'org-agenda-list)))
3169 ((eq type 'agenda*)
3170 (org-let2 gprops lprops
3171 '(funcall 'org-agenda-list nil nil t)))
3172 ((eq type 'alltodo)
3173 (org-let2 gprops lprops
3174 '(call-interactively 'org-todo-list)))
3175 ((eq type 'search)
3176 (org-let2 gprops lprops
3177 '(org-search-view current-prefix-arg match nil)))
3178 ((eq type 'stuck)
3179 (org-let2 gprops lprops
3180 '(call-interactively 'org-agenda-list-stuck-projects)))
3181 ((eq type 'tags)
3182 (org-let2 gprops lprops
3183 '(org-tags-view current-prefix-arg match)))
3184 ((eq type 'tags-todo)
3185 (org-let2 gprops lprops
3186 '(org-tags-view '(4) match)))
3187 ((eq type 'todo)
3188 (org-let2 gprops lprops
3189 '(org-todo-list match)))
3190 ((fboundp type)
3191 (org-let2 gprops lprops
3192 '(funcall type match)))
3193 (t (error "Invalid type in command series")))))
3194 (widen)
3195 (let ((inhibit-read-only t))
3196 (add-text-properties (point-min) (point-max)
3197 `(org-series t org-series-redo-cmd ,redo)))
3198 (setq org-agenda-redo-command redo)
3199 (goto-char (point-min)))
3200 (org-agenda-fit-window-to-buffer)
3201 (org-let (nth 1 series) '(org-agenda-finalize)))
3203 ;;;###autoload
3204 (defmacro org-batch-agenda (cmd-key &rest parameters)
3205 "Run an agenda command in batch mode and send the result to STDOUT.
3206 If CMD-KEY is a string of length 1, it is used as a key in
3207 `org-agenda-custom-commands' and triggers this command. If it is a
3208 longer string it is used as a tags/todo match string.
3209 Parameters are alternating variable names and values that will be bound
3210 before running the agenda command."
3211 (org-eval-in-environment (org-make-parameter-alist parameters)
3212 (let (org-agenda-sticky)
3213 (if (> (length cmd-key) 1)
3214 (org-tags-view nil cmd-key)
3215 (org-agenda nil cmd-key))))
3216 (set-buffer org-agenda-buffer-name)
3217 (princ (buffer-string)))
3219 (defvar org-agenda-info nil)
3221 ;;;###autoload
3222 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
3223 "Run an agenda command in batch mode and send the result to STDOUT.
3224 If CMD-KEY is a string of length 1, it is used as a key in
3225 `org-agenda-custom-commands' and triggers this command. If it is a
3226 longer string it is used as a tags/todo match string.
3227 Parameters are alternating variable names and values that will be bound
3228 before running the agenda command.
3230 The output gives a line for each selected agenda item. Each
3231 item is a list of comma-separated values, like this:
3233 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3235 category The category of the item
3236 head The headline, without TODO kwd, TAGS and PRIORITY
3237 type The type of the agenda entry, can be
3238 todo selected in TODO match
3239 tagsmatch selected in tags match
3240 diary imported from diary
3241 deadline a deadline on given date
3242 scheduled scheduled on given date
3243 timestamp entry has timestamp on given date
3244 closed entry was closed on given date
3245 upcoming-deadline warning about deadline
3246 past-scheduled forwarded scheduled item
3247 block entry has date block including g. date
3248 todo The todo keyword, if any
3249 tags All tags including inherited ones, separated by colons
3250 date The relevant date, like 2007-2-14
3251 time The time, like 15:00-16:50
3252 extra Sting with extra planning info
3253 priority-l The priority letter if any was given
3254 priority-n The computed numerical priority
3255 agenda-day The day in the agenda where this is listed"
3256 (org-eval-in-environment (append '((org-agenda-remove-tags t))
3257 (org-make-parameter-alist parameters))
3258 (if (> (length cmd-key) 2)
3259 (org-tags-view nil cmd-key)
3260 (org-agenda nil cmd-key)))
3261 (set-buffer org-agenda-buffer-name)
3262 (let ((lines (org-split-string (buffer-string) "\n")))
3263 (dolist (line lines)
3264 (when (get-text-property 0 'org-category line)
3265 (setq org-agenda-info
3266 (org-fix-agenda-info (text-properties-at 0 line)))
3267 (princ
3268 (mapconcat 'org-agenda-export-csv-mapper
3269 '(org-category txt type todo tags date time extra
3270 priority-letter priority agenda-day)
3271 ","))
3272 (princ "\n")))))
3274 (defun org-fix-agenda-info (props)
3275 "Make sure all properties on an agenda item have a canonical form.
3276 This ensures the export commands can easily use it."
3277 (let (tmp re)
3278 (when (setq tmp (plist-get props 'tags))
3279 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3280 (when (setq tmp (plist-get props 'date))
3281 (when (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3282 (let ((calendar-date-display-form '(year "-" month "-" day)))
3283 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3285 (setq tmp (calendar-date-string tmp)))
3286 (setq props (plist-put props 'date tmp)))
3287 (when (setq tmp (plist-get props 'day))
3288 (when (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3289 (let ((calendar-date-display-form '(year "-" month "-" day)))
3290 (setq tmp (calendar-date-string tmp)))
3291 (setq props (plist-put props 'day tmp))
3292 (setq props (plist-put props 'agenda-day tmp)))
3293 (when (setq tmp (plist-get props 'txt))
3294 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3295 (plist-put props 'priority-letter (match-string 1 tmp))
3296 (setq tmp (replace-match "" t t tmp)))
3297 (when (and (setq re (plist-get props 'org-todo-regexp))
3298 (setq re (concat "\\`\\.*" re " ?"))
3299 (let ((case-fold-search nil)) (string-match re tmp)))
3300 (plist-put props 'todo (match-string 1 tmp))
3301 (setq tmp (replace-match "" t t tmp)))
3302 (plist-put props 'txt tmp)))
3303 props)
3305 (defun org-agenda-export-csv-mapper (prop)
3306 (let ((res (plist-get org-agenda-info prop)))
3307 (setq res
3308 (cond
3309 ((not res) "")
3310 ((stringp res) res)
3311 (t (prin1-to-string res))))
3312 (org-trim (replace-regexp-in-string "," ";" res nil t))))
3314 ;;;###autoload
3315 (defun org-store-agenda-views (&rest parameters)
3316 "Store agenda views."
3317 (interactive)
3318 (eval (list 'org-batch-store-agenda-views)))
3320 ;;;###autoload
3321 (defmacro org-batch-store-agenda-views (&rest parameters)
3322 "Run all custom agenda commands that have a file argument."
3323 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3324 (pop-up-frames nil)
3325 (dir default-directory)
3326 (pars (org-make-parameter-alist parameters))
3327 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
3328 (save-window-excursion
3329 (while cmds
3330 (setq cmd (pop cmds)
3331 thiscmdkey (car cmd)
3332 thiscmdcmd (cdr cmd)
3333 match (nth 2 thiscmdcmd)
3334 bufname (if org-agenda-sticky
3335 (or (and (stringp match)
3336 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3337 (format "*Org Agenda(%s)*" thiscmdkey))
3338 org-agenda-buffer-name)
3339 cmd-or-set (nth 2 cmd)
3340 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3341 files (nth (if (listp cmd-or-set) 4 5) cmd))
3342 (if (stringp files) (setq files (list files)))
3343 (when files
3344 (org-eval-in-environment (append org-agenda-exporter-settings
3345 opts pars)
3346 (org-agenda nil thiscmdkey))
3347 (set-buffer bufname)
3348 (while files
3349 (org-eval-in-environment (append org-agenda-exporter-settings
3350 opts pars)
3351 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3352 (and (get-buffer bufname)
3353 (kill-buffer bufname)))))))
3355 (defvar org-agenda-current-span nil
3356 "The current span used in the agenda view.") ; local variable in the agenda buffer
3357 (defun org-agenda-mark-header-line (pos)
3358 "Mark the line at POS as an agenda structure header."
3359 (save-excursion
3360 (goto-char pos)
3361 (put-text-property (point-at-bol) (point-at-eol)
3362 'org-agenda-structural-header t)
3363 (when org-agenda-title-append
3364 (put-text-property (point-at-bol) (point-at-eol)
3365 'org-agenda-title-append org-agenda-title-append))))
3367 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3368 (defvar org-agenda-write-buffer-name "Agenda View")
3369 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3370 "Write the current buffer (an agenda view) as a file.
3372 Depending on the extension of the file name, plain text (.txt),
3373 HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
3374 If the extension is .ics, translate visible agenda into iCalendar
3375 format. If the extension is .org, collect all subtrees
3376 corresponding to the agenda entries and add them in an .org file.
3378 With prefix argument OPEN, open the new file immediately. If
3379 NOSETTINGS is given, do not scope the settings of
3380 `org-agenda-exporter-settings' into the export commands. This is
3381 used when the settings have already been scoped and we do not
3382 wish to overrule other, higher priority settings. If
3383 AGENDA-BUFFER-NAME is provided, use this as the buffer name for
3384 the agenda to write."
3385 (interactive "FWrite agenda to file: \nP")
3386 (if (or (not (file-writable-p file))
3387 (and (file-exists-p file)
3388 (if (called-interactively-p 'any)
3389 (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
3390 (user-error "Cannot write agenda to file %s" file))
3391 (org-let (if nosettings nil org-agenda-exporter-settings)
3392 '(save-excursion
3393 (save-window-excursion
3394 (let ((bs (copy-sequence (buffer-string)))
3395 (extension (file-name-extension file))
3396 (default-directory (file-name-directory file))
3397 beg content)
3398 (with-temp-buffer
3399 (rename-buffer org-agenda-write-buffer-name t)
3400 (set-buffer-modified-p nil)
3401 (insert bs)
3402 (org-agenda-remove-marked-text 'invisible 'org-filtered)
3403 (run-hooks 'org-agenda-before-write-hook)
3404 (cond
3405 ((bound-and-true-p org-mobile-creating-agendas)
3406 (org-mobile-write-agenda-for-mobile file))
3407 ((string= "org" extension)
3408 (let (content p m message-log-max)
3409 (goto-char (point-min))
3410 (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
3411 (goto-char p)
3412 (setq m (get-text-property (point) 'org-hd-marker))
3413 (when m
3414 (push (save-excursion
3415 (set-buffer (marker-buffer m))
3416 (goto-char m)
3417 (org-copy-subtree 1 nil t t)
3418 org-subtree-clip)
3419 content)))
3420 (find-file file)
3421 (erase-buffer)
3422 (dolist (s content) (org-paste-subtree 1 s))
3423 (write-file file)
3424 (kill-buffer (current-buffer))
3425 (message "Org file written to %s" file)))
3426 ((member extension '("html" "htm"))
3427 (or (require 'htmlize nil t)
3428 (error "Please install htmlize from https://github.com/hniksic/emacs-htmlize"))
3429 (set-buffer (htmlize-buffer (current-buffer)))
3430 (when org-agenda-export-html-style
3431 ;; replace <style> section with org-agenda-export-html-style
3432 (goto-char (point-min))
3433 (kill-region (- (search-forward "<style") 6)
3434 (search-forward "</style>"))
3435 (insert org-agenda-export-html-style))
3436 (write-file file)
3437 (kill-buffer (current-buffer))
3438 (message "HTML written to %s" file))
3439 ((string= "ps" extension)
3440 (require 'ps-print)
3441 (ps-print-buffer-with-faces file)
3442 (message "Postscript written to %s" file))
3443 ((string= "pdf" extension)
3444 (require 'ps-print)
3445 (ps-print-buffer-with-faces
3446 (concat (file-name-sans-extension file) ".ps"))
3447 (call-process "ps2pdf" nil nil nil
3448 (expand-file-name
3449 (concat (file-name-sans-extension file) ".ps"))
3450 (expand-file-name file))
3451 (delete-file (concat (file-name-sans-extension file) ".ps"))
3452 (message "PDF written to %s" file))
3453 ((string= "ics" extension)
3454 (require 'ox-icalendar)
3455 (org-icalendar-export-current-agenda (expand-file-name file)))
3457 (let ((bs (buffer-string)))
3458 (find-file file)
3459 (erase-buffer)
3460 (insert bs)
3461 (save-buffer 0)
3462 (kill-buffer (current-buffer))
3463 (message "Plain text written to %s" file))))))))
3464 (set-buffer (or agenda-bufname
3465 (and (called-interactively-p 'any) (buffer-name))
3466 org-agenda-buffer-name)))
3467 (when open (org-open-file file)))
3469 (defun org-agenda-remove-marked-text (property &optional value)
3470 "Delete all text marked with VALUE of PROPERTY.
3471 VALUE defaults to t."
3472 (let (beg)
3473 (setq value (or value t))
3474 (while (setq beg (text-property-any (point-min) (point-max)
3475 property value))
3476 (delete-region
3477 beg (or (next-single-property-change beg property)
3478 (point-max))))))
3480 (defun org-agenda-add-entry-text ()
3481 "Add entry text to agenda lines.
3482 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3483 entry text following headings shown in the agenda.
3484 Drawers will be excluded, also the line with scheduling/deadline info."
3485 (when (and (> org-agenda-add-entry-text-maxlines 0)
3486 (not (bound-and-true-p org-mobile-creating-agendas)))
3487 (let (m txt)
3488 (goto-char (point-min))
3489 (while (not (eobp))
3490 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3491 (beginning-of-line 2)
3492 (setq txt (org-agenda-get-some-entry-text
3493 m org-agenda-add-entry-text-maxlines " > "))
3494 (end-of-line 1)
3495 (if (string-match "\\S-" txt)
3496 (insert "\n" txt)
3497 (or (eobp) (forward-char 1))))))))
3499 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3500 &rest keep)
3501 "Extract entry text from MARKER, at most N-LINES lines.
3502 This will ignore drawers etc, just get the text.
3503 If INDENT is given, prefix every line with this string. If KEEP is
3504 given, it is a list of symbols, defining stuff that should not be
3505 removed from the entry content. Currently only `planning' is allowed here."
3506 (let (txt drawer-re kwd-time-re ind)
3507 (save-excursion
3508 (with-current-buffer (marker-buffer marker)
3509 (if (not (derived-mode-p 'org-mode))
3510 (setq txt "")
3511 (org-with-wide-buffer
3512 (goto-char marker)
3513 (end-of-line 1)
3514 (setq txt (buffer-substring
3515 (min (1+ (point)) (point-max))
3516 (progn (outline-next-heading) (point)))
3517 drawer-re org-drawer-regexp
3518 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3519 ".*\n?"))
3520 (with-temp-buffer
3521 (insert txt)
3522 (when org-agenda-add-entry-text-descriptive-links
3523 (goto-char (point-min))
3524 (while (org-activate-links (point-max))
3525 (add-text-properties (match-beginning 0) (match-end 0)
3526 '(face org-link))))
3527 (goto-char (point-min))
3528 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3529 (set-text-properties (match-beginning 0) (match-end 0)
3530 nil))
3531 (goto-char (point-min))
3532 (while (re-search-forward drawer-re nil t)
3533 (delete-region
3534 (match-beginning 0)
3535 (progn (re-search-forward
3536 "^[ \t]*:END:.*\n?" nil 'move)
3537 (point))))
3538 (unless (member 'planning keep)
3539 (goto-char (point-min))
3540 (while (re-search-forward kwd-time-re nil t)
3541 (replace-match "")))
3542 (goto-char (point-min))
3543 (when org-agenda-entry-text-exclude-regexps
3544 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3545 (while (setq re (pop re-list))
3546 (goto-char (point-min))
3547 (while (re-search-forward re nil t)
3548 (replace-match "")))))
3549 (goto-char (point-max))
3550 (skip-chars-backward " \t\n")
3551 (when (looking-at "[ \t\n]+\\'") (replace-match ""))
3553 ;; find and remove min common indentation
3554 (goto-char (point-min))
3555 (untabify (point-min) (point-max))
3556 (setq ind (current-indentation))
3557 (while (not (eobp))
3558 (unless (looking-at "[ \t]*$")
3559 (setq ind (min ind (current-indentation))))
3560 (beginning-of-line 2))
3561 (goto-char (point-min))
3562 (while (not (eobp))
3563 (unless (looking-at "[ \t]*$")
3564 (move-to-column ind)
3565 (delete-region (point-at-bol) (point)))
3566 (beginning-of-line 2))
3568 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3570 (goto-char (point-min))
3571 (when indent
3572 (while (and (not (eobp)) (re-search-forward "^" nil t))
3573 (replace-match indent t t)))
3574 (goto-char (point-min))
3575 (while (looking-at "[ \t]*\n") (replace-match ""))
3576 (goto-char (point-max))
3577 (when (> (org-current-line)
3578 n-lines)
3579 (org-goto-line (1+ n-lines))
3580 (backward-char 1))
3581 (setq txt (buffer-substring (point-min) (point))))))))
3582 txt))
3584 (defun org-check-for-org-mode ()
3585 "Make sure current buffer is in Org mode. Error if not."
3586 (or (derived-mode-p 'org-mode)
3587 (error "Cannot execute Org agenda command on buffer in %s"
3588 major-mode)))
3590 ;;; Agenda prepare and finalize
3592 (defvar org-agenda-multi nil) ; dynamically scoped
3593 (defvar org-agenda-pre-window-conf nil)
3594 (defvar org-agenda-columns-active nil)
3595 (defvar org-agenda-name nil)
3596 (defvar org-agenda-tag-filter nil)
3597 (defvar org-agenda-category-filter nil)
3598 (defvar org-agenda-regexp-filter nil)
3599 (defvar org-agenda-effort-filter nil)
3600 (defvar org-agenda-top-headline-filter nil)
3601 (defvar org-agenda-tag-filter-preset nil
3602 "A preset of the tags filter used for secondary agenda filtering.
3603 This must be a list of strings, each string must be a single tag preceded
3604 by \"+\" or \"-\".
3605 This variable should not be set directly, but agenda custom commands can
3606 bind it in the options section. The preset filter is a global property of
3607 the entire agenda view. In a block agenda, it will not work reliably to
3608 define a filter for one of the individual blocks. You need to set it in
3609 the global options and expect it to be applied to the entire view.")
3611 (defvar org-agenda-category-filter-preset nil
3612 "A preset of the category filter used for secondary agenda filtering.
3613 This must be a list of strings, each string must be a single category
3614 preceded by \"+\" or \"-\".
3615 This variable should not be set directly, but agenda custom commands can
3616 bind it in the options section. The preset filter is a global property of
3617 the entire agenda view. In a block agenda, it will not work reliably to
3618 define a filter for one of the individual blocks. You need to set it in
3619 the global options and expect it to be applied to the entire view.")
3621 (defvar org-agenda-regexp-filter-preset nil
3622 "A preset of the regexp filter used for secondary agenda filtering.
3623 This must be a list of strings, each string must be a single regexp
3624 preceded by \"+\" or \"-\".
3625 This variable should not be set directly, but agenda custom commands can
3626 bind it in the options section. The preset filter is a global property of
3627 the entire agenda view. In a block agenda, it will not work reliably to
3628 define a filter for one of the individual blocks. You need to set it in
3629 the global options and expect it to be applied to the entire view.")
3631 (defvar org-agenda-effort-filter-preset nil
3632 "A preset of the effort condition used for secondary agenda filtering.
3633 This must be a list of strings, each string must be a single regexp
3634 preceded by \"+\" or \"-\".
3635 This variable should not be set directly, but agenda custom commands can
3636 bind it in the options section. The preset filter is a global property of
3637 the entire agenda view. In a block agenda, it will not work reliably to
3638 define a filter for one of the individual blocks. You need to set it in
3639 the global options and expect it to be applied to the entire view.")
3641 (defun org-agenda-use-sticky-p ()
3642 "Return non-nil if an agenda buffer named
3643 `org-agenda-buffer-name' exists and should be shown instead of
3644 generating a new one."
3645 (and
3646 ;; turned off by user
3647 org-agenda-sticky
3648 ;; For multi-agenda buffer already exists
3649 (not org-agenda-multi)
3650 ;; buffer found
3651 (get-buffer org-agenda-buffer-name)
3652 ;; C-u parameter is same as last call
3653 (with-current-buffer (get-buffer org-agenda-buffer-name)
3654 (and
3655 (equal current-prefix-arg
3656 org-agenda-last-prefix-arg)
3657 ;; In case user turned stickiness on, while having existing
3658 ;; Agenda buffer active, don't reuse that buffer, because it
3659 ;; does not have org variables local
3660 org-agenda-this-buffer-is-sticky))))
3662 (defun org-agenda-prepare-window (abuf filter-alist)
3663 "Setup agenda buffer in the window.
3664 ABUF is the buffer for the agenda window.
3665 FILTER-ALIST is an alist of filters we need to apply when
3666 `org-agenda-persistent-filter' is non-nil."
3667 (let* ((awin (get-buffer-window abuf)) wconf)
3668 (cond
3669 ((equal (current-buffer) abuf) nil)
3670 (awin (select-window awin))
3671 ((not (setq wconf (current-window-configuration))))
3672 ((eq org-agenda-window-setup 'current-window)
3673 (pop-to-buffer-same-window abuf))
3674 ((eq org-agenda-window-setup 'other-window)
3675 (org-switch-to-buffer-other-window abuf))
3676 ((eq org-agenda-window-setup 'other-frame)
3677 (switch-to-buffer-other-frame abuf))
3678 ((eq org-agenda-window-setup 'only-window)
3679 (delete-other-windows)
3680 (pop-to-buffer-same-window abuf))
3681 ((eq org-agenda-window-setup 'reorganize-frame)
3682 (delete-other-windows)
3683 (org-switch-to-buffer-other-window abuf)))
3684 (setq org-agenda-tag-filter (cdr (assq 'tag filter-alist)))
3685 (setq org-agenda-category-filter (cdr (assq 'cat filter-alist)))
3686 (setq org-agenda-effort-filter (cdr (assq 'effort filter-alist)))
3687 (setq org-agenda-regexp-filter (cdr (assq 're filter-alist)))
3688 ;; Additional test in case agenda is invoked from within agenda
3689 ;; buffer via elisp link.
3690 (unless (equal (current-buffer) abuf)
3691 (pop-to-buffer-same-window abuf))
3692 (setq org-agenda-pre-window-conf
3693 (or wconf org-agenda-pre-window-conf))))
3695 (defun org-agenda-prepare (&optional name)
3696 (let ((filter-alist (when org-agenda-persistent-filter
3697 (with-current-buffer
3698 (get-buffer-create org-agenda-buffer-name)
3699 `((tag . ,org-agenda-tag-filter)
3700 (re . ,org-agenda-regexp-filter)
3701 (effort . ,org-agenda-effort-filter)
3702 (cat . ,org-agenda-category-filter))))))
3703 (if (org-agenda-use-sticky-p)
3704 (progn
3705 (put 'org-agenda-tag-filter :preset-filter nil)
3706 (put 'org-agenda-category-filter :preset-filter nil)
3707 (put 'org-agenda-regexp-filter :preset-filter nil)
3708 ;; Popup existing buffer
3709 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name)
3710 filter-alist)
3711 (message "Sticky Agenda buffer, use `r' to refresh")
3712 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3713 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3714 (setq org-todo-keywords-for-agenda nil)
3715 (put 'org-agenda-tag-filter :preset-filter
3716 org-agenda-tag-filter-preset)
3717 (put 'org-agenda-category-filter :preset-filter
3718 org-agenda-category-filter-preset)
3719 (put 'org-agenda-regexp-filter :preset-filter
3720 org-agenda-regexp-filter-preset)
3721 (put 'org-agenda-effort-filter :preset-filter
3722 org-agenda-effort-filter-preset)
3723 (if org-agenda-multi
3724 (progn
3725 (setq buffer-read-only nil)
3726 (goto-char (point-max))
3727 (unless (or (bobp) org-agenda-compact-blocks
3728 (not org-agenda-block-separator))
3729 (insert "\n"
3730 (if (stringp org-agenda-block-separator)
3731 org-agenda-block-separator
3732 (make-string (window-width) org-agenda-block-separator))
3733 "\n"))
3734 (narrow-to-region (point) (point-max)))
3735 (setq org-done-keywords-for-agenda nil)
3736 ;; Setting any org variables that are in org-agenda-local-vars
3737 ;; list need to be done after the prepare call
3738 (org-agenda-prepare-window
3739 (get-buffer-create org-agenda-buffer-name) filter-alist)
3740 (setq buffer-read-only nil)
3741 (org-agenda-reset-markers)
3742 (let ((inhibit-read-only t)) (erase-buffer))
3743 (org-agenda-mode)
3744 (setq org-agenda-buffer (current-buffer))
3745 (setq org-agenda-contributing-files nil)
3746 (setq org-agenda-columns-active nil)
3747 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3748 (setq org-todo-keywords-for-agenda
3749 (org-uniquify org-todo-keywords-for-agenda))
3750 (setq org-done-keywords-for-agenda
3751 (org-uniquify org-done-keywords-for-agenda))
3752 (setq org-agenda-last-prefix-arg current-prefix-arg)
3753 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3754 (and name (not org-agenda-name)
3755 (setq-local org-agenda-name name)))
3756 (setq buffer-read-only nil))))
3758 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3759 (defun org-agenda-finalize ()
3760 "Finishing touch for the agenda buffer, called just before displaying it."
3761 (unless org-agenda-multi
3762 (save-excursion
3763 (let ((inhibit-read-only t))
3764 (goto-char (point-min))
3765 (save-excursion
3766 (while (org-activate-links (point-max))
3767 (add-text-properties (match-beginning 0) (match-end 0)
3768 '(face org-link))))
3769 (unless (eq org-agenda-remove-tags t)
3770 (org-agenda-align-tags))
3771 (unless org-agenda-with-colors
3772 (remove-text-properties (point-min) (point-max) '(face nil)))
3773 (when (bound-and-true-p org-agenda-overriding-columns-format)
3774 (setq-local org-agenda-overriding-columns-format
3775 org-agenda-overriding-columns-format))
3776 (when org-agenda-view-columns-initially
3777 (org-agenda-columns))
3778 (when org-agenda-fontify-priorities
3779 (org-agenda-fontify-priorities))
3780 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3781 (org-agenda-dim-blocked-tasks))
3782 (org-agenda-mark-clocking-task)
3783 (when org-agenda-entry-text-mode
3784 (org-agenda-entry-text-hide)
3785 (org-agenda-entry-text-show))
3786 (when (and (featurep 'org-habit)
3787 (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
3788 (org-habit-insert-consistency-graphs))
3789 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3790 (unless (or (eq org-agenda-show-inherited-tags 'always)
3791 (and (listp org-agenda-show-inherited-tags)
3792 (memq org-agenda-type org-agenda-show-inherited-tags))
3793 (and (eq org-agenda-show-inherited-tags t)
3794 (or (eq org-agenda-use-tag-inheritance t)
3795 (and (listp org-agenda-use-tag-inheritance)
3796 (not (memq org-agenda-type
3797 org-agenda-use-tag-inheritance))))))
3798 (let (mrk)
3799 (save-excursion
3800 (goto-char (point-min))
3801 (while (equal (forward-line) 0)
3802 (when (setq mrk (get-text-property (point) 'org-hd-marker))
3803 (put-text-property (point-at-bol) (point-at-eol)
3804 'tags
3805 (org-with-point-at mrk
3806 (mapcar #'downcase (org-get-tags)))))))))
3807 (run-hooks 'org-agenda-finalize-hook)
3808 (when org-agenda-top-headline-filter
3809 (org-agenda-filter-top-headline-apply
3810 org-agenda-top-headline-filter))
3811 (when org-agenda-tag-filter
3812 (org-agenda-filter-apply org-agenda-tag-filter 'tag t))
3813 (when (get 'org-agenda-tag-filter :preset-filter)
3814 (org-agenda-filter-apply
3815 (get 'org-agenda-tag-filter :preset-filter) 'tag t))
3816 (when org-agenda-category-filter
3817 (org-agenda-filter-apply org-agenda-category-filter 'category))
3818 (when (get 'org-agenda-category-filter :preset-filter)
3819 (org-agenda-filter-apply
3820 (get 'org-agenda-category-filter :preset-filter) 'category))
3821 (when org-agenda-regexp-filter
3822 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
3823 (when (get 'org-agenda-regexp-filter :preset-filter)
3824 (org-agenda-filter-apply
3825 (get 'org-agenda-regexp-filter :preset-filter) 'regexp))
3826 (when org-agenda-effort-filter
3827 (org-agenda-filter-apply org-agenda-effort-filter 'effort))
3828 (when (get 'org-agenda-effort-filter :preset-filter)
3829 (org-agenda-filter-apply
3830 (get 'org-agenda-effort-filter :preset-filter) 'effort))
3831 (add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)))))
3833 (defun org-agenda-mark-clocking-task ()
3834 "Mark the current clock entry in the agenda if it is present."
3835 ;; We need to widen when `org-agenda-finalize' is called from
3836 ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
3837 (when (bound-and-true-p org-clock-current-task)
3838 (save-restriction
3839 (widen)
3840 (org-agenda-unmark-clocking-task)
3841 (when (marker-buffer org-clock-hd-marker)
3842 (save-excursion
3843 (goto-char (point-min))
3844 (let (s ov)
3845 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3846 (goto-char s)
3847 (when (equal (org-get-at-bol 'org-hd-marker)
3848 org-clock-hd-marker)
3849 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3850 (overlay-put ov 'type 'org-agenda-clocking)
3851 (overlay-put ov 'face 'org-agenda-clocking)
3852 (overlay-put ov 'help-echo
3853 "The clock is running in this item")))))))))
3855 (defun org-agenda-unmark-clocking-task ()
3856 "Unmark the current clocking task."
3857 (mapc (lambda (o)
3858 (when (eq (overlay-get o 'type) 'org-agenda-clocking)
3859 (delete-overlay o)))
3860 (overlays-in (point-min) (point-max))))
3862 (defun org-agenda-fontify-priorities ()
3863 "Make highest priority lines bold, and lowest italic."
3864 (interactive)
3865 (mapc (lambda (o) (when (eq (overlay-get o 'org-type) 'org-priority)
3866 (delete-overlay o)))
3867 (overlays-in (point-min) (point-max)))
3868 (save-excursion
3869 (let (b e p ov h l)
3870 (goto-char (point-min))
3871 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3872 (setq h (or (get-char-property (point) 'org-highest-priority)
3873 org-highest-priority)
3874 l (or (get-char-property (point) 'org-lowest-priority)
3875 org-lowest-priority)
3876 p (string-to-char (match-string 1))
3877 b (match-beginning 0)
3878 e (if (eq org-agenda-fontify-priorities 'cookies)
3879 (match-end 0)
3880 (point-at-eol))
3881 ov (make-overlay b e))
3882 (overlay-put
3883 ov 'face
3884 (let ((special-face
3885 (cond ((org-face-from-face-or-color
3886 'priority 'org-priority
3887 (cdr (assoc p org-priority-faces))))
3888 ((and (listp org-agenda-fontify-priorities)
3889 (org-face-from-face-or-color
3890 'priority 'org-priority
3891 (cdr (assoc p org-agenda-fontify-priorities)))))
3892 ((equal p l) 'italic)
3893 ((equal p h) 'bold))))
3894 (if special-face (list special-face 'org-priority) 'org-priority)))
3895 (overlay-put ov 'org-type 'org-priority)))))
3897 (defvar org-depend-tag-blocked)
3899 (defun org-agenda-dim-blocked-tasks (&optional invisible)
3900 "Dim currently blocked TODOs in the agenda display.
3901 When INVISIBLE is non-nil, hide currently blocked TODO instead of
3902 dimming them."
3903 (interactive "P")
3904 (when (called-interactively-p 'interactive)
3905 (message "Dim or hide blocked tasks..."))
3906 (dolist (o (overlays-in (point-min) (point-max)))
3907 (when (eq (overlay-get o 'org-type) 'org-blocked-todo)
3908 (delete-overlay o)))
3909 (save-excursion
3910 (let ((inhibit-read-only t))
3911 (goto-char (point-min))
3912 (while (let ((pos (text-property-not-all
3913 (point) (point-max) 'org-todo-blocked nil)))
3914 (when pos (goto-char pos)))
3915 (let* ((invisible (eq (org-get-at-bol 'org-todo-blocked) 'invisible))
3916 (ov (make-overlay (if invisible
3917 (line-end-position 0)
3918 (line-beginning-position))
3919 (line-end-position))))
3920 (if invisible
3921 (overlay-put ov 'invisible t)
3922 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3923 (overlay-put ov 'org-type 'org-blocked-todo))
3924 (forward-line))))
3925 (when (called-interactively-p 'interactive)
3926 (message "Dim or hide blocked tasks...done")))
3928 (defun org-agenda--mark-blocked-entry (entry)
3929 "For ENTRY a string with the text property `org-hd-marker', if
3930 the header at `org-hd-marker' is blocked according to
3931 `org-entry-blocked-p', then if `org-agenda-dim-blocked-tasks' is
3932 'invisible and the header is not blocked by checkboxes, set the
3933 text property `org-todo-blocked' to 'invisible, otherwise set it
3934 to t."
3935 (when (get-text-property 0 'todo-state entry)
3936 (let ((entry-marker (get-text-property 0 'org-hd-marker entry))
3937 (org-blocked-by-checkboxes nil)
3938 ;; Necessary so that `org-entry-blocked-p' does not change
3939 ;; the buffer.
3940 (org-depend-tag-blocked nil))
3941 (when entry-marker
3942 (let ((blocked
3943 (with-current-buffer (marker-buffer entry-marker)
3944 (save-excursion
3945 (goto-char entry-marker)
3946 (org-entry-blocked-p)))))
3947 (when blocked
3948 (let ((really-invisible
3949 (and (not org-blocked-by-checkboxes)
3950 (eq org-agenda-dim-blocked-tasks 'invisible))))
3951 (put-text-property
3952 0 (length entry) 'org-todo-blocked
3953 (if really-invisible 'invisible t)
3954 entry)))))))
3955 entry)
3957 (defvar org-agenda-skip-function nil
3958 "Function to be called at each match during agenda construction.
3959 If this function returns nil, the current match should not be skipped.
3960 Otherwise, the function must return a position from where the search
3961 should be continued.
3962 This may also be a Lisp form, it will be evaluated.
3963 Never set this variable using `setq' or so, because then it will apply
3964 to all future agenda commands. If you do want a global skipping condition,
3965 use the option `org-agenda-skip-function-global' instead.
3966 The correct usage for `org-agenda-skip-function' is to bind it with
3967 `let' to scope it dynamically into the agenda-constructing command.
3968 A good way to set it is through options in `org-agenda-custom-commands'.")
3970 (defun org-agenda-skip ()
3971 "Throw to `:skip' in places that should be skipped.
3972 Also moves point to the end of the skipped region, so that search can
3973 continue from there."
3974 (let ((p (point-at-bol)) to)
3975 (when (or
3976 (save-excursion (goto-char p) (looking-at comment-start-skip))
3977 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3978 (get-text-property p :org-archived)
3979 (org-end-of-subtree t))
3980 (and org-agenda-skip-comment-trees
3981 (get-text-property p :org-comment)
3982 (org-end-of-subtree t))
3983 (and (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3984 (org-agenda-skip-eval org-agenda-skip-function)))
3985 (goto-char to))
3986 (org-in-src-block-p t))
3987 (throw :skip t))))
3989 (defun org-agenda-skip-eval (form)
3990 "If FORM is a function or a list, call (or eval) it and return the result.
3991 `save-excursion' and `save-match-data' are wrapped around the call, so point
3992 and match data are returned to the previous state no matter what these
3993 functions do."
3994 (let (fp)
3995 (and form
3996 (or (setq fp (functionp form))
3997 (consp form))
3998 (save-excursion
3999 (save-match-data
4000 (if fp
4001 (funcall form)
4002 (eval form)))))))
4004 (defvar org-agenda-markers nil
4005 "List of all currently active markers created by `org-agenda'.")
4006 (defvar org-agenda-last-marker-time (float-time)
4007 "Creation time of the last agenda marker.")
4009 (defun org-agenda-new-marker (&optional pos)
4010 "Return a new agenda marker.
4011 Maker is at point, or at POS if non-nil. Org mode keeps a list of
4012 these markers and resets them when they are no longer in use."
4013 (let ((m (copy-marker (or pos (point)) t)))
4014 (setq org-agenda-last-marker-time (float-time))
4015 (if org-agenda-buffer
4016 (with-current-buffer org-agenda-buffer
4017 (push m org-agenda-markers))
4018 (push m org-agenda-markers))
4021 (defun org-agenda-reset-markers ()
4022 "Reset markers created by `org-agenda'."
4023 (while org-agenda-markers
4024 (move-marker (pop org-agenda-markers) nil)))
4026 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
4027 "Save relative positions of markers in region.
4028 This check for agenda markers in all agenda buffers currently active."
4029 (dolist (buf (buffer-list))
4030 (with-current-buffer buf
4031 (when (eq major-mode 'org-agenda-mode)
4032 (mapc (lambda (m) (org-check-and-save-marker m beg end))
4033 org-agenda-markers)))))
4035 ;;; Entry text mode
4037 (defun org-agenda-entry-text-show-here ()
4038 "Add some text from the entry as context to the current line."
4039 (let (m txt o)
4040 (setq m (org-get-at-bol 'org-hd-marker))
4041 (unless (marker-buffer m)
4042 (error "No marker points to an entry here"))
4043 (setq txt (concat "\n" (org-no-properties
4044 (org-agenda-get-some-entry-text
4045 m org-agenda-entry-text-maxlines
4046 org-agenda-entry-text-leaders))))
4047 (when (string-match "\\S-" txt)
4048 (setq o (make-overlay (point-at-bol) (point-at-eol)))
4049 (overlay-put o 'evaporate t)
4050 (overlay-put o 'org-overlay-type 'agenda-entry-content)
4051 (overlay-put o 'after-string txt))))
4053 (defun org-agenda-entry-text-show ()
4054 "Add entry context for all agenda lines."
4055 (interactive)
4056 (save-excursion
4057 (goto-char (point-max))
4058 (beginning-of-line 1)
4059 (while (not (bobp))
4060 (when (org-get-at-bol 'org-hd-marker)
4061 (org-agenda-entry-text-show-here))
4062 (beginning-of-line 0))))
4064 (defun org-agenda-entry-text-hide ()
4065 "Remove any shown entry context."
4066 (mapc (lambda (o)
4067 (when (eq (overlay-get o 'org-overlay-type)
4068 'agenda-entry-content)
4069 (delete-overlay o)))
4070 (overlays-in (point-min) (point-max))))
4072 (defun org-agenda-get-day-face (date)
4073 "Return the face DATE should be displayed with."
4074 (cond ((and (functionp org-agenda-day-face-function)
4075 (funcall org-agenda-day-face-function date)))
4076 ((org-agenda-today-p date) 'org-agenda-date-today)
4077 ((memq (calendar-day-of-week date) org-agenda-weekend-days)
4078 'org-agenda-date-weekend)
4079 (t 'org-agenda-date)))
4081 (defvar org-agenda-show-log-scoped)
4083 ;;; Agenda Daily/Weekly
4085 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4086 "Start day for the agenda view.
4087 Custom commands can set this variable in the options section.
4088 This is usually a string like \"2007-11-01\", \"+2d\" or any other
4089 input allowed when reading a date through the Org calendar.
4090 See the docstring of `org-read-date' for details.")
4091 (defvar org-starting-day nil) ; local variable in the agenda buffer
4092 (defvar org-arg-loc nil) ; local variable
4094 (defvar org-agenda-buffer-tmp-name nil)
4095 ;;;###autoload
4096 (defun org-agenda-list (&optional arg start-day span with-hour)
4097 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4098 The view will be for the current day or week, but from the overview buffer
4099 you will be able to go to other days/weeks.
4101 With a numeric prefix argument in an interactive call, the agenda will
4102 span ARG days. Lisp programs should instead specify SPAN to change
4103 the number of days. SPAN defaults to `org-agenda-span'.
4105 START-DAY defaults to TODAY, or to the most recent match for the weekday
4106 given in `org-agenda-start-on-weekday'.
4108 When WITH-HOUR is non-nil, only include scheduled and deadline
4109 items if they have an hour specification like [h]h:mm."
4110 (interactive "P")
4111 (when org-agenda-overriding-arguments
4112 (setq arg (car org-agenda-overriding-arguments)
4113 start-day (nth 1 org-agenda-overriding-arguments)
4114 span (nth 2 org-agenda-overriding-arguments)))
4115 (when (and (integerp arg) (> arg 0))
4116 (setq span arg arg nil))
4117 (catch 'exit
4118 (setq org-agenda-buffer-name
4119 (or org-agenda-buffer-tmp-name
4120 (and org-agenda-doing-sticky-redo org-agenda-buffer-name)
4121 (when org-agenda-sticky
4122 (cond ((and org-keys (stringp org-match))
4123 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4124 (org-keys
4125 (format "*Org Agenda(%s)*" org-keys))
4126 (t "*Org Agenda(a)*")))
4127 "*Org Agenda*"))
4128 (org-agenda-prepare "Day/Week")
4129 (setq start-day (or start-day org-agenda-start-day))
4130 (when (stringp start-day)
4131 ;; Convert to an absolute day number
4132 (setq start-day (time-to-days (org-read-date nil t start-day))))
4133 (org-compile-prefix-format 'agenda)
4134 (org-set-sorting-strategy 'agenda)
4135 (let* ((span (org-agenda-ndays-to-span (or span org-agenda-span)))
4136 (today (org-today))
4137 (sd (or start-day today))
4138 (ndays (org-agenda-span-to-ndays span sd))
4139 (org-agenda-start-on-weekday
4140 (and (or (eq ndays 7) (eq ndays 14))
4141 org-agenda-start-on-weekday))
4142 (thefiles (org-agenda-files nil 'ifmode))
4143 (files thefiles)
4144 (start (if (or (null org-agenda-start-on-weekday)
4145 (< ndays 7))
4147 (let* ((nt (calendar-day-of-week
4148 (calendar-gregorian-from-absolute sd)))
4149 (n1 org-agenda-start-on-weekday)
4150 (d (- nt n1)))
4151 (- sd (+ (if (< d 0) 7 0) d)))))
4152 (day-numbers (list start))
4153 (day-cnt 0)
4154 (inhibit-redisplay (not debug-on-error))
4155 (org-agenda-show-log-scoped org-agenda-show-log)
4156 s e rtn rtnall file date d start-pos end-pos todayp
4157 clocktable-start clocktable-end filter)
4158 (setq org-agenda-redo-command
4159 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4160 (dotimes (n (1- ndays))
4161 (push (1+ (car day-numbers)) day-numbers))
4162 (setq day-numbers (nreverse day-numbers))
4163 (setq clocktable-start (car day-numbers)
4164 clocktable-end (1+ (or (org-last day-numbers) 0)))
4165 (setq-local org-starting-day (car day-numbers))
4166 (setq-local org-arg-loc arg)
4167 (setq-local org-agenda-current-span (org-agenda-ndays-to-span span))
4168 (unless org-agenda-compact-blocks
4169 (let* ((d1 (car day-numbers))
4170 (d2 (org-last day-numbers))
4171 (w1 (org-days-to-iso-week d1))
4172 (w2 (org-days-to-iso-week d2)))
4173 (setq s (point))
4174 (org-agenda--insert-overriding-header
4175 (concat (org-agenda-span-name span)
4176 "-agenda"
4177 (cond ((<= 350 (- d2 d1)) "")
4178 ((= w1 w2) (format " (W%02d)" w1))
4179 (t (format " (W%02d-W%02d)" w1 w2)))
4180 ":\n")))
4181 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4182 'org-date-line t))
4183 (org-agenda-mark-header-line s))
4184 (while (setq d (pop day-numbers))
4185 (setq date (calendar-gregorian-from-absolute d)
4186 s (point))
4187 (if (or (setq todayp (= d today))
4188 (and (not start-pos) (= d sd)))
4189 (setq start-pos (point))
4190 (when (and start-pos (not end-pos))
4191 (setq end-pos (point))))
4192 (setq files thefiles
4193 rtnall nil)
4194 (while (setq file (pop files))
4195 (catch 'nextfile
4196 (org-check-agenda-file file)
4197 (let ((org-agenda-entry-types org-agenda-entry-types))
4198 ;; Starred types override non-starred equivalents
4199 (when (member :deadline* org-agenda-entry-types)
4200 (setq org-agenda-entry-types
4201 (delq :deadline org-agenda-entry-types)))
4202 (when (member :scheduled* org-agenda-entry-types)
4203 (setq org-agenda-entry-types
4204 (delq :scheduled org-agenda-entry-types)))
4205 ;; Honor with-hour
4206 (when with-hour
4207 (when (member :deadline org-agenda-entry-types)
4208 (setq org-agenda-entry-types
4209 (delq :deadline org-agenda-entry-types))
4210 (push :deadline* org-agenda-entry-types))
4211 (when (member :scheduled org-agenda-entry-types)
4212 (setq org-agenda-entry-types
4213 (delq :scheduled org-agenda-entry-types))
4214 (push :scheduled* org-agenda-entry-types)))
4215 (unless org-agenda-include-deadlines
4216 (setq org-agenda-entry-types
4217 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4218 (cond
4219 ((memq org-agenda-show-log-scoped '(only clockcheck))
4220 (setq rtn (org-agenda-get-day-entries
4221 file date :closed)))
4222 (org-agenda-show-log-scoped
4223 (setq rtn (apply 'org-agenda-get-day-entries
4224 file date
4225 (append '(:closed) org-agenda-entry-types))))
4227 (setq rtn (apply 'org-agenda-get-day-entries
4228 file date
4229 org-agenda-entry-types)))))
4230 (setq rtnall (append rtnall rtn)))) ;; all entries
4231 (when org-agenda-include-diary
4232 (let ((org-agenda-search-headline-for-time t))
4233 (require 'diary-lib)
4234 (setq rtn (org-get-entries-from-diary date))
4235 (setq rtnall (append rtnall rtn))))
4236 (when (or rtnall org-agenda-show-all-dates)
4237 (setq day-cnt (1+ day-cnt))
4238 (insert
4239 (if (stringp org-agenda-format-date)
4240 (format-time-string org-agenda-format-date
4241 (org-time-from-absolute date))
4242 (funcall org-agenda-format-date date))
4243 "\n")
4244 (put-text-property s (1- (point)) 'face
4245 (org-agenda-get-day-face date))
4246 (put-text-property s (1- (point)) 'org-date-line t)
4247 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4248 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4249 (when todayp
4250 (put-text-property s (1- (point)) 'org-today t))
4251 (setq rtnall
4252 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4253 (when rtnall (insert ;; all entries
4254 (org-agenda-finalize-entries rtnall 'agenda)
4255 "\n"))
4256 (put-text-property s (1- (point)) 'day d)
4257 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)))
4258 (when (and org-agenda-clockreport-mode clocktable-start)
4259 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4260 ;; the above line is to ensure the restricted range!
4261 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4262 tbl)
4263 (setq p (org-plist-delete p :block))
4264 (setq p (plist-put p :tstart clocktable-start))
4265 (setq p (plist-put p :tend clocktable-end))
4266 (setq p (plist-put p :scope 'agenda))
4267 (setq tbl (apply 'org-clock-get-clocktable p))
4268 (insert tbl)))
4269 (goto-char (point-min))
4270 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4271 (unless (or (not (get-buffer-window))
4272 (and (pos-visible-in-window-p (point-min))
4273 (pos-visible-in-window-p (point-max))))
4274 (goto-char (1- (point-max)))
4275 (recenter -1)
4276 (when (not (pos-visible-in-window-p (or start-pos 1)))
4277 (goto-char (or start-pos 1))
4278 (recenter 1)))
4279 (goto-char (or start-pos 1))
4280 (add-text-properties (point-min) (point-max)
4281 `(org-agenda-type agenda
4282 org-last-args (,arg ,start-day ,span)
4283 org-redo-cmd ,org-agenda-redo-command
4284 org-series-cmd ,org-cmd))
4285 (when (eq org-agenda-show-log-scoped 'clockcheck)
4286 (org-agenda-show-clocking-issues))
4287 (org-agenda-finalize)
4288 (setq buffer-read-only t)
4289 (message ""))))
4291 (defun org-agenda-ndays-to-span (n)
4292 "Return a span symbol for a span of N days, or N if none matches."
4293 (cond ((symbolp n) n)
4294 ((= n 1) 'day)
4295 ((= n 7) 'week)
4296 ((= n 14) 'fortnight)
4297 (t n)))
4299 (defun org-agenda-span-to-ndays (span &optional start-day)
4300 "Return ndays from SPAN, possibly starting at START-DAY.
4301 START-DAY is an absolute time value."
4302 (cond ((numberp span) span)
4303 ((eq span 'day) 1)
4304 ((eq span 'week) 7)
4305 ((eq span 'fortnight) 14)
4306 ((eq span 'month)
4307 (let ((date (calendar-gregorian-from-absolute start-day)))
4308 (calendar-last-day-of-month (car date) (cl-caddr date))))
4309 ((eq span 'year)
4310 (let ((date (calendar-gregorian-from-absolute start-day)))
4311 (if (calendar-leap-year-p (cl-caddr date)) 366 365)))))
4313 (defun org-agenda-span-name (span)
4314 "Return a SPAN name."
4315 (if (null span)
4317 (if (symbolp span)
4318 (capitalize (symbol-name span))
4319 (format "%d days" span))))
4321 ;;; Agenda word search
4323 (defvar org-agenda-search-history nil)
4325 (defvar org-search-syntax-table nil
4326 "Special syntax table for Org search.
4327 In this table, we have single quotes not as word constituents, to
4328 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4330 (defvar org-mode-syntax-table) ; From org.el
4331 (defun org-search-syntax-table ()
4332 (unless org-search-syntax-table
4333 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4334 (modify-syntax-entry ?' "." org-search-syntax-table)
4335 (modify-syntax-entry ?` "." org-search-syntax-table))
4336 org-search-syntax-table)
4338 (defvar org-agenda-last-search-view-search-was-boolean nil)
4340 ;;;###autoload
4341 (defun org-search-view (&optional todo-only string edit-at)
4342 "Show all entries that contain a phrase or words or regular expressions.
4344 With optional prefix argument TODO-ONLY, only consider entries that are
4345 TODO entries. The argument STRING can be used to pass a default search
4346 string into this function. If EDIT-AT is non-nil, it means that the
4347 user should get a chance to edit this string, with cursor at position
4348 EDIT-AT.
4350 The search string can be viewed either as a phrase that should be found as
4351 is, or it can be broken into a number of snippets, each of which must match
4352 in a Boolean way to select an entry. The default depends on the variable
4353 `org-agenda-search-view-always-boolean'.
4354 Even if this is turned off (the default) you can always switch to
4355 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4357 The default is a direct search of the whole phrase, where each space in
4358 the search string can expand to an arbitrary amount of whitespace,
4359 including newlines.
4361 If using a Boolean search, the search string is split on whitespace and
4362 each snippet is searched separately, with logical AND to select an entry.
4363 Words prefixed with a minus must *not* occur in the entry. Words without
4364 a prefix or prefixed with a plus must occur in the entry. Matching is
4365 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4366 match whole words, not parts of a word) if
4367 `org-agenda-search-view-force-full-words' is set (default is nil).
4369 Boolean search snippets enclosed by curly braces are interpreted as
4370 regular expressions that must or (when preceded with \"-\") must not
4371 match in the entry. Snippets enclosed into double quotes will be taken
4372 as a whole, to include whitespace.
4374 - If the search string starts with an asterisk, search only in headlines.
4375 - If (possibly after the leading star) the search string starts with an
4376 exclamation mark, this also means to look at TODO entries only, an effect
4377 that can also be achieved with a prefix argument.
4378 - If (possibly after star and exclamation mark) the search string starts
4379 with a colon, this will mean that the (non-regexp) snippets of the
4380 Boolean search must match as full words.
4382 This command searches the agenda files, and in addition the files
4383 listed in `org-agenda-text-search-extra-files' unless a restriction lock
4384 is active."
4385 (interactive "P")
4386 (when org-agenda-overriding-arguments
4387 (setq todo-only (car org-agenda-overriding-arguments)
4388 string (nth 1 org-agenda-overriding-arguments)
4389 edit-at (nth 2 org-agenda-overriding-arguments)))
4390 (let* ((props (list 'face nil
4391 'done-face 'org-agenda-done
4392 'org-not-done-regexp org-not-done-regexp
4393 'org-todo-regexp org-todo-regexp
4394 'org-complex-heading-regexp org-complex-heading-regexp
4395 'mouse-face 'highlight
4396 'help-echo (format "mouse-2 or RET jump to location")))
4397 (full-words org-agenda-search-view-force-full-words)
4398 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4399 regexp rtn rtnall files file pos inherited-tags
4400 marker category level tags c neg re boolean
4401 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4402 (unless (and (not edit-at)
4403 (stringp string)
4404 (string-match "\\S-" string))
4405 (setq string (read-string
4406 (if org-agenda-search-view-always-boolean
4407 "[+-]Word/{Regexp} ...: "
4408 "Phrase or [+-]Word/{Regexp} ...: ")
4409 (cond
4410 ((integerp edit-at) (cons string edit-at))
4411 (edit-at string))
4412 'org-agenda-search-history)))
4413 (catch 'exit
4414 (when org-agenda-sticky
4415 (setq org-agenda-buffer-name
4416 (if (stringp string)
4417 (format "*Org Agenda(%s:%s)*"
4418 (or org-keys (or (and todo-only "S") "s")) string)
4419 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4420 (org-agenda-prepare "SEARCH")
4421 (org-compile-prefix-format 'search)
4422 (org-set-sorting-strategy 'search)
4423 (setq org-agenda-redo-command
4424 (list 'org-search-view (if todo-only t nil)
4425 (list 'if 'current-prefix-arg nil string)))
4426 (setq org-agenda-query-string string)
4427 (if (equal (string-to-char string) ?*)
4428 (setq hdl-only t
4429 words (substring string 1))
4430 (setq words string))
4431 (when (equal (string-to-char words) ?!)
4432 (setq todo-only t
4433 words (substring words 1)))
4434 (when (equal (string-to-char words) ?:)
4435 (setq full-words t
4436 words (substring words 1)))
4437 (when (or org-agenda-search-view-always-boolean
4438 (member (string-to-char words) '(?- ?+ ?\{)))
4439 (setq boolean t))
4440 (setq words (split-string words))
4441 (let (www w)
4442 (while (setq w (pop words))
4443 (while (and (string-match "\\\\\\'" w) words)
4444 (setq w (concat (substring w 0 -1) " " (pop words))))
4445 (push w www))
4446 (setq words (nreverse www) www nil)
4447 (while (setq w (pop words))
4448 (when (and (string-match "\\`[-+]?{" w)
4449 (not (string-match "}\\'" w)))
4450 (while (and words (not (string-match "}\\'" (car words))))
4451 (setq w (concat w " " (pop words))))
4452 (setq w (concat w " " (pop words))))
4453 (push w www))
4454 (setq words (nreverse www)))
4455 (setq org-agenda-last-search-view-search-was-boolean boolean)
4456 (when boolean
4457 (let (wds w)
4458 (while (setq w (pop words))
4459 (when (or (equal (substring w 0 1) "\"")
4460 (and (> (length w) 1)
4461 (member (substring w 0 1) '("+" "-"))
4462 (equal (substring w 1 2) "\"")))
4463 (while (and words (not (equal (substring w -1) "\"")))
4464 (setq w (concat w " " (pop words)))))
4465 (and (string-match "\\`\\([-+]?\\)\"" w)
4466 (setq w (replace-match "\\1" nil nil w)))
4467 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4468 (push w wds))
4469 (setq words (nreverse wds))))
4470 (if boolean
4471 (mapc (lambda (w)
4472 (setq c (string-to-char w))
4473 (if (equal c ?-)
4474 (setq neg t w (substring w 1))
4475 (if (equal c ?+)
4476 (setq neg nil w (substring w 1))
4477 (setq neg nil)))
4478 (if (string-match "\\`{.*}\\'" w)
4479 (setq re (substring w 1 -1))
4480 (if full-words
4481 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4482 (setq re (regexp-quote (downcase w)))))
4483 (if neg (push re regexps-) (push re regexps+)))
4484 words)
4485 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4486 regexps+))
4487 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4488 (if (not regexps+)
4489 (setq regexp org-outline-regexp-bol)
4490 (setq regexp (pop regexps+))
4491 (when hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4492 regexp))))
4493 (setq files (org-agenda-files nil 'ifmode))
4494 ;; Add `org-agenda-text-search-extra-files' unless there is some
4495 ;; restriction.
4496 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4497 (pop org-agenda-text-search-extra-files)
4498 (unless (get 'org-agenda-files 'org-restrict)
4499 (setq files (org-add-archive-files files))))
4500 ;; Uniquify files. However, let `org-check-agenda-file' handle
4501 ;; non-existent ones.
4502 (setq files (cl-remove-duplicates
4503 (append files org-agenda-text-search-extra-files)
4504 :test (lambda (a b)
4505 (and (file-exists-p a)
4506 (file-exists-p b)
4507 (file-equal-p a b))))
4508 rtnall nil)
4509 (while (setq file (pop files))
4510 (setq ee nil)
4511 (catch 'nextfile
4512 (org-check-agenda-file file)
4513 (setq buffer (if (file-exists-p file)
4514 (org-get-agenda-file-buffer file)
4515 (error "No such file %s" file)))
4516 (unless buffer
4517 ;; If file does not exist, make sure an error message is sent
4518 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4519 file))))
4520 (with-current-buffer buffer
4521 (with-syntax-table (org-search-syntax-table)
4522 (unless (derived-mode-p 'org-mode)
4523 (error "Agenda file %s is not in Org mode" file))
4524 (let ((case-fold-search t))
4525 (save-excursion
4526 (save-restriction
4527 (if (eq buffer org-agenda-restrict)
4528 (narrow-to-region org-agenda-restrict-begin
4529 org-agenda-restrict-end)
4530 (widen))
4531 (goto-char (point-min))
4532 (unless (or (org-at-heading-p)
4533 (outline-next-heading))
4534 (throw 'nextfile t))
4535 (goto-char (max (point-min) (1- (point))))
4536 (while (re-search-forward regexp nil t)
4537 (org-back-to-heading t)
4538 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4539 (> (org-reduced-level (org-outline-level))
4540 org-agenda-search-view-max-outline-level)
4541 (forward-line -1)
4542 (org-back-to-heading t)))
4543 (skip-chars-forward "* ")
4544 (setq beg (point-at-bol)
4545 beg1 (point)
4546 end (progn
4547 (outline-next-heading)
4548 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4549 (> (org-reduced-level (org-outline-level))
4550 org-agenda-search-view-max-outline-level)
4551 (forward-line 1)
4552 (outline-next-heading)))
4553 (point)))
4555 (catch :skip
4556 (goto-char beg)
4557 (org-agenda-skip)
4558 (setq str (buffer-substring-no-properties
4559 (point-at-bol)
4560 (if hdl-only (point-at-eol) end)))
4561 (mapc (lambda (wr) (when (string-match wr str)
4562 (goto-char (1- end))
4563 (throw :skip t)))
4564 regexps-)
4565 (mapc (lambda (wr) (unless (string-match wr str)
4566 (goto-char (1- end))
4567 (throw :skip t)))
4568 (if todo-only
4569 (cons (concat "^\\*+[ \t]+"
4570 org-not-done-regexp)
4571 regexps+)
4572 regexps+))
4573 (goto-char beg)
4574 (setq marker (org-agenda-new-marker (point))
4575 category (org-get-category)
4576 level (make-string (org-reduced-level (org-outline-level)) ? )
4577 inherited-tags
4578 (or (eq org-agenda-show-inherited-tags 'always)
4579 (and (listp org-agenda-show-inherited-tags)
4580 (memq 'todo org-agenda-show-inherited-tags))
4581 (and (eq org-agenda-show-inherited-tags t)
4582 (or (eq org-agenda-use-tag-inheritance t)
4583 (memq 'todo org-agenda-use-tag-inheritance))))
4584 tags (org-get-tags nil (not inherited-tags))
4585 txt (org-agenda-format-item
4587 (buffer-substring-no-properties
4588 beg1 (point-at-eol))
4589 level category tags t))
4590 (org-add-props txt props
4591 'org-marker marker 'org-hd-marker marker
4592 'org-todo-regexp org-todo-regexp
4593 'level level
4594 'org-complex-heading-regexp org-complex-heading-regexp
4595 'priority 1000
4596 'type "search")
4597 (push txt ee)
4598 (goto-char (1- end))))))))))
4599 (setq rtn (nreverse ee))
4600 (setq rtnall (append rtnall rtn)))
4601 (org-agenda--insert-overriding-header
4602 (with-temp-buffer
4603 (insert "Search words: ")
4604 (add-text-properties (point-min) (1- (point))
4605 (list 'face 'org-agenda-structure))
4606 (setq pos (point))
4607 (insert string "\n")
4608 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4609 (setq pos (point))
4610 (unless org-agenda-multi
4611 (insert (substitute-command-keys "\\<org-agenda-mode-map>\
4612 Press `\\[org-agenda-manipulate-query-add]', \
4613 `\\[org-agenda-manipulate-query-subtract]' to add/sub word, \
4614 `\\[org-agenda-manipulate-query-add-re]', \
4615 `\\[org-agenda-manipulate-query-subtract-re]' to add/sub regexp, \
4616 `\\[universal-argument] \\[org-agenda-redo]' to edit\n"))
4617 (add-text-properties pos (1- (point))
4618 (list 'face 'org-agenda-structure)))
4619 (buffer-string)))
4620 (org-agenda-mark-header-line (point-min))
4621 (when rtnall
4622 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4623 (goto-char (point-min))
4624 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4625 (add-text-properties (point-min) (point-max)
4626 `(org-agenda-type search
4627 org-last-args (,todo-only ,string ,edit-at)
4628 org-redo-cmd ,org-agenda-redo-command
4629 org-series-cmd ,org-cmd))
4630 (org-agenda-finalize)
4631 (setq buffer-read-only t))))
4633 ;;; Agenda TODO list
4635 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4636 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4637 (concat
4638 (if (or (equal keywords "ALL") (not keywords))
4639 (propertize "ALL" 'face 'warning)
4640 (mapconcat
4641 (lambda (kw)
4642 (propertize kw 'face (org-get-todo-face kw)))
4643 (org-split-string keywords "|")
4644 "|"))
4645 "\n"))
4647 (defvar org-select-this-todo-keyword nil)
4648 (defvar org-last-arg nil)
4650 ;;;###autoload
4651 (defun org-todo-list (&optional arg)
4652 "Show all (not done) TODO entries from all agenda file in a single list.
4653 The prefix arg can be used to select a specific TODO keyword and limit
4654 the list to these. When using `\\[universal-argument]', you will be prompted
4655 for a keyword. A numeric prefix directly selects the Nth keyword in
4656 `org-todo-keywords-1'."
4657 (interactive "P")
4658 (when org-agenda-overriding-arguments
4659 (setq arg org-agenda-overriding-arguments))
4660 (when (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4661 (let* ((today (org-today))
4662 (date (calendar-gregorian-from-absolute today))
4663 (kwds org-todo-keywords-for-agenda)
4664 (completion-ignore-case t)
4665 (org-select-this-todo-keyword
4666 (if (stringp arg) arg
4667 (and arg (integerp arg) (> arg 0)
4668 (nth (1- arg) kwds))))
4669 rtn rtnall files file pos)
4670 (when (equal arg '(4))
4671 (setq org-select-this-todo-keyword
4672 (completing-read "Keyword (or KWD1|K2D2|...): "
4673 (mapcar #'list kwds) nil nil)))
4674 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4675 (catch 'exit
4676 (when org-agenda-sticky
4677 (setq org-agenda-buffer-name
4678 (if (stringp org-select-this-todo-keyword)
4679 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4680 org-select-this-todo-keyword)
4681 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4682 (org-agenda-prepare "TODO")
4683 (org-compile-prefix-format 'todo)
4684 (org-set-sorting-strategy 'todo)
4685 (setq org-agenda-redo-command
4686 `(org-todo-list (or (and (numberp current-prefix-arg)
4687 current-prefix-arg)
4688 ,org-select-this-todo-keyword
4689 current-prefix-arg ,arg)))
4690 (setq files (org-agenda-files nil 'ifmode)
4691 rtnall nil)
4692 (while (setq file (pop files))
4693 (catch 'nextfile
4694 (org-check-agenda-file file)
4695 (setq rtn (org-agenda-get-day-entries file date :todo))
4696 (setq rtnall (append rtnall rtn))))
4697 (org-agenda--insert-overriding-header
4698 (with-temp-buffer
4699 (insert "Global list of TODO items of type: ")
4700 (add-text-properties (point-min) (1- (point))
4701 (list 'face 'org-agenda-structure
4702 'short-heading
4703 (concat "ToDo: "
4704 (or org-select-this-todo-keyword "ALL"))))
4705 (org-agenda-mark-header-line (point-min))
4706 (insert (org-agenda-propertize-selected-todo-keywords
4707 org-select-this-todo-keyword))
4708 (setq pos (point))
4709 (unless org-agenda-multi
4710 (insert (substitute-command-keys "Available with \
4711 \\<org-agenda-mode-map>`N \\[org-agenda-redo]': (0)[ALL]"))
4712 (let ((n 0))
4713 (dolist (k kwds)
4714 (let ((s (format "(%d)%s" (cl-incf n) k)))
4715 (when (> (+ (current-column) (string-width s) 1) (window-width))
4716 (insert "\n "))
4717 (insert " " s))))
4718 (insert "\n"))
4719 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure))
4720 (buffer-string)))
4721 (org-agenda-mark-header-line (point-min))
4722 (when rtnall
4723 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4724 (goto-char (point-min))
4725 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4726 (add-text-properties (point-min) (point-max)
4727 `(org-agenda-type todo
4728 org-last-args ,arg
4729 org-redo-cmd ,org-agenda-redo-command
4730 org-series-cmd ,org-cmd))
4731 (org-agenda-finalize)
4732 (setq buffer-read-only t))))
4734 ;;; Agenda tags match
4736 ;;;###autoload
4737 (defun org-tags-view (&optional todo-only match)
4738 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4739 The prefix arg TODO-ONLY limits the search to TODO entries."
4740 (interactive "P")
4741 (when org-agenda-overriding-arguments
4742 (setq todo-only (car org-agenda-overriding-arguments)
4743 match (nth 1 org-agenda-overriding-arguments)))
4744 (let* ((org-tags-match-list-sublevels
4745 org-tags-match-list-sublevels)
4746 (completion-ignore-case t)
4747 (org--matcher-tags-todo-only todo-only)
4748 rtn rtnall files file pos matcher
4749 buffer)
4750 (when (and (stringp match) (not (string-match "\\S-" match)))
4751 (setq match nil))
4752 (catch 'exit
4753 ;; TODO: this code is repeated a lot...
4754 (when org-agenda-sticky
4755 (setq org-agenda-buffer-name
4756 (if (stringp match)
4757 (format "*Org Agenda(%s:%s)*"
4758 (or org-keys (or (and todo-only "M") "m")) match)
4759 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4760 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4761 ;; expanding tags within `org-make-tags-matcher'
4762 (org-agenda-prepare (concat "TAGS " match))
4763 (setq match (car matcher)
4764 matcher (cdr matcher))
4765 (org-compile-prefix-format 'tags)
4766 (org-set-sorting-strategy 'tags)
4767 (setq org-agenda-query-string match)
4768 (setq org-agenda-redo-command
4769 (list 'org-tags-view
4770 `(quote ,org--matcher-tags-todo-only)
4771 `(if current-prefix-arg nil ,org-agenda-query-string)))
4772 (setq files (org-agenda-files nil 'ifmode)
4773 rtnall nil)
4774 (while (setq file (pop files))
4775 (catch 'nextfile
4776 (org-check-agenda-file file)
4777 (setq buffer (if (file-exists-p file)
4778 (org-get-agenda-file-buffer file)
4779 (error "No such file %s" file)))
4780 (if (not buffer)
4781 ;; If file does not exist, error message to agenda
4782 (setq rtn (list
4783 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4784 rtnall (append rtnall rtn))
4785 (with-current-buffer buffer
4786 (unless (derived-mode-p 'org-mode)
4787 (error "Agenda file %s is not in Org mode" file))
4788 (save-excursion
4789 (save-restriction
4790 (if (eq buffer org-agenda-restrict)
4791 (narrow-to-region org-agenda-restrict-begin
4792 org-agenda-restrict-end)
4793 (widen))
4794 (setq rtn (org-scan-tags 'agenda
4795 matcher
4796 org--matcher-tags-todo-only))
4797 (setq rtnall (append rtnall rtn))))))))
4798 (org-agenda--insert-overriding-header
4799 (with-temp-buffer
4800 (insert "Headlines with TAGS match: ")
4801 (add-text-properties (point-min) (1- (point))
4802 (list 'face 'org-agenda-structure
4803 'short-heading
4804 (concat "Match: " match)))
4805 (setq pos (point))
4806 (insert match "\n")
4807 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4808 (setq pos (point))
4809 (unless org-agenda-multi
4810 (insert (substitute-command-keys
4811 "Press \
4812 \\<org-agenda-mode-map>`\\[universal-argument] \\[org-agenda-redo]' \
4813 to search again with new search string\n")))
4814 (add-text-properties pos (1- (point))
4815 (list 'face 'org-agenda-structure))
4816 (buffer-string)))
4817 (org-agenda-mark-header-line (point-min))
4818 (when rtnall
4819 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4820 (goto-char (point-min))
4821 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4822 (add-text-properties
4823 (point-min) (point-max)
4824 `(org-agenda-type tags
4825 org-last-args (,org--matcher-tags-todo-only ,match)
4826 org-redo-cmd ,org-agenda-redo-command
4827 org-series-cmd ,org-cmd))
4828 (org-agenda-finalize)
4829 (setq buffer-read-only t))))
4831 ;;; Agenda Finding stuck projects
4833 (defvar org-agenda-skip-regexp nil
4834 "Regular expression used in skipping subtrees for the agenda.
4835 This is basically a temporary global variable that can be set and then
4836 used by user-defined selections using `org-agenda-skip-function'.")
4838 (defvar org-agenda-overriding-header nil
4839 "When set during agenda, todo and tags searches it replaces the header.
4840 If an empty string, no header will be inserted. If any other
4841 string, it will be inserted as a header. If nil, a header will
4842 be generated automatically according to the command. This
4843 variable should not be set directly, but custom commands can bind
4844 it in the options section.")
4846 (defun org-agenda-skip-entry-if (&rest conditions)
4847 "Skip entry if any of CONDITIONS is true.
4848 See `org-agenda-skip-if' for details."
4849 (org-agenda-skip-if nil conditions))
4851 (defun org-agenda-skip-subtree-if (&rest conditions)
4852 "Skip subtree if any of CONDITIONS is true.
4853 See `org-agenda-skip-if' for details."
4854 (org-agenda-skip-if t conditions))
4856 (defun org-agenda-skip-if (subtree conditions)
4857 "Checks current entity for CONDITIONS.
4858 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4859 the entry (i.e. the text before the next heading) is checked.
4861 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4862 from different tests. Valid conditions are:
4864 scheduled Check if there is a scheduled cookie
4865 notscheduled Check if there is no scheduled cookie
4866 deadline Check if there is a deadline
4867 notdeadline Check if there is no deadline
4868 timestamp Check if there is a timestamp (also deadline or scheduled)
4869 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4870 regexp Check if regexp matches
4871 notregexp Check if regexp does not match.
4872 todo Check if TODO keyword matches
4873 nottodo Check if TODO keyword does not match
4875 The regexp is taken from the conditions list, it must come right after
4876 the `regexp' or `notregexp' element.
4878 `todo' and `nottodo' accept as an argument a list of todo
4879 keywords, which may include \"*\" to match any todo keyword.
4881 (org-agenda-skip-entry-if \\='todo \\='(\"TODO\" \"WAITING\"))
4883 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4885 Instead of a list, a keyword class may be given. For example:
4887 (org-agenda-skip-entry-if \\='nottodo \\='done)
4889 would skip entries that haven't been marked with any of \"DONE\"
4890 keywords. Possible classes are: `todo', `done', `any'.
4892 If any of these conditions is met, this function returns the end point of
4893 the entity, causing the search to continue from there. This is a function
4894 that can be put into `org-agenda-skip-function' for the duration of a command."
4895 (org-back-to-heading t)
4896 (let* ((beg (point))
4897 (end (if subtree (save-excursion (org-end-of-subtree t) (point))
4898 (org-entry-end-position)))
4899 (planning-end (if subtree end (line-end-position 2)))
4901 (and
4902 (or (and (memq 'scheduled conditions)
4903 (re-search-forward org-scheduled-time-regexp planning-end t))
4904 (and (memq 'notscheduled conditions)
4905 (not
4906 (save-excursion
4907 (re-search-forward org-scheduled-time-regexp planning-end t))))
4908 (and (memq 'deadline conditions)
4909 (re-search-forward org-deadline-time-regexp planning-end t))
4910 (and (memq 'notdeadline conditions)
4911 (not
4912 (save-excursion
4913 (re-search-forward org-deadline-time-regexp planning-end t))))
4914 (and (memq 'timestamp conditions)
4915 (re-search-forward org-ts-regexp end t))
4916 (and (memq 'nottimestamp conditions)
4917 (not (save-excursion (re-search-forward org-ts-regexp end t))))
4918 (and (setq m (memq 'regexp conditions))
4919 (stringp (nth 1 m))
4920 (re-search-forward (nth 1 m) end t))
4921 (and (setq m (memq 'notregexp conditions))
4922 (stringp (nth 1 m))
4923 (not (save-excursion (re-search-forward (nth 1 m) end t))))
4924 (and (or
4925 (setq m (memq 'nottodo conditions))
4926 (setq m (memq 'todo-unblocked conditions))
4927 (setq m (memq 'nottodo-unblocked conditions))
4928 (setq m (memq 'todo conditions)))
4929 (org-agenda-skip-if-todo m end)))
4930 end)))
4932 (defun org-agenda-skip-if-todo (args end)
4933 "Helper function for `org-agenda-skip-if', do not use it directly.
4934 ARGS is a list with first element either `todo', `nottodo',
4935 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
4936 a list of TODO keywords, or a state symbol `todo' or `done' or
4937 `any'."
4938 (let ((todo-re
4939 (concat "^\\*+[ \t]+"
4940 (regexp-opt
4941 (pcase args
4942 (`(,_ todo)
4943 (org-delete-all org-done-keywords
4944 (copy-sequence org-todo-keywords-1)))
4945 (`(,_ done) org-done-keywords)
4946 (`(,_ any) org-todo-keywords-1)
4947 (`(,_ ,(pred atom))
4948 (error "Invalid TODO class or type: %S" args))
4949 (`(,_ ,(pred (member "*"))) org-todo-keywords-1)
4950 (`(,_ ,todo-list) todo-list))
4951 'words))))
4952 (pcase args
4953 (`(todo . ,_)
4954 (let (case-fold-search) (re-search-forward todo-re end t)))
4955 (`(nottodo . ,_)
4956 (not (let (case-fold-search) (re-search-forward todo-re end t))))
4957 (`(todo-unblocked . ,_)
4958 (catch :unblocked
4959 (while (let (case-fold-search) (re-search-forward todo-re end t))
4960 (when (org-entry-blocked-p) (throw :unblocked t)))
4961 nil))
4962 (`(nottodo-unblocked . ,_)
4963 (catch :unblocked
4964 (while (let (case-fold-search) (re-search-forward todo-re end t))
4965 (when (org-entry-blocked-p) (throw :unblocked nil)))
4967 (`(,type . ,_) (error "Unknown TODO skip type: %S" type)))))
4969 ;;;###autoload
4970 (defun org-agenda-list-stuck-projects (&rest ignore)
4971 "Create agenda view for projects that are stuck.
4972 Stuck projects are project that have no next actions. For the definitions
4973 of what a project is and how to check if it stuck, customize the variable
4974 `org-stuck-projects'."
4975 (interactive)
4976 (let* ((org-agenda-overriding-header
4977 (or org-agenda-overriding-header "List of stuck projects: "))
4978 (matcher (nth 0 org-stuck-projects))
4979 (todo (nth 1 org-stuck-projects))
4980 (tags (nth 2 org-stuck-projects))
4981 (gen-re (org-string-nw-p (nth 3 org-stuck-projects)))
4982 (todo-wds
4983 (if (not (member "*" todo)) todo
4984 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
4985 (org-delete-all org-done-keywords-for-agenda
4986 (copy-sequence org-todo-keywords-for-agenda))))
4987 (todo-re (and todo
4988 (format "^\\*+[ \t]+\\(%s\\)\\>"
4989 (mapconcat #'identity todo-wds "\\|"))))
4990 (tags-re (cond ((null tags) nil)
4991 ((member "*" tags) org-tag-line-re)
4992 (tags
4993 (let ((other-tags (format "\\(?:%s:\\)*" org-tag-re)))
4994 (concat org-outline-regexp-bol
4995 ".*?[ \t]:"
4996 other-tags
4997 (regexp-opt tags t)
4998 ":" other-tags "[ \t]*$")))
4999 (t nil)))
5000 (re-list (delq nil (list todo-re tags-re gen-re)))
5001 (skip-re
5002 (if (null re-list)
5003 (error "Missing information to identify unstuck projects")
5004 (mapconcat #'identity re-list "\\|")))
5005 (org-agenda-skip-function
5006 ;; Skip entry if `org-agenda-skip-regexp' matches anywhere
5007 ;; in the subtree.
5008 `(lambda ()
5009 (and (save-excursion
5010 (let ((case-fold-search nil))
5011 (re-search-forward
5012 ,skip-re (save-excursion (org-end-of-subtree t)) t)))
5013 (progn (outline-next-heading) (point))))))
5014 (org-tags-view nil matcher)
5015 (setq org-agenda-buffer-name (buffer-name))
5016 (with-current-buffer org-agenda-buffer-name
5017 (setq org-agenda-redo-command
5018 `(org-agenda-list-stuck-projects ,current-prefix-arg))
5019 (let ((inhibit-read-only t))
5020 (add-text-properties
5021 (point-min) (point-max)
5022 `(org-redo-cmd ,org-agenda-redo-command))))))
5024 ;;; Diary integration
5026 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
5027 (defvar diary-list-entries-hook)
5028 (defvar diary-time-regexp)
5029 (defun org-get-entries-from-diary (date)
5030 "Get the (Emacs Calendar) diary entries for DATE."
5031 (require 'diary-lib)
5032 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
5033 (diary-display-function 'diary-fancy-display)
5034 (pop-up-frames nil)
5035 (diary-list-entries-hook
5036 (cons 'org-diary-default-entry diary-list-entries-hook))
5037 (diary-file-name-prefix nil) ; turn this feature off
5038 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
5039 entries
5040 (org-disable-agenda-to-diary t))
5041 (save-excursion
5042 (save-window-excursion
5043 (funcall (if (fboundp 'diary-list-entries)
5044 'diary-list-entries 'list-diary-entries)
5045 date 1)))
5046 (if (not (get-buffer diary-fancy-buffer))
5047 (setq entries nil)
5048 (with-current-buffer diary-fancy-buffer
5049 (setq buffer-read-only nil)
5050 (if (zerop (buffer-size))
5051 ;; No entries
5052 (setq entries nil)
5053 ;; Omit the date and other unnecessary stuff
5054 (org-agenda-cleanup-fancy-diary)
5055 ;; Add prefix to each line and extend the text properties
5056 (if (zerop (buffer-size))
5057 (setq entries nil)
5058 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
5059 (setq entries
5060 (with-temp-buffer
5061 (insert entries) (goto-char (point-min))
5062 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5063 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5064 (replace-match (concat "; " (match-string 1)))))
5065 (buffer-string)))))
5066 (set-buffer-modified-p nil)
5067 (kill-buffer diary-fancy-buffer)))
5068 (when entries
5069 (setq entries (org-split-string entries "\n"))
5070 (setq entries
5071 (mapcar
5072 (lambda (x)
5073 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5074 ;; Extend the text properties to the beginning of the line
5075 (org-add-props x (text-properties-at (1- (length x)) x)
5076 'type "diary" 'date date 'face 'org-agenda-diary))
5077 entries)))))
5079 (defvar org-agenda-cleanup-fancy-diary-hook nil
5080 "Hook run when the fancy diary buffer is cleaned up.")
5082 (defun org-agenda-cleanup-fancy-diary ()
5083 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5084 This gets rid of the date, the underline under the date, and the
5085 dummy entry installed by Org mode to ensure non-empty diary for
5086 each date. It also removes lines that contain only whitespace."
5087 (goto-char (point-min))
5088 (if (looking-at ".*?:[ \t]*")
5089 (progn
5090 (replace-match "")
5091 (re-search-forward "\n=+$" nil t)
5092 (replace-match "")
5093 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5094 (re-search-forward "\n=+$" nil t)
5095 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5096 (goto-char (point-min))
5097 (while (re-search-forward "^ +\n" nil t)
5098 (replace-match ""))
5099 (goto-char (point-min))
5100 (when (re-search-forward "^Org mode dummy\n?" nil t)
5101 (replace-match ""))
5102 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5104 (defun org-modify-diary-entry-string (string)
5105 "Add text properties to string, allowing Org to act on it."
5106 (org-add-props string nil
5107 'mouse-face 'highlight
5108 'help-echo (if buffer-file-name
5109 (format "mouse-2 or RET jump to diary file %s"
5110 (abbreviate-file-name buffer-file-name))
5112 'org-agenda-diary-link t
5113 'org-marker (org-agenda-new-marker (point-at-bol))))
5115 (defun org-diary-default-entry ()
5116 "Add a dummy entry to the diary.
5117 Needed to avoid empty dates which mess up holiday display."
5118 ;; Catch the error if dealing with the new add-to-diary-alist
5119 (when org-disable-agenda-to-diary
5120 (condition-case nil
5121 (org-add-to-diary-list original-date "Org mode dummy" "")
5122 (error
5123 (org-add-to-diary-list original-date "Org mode dummy" "" nil)))))
5125 (defun org-add-to-diary-list (&rest args)
5126 (if (fboundp 'diary-add-to-list)
5127 (apply 'diary-add-to-list args)
5128 (apply 'add-to-diary-list args)))
5130 (defvar org-diary-last-run-time nil)
5132 ;;;###autoload
5133 (defun org-diary (&rest args)
5134 "Return diary information from org files.
5135 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5136 It accesses org files and extracts information from those files to be
5137 listed in the diary. The function accepts arguments specifying what
5138 items should be listed. For a list of arguments allowed here, see the
5139 variable `org-agenda-entry-types'.
5141 The call in the diary file should look like this:
5143 &%%(org-diary) ~/path/to/some/orgfile.org
5145 Use a separate line for each org file to check. Or, if you omit the file name,
5146 all files listed in `org-agenda-files' will be checked automatically:
5148 &%%(org-diary)
5150 If you don't give any arguments (as in the example above), the default value
5151 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5152 So the example above may also be written as
5154 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5156 The function expects the lisp variables `entry' and `date' to be provided
5157 by the caller, because this is how the calendar works. Don't use this
5158 function from a program - use `org-agenda-get-day-entries' instead."
5159 (when (> (- (float-time)
5160 org-agenda-last-marker-time)
5162 ;; I am not sure if this works with sticky agendas, because the marker
5163 ;; list is then no longer a global variable.
5164 (org-agenda-reset-markers))
5165 (org-compile-prefix-format 'agenda)
5166 (org-set-sorting-strategy 'agenda)
5167 (setq args (or args org-agenda-entry-types))
5168 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5169 (list entry)
5170 (org-agenda-files t)))
5171 (time (float-time))
5172 file rtn results)
5173 (when (or (not org-diary-last-run-time)
5174 (> (- time
5175 org-diary-last-run-time)
5177 (org-agenda-prepare-buffers files))
5178 (setq org-diary-last-run-time time)
5179 ;; If this is called during org-agenda, don't return any entries to
5180 ;; the calendar. Org Agenda will list these entries itself.
5181 (when org-disable-agenda-to-diary (setq files nil))
5182 (while (setq file (pop files))
5183 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5184 (setq results (append results rtn)))
5185 (when results
5186 (setq results
5187 (mapcar (lambda (i) (replace-regexp-in-string
5188 org-bracket-link-regexp "\\3" i)) results))
5189 (concat (org-agenda-finalize-entries results) "\n"))))
5191 ;;; Agenda entry finders
5193 (defun org-agenda--timestamp-to-absolute (&rest args)
5194 "Call `org-time-string-to-absolute' with ARGS.
5195 However, throw `:skip' whenever an error is raised."
5196 (condition-case e
5197 (apply #'org-time-string-to-absolute args)
5198 (org-diary-sexp-no-match (throw :skip nil))
5199 (error
5200 (message "%s; Skipping entry" (error-message-string e))
5201 (throw :skip nil))))
5203 (defun org-agenda-get-day-entries (file date &rest args)
5204 "Does the work for `org-diary' and `org-agenda'.
5205 FILE is the path to a file to be checked for entries. DATE is date like
5206 the one returned by `calendar-current-date'. ARGS are symbols indicating
5207 which kind of entries should be extracted. For details about these, see
5208 the documentation of `org-diary'."
5209 (let* ((org-startup-folded nil)
5210 (org-startup-align-all-tables nil)
5211 (buffer (if (file-exists-p file) (org-get-agenda-file-buffer file)
5212 (error "No such file %s" file))))
5213 (if (not buffer)
5214 ;; If file does not exist, signal it in diary nonetheless.
5215 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5216 (with-current-buffer buffer
5217 (unless (derived-mode-p 'org-mode)
5218 (error "Agenda file %s is not in Org mode" file))
5219 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5220 (setf org-agenda-current-date date)
5221 (save-excursion
5222 (save-restriction
5223 (if (eq buffer org-agenda-restrict)
5224 (narrow-to-region org-agenda-restrict-begin
5225 org-agenda-restrict-end)
5226 (widen))
5227 ;; Rationalize ARGS. Also make sure `:deadline' comes
5228 ;; first in order to populate DEADLINES before passing it.
5230 ;; We use `delq' since `org-uniquify' duplicates ARGS,
5231 ;; guarding us from modifying `org-agenda-entry-types'.
5232 (setf args (org-uniquify (or args org-agenda-entry-types)))
5233 (when (and (memq :scheduled args) (memq :scheduled* args))
5234 (setf args (delq :scheduled* args)))
5235 (cond
5236 ((memq :deadline args)
5237 (setf args (cons :deadline
5238 (delq :deadline (delq :deadline* args)))))
5239 ((memq :deadline* args)
5240 (setf args (cons :deadline* (delq :deadline* args)))))
5241 ;; Collect list of headlines. Return them flattened.
5242 (let ((case-fold-search nil) results deadlines)
5243 (dolist (arg args (apply #'nconc (nreverse results)))
5244 (pcase arg
5245 ((and :todo (guard (org-agenda-today-p date)))
5246 (push (org-agenda-get-todos) results))
5247 (:timestamp
5248 (push (org-agenda-get-blocks) results)
5249 (push (org-agenda-get-timestamps deadlines) results))
5250 (:sexp
5251 (push (org-agenda-get-sexps) results))
5252 (:scheduled
5253 (push (org-agenda-get-scheduled deadlines) results))
5254 (:scheduled*
5255 (push (org-agenda-get-scheduled deadlines t) results))
5256 (:closed
5257 (push (org-agenda-get-progress) results))
5258 (:deadline
5259 (setf deadlines (org-agenda-get-deadlines))
5260 (push deadlines results))
5261 (:deadline*
5262 (setf deadlines (org-agenda-get-deadlines t))
5263 (push deadlines results)))))))))))
5265 (defsubst org-em (x y list)
5266 "Is X or Y a member of LIST?"
5267 (or (memq x list) (memq y list)))
5269 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5270 (defvar org-agenda-sorting-strategy-selected nil)
5272 (defun org-agenda-entry-get-agenda-timestamp (pom)
5273 "Retrieve timestamp information for sorting agenda views.
5274 Given a point or marker POM, returns a cons cell of the timestamp
5275 and the timestamp type relevant for the sorting strategy in
5276 `org-agenda-sorting-strategy-selected'."
5277 (let (ts ts-date-type)
5278 (save-match-data
5279 (cond ((org-em 'scheduled-up 'scheduled-down
5280 org-agenda-sorting-strategy-selected)
5281 (setq ts (org-entry-get pom "SCHEDULED")
5282 ts-date-type " scheduled"))
5283 ((org-em 'deadline-up 'deadline-down
5284 org-agenda-sorting-strategy-selected)
5285 (setq ts (org-entry-get pom "DEADLINE")
5286 ts-date-type " deadline"))
5287 ((org-em 'ts-up 'ts-down
5288 org-agenda-sorting-strategy-selected)
5289 (setq ts (org-entry-get pom "TIMESTAMP")
5290 ts-date-type " timestamp"))
5291 ((org-em 'tsia-up 'tsia-down
5292 org-agenda-sorting-strategy-selected)
5293 (setq ts (org-entry-get pom "TIMESTAMP_IA")
5294 ts-date-type " timestamp_ia"))
5295 ((org-em 'timestamp-up 'timestamp-down
5296 org-agenda-sorting-strategy-selected)
5297 (setq ts (or (org-entry-get pom "SCHEDULED")
5298 (org-entry-get pom "DEADLINE")
5299 (org-entry-get pom "TIMESTAMP")
5300 (org-entry-get pom "TIMESTAMP_IA"))
5301 ts-date-type ""))
5302 (t (setq ts-date-type "")))
5303 (cons (when ts (ignore-errors (org-time-string-to-absolute ts)))
5304 ts-date-type))))
5306 (defun org-agenda-get-todos ()
5307 "Return the TODO information for agenda display."
5308 (let* ((props (list 'face nil
5309 'done-face 'org-agenda-done
5310 'org-not-done-regexp org-not-done-regexp
5311 'org-todo-regexp org-todo-regexp
5312 'org-complex-heading-regexp org-complex-heading-regexp
5313 'mouse-face 'highlight
5314 'help-echo
5315 (format "mouse-2 or RET jump to org file %s"
5316 (abbreviate-file-name buffer-file-name))))
5317 (case-fold-search nil)
5318 (regexp (format org-heading-keyword-regexp-format
5319 (cond
5320 ((and org-select-this-todo-keyword
5321 (equal org-select-this-todo-keyword "*"))
5322 org-todo-regexp)
5323 (org-select-this-todo-keyword
5324 (concat "\\("
5325 (mapconcat 'identity
5326 (org-split-string
5327 org-select-this-todo-keyword
5328 "|")
5329 "\\|") "\\)"))
5330 (t org-not-done-regexp))))
5331 marker priority category level tags todo-state
5332 ts-date ts-date-type ts-date-pair
5333 ee txt beg end inherited-tags todo-state-end-pos)
5334 (goto-char (point-min))
5335 (while (re-search-forward regexp nil t)
5336 (catch :skip
5337 (save-match-data
5338 (beginning-of-line)
5339 (org-agenda-skip)
5340 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5341 (unless (and (setq todo-state (org-get-todo-state))
5342 (setq todo-state-end-pos (match-end 2)))
5343 (goto-char end)
5344 (throw :skip nil))
5345 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5346 (goto-char (1+ beg))
5347 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5348 (throw :skip nil)))
5349 (goto-char (match-beginning 2))
5350 (setq marker (org-agenda-new-marker (match-beginning 0))
5351 category (org-get-category)
5352 ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
5353 ts-date (car ts-date-pair)
5354 ts-date-type (cdr ts-date-pair)
5355 txt (org-trim (buffer-substring (match-beginning 2) (match-end 0)))
5356 inherited-tags
5357 (or (eq org-agenda-show-inherited-tags 'always)
5358 (and (listp org-agenda-show-inherited-tags)
5359 (memq 'todo org-agenda-show-inherited-tags))
5360 (and (eq org-agenda-show-inherited-tags t)
5361 (or (eq org-agenda-use-tag-inheritance t)
5362 (memq 'todo org-agenda-use-tag-inheritance))))
5363 tags (org-get-tags nil (not inherited-tags))
5364 level (make-string (org-reduced-level (org-outline-level)) ? )
5365 txt (org-agenda-format-item "" txt level category tags t)
5366 priority (1+ (org-get-priority txt)))
5367 (org-add-props txt props
5368 'org-marker marker 'org-hd-marker marker
5369 'priority priority
5370 'level level
5371 'ts-date ts-date
5372 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5373 (push txt ee)
5374 (if org-agenda-todo-list-sublevels
5375 (goto-char todo-state-end-pos)
5376 (org-end-of-subtree 'invisible))))
5377 (nreverse ee)))
5379 (defun org-agenda-todo-custom-ignore-p (time n)
5380 "Check whether timestamp is farther away than n number of days.
5381 This function is invoked if `org-agenda-todo-ignore-deadlines',
5382 `org-agenda-todo-ignore-scheduled' or
5383 `org-agenda-todo-ignore-timestamp' is set to an integer."
5384 (let ((days (org-time-stamp-to-now
5385 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5386 (if (>= n 0)
5387 (>= days n)
5388 (<= days n))))
5390 ;;;###autoload
5391 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5392 (&optional end)
5393 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5394 (when (or org-agenda-todo-ignore-with-date
5395 org-agenda-todo-ignore-scheduled
5396 org-agenda-todo-ignore-deadlines
5397 org-agenda-todo-ignore-timestamp)
5398 (setq end (or end (save-excursion (outline-next-heading) (point))))
5399 (save-excursion
5400 (or (and org-agenda-todo-ignore-with-date
5401 (re-search-forward org-ts-regexp end t))
5402 (and org-agenda-todo-ignore-scheduled
5403 (re-search-forward org-scheduled-time-regexp end t)
5404 (cond
5405 ((eq org-agenda-todo-ignore-scheduled 'future)
5406 (> (org-time-stamp-to-now
5407 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5408 ((eq org-agenda-todo-ignore-scheduled 'past)
5409 (<= (org-time-stamp-to-now
5410 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5411 ((numberp org-agenda-todo-ignore-scheduled)
5412 (org-agenda-todo-custom-ignore-p
5413 (match-string 1) org-agenda-todo-ignore-scheduled))
5414 (t)))
5415 (and org-agenda-todo-ignore-deadlines
5416 (re-search-forward org-deadline-time-regexp end t)
5417 (cond
5418 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5419 ((eq org-agenda-todo-ignore-deadlines 'far)
5420 (not (org-deadline-close-p (match-string 1))))
5421 ((eq org-agenda-todo-ignore-deadlines 'future)
5422 (> (org-time-stamp-to-now
5423 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5424 ((eq org-agenda-todo-ignore-deadlines 'past)
5425 (<= (org-time-stamp-to-now
5426 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5427 ((numberp org-agenda-todo-ignore-deadlines)
5428 (org-agenda-todo-custom-ignore-p
5429 (match-string 1) org-agenda-todo-ignore-deadlines))
5430 (t (org-deadline-close-p (match-string 1)))))
5431 (and org-agenda-todo-ignore-timestamp
5432 (let ((buffer (current-buffer))
5433 (regexp
5434 (concat
5435 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5436 (start (point)))
5437 ;; Copy current buffer into a temporary one
5438 (with-temp-buffer
5439 (insert-buffer-substring buffer start end)
5440 (goto-char (point-min))
5441 ;; Delete SCHEDULED and DEADLINE items
5442 (while (re-search-forward regexp end t)
5443 (delete-region (match-beginning 0) (match-end 0)))
5444 (goto-char (point-min))
5445 ;; No search for timestamp left
5446 (when (re-search-forward org-ts-regexp nil t)
5447 (cond
5448 ((eq org-agenda-todo-ignore-timestamp 'future)
5449 (> (org-time-stamp-to-now
5450 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5451 ((eq org-agenda-todo-ignore-timestamp 'past)
5452 (<= (org-time-stamp-to-now
5453 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5454 ((numberp org-agenda-todo-ignore-timestamp)
5455 (org-agenda-todo-custom-ignore-p
5456 (match-string 1) org-agenda-todo-ignore-timestamp))
5457 (t))))))))))
5459 (defun org-agenda-get-timestamps (&optional deadlines)
5460 "Return the date stamp information for agenda display.
5461 Optional argument DEADLINES is a list of deadline items to be
5462 displayed in agenda view."
5463 (let* ((props (list 'face 'org-agenda-calendar-event
5464 'org-not-done-regexp org-not-done-regexp
5465 'org-todo-regexp org-todo-regexp
5466 'org-complex-heading-regexp org-complex-heading-regexp
5467 'mouse-face 'highlight
5468 'help-echo
5469 (format "mouse-2 or RET jump to Org file %s"
5470 (abbreviate-file-name buffer-file-name))))
5471 (current (calendar-absolute-from-gregorian date))
5472 (today (org-today))
5473 (deadline-position-alist
5474 (mapcar (lambda (d)
5475 (let ((m (get-text-property 0 'org-hd-marker d)))
5476 (and m (marker-position m))))
5477 deadlines))
5478 ;; Match time-stamps set to current date, time-stamps with
5479 ;; a repeater, and S-exp time-stamps.
5480 (regexp
5481 (concat
5482 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5483 (regexp-quote
5484 (substring
5485 (format-time-string
5486 (car org-time-stamp-formats)
5487 (apply #'encode-time ; DATE bound by calendar
5488 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5489 1 11))
5490 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5491 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5492 timestamp-items)
5493 (goto-char (point-min))
5494 (while (re-search-forward regexp nil t)
5495 ;; Skip date ranges, scheduled and deadlines, which are handled
5496 ;; specially. Also skip time-stamps before first headline as
5497 ;; there would be no entry to add to the agenda. Eventually,
5498 ;; ignore clock entries.
5499 (catch :skip
5500 (save-match-data
5501 (when (or (org-at-date-range-p)
5502 (org-at-planning-p)
5503 (org-before-first-heading-p)
5504 (and org-agenda-include-inactive-timestamps
5505 (org-at-clock-log-p)))
5506 (throw :skip nil))
5507 (org-agenda-skip))
5508 (let* ((pos (match-beginning 0))
5509 (repeat (match-string 1))
5510 (sexp-entry (match-string 3))
5511 (time-stamp (if (or repeat sexp-entry) (match-string 0)
5512 (save-excursion
5513 (goto-char pos)
5514 (looking-at org-ts-regexp-both)
5515 (match-string 0))))
5516 (todo-state (org-get-todo-state))
5517 (warntime (get-text-property (point) 'org-appt-warntime))
5518 (done? (member todo-state org-done-keywords)))
5519 ;; Possibly skip done tasks.
5520 (when (and done? org-agenda-skip-timestamp-if-done)
5521 (throw :skip t))
5522 ;; S-exp entry doesn't match current day: skip it.
5523 (when (and sexp-entry (not (org-diary-sexp-entry sexp-entry "" date)))
5524 (throw :skip nil))
5525 (when repeat
5526 (let* ((past
5527 ;; A repeating time stamp is shown at its base
5528 ;; date and every repeated date up to TODAY. If
5529 ;; `org-agenda-prefer-last-repeat' is non-nil,
5530 ;; however, only the last repeat before today
5531 ;; (inclusive) is shown.
5532 (org-agenda--timestamp-to-absolute
5533 repeat
5534 (if (or (> current today)
5535 (eq org-agenda-prefer-last-repeat t)
5536 (member todo-state org-agenda-prefer-last-repeat))
5537 today
5538 current)
5539 'past (current-buffer) pos))
5540 (future
5541 ;; Display every repeated date past TODAY
5542 ;; (exclusive) unless
5543 ;; `org-agenda-show-future-repeats' is nil. If
5544 ;; this variable is set to `next', only display
5545 ;; the first repeated date after TODAY
5546 ;; (exclusive).
5547 (cond
5548 ((<= current today) past)
5549 ((not org-agenda-show-future-repeats) past)
5551 (let ((base (if (eq org-agenda-show-future-repeats 'next)
5552 (1+ today)
5553 current)))
5554 (org-agenda--timestamp-to-absolute
5555 repeat base 'future (current-buffer) pos))))))
5556 (when (and (/= current past) (/= current future))
5557 (throw :skip nil))))
5558 (save-excursion
5559 (re-search-backward org-outline-regexp-bol nil t)
5560 ;; Possibly skip time-stamp when a deadline is set.
5561 (when (and org-agenda-skip-timestamp-if-deadline-is-shown
5562 (assq (point) deadline-position-alist))
5563 (throw :skip nil))
5564 (let* ((category (org-get-category pos))
5565 (inherited-tags
5566 (or (eq org-agenda-show-inherited-tags 'always)
5567 (and (consp org-agenda-show-inherited-tags)
5568 (memq 'agenda org-agenda-show-inherited-tags))
5569 (and (eq org-agenda-show-inherited-tags t)
5570 (or (eq org-agenda-use-tag-inheritance t)
5571 (memq 'agenda
5572 org-agenda-use-tag-inheritance)))))
5573 (tags (org-get-tags nil (not inherited-tags)))
5574 (level (make-string (org-reduced-level (org-outline-level))
5575 ?\s))
5576 (head (and (looking-at "\\*+[ \t]+\\(.*\\)")
5577 (match-string 1)))
5578 (inactive? (= (char-after pos) ?\[))
5579 (habit? (and (fboundp 'org-is-habit-p) (org-is-habit-p)))
5580 (item
5581 (org-agenda-format-item
5582 (and inactive? org-agenda-inactive-leader)
5583 head level category tags time-stamp org-ts-regexp habit?)))
5584 (org-add-props item props
5585 'priority (if habit?
5586 (org-habit-get-priority (org-habit-parse-todo))
5587 (org-get-priority item))
5588 'org-marker (org-agenda-new-marker pos)
5589 'org-hd-marker (org-agenda-new-marker)
5590 'date date
5591 'level level
5592 'ts-date (if repeat (org-agenda--timestamp-to-absolute repeat)
5593 current)
5594 'todo-state todo-state
5595 'warntime warntime
5596 'type "timestamp")
5597 (push item timestamp-items))))
5598 (when org-agenda-skip-additional-timestamps-same-entry
5599 (outline-next-heading))))
5600 (nreverse timestamp-items)))
5602 (defun org-agenda-get-sexps ()
5603 "Return the sexp information for agenda display."
5604 (require 'diary-lib)
5605 (let* ((props (list 'face 'org-agenda-calendar-sexp
5606 'mouse-face 'highlight
5607 'help-echo
5608 (format "mouse-2 or RET jump to org file %s"
5609 (abbreviate-file-name buffer-file-name))))
5610 (regexp "^&?%%(")
5611 marker category extra level ee txt tags entry
5612 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5613 (goto-char (point-min))
5614 (while (re-search-forward regexp nil t)
5615 (catch :skip
5616 (org-agenda-skip)
5617 (setq beg (match-beginning 0))
5618 (goto-char (1- (match-end 0)))
5619 (setq b (point))
5620 (forward-sexp 1)
5621 (setq sexp (buffer-substring b (point)))
5622 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5623 (org-trim (match-string 1))
5624 ""))
5625 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5626 (when result
5627 (setq marker (org-agenda-new-marker beg)
5628 level (make-string (org-reduced-level (org-outline-level)) ? )
5629 category (org-get-category beg)
5630 inherited-tags
5631 (or (eq org-agenda-show-inherited-tags 'always)
5632 (and (listp org-agenda-show-inherited-tags)
5633 (memq 'agenda org-agenda-show-inherited-tags))
5634 (and (eq org-agenda-show-inherited-tags t)
5635 (or (eq org-agenda-use-tag-inheritance t)
5636 (memq 'agenda org-agenda-use-tag-inheritance))))
5637 tags (org-get-tags nil (not inherited-tags))
5638 todo-state (org-get-todo-state)
5639 warntime (get-text-property (point) 'org-appt-warntime)
5640 extra nil)
5642 (dolist (r (if (stringp result)
5643 (list result)
5644 result)) ;; we expect a list here
5645 (when (and org-agenda-diary-sexp-prefix
5646 (string-match org-agenda-diary-sexp-prefix r))
5647 (setq extra (match-string 0 r)
5648 r (replace-match "" nil nil r)))
5649 (if (string-match "\\S-" r)
5650 (setq txt r)
5651 (setq txt "SEXP entry returned empty string"))
5652 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5653 (org-add-props txt props 'org-marker marker
5654 'date date 'todo-state todo-state
5655 'level level 'type "sexp" 'warntime warntime)
5656 (push txt ee)))))
5657 (nreverse ee)))
5659 ;; Calendar sanity: define some functions that are independent of
5660 ;; `calendar-date-style'.
5661 (defun org-anniversary (year month day &optional mark)
5662 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5663 (with-no-warnings
5664 (let ((calendar-date-style 'iso))
5665 (diary-anniversary year month day mark))))
5666 (defun org-cyclic (N year month day &optional mark)
5667 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5668 (with-no-warnings
5669 (let ((calendar-date-style 'iso))
5670 (diary-cyclic N year month day mark))))
5671 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5672 "Like `diary-block', but with fixed (ISO) order of arguments."
5673 (with-no-warnings
5674 (let ((calendar-date-style 'iso))
5675 (diary-block Y1 M1 D1 Y2 M2 D2 mark))))
5676 (defun org-date (year month day &optional mark)
5677 "Like `diary-date', but with fixed (ISO) order of arguments."
5678 (with-no-warnings
5679 (let ((calendar-date-style 'iso))
5680 (diary-date year month day mark))))
5682 ;; Define the `org-class' function
5683 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5684 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5685 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5686 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5687 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5688 `holidays', then any date that is known by the Emacs calendar to be a
5689 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5690 then those holidays will be skipped."
5691 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5692 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5693 (d (calendar-absolute-from-gregorian date))
5694 (h (when skip-weeks (calendar-check-holidays date))))
5695 (and
5696 (<= date1 d)
5697 (<= d date2)
5698 (= (calendar-day-of-week date) dayname)
5699 (or (not skip-weeks)
5700 (progn
5701 (require 'cal-iso)
5702 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5703 (not (or (and h (memq 'holidays skip-weeks))
5704 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5705 entry)))
5707 (defalias 'org-get-closed 'org-agenda-get-progress)
5708 (defun org-agenda-get-progress ()
5709 "Return the logged TODO entries for agenda display."
5710 (let* ((props (list 'mouse-face 'highlight
5711 'org-not-done-regexp org-not-done-regexp
5712 'org-todo-regexp org-todo-regexp
5713 'org-complex-heading-regexp org-complex-heading-regexp
5714 'help-echo
5715 (format "mouse-2 or RET jump to org file %s"
5716 (abbreviate-file-name buffer-file-name))))
5717 (items (if (consp org-agenda-show-log-scoped)
5718 org-agenda-show-log-scoped
5719 (if (eq org-agenda-show-log-scoped 'clockcheck)
5720 '(clock)
5721 org-agenda-log-mode-items)))
5722 (parts
5723 (delq nil
5724 (list
5725 (when (memq 'closed items) (concat "\\<" org-closed-string))
5726 (when (memq 'clock items) (concat "\\<" org-clock-string))
5727 (when (memq 'state items)
5728 (format "- State \"%s\".*?" org-todo-regexp)))))
5729 (parts-re (if parts (mapconcat 'identity parts "\\|")
5730 (error "`org-agenda-log-mode-items' is empty")))
5731 (regexp (concat
5732 "\\(" parts-re "\\)"
5733 " *\\["
5734 (regexp-quote
5735 (substring
5736 (format-time-string
5737 (car org-time-stamp-formats)
5738 (apply 'encode-time ; DATE bound by calendar
5739 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5740 1 11))))
5741 (org-agenda-search-headline-for-time nil)
5742 marker hdmarker priority category level tags closedp
5743 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5744 (goto-char (point-min))
5745 (while (re-search-forward regexp nil t)
5746 (catch :skip
5747 (org-agenda-skip)
5748 (setq marker (org-agenda-new-marker (match-beginning 0))
5749 closedp (equal (match-string 1) org-closed-string)
5750 statep (equal (string-to-char (match-string 1)) ?-)
5751 clockp (not (or closedp statep))
5752 state (and statep (match-string 2))
5753 category (org-get-category (match-beginning 0))
5754 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5755 (when (string-match "\\]" timestr)
5756 ;; substring should only run to end of time stamp
5757 (setq rest (substring timestr (match-end 0))
5758 timestr (substring timestr 0 (match-end 0)))
5759 (if (and (not closedp) (not statep)
5760 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5761 rest))
5762 (progn (setq timestr (concat (substring timestr 0 -1)
5763 "-" (match-string 1 rest) "]"))
5764 (setq clocked (match-string 2 rest)))
5765 (setq clocked "-")))
5766 (save-excursion
5767 (setq extra
5768 (cond
5769 ((not org-agenda-log-mode-add-notes) nil)
5770 (statep
5771 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5772 (match-string 1)))
5773 (clockp
5774 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5775 (match-string 1)))))
5776 (if (not (re-search-backward org-outline-regexp-bol nil t))
5777 (throw :skip nil)
5778 (goto-char (match-beginning 0))
5779 (setq hdmarker (org-agenda-new-marker)
5780 inherited-tags
5781 (or (eq org-agenda-show-inherited-tags 'always)
5782 (and (listp org-agenda-show-inherited-tags)
5783 (memq 'todo org-agenda-show-inherited-tags))
5784 (and (eq org-agenda-show-inherited-tags t)
5785 (or (eq org-agenda-use-tag-inheritance t)
5786 (memq 'todo org-agenda-use-tag-inheritance))))
5787 tags (org-get-tags nil (not inherited-tags))
5788 level (make-string (org-reduced-level (org-outline-level)) ? ))
5789 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5790 (setq txt (match-string 1))
5791 (when extra
5792 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5793 (setq txt (concat (substring txt 0 (match-beginning 1))
5794 " - " extra " " (match-string 2 txt)))
5795 (setq txt (concat txt " - " extra))))
5796 (setq txt (org-agenda-format-item
5797 (cond
5798 (closedp "Closed: ")
5799 (statep (concat "State: (" state ")"))
5800 (t (concat "Clocked: (" clocked ")")))
5801 txt level category tags timestr)))
5802 (setq priority 100000)
5803 (org-add-props txt props
5804 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5805 'priority priority 'level level
5806 'type "closed" 'date date
5807 'undone-face 'org-warning 'done-face 'org-agenda-done)
5808 (push txt ee))
5809 (goto-char (point-at-eol))))
5810 (nreverse ee)))
5812 (defun org-agenda-show-clocking-issues ()
5813 "Add overlays, showing issues with clocking.
5814 See also the user option `org-agenda-clock-consistency-checks'."
5815 (interactive)
5816 (let* ((pl org-agenda-clock-consistency-checks)
5817 (re (concat "^[ \t]*"
5818 org-clock-string
5819 "[ \t]+"
5820 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5821 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5822 (tlstart 0.)
5823 (tlend 0.)
5824 (maxtime (org-duration-to-minutes
5825 (or (plist-get pl :max-duration) "24:00")))
5826 (mintime (org-duration-to-minutes
5827 (or (plist-get pl :min-duration) 0)))
5828 (maxgap (org-duration-to-minutes
5829 ;; default 30:00 means never complain
5830 (or (plist-get pl :max-gap) "30:00")))
5831 (gapok (mapcar #'org-duration-to-minutes
5832 (plist-get pl :gap-ok-around)))
5833 (def-face (or (plist-get pl :default-face)
5834 '((:background "DarkRed") (:foreground "white"))))
5835 issue face m te ts dt ov)
5836 (goto-char (point-min))
5837 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5838 (setq issue nil face def-face)
5839 (catch 'next
5840 (setq m (org-get-at-bol 'org-marker)
5841 te nil ts nil)
5842 (unless (and m (markerp m))
5843 (setq issue "No valid clock line") (throw 'next t))
5844 (org-with-point-at m
5845 (save-excursion
5846 (goto-char (point-at-bol))
5847 (unless (looking-at re)
5848 (error "No valid Clock line")
5849 (throw 'next t))
5850 (unless (match-end 3)
5851 (setq issue "No end time"
5852 face (or (plist-get pl :no-end-time-face) face))
5853 (throw 'next t))
5854 (setq ts (match-string 1)
5855 te (match-string 3)
5856 ts (float-time
5857 (apply #'encode-time (org-parse-time-string ts)))
5858 te (float-time
5859 (apply #'encode-time (org-parse-time-string te)))
5860 dt (- te ts))))
5861 (cond
5862 ((> dt (* 60 maxtime))
5863 ;; a very long clocking chunk
5864 (setq issue (format "Clocking interval is very long: %s"
5865 (org-duration-from-minutes (floor (/ dt 60.))))
5866 face (or (plist-get pl :long-face) face)))
5867 ((< dt (* 60 mintime))
5868 ;; a very short clocking chunk
5869 (setq issue (format "Clocking interval is very short: %s"
5870 (org-duration-from-minutes (floor (/ dt 60.))))
5871 face (or (plist-get pl :short-face) face)))
5872 ((and (> tlend 0) (< ts tlend))
5873 ;; Two clock entries are overlapping
5874 (setq issue (format "Clocking overlap: %d minutes"
5875 (/ (- tlend ts) 60))
5876 face (or (plist-get pl :overlap-face) face)))
5877 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5878 ;; There is a gap, lets see if we need to report it
5879 (unless (org-agenda-check-clock-gap tlend ts gapok)
5880 (setq issue (format "Clocking gap: %d minutes"
5881 (/ (- ts tlend) 60))
5882 face (or (plist-get pl :gap-face) face))))
5883 (t nil)))
5884 (setq tlend (or te tlend) tlstart (or ts tlstart))
5885 (when issue
5886 ;; OK, there was some issue, add an overlay to show the issue
5887 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5888 (overlay-put ov 'before-string
5889 (concat
5890 (org-add-props
5891 (format "%-43s" (concat " " issue))
5893 'face face)
5894 "\n"))
5895 (overlay-put ov 'evaporate t)))))
5897 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5898 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5899 (catch 'exit
5900 (unless ok-list
5901 ;; there are no OK times for gaps...
5902 (throw 'exit nil))
5903 (when (> (- (/ t2 36000) (/ t1 36000)) 24)
5904 ;; This is more than 24 hours, so it is OK.
5905 ;; because we have at least one OK time, that must be in the
5906 ;; 24 hour interval.
5907 (throw 'exit t))
5908 ;; We have a shorter gap.
5909 ;; Now we have to get the minute of the day when these times are
5910 (let* ((t1dec (decode-time (seconds-to-time t1)))
5911 (t2dec (decode-time (seconds-to-time t2)))
5912 ;; compute the minute on the day
5913 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5914 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5915 (when (< min2 min1)
5916 ;; if min2 is smaller than min1, this means it is on the next day.
5917 ;; Wrap it to after midnight.
5918 (setq min2 (+ min2 1440)))
5919 ;; Now check if any of the OK times is in the gap
5920 (mapc (lambda (x)
5921 ;; Wrap the time to after midnight if necessary
5922 (when (< x min1) (setq x (+ x 1440)))
5923 ;; Check if in interval
5924 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5925 ok-list)
5926 ;; Nope, this gap is not OK
5927 nil)))
5929 (defun org-agenda-get-deadlines (&optional with-hour)
5930 "Return the deadline information for agenda display.
5931 When WITH-HOUR is non-nil, only return deadlines with an hour
5932 specification like [h]h:mm."
5933 (let* ((props (list 'mouse-face 'highlight
5934 'org-not-done-regexp org-not-done-regexp
5935 'org-todo-regexp org-todo-regexp
5936 'org-complex-heading-regexp org-complex-heading-regexp
5937 'help-echo
5938 (format "mouse-2 or RET jump to org file %s"
5939 (abbreviate-file-name buffer-file-name))))
5940 (regexp (if with-hour
5941 org-deadline-time-hour-regexp
5942 org-deadline-time-regexp))
5943 (today (org-today))
5944 (today? (org-agenda-today-p date)) ; DATE bound by calendar.
5945 (current (calendar-absolute-from-gregorian date))
5946 deadline-items)
5947 (goto-char (point-min))
5948 (while (re-search-forward regexp nil t)
5949 (catch :skip
5950 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
5951 (org-agenda-skip)
5952 (let* ((s (match-string 1))
5953 (pos (1- (match-beginning 1)))
5954 (todo-state (save-match-data (org-get-todo-state)))
5955 (done? (member todo-state org-done-keywords))
5956 (sexp? (string-prefix-p "%%" s))
5957 ;; DEADLINE is the deadline date for the entry. It is
5958 ;; either the base date or the last repeat, according
5959 ;; to `org-agenda-prefer-last-repeat'.
5960 (deadline
5961 (cond
5962 (sexp? (org-agenda--timestamp-to-absolute s current))
5963 ((or (eq org-agenda-prefer-last-repeat t)
5964 (member todo-state org-agenda-prefer-last-repeat))
5965 (org-agenda--timestamp-to-absolute
5966 s today 'past (current-buffer) pos))
5967 (t (org-agenda--timestamp-to-absolute s))))
5968 ;; REPEAT is the future repeat closest from CURRENT,
5969 ;; according to `org-agenda-show-future-repeats'. If
5970 ;; the latter is nil, or if the time stamp has no
5971 ;; repeat part, default to DEADLINE.
5972 (repeat
5973 (cond
5974 (sexp? deadline)
5975 ((<= current today) deadline)
5976 ((not org-agenda-show-future-repeats) deadline)
5978 (let ((base (if (eq org-agenda-show-future-repeats 'next)
5979 (1+ today)
5980 current)))
5981 (org-agenda--timestamp-to-absolute
5982 s base 'future (current-buffer) pos)))))
5983 (diff (- deadline current))
5984 (suppress-prewarning
5985 (let ((scheduled
5986 (and org-agenda-skip-deadline-prewarning-if-scheduled
5987 (org-entry-get nil "SCHEDULED"))))
5988 (cond
5989 ((not scheduled) nil)
5990 ;; The current item has a scheduled date, so
5991 ;; evaluate its prewarning lead time.
5992 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
5993 ;; Use global prewarning-restart lead time.
5994 org-agenda-skip-deadline-prewarning-if-scheduled)
5995 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
5996 'pre-scheduled)
5997 ;; Set pre-warning to no earlier than SCHEDULED.
5998 (min (- deadline
5999 (org-agenda--timestamp-to-absolute scheduled))
6000 org-deadline-warning-days))
6001 ;; Set pre-warning to deadline.
6002 (t 0))))
6003 (wdays (or suppress-prewarning (org-get-wdays s))))
6004 (cond
6005 ;; Only display deadlines at their base date, at future
6006 ;; repeat occurrences or in today agenda.
6007 ((= current deadline) nil)
6008 ((= current repeat) nil)
6009 ((not today?) (throw :skip nil))
6010 ;; Upcoming deadline: display within warning period WDAYS.
6011 ((> deadline current) (when (> diff wdays) (throw :skip nil)))
6012 ;; Overdue deadline: warn about it for
6013 ;; `org-deadline-past-days' duration.
6014 (t (when (< org-deadline-past-days (- diff)) (throw :skip nil))))
6015 ;; Possibly skip done tasks.
6016 (when (and done?
6017 (or org-agenda-skip-deadline-if-done
6018 (/= deadline current)))
6019 (throw :skip nil))
6020 (save-excursion
6021 (re-search-backward "^\\*+[ \t]+" nil t)
6022 (goto-char (match-end 0))
6023 (let* ((category (org-get-category))
6024 (level (make-string (org-reduced-level (org-outline-level))
6025 ?\s))
6026 (head (buffer-substring (point) (line-end-position)))
6027 (inherited-tags
6028 (or (eq org-agenda-show-inherited-tags 'always)
6029 (and (listp org-agenda-show-inherited-tags)
6030 (memq 'agenda org-agenda-show-inherited-tags))
6031 (and (eq org-agenda-show-inherited-tags t)
6032 (or (eq org-agenda-use-tag-inheritance t)
6033 (memq 'agenda
6034 org-agenda-use-tag-inheritance)))))
6035 (tags (org-get-tags nil (not inherited-tags)))
6036 (time
6037 (cond
6038 ;; No time of day designation if it is only
6039 ;; a reminder.
6040 ((and (/= current deadline) (/= current repeat)) nil)
6041 ((string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6042 (concat (substring s (match-beginning 1)) " "))
6043 (t 'time)))
6044 (item
6045 (org-agenda-format-item
6046 ;; Insert appropriate suffixes before deadlines.
6047 ;; Those only apply to today agenda.
6048 (pcase-let ((`(,now ,future ,past)
6049 org-agenda-deadline-leaders))
6050 (cond
6051 ((and today? (< deadline today)) (format past (- diff)))
6052 ((and today? (> deadline today)) (format future diff))
6053 (t now)))
6054 head level category tags time))
6055 (face (org-agenda-deadline-face
6056 (- 1 (/ (float diff) (max wdays 1)))))
6057 (upcoming? (and today? (> deadline today)))
6058 (warntime (get-text-property (point) 'org-appt-warntime)))
6059 (org-add-props item props
6060 'org-marker (org-agenda-new-marker pos)
6061 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
6062 'warntime warntime
6063 'level level
6064 'ts-date deadline
6065 'priority
6066 ;; Adjust priority to today reminders about deadlines.
6067 ;; Overdue deadlines get the highest priority
6068 ;; increase, then imminent deadlines and eventually
6069 ;; more distant deadlines.
6070 (let ((adjust (if today? (- diff) 0)))
6071 (+ adjust (org-get-priority item)))
6072 'todo-state todo-state
6073 'type (if upcoming? "upcoming-deadline" "deadline")
6074 'date (if upcoming? date deadline)
6075 'face (if done? 'org-agenda-done face)
6076 'undone-face face
6077 'done-face 'org-agenda-done)
6078 (push item deadline-items))))))
6079 (nreverse deadline-items)))
6081 (defun org-agenda-deadline-face (fraction)
6082 "Return the face to displaying a deadline item.
6083 FRACTION is what fraction of the head-warning time has passed."
6084 (assoc-default fraction org-agenda-deadline-faces #'<=))
6086 (defun org-agenda-get-scheduled (&optional deadlines with-hour)
6087 "Return the scheduled information for agenda display.
6088 Optional argument DEADLINES is a list of deadline items to be
6089 displayed in agenda view. When WITH-HOUR is non-nil, only return
6090 scheduled items with an hour specification like [h]h:mm."
6091 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
6092 'org-todo-regexp org-todo-regexp
6093 'org-complex-heading-regexp org-complex-heading-regexp
6094 'done-face 'org-agenda-done
6095 'mouse-face 'highlight
6096 'help-echo
6097 (format "mouse-2 or RET jump to Org file %s"
6098 (abbreviate-file-name buffer-file-name))))
6099 (regexp (if with-hour
6100 org-scheduled-time-hour-regexp
6101 org-scheduled-time-regexp))
6102 (today (org-today))
6103 (todayp (org-agenda-today-p date)) ; DATE bound by calendar.
6104 (current (calendar-absolute-from-gregorian date))
6105 (deadline-pos
6106 (mapcar (lambda (d)
6107 (let ((m (get-text-property 0 'org-hd-marker d)))
6108 (and m (marker-position m))))
6109 deadlines))
6110 scheduled-items)
6111 (goto-char (point-min))
6112 (while (re-search-forward regexp nil t)
6113 (catch :skip
6114 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
6115 (org-agenda-skip)
6116 (let* ((s (match-string 1))
6117 (pos (1- (match-beginning 1)))
6118 (todo-state (save-match-data (org-get-todo-state)))
6119 (donep (member todo-state org-done-keywords))
6120 (sexp? (string-prefix-p "%%" s))
6121 ;; SCHEDULE is the scheduled date for the entry. It is
6122 ;; either the bare date or the last repeat, according
6123 ;; to `org-agenda-prefer-last-repeat'.
6124 (schedule
6125 (cond
6126 (sexp? (org-agenda--timestamp-to-absolute s current))
6127 ((or (eq org-agenda-prefer-last-repeat t)
6128 (member todo-state org-agenda-prefer-last-repeat))
6129 (org-agenda--timestamp-to-absolute
6130 s today 'past (current-buffer) pos))
6131 (t (org-agenda--timestamp-to-absolute s))))
6132 ;; REPEAT is the future repeat closest from CURRENT,
6133 ;; according to `org-agenda-show-future-repeats'. If
6134 ;; the latter is nil, or if the time stamp has no
6135 ;; repeat part, default to SCHEDULE.
6136 (repeat
6137 (cond
6138 (sexp? schedule)
6139 ((<= current today) schedule)
6140 ((not org-agenda-show-future-repeats) schedule)
6142 (let ((base (if (eq org-agenda-show-future-repeats 'next)
6143 (1+ today)
6144 current)))
6145 (org-agenda--timestamp-to-absolute
6146 s base 'future (current-buffer) pos)))))
6147 (diff (- current schedule))
6148 (warntime (get-text-property (point) 'org-appt-warntime))
6149 (pastschedp (< schedule today))
6150 (habitp (and (fboundp 'org-is-habit-p) (org-is-habit-p)))
6151 (suppress-delay
6152 (let ((deadline (and org-agenda-skip-scheduled-delay-if-deadline
6153 (org-entry-get nil "DEADLINE"))))
6154 (cond
6155 ((not deadline) nil)
6156 ;; The current item has a deadline date, so
6157 ;; evaluate its delay time.
6158 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6159 ;; Use global delay time.
6160 (- org-agenda-skip-scheduled-delay-if-deadline))
6161 ((eq org-agenda-skip-scheduled-delay-if-deadline
6162 'post-deadline)
6163 ;; Set delay to no later than DEADLINE.
6164 (min (- schedule
6165 (org-agenda--timestamp-to-absolute deadline))
6166 org-scheduled-delay-days))
6167 (t 0))))
6168 (ddays
6169 (cond
6170 ;; Nullify delay when a repeater triggered already
6171 ;; and the delay is of the form --Xd.
6172 ((and (string-match-p "--[0-9]+[hdwmy]" s)
6173 (> schedule (org-agenda--timestamp-to-absolute s)))
6175 (suppress-delay
6176 (let ((org-scheduled-delay-days suppress-delay))
6177 (org-get-wdays s t t)))
6178 (t (org-get-wdays s t)))))
6179 ;; Display scheduled items at base date (SCHEDULE), today if
6180 ;; scheduled before the current date, and at any repeat past
6181 ;; today. However, skip delayed items and items that have
6182 ;; been displayed for more than `org-scheduled-past-days'.
6183 (unless (and todayp
6184 habitp
6185 (bound-and-true-p org-habit-show-all-today))
6186 (when (or (and (> ddays 0) (< diff ddays))
6187 (> diff org-scheduled-past-days)
6188 (> schedule current)
6189 (and (/= current schedule)
6190 (/= current today)
6191 (/= current repeat)))
6192 (throw :skip nil)))
6193 ;; Possibly skip done tasks.
6194 (when (and donep
6195 (or org-agenda-skip-scheduled-if-done
6196 (/= schedule current)))
6197 (throw :skip nil))
6198 ;; Skip entry if it already appears as a deadline, per
6199 ;; `org-agenda-skip-scheduled-if-deadline-is-shown'. This
6200 ;; doesn't apply to habits.
6201 (when (pcase org-agenda-skip-scheduled-if-deadline-is-shown
6202 ((guard
6203 (or (not (memq (line-beginning-position 0) deadline-pos))
6204 habitp))
6205 nil)
6206 (`repeated-after-deadline
6207 (let ((deadline (time-to-days
6208 (org-get-deadline-time (point)))))
6209 (and (<= schedule deadline) (> current deadline))))
6210 (`not-today pastschedp)
6211 (`t t)
6212 (_ nil))
6213 (throw :skip nil))
6214 ;; Skip habits if `org-habit-show-habits' is nil, or if we
6215 ;; only show them for today. Also skip done habits.
6216 (when (and habitp
6217 (or donep
6218 (not (bound-and-true-p org-habit-show-habits))
6219 (and (not todayp)
6220 (bound-and-true-p
6221 org-habit-show-habits-only-for-today))))
6222 (throw :skip nil))
6223 (save-excursion
6224 (re-search-backward "^\\*+[ \t]+" nil t)
6225 (goto-char (match-end 0))
6226 (let* ((category (org-get-category))
6227 (inherited-tags
6228 (or (eq org-agenda-show-inherited-tags 'always)
6229 (and (listp org-agenda-show-inherited-tags)
6230 (memq 'agenda org-agenda-show-inherited-tags))
6231 (and (eq org-agenda-show-inherited-tags t)
6232 (or (eq org-agenda-use-tag-inheritance t)
6233 (memq 'agenda
6234 org-agenda-use-tag-inheritance)))))
6235 (tags (org-get-tags nil (not inherited-tags)))
6236 (level (make-string (org-reduced-level (org-outline-level))
6237 ?\s))
6238 (head (buffer-substring (point) (line-end-position)))
6239 (time
6240 (cond
6241 ;; No time of day designation if it is only
6242 ;; a reminder.
6243 ((and (/= current schedule) (/= current repeat)) nil)
6244 ((string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6245 (concat (substring s (match-beginning 1)) " "))
6246 (t 'time)))
6247 (item
6248 (org-agenda-format-item
6249 (pcase-let ((`(,first ,past) org-agenda-scheduled-leaders))
6250 ;; Show a reminder of a past scheduled today.
6251 (if (and todayp pastschedp)
6252 (format past diff)
6253 first))
6254 head level category tags time nil habitp))
6255 (face (cond ((and (not habitp) pastschedp)
6256 'org-scheduled-previously)
6257 (todayp 'org-scheduled-today)
6258 (t 'org-scheduled)))
6259 (habitp (and habitp (org-habit-parse-todo))))
6260 (org-add-props item props
6261 'undone-face face
6262 'face (if donep 'org-agenda-done face)
6263 'org-marker (org-agenda-new-marker pos)
6264 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
6265 'type (if pastschedp "past-scheduled" "scheduled")
6266 'date (if pastschedp schedule date)
6267 'ts-date schedule
6268 'warntime warntime
6269 'level level
6270 'priority (if habitp (org-habit-get-priority habitp)
6271 (+ 99 diff (org-get-priority item)))
6272 'org-habit-p habitp
6273 'todo-state todo-state)
6274 (push item scheduled-items))))))
6275 (nreverse scheduled-items)))
6277 (defun org-agenda-get-blocks ()
6278 "Return the date-range information for agenda display."
6279 (let* ((props (list 'face nil
6280 'org-not-done-regexp org-not-done-regexp
6281 'org-todo-regexp org-todo-regexp
6282 'org-complex-heading-regexp org-complex-heading-regexp
6283 'mouse-face 'highlight
6284 'help-echo
6285 (format "mouse-2 or RET jump to org file %s"
6286 (abbreviate-file-name buffer-file-name))))
6287 (regexp org-tr-regexp)
6288 (d0 (calendar-absolute-from-gregorian date))
6289 marker hdmarker ee txt d1 d2 s1 s2 category
6290 level todo-state tags pos head donep inherited-tags)
6291 (goto-char (point-min))
6292 (while (re-search-forward regexp nil t)
6293 (catch :skip
6294 (org-agenda-skip)
6295 (setq pos (point))
6296 (let ((start-time (match-string 1))
6297 (end-time (match-string 2)))
6298 (setq s1 (match-string 1)
6299 s2 (match-string 2)
6300 d1 (time-to-days
6301 (condition-case err
6302 (org-time-string-to-time s1)
6303 (error
6304 (error
6305 "Bad timestamp %S at %d in buffer %S\nError was: %s"
6308 (current-buffer)
6309 (error-message-string err)))))
6310 d2 (time-to-days
6311 (condition-case err
6312 (org-time-string-to-time s2)
6313 (error
6314 (error
6315 "Bad timestamp %S at %d in buffer %S\nError was: %s"
6318 (current-buffer)
6319 (error-message-string err))))))
6320 (when (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6321 ;; Only allow days between the limits, because the normal
6322 ;; date stamps will catch the limits.
6323 (save-excursion
6324 (setq todo-state (org-get-todo-state))
6325 (setq donep (member todo-state org-done-keywords))
6326 (when (and donep org-agenda-skip-timestamp-if-done)
6327 (throw :skip t))
6328 (setq marker (org-agenda-new-marker (point))
6329 category (org-get-category))
6330 (if (not (re-search-backward org-outline-regexp-bol nil t))
6331 (throw :skip nil)
6332 (goto-char (match-beginning 0))
6333 (setq hdmarker (org-agenda-new-marker (point))
6334 inherited-tags
6335 (or (eq org-agenda-show-inherited-tags 'always)
6336 (and (listp org-agenda-show-inherited-tags)
6337 (memq 'agenda org-agenda-show-inherited-tags))
6338 (and (eq org-agenda-show-inherited-tags t)
6339 (or (eq org-agenda-use-tag-inheritance t)
6340 (memq 'agenda org-agenda-use-tag-inheritance))))
6342 tags (org-get-tags nil (not inherited-tags)))
6343 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6344 (looking-at "\\*+[ \t]+\\(.*\\)")
6345 (setq head (match-string 1))
6346 (let ((remove-re
6347 (if org-agenda-remove-timeranges-from-blocks
6348 (concat
6349 "<" (regexp-quote s1) ".*?>"
6350 "--"
6351 "<" (regexp-quote s2) ".*?>")
6352 nil)))
6353 (setq txt (org-agenda-format-item
6354 (format
6355 (nth (if (= d1 d2) 0 1)
6356 org-agenda-timerange-leaders)
6357 (1+ (- d0 d1)) (1+ (- d2 d1)))
6358 head level category tags
6359 (cond ((and (= d1 d0) (= d2 d0))
6360 (concat "<" start-time ">--<" end-time ">"))
6361 ((= d1 d0)
6362 (concat "<" start-time ">"))
6363 ((= d2 d0)
6364 (concat "<" end-time ">")))
6365 remove-re))))
6366 (org-add-props txt props
6367 'org-marker marker 'org-hd-marker hdmarker
6368 'type "block" 'date date
6369 'level level
6370 'todo-state todo-state
6371 'priority (org-get-priority txt))
6372 (push txt ee))))
6373 (goto-char pos)))
6374 ;; Sort the entries by expiration date.
6375 (nreverse ee)))
6377 ;;; Agenda presentation and sorting
6379 (defvar org-prefix-has-time nil
6380 "A flag, set by `org-compile-prefix-format'.
6381 The flag is set if the currently compiled format contains a `%t'.")
6382 (defvar org-prefix-has-tag nil
6383 "A flag, set by `org-compile-prefix-format'.
6384 The flag is set if the currently compiled format contains a `%T'.")
6385 (defvar org-prefix-has-effort nil
6386 "A flag, set by `org-compile-prefix-format'.
6387 The flag is set if the currently compiled format contains a `%e'.")
6388 (defvar org-prefix-has-breadcrumbs nil
6389 "A flag, set by `org-compile-prefix-format'.
6390 The flag is set if the currently compiled format contains a `%b'.")
6391 (defvar org-prefix-category-length nil
6392 "Used by `org-compile-prefix-format' to remember the category field width.")
6393 (defvar org-prefix-category-max-length nil
6394 "Used by `org-compile-prefix-format' to remember the category field width.")
6396 (defun org-agenda-get-category-icon (category)
6397 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6398 (cl-dolist (entry org-agenda-category-icon-alist)
6399 (when (string-match-p (car entry) category)
6400 (if (listp (cadr entry))
6401 (cl-return (cadr entry))
6402 (cl-return (apply #'create-image (cdr entry)))))))
6404 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6405 remove-re habitp)
6406 "Format TXT to be inserted into the agenda buffer.
6407 In particular, add the prefix and corresponding text properties.
6409 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6410 LEVEL may be a string to replace the `%l' specifier.
6411 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6412 category taken from local variable or file name. It will replace the `%c'
6413 specifier in the format.
6414 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6415 TXT for sorting of this entry, and for the `%t' specifier in the format.
6416 When DOTIME is a string, this string is searched for a time before TXT is.
6417 TAGS can be the tags of the headline.
6418 Any match of REMOVE-RE will be removed from TXT."
6419 ;; We keep the org-prefix-* variable values along with a compiled
6420 ;; formatter, so that multiple agendas existing at the same time do
6421 ;; not step on each other toes.
6423 ;; It was inconvenient to make these variables buffer local in
6424 ;; Agenda buffers, because this function expects to be called with
6425 ;; the buffer where item comes from being current, and not agenda
6426 ;; buffer
6427 (let* ((bindings (car org-prefix-format-compiled))
6428 (formatter (cadr org-prefix-format-compiled)))
6429 (cl-loop for (var value) in bindings
6430 do (set var value))
6431 (save-match-data
6432 ;; Diary entries sometimes have extra whitespace at the beginning
6433 (setq txt (org-trim txt))
6435 ;; Fix the tags part in txt
6436 (setq txt (org-agenda-fix-displayed-tags
6437 txt tags
6438 org-agenda-show-inherited-tags
6439 org-agenda-hide-tags-regexp))
6441 (let* ((category (or category
6442 (if buffer-file-name
6443 (file-name-sans-extension
6444 (file-name-nondirectory buffer-file-name))
6445 "")))
6446 (category-icon (org-agenda-get-category-icon category))
6447 (category-icon (if category-icon
6448 (propertize " " 'display category-icon)
6449 ""))
6450 (effort (and (not (string= txt ""))
6451 (get-text-property 1 'effort txt)))
6452 ;; time, tag, effort are needed for the eval of the prefix format
6453 (tag (if tags (nth (1- (length tags)) tags) ""))
6454 (time-grid-trailing-characters (nth 2 org-agenda-time-grid))
6455 time
6456 (ts (when dotime (concat
6457 (if (stringp dotime) dotime "")
6458 (and org-agenda-search-headline-for-time txt))))
6459 (time-of-day (and dotime (org-get-time-of-day ts)))
6460 stamp plain s0 s1 s2 rtn srp l
6461 duration breadcrumbs)
6462 (and (derived-mode-p 'org-mode) buffer-file-name
6463 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6464 (when (and dotime time-of-day)
6465 ;; Extract starting and ending time and move them to prefix
6466 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6467 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6468 (setq s0 (match-string 0 ts)
6469 srp (and stamp (match-end 3))
6470 s1 (match-string (if plain 1 2) ts)
6471 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6473 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6474 ;; them, we might want to remove them there to avoid duplication.
6475 ;; The user can turn this off with a variable.
6476 (when (and org-prefix-has-time
6477 org-agenda-remove-times-when-in-prefix (or stamp plain)
6478 (string-match (concat (regexp-quote s0) " *") txt)
6479 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6480 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6481 (= (match-beginning 0) 0)
6483 (setq txt (replace-match "" nil nil txt))))
6484 ;; Normalize the time(s) to 24 hour
6485 (when s1 (setq s1 (org-get-time-of-day s1 'string t)))
6486 (when s2 (setq s2 (org-get-time-of-day s2 'string t)))
6488 ;; Try to set s2 if s1 and
6489 ;; `org-agenda-default-appointment-duration' are set
6490 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6491 (setq s2
6492 (org-duration-from-minutes
6493 (+ (org-duration-to-minutes s1 t)
6494 org-agenda-default-appointment-duration)
6495 nil t)))
6497 ;; Compute the duration
6498 (when s2
6499 (setq duration (- (org-duration-to-minutes s2)
6500 (org-duration-to-minutes s1)))))
6502 (when (string-match org-tag-group-re txt)
6503 ;; Tags are in the string
6504 (if (or (eq org-agenda-remove-tags t)
6505 (and org-agenda-remove-tags
6506 org-prefix-has-tag))
6507 (setq txt (replace-match "" t t txt))
6508 (setq txt (replace-match
6509 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6510 (match-string 1 txt))
6511 t t txt))))
6513 (when remove-re
6514 (while (string-match remove-re txt)
6515 (setq txt (replace-match "" t t txt))))
6517 ;; Set org-heading property on `txt' to mark the start of the
6518 ;; heading.
6519 (add-text-properties 0 (length txt) '(org-heading t) txt)
6521 ;; Prepare the variables needed in the eval of the compiled format
6522 (when org-prefix-has-breadcrumbs
6523 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6524 (let ((s (org-format-outline-path (org-get-outline-path)
6525 (1- (frame-width))
6526 nil "->")))
6527 (if (eq "" s) "" (concat s "->"))))))
6528 (setq time (cond (s2 (concat
6529 (org-agenda-time-of-day-to-ampm-maybe s1)
6530 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6531 (when org-agenda-timegrid-use-ampm " ")))
6532 (s1 (concat
6533 (org-agenda-time-of-day-to-ampm-maybe s1)
6534 (if org-agenda-timegrid-use-ampm
6535 (concat time-grid-trailing-characters " ")
6536 time-grid-trailing-characters)))
6537 (t ""))
6538 extra (or (and (not habitp) extra) "")
6539 category (if (symbolp category) (symbol-name category) category)
6540 level (or level ""))
6541 (if (string-match org-bracket-link-regexp category)
6542 (progn
6543 (setq l (if (match-end 3)
6544 (- (match-end 3) (match-beginning 3))
6545 (- (match-end 1) (match-beginning 1))))
6546 (when (< l (or org-prefix-category-length 0))
6547 (setq category (copy-sequence category))
6548 (org-add-props category nil
6549 'extra-space (make-string
6550 (- org-prefix-category-length l 1) ?\ ))))
6551 (when (and org-prefix-category-max-length
6552 (>= (length category) org-prefix-category-max-length))
6553 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6554 ;; Evaluate the compiled format
6555 (setq rtn (concat (eval formatter) txt))
6557 ;; And finally add the text properties
6558 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6559 (org-add-props rtn nil
6560 'org-category category
6561 'tags (mapcar 'org-downcase-keep-props tags)
6562 'org-highest-priority org-highest-priority
6563 'org-lowest-priority org-lowest-priority
6564 'time-of-day time-of-day
6565 'duration duration
6566 'breadcrumbs breadcrumbs
6567 'txt txt
6568 'level level
6569 'time time
6570 'extra extra
6571 'format org-prefix-format-compiled
6572 'dotime dotime)))))
6574 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6575 "Remove tags string from TXT, and add a modified list of tags.
6576 The modified list may contain inherited tags, and tags matched by
6577 `org-agenda-hide-tags-regexp' will be removed."
6578 (when (or add-inherited hide-re)
6579 (when (string-match org-tag-group-re txt)
6580 (setq txt (substring txt 0 (match-beginning 0))))
6581 (setq tags
6582 (delq nil
6583 (mapcar (lambda (tg)
6584 (if (or (and hide-re (string-match hide-re tg))
6585 (and (not add-inherited)
6586 (get-text-property 0 'inherited tg)))
6588 tg))
6589 tags)))
6590 (when tags
6591 (let ((have-i (get-text-property 0 'inherited (car tags)))
6593 (setq txt (concat txt " :"
6594 (mapconcat
6595 (lambda (x)
6596 (setq i (get-text-property 0 'inherited x))
6597 (if (and have-i (not i))
6598 (progn
6599 (setq have-i nil)
6600 (concat ":" x))
6602 tags ":")
6603 (if have-i "::" ":"))))))
6604 txt)
6606 (defun org-downcase-keep-props (s)
6607 (let ((props (text-properties-at 0 s)))
6608 (setq s (downcase s))
6609 (add-text-properties 0 (length s) props s)
6612 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6614 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6615 "Add a time-grid for agenda items which need it.
6617 LIST is the list of agenda items formatted by `org-agenda-list'.
6618 NDAYS is the span of the current agenda view.
6619 TODAYP is t when the current agenda view is on today."
6620 (catch 'exit
6621 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6622 ((and todayp (member 'today (car org-agenda-time-grid))))
6623 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6624 ((member 'weekly (car org-agenda-time-grid)))
6625 (t (throw 'exit list)))
6626 (let* ((have (delq nil (mapcar
6627 (lambda (x) (get-text-property 1 'time-of-day x))
6628 list)))
6629 (string (nth 3 org-agenda-time-grid))
6630 (gridtimes (nth 1 org-agenda-time-grid))
6631 (req (car org-agenda-time-grid))
6632 (remove (member 'remove-match req))
6633 new time)
6634 (when (and (member 'require-timed req) (not have))
6635 ;; don't show empty grid
6636 (throw 'exit list))
6637 (while (setq time (pop gridtimes))
6638 (unless (and remove (member time have))
6639 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6640 (push (org-agenda-format-item
6641 nil string nil "" nil
6642 (concat (substring time 0 -2) ":" (substring time -2)))
6643 new)
6644 (put-text-property
6645 2 (length (car new)) 'face 'org-time-grid (car new))))
6646 (when (and todayp org-agenda-show-current-time-in-grid)
6647 (push (org-agenda-format-item
6648 nil org-agenda-current-time-string nil "" nil
6649 (format-time-string "%H:%M "))
6650 new)
6651 (put-text-property
6652 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6654 (if (member 'time-up org-agenda-sorting-strategy-selected)
6655 (append new list)
6656 (append list new)))))
6658 (defun org-compile-prefix-format (key)
6659 "Compile the prefix format into a Lisp form that can be evaluated.
6660 The resulting form and associated variable bindings is returned
6661 and stored in the variable `org-prefix-format-compiled'."
6662 (setq org-prefix-has-time nil
6663 org-prefix-has-tag nil
6664 org-prefix-category-length nil
6665 org-prefix-has-effort nil
6666 org-prefix-has-breadcrumbs nil)
6667 (let ((s (cond
6668 ((stringp org-agenda-prefix-format)
6669 org-agenda-prefix-format)
6670 ((assq key org-agenda-prefix-format)
6671 (cdr (assq key org-agenda-prefix-format)))
6672 (t " %-12:c%?-12t% s")))
6673 (start 0)
6674 varform vars var e c f opt)
6675 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6676 s start)
6677 (setq var (or (cdr (assoc (match-string 4 s)
6678 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6679 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6680 'eval)
6681 c (or (match-string 3 s) "")
6682 opt (match-beginning 1)
6683 start (1+ (match-beginning 0)))
6684 (cl-case var
6685 (time (setq org-prefix-has-time t))
6686 (tag (setq org-prefix-has-tag t))
6687 (effort (setq org-prefix-has-effort t))
6688 (breadcrumbs (setq org-prefix-has-breadcrumbs t)))
6689 (setq f (concat "%" (match-string 2 s) "s"))
6690 (when (eq var 'category)
6691 (setq org-prefix-category-length
6692 (floor (abs (string-to-number (match-string 2 s)))))
6693 (setq org-prefix-category-max-length
6694 (let ((x (match-string 2 s)))
6695 (when (string-match-p "\\.[0-9]+" x)
6696 (string-to-number (substring (match-string 0 x) 1))))))
6697 (if (eq var 'eval)
6698 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6699 (if opt
6700 (setq varform
6701 `(if (or (equal "" ,var) (equal nil ,var))
6703 (format ,f (concat ,var ,c))))
6704 (setq varform
6705 `(format ,f (if (or (equal ,var "")
6706 (equal ,var nil)) ""
6707 (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6708 (setq s (replace-match "%s" t nil s))
6709 (push varform vars))
6710 (setq vars (nreverse vars))
6711 (with-current-buffer (or org-agenda-buffer (current-buffer))
6712 (setq org-prefix-format-compiled
6713 (list
6714 `((org-prefix-has-time ,org-prefix-has-time)
6715 (org-prefix-has-tag ,org-prefix-has-tag)
6716 (org-prefix-category-length ,org-prefix-category-length)
6717 (org-prefix-has-effort ,org-prefix-has-effort)
6718 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6719 `(format ,s ,@vars))))))
6721 (defun org-set-sorting-strategy (key)
6722 (if (symbolp (car org-agenda-sorting-strategy))
6723 ;; the old format
6724 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6725 (setq org-agenda-sorting-strategy-selected
6726 (or (cdr (assq key org-agenda-sorting-strategy))
6727 (cdr (assq 'agenda org-agenda-sorting-strategy))
6728 '(time-up category-keep priority-down)))))
6730 (defun org-get-time-of-day (s &optional string mod24)
6731 "Check string S for a time of day.
6732 If found, return it as a military time number between 0 and 2400.
6733 If not found, return nil.
6734 The optional STRING argument forces conversion into a 5 character wide string
6735 HH:MM."
6736 (save-match-data
6737 (when
6738 (and
6739 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6740 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6741 (not (eq (get-text-property 1 'face s) 'org-link)))
6742 (let* ((h (string-to-number (match-string 1 s)))
6743 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6744 (ampm (when (match-end 4) (downcase (match-string 4 s))))
6745 (am-p (equal ampm "am"))
6746 (h1 (cond ((not ampm) h)
6747 ((= h 12) (if am-p 0 12))
6748 (t (+ h (if am-p 0 12)))))
6749 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6750 (mod h1 24) h1))
6751 (t0 (+ (* 100 h2) m))
6752 (t1 (concat (if (>= h1 24) "+" " ")
6753 (if (and org-agenda-time-leading-zero
6754 (< t0 1000)) "0" "")
6755 (if (< t0 100) "0" "")
6756 (if (< t0 10) "0" "")
6757 (int-to-string t0))))
6758 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6760 (defvar org-agenda-before-sorting-filter-function nil
6761 "Function to be applied to agenda items prior to sorting.
6762 Prior to sorting also means just before they are inserted into the agenda.
6764 To aid sorting, you may revisit the original entries and add more text
6765 properties which will later be used by the sorting functions.
6767 The function should take a string argument, an agenda line.
6768 It has access to the text properties in that line, which contain among
6769 other things, the property `org-hd-marker' that points to the entry
6770 where the line comes from. Note that not all lines going into the agenda
6771 have this property, only most.
6773 The function should return the modified string. It is probably best
6774 to ONLY change text properties.
6776 You can also use this function as a filter, by returning nil for lines
6777 you don't want to have in the agenda at all. For this application, you
6778 could bind the variable in the options section of a custom command.")
6780 (defun org-agenda-finalize-entries (list &optional type)
6781 "Sort, limit and concatenate the LIST of agenda items.
6782 The optional argument TYPE tells the agenda type."
6783 (let ((max-effort (cond ((listp org-agenda-max-effort)
6784 (cdr (assoc type org-agenda-max-effort)))
6785 (t org-agenda-max-effort)))
6786 (max-todo (cond ((listp org-agenda-max-todos)
6787 (cdr (assoc type org-agenda-max-todos)))
6788 (t org-agenda-max-todos)))
6789 (max-tags (cond ((listp org-agenda-max-tags)
6790 (cdr (assoc type org-agenda-max-tags)))
6791 (t org-agenda-max-tags)))
6792 (max-entries (cond ((listp org-agenda-max-entries)
6793 (cdr (assoc type org-agenda-max-entries)))
6794 (t org-agenda-max-entries))))
6795 (when org-agenda-before-sorting-filter-function
6796 (setq list
6797 (delq nil
6798 (mapcar
6799 org-agenda-before-sorting-filter-function list))))
6800 (setq list (mapcar 'org-agenda-highlight-todo list)
6801 list (mapcar 'identity (sort list 'org-entries-lessp)))
6802 (when max-effort
6803 (setq list (org-agenda-limit-entries
6804 list 'effort-minutes max-effort
6805 (lambda (e) (or e (if org-sort-agenda-noeffort-is-high
6806 32767 -1))))))
6807 (when max-todo
6808 (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
6809 (when max-tags
6810 (setq list (org-agenda-limit-entries list 'tags max-tags)))
6811 (when max-entries
6812 (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
6813 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
6814 (setq list (mapcar #'org-agenda--mark-blocked-entry list)))
6815 (mapconcat 'identity list "\n")))
6817 (defun org-agenda-limit-entries (list prop limit &optional fn)
6818 "Limit the number of agenda entries."
6819 (let ((include (and limit (< limit 0))))
6820 (if limit
6821 (let ((fun (or fn (lambda (p) (when p 1))))
6822 (lim 0))
6823 (delq nil
6824 (mapcar
6825 (lambda (e)
6826 (let ((pval (funcall
6827 fun (get-text-property (1- (length e))
6828 prop e))))
6829 (when pval (setq lim (+ lim pval)))
6830 (cond ((and pval (<= lim (abs limit))) e)
6831 ((and include (not pval)) e))))
6832 list)))
6833 list)))
6835 (defun org-agenda-limit-interactively (remove)
6836 "In agenda, interactively limit entries to various maximums."
6837 (interactive "P")
6838 (if remove
6839 (progn (setq org-agenda-max-entries nil
6840 org-agenda-max-todos nil
6841 org-agenda-max-tags nil
6842 org-agenda-max-effort nil)
6843 (org-agenda-redo))
6844 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6845 (msg (cond ((= max ?E) "How many minutes? ")
6846 ((= max ?e) "How many entries? ")
6847 ((= max ?t) "How many TODO entries? ")
6848 ((= max ?T) "How many tagged entries? ")
6849 (t (user-error "Wrong input"))))
6850 (num (string-to-number (read-from-minibuffer msg))))
6851 (cond ((equal max ?e)
6852 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6853 ((equal max ?t)
6854 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6855 ((equal max ?T)
6856 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6857 ((equal max ?E)
6858 (let ((org-agenda-max-effort num)) (org-agenda-redo))))))
6859 (org-agenda-fit-window-to-buffer))
6861 (defun org-agenda-highlight-todo (x)
6862 (let ((org-done-keywords org-done-keywords-for-agenda)
6863 (case-fold-search nil)
6865 (if (eq x 'line)
6866 (save-excursion
6867 (beginning-of-line 1)
6868 (setq re (org-get-at-bol 'org-todo-regexp))
6869 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6870 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6871 (add-text-properties (match-beginning 0) (match-end 1)
6872 (list 'face (org-get-todo-face 1)))
6873 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6874 (delete-region (match-beginning 1) (1- (match-end 0)))
6875 (goto-char (match-beginning 1))
6876 (insert (format org-agenda-todo-keyword-format s)))))
6877 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6878 (setq re (get-text-property 0 'org-todo-regexp x))
6879 (when (and re
6880 ;; Test `pl' because if there's no heading content,
6881 ;; there's no point matching to highlight. Note
6882 ;; that if we didn't test `pl' first, and there
6883 ;; happened to be no keyword from `org-todo-regexp'
6884 ;; on this heading line, then the `equal' comparison
6885 ;; afterwards would spuriously succeed in the case
6886 ;; where `pl' is nil -- causing an args-out-of-range
6887 ;; error when we try to add text properties to text
6888 ;; that isn't there.
6890 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6891 x pl) pl))
6892 (add-text-properties
6893 (or (match-end 1) (match-end 0)) (match-end 0)
6894 (list 'face (org-get-todo-face (match-string 2 x)))
6896 (when (match-end 1)
6897 (setq x
6898 (concat
6899 (substring x 0 (match-end 1))
6900 (format org-agenda-todo-keyword-format
6901 (match-string 2 x))
6902 ;; Remove `display' property as the icon could leak
6903 ;; on the white space.
6904 (org-add-props " " (org-plist-delete (text-properties-at 0 x)
6905 'display))
6906 (substring x (match-end 3)))))))
6907 x)))
6909 (defsubst org-cmp-values (a b property)
6910 "Compare the numeric value of text PROPERTY for string A and B."
6911 (let ((pa (or (get-text-property (1- (length a)) property a) 0))
6912 (pb (or (get-text-property (1- (length b)) property b) 0)))
6913 (cond ((> pa pb) +1)
6914 ((< pa pb) -1))))
6916 (defsubst org-cmp-effort (a b)
6917 "Compare the effort values of string A and B."
6918 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6919 ;; `effort-minutes' property is not directly accessible from
6920 ;; the strings, but is stored as a property in `txt'.
6921 (ea (or (get-text-property
6922 0 'effort-minutes (get-text-property 0 'txt a))
6923 def))
6924 (eb (or (get-text-property
6925 0 'effort-minutes (get-text-property 0 'txt b))
6926 def)))
6927 (cond ((> ea eb) +1)
6928 ((< ea eb) -1))))
6930 (defsubst org-cmp-category (a b)
6931 "Compare the string values of categories of strings A and B."
6932 (let ((ca (or (get-text-property (1- (length a)) 'org-category a) ""))
6933 (cb (or (get-text-property (1- (length b)) 'org-category b) "")))
6934 (cond ((string-lessp ca cb) -1)
6935 ((string-lessp cb ca) +1))))
6937 (defsubst org-cmp-todo-state (a b)
6938 "Compare the todo states of strings A and B."
6939 (let* ((ma (or (get-text-property 1 'org-marker a)
6940 (get-text-property 1 'org-hd-marker a)))
6941 (mb (or (get-text-property 1 'org-marker b)
6942 (get-text-property 1 'org-hd-marker b)))
6943 (fa (and ma (marker-buffer ma)))
6944 (fb (and mb (marker-buffer mb)))
6945 (todo-kwds
6946 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6947 (and fb (with-current-buffer fb org-todo-keywords-1))))
6948 (ta (or (get-text-property 1 'todo-state a) ""))
6949 (tb (or (get-text-property 1 'todo-state b) ""))
6950 (la (- (length (member ta todo-kwds))))
6951 (lb (- (length (member tb todo-kwds))))
6952 (donepa (member ta org-done-keywords-for-agenda))
6953 (donepb (member tb org-done-keywords-for-agenda)))
6954 (cond ((and donepa (not donepb)) -1)
6955 ((and (not donepa) donepb) +1)
6956 ((< la lb) -1)
6957 ((< lb la) +1))))
6959 (defsubst org-cmp-alpha (a b)
6960 "Compare the headlines, alphabetically."
6961 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6962 (plb (text-property-any 0 (length b) 'org-heading t b))
6963 (ta (and pla (substring a pla)))
6964 (tb (and plb (substring b plb)))
6965 (case-fold-search nil))
6966 (when pla
6967 (when (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6968 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6969 (setq ta (substring ta (match-end 0))))
6970 (setq ta (downcase ta)))
6971 (when plb
6972 (when (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6973 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6974 (setq tb (substring tb (match-end 0))))
6975 (setq tb (downcase tb)))
6976 (cond ((not ta) +1)
6977 ((not tb) -1)
6978 ((string-lessp ta tb) -1)
6979 ((string-lessp tb ta) +1))))
6981 (defsubst org-cmp-tag (a b)
6982 "Compare the string values of the first tags of A and B."
6983 (let ((ta (car (last (get-text-property 1 'tags a))))
6984 (tb (car (last (get-text-property 1 'tags b)))))
6985 (cond ((not ta) +1)
6986 ((not tb) -1)
6987 ((string-lessp ta tb) -1)
6988 ((string-lessp tb ta) +1))))
6990 (defsubst org-cmp-time (a b)
6991 "Compare the time-of-day values of strings A and B."
6992 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6993 (ta (or (get-text-property 1 'time-of-day a) def))
6994 (tb (or (get-text-property 1 'time-of-day b) def)))
6995 (cond ((< ta tb) -1)
6996 ((< tb ta) +1))))
6998 (defsubst org-cmp-ts (a b type)
6999 "Compare the timestamps values of entries A and B.
7000 When TYPE is \"scheduled\", \"deadline\", \"timestamp\" or
7001 \"timestamp_ia\", compare within each of these type. When TYPE
7002 is the empty string, compare all timestamps without respect of
7003 their type."
7004 (let* ((def (if org-sort-agenda-notime-is-late most-positive-fixnum -1))
7005 (ta (or (and (string-match type (or (get-text-property 1 'type a) ""))
7006 (get-text-property 1 'ts-date a))
7007 def))
7008 (tb (or (and (string-match type (or (get-text-property 1 'type b) ""))
7009 (get-text-property 1 'ts-date b))
7010 def)))
7011 (cond ((< ta tb) -1)
7012 ((< tb ta) +1))))
7014 (defsubst org-cmp-habit-p (a b)
7015 "Compare the todo states of strings A and B."
7016 (let ((ha (get-text-property 1 'org-habit-p a))
7017 (hb (get-text-property 1 'org-habit-p b)))
7018 (cond ((and ha (not hb)) -1)
7019 ((and (not ha) hb) +1))))
7021 (defun org-entries-lessp (a b)
7022 "Predicate for sorting agenda entries."
7023 ;; The following variables will be used when the form is evaluated.
7024 ;; So even though the compiler complains, keep them.
7025 (let* ((ss org-agenda-sorting-strategy-selected)
7026 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
7027 (org-cmp-ts a b "")))
7028 (timestamp-down (if timestamp-up (- timestamp-up) nil))
7029 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
7030 (org-cmp-ts a b "scheduled")))
7031 (scheduled-down (if scheduled-up (- scheduled-up) nil))
7032 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
7033 (org-cmp-ts a b "deadline")))
7034 (deadline-down (if deadline-up (- deadline-up) nil))
7035 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
7036 (org-cmp-ts a b "timestamp_ia")))
7037 (tsia-down (if tsia-up (- tsia-up) nil))
7038 (ts-up (and (org-em 'ts-up 'ts-down ss)
7039 (org-cmp-ts a b "timestamp")))
7040 (ts-down (if ts-up (- ts-up) nil))
7041 (time-up (and (org-em 'time-up 'time-down ss)
7042 (org-cmp-time a b)))
7043 (time-down (if time-up (- time-up) nil))
7044 (stats-up (and (org-em 'stats-up 'stats-down ss)
7045 (org-cmp-values a b 'org-stats)))
7046 (stats-down (if stats-up (- stats-up) nil))
7047 (priority-up (and (org-em 'priority-up 'priority-down ss)
7048 (org-cmp-values a b 'priority)))
7049 (priority-down (if priority-up (- priority-up) nil))
7050 (effort-up (and (org-em 'effort-up 'effort-down ss)
7051 (org-cmp-effort a b)))
7052 (effort-down (if effort-up (- effort-up) nil))
7053 (category-up (and (or (org-em 'category-up 'category-down ss)
7054 (memq 'category-keep ss))
7055 (org-cmp-category a b)))
7056 (category-down (if category-up (- category-up) nil))
7057 (category-keep (if category-up +1 nil))
7058 (tag-up (and (org-em 'tag-up 'tag-down ss)
7059 (org-cmp-tag a b)))
7060 (tag-down (if tag-up (- tag-up) nil))
7061 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
7062 (org-cmp-todo-state a b)))
7063 (todo-state-down (if todo-state-up (- todo-state-up) nil))
7064 (habit-up (and (org-em 'habit-up 'habit-down ss)
7065 (org-cmp-habit-p a b)))
7066 (habit-down (if habit-up (- habit-up) nil))
7067 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
7068 (org-cmp-alpha a b)))
7069 (alpha-down (if alpha-up (- alpha-up) nil))
7070 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
7071 user-defined-up user-defined-down)
7072 (when (and need-user-cmp org-agenda-cmp-user-defined
7073 (functionp org-agenda-cmp-user-defined))
7074 (setq user-defined-up
7075 (funcall org-agenda-cmp-user-defined a b)
7076 user-defined-down (if user-defined-up (- user-defined-up) nil)))
7077 (cdr (assoc
7078 (eval (cons 'or org-agenda-sorting-strategy-selected))
7079 '((-1 . t) (1 . nil) (nil . nil))))))
7081 ;;; Agenda restriction lock
7083 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
7084 "Overlay to mark the headline to which agenda commands are restricted.")
7085 (overlay-put org-agenda-restriction-lock-overlay
7086 'face 'org-agenda-restriction-lock)
7087 (overlay-put org-agenda-restriction-lock-overlay
7088 'help-echo "Agendas are currently limited to this subtree.")
7089 (delete-overlay org-agenda-restriction-lock-overlay)
7091 (defun org-agenda-set-restriction-lock-from-agenda (arg)
7092 "Set the restriction lock to the agenda item at point from within the agenda.
7093 When called with a `\\[universal-argument]' prefix, restrict to
7094 the file which contains the item.
7095 Argument ARG is the prefix argument."
7096 (interactive "P")
7097 (unless (derived-mode-p 'org-agenda-mode)
7098 (user-error "Not in an Org agenda buffer"))
7099 (let* ((marker (or (org-get-at-bol 'org-marker)
7100 (org-agenda-error)))
7101 (buffer (marker-buffer marker))
7102 (pos (marker-position marker)))
7103 (with-current-buffer buffer
7104 (goto-char pos)
7105 (org-agenda-set-restriction-lock arg))))
7107 ;;;###autoload
7108 (defun org-agenda-set-restriction-lock (&optional type)
7109 "Set restriction lock for agenda to current subtree or file.
7110 When in a restricted subtree, remove it.
7112 The restriction will span over the entire file if TYPE is `file',
7113 or if type is '(4), or if the cursor is before the first headline
7114 in the file. Otherwise, only apply the restriction to the current
7115 subtree."
7116 (interactive "P")
7117 (if (and org-agenda-overriding-restriction
7118 (member org-agenda-restriction-lock-overlay
7119 (overlays-at (point)))
7120 (equal (overlay-start org-agenda-restriction-lock-overlay)
7121 (point)))
7122 (org-agenda-remove-restriction-lock 'noupdate)
7123 (org-agenda-remove-restriction-lock 'noupdate)
7124 (and (equal type '(4)) (setq type 'file))
7125 (setq type (cond
7126 (type type)
7127 ((org-at-heading-p) 'subtree)
7128 ((condition-case nil (org-back-to-heading t) (error nil))
7129 'subtree)
7130 (t 'file)))
7131 (if (eq type 'subtree)
7132 (progn
7133 (setq org-agenda-restrict (current-buffer))
7134 (setq org-agenda-overriding-restriction 'subtree)
7135 (put 'org-agenda-files 'org-restrict
7136 (list (buffer-file-name (buffer-base-buffer))))
7137 (org-back-to-heading t)
7138 (move-overlay org-agenda-restriction-lock-overlay
7139 (point)
7140 (if org-agenda-restriction-lock-highlight-subtree
7141 (save-excursion (org-end-of-subtree t t) (point))
7142 (point-at-eol)))
7143 (move-marker org-agenda-restrict-begin (point))
7144 (move-marker org-agenda-restrict-end
7145 (save-excursion (org-end-of-subtree t t)))
7146 (message "Locking agenda restriction to subtree"))
7147 (put 'org-agenda-files 'org-restrict
7148 (list (buffer-file-name (buffer-base-buffer))))
7149 (setq org-agenda-restrict nil)
7150 (setq org-agenda-overriding-restriction 'file)
7151 (move-marker org-agenda-restrict-begin nil)
7152 (move-marker org-agenda-restrict-end nil)
7153 (message "Locking agenda restriction to file"))
7154 (setq current-prefix-arg nil))
7155 (org-agenda-maybe-redo))
7157 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7158 "Remove agenda restriction lock."
7159 (interactive "P")
7160 (if (not org-agenda-restrict)
7161 (message "No agenda restriction to remove.")
7162 (delete-overlay org-agenda-restriction-lock-overlay)
7163 (delete-overlay org-speedbar-restriction-lock-overlay)
7164 (setq org-agenda-overriding-restriction nil)
7165 (setq org-agenda-restrict nil)
7166 (put 'org-agenda-files 'org-restrict nil)
7167 (move-marker org-agenda-restrict-begin nil)
7168 (move-marker org-agenda-restrict-end nil)
7169 (setq current-prefix-arg nil)
7170 (message "Agenda restriction lock removed")
7171 (or noupdate (org-agenda-maybe-redo))))
7173 (defun org-agenda-maybe-redo ()
7174 "If there is any window showing the agenda view, update it."
7175 (let ((w (get-buffer-window (or org-agenda-this-buffer-name
7176 org-agenda-buffer-name)
7178 (w0 (selected-window)))
7179 (when w
7180 (select-window w)
7181 (org-agenda-redo)
7182 (select-window w0)
7183 (if org-agenda-overriding-restriction
7184 (message "Agenda view shifted to new %s restriction"
7185 org-agenda-overriding-restriction)
7186 (message "Agenda restriction lock removed")))))
7188 ;;; Agenda commands
7190 (defun org-agenda-check-type (error &rest types)
7191 "Check if agenda buffer is of allowed type.
7192 If ERROR is non-nil, throw an error, otherwise just return nil.
7193 Allowed types are `agenda' `todo' `tags' `search'."
7194 (cond ((not org-agenda-type)
7195 (error "No Org agenda currently displayed"))
7196 ((memq org-agenda-type types) t)
7197 (error
7198 (error "Not allowed in %s-type agenda buffers" org-agenda-type))
7199 (t nil)))
7201 (defun org-agenda-Quit ()
7202 "Exit the agenda, killing the agenda buffer.
7203 Like `org-agenda-quit', but kill the buffer even when
7204 `org-agenda-sticky' is non-nil."
7205 (interactive)
7206 (org-agenda--quit))
7208 (defun org-agenda-quit ()
7209 "Exit the agenda.
7211 When `org-agenda-sticky' is non-nil, bury the agenda buffer
7212 instead of killing it.
7214 When `org-agenda-restore-windows-after-quit' is non-nil, restore
7215 the pre-agenda window configuration.
7217 When column view is active, exit column view instead of the
7218 agenda."
7219 (interactive)
7220 (org-agenda--quit org-agenda-sticky))
7222 (defun org-agenda--quit (&optional bury)
7223 (if org-agenda-columns-active
7224 (org-columns-quit)
7225 (let ((wconf org-agenda-pre-window-conf)
7226 (buf (current-buffer))
7227 (org-agenda-last-indirect-window
7228 (and (eq org-indirect-buffer-display 'other-window)
7229 org-agenda-last-indirect-buffer
7230 (get-buffer-window org-agenda-last-indirect-buffer))))
7231 (cond
7232 ((eq org-agenda-window-setup 'other-frame)
7233 (delete-frame))
7234 ((and org-agenda-restore-windows-after-quit
7235 wconf)
7236 ;; Maybe restore the pre-agenda window configuration. Reset
7237 ;; `org-agenda-pre-window-conf' before running
7238 ;; `set-window-configuration', which loses the current buffer.
7239 (setq org-agenda-pre-window-conf nil)
7240 (set-window-configuration wconf))
7242 (when org-agenda-last-indirect-window
7243 (delete-window org-agenda-last-indirect-window))
7244 (and (not (eq org-agenda-window-setup 'current-window))
7245 (not (one-window-p))
7246 (delete-window))))
7247 (if bury
7248 ;; Set the agenda buffer as the current buffer instead of
7249 ;; passing it as an argument to `bury-buffer' so that
7250 ;; `bury-buffer' removes it from the window.
7251 (with-current-buffer buf
7252 (bury-buffer))
7253 (kill-buffer buf)
7254 (setq org-agenda-archives-mode nil
7255 org-agenda-buffer nil)))))
7257 (defun org-agenda-exit ()
7258 "Exit the agenda, killing Org buffers loaded by the agenda.
7259 Like `org-agenda-Quit', but kill any buffers that were created by
7260 the agenda. Org buffers visited directly by the user will not be
7261 touched. Also, exit the agenda even if it is in column view."
7262 (interactive)
7263 (when org-agenda-columns-active
7264 (org-columns-quit))
7265 (org-release-buffers org-agenda-new-buffers)
7266 (setq org-agenda-new-buffers nil)
7267 (org-agenda-Quit))
7269 (defun org-agenda-kill-all-agenda-buffers ()
7270 "Kill all buffers in `org-agenda-mode'.
7271 This is used when toggling sticky agendas."
7272 (interactive)
7273 (let (blist)
7274 (dolist (buf (buffer-list))
7275 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7276 (push buf blist)))
7277 (mapc 'kill-buffer blist)))
7279 (defun org-agenda-execute (arg)
7280 "Execute another agenda command, keeping same window.
7281 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7282 in the agenda."
7283 (interactive "P")
7284 (let ((org-agenda-window-setup 'current-window))
7285 (org-agenda arg)))
7287 (defun org-agenda-redo (&optional all)
7288 "Rebuild possibly ALL agenda view(s) in the current buffer."
7289 (interactive "P")
7290 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7291 (cpa (unless (eq all t) current-prefix-arg))
7292 (org-agenda-doing-sticky-redo org-agenda-sticky)
7293 (org-agenda-sticky nil)
7294 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7295 org-agenda-buffer-name))
7296 (org-agenda-keep-modes t)
7297 (tag-filter org-agenda-tag-filter)
7298 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7299 (top-hl-filter org-agenda-top-headline-filter)
7300 (cat-filter org-agenda-category-filter)
7301 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7302 (re-filter org-agenda-regexp-filter)
7303 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7304 (effort-filter org-agenda-effort-filter)
7305 (effort-preset (get 'org-agenda-effort-filter :preset-filter))
7306 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7307 (cols org-agenda-columns-active)
7308 (line (org-current-line))
7309 (window-line (- line (org-current-line (window-start))))
7310 (lprops (get 'org-agenda-redo-command 'org-lprops))
7311 (redo-cmd (get-text-property p 'org-redo-cmd))
7312 (last-args (get-text-property p 'org-last-args))
7313 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7314 (org-agenda-overriding-cmd-arguments
7315 (unless (eq all t)
7316 (cond ((listp last-args)
7317 (cons (or cpa (car last-args)) (cdr last-args)))
7318 ((stringp last-args)
7319 last-args))))
7320 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7321 (put 'org-agenda-tag-filter :preset-filter nil)
7322 (put 'org-agenda-category-filter :preset-filter nil)
7323 (put 'org-agenda-regexp-filter :preset-filter nil)
7324 (put 'org-agenda-effort-filter :preset-filter nil)
7325 (and cols (org-columns-quit))
7326 (message "Rebuilding agenda buffer...")
7327 (if series-redo-cmd
7328 (eval series-redo-cmd)
7329 (org-let lprops redo-cmd))
7330 (setq org-agenda-undo-list nil
7331 org-agenda-pending-undo-list nil
7332 org-agenda-tag-filter tag-filter
7333 org-agenda-category-filter cat-filter
7334 org-agenda-regexp-filter re-filter
7335 org-agenda-effort-filter effort-filter
7336 org-agenda-top-headline-filter top-hl-filter)
7337 (message "Rebuilding agenda buffer...done")
7338 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7339 (put 'org-agenda-category-filter :preset-filter cat-preset)
7340 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7341 (put 'org-agenda-effort-filter :preset-filter effort-preset)
7342 (let ((tag (or tag-filter tag-preset))
7343 (cat (or cat-filter cat-preset))
7344 (effort (or effort-filter effort-preset))
7345 (re (or re-filter re-preset)))
7346 (when tag (org-agenda-filter-apply tag 'tag t))
7347 (when cat (org-agenda-filter-apply cat 'category))
7348 (when effort (org-agenda-filter-apply effort 'effort))
7349 (when re (org-agenda-filter-apply re 'regexp)))
7350 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7351 (and cols (called-interactively-p 'any) (org-agenda-columns))
7352 (org-goto-line line)
7353 (recenter window-line)))
7355 (defun org-agenda-redo-all (&optional exhaustive)
7356 "Rebuild all agenda views in the current buffer.
7357 With a prefix argument, do so in all agenda buffers."
7358 (interactive "P")
7359 (if exhaustive
7360 (dolist (buffer (buffer-list))
7361 (with-current-buffer buffer
7362 (when (derived-mode-p 'org-agenda-mode)
7363 (org-agenda-redo t))))
7364 (org-agenda-redo t)))
7366 (defvar org-global-tags-completion-table nil)
7367 (defvar org-agenda-filter-form nil)
7368 (defvar org-agenda-filtered-by-category nil)
7370 (defun org-agenda-filter-by-category (strip)
7371 "Filter lines in the agenda buffer that have a specific category.
7372 The category is that of the current line.
7373 Without prefix argument, keep only the lines of that category.
7374 With a prefix argument, exclude the lines of that category.
7376 (interactive "P")
7377 (if (and org-agenda-filtered-by-category
7378 org-agenda-category-filter)
7379 (org-agenda-filter-show-all-cat)
7380 (let ((cat (org-no-properties (org-get-at-eol 'org-category 1))))
7381 (cond
7382 ((and cat strip)
7383 (org-agenda-filter-apply
7384 (push (concat "-" cat) org-agenda-category-filter) 'category))
7385 (cat
7386 (org-agenda-filter-apply
7387 (setq org-agenda-category-filter
7388 (list (concat "+" cat))) 'category))
7389 (t (error "No category at point"))))))
7391 (defun org-find-top-headline (&optional pos)
7392 "Find the topmost parent headline and return it.
7393 POS when non-nil is the marker or buffer position to start the
7394 search from."
7395 (save-excursion
7396 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
7397 (when pos (goto-char pos))
7398 ;; Skip up to the topmost parent.
7399 (while (org-up-heading-safe))
7400 (ignore-errors (nth 4 (org-heading-components))))))
7402 (defvar org-agenda-filtered-by-top-headline nil)
7403 (defun org-agenda-filter-by-top-headline (strip)
7404 "Keep only those lines that are descendants from the same top headline.
7405 The top headline is that of the current line."
7406 (interactive "P")
7407 (if org-agenda-filtered-by-top-headline
7408 (progn
7409 (setq org-agenda-filtered-by-top-headline nil
7410 org-agenda-top-headline-filter nil)
7411 (org-agenda-filter-show-all-top-filter))
7412 (let ((toph (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7413 (if toph (org-agenda-filter-top-headline-apply toph strip)
7414 (error "No top-level headline at point")))))
7416 (defvar org-agenda-regexp-filter nil)
7417 (defun org-agenda-filter-by-regexp (strip)
7418 "Filter agenda entries by regular expressions.
7420 With one prefix argument, filter out entries matching the regexp.
7421 If there is already a regexp filter, remove it unless called with
7422 two prefix arguments."
7423 (interactive "P")
7424 (cond
7425 ((and org-agenda-regexp-filter (not (equal strip '(16))))
7426 (org-agenda-filter-show-all-re)
7427 (message "Regexp filter removed"))
7428 (t (let ((flt (concat (if (equal strip '(4)) "-" "+")
7429 (read-from-minibuffer
7430 (if (equal strip '(4))
7431 "Filter out entries matching regexp: "
7432 "Narrow to entries matching regexp: ")))))
7433 (push flt org-agenda-regexp-filter)
7434 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp)))))
7436 (defvar org-agenda-effort-filter nil)
7437 (defun org-agenda-filter-by-effort (strip)
7438 "Filter agenda entries by effort.
7439 With no prefix argument, keep entries matching the effort condition.
7440 With one prefix argument, filter out entries matching the condition.
7441 With two prefix arguments, remove the effort filters."
7442 (interactive "P")
7443 (cond
7444 ((member strip '(nil 4))
7445 (let* ((efforts (split-string
7446 (or (cdr (assoc (concat org-effort-property "_ALL")
7447 org-global-properties))
7448 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00")))
7449 ;; XXX: the following handles only up to 10 different
7450 ;; effort values.
7451 (allowed-keys (if (null efforts) nil
7452 (mapcar (lambda (n) (mod n 10)) ;turn 10 into 0
7453 (number-sequence 1 (length efforts)))))
7454 (op nil))
7455 (while (not (memq op '(?< ?> ?=)))
7456 (setq op (read-char-exclusive "Effort operator? (> = or <)")))
7457 ;; Select appropriate duration. Ignore non-digit characters.
7458 (let ((prompt
7459 (apply #'format
7460 (concat "Effort %c "
7461 (mapconcat (lambda (s) (concat "[%d]" s))
7462 efforts
7463 " "))
7464 op allowed-keys))
7465 (eff -1))
7466 (while (not (memq eff allowed-keys))
7467 (message prompt)
7468 (setq eff (- (read-char-exclusive) 48)))
7469 (setq org-agenda-effort-filter
7470 (list (concat (if strip "-" "+")
7471 (char-to-string op)
7472 ;; Numbering is 1 2 3 ... 9 0, but we want
7473 ;; 0 1 2 ... 8 9.
7474 (nth (mod (1- eff) 10) efforts)))))
7475 (org-agenda-filter-apply org-agenda-effort-filter 'effort)))
7476 (t (org-agenda-filter-show-all-effort)
7477 (message "Effort filter removed"))))
7479 (defun org-agenda-filter-remove-all ()
7480 "Remove all filters from the current agenda buffer."
7481 (interactive)
7482 (when org-agenda-tag-filter
7483 (org-agenda-filter-show-all-tag))
7484 (when org-agenda-category-filter
7485 (org-agenda-filter-show-all-cat))
7486 (when org-agenda-regexp-filter
7487 (org-agenda-filter-show-all-re))
7488 (when org-agenda-top-headline-filter
7489 (org-agenda-filter-show-all-top-filter))
7490 (when org-agenda-effort-filter
7491 (org-agenda-filter-show-all-effort))
7492 (org-agenda-finalize))
7494 (defun org-agenda-filter-by-tag (arg &optional char exclude)
7495 "Keep only those lines in the agenda buffer that have a specific tag.
7497 The tag is selected with its fast selection letter, as configured.
7499 With a `\\[universal-argument]' prefix, exclude the agenda search.
7501 With a `\\[universal-argument] \\[universal-argument]' prefix, filter the literal tag, \
7502 i.e. don't
7503 filter on all its group members.
7505 A lisp caller can specify CHAR. EXCLUDE means that the new tag
7506 should be used to exclude the search - the interactive user can
7507 also press `-' or `+' to switch between filtering and excluding."
7508 (interactive "P")
7509 (let* ((alist org-tag-alist-for-agenda)
7510 (tag-chars (mapconcat
7511 (lambda (x) (if (and (not (symbolp (car x)))
7512 (cdr x))
7513 (char-to-string (cdr x))
7514 ""))
7515 org-tag-alist-for-agenda ""))
7516 (valid-char-list (append '(?\t ?\r ?/ ?. ?\s ?q)
7517 (string-to-list tag-chars)))
7518 (exclude (or exclude (equal arg '(4))))
7519 (expand (not (equal arg '(16))))
7520 (inhibit-read-only t)
7521 (current org-agenda-tag-filter)
7522 a n tag)
7523 (unless char
7524 (while (not (memq char valid-char-list))
7525 (org-unlogged-message
7526 "%s by tag [%s ]:tag-char, [TAB]:tag, %s[/]:off, [+/-]:filter/exclude%s, [q]:quit"
7527 (if exclude "Exclude" "Filter")
7528 tag-chars
7529 (if org-agenda-auto-exclude-function "[RET], " "")
7530 (if expand "" ", no grouptag expand"))
7531 (setq char (read-char-exclusive))
7532 ;; Excluding or filtering down
7533 (cond ((eq char ?-) (setq exclude t))
7534 ((eq char ?+) (setq exclude nil)))))
7535 (when (eq char ?\t)
7536 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7537 (setq-local org-global-tags-completion-table
7538 (org-global-tags-completion-table)))
7539 (let ((completion-ignore-case t))
7540 (setq tag (completing-read
7541 "Tag: " org-global-tags-completion-table nil t))))
7542 (cond
7543 ((eq char ?\r)
7544 (org-agenda-filter-show-all-tag)
7545 (when org-agenda-auto-exclude-function
7546 (setq org-agenda-tag-filter nil)
7547 (dolist (tag (org-agenda-get-represented-tags))
7548 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7549 (when modifier
7550 (push modifier org-agenda-tag-filter))))
7551 (unless (null org-agenda-tag-filter)
7552 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))))
7553 ((eq char ?/)
7554 (org-agenda-filter-show-all-tag)
7555 (when (get 'org-agenda-tag-filter :preset-filter)
7556 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand)))
7557 ((eq char ?.)
7558 (setq org-agenda-tag-filter
7559 (mapcar (lambda(tag) (concat "+" tag))
7560 (org-get-at-bol 'tags)))
7561 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7562 ((eq char ?q)) ;If q, abort (even if there is a q-key for a tag...)
7563 ((or (eq char ?\s)
7564 (setq a (rassoc char alist))
7565 (and tag (setq a (cons tag nil))))
7566 (org-agenda-filter-show-all-tag)
7567 (setq tag (car a))
7568 (setq org-agenda-tag-filter
7569 (cons (concat (if exclude "-" "+") tag)
7570 current))
7571 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7572 (t (error "Invalid tag selection character %c" char)))))
7574 (defun org-agenda-get-represented-tags ()
7575 "Get a list of all tags currently represented in the agenda."
7576 (let (p tags)
7577 (save-excursion
7578 (goto-char (point-min))
7579 (while (setq p (next-single-property-change (point) 'tags))
7580 (goto-char p)
7581 (mapc (lambda (x) (add-to-list 'tags x))
7582 (get-text-property (point) 'tags))))
7583 tags))
7586 (defun org-agenda-filter-make-matcher (filter type &optional expand)
7587 "Create the form that tests a line for agenda filter. Optional
7588 argument EXPAND can be used for the TYPE tag and will expand the
7589 tags in the FILTER if any of the tags in FILTER are grouptags."
7590 (let (f f1)
7591 (cond
7592 ;; Tag filter
7593 ((eq type 'tag)
7594 (setq filter
7595 (delete-dups
7596 (append (get 'org-agenda-tag-filter :preset-filter)
7597 filter)))
7598 (dolist (x filter)
7599 (let ((op (string-to-char x)))
7600 (if expand (setq x (org-agenda-filter-expand-tags (list x) t))
7601 (setq x (list x)))
7602 (setq f1 (org-agenda-filter-make-matcher-tag-exp x op))
7603 (push f1 f))))
7604 ;; Category filter
7605 ((eq type 'category)
7606 (setq filter
7607 (delete-dups
7608 (append (get 'org-agenda-category-filter :preset-filter)
7609 filter)))
7610 (dolist (x filter)
7611 (if (equal "-" (substring x 0 1))
7612 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7613 (setq f1 (list 'equal (substring x 1) 'cat)))
7614 (push f1 f)))
7615 ;; Regexp filter
7616 ((eq type 'regexp)
7617 (setq filter
7618 (delete-dups
7619 (append (get 'org-agenda-regexp-filter :preset-filter)
7620 filter)))
7621 (dolist (x filter)
7622 (if (equal "-" (substring x 0 1))
7623 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7624 (setq f1 (list 'string-match (substring x 1) 'txt)))
7625 (push f1 f)))
7626 ;; Effort filter
7627 ((eq type 'effort)
7628 (setq filter
7629 (delete-dups
7630 (append (get 'org-agenda-effort-filter :preset-filter)
7631 filter)))
7632 (dolist (x filter)
7633 (push (org-agenda-filter-effort-form x) f))))
7634 (cons 'and (nreverse f))))
7636 (defun org-agenda-filter-make-matcher-tag-exp (tags op)
7637 "Return a form associated to tag-expression TAGS.
7638 Build a form testing a line for agenda filter for
7639 tag-expressions. OP is an operator of type CHAR that allows the
7640 function to set the right switches in the returned form."
7641 (let (form)
7642 ;; Any of the expressions can match if OP is +, all must match if
7643 ;; the operator is -.
7644 (dolist (x tags (cons (if (eq op ?-) 'and 'or) form))
7645 (let* ((tag (substring x 1))
7646 (f (cond
7647 ((string= "" tag) '(not tags))
7648 ((and (string-match-p "\\`{" tag) (string-match-p "}\\'" tag))
7649 ;; TAG is a regexp.
7650 (list 'org-match-any-p (substring tag 1 -1) 'tags))
7651 (t (list 'member (downcase tag) 'tags)))))
7652 (push (if (eq op ?-) (list 'not f) f) form)))))
7654 (defun org-agenda-filter-effort-form (e)
7655 "Return the form to compare the effort of the current line with what E says.
7656 E looks like \"+<2:25\"."
7657 (let (op)
7658 (setq e (substring e 1))
7659 (setq op (string-to-char e) e (substring e 1))
7660 (setq op (cond ((equal op ?<) '<=)
7661 ((equal op ?>) '>=)
7662 ((equal op ??) op)
7663 (t '=)))
7664 (list 'org-agenda-compare-effort (list 'quote op)
7665 (org-duration-to-minutes e))))
7667 (defun org-agenda-compare-effort (op value)
7668 "Compare the effort of the current line with VALUE, using OP.
7669 If the line does not have an effort defined, return nil."
7670 ;; `effort-minutes' property cannot be extracted directly from
7671 ;; current line but is stored as a property in `txt'.
7672 (let ((effort (get-text-property 0 'effort-minutes (org-get-at-bol 'txt))))
7673 (funcall op
7674 (or effort (if org-sort-agenda-noeffort-is-high 32767 -1))
7675 value)))
7677 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7678 "Expand group tags in FILTER for the agenda.
7679 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7680 (if org-group-tags
7681 (let ((case-fold-search t) rtn)
7682 (mapc
7683 (lambda (f)
7684 (let (f0 dir)
7685 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7686 (setq dir (match-string 1 f) f0 (match-string 2 f))
7687 (setq dir (if no-operator "" "+") f0 f))
7688 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7689 (org-tags-expand f0 t t))
7690 rtn))))
7691 filter)
7692 (reverse rtn))
7693 filter))
7695 (defun org-agenda-filter-apply (filter type &optional expand)
7696 "Set FILTER as the new agenda filter and apply it. Optional
7697 argument EXPAND can be used for the TYPE tag and will expand the
7698 tags in the FILTER if any of the tags in FILTER are grouptags."
7699 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7700 (when org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7701 (let (tags cat txt)
7702 (setq org-agenda-filter-form (org-agenda-filter-make-matcher filter type expand))
7703 ;; Only set `org-agenda-filtered-by-category' to t when a unique
7704 ;; category is used as the filter:
7705 (setq org-agenda-filtered-by-category
7706 (and (eq type 'category)
7707 (not (equal (substring (car filter) 0 1) "-"))))
7708 (org-agenda-set-mode-name)
7709 (save-excursion
7710 (goto-char (point-min))
7711 (while (not (eobp))
7712 (if (org-get-at-bol 'org-marker)
7713 (progn
7714 (setq tags (org-get-at-bol 'tags)
7715 cat (org-get-at-eol 'org-category 1)
7716 txt (org-get-at-bol 'txt))
7717 (unless (eval org-agenda-filter-form)
7718 (org-agenda-filter-hide-line type))
7719 (beginning-of-line 2))
7720 (beginning-of-line 2))))
7721 (when (get-char-property (point) 'invisible)
7722 (ignore-errors (org-agenda-previous-line)))))
7724 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7725 "Filter by top headline HL."
7726 (org-agenda-set-mode-name)
7727 (save-excursion
7728 (goto-char (point-min))
7729 (while (not (eobp))
7730 (let* ((pos (org-get-at-bol 'org-hd-marker))
7731 (tophl (and pos (org-find-top-headline pos))))
7732 (when (and tophl (funcall (if negative 'identity 'not)
7733 (string= hl tophl)))
7734 (org-agenda-filter-hide-line 'top-headline)))
7735 (beginning-of-line 2)))
7736 (when (get-char-property (point) 'invisible)
7737 (org-agenda-previous-line))
7738 (setq org-agenda-top-headline-filter hl
7739 org-agenda-filtered-by-top-headline t))
7741 (defun org-agenda-filter-hide-line (type)
7742 "Hide lines with TYPE in the agenda buffer."
7743 (let* ((b (max (point-min) (1- (point-at-bol))))
7744 (e (point-at-eol)))
7745 (let ((inhibit-read-only t))
7746 (add-text-properties
7747 b e `(invisible org-filtered org-filter-type ,type)))))
7749 (defun org-agenda-remove-filter (type)
7750 (interactive)
7751 "Remove filter of type TYPE from the agenda buffer."
7752 (save-excursion
7753 (goto-char (point-min))
7754 (let ((inhibit-read-only t) pos)
7755 (while (setq pos (text-property-any (point) (point-max) 'org-filter-type type))
7756 (goto-char pos)
7757 (remove-text-properties
7758 (point) (next-single-property-change (point) 'org-filter-type)
7759 `(invisible org-filtered org-filter-type ,type))))
7760 (set (intern (format "org-agenda-%s-filter" (intern-soft type))) nil)
7761 (setq org-agenda-filter-form nil)
7762 (org-agenda-set-mode-name)
7763 (org-agenda-finalize)))
7765 (defun org-agenda-filter-show-all-tag nil
7766 (org-agenda-remove-filter 'tag))
7767 (defun org-agenda-filter-show-all-re nil
7768 (org-agenda-remove-filter 'regexp))
7769 (defun org-agenda-filter-show-all-effort nil
7770 (org-agenda-remove-filter 'effort))
7771 (defun org-agenda-filter-show-all-cat nil
7772 (org-agenda-remove-filter 'category))
7773 (defun org-agenda-filter-show-all-top-filter nil
7774 (org-agenda-remove-filter 'top-headline))
7776 (defun org-agenda-manipulate-query-add ()
7777 "Manipulate the query by adding a search term with positive selection.
7778 Positive selection means the term must be matched for selection of an entry."
7779 (interactive)
7780 (org-agenda-manipulate-query ?\[))
7781 (defun org-agenda-manipulate-query-subtract ()
7782 "Manipulate the query by adding a search term with negative selection.
7783 Negative selection means term must not be matched for selection of an entry."
7784 (interactive)
7785 (org-agenda-manipulate-query ?\]))
7786 (defun org-agenda-manipulate-query-add-re ()
7787 "Manipulate the query by adding a search regexp with positive selection.
7788 Positive selection means the regexp must match for selection of an entry."
7789 (interactive)
7790 (org-agenda-manipulate-query ?\{))
7791 (defun org-agenda-manipulate-query-subtract-re ()
7792 "Manipulate the query by adding a search regexp with negative selection.
7793 Negative selection means regexp must not match for selection of an entry."
7794 (interactive)
7795 (org-agenda-manipulate-query ?\}))
7796 (defun org-agenda-manipulate-query (char)
7797 (cond
7798 ((eq org-agenda-type 'agenda)
7799 (let ((org-agenda-include-inactive-timestamps t))
7800 (org-agenda-redo))
7801 (message "Display now includes inactive timestamps as well"))
7802 ((eq org-agenda-type 'search)
7803 (org-add-to-string
7804 'org-agenda-query-string
7805 (if org-agenda-last-search-view-search-was-boolean
7806 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7807 (?\{ . " +{}") (?\} . " -{}"))))
7808 " "))
7809 (setq org-agenda-redo-command
7810 (list 'org-search-view
7811 (car (get-text-property (min (1- (point-max)) (point))
7812 'org-last-args))
7813 org-agenda-query-string
7814 (+ (length org-agenda-query-string)
7815 (if (member char '(?\{ ?\})) 0 1))))
7816 (set-register org-agenda-query-register org-agenda-query-string)
7817 (let ((org-agenda-overriding-arguments
7818 (cdr org-agenda-redo-command)))
7819 (org-agenda-redo)))
7820 (t (error "Cannot manipulate query for %s-type agenda buffers"
7821 org-agenda-type))))
7823 (defun org-add-to-string (var string)
7824 (set var (concat (symbol-value var) string)))
7826 (defun org-agenda-goto-date (span)
7827 "Jump to DATE in agenda."
7828 (interactive "P")
7829 (let* ((org-read-date-prefer-future
7830 (eval org-agenda-jump-prefer-future))
7831 (date (org-read-date))
7832 (day (time-to-days (org-time-string-to-time date)))
7833 (org-agenda-sticky-orig org-agenda-sticky)
7834 (org-agenda-buffer-tmp-name (buffer-name))
7835 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7836 (0-arg (or current-prefix-arg (car args)))
7837 (2-arg (nth 2 args))
7838 (with-hour-p (nth 4 org-agenda-redo-command))
7839 (newcmd (list 'org-agenda-list 0-arg date
7840 (org-agenda-span-to-ndays
7841 2-arg (org-time-string-to-absolute date))
7842 with-hour-p))
7843 (newargs (cdr newcmd))
7844 (inhibit-read-only t)
7845 org-agenda-sticky)
7846 (if (not (org-agenda-check-type t 'agenda))
7847 (error "Not available in non-agenda views")
7848 (add-text-properties (point-min) (point-max)
7849 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7850 (org-agenda-redo)
7851 (goto-char (point-min))
7852 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7853 (save-excursion (move-beginning-of-line 2) (eobp))))
7854 (move-beginning-of-line 2))
7855 (setq org-agenda-sticky org-agenda-sticky-orig
7856 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7858 (defun org-agenda-goto-today ()
7859 "Go to today."
7860 (interactive)
7861 (org-agenda-check-type t 'agenda)
7862 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7863 (curspan (nth 2 args))
7864 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7865 (cond
7866 (tdpos (goto-char tdpos))
7867 ((eq org-agenda-type 'agenda)
7868 (let* ((sd (org-agenda-compute-starting-span
7869 (org-today) (or curspan org-agenda-span)))
7870 (org-agenda-overriding-arguments args))
7871 (setf (nth 1 org-agenda-overriding-arguments) sd)
7872 (org-agenda-redo)
7873 (org-agenda-find-same-or-today-or-agenda)))
7874 (t (error "Cannot find today")))))
7876 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7877 (goto-char
7878 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7879 (text-property-any (point-min) (point-max) 'org-today t)
7880 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7881 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7882 (org-agenda-backward-block))
7883 (point-min))))
7885 (defun org-agenda-backward-block ()
7886 "Move backward by one agenda block."
7887 (interactive)
7888 (org-agenda-forward-block 'backward))
7890 (defun org-agenda-forward-block (&optional backward)
7891 "Move forward by one agenda block.
7892 When optional argument BACKWARD is set, go backward"
7893 (interactive)
7894 (cond ((not (derived-mode-p 'org-agenda-mode))
7895 (user-error
7896 "Cannot execute this command outside of org-agenda-mode buffers"))
7897 ((looking-at (if backward "\\`" "\\'"))
7898 (message "Already at the %s block" (if backward "first" "last")))
7899 (t (let ((pos (prog1 (point)
7900 (ignore-errors (if backward (backward-char 1)
7901 (move-end-of-line 1)))))
7902 (f (if backward
7903 'previous-single-property-change
7904 'next-single-property-change))
7905 moved dest)
7906 (while (and (setq dest (funcall
7907 f (point) 'org-agenda-structural-header))
7908 (not (get-text-property
7909 (point) 'org-agenda-structural-header)))
7910 (setq moved t)
7911 (goto-char dest))
7912 (if moved (move-beginning-of-line 1)
7913 (goto-char (if backward (point-min) (point-max)))
7914 (move-beginning-of-line 1)
7915 (message "No %s block" (if backward "previous" "further")))))))
7917 (defun org-agenda-later (arg)
7918 "Go forward in time by the current span.
7919 With prefix ARG, go forward that many times the current span."
7920 (interactive "p")
7921 (org-agenda-check-type t 'agenda)
7922 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7923 (span (or (nth 2 args) org-agenda-current-span))
7924 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7925 (greg (calendar-gregorian-from-absolute sd))
7926 (cnt (org-get-at-bol 'org-day-cnt))
7927 greg2)
7928 (cond
7929 ((numberp span)
7930 (setq sd (+ (* span arg) sd)))
7931 ((eq span 'day)
7932 (setq sd (+ arg sd)))
7933 ((eq span 'week)
7934 (setq sd (+ (* 7 arg) sd)))
7935 ((eq span 'fortnight)
7936 (setq sd (+ (* 14 arg) sd)))
7937 ((eq span 'month)
7938 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7939 sd (calendar-absolute-from-gregorian greg2))
7940 (setcar greg2 (1+ (car greg2))))
7941 ((eq span 'year)
7942 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7943 sd (calendar-absolute-from-gregorian greg2))
7944 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7946 (setq sd (+ (* span arg) sd))))
7947 (let ((org-agenda-overriding-cmd
7948 ;; `cmd' may have been set by `org-agenda-run-series' which
7949 ;; uses `org-agenda-overriding-cmd' to decide whether
7950 ;; overriding is allowed for `cmd'
7951 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7952 (org-agenda-overriding-arguments
7953 (list (car args) sd span)))
7954 (org-agenda-redo)
7955 (org-agenda-find-same-or-today-or-agenda cnt))))
7957 (defun org-agenda-earlier (arg)
7958 "Go backward in time by the current span.
7959 With prefix ARG, go backward that many times the current span."
7960 (interactive "p")
7961 (org-agenda-later (- arg)))
7963 (defun org-agenda-view-mode-dispatch ()
7964 "Call one of the view mode commands."
7965 (interactive)
7966 (org-unlogged-message
7967 "View: [d]ay [w]eek for[t]night [m]onth [y]ear [SPC]reset [q]uit/abort
7968 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7969 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7970 (pcase (read-char-exclusive)
7971 (?\ (call-interactively 'org-agenda-reset-view))
7972 (?d (call-interactively 'org-agenda-day-view))
7973 (?w (call-interactively 'org-agenda-week-view))
7974 (?t (call-interactively 'org-agenda-fortnight-view))
7975 (?m (call-interactively 'org-agenda-month-view))
7976 (?y (call-interactively 'org-agenda-year-view))
7977 (?l (call-interactively 'org-agenda-log-mode))
7978 (?L (org-agenda-log-mode '(4)))
7979 (?c (org-agenda-log-mode 'clockcheck))
7980 ((or ?F ?f) (call-interactively 'org-agenda-follow-mode))
7981 (?a (call-interactively 'org-agenda-archives-mode))
7982 (?A (org-agenda-archives-mode 'files))
7983 ((or ?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7984 ((or ?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7985 (?G (call-interactively 'org-agenda-toggle-time-grid))
7986 (?D (call-interactively 'org-agenda-toggle-diary))
7987 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7988 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7989 (org-agenda-check-type t 'agenda)
7990 (org-agenda-redo))
7991 (message "Display now includes inactive timestamps as well"))
7992 (?q (message "Abort"))
7993 (key (user-error "Invalid key: %s" key))))
7995 (defun org-agenda-reset-view ()
7996 "Switch to default view for agenda."
7997 (interactive)
7998 (org-agenda-change-time-span org-agenda-span))
8000 (defun org-agenda-day-view (&optional day-of-month)
8001 "Switch to daily view for agenda.
8002 With argument DAY-OF-MONTH, switch to that day of the month."
8003 (interactive "P")
8004 (org-agenda-change-time-span 'day day-of-month))
8006 (defun org-agenda-week-view (&optional iso-week)
8007 "Switch to weekly view for agenda.
8008 With argument ISO-WEEK, switch to the corresponding ISO week.
8009 If ISO-WEEK has more then 2 digits, only the last two encode
8010 the week. Any digits before this encode a year. So 200712
8011 means week 12 of year 2007. Years ranging from 70 years ago
8012 to 30 years in the future can also be written as 2-digit years."
8013 (interactive "P")
8014 (org-agenda-change-time-span 'week iso-week))
8016 (defun org-agenda-fortnight-view (&optional iso-week)
8017 "Switch to fortnightly view for agenda.
8018 With argument ISO-WEEK, switch to the corresponding ISO week.
8019 If ISO-WEEK has more then 2 digits, only the last two encode
8020 the week. Any digits before this encode a year. So 200712
8021 means week 12 of year 2007. Years ranging from 70 years ago
8022 to 30 years in the future can also be written as 2-digit years."
8023 (interactive "P")
8024 (org-agenda-change-time-span 'fortnight iso-week))
8026 (defun org-agenda-month-view (&optional month)
8027 "Switch to monthly view for agenda.
8028 With argument MONTH, switch to that month. If MONTH has more
8029 then 2 digits, only the last two encode the month. Any digits
8030 before this encode a year. So 200712 means December year 2007.
8031 Years ranging from 70 years ago to 30 years in the future can
8032 also be written as 2-digit years."
8033 (interactive "P")
8034 (org-agenda-change-time-span 'month month))
8036 (defun org-agenda-year-view (&optional year)
8037 "Switch to yearly view for agenda.
8038 With argument YEAR, switch to that year. Years ranging from 70
8039 years ago to 30 years in the future can also be written as
8040 2-digit years."
8041 (interactive "P")
8042 (when year
8043 (setq year (org-small-year-to-year year)))
8044 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
8045 (org-agenda-change-time-span 'year year)
8046 (error "Abort")))
8048 (defun org-agenda-change-time-span (span &optional n)
8049 "Change the agenda view to SPAN.
8050 SPAN may be `day', `week', `fortnight', `month', `year'."
8051 (org-agenda-check-type t 'agenda)
8052 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
8053 (curspan (nth 2 args)))
8054 (when (and (not n) (equal curspan span))
8055 (error "Viewing span is already \"%s\"" span))
8056 (let* ((sd (or (org-get-at-bol 'day)
8057 (nth 1 args)
8058 org-starting-day))
8059 (sd (org-agenda-compute-starting-span sd span n))
8060 (org-agenda-overriding-cmd
8061 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
8062 (org-agenda-overriding-arguments
8063 (list (car args) sd span)))
8064 (org-agenda-redo)
8065 (org-agenda-find-same-or-today-or-agenda))
8066 (org-agenda-set-mode-name)
8067 (message "Switched to %s view" span)))
8069 (defun org-agenda-compute-starting-span (sd span &optional n)
8070 "Compute starting date for agenda.
8071 SPAN may be `day', `week', `fortnight', `month', `year'. The return value
8072 is a cons cell with the starting date and the number of days,
8073 so that the date SD will be in that range."
8074 (let* ((greg (calendar-gregorian-from-absolute sd))
8075 (dg (nth 1 greg))
8076 (mg (car greg))
8077 (yg (nth 2 greg)))
8078 (cond
8079 ((eq span 'day)
8080 (when n
8081 (setq sd (+ (calendar-absolute-from-gregorian
8082 (list mg 1 yg))
8083 n -1))))
8084 ((or (eq span 'week) (eq span 'fortnight))
8085 (let* ((nt (calendar-day-of-week
8086 (calendar-gregorian-from-absolute sd)))
8087 (d (if org-agenda-start-on-weekday
8088 (- nt org-agenda-start-on-weekday)
8091 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
8092 (when n
8093 (require 'cal-iso)
8094 (when (> n 99)
8095 (setq y1 (org-small-year-to-year (/ n 100))
8096 n (mod n 100)))
8097 (setq sd
8098 (calendar-iso-to-absolute
8099 (list n 1
8100 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
8101 ((eq span 'month)
8102 (let (y1)
8103 (when (and n (> n 99))
8104 (setq y1 (org-small-year-to-year (/ n 100))
8105 n (mod n 100)))
8106 (setq sd (calendar-absolute-from-gregorian
8107 (list (or n mg) 1 (or y1 yg))))))
8108 ((eq span 'year)
8109 (setq sd (calendar-absolute-from-gregorian
8110 (list 1 1 (or n yg))))))
8111 sd))
8113 (defun org-agenda-next-date-line (&optional arg)
8114 "Jump to the next line indicating a date in agenda buffer."
8115 (interactive "p")
8116 (org-agenda-check-type t 'agenda)
8117 (beginning-of-line 1)
8118 ;; This does not work if user makes date format that starts with a blank
8119 (when (looking-at-p "^\\S-") (forward-char 1))
8120 (unless (re-search-forward "^\\S-" nil t arg)
8121 (backward-char 1)
8122 (error "No next date after this line in this buffer"))
8123 (goto-char (match-beginning 0)))
8125 (defun org-agenda-previous-date-line (&optional arg)
8126 "Jump to the previous line indicating a date in agenda buffer."
8127 (interactive "p")
8128 (org-agenda-check-type t 'agenda)
8129 (beginning-of-line 1)
8130 (unless (re-search-backward "^\\S-" nil t arg)
8131 (error "No previous date before this line in this buffer")))
8133 ;; Initialize the highlight
8134 (defvar org-hl (make-overlay 1 1))
8135 (overlay-put org-hl 'face 'highlight)
8137 (defun org-highlight (begin end &optional buffer)
8138 "Highlight a region with overlay."
8139 (move-overlay org-hl begin end (or buffer (current-buffer))))
8141 (defun org-unhighlight ()
8142 "Detach overlay INDEX."
8143 (delete-overlay org-hl))
8145 (defun org-unhighlight-once ()
8146 "Remove the highlight from its position, and this function from the hook."
8147 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8148 (org-unhighlight))
8150 (defvar org-agenda-pre-follow-window-conf nil)
8151 (defun org-agenda-follow-mode ()
8152 "Toggle follow mode in an agenda buffer."
8153 (interactive)
8154 (unless org-agenda-follow-mode
8155 (setq org-agenda-pre-follow-window-conf
8156 (current-window-configuration)))
8157 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8158 (unless org-agenda-follow-mode
8159 (set-window-configuration org-agenda-pre-follow-window-conf))
8160 (org-agenda-set-mode-name)
8161 (org-agenda-do-context-action)
8162 (message "Follow mode is %s"
8163 (if org-agenda-follow-mode "on" "off")))
8165 (defun org-agenda-entry-text-mode (&optional arg)
8166 "Toggle entry text mode in an agenda buffer."
8167 (interactive "P")
8168 (if (or org-agenda-tag-filter
8169 org-agenda-category-filter
8170 org-agenda-regexp-filter
8171 org-agenda-top-headline-filter)
8172 (user-error "Can't show entry text in filtered views")
8173 (setq org-agenda-entry-text-mode (or (integerp arg)
8174 (not org-agenda-entry-text-mode)))
8175 (org-agenda-entry-text-hide)
8176 (and org-agenda-entry-text-mode
8177 (let ((org-agenda-entry-text-maxlines
8178 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8179 (org-agenda-entry-text-show)))
8180 (org-agenda-set-mode-name)
8181 (message "Entry text mode is %s%s"
8182 (if org-agenda-entry-text-mode "on" "off")
8183 (if (not org-agenda-entry-text-mode) ""
8184 (format " (maximum number of lines is %d)"
8185 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8187 (defun org-agenda-clockreport-mode ()
8188 "Toggle clocktable mode in an agenda buffer."
8189 (interactive)
8190 (org-agenda-check-type t 'agenda)
8191 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
8192 (org-agenda-set-mode-name)
8193 (org-agenda-redo)
8194 (message "Clocktable mode is %s"
8195 (if org-agenda-clockreport-mode "on" "off")))
8197 (defun org-agenda-log-mode (&optional special)
8198 "Toggle log mode in an agenda buffer.
8200 With argument SPECIAL, show all possible log items, not only the ones
8201 configured in `org-agenda-log-mode-items'.
8203 With a `\\[universal-argument] \\[universal-argument]' prefix, show *only* \
8204 log items, nothing else."
8205 (interactive "P")
8206 (org-agenda-check-type t 'agenda)
8207 (setq org-agenda-show-log
8208 (cond
8209 ((equal special '(16)) 'only)
8210 ((eq special 'clockcheck)
8211 (if (eq org-agenda-show-log 'clockcheck)
8212 nil 'clockcheck))
8213 (special '(closed clock state))
8214 (t (not org-agenda-show-log))))
8215 (org-agenda-set-mode-name)
8216 (org-agenda-redo)
8217 (message "Log mode is %s" (if org-agenda-show-log "on" "off")))
8219 (defun org-agenda-archives-mode (&optional with-files)
8220 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8221 When called with a prefix argument, include all archive files as well."
8222 (interactive "P")
8223 (setq org-agenda-archives-mode
8224 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8225 (org-agenda-set-mode-name)
8226 (org-agenda-redo)
8227 (message
8228 "%s"
8229 (cond
8230 ((eq org-agenda-archives-mode nil)
8231 "No archives are included")
8232 ((eq org-agenda-archives-mode 'trees)
8233 (format "Trees with :%s: tag are included" org-archive-tag))
8234 ((eq org-agenda-archives-mode t)
8235 (format "Trees with :%s: tag and all active archive files are included"
8236 org-archive-tag)))))
8238 (defun org-agenda-toggle-diary ()
8239 "Toggle diary inclusion in an agenda buffer."
8240 (interactive)
8241 (org-agenda-check-type t 'agenda)
8242 (setq org-agenda-include-diary (not org-agenda-include-diary))
8243 (org-agenda-redo)
8244 (org-agenda-set-mode-name)
8245 (message "Diary inclusion turned %s"
8246 (if org-agenda-include-diary "on" "off")))
8248 (defun org-agenda-toggle-deadlines ()
8249 "Toggle inclusion of entries with a deadline in an agenda buffer."
8250 (interactive)
8251 (org-agenda-check-type t 'agenda)
8252 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8253 (org-agenda-redo)
8254 (org-agenda-set-mode-name)
8255 (message "Deadlines inclusion turned %s"
8256 (if org-agenda-include-deadlines "on" "off")))
8258 (defun org-agenda-toggle-time-grid ()
8259 "Toggle time grid in an agenda buffer."
8260 (interactive)
8261 (org-agenda-check-type t 'agenda)
8262 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8263 (org-agenda-redo)
8264 (org-agenda-set-mode-name)
8265 (message "Time-grid turned %s"
8266 (if org-agenda-use-time-grid "on" "off")))
8268 (defun org-agenda-set-mode-name ()
8269 "Set the mode name to indicate all the small mode settings."
8270 (setq mode-name
8271 (list "Org-Agenda"
8272 (if (get 'org-agenda-files 'org-restrict) " []" "")
8274 '(:eval (org-agenda-span-name org-agenda-current-span))
8275 (if org-agenda-follow-mode " Follow" "")
8276 (if org-agenda-entry-text-mode " ETxt" "")
8277 (if org-agenda-include-diary " Diary" "")
8278 (if org-agenda-include-deadlines " Ddl" "")
8279 (if org-agenda-use-time-grid " Grid" "")
8280 (if (and (boundp 'org-habit-show-habits)
8281 org-habit-show-habits) " Habit" "")
8282 (cond
8283 ((consp org-agenda-show-log) " LogAll")
8284 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8285 (org-agenda-show-log " Log")
8286 (t ""))
8287 (if (or org-agenda-category-filter
8288 (get 'org-agenda-category-filter :preset-filter))
8289 '(:eval (propertize
8290 (concat " <"
8291 (mapconcat
8292 'identity
8293 (append
8294 (get 'org-agenda-category-filter :preset-filter)
8295 org-agenda-category-filter)
8297 ">")
8298 'face 'org-agenda-filter-category
8299 'help-echo "Category used in filtering")) "")
8300 (if (or org-agenda-tag-filter
8301 (get 'org-agenda-tag-filter :preset-filter))
8302 '(:eval (propertize
8303 (concat " {"
8304 (mapconcat
8305 'identity
8306 (append
8307 (get 'org-agenda-tag-filter :preset-filter)
8308 org-agenda-tag-filter)
8310 "}")
8311 'face 'org-agenda-filter-tags
8312 'help-echo "Tags used in filtering")) "")
8313 (if (or org-agenda-effort-filter
8314 (get 'org-agenda-effort-filter :preset-filter))
8315 '(:eval (propertize
8316 (concat " {"
8317 (mapconcat
8318 'identity
8319 (append
8320 (get 'org-agenda-effort-filter :preset-filter)
8321 org-agenda-effort-filter)
8323 "}")
8324 'face 'org-agenda-filter-effort
8325 'help-echo "Effort conditions used in filtering")) "")
8326 (if (or org-agenda-regexp-filter
8327 (get 'org-agenda-regexp-filter :preset-filter))
8328 '(:eval (propertize
8329 (concat " ["
8330 (mapconcat
8331 'identity
8332 (append
8333 (get 'org-agenda-regexp-filter :preset-filter)
8334 org-agenda-regexp-filter)
8336 "]")
8337 'face 'org-agenda-filter-regexp
8338 'help-echo "Regexp used in filtering")) "")
8339 (if org-agenda-archives-mode
8340 (if (eq org-agenda-archives-mode t)
8341 " Archives"
8342 (format " :%s:" org-archive-tag))
8344 (if org-agenda-clockreport-mode " Clock" "")))
8345 (force-mode-line-update))
8347 (defun org-agenda-update-agenda-type ()
8348 "Update the agenda type after each command."
8349 (setq org-agenda-type
8350 (or (get-text-property (point) 'org-agenda-type)
8351 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8353 (defun org-agenda-next-line ()
8354 "Move cursor to the next line, and show if follow mode is active."
8355 (interactive)
8356 (call-interactively 'next-line)
8357 (org-agenda-do-context-action))
8359 (defun org-agenda-previous-line ()
8360 "Move cursor to the previous line, and show if follow-mode is active."
8361 (interactive)
8362 (call-interactively 'previous-line)
8363 (org-agenda-do-context-action))
8365 (defun org-agenda-next-item (n)
8366 "Move cursor to next agenda item."
8367 (interactive "p")
8368 (let ((col (current-column)))
8369 (dotimes (c n)
8370 (when (next-single-property-change (point-at-eol) 'org-marker)
8371 (move-end-of-line 1)
8372 (goto-char (next-single-property-change (point) 'org-marker))))
8373 (org-move-to-column col))
8374 (org-agenda-do-context-action))
8376 (defun org-agenda-previous-item (n)
8377 "Move cursor to next agenda item."
8378 (interactive "p")
8379 (dotimes (c n)
8380 (let ((col (current-column))
8381 (goto (save-excursion
8382 (move-end-of-line 0)
8383 (previous-single-property-change (point) 'org-marker))))
8384 (when goto (goto-char goto))
8385 (org-move-to-column col)))
8386 (org-agenda-do-context-action))
8388 (defun org-agenda-do-context-action ()
8389 "Show outline path and, maybe, follow mode window."
8390 (let ((m (org-get-at-bol 'org-marker)))
8391 (when (and (markerp m) (marker-buffer m))
8392 (and org-agenda-follow-mode
8393 (if org-agenda-follow-indirect
8394 (org-agenda-tree-to-indirect-buffer nil)
8395 (org-agenda-show)))
8396 (and org-agenda-show-outline-path
8397 (org-with-point-at m (org-display-outline-path t))))))
8399 (defun org-agenda-show-tags ()
8400 "Show the tags applicable to the current item."
8401 (interactive)
8402 (let* ((tags (org-get-at-bol 'tags)))
8403 (if tags
8404 (message "Tags are :%s:"
8405 (org-no-properties (mapconcat 'identity tags ":")))
8406 (message "No tags associated with this line"))))
8408 (defun org-agenda-goto (&optional highlight)
8409 "Go to the entry at point in the corresponding Org file."
8410 (interactive)
8411 (let* ((marker (or (org-get-at-bol 'org-marker)
8412 (org-agenda-error)))
8413 (buffer (marker-buffer marker))
8414 (pos (marker-position marker)))
8415 (switch-to-buffer-other-window buffer)
8416 (widen)
8417 (push-mark)
8418 (goto-char pos)
8419 (when (derived-mode-p 'org-mode)
8420 (org-show-context 'agenda)
8421 (recenter (/ (window-height) 2))
8422 (org-back-to-heading t)
8423 (let ((case-fold-search nil))
8424 (when (re-search-forward org-complex-heading-regexp nil t)
8425 (goto-char (match-beginning 4)))))
8426 (run-hooks 'org-agenda-after-show-hook)
8427 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8429 (defvar org-agenda-after-show-hook nil
8430 "Normal hook run after an item has been shown from the agenda.
8431 Point is in the buffer where the item originated.")
8433 (defun org-agenda-kill ()
8434 "Kill the entry or subtree belonging to the current agenda entry."
8435 (interactive)
8436 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8437 (let* ((bufname-orig (buffer-name))
8438 (marker (or (org-get-at-bol 'org-marker)
8439 (org-agenda-error)))
8440 (buffer (marker-buffer marker))
8441 (pos (marker-position marker))
8442 (type (org-get-at-bol 'type))
8443 dbeg dend (n 0) conf)
8444 (org-with-remote-undo buffer
8445 (with-current-buffer buffer
8446 (save-excursion
8447 (goto-char pos)
8448 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8449 (setq dbeg (progn (org-back-to-heading t) (point))
8450 dend (org-end-of-subtree t t))
8451 (setq dbeg (point-at-bol)
8452 dend (min (point-max) (1+ (point-at-eol)))))
8453 (goto-char dbeg)
8454 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8455 (setq conf (or (eq t org-agenda-confirm-kill)
8456 (and (numberp org-agenda-confirm-kill)
8457 (> n org-agenda-confirm-kill))))
8458 (and conf
8459 (not (y-or-n-p
8460 (format "Delete entry with %d lines in buffer \"%s\"? "
8461 n (buffer-name buffer))))
8462 (error "Abort"))
8463 (let ((org-agenda-buffer-name bufname-orig))
8464 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8465 (with-current-buffer buffer (delete-region dbeg dend))
8466 (message "Agenda item and source killed"))))
8468 (defvar org-archive-default-command) ; defined in org-archive.el
8469 (defun org-agenda-archive-default ()
8470 "Archive the entry or subtree belonging to the current agenda entry."
8471 (interactive)
8472 (require 'org-archive)
8473 (org-agenda-archive-with org-archive-default-command))
8475 (defun org-agenda-archive-default-with-confirmation ()
8476 "Archive the entry or subtree belonging to the current agenda entry."
8477 (interactive)
8478 (require 'org-archive)
8479 (org-agenda-archive-with org-archive-default-command 'confirm))
8481 (defun org-agenda-archive ()
8482 "Archive the entry or subtree belonging to the current agenda entry."
8483 (interactive)
8484 (org-agenda-archive-with 'org-archive-subtree))
8486 (defun org-agenda-archive-to-archive-sibling ()
8487 "Move the entry to the archive sibling."
8488 (interactive)
8489 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8491 (defun org-agenda-archive-with (cmd &optional confirm)
8492 "Move the entry to the archive sibling."
8493 (interactive)
8494 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8495 (let* ((bufname-orig (buffer-name))
8496 (marker (or (org-get-at-bol 'org-marker)
8497 (org-agenda-error)))
8498 (buffer (marker-buffer marker))
8499 (pos (marker-position marker)))
8500 (org-with-remote-undo buffer
8501 (with-current-buffer buffer
8502 (if (derived-mode-p 'org-mode)
8503 (if (and confirm
8504 (not (y-or-n-p "Archive this subtree or entry? ")))
8505 (error "Abort")
8506 (save-window-excursion
8507 (goto-char pos)
8508 (let ((org-agenda-buffer-name bufname-orig))
8509 (org-remove-subtree-entries-from-agenda))
8510 (org-back-to-heading t)
8511 (funcall cmd)))
8512 (error "Archiving works only in Org files"))))))
8514 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8515 "Remove all lines in the agenda that correspond to a given subtree.
8516 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8517 If this information is not given, the function uses the tree at point."
8518 (let ((buf (or buf (current-buffer))) m p)
8519 (save-excursion
8520 (unless (and beg end)
8521 (org-back-to-heading t)
8522 (setq beg (point))
8523 (org-end-of-subtree t)
8524 (setq end (point)))
8525 (set-buffer (get-buffer org-agenda-buffer-name))
8526 (save-excursion
8527 (goto-char (point-max))
8528 (beginning-of-line 1)
8529 (while (not (bobp))
8530 (when (and (setq m (org-get-at-bol 'org-marker))
8531 (equal buf (marker-buffer m))
8532 (setq p (marker-position m))
8533 (>= p beg)
8534 (< p end))
8535 (let ((inhibit-read-only t))
8536 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8537 (beginning-of-line 0))))))
8539 (defun org-agenda-refile (&optional goto rfloc no-update)
8540 "Refile the item at point.
8542 When called with `\\[universal-argument] \\[universal-argument]', \
8543 go to the location of the last
8544 refiled item.
8546 When called with `\\[universal-argument] \\[universal-argument] \
8547 \\[universal-argument]' prefix or when GOTO is 0, clear
8548 the refile cache.
8550 RFLOC can be a refile location obtained in a different way.
8552 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8553 (interactive "P")
8554 (cond
8555 ((member goto '(0 (64)))
8556 (org-refile-cache-clear))
8557 ((equal goto '(16))
8558 (org-refile-goto-last-stored))
8560 (let* ((buffer-orig (buffer-name))
8561 (marker (or (org-get-at-bol 'org-hd-marker)
8562 (org-agenda-error)))
8563 (buffer (marker-buffer marker))
8564 (pos (marker-position marker))
8565 (rfloc (or rfloc
8566 (org-refile-get-location
8567 (if goto "Goto" "Refile to") buffer
8568 org-refile-allow-creating-parent-nodes))))
8569 (with-current-buffer buffer
8570 (org-with-wide-buffer
8571 (goto-char marker)
8572 (let ((org-agenda-buffer-name buffer-orig))
8573 (org-remove-subtree-entries-from-agenda))
8574 (org-refile goto buffer rfloc))))
8575 (unless no-update (org-agenda-redo)))))
8577 (defun org-agenda-open-link (&optional arg)
8578 "Open the link(s) in the current entry, if any.
8579 This looks for a link in the displayed line in the agenda.
8580 It also looks at the text of the entry itself."
8581 (interactive "P")
8582 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8583 (org-get-at-bol 'org-marker)))
8584 (buffer (and marker (marker-buffer marker)))
8585 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8586 (lkall (and buffer (org-offer-links-in-entry
8587 buffer marker arg prefix)))
8588 (lk0 (car lkall))
8589 (lk (if (stringp lk0) (list lk0) lk0))
8590 (lkend (cdr lkall))
8591 trg)
8592 (cond
8593 ((and buffer lk)
8594 (mapcar (lambda(l)
8595 (with-current-buffer buffer
8596 (setq trg (and (string-match org-bracket-link-regexp l)
8597 (match-string 1 l)))
8598 (if (or (not trg) (string-match org-any-link-re trg))
8599 (org-with-wide-buffer
8600 (goto-char marker)
8601 (when (search-forward l nil lkend)
8602 (goto-char (match-beginning 0))
8603 (org-open-at-point)))
8604 ;; This is an internal link, widen the buffer
8605 (switch-to-buffer-other-window buffer)
8606 (widen)
8607 (goto-char marker)
8608 (when (search-forward l nil lkend)
8609 (goto-char (match-beginning 0))
8610 (org-open-at-point)))))
8611 lk))
8612 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8613 (save-excursion
8614 (beginning-of-line 1)
8615 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8616 (org-open-link-from-string (match-string 1)))
8617 (t (message "No link to open here")))))
8619 (defun org-agenda-copy-local-variable (var)
8620 "Get a variable from a referenced buffer and install it here."
8621 (let ((m (org-get-at-bol 'org-marker)))
8622 (when (and m (buffer-live-p (marker-buffer m)))
8623 (set (make-local-variable var)
8624 (with-current-buffer (marker-buffer m)
8625 (symbol-value var))))))
8627 (defun org-agenda-switch-to (&optional delete-other-windows)
8628 "Go to the Org mode file which contains the item at point.
8629 When optional argument DELETE-OTHER-WINDOWS is non-nil, the
8630 displayed Org file fills the frame."
8631 (interactive)
8632 (if (and org-return-follows-link
8633 (not (org-get-at-bol 'org-marker))
8634 (org-in-regexp org-bracket-link-regexp))
8635 (org-open-link-from-string (match-string 0))
8636 (let* ((marker (or (org-get-at-bol 'org-marker)
8637 (org-agenda-error)))
8638 (buffer (marker-buffer marker))
8639 (pos (marker-position marker)))
8640 (unless buffer (user-error "Trying to switch to non-existent buffer"))
8641 (pop-to-buffer-same-window buffer)
8642 (when delete-other-windows (delete-other-windows))
8643 (widen)
8644 (goto-char pos)
8645 (when (derived-mode-p 'org-mode)
8646 (org-show-context 'agenda)
8647 (run-hooks 'org-agenda-after-show-hook)))))
8649 (defun org-agenda-goto-mouse (ev)
8650 "Go to the Org file which contains the item at the mouse click."
8651 (interactive "e")
8652 (mouse-set-point ev)
8653 (org-agenda-goto))
8655 (defun org-agenda-show (&optional full-entry)
8656 "Display the Org file which contains the item at point.
8657 With prefix argument FULL-ENTRY, make the entire entry visible
8658 if it was hidden in the outline."
8659 (interactive "P")
8660 (let ((win (selected-window)))
8661 (org-agenda-goto t)
8662 (when full-entry (org-show-entry))
8663 (select-window win)))
8665 (defvar org-agenda-show-window nil)
8666 (defun org-agenda-show-and-scroll-up (&optional arg)
8667 "Display the Org file which contains the item at point.
8669 When called repeatedly, scroll the window that is displaying the buffer.
8671 With a `\\[universal-argument]' prefix, use `org-show-entry' instead of \
8672 `outline-show-subtree'
8673 to display the item, so that drawers and logbooks stay folded."
8674 (interactive "P")
8675 (let ((win (selected-window)))
8676 (if (and (window-live-p org-agenda-show-window)
8677 (eq this-command last-command))
8678 (progn
8679 (select-window org-agenda-show-window)
8680 (ignore-errors (scroll-up)))
8681 (org-agenda-goto t)
8682 (if arg (org-show-entry) (outline-show-subtree))
8683 (setq org-agenda-show-window (selected-window)))
8684 (select-window win)))
8686 (defun org-agenda-show-scroll-down ()
8687 "Scroll down the window showing the agenda."
8688 (interactive)
8689 (let ((win (selected-window)))
8690 (when (window-live-p org-agenda-show-window)
8691 (select-window org-agenda-show-window)
8692 (ignore-errors (scroll-down))
8693 (select-window win))))
8695 (defun org-agenda-show-1 (&optional more)
8696 "Display the Org file which contains the item at point.
8697 The prefix arg selects the amount of information to display:
8699 0 hide the subtree
8700 1 just show the entry according to defaults.
8701 2 show the children view
8702 3 show the subtree view
8703 4 show the entire subtree and any LOGBOOK drawers
8704 5 show the entire subtree and any drawers
8705 With prefix argument FULL-ENTRY, make the entire entry visible
8706 if it was hidden in the outline."
8707 (interactive "p")
8708 (let ((win (selected-window)))
8709 (org-agenda-goto t)
8710 (org-back-to-heading)
8711 (set-window-start (selected-window) (point-at-bol))
8712 (cond
8713 ((= more 0)
8714 (outline-hide-subtree)
8715 (save-excursion
8716 (org-back-to-heading)
8717 (run-hook-with-args 'org-cycle-hook 'folded))
8718 (message "Remote: FOLDED"))
8719 ((and (called-interactively-p 'any) (= more 1))
8720 (message "Remote: show with default settings"))
8721 ((= more 2)
8722 (outline-show-entry)
8723 (org-show-children)
8724 (save-excursion
8725 (org-back-to-heading)
8726 (run-hook-with-args 'org-cycle-hook 'children))
8727 (message "Remote: CHILDREN"))
8728 ((= more 3)
8729 (outline-show-subtree)
8730 (save-excursion
8731 (org-back-to-heading)
8732 (run-hook-with-args 'org-cycle-hook 'subtree))
8733 (message "Remote: SUBTREE"))
8734 ((= more 4)
8735 (outline-show-subtree)
8736 (save-excursion
8737 (org-back-to-heading)
8738 (org-cycle-hide-drawers 'subtree '("LOGBOOK")))
8739 (message "Remote: SUBTREE AND LOGBOOK"))
8740 ((> more 4)
8741 (outline-show-subtree)
8742 (message "Remote: SUBTREE AND ALL DRAWERS")))
8743 (select-window win)))
8745 (defvar org-agenda-cycle-counter nil)
8746 (defun org-agenda-cycle-show (&optional n)
8747 "Show the current entry in another window, with default settings.
8749 Default settings are taken from `org-show-context-detail'. When
8750 use repeatedly in immediate succession, the remote entry will
8751 cycle through visibility
8753 children -> subtree -> folded
8755 When called with a numeric prefix arg, that arg will be passed through to
8756 `org-agenda-show-1'. For the interpretation of that argument, see the
8757 docstring of `org-agenda-show-1'."
8758 (interactive "P")
8759 (if (integerp n)
8760 (setq org-agenda-cycle-counter n)
8761 (if (not (eq last-command this-command))
8762 (setq org-agenda-cycle-counter 1)
8763 (if (equal org-agenda-cycle-counter 0)
8764 (setq org-agenda-cycle-counter 2)
8765 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8766 (when (> org-agenda-cycle-counter 3)
8767 (setq org-agenda-cycle-counter 0)))))
8768 (org-agenda-show-1 org-agenda-cycle-counter))
8770 (defun org-agenda-recenter (arg)
8771 "Display the Org file which contains the item at point and recenter."
8772 (interactive "P")
8773 (let ((win (selected-window)))
8774 (org-agenda-goto t)
8775 (recenter arg)
8776 (select-window win)))
8778 (defun org-agenda-show-mouse (ev)
8779 "Display the Org file which contains the item at the mouse click."
8780 (interactive "e")
8781 (mouse-set-point ev)
8782 (org-agenda-show))
8784 (defun org-agenda-check-no-diary ()
8785 "Check if the entry is a diary link and abort if yes."
8786 (when (org-get-at-bol 'org-agenda-diary-link)
8787 (org-agenda-error)))
8789 (defun org-agenda-error ()
8790 "Throw an error when a command is not allowed in the agenda."
8791 (user-error "Command not allowed in this line"))
8793 (defun org-agenda-tree-to-indirect-buffer (arg)
8794 "Show the subtree corresponding to the current entry in an indirect buffer.
8795 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8797 With a numerical prefix ARG, go up to this level and then take that tree.
8798 With a negative numeric ARG, go up by this number of levels.
8800 With a `\\[universal-argument]' prefix, make a separate frame for this tree, \
8801 i.e. don't use
8802 the dedicated frame."
8803 (interactive "P")
8804 (if current-prefix-arg
8805 (org-agenda-do-tree-to-indirect-buffer arg)
8806 (let ((agenda-buffer (buffer-name))
8807 (agenda-window (selected-window))
8808 (indirect-window
8809 (and org-last-indirect-buffer
8810 (get-buffer-window org-last-indirect-buffer))))
8811 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8812 (unless (or (eq org-indirect-buffer-display 'new-frame)
8813 (eq org-indirect-buffer-display 'dedicated-frame))
8814 (unwind-protect
8815 (unless (and indirect-window (window-live-p indirect-window))
8816 (setq indirect-window (split-window agenda-window)))
8817 (and indirect-window (select-window indirect-window))
8818 (switch-to-buffer org-last-indirect-buffer :norecord)
8819 (fit-window-to-buffer indirect-window)))
8820 (select-window (get-buffer-window agenda-buffer))
8821 (setq org-agenda-last-indirect-buffer org-last-indirect-buffer))))
8823 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8824 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8825 (org-agenda-check-no-diary)
8826 (let* ((marker (or (org-get-at-bol 'org-marker)
8827 (org-agenda-error)))
8828 (buffer (marker-buffer marker))
8829 (pos (marker-position marker)))
8830 (with-current-buffer buffer
8831 (save-excursion
8832 (goto-char pos)
8833 (org-tree-to-indirect-buffer arg)))))
8835 (defvar org-last-heading-marker (make-marker)
8836 "Marker pointing to the headline that last changed its TODO state
8837 by a remote command from the agenda.")
8839 (defun org-agenda-todo-nextset ()
8840 "Switch TODO entry to next sequence."
8841 (interactive)
8842 (org-agenda-todo 'nextset))
8844 (defun org-agenda-todo-previousset ()
8845 "Switch TODO entry to previous sequence."
8846 (interactive)
8847 (org-agenda-todo 'previousset))
8849 (defun org-agenda-todo (&optional arg)
8850 "Cycle TODO state of line at point, also in Org file.
8851 This changes the line at point, all other lines in the agenda referring to
8852 the same tree node, and the headline of the tree node in the Org file."
8853 (interactive "P")
8854 (org-agenda-check-no-diary)
8855 (let* ((col (current-column))
8856 (marker (or (org-get-at-bol 'org-marker)
8857 (org-agenda-error)))
8858 (buffer (marker-buffer marker))
8859 (pos (marker-position marker))
8860 (hdmarker (org-get-at-bol 'org-hd-marker))
8861 (todayp (org-agenda-today-p (org-get-at-bol 'day)))
8862 (inhibit-read-only t)
8863 org-agenda-headline-snapshot-before-repeat newhead just-one)
8864 (org-with-remote-undo buffer
8865 (with-current-buffer buffer
8866 (widen)
8867 (goto-char pos)
8868 (org-show-context 'agenda)
8869 (let ((current-prefix-arg arg))
8870 (call-interactively 'org-todo))
8871 (and (bolp) (forward-char 1))
8872 (setq newhead (org-get-heading))
8873 (when (and (bound-and-true-p
8874 org-agenda-headline-snapshot-before-repeat)
8875 (not (equal org-agenda-headline-snapshot-before-repeat
8876 newhead))
8877 todayp)
8878 (setq newhead org-agenda-headline-snapshot-before-repeat
8879 just-one t))
8880 (save-excursion
8881 (org-back-to-heading)
8882 (move-marker org-last-heading-marker (point))))
8883 (beginning-of-line 1)
8884 (save-window-excursion
8885 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8886 (when (bound-and-true-p org-clock-out-when-done)
8887 (string-match (concat "^" (regexp-opt org-done-keywords-for-agenda))
8888 newhead)
8889 (org-agenda-unmark-clocking-task))
8890 (org-move-to-column col)
8891 (org-agenda-mark-clocking-task))))
8893 (defun org-agenda-add-note (&optional arg)
8894 "Add a time-stamped note to the entry at point."
8895 (interactive "P")
8896 (org-agenda-check-no-diary)
8897 (let* ((marker (or (org-get-at-bol 'org-marker)
8898 (org-agenda-error)))
8899 (buffer (marker-buffer marker))
8900 (pos (marker-position marker))
8901 (hdmarker (org-get-at-bol 'org-hd-marker))
8902 (inhibit-read-only t))
8903 (with-current-buffer buffer
8904 (widen)
8905 (goto-char pos)
8906 (org-show-context 'agenda)
8907 (org-add-note))))
8909 (defun org-agenda-change-all-lines (newhead hdmarker
8910 &optional fixface just-this)
8911 "Change all lines in the agenda buffer which match HDMARKER.
8912 The new content of the line will be NEWHEAD (as modified by
8913 `org-agenda-format-item'). HDMARKER is checked with
8914 `equal' against all `org-hd-marker' text properties in the file.
8915 If FIXFACE is non-nil, the face of each item is modified according to
8916 the new TODO state.
8917 If JUST-THIS is non-nil, change just the current line, not all.
8918 If FORCE-TAGS is non nil, the car of it returns the new tags."
8919 (let* ((inhibit-read-only t)
8920 (line (org-current-line))
8921 (org-agenda-buffer (current-buffer))
8922 (thetags (with-current-buffer (marker-buffer hdmarker)
8923 (org-get-tags hdmarker)))
8924 props m pl undone-face done-face finish new dotime level cat tags)
8925 (save-excursion
8926 (goto-char (point-max))
8927 (beginning-of-line 1)
8928 (while (not finish)
8929 (setq finish (bobp))
8930 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8931 (or (not just-this) (= (org-current-line) line))
8932 (equal m hdmarker))
8933 (setq props (text-properties-at (point))
8934 dotime (org-get-at-bol 'dotime)
8935 cat (org-get-at-eol 'org-category 1)
8936 level (org-get-at-bol 'level)
8937 tags thetags
8939 (let ((org-prefix-format-compiled
8940 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8941 org-prefix-format-compiled))
8942 (extra (org-get-at-bol 'extra)))
8943 (with-current-buffer (marker-buffer hdmarker)
8944 (org-with-wide-buffer
8945 (org-agenda-format-item extra newhead level cat tags dotime))))
8946 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8947 undone-face (org-get-at-bol 'undone-face)
8948 done-face (org-get-at-bol 'done-face))
8949 (beginning-of-line 1)
8950 (cond
8951 ((equal new "") (delete-region (point) (line-beginning-position 2)))
8952 ((looking-at ".*")
8953 ;; When replacing the whole line, preserve bulk mark
8954 ;; overlay, if any.
8955 (let ((mark (catch :overlay
8956 (dolist (o (overlays-in (point) (+ 2 (point))))
8957 (when (eq (overlay-get o 'type)
8958 'org-marked-entry-overlay)
8959 (throw :overlay o))))))
8960 (replace-match new t t)
8961 (beginning-of-line)
8962 (when mark (move-overlay mark (point) (+ 2 (point)))))
8963 (add-text-properties (point-at-bol) (point-at-eol) props)
8964 (when fixface
8965 (add-text-properties
8966 (point-at-bol) (point-at-eol)
8967 (list 'face
8968 (if org-last-todo-state-is-todo
8969 undone-face done-face))))
8970 (org-agenda-highlight-todo 'line)
8971 (beginning-of-line 1))
8972 (t (error "Line update did not work")))
8973 (save-restriction
8974 (narrow-to-region (point-at-bol) (point-at-eol))
8975 (org-agenda-finalize)))
8976 (beginning-of-line 0)))))
8978 (defun org-agenda-align-tags (&optional line)
8979 "Align all tags in agenda items to `org-agenda-tags-column'.
8980 When optional argument LINE is non-nil, align tags only on the
8981 current line."
8982 (let ((inhibit-read-only t)
8983 (org-agenda-tags-column (if (eq 'auto org-agenda-tags-column)
8984 (- (window-text-width))
8985 org-agenda-tags-column))
8986 (end (and line (line-end-position)))
8987 l c)
8988 (save-excursion
8989 (goto-char (if line (line-beginning-position) (point-min)))
8990 (while (re-search-forward org-tag-group-re end t)
8991 (add-text-properties
8992 (match-beginning 1) (match-end 1)
8993 (list 'face (delq nil (let ((prop (get-text-property
8994 (match-beginning 1) 'face)))
8995 (or (listp prop) (setq prop (list prop)))
8996 (if (memq 'org-tag prop)
8997 prop
8998 (cons 'org-tag prop))))))
8999 (setq l (- (match-end 1) (match-beginning 1))
9000 c (if (< org-agenda-tags-column 0)
9001 (- (abs org-agenda-tags-column) l)
9002 org-agenda-tags-column))
9003 (goto-char (match-beginning 1))
9004 (delete-region (save-excursion (skip-chars-backward " \t") (point))
9005 (point))
9006 (insert (org-add-props
9007 (make-string (max 1 (- c (current-column))) ?\s)
9008 (plist-put (copy-sequence (text-properties-at (point)))
9009 'face nil))))
9010 (goto-char (point-min))
9011 (org-font-lock-add-tag-faces (point-max)))))
9013 (defun org-agenda-priority-up ()
9014 "Increase the priority of line at point, also in Org file."
9015 (interactive)
9016 (org-agenda-priority 'up))
9018 (defun org-agenda-priority-down ()
9019 "Decrease the priority of line at point, also in Org file."
9020 (interactive)
9021 (org-agenda-priority 'down))
9023 (defun org-agenda-priority (&optional force-direction)
9024 "Set the priority of line at point, also in Org file.
9025 This changes the line at point, all other lines in the agenda referring to
9026 the same tree node, and the headline of the tree node in the Org file.
9027 Called with a universal prefix arg, show the priority instead of setting it."
9028 (interactive "P")
9029 (if (equal force-direction '(4))
9030 (org-show-priority)
9031 (unless org-enable-priority-commands
9032 (error "Priority commands are disabled"))
9033 (org-agenda-check-no-diary)
9034 (let* ((col (current-column))
9035 (marker (or (org-get-at-bol 'org-marker)
9036 (org-agenda-error)))
9037 (hdmarker (org-get-at-bol 'org-hd-marker))
9038 (buffer (marker-buffer hdmarker))
9039 (pos (marker-position hdmarker))
9040 (inhibit-read-only t)
9041 newhead)
9042 (org-with-remote-undo buffer
9043 (with-current-buffer buffer
9044 (widen)
9045 (goto-char pos)
9046 (org-show-context 'agenda)
9047 (org-priority force-direction)
9048 (end-of-line 1)
9049 (setq newhead (org-get-heading)))
9050 (org-agenda-change-all-lines newhead hdmarker)
9051 (org-move-to-column col)))))
9053 ;; FIXME: should fix the tags property of the agenda line.
9054 (defun org-agenda-set-tags (&optional tag onoff)
9055 "Set tags for the current headline."
9056 (interactive)
9057 (org-agenda-check-no-diary)
9058 (if (and (org-region-active-p) (called-interactively-p 'any))
9059 (call-interactively 'org-change-tag-in-region)
9060 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9061 (org-agenda-error)))
9062 (buffer (marker-buffer hdmarker))
9063 (pos (marker-position hdmarker))
9064 (inhibit-read-only t)
9065 newhead)
9066 (org-with-remote-undo buffer
9067 (with-current-buffer buffer
9068 (widen)
9069 (goto-char pos)
9070 (org-show-context 'agenda)
9071 (if tag
9072 (org-toggle-tag tag onoff)
9073 (call-interactively #'org-set-tags-command))
9074 (end-of-line 1)
9075 (setq newhead (org-get-heading)))
9076 (org-agenda-change-all-lines newhead hdmarker)
9077 (beginning-of-line 1)))))
9079 (defun org-agenda-set-property ()
9080 "Set a property for the current headline."
9081 (interactive)
9082 (org-agenda-check-no-diary)
9083 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9084 (org-agenda-error)))
9085 (buffer (marker-buffer hdmarker))
9086 (pos (marker-position hdmarker))
9087 (inhibit-read-only t)
9088 newhead)
9089 (org-with-remote-undo buffer
9090 (with-current-buffer buffer
9091 (widen)
9092 (goto-char pos)
9093 (org-show-context 'agenda)
9094 (call-interactively 'org-set-property)))))
9096 (defun org-agenda-set-effort ()
9097 "Set the effort property for the current headline."
9098 (interactive)
9099 (org-agenda-check-no-diary)
9100 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9101 (org-agenda-error)))
9102 (buffer (marker-buffer hdmarker))
9103 (pos (marker-position hdmarker))
9104 (inhibit-read-only t)
9105 newhead)
9106 (org-with-remote-undo buffer
9107 (with-current-buffer buffer
9108 (widen)
9109 (goto-char pos)
9110 (org-show-context 'agenda)
9111 (call-interactively 'org-set-effort)
9112 (end-of-line 1)
9113 (setq newhead (org-get-heading)))
9114 (org-agenda-change-all-lines newhead hdmarker))))
9116 (defun org-agenda-toggle-archive-tag ()
9117 "Toggle the archive tag for the current entry."
9118 (interactive)
9119 (org-agenda-check-no-diary)
9120 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9121 (org-agenda-error)))
9122 (buffer (marker-buffer hdmarker))
9123 (pos (marker-position hdmarker))
9124 (inhibit-read-only t)
9125 newhead)
9126 (org-with-remote-undo buffer
9127 (with-current-buffer buffer
9128 (widen)
9129 (goto-char pos)
9130 (org-show-context 'agenda)
9131 (call-interactively 'org-toggle-archive-tag)
9132 (end-of-line 1)
9133 (setq newhead (org-get-heading)))
9134 (org-agenda-change-all-lines newhead hdmarker)
9135 (beginning-of-line 1))))
9137 (defun org-agenda-do-date-later (arg)
9138 (interactive "P")
9139 (cond
9140 ((or (equal arg '(16))
9141 (memq last-command
9142 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9143 (setq this-command 'org-agenda-date-later-minutes)
9144 (org-agenda-date-later-minutes 1))
9145 ((or (equal arg '(4))
9146 (memq last-command
9147 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9148 (setq this-command 'org-agenda-date-later-hours)
9149 (org-agenda-date-later-hours 1))
9151 (org-agenda-date-later (prefix-numeric-value arg)))))
9153 (defun org-agenda-do-date-earlier (arg)
9154 (interactive "P")
9155 (cond
9156 ((or (equal arg '(16))
9157 (memq last-command
9158 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9159 (setq this-command 'org-agenda-date-earlier-minutes)
9160 (org-agenda-date-earlier-minutes 1))
9161 ((or (equal arg '(4))
9162 (memq last-command
9163 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9164 (setq this-command 'org-agenda-date-earlier-hours)
9165 (org-agenda-date-earlier-hours 1))
9167 (org-agenda-date-earlier (prefix-numeric-value arg)))))
9169 (defun org-agenda-date-later (arg &optional what)
9170 "Change the date of this item to ARG day(s) later."
9171 (interactive "p")
9172 (org-agenda-check-type t 'agenda)
9173 (org-agenda-check-no-diary)
9174 (let* ((marker (or (org-get-at-bol 'org-marker)
9175 (org-agenda-error)))
9176 (buffer (marker-buffer marker))
9177 (pos (marker-position marker))
9178 cdate today)
9179 (org-with-remote-undo buffer
9180 (with-current-buffer buffer
9181 (widen)
9182 (goto-char pos)
9183 (unless (org-at-timestamp-p 'lax) (error "Cannot find time stamp"))
9184 (when (and org-agenda-move-date-from-past-immediately-to-today
9185 (equal arg 1)
9186 (or (not what) (eq what 'day))
9187 (not (save-match-data (org-at-date-range-p))))
9188 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9189 cdate (calendar-absolute-from-gregorian
9190 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9191 today (org-today))
9192 (when (> today cdate)
9193 ;; immediately shift to today
9194 (setq arg (- today cdate))))
9195 (org-timestamp-change arg (or what 'day))
9196 (when (and (org-at-date-range-p)
9197 (re-search-backward org-tr-regexp-both (point-at-bol)))
9198 (let ((end org-last-changed-timestamp))
9199 (org-timestamp-change arg (or what 'day))
9200 (setq org-last-changed-timestamp
9201 (concat org-last-changed-timestamp "--" end)))))
9202 (org-agenda-show-new-time marker org-last-changed-timestamp))
9203 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9205 (defun org-agenda-date-earlier (arg &optional what)
9206 "Change the date of this item to ARG day(s) earlier."
9207 (interactive "p")
9208 (org-agenda-date-later (- arg) what))
9210 (defun org-agenda-date-later-minutes (arg)
9211 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9212 (interactive "p")
9213 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9214 (org-agenda-date-later arg 'minute))
9216 (defun org-agenda-date-earlier-minutes (arg)
9217 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9218 (interactive "p")
9219 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9220 (org-agenda-date-earlier arg 'minute))
9222 (defun org-agenda-date-later-hours (arg)
9223 "Change the time of this item, in hour steps."
9224 (interactive "p")
9225 (org-agenda-date-later arg 'hour))
9227 (defun org-agenda-date-earlier-hours (arg)
9228 "Change the time of this item, in hour steps."
9229 (interactive "p")
9230 (org-agenda-date-earlier arg 'hour))
9232 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9233 "Show new date stamp via text properties."
9234 ;; We use text properties to make this undoable
9235 (let ((inhibit-read-only t))
9236 (setq stamp (concat prefix " => " stamp " "))
9237 (save-excursion
9238 (goto-char (point-max))
9239 (while (not (bobp))
9240 (when (equal marker (org-get-at-bol 'org-marker))
9241 (remove-text-properties (point-at-bol) (point-at-eol) '(display))
9242 (org-move-to-column (- (window-width) (length stamp)) t)
9243 (add-text-properties
9244 (1- (point)) (point-at-eol)
9245 (list 'display (org-add-props stamp nil
9246 'face '(secondary-selection default))))
9247 (beginning-of-line 1))
9248 (beginning-of-line 0)))))
9250 (defun org-agenda-date-prompt (arg)
9251 "Change the date of this item. Date is prompted for, with default today.
9252 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9253 be used to request time specification in the time stamp."
9254 (interactive "P")
9255 (org-agenda-check-type t 'agenda)
9256 (org-agenda-check-no-diary)
9257 (let* ((marker (or (org-get-at-bol 'org-marker)
9258 (org-agenda-error)))
9259 (buffer (marker-buffer marker))
9260 (pos (marker-position marker)))
9261 (org-with-remote-undo buffer
9262 (with-current-buffer buffer
9263 (widen)
9264 (goto-char pos)
9265 (unless (org-at-timestamp-p 'lax) (error "Cannot find time stamp"))
9266 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
9267 (org-agenda-show-new-time marker org-last-changed-timestamp))
9268 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9270 (defun org-agenda-schedule (arg &optional time)
9271 "Schedule the item at point.
9272 ARG is passed through to `org-schedule'."
9273 (interactive "P")
9274 (org-agenda-check-type t 'agenda 'todo 'tags 'search)
9275 (org-agenda-check-no-diary)
9276 (let* ((marker (or (org-get-at-bol 'org-marker)
9277 (org-agenda-error)))
9278 (type (marker-insertion-type marker))
9279 (buffer (marker-buffer marker))
9280 (pos (marker-position marker))
9282 (set-marker-insertion-type marker t)
9283 (org-with-remote-undo buffer
9284 (with-current-buffer buffer
9285 (widen)
9286 (goto-char pos)
9287 (setq ts (org-schedule arg time)))
9288 (org-agenda-show-new-time marker ts " S"))
9289 (message "%s" ts)))
9291 (defun org-agenda-deadline (arg &optional time)
9292 "Schedule the item at point.
9293 ARG is passed through to `org-deadline'."
9294 (interactive "P")
9295 (org-agenda-check-type t 'agenda 'todo 'tags 'search)
9296 (org-agenda-check-no-diary)
9297 (let* ((marker (or (org-get-at-bol 'org-marker)
9298 (org-agenda-error)))
9299 (buffer (marker-buffer marker))
9300 (pos (marker-position marker))
9302 (org-with-remote-undo buffer
9303 (with-current-buffer buffer
9304 (widen)
9305 (goto-char pos)
9306 (setq ts (org-deadline arg time)))
9307 (org-agenda-show-new-time marker ts " D"))
9308 (message "%s" ts)))
9310 (defun org-agenda-clock-in (&optional arg)
9311 "Start the clock on the currently selected item."
9312 (interactive "P")
9313 (org-agenda-check-no-diary)
9314 (if (equal arg '(4))
9315 (org-clock-in arg)
9316 (let* ((marker (or (org-get-at-bol 'org-marker)
9317 (org-agenda-error)))
9318 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9319 (pos (marker-position marker))
9320 (col (current-column))
9321 newhead)
9322 (org-with-remote-undo (marker-buffer marker)
9323 (with-current-buffer (marker-buffer marker)
9324 (widen)
9325 (goto-char pos)
9326 (org-show-context 'agenda)
9327 (org-clock-in arg)
9328 (setq newhead (org-get-heading)))
9329 (org-agenda-change-all-lines newhead hdmarker))
9330 (org-move-to-column col))))
9332 (defun org-agenda-clock-out ()
9333 "Stop the currently running clock."
9334 (interactive)
9335 (unless (marker-buffer org-clock-marker)
9336 (error "No running clock"))
9337 (let ((marker (make-marker)) (col (current-column)) newhead)
9338 (org-with-remote-undo (marker-buffer org-clock-marker)
9339 (with-current-buffer (marker-buffer org-clock-marker)
9340 (org-with-wide-buffer
9341 (goto-char org-clock-marker)
9342 (org-back-to-heading t)
9343 (move-marker marker (point))
9344 (org-clock-out)
9345 (setq newhead (org-get-heading)))))
9346 (org-agenda-change-all-lines newhead marker)
9347 (move-marker marker nil)
9348 (org-move-to-column col)
9349 (org-agenda-unmark-clocking-task)))
9351 (defun org-agenda-clock-cancel (&optional arg)
9352 "Cancel the currently running clock."
9353 (interactive "P")
9354 (unless (marker-buffer org-clock-marker)
9355 (user-error "No running clock"))
9356 (org-with-remote-undo (marker-buffer org-clock-marker)
9357 (org-clock-cancel)))
9359 (defun org-agenda-clock-goto ()
9360 "Jump to the currently clocked in task within the agenda.
9361 If the currently clocked in task is not listed in the agenda
9362 buffer, display it in another window."
9363 (interactive)
9364 (let (pos)
9365 (mapc (lambda (o)
9366 (when (eq (overlay-get o 'type) 'org-agenda-clocking)
9367 (setq pos (overlay-start o))))
9368 (overlays-in (point-min) (point-max)))
9369 (cond (pos (goto-char pos))
9370 ;; If the currently clocked entry is not in the agenda
9371 ;; buffer, we visit it in another window:
9372 ((bound-and-true-p org-clock-current-task)
9373 (org-switch-to-buffer-other-window (org-clock-goto)))
9374 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9376 (defun org-agenda-diary-entry-in-org-file ()
9377 "Make a diary entry in the file `org-agenda-diary-file'."
9378 (let (d1 d2 char (text "") dp1 dp2)
9379 (if (equal (buffer-name) "*Calendar*")
9380 (setq d1 (calendar-cursor-to-date t)
9381 d2 (car calendar-mark-ring))
9382 (setq dp1 (get-text-property (point-at-bol) 'day))
9383 (unless dp1 (user-error "No date defined in current line"))
9384 (setq d1 (calendar-gregorian-from-absolute dp1)
9385 d2 (and (ignore-errors (mark))
9386 (save-excursion
9387 (goto-char (mark))
9388 (setq dp2 (get-text-property (point-at-bol) 'day)))
9389 (calendar-gregorian-from-absolute dp2))))
9390 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9391 (setq char (read-char-exclusive))
9392 (cond
9393 ((equal char ?d)
9394 (setq text (read-string "Day entry: "))
9395 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9396 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9397 ((equal char ?a)
9398 (setq d1 (list (car d1) (nth 1 d1)
9399 (read-number (format "Reference year [%d]: " (nth 2 d1))
9400 (nth 2 d1))))
9401 (setq text (read-string "Anniversary (use %d to show years): "))
9402 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9403 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9404 ((equal char ?b)
9405 (setq text (read-string "Block entry: "))
9406 (unless (and d1 d2 (not (equal d1 d2)))
9407 (user-error "No block of days selected"))
9408 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9409 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9410 ((equal char ?j)
9411 (org-switch-to-buffer-other-window
9412 (find-file-noselect org-agenda-diary-file))
9413 (require 'org-datetree)
9414 (org-datetree-find-date-create d1)
9415 (org-reveal t))
9416 (t (user-error "Invalid selection character `%c'" char)))))
9418 (defcustom org-agenda-insert-diary-strategy 'date-tree
9419 "Where in `org-agenda-diary-file' should new entries be added?
9420 Valid values:
9422 date-tree in the date tree, as first child of the date
9423 date-tree-last in the date tree, as last child of the date
9424 top-level as top-level entries at the end of the file."
9425 :group 'org-agenda
9426 :type '(choice
9427 (const :tag "first in a date tree" date-tree)
9428 (const :tag "last in a date tree" date-tree-last)
9429 (const :tag "as top level at end of file" top-level)))
9431 (defcustom org-agenda-insert-diary-extract-time nil
9432 "Non-nil means extract any time specification from the diary entry."
9433 :group 'org-agenda
9434 :version "24.1"
9435 :type 'boolean)
9437 (defcustom org-agenda-bulk-mark-char ">"
9438 "A single-character string to be used as the bulk mark."
9439 :group 'org-agenda
9440 :version "24.1"
9441 :type 'string)
9443 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9444 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9445 If TEXT is not empty, it will become the headline of the new entry, and
9446 the resulting entry will not be shown. When TEXT is empty, switch to
9447 `org-agenda-diary-file' and let the user finish the entry there."
9448 (let ((cw (current-window-configuration)))
9449 (org-switch-to-buffer-other-window
9450 (find-file-noselect org-agenda-diary-file))
9451 (widen)
9452 (goto-char (point-min))
9453 (cl-case type
9454 (anniversary
9455 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
9456 (progn
9457 (or (org-at-heading-p t)
9458 (progn
9459 (outline-next-heading)
9460 (insert "* Anniversaries\n\n")
9461 (beginning-of-line -1)))))
9462 (outline-next-heading)
9463 (org-back-over-empty-lines)
9464 (backward-char 1)
9465 (insert "\n")
9466 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9467 (nth 2 d1) (car d1) (nth 1 d1) text)))
9468 (day
9469 (let ((org-prefix-has-time t)
9470 (org-agenda-time-leading-zero t)
9471 fmt time time2)
9472 (when org-agenda-insert-diary-extract-time
9473 ;; Use org-agenda-format-item to parse text for a time-range and
9474 ;; remove it. FIXME: This is a hack, we should refactor
9475 ;; that function to make time extraction available separately
9476 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9477 time (get-text-property 0 'time fmt)
9478 time2 (if (> (length time) 0)
9479 ;; split-string removes trailing ...... if
9480 ;; no end time given. First space
9481 ;; separates time from date.
9482 (concat " " (car (split-string time "\\.")))
9483 nil)
9484 text (get-text-property 0 'txt fmt)))
9485 (if (eq org-agenda-insert-diary-strategy 'top-level)
9486 (org-agenda-insert-diary-as-top-level text)
9487 (require 'org-datetree)
9488 (org-datetree-find-date-create d1)
9489 (org-agenda-insert-diary-make-new-entry text))
9490 (org-insert-time-stamp (org-time-from-absolute
9491 (calendar-absolute-from-gregorian d1))
9492 nil nil nil nil time2))
9493 (end-of-line 0))
9494 ((block) ;; Wrap this in (strictly unnecessary) parens because
9495 ;; otherwise the indentation gets confused by the
9496 ;; special meaning of 'block
9497 (when (> (calendar-absolute-from-gregorian d1)
9498 (calendar-absolute-from-gregorian d2))
9499 (setq d1 (prog1 d2 (setq d2 d1))))
9500 (if (eq org-agenda-insert-diary-strategy 'top-level)
9501 (org-agenda-insert-diary-as-top-level text)
9502 (require 'org-datetree)
9503 (org-datetree-find-date-create d1)
9504 (org-agenda-insert-diary-make-new-entry text))
9505 (org-insert-time-stamp (org-time-from-absolute
9506 (calendar-absolute-from-gregorian d1)))
9507 (insert "--")
9508 (org-insert-time-stamp (org-time-from-absolute
9509 (calendar-absolute-from-gregorian d2)))
9510 (end-of-line 0)))
9511 (if (string-match "\\S-" text)
9512 (progn
9513 (set-window-configuration cw)
9514 (message "%s entry added to %s"
9515 (capitalize (symbol-name type))
9516 (abbreviate-file-name org-agenda-diary-file)))
9517 (org-reveal t)
9518 (message "Please finish entry here"))))
9520 (defun org-agenda-insert-diary-as-top-level (text)
9521 "Make new entry as a top-level entry at the end of the file.
9522 Add TEXT as headline, and position the cursor in the second line so that
9523 a timestamp can be added there."
9524 (widen)
9525 (goto-char (point-max))
9526 (unless (bolp) (insert "\n"))
9527 (org-insert-heading nil t t)
9528 (insert text)
9529 (org-end-of-meta-data)
9530 (unless (bolp) (insert "\n"))
9531 (when org-adapt-indentation (indent-to-column 2)))
9533 (defun org-agenda-insert-diary-make-new-entry (text)
9534 "Make a new entry with TEXT as a child of the current subtree.
9535 Position the point in the heading's first body line so that
9536 a timestamp can be added there."
9537 (cond
9538 ((eq org-agenda-insert-diary-strategy 'date-tree-last)
9539 (end-of-line)
9540 (org-insert-heading '(4) t)
9541 (org-do-demote))
9543 (outline-next-heading)
9544 (org-back-over-empty-lines)
9545 (unless (looking-at "[ \t]*$") (save-excursion (insert "\n")))
9546 (org-insert-heading nil t)
9547 (org-do-demote)))
9548 (let ((col (current-column)))
9549 (insert text)
9550 (org-end-of-meta-data)
9551 ;; Ensure point is left on a blank line, at proper indentation.
9552 (unless (bolp) (insert "\n"))
9553 (unless (looking-at-p "^[ \t]*$") (save-excursion (insert "\n")))
9554 (when org-adapt-indentation (indent-to-column col)))
9555 (org-show-set-visibility 'lineage))
9557 (defun org-agenda-diary-entry ()
9558 "Make a diary entry, like the `i' command from the calendar.
9559 All the standard commands work: block, weekly etc.
9560 When `org-agenda-diary-file' points to a file,
9561 `org-agenda-diary-entry-in-org-file' is called instead to create
9562 entries in that Org file."
9563 (interactive)
9564 (if (not (eq org-agenda-diary-file 'diary-file))
9565 (org-agenda-diary-entry-in-org-file)
9566 (require 'diary-lib)
9567 (let* ((char (read-char-exclusive
9568 "Diary entry: [d]ay [w]eekly [m]onthly [y]early\
9569 [a]nniversary [b]lock [c]yclic"))
9570 (cmd (cdr (assoc char
9571 '((?d . diary-insert-entry)
9572 (?w . diary-insert-weekly-entry)
9573 (?m . diary-insert-monthly-entry)
9574 (?y . diary-insert-yearly-entry)
9575 (?a . diary-insert-anniversary-entry)
9576 (?b . diary-insert-block-entry)
9577 (?c . diary-insert-cyclic-entry)))))
9578 (oldf (symbol-function 'calendar-cursor-to-date))
9579 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9580 (point (point))
9581 (mark (or (mark t) (point))))
9582 (unless cmd
9583 (user-error "No command associated with <%c>" char))
9584 (unless (and (get-text-property point 'day)
9585 (or (not (equal ?b char))
9586 (get-text-property mark 'day)))
9587 (user-error "Don't know which date to use for diary entry"))
9588 ;; We implement this by hacking the `calendar-cursor-to-date' function
9589 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9590 (let ((calendar-mark-ring
9591 (list (calendar-gregorian-from-absolute
9592 (or (get-text-property mark 'day)
9593 (get-text-property point 'day))))))
9594 (unwind-protect
9595 (progn
9596 (fset 'calendar-cursor-to-date
9597 (lambda (&optional error dummy)
9598 (calendar-gregorian-from-absolute
9599 (get-text-property point 'day))))
9600 (call-interactively cmd))
9601 (fset 'calendar-cursor-to-date oldf))))))
9603 (defun org-agenda-execute-calendar-command (cmd)
9604 "Execute a calendar command from the agenda with date from cursor."
9605 (org-agenda-check-type t 'agenda)
9606 (require 'diary-lib)
9607 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9608 (user-error "Don't know which date to use for the calendar command"))
9609 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9610 (point (point))
9611 (date (calendar-gregorian-from-absolute
9612 (get-text-property point 'day)))
9613 ;; the following 2 vars are needed in the calendar
9614 (displayed-month (car date))
9615 (displayed-year (nth 2 date)))
9616 (unwind-protect
9617 (progn
9618 (fset 'calendar-cursor-to-date
9619 (lambda (&optional error dummy)
9620 (calendar-gregorian-from-absolute
9621 (get-text-property point 'day))))
9622 (call-interactively cmd))
9623 (fset 'calendar-cursor-to-date oldf))))
9625 (defun org-agenda-phases-of-moon ()
9626 "Display the phases of the moon for the 3 months around the cursor date."
9627 (interactive)
9628 (org-agenda-execute-calendar-command 'calendar-lunar-phases))
9630 (defun org-agenda-holidays ()
9631 "Display the holidays for the 3 months around the cursor date."
9632 (interactive)
9633 (org-agenda-execute-calendar-command 'calendar-list-holidays))
9635 (defvar calendar-longitude) ; defined in calendar.el
9636 (defvar calendar-latitude) ; defined in calendar.el
9637 (defvar calendar-location-name) ; defined in calendar.el
9639 (defun org-agenda-sunrise-sunset (arg)
9640 "Display sunrise and sunset for the cursor date.
9641 Latitude and longitude can be specified with the variables
9642 `calendar-latitude' and `calendar-longitude'. When called with prefix
9643 argument, latitude and longitude will be prompted for."
9644 (interactive "P")
9645 (require 'solar)
9646 (let ((calendar-longitude (if arg nil calendar-longitude))
9647 (calendar-latitude (if arg nil calendar-latitude))
9648 (calendar-location-name
9649 (if arg "the given coordinates" calendar-location-name)))
9650 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9652 (defun org-agenda-goto-calendar ()
9653 "Open the Emacs calendar with the date at the cursor."
9654 (interactive)
9655 (org-agenda-check-type t 'agenda)
9656 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9657 (user-error "Don't know which date to open in calendar")))
9658 (date (calendar-gregorian-from-absolute day))
9659 (calendar-move-hook nil)
9660 (calendar-view-holidays-initially-flag nil)
9661 (calendar-view-diary-initially-flag nil))
9662 (calendar)
9663 (calendar-goto-date date)))
9665 ;;;###autoload
9666 (defun org-calendar-goto-agenda ()
9667 "Compute the Org agenda for the calendar date displayed at the cursor.
9668 This is a command that has to be installed in `calendar-mode-map'."
9669 (interactive)
9670 ;; Temporarily disable sticky agenda since user clearly wants to
9671 ;; refresh view anyway.
9672 (let ((org-agenda-buffer-tmp-name "*Org Agenda(a)*")
9673 (org-agenda-sticky nil))
9674 (org-agenda-list nil (calendar-absolute-from-gregorian
9675 (calendar-cursor-to-date))
9676 nil)))
9678 (defun org-agenda-convert-date ()
9679 (interactive)
9680 (org-agenda-check-type t 'agenda)
9681 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9682 date s)
9683 (unless day
9684 (user-error "Don't know which date to convert"))
9685 (setq date (calendar-gregorian-from-absolute day))
9686 (setq s (concat
9687 "Gregorian: " (calendar-date-string date) "\n"
9688 "ISO: " (calendar-iso-date-string date) "\n"
9689 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9690 "Julian: " (calendar-julian-date-string date) "\n"
9691 "Astron. JD: " (calendar-astro-date-string date)
9692 " (Julian date number at noon UTC)\n"
9693 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9694 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9695 "French: " (calendar-french-date-string date) "\n"
9696 "Bahá’í: " (calendar-bahai-date-string date) " (until sunset)\n"
9697 "Mayan: " (calendar-mayan-date-string date) "\n"
9698 "Coptic: " (calendar-coptic-date-string date) "\n"
9699 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9700 "Persian: " (calendar-persian-date-string date) "\n"
9701 "Chinese: " (calendar-chinese-date-string date) "\n"))
9702 (with-output-to-temp-buffer "*Dates*"
9703 (princ s))
9704 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9706 ;;; Bulk commands
9708 (defun org-agenda-bulk-marked-p ()
9709 "Non-nil when current entry is marked for bulk action."
9710 (eq (get-char-property (point-at-bol) 'type)
9711 'org-marked-entry-overlay))
9713 (defun org-agenda-bulk-mark (&optional arg)
9714 "Mark entries for future bulk action.
9716 When ARG is nil or one and region is not active then mark the
9717 entry at point.
9719 When ARG is nil or one and region is active then mark the entries
9720 in the region.
9722 When ARG is greater than one mark ARG lines."
9723 (interactive "p")
9724 (when (and (or (not arg) (= arg 1)) (use-region-p))
9725 (setq arg (count-lines (region-beginning) (region-end)))
9726 (goto-char (region-beginning))
9727 (deactivate-mark))
9728 (dotimes (i (or arg 1))
9729 (unless (org-get-at-bol 'org-agenda-diary-link)
9730 (let* ((m (org-get-at-bol 'org-hd-marker))
9732 (unless (org-agenda-bulk-marked-p)
9733 (unless m (user-error "Nothing to mark at point"))
9734 (push m org-agenda-bulk-marked-entries)
9735 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9736 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9737 (org-get-todo-face "TODO")
9738 'evaporate)
9739 (overlay-put ov 'type 'org-marked-entry-overlay))
9740 (end-of-line 1)
9741 (or (ignore-errors
9742 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9743 (beginning-of-line 2))
9744 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9745 (beginning-of-line 2)))))
9746 (message "%d entries marked for bulk action"
9747 (length org-agenda-bulk-marked-entries)))
9749 (defun org-agenda-bulk-mark-all ()
9750 "Mark all entries for future agenda bulk action."
9751 (interactive)
9752 (org-agenda-bulk-mark-regexp "."))
9754 (defun org-agenda-bulk-mark-regexp (regexp)
9755 "Mark entries matching REGEXP for future agenda bulk action."
9756 (interactive "sMark entries matching regexp: ")
9757 (let ((entries-marked 0) txt-at-point)
9758 (save-excursion
9759 (goto-char (point-min))
9760 (goto-char (next-single-property-change (point) 'org-hd-marker))
9761 (while (and (re-search-forward regexp nil t)
9762 (setq txt-at-point (get-text-property (point) 'txt)))
9763 (if (get-char-property (point) 'invisible)
9764 (beginning-of-line 2)
9765 (when (string-match regexp txt-at-point)
9766 (setq entries-marked (1+ entries-marked))
9767 (call-interactively 'org-agenda-bulk-mark)))))
9769 (unless entries-marked
9770 (message "No entry matching this regexp."))))
9772 (defun org-agenda-bulk-unmark (&optional arg)
9773 "Unmark the entry at point for future bulk action."
9774 (interactive "P")
9775 (if arg
9776 (org-agenda-bulk-unmark-all)
9777 (cond ((org-agenda-bulk-marked-p)
9778 (org-agenda-bulk-remove-overlays
9779 (point-at-bol) (+ 2 (point-at-bol)))
9780 (setq org-agenda-bulk-marked-entries
9781 (delete (org-get-at-bol 'org-hd-marker)
9782 org-agenda-bulk-marked-entries))
9783 (end-of-line 1)
9784 (or (ignore-errors
9785 (goto-char (next-single-property-change (point) 'txt)))
9786 (beginning-of-line 2))
9787 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9788 (beginning-of-line 2))
9789 (message "%d entries left marked for bulk action"
9790 (length org-agenda-bulk-marked-entries)))
9791 (t (message "No entry to unmark here")))))
9793 (defun org-agenda-bulk-toggle-all ()
9794 "Toggle all marks for bulk action."
9795 (interactive)
9796 (save-excursion
9797 (goto-char (point-min))
9798 (while (ignore-errors
9799 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9800 (org-agenda-bulk-toggle))))
9802 (defun org-agenda-bulk-toggle ()
9803 "Toggle the mark at point for bulk action."
9804 (interactive)
9805 (if (org-agenda-bulk-marked-p)
9806 (org-agenda-bulk-unmark)
9807 (org-agenda-bulk-mark)))
9809 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9810 "Remove the mark overlays between BEG and END in the agenda buffer.
9811 BEG and END default to the buffer limits.
9813 This only removes the overlays, it does not remove the markers
9814 from the list in `org-agenda-bulk-marked-entries'."
9815 (interactive)
9816 (mapc (lambda (ov)
9817 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9818 (delete-overlay ov)))
9819 (overlays-in (or beg (point-min)) (or end (point-max)))))
9821 (defun org-agenda-bulk-unmark-all ()
9822 "Remove all marks in the agenda buffer.
9823 This will remove the markers and the overlays."
9824 (interactive)
9825 (if (null org-agenda-bulk-marked-entries)
9826 (message "No entry to unmark")
9827 (setq org-agenda-bulk-marked-entries nil)
9828 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9830 (defcustom org-agenda-persistent-marks nil
9831 "Non-nil means marked items will stay marked after a bulk action.
9832 You can toggle this interactively by typing `p' when prompted for a
9833 bulk action."
9834 :group 'org-agenda
9835 :version "24.1"
9836 :type 'boolean)
9838 (defun org-agenda-bulk-action (&optional arg)
9839 "Execute an remote-editing action on all marked entries.
9840 The prefix arg is passed through to the command if possible."
9841 (interactive "P")
9842 ;; When there is no mark, act on the agenda entry at point.
9843 (if (not org-agenda-bulk-marked-entries)
9844 (save-excursion (org-agenda-bulk-mark)))
9845 (dolist (m org-agenda-bulk-marked-entries)
9846 (unless (and (markerp m)
9847 (marker-buffer m)
9848 (buffer-live-p (marker-buffer m))
9849 (marker-position m))
9850 (user-error "Marker %s for bulk command is invalid" m)))
9852 ;; Prompt for the bulk command.
9853 (org-unlogged-message
9854 (concat (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")
9855 "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9856 "[S]catter [f]unction "
9857 (and org-agenda-bulk-custom-functions
9858 (format " Custom: [%s]"
9859 (mapconcat (lambda (f) (char-to-string (car f)))
9860 org-agenda-bulk-custom-functions
9861 "")))))
9862 (catch 'exit
9863 (let* ((org-log-refile (if org-log-refile 'time nil))
9864 (entries (reverse org-agenda-bulk-marked-entries))
9865 (org-overriding-default-time
9866 (and (get-text-property (point) 'org-agenda-date-header)
9867 (org-get-cursor-date)))
9868 redo-at-end
9869 cmd)
9870 (pcase (read-char-exclusive)
9872 (let ((org-agenda-persistent-marks
9873 (not org-agenda-persistent-marks)))
9874 (org-agenda-bulk-action)
9875 (throw 'exit nil)))
9878 (setq cmd #'org-agenda-archive))
9881 (setq cmd #'org-agenda-archive-to-archive-sibling))
9883 ((or ?r ?w)
9884 (let ((refile-location
9885 (org-refile-get-location
9886 "Refile to"
9887 (marker-buffer (car entries))
9888 org-refile-allow-creating-parent-nodes)))
9889 (when (nth 3 refile-location)
9890 (setcar (nthcdr 3 refile-location)
9891 (move-marker
9892 (make-marker)
9893 (nth 3 refile-location)
9894 (or (get-file-buffer (nth 1 refile-location))
9895 (find-buffer-visiting (nth 1 refile-location))
9896 (error "This should not happen")))))
9898 (setq cmd `(lambda () (org-agenda-refile nil ',refile-location t)))
9899 (setq redo-at-end t)))
9902 (let ((state (completing-read
9903 "Todo state: "
9904 (with-current-buffer (marker-buffer (car entries))
9905 (mapcar #'list org-todo-keywords-1)))))
9906 (setq cmd `(lambda ()
9907 (let ((org-inhibit-blocking t)
9908 (org-inhibit-logging 'note))
9909 (org-agenda-todo ,state))))))
9911 ((and (or ?- ?+) action)
9912 (let ((tag (completing-read
9913 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9914 (with-current-buffer (marker-buffer (car entries))
9915 (delq nil
9916 (mapcar (lambda (x) (and (stringp (car x)) x))
9917 org-current-tag-alist))))))
9918 (setq cmd
9919 `(lambda ()
9920 (org-agenda-set-tags ,tag
9921 ,(if (eq action ?+) ''on ''off))))))
9923 ((and (or ?s ?d) c)
9924 (let* ((schedule? (eq c ?s))
9925 (prompt (if schedule? "(Re)Schedule to" "(Re)Set Deadline to"))
9926 (time
9927 (and (not arg)
9928 (let ((new (org-read-date
9929 nil nil nil prompt org-overriding-default-time)))
9930 ;; A "double plus" answer applies to every
9931 ;; scheduled time. Do not turn it into
9932 ;; a fixed date yet.
9933 (if (string-match-p "\\`[ \t]*\\+\\+"
9934 org-read-date-final-answer)
9935 org-read-date-final-answer
9936 new)))))
9937 ;; Make sure to not prompt for a note when bulk
9938 ;; rescheduling/resetting deadline as Org cannot cope with
9939 ;; simultaneous notes. Besides, it could be annoying
9940 ;; depending on the number of marked items.
9941 (setq cmd
9942 (if schedule?
9943 `(lambda ()
9944 (let ((org-log-reschedule
9945 (and org-log-reschedule 'time)))
9946 (org-agenda-schedule arg ,time)))
9947 `(lambda ()
9948 (let ((org-log-redeadline (and org-log-redeadline 'time)))
9949 (org-agenda-deadline arg ,time)))))))
9952 (unless (org-agenda-check-type nil 'agenda 'todo)
9953 (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type))
9954 (let ((days (read-number
9955 (format "Scatter tasks across how many %sdays: "
9956 (if arg "week" ""))
9957 7)))
9958 (setq cmd
9959 `(lambda ()
9960 (let ((distance (1+ (random ,days))))
9961 (when arg
9962 (let ((dist distance)
9963 (day-of-week
9964 (calendar-day-of-week
9965 (calendar-gregorian-from-absolute (org-today)))))
9966 (dotimes (i (1+ dist))
9967 (while (member day-of-week org-agenda-weekend-days)
9968 (cl-incf distance)
9969 (cl-incf day-of-week)
9970 (when (= day-of-week 7)
9971 (setq day-of-week 0)))
9972 (cl-incf day-of-week)
9973 (when (= day-of-week 7)
9974 (setq day-of-week 0)))))
9975 ;; Silently fail when try to replan a sexp entry.
9976 (ignore-errors
9977 (let* ((date (calendar-gregorian-from-absolute
9978 (+ (org-today) distance)))
9979 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9980 (nth 2 date))))
9981 (org-agenda-schedule nil time))))))))
9984 (setq cmd
9985 (intern
9986 (completing-read "Function: " obarray #'fboundp t nil nil))))
9988 (action
9989 (pcase (assoc action org-agenda-bulk-custom-functions)
9990 (`(,_ ,f) (setq cmd f) (setq redo-at-end t))
9991 (_ (user-error "Invalid bulk action: %c" action)))))
9993 ;; Sort the markers, to make sure that parents are handled
9994 ;; before children.
9995 (setq entries (sort entries
9996 (lambda (a b)
9997 (cond
9998 ((eq (marker-buffer a) (marker-buffer b))
9999 (< (marker-position a) (marker-position b)))
10001 (string< (buffer-name (marker-buffer a))
10002 (buffer-name (marker-buffer b))))))))
10004 ;; Now loop over all markers and apply CMD.
10005 (let ((processed 0)
10006 (skipped 0))
10007 (dolist (e entries)
10008 (let ((pos (text-property-any (point-min) (point-max) 'org-hd-marker e)))
10009 (if (not pos)
10010 (progn (message "Skipping removed entry at %s" e)
10011 (cl-incf skipped))
10012 (goto-char pos)
10013 (let (org-loop-over-headlines-in-active-region) (funcall cmd))
10014 ;; `post-command-hook' is not run yet. We make sure any
10015 ;; pending log note is processed.
10016 (when (or (memq 'org-add-log-note (default-value 'post-command-hook))
10017 (memq 'org-add-log-note post-command-hook))
10018 (org-add-log-note))
10019 (cl-incf processed))))
10020 (when redo-at-end (org-agenda-redo))
10021 (unless org-agenda-persistent-marks (org-agenda-bulk-unmark-all))
10022 (message "Acted on %d entries%s%s"
10023 processed
10024 (if (= skipped 0)
10026 (format ", skipped %d (disappeared before their turn)"
10027 skipped))
10028 (if (not org-agenda-persistent-marks) "" " (kept marked)"))))))
10030 (defun org-agenda-capture (&optional with-time)
10031 "Call `org-capture' with the date at point.
10032 With a `C-1' prefix, use the HH:MM value at point (if any) or the
10033 current HH:MM time."
10034 (interactive "P")
10035 (if (not (eq major-mode 'org-agenda-mode))
10036 (user-error "You cannot do this outside of agenda buffers")
10037 (let ((org-overriding-default-time
10038 (org-get-cursor-date (equal with-time 1))))
10039 (call-interactively 'org-capture))))
10041 ;;; Dragging agenda lines forward/backward
10043 (defun org-agenda-reapply-filters ()
10044 "Re-apply all agenda filters."
10045 (mapcar
10046 (lambda(f) (when (car f) (org-agenda-filter-apply (car f) (cadr f) t)))
10047 `((,org-agenda-tag-filter tag)
10048 (,org-agenda-category-filter category)
10049 (,org-agenda-regexp-filter regexp)
10050 (,org-agenda-effort-filter effort)
10051 (,(get 'org-agenda-tag-filter :preset-filter) tag)
10052 (,(get 'org-agenda-category-filter :preset-filter) category)
10053 (,(get 'org-agenda-effort-filter :preset-filter) effort)
10054 (,(get 'org-agenda-regexp-filter :preset-filter) regexp))))
10056 (defun org-agenda-drag-line-forward (arg &optional backward)
10057 "Drag an agenda line forward by ARG lines.
10058 When the optional argument `backward' is non-nil, move backward."
10059 (interactive "p")
10060 (let ((inhibit-read-only t) lst line)
10061 (if (or (not (get-text-property (point) 'txt))
10062 (save-excursion
10063 (dotimes (n arg)
10064 (move-beginning-of-line (if backward 0 2))
10065 (push (not (get-text-property (point) 'txt)) lst))
10066 (delq nil lst)))
10067 (message "Cannot move line forward")
10068 (let ((end (save-excursion (move-beginning-of-line 2) (point))))
10069 (move-beginning-of-line 1)
10070 (setq line (buffer-substring (point) end))
10071 (delete-region (point) end)
10072 (move-beginning-of-line (funcall (if backward '1- '1+) arg))
10073 (insert line)
10074 (org-agenda-reapply-filters)
10075 (org-agenda-mark-clocking-task)
10076 (move-beginning-of-line 0)))))
10078 (defun org-agenda-drag-line-backward (arg)
10079 "Drag an agenda line backward by ARG lines."
10080 (interactive "p")
10081 (org-agenda-drag-line-forward arg t))
10083 ;;; Flagging notes
10085 (defun org-agenda-show-the-flagging-note ()
10086 "Display the flagging note in the other window.
10087 When called a second time in direct sequence, offer to remove the FLAGGING
10088 tag and (if present) the flagging note."
10089 (interactive)
10090 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
10091 (win (selected-window))
10092 note heading newhead)
10093 (unless hdmarker
10094 (user-error "No linked entry at point"))
10095 (if (and (eq this-command last-command)
10096 (y-or-n-p "Unflag and remove any flagging note? "))
10097 (progn
10098 (org-agenda-remove-flag hdmarker)
10099 (let ((win (get-buffer-window "*Flagging Note*")))
10100 (and win (delete-window win)))
10101 (message "Entry unflagged"))
10102 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
10103 (unless note
10104 (user-error "No flagging note"))
10105 (org-kill-new note)
10106 (org-switch-to-buffer-other-window "*Flagging Note*")
10107 (erase-buffer)
10108 (insert note)
10109 (goto-char (point-min))
10110 (while (re-search-forward "\\\\n" nil t)
10111 (replace-match "\n" t t))
10112 (goto-char (point-min))
10113 (select-window win)
10114 (message "%s" (substitute-command-keys "Flagging note pushed to \
10115 kill ring. Press `\\[org-agenda-show-the-flagging-note]' again to remove \
10116 tag and note")))))
10118 (defun org-agenda-remove-flag (marker)
10119 "Remove the FLAGGED tag and any flagging note in the entry."
10120 (let (newhead)
10121 (org-with-point-at marker
10122 (org-toggle-tag "FLAGGED" 'off)
10123 (org-entry-delete nil "THEFLAGGINGNOTE")
10124 (setq newhead (org-get-heading)))
10125 (org-agenda-change-all-lines newhead marker)
10126 (message "Entry unflagged")))
10128 (defun org-agenda-get-any-marker (&optional pos)
10129 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
10130 (get-text-property (or pos (point-at-bol)) 'org-marker)))
10132 ;;; Appointment reminders
10134 (defvar appt-time-msg-list) ; defined in appt.el
10136 ;;;###autoload
10137 (defun org-agenda-to-appt (&optional refresh filter &rest args)
10138 "Activate appointments found in `org-agenda-files'.
10140 With a `\\[universal-argument]' prefix, refresh the list of \
10141 appointments.
10143 If FILTER is t, interactively prompt the user for a regular
10144 expression, and filter out entries that don't match it.
10146 If FILTER is a string, use this string as a regular expression
10147 for filtering entries out.
10149 If FILTER is a function, filter out entries against which
10150 calling the function returns nil. This function takes one
10151 argument: an entry from `org-agenda-get-day-entries'.
10153 FILTER can also be an alist with the car of each cell being
10154 either `headline' or `category'. For example:
10156 \\='((headline \"IMPORTANT\")
10157 (category \"Work\"))
10159 will only add headlines containing IMPORTANT or headlines
10160 belonging to the \"Work\" category.
10162 ARGS are symbols indicating what kind of entries to consider.
10163 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
10164 \(i.e., deadlines and scheduled items with a hh:mm specification)
10165 and :timestamp entries. See the docstring of `org-diary' for
10166 details and examples.
10168 If an entry has a APPT_WARNTIME property, its value will be used
10169 to override `appt-message-warning-time'."
10170 (interactive "P")
10171 (when refresh (setq appt-time-msg-list nil))
10172 (when (eq filter t)
10173 (setq filter (read-from-minibuffer "Regexp filter: ")))
10174 (let* ((cnt 0) ; count added events
10175 (scope (or args '(:deadline* :scheduled* :timestamp)))
10176 (org-agenda-new-buffers nil)
10177 (org-deadline-warning-days 0)
10178 ;; Do not use `org-today' here because appt only takes
10179 ;; time and without date as argument, so it may pass wrong
10180 ;; information otherwise
10181 (today (org-date-to-gregorian
10182 (time-to-days (current-time))))
10183 (org-agenda-restrict nil)
10184 (files (org-agenda-files 'unrestricted)) entries file
10185 (org-agenda-buffer nil))
10186 ;; Get all entries which may contain an appt
10187 (org-agenda-prepare-buffers files)
10188 (while (setq file (pop files))
10189 (setq entries
10190 (delq nil
10191 (append entries
10192 (apply 'org-agenda-get-day-entries
10193 file today scope)))))
10194 ;; Map thru entries and find if we should filter them out
10195 (mapc
10196 (lambda (x)
10197 (let* ((evt (org-trim
10198 (replace-regexp-in-string
10199 org-bracket-link-regexp "\\3"
10200 (or (get-text-property 1 'txt x) ""))))
10201 (cat (get-text-property (1- (length x)) 'org-category x))
10202 (tod (get-text-property 1 'time-of-day x))
10203 (ok (or (null filter)
10204 (and (stringp filter) (string-match filter evt))
10205 (and (functionp filter) (funcall filter x))
10206 (and (listp filter)
10207 (let ((cat-filter (cadr (assq 'category filter)))
10208 (evt-filter (cadr (assq 'headline filter))))
10209 (or (and (stringp cat-filter)
10210 (string-match cat-filter cat))
10211 (and (stringp evt-filter)
10212 (string-match evt-filter evt)))))))
10213 (wrn (get-text-property 1 'warntime x)))
10214 ;; FIXME: Shall we remove text-properties for the appt text?
10215 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
10216 (when (and ok tod (not (string-match "\\`DONE\\|CANCELLED" evt)))
10217 (setq tod (concat "00" (number-to-string tod)))
10218 (setq tod (when (string-match
10219 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
10220 (concat (match-string 1 tod) ":"
10221 (match-string 2 tod))))
10222 (when (appt-add tod evt wrn)
10223 (setq cnt (1+ cnt))))))
10224 entries)
10225 (org-release-buffers org-agenda-new-buffers)
10226 (if (eq cnt 0)
10227 (message "No event to add")
10228 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
10230 (defun org-agenda-today-p (date)
10231 "Non nil when DATE means today.
10232 DATE is either a list of the form (month day year) or a number of
10233 days as returned by `calendar-absolute-from-gregorian' or
10234 `org-today'. This function considers `org-extend-today-until'
10235 when defining today."
10236 (eq (org-today)
10237 (if (consp date) (calendar-absolute-from-gregorian date) date)))
10239 (defun org-agenda-todo-yesterday (&optional arg)
10240 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
10241 (interactive "P")
10242 (let* ((org-use-effective-time t)
10243 (hour (nth 2 (decode-time (org-current-time))))
10244 (org-extend-today-until (1+ hour)))
10245 (org-agenda-todo arg)))
10247 (provide 'org-agenda)
10249 ;;; org-agenda.el ends here