Merge branch 'maint'
[org-mode.git] / lisp / org-agenda.el
blob8f6459622a3d09ff527c2165c756b782ba047590
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004-2017 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <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.
1647 An alist with five entries, each for the different agenda types. The
1648 keys of the sublists are `agenda', `todo', `search' and `tags'.
1649 The values are format strings.
1651 This format works similar to a printf format, with the following meaning:
1653 %c the category of the item, \"Diary\" for entries from the diary,
1654 or as given by the CATEGORY keyword or derived from the file name
1655 %e the effort required by the item
1656 %l the level of the item (insert X space(s) if item is of level X)
1657 %i the icon category of the item, see `org-agenda-category-icon-alist'
1658 %T the last tag of the item (ignore inherited tags, which come first)
1659 %t the HH:MM time-of-day specification if one applies to the entry
1660 %s Scheduling/Deadline information, a short string
1661 %b show breadcrumbs, i.e., the names of the higher levels
1662 %(expression) Eval EXPRESSION and replace the control string
1663 by the result
1665 All specifiers work basically like the standard `%s' of printf, but may
1666 contain two additional characters: a question mark just after the `%'
1667 and a whitespace/punctuation character just before the final letter.
1669 If the first character after `%' is a question mark, the entire field
1670 will only be included if the corresponding value applies to the current
1671 entry. This is useful for fields which should have fixed width when
1672 present, but zero width when absent. For example, \"%?-12t\" will
1673 result in a 12 character time field if a time of the day is specified,
1674 but will completely disappear in entries which do not contain a time.
1676 If there is punctuation or whitespace character just before the
1677 final format letter, this character will be appended to the field
1678 value if the value is not empty. For example, the format
1679 \"%-12:c\" leads to \"Diary: \" if the category is \"Diary\". If
1680 the category is empty, no additional colon is inserted.
1682 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1683 which means:
1685 - Indent the line with two space characters
1686 - Give the category a 12 chars wide field, padded with whitespace on
1687 the right (because of `-'). Append a colon if there is a category
1688 (because of `:').
1689 - If there is a time-of-day, put it into a 12 chars wide field. If no
1690 time, don't put in an empty field, just skip it (because of '?').
1691 - Finally, put the scheduling information.
1693 See also the variables `org-agenda-remove-times-when-in-prefix' and
1694 `org-agenda-remove-tags'.
1696 Custom commands can set this variable in the options section."
1697 :type '(choice
1698 (string :tag "General format")
1699 (list :greedy t :tag "View dependent"
1700 (cons (const agenda) (string :tag "Format"))
1701 (cons (const todo) (string :tag "Format"))
1702 (cons (const tags) (string :tag "Format"))
1703 (cons (const search) (string :tag "Format"))))
1704 :group 'org-agenda-line-format
1705 :version "26.1"
1706 :package-version '(Org . "9.1"))
1708 (defvar org-prefix-format-compiled nil
1709 "The compiled prefix format and associated variables.
1710 This is a list where first element is a list of variable bindings, and second
1711 element is the compiled format expression. See the variable
1712 `org-agenda-prefix-format'.")
1714 (defcustom org-agenda-todo-keyword-format "%-1s"
1715 "Format for the TODO keyword in agenda lines.
1716 Set this to something like \"%-12s\" if you want all TODO keywords
1717 to occupy a fixed space in the agenda display."
1718 :group 'org-agenda-line-format
1719 :type 'string)
1721 (defcustom org-agenda-diary-sexp-prefix nil
1722 "A regexp that matches part of a diary sexp entry
1723 which should be treated as scheduling/deadline information in
1724 `org-agenda'.
1726 For example, you can use this to extract the `diary-remind-message' from
1727 `diary-remind' entries."
1728 :group 'org-agenda-line-format
1729 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1731 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1732 "Text preceding timerange entries in the agenda view.
1733 This is a list with two strings. The first applies when the range
1734 is entirely on one day. The second applies if the range spans several days.
1735 The strings may have two \"%d\" format specifiers which will be filled
1736 with the sequence number of the days, and the total number of days in the
1737 range, respectively."
1738 :group 'org-agenda-line-format
1739 :type '(list
1740 (string :tag "Deadline today ")
1741 (choice :tag "Deadline relative"
1742 (string :tag "Format string")
1743 (function))))
1745 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1746 "Text preceding scheduled items in the agenda view.
1747 This is a list with two strings. The first applies when the item is
1748 scheduled on the current day. The second applies when it has been scheduled
1749 previously, it may contain a %d indicating that this is the nth time that
1750 this item is scheduled, due to automatic rescheduling of unfinished items
1751 for the following day. So this number is one larger than the number of days
1752 that passed since this item was scheduled first."
1753 :group 'org-agenda-line-format
1754 :version "24.4"
1755 :package-version '(Org . "8.0")
1756 :type '(list
1757 (string :tag "Scheduled today ")
1758 (string :tag "Scheduled previously")))
1760 (defcustom org-agenda-inactive-leader "["
1761 "Text preceding item pulled into the agenda by inactive time stamps.
1762 These entries are added to the agenda when pressing \"[\"."
1763 :group 'org-agenda-line-format
1764 :version "24.1"
1765 :type 'string)
1767 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: " "%2d d. ago: ")
1768 "Text preceding deadline items in the agenda view.
1769 This is a list with three strings. The first applies when the item has its
1770 deadline on the current day. The second applies when the deadline is in the
1771 future, the third one when it is in the past. The strings may contain %d
1772 to capture the number of days."
1773 :group 'org-agenda-line-format
1774 :version "24.4"
1775 :package-version '(Org . "8.0")
1776 :type '(list
1777 (string :tag "Deadline today ")
1778 (string :tag "Deadline in the future ")
1779 (string :tag "Deadline in the past ")))
1781 (defcustom org-agenda-remove-times-when-in-prefix t
1782 "Non-nil means remove duplicate time specifications in agenda items.
1783 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1784 time-of-day specification in a headline or diary entry is extracted and
1785 placed into the prefix. If this option is non-nil, the original specification
1786 \(a timestamp or -range, or just a plain time(range) specification like
1787 11:30-4pm) will be removed for agenda display. This makes the agenda less
1788 cluttered.
1789 The option can be t or nil. It may also be the symbol `beg', indicating
1790 that the time should only be removed when it is located at the beginning of
1791 the headline/diary entry."
1792 :group 'org-agenda-line-format
1793 :type '(choice
1794 (const :tag "Always" t)
1795 (const :tag "Never" nil)
1796 (const :tag "When at beginning of entry" beg)))
1798 (defcustom org-agenda-remove-timeranges-from-blocks nil
1799 "Non-nil means remove time ranges specifications in agenda
1800 items that span on several days."
1801 :group 'org-agenda-line-format
1802 :version "24.1"
1803 :type 'boolean)
1805 (defcustom org-agenda-default-appointment-duration nil
1806 "Default duration for appointments that only have a starting time.
1807 When nil, no duration is specified in such cases.
1808 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1809 :group 'org-agenda-line-format
1810 :type '(choice
1811 (integer :tag "Minutes")
1812 (const :tag "No default duration")))
1814 (defcustom org-agenda-show-inherited-tags t
1815 "Non-nil means show inherited tags in each agenda line.
1817 When this option is set to `always', it takes precedence over
1818 `org-agenda-use-tag-inheritance' and inherited tags are shown
1819 in every agenda.
1821 When this option is set to t (the default), inherited tags are
1822 shown when they are available, i.e. when the value of
1823 `org-agenda-use-tag-inheritance' enables tag inheritance for the
1824 given agenda type.
1826 This can be set to a list of agenda types in which the agenda
1827 must display the inherited tags. Available types are `todo',
1828 `agenda' and `search'.
1830 When set to nil, never show inherited tags in agenda lines."
1831 :group 'org-agenda-line-format
1832 :group 'org-agenda
1833 :version "24.3"
1834 :type '(choice
1835 (const :tag "Show inherited tags when available" t)
1836 (const :tag "Always show inherited tags" always)
1837 (repeat :tag "Show inherited tags only in selected agenda types"
1838 (symbol :tag "Agenda type"))))
1840 (defcustom org-agenda-use-tag-inheritance '(todo search agenda)
1841 "List of agenda view types where to use tag inheritance.
1843 In tags/tags-todo/tags-tree agenda views, tag inheritance is
1844 controlled by `org-use-tag-inheritance'. In other agenda types,
1845 `org-use-tag-inheritance' is not used for the selection of the
1846 agenda entries. Still, you may want the agenda to be aware of
1847 the inherited tags anyway, e.g. for later tag filtering.
1849 Allowed value are `todo', `search' and `agenda'.
1851 This variable has no effect if `org-agenda-show-inherited-tags'
1852 is set to `always'. In that case, the agenda is aware of those
1853 tags.
1855 The default value sets tags in every agenda type. Setting this
1856 option to nil will speed up non-tags agenda view a lot."
1857 :group 'org-agenda
1858 :version "26.1"
1859 :package-version '(Org . "9.1")
1860 :type '(choice
1861 (const :tag "Use tag inheritance in all agenda types" t)
1862 (repeat :tag "Use tag inheritance in selected agenda types"
1863 (symbol :tag "Agenda type"))))
1865 (defcustom org-agenda-hide-tags-regexp nil
1866 "Regular expression used to filter away specific tags in agenda views.
1867 This means that these tags will be present, but not be shown in the agenda
1868 line. Secondary filtering will still work on the hidden tags.
1869 Nil means don't hide any tags."
1870 :group 'org-agenda-line-format
1871 :type '(choice
1872 (const :tag "Hide none" nil)
1873 (string :tag "Regexp ")))
1875 (defcustom org-agenda-remove-tags nil
1876 "Non-nil means remove the tags from the headline copy in the agenda.
1877 When this is the symbol `prefix', only remove tags when
1878 `org-agenda-prefix-format' contains a `%T' specifier."
1879 :group 'org-agenda-line-format
1880 :type '(choice
1881 (const :tag "Always" t)
1882 (const :tag "Never" nil)
1883 (const :tag "When prefix format contains %T" prefix)))
1885 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1886 'org-agenda-remove-tags)
1888 (defcustom org-agenda-tags-column 'auto
1889 "Shift tags in agenda items to this column.
1890 If set to `auto', tags will be automatically aligned to the right
1891 edge of the window.
1893 If set to a positive number, tags will be left-aligned to that
1894 column. If set to a negative number, tags will be right-aligned
1895 to that column. For example, -80 works well for a normal 80
1896 character screen."
1897 :group 'org-agenda-line-format
1898 :type '(choice
1899 (const :tag "Automatically align to right edge of window" auto)
1900 (integer :tag "Specific column" -80))
1901 :package-version '(Org . "9.1")
1902 :version "26.1")
1904 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column)
1906 (defcustom org-agenda-fontify-priorities 'cookies
1907 "Non-nil means highlight low and high priorities in agenda.
1908 When t, the highest priority entries are bold, lowest priority italic.
1909 However, settings in `org-priority-faces' will overrule these faces.
1910 When this variable is the symbol `cookies', only fontify the
1911 cookies, not the entire task.
1912 This may also be an association list of priority faces, whose
1913 keys are the character values of `org-highest-priority',
1914 `org-default-priority', and `org-lowest-priority' (the default values
1915 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1916 color as a string, or a list like `(:background \"Red\")'.
1917 If it is a color, the variable `org-faces-easy-properties'
1918 determines if it is a foreground or a background color."
1919 :group 'org-agenda-line-format
1920 :type '(choice
1921 (const :tag "Never" nil)
1922 (const :tag "Defaults" t)
1923 (const :tag "Cookies only" cookies)
1924 (repeat :tag "Specify"
1925 (list (character :tag "Priority" :value ?A)
1926 (choice :tag "Face "
1927 (string :tag "Color")
1928 (sexp :tag "Face"))))))
1930 (defcustom org-agenda-day-face-function nil
1931 "Function called to determine what face should be used to display a day.
1932 The only argument passed to that function is the day. It should
1933 returns a face, or nil if does not want to specify a face and let
1934 the normal rules apply."
1935 :group 'org-agenda-line-format
1936 :version "24.1"
1937 :type '(choice (const nil) (function)))
1939 (defcustom org-agenda-category-icon-alist nil
1940 "Alist of category icon to be displayed in agenda views.
1942 Each entry should have the following format:
1944 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1946 Where CATEGORY-REGEXP is a regexp matching the categories where
1947 the icon should be displayed.
1948 FILE-OR-DATA either a file path or a string containing image data.
1950 The other fields can be omitted safely if not needed:
1951 TYPE indicates the image type.
1952 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1953 image data.
1954 PROPS are additional image attributes to assign to the image,
1955 like, e.g. `:ascent center'.
1957 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1959 If you want to set the display properties yourself, just put a
1960 list as second element:
1962 (CATEGORY-REGEXP (MY PROPERTY LIST))
1964 For example, to display a 16px horizontal space for Emacs
1965 category, you can use:
1967 (\"Emacs\" \\='(space . (:width (16))))"
1968 :group 'org-agenda-line-format
1969 :version "24.1"
1970 :type '(alist :key-type (string :tag "Regexp matching category")
1971 :value-type (choice (list :tag "Icon"
1972 (string :tag "File or data")
1973 (symbol :tag "Type")
1974 (boolean :tag "Data?")
1975 (repeat :tag "Extra image properties" :inline t symbol))
1976 (list :tag "Display properties" sexp))))
1978 (defgroup org-agenda-column-view nil
1979 "Options concerning column view in the agenda."
1980 :tag "Org Agenda Column View"
1981 :group 'org-agenda)
1983 (defcustom org-agenda-view-columns-initially nil
1984 "When non-nil, switch to columns view right after creating the agenda."
1985 :group 'org-agenda-column-view
1986 :type 'boolean
1987 :version "26.1"
1988 :package-version '(Org . "9.0")
1989 :safe #'booleanp)
1991 (defcustom org-agenda-columns-show-summaries t
1992 "Non-nil means show summaries for columns displayed in the agenda view."
1993 :group 'org-agenda-column-view
1994 :type 'boolean)
1996 (defcustom org-agenda-columns-compute-summary-properties t
1997 "Non-nil means recompute all summary properties before column view.
1998 When column view in the agenda is listing properties that have a summary
1999 operator, it can go to all relevant buffers and recompute the summaries
2000 there. This can mean overhead for the agenda column view, but is necessary
2001 to have thing up to date.
2002 As a special case, a CLOCKSUM property also makes sure that the clock
2003 computations are current."
2004 :group 'org-agenda-column-view
2005 :type 'boolean)
2007 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
2008 "Non-nil means the duration of an appointment will add to day effort.
2009 The property to which appointment durations will be added is the one given
2010 in the option `org-effort-property'. If an appointment does not have
2011 an end time, `org-agenda-default-appointment-duration' will be used. If that
2012 is not set, an appointment without end time will not contribute to the time
2013 estimate."
2014 :group 'org-agenda-column-view
2015 :type 'boolean)
2017 (defcustom org-agenda-auto-exclude-function nil
2018 "A function called with a tag to decide if it is filtered on \
2019 \\<org-agenda-mode-map>`\\[org-agenda-filter-by-tag] RET'.
2020 The sole argument to the function, which is called once for each
2021 possible tag, is a string giving the name of the tag. The
2022 function should return either nil if the tag should be included
2023 as normal, or \"-<TAG>\" to exclude the tag.
2024 Note that for the purpose of tag filtering, only the lower-case version of
2025 all tags will be considered, so that this function will only ever see
2026 the lower-case version of all tags."
2027 :group 'org-agenda
2028 :type '(choice (const nil) (function)))
2030 (defcustom org-agenda-bulk-custom-functions nil
2031 "Alist of characters and custom functions for bulk actions.
2032 For example, this value makes those two functions available:
2034 \\='((?R set-category)
2035 (?C bulk-cut))
2037 With selected entries in an agenda buffer, `B R' will call
2038 the custom function `set-category' on the selected entries.
2039 Note that functions in this alist don't need to be quoted."
2040 :type '(alist :key-type character :value-type (group function))
2041 :version "24.1"
2042 :group 'org-agenda)
2044 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
2045 "Execute BODY with point at location given by `org-hd-marker' property.
2046 If STRING is non-nil, the text property will be fetched from position 0
2047 in that string. If STRING is nil, it will be fetched from the beginning
2048 of the current line."
2049 (org-with-gensyms (marker)
2050 `(let ((,marker (get-text-property (if ,string 0 (point-at-bol))
2051 'org-hd-marker ,string)))
2052 (with-current-buffer (marker-buffer ,marker)
2053 (save-excursion
2054 (goto-char ,marker)
2055 ,@body)))))
2056 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
2058 (defun org-add-agenda-custom-command (entry)
2059 "Replace or add a command in `org-agenda-custom-commands'.
2060 This is mostly for hacking and trying a new command - once the command
2061 works you probably want to add it to `org-agenda-custom-commands' for good."
2062 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
2063 (if ass
2064 (setcdr ass (cdr entry))
2065 (push entry org-agenda-custom-commands))))
2067 (defmacro org-agenda--insert-overriding-header (default)
2068 "Insert header into agenda view.
2069 The inserted header depends on `org-agenda-overriding-header'.
2070 If the empty string, don't insert a header. If any other string,
2071 insert it as a header. If nil, insert DEFAULT, which should
2072 evaluate to a string."
2073 (declare (debug (form)) (indent defun))
2074 `(cond
2075 ((not org-agenda-overriding-header) (insert ,default))
2076 ((equal org-agenda-overriding-header "") nil)
2077 ((stringp org-agenda-overriding-header)
2078 (insert (propertize org-agenda-overriding-header
2079 'face 'org-agenda-structure)
2080 "\n"))
2081 (t (user-error "Invalid value for `org-agenda-overriding-header': %S"
2082 org-agenda-overriding-header))))
2084 ;;; Define the org-agenda-mode
2086 (defvar org-agenda-mode-map (make-sparse-keymap)
2087 "Keymap for `org-agenda-mode'.")
2088 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map)
2090 (defvar org-agenda-menu) ; defined later in this file.
2091 (defvar org-agenda-restrict nil) ; defined later in this file.
2092 (defvar org-agenda-follow-mode nil)
2093 (defvar org-agenda-entry-text-mode nil)
2094 (defvar org-agenda-clockreport-mode nil)
2095 (defvar org-agenda-show-log nil)
2096 (defvar org-agenda-redo-command nil)
2097 (defvar org-agenda-query-string nil)
2098 (defvar org-agenda-mode-hook nil
2099 "Hook run after `org-agenda-mode' is turned on.
2100 The buffer is still writable when this hook is called.")
2101 (defvar org-agenda-type nil)
2102 (defvar org-agenda-force-single-file nil)
2103 (defvar org-agenda-bulk-marked-entries nil
2104 "List of markers that refer to marked entries in the agenda.")
2105 (defvar org-agenda-current-date nil
2106 "Active date when building the agenda.")
2108 ;;; Multiple agenda buffers support
2110 (defcustom org-agenda-sticky nil
2111 "Non-nil means agenda q key will bury agenda buffers.
2112 Agenda commands will then show existing buffer instead of generating new ones.
2113 When nil, `q' will kill the single agenda buffer."
2114 :group 'org-agenda
2115 :version "24.3"
2116 :type 'boolean)
2119 ;;;###autoload
2120 (defun org-toggle-sticky-agenda (&optional arg)
2121 "Toggle `org-agenda-sticky'."
2122 (interactive "P")
2123 (let ((new-value (if arg
2124 (> (prefix-numeric-value arg) 0)
2125 (not org-agenda-sticky))))
2126 (if (equal new-value org-agenda-sticky)
2127 (and (called-interactively-p 'interactive)
2128 (message "Sticky agenda was already %s"
2129 (if org-agenda-sticky "enabled" "disabled")))
2130 (setq org-agenda-sticky new-value)
2131 (org-agenda-kill-all-agenda-buffers)
2132 (and (called-interactively-p 'interactive)
2133 (message "Sticky agenda %s"
2134 (if org-agenda-sticky "enabled" "disabled"))))))
2136 (defvar org-agenda-buffer nil
2137 "Agenda buffer currently being generated.")
2139 (defvar org-agenda-last-prefix-arg nil)
2140 (defvar org-agenda-this-buffer-name nil)
2141 (defvar org-agenda-doing-sticky-redo nil)
2142 (defvar org-agenda-this-buffer-is-sticky nil)
2143 (defvar org-agenda-last-indirect-buffer nil
2144 "Last buffer loaded by `org-agenda-tree-to-indirect-buffer'.")
2146 (defconst org-agenda-local-vars
2147 '(org-agenda-this-buffer-name
2148 org-agenda-undo-list
2149 org-agenda-pending-undo-list
2150 org-agenda-follow-mode
2151 org-agenda-entry-text-mode
2152 org-agenda-clockreport-mode
2153 org-agenda-show-log
2154 org-agenda-redo-command
2155 org-agenda-query-string
2156 org-agenda-type
2157 org-agenda-bulk-marked-entries
2158 org-agenda-undo-has-started-in
2159 org-agenda-info
2160 org-agenda-pre-window-conf
2161 org-agenda-columns-active
2162 org-agenda-tag-filter
2163 org-agenda-category-filter
2164 org-agenda-top-headline-filter
2165 org-agenda-regexp-filter
2166 org-agenda-effort-filter
2167 org-agenda-markers
2168 org-agenda-last-search-view-search-was-boolean
2169 org-agenda-last-indirect-buffer
2170 org-agenda-filtered-by-category
2171 org-agenda-filter-form
2172 org-agenda-cycle-counter
2173 org-agenda-last-prefix-arg)
2174 "Variables that must be local in agenda buffers to allow multiple buffers.")
2176 (defun org-agenda-mode ()
2177 "Mode for time-sorted view on action items in Org files.
2179 The following commands are available:
2181 \\{org-agenda-mode-map}"
2182 (interactive)
2183 (cond (org-agenda-doing-sticky-redo
2184 ;; Refreshing sticky agenda-buffer
2186 ;; Preserve the value of `org-agenda-local-vars' variables,
2187 ;; while letting `kill-all-local-variables' kill the rest
2188 (let ((save (buffer-local-variables)))
2189 (kill-all-local-variables)
2190 (mapc #'make-local-variable org-agenda-local-vars)
2191 (dolist (elem save)
2192 (pcase elem
2193 (`(,var . ,val) ;ignore unbound variables
2194 (when (and val (memq var org-agenda-local-vars))
2195 (set var val))))))
2196 (setq-local org-agenda-this-buffer-is-sticky t))
2197 (org-agenda-sticky
2198 ;; Creating a sticky Agenda buffer for the first time
2199 (kill-all-local-variables)
2200 (mapc 'make-local-variable org-agenda-local-vars)
2201 (setq-local org-agenda-this-buffer-is-sticky t))
2203 ;; Creating a non-sticky agenda buffer
2204 (kill-all-local-variables)
2205 (setq-local org-agenda-this-buffer-is-sticky nil)))
2206 (setq org-agenda-undo-list nil
2207 org-agenda-pending-undo-list nil
2208 org-agenda-bulk-marked-entries nil)
2209 (setq major-mode 'org-agenda-mode)
2210 ;; Keep global-font-lock-mode from turning on font-lock-mode
2211 (setq-local font-lock-global-modes (list 'not major-mode))
2212 (setq mode-name "Org-Agenda")
2213 (setq indent-tabs-mode nil)
2214 (use-local-map org-agenda-mode-map)
2215 (easy-menu-add org-agenda-menu)
2216 (if org-startup-truncated (setq truncate-lines t))
2217 (setq-local line-move-visual nil)
2218 (add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
2219 (add-hook 'pre-command-hook 'org-unhighlight nil 'local)
2220 ;; Make sure properties are removed when copying text
2221 (add-hook 'filter-buffer-substring-functions
2222 (lambda (fun start end delete)
2223 (substring-no-properties (funcall fun start end delete)))
2224 nil t)
2225 (unless org-agenda-keep-modes
2226 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
2227 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode))
2228 (setq org-agenda-show-log org-agenda-start-with-log-mode)
2229 (setq org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode)
2230 (add-to-invisibility-spec '(org-filtered))
2231 (add-to-invisibility-spec '(org-link))
2232 (easy-menu-change
2233 '("Agenda") "Agenda Files"
2234 (append
2235 (list
2236 (vector
2237 (if (get 'org-agenda-files 'org-restrict)
2238 "Restricted to single file"
2239 "Edit File List")
2240 '(org-edit-agenda-file-list)
2241 (not (get 'org-agenda-files 'org-restrict)))
2242 "--")
2243 (mapcar 'org-file-menu-entry (org-agenda-files))))
2244 (org-agenda-set-mode-name)
2245 (apply
2246 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2247 (list 'org-agenda-mode-hook)))
2249 (substitute-key-definition 'undo 'org-agenda-undo
2250 org-agenda-mode-map global-map)
2251 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2252 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2253 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2254 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2255 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2256 (org-defkey org-agenda-mode-map [(meta down)] 'org-agenda-drag-line-forward)
2257 (org-defkey org-agenda-mode-map [(meta up)] 'org-agenda-drag-line-backward)
2258 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2259 (org-defkey org-agenda-mode-map "\M-m" 'org-agenda-bulk-toggle)
2260 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2261 (org-defkey org-agenda-mode-map "\M-*" 'org-agenda-bulk-toggle-all)
2262 (org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
2263 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2264 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2265 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2266 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2267 (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2268 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2269 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2270 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2271 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2272 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2273 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2274 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2275 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2276 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2277 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2278 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2279 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2280 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2281 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2282 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2283 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2284 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2285 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2286 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2287 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2288 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2289 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2290 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2291 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2292 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2293 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2294 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2295 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2296 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2297 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2298 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2299 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2300 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2302 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2303 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2304 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2305 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2306 (while l (org-defkey org-agenda-mode-map
2307 (int-to-string (pop l)) 'digit-argument)))
2309 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2310 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2311 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2312 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2313 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2314 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2315 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2316 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2317 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2318 (org-defkey org-agenda-mode-map "g" 'org-agenda-redo-all)
2319 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2320 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2321 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2322 'org-clock-modify-effort-estimate)
2323 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2324 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2325 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2326 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2327 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2328 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2329 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2330 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2331 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2332 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2333 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2334 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2335 (substitute-key-definition 'next-line 'org-agenda-next-line
2336 org-agenda-mode-map global-map)
2337 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2338 org-agenda-mode-map global-map)
2339 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2340 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2341 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2342 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2343 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2344 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2345 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2346 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2347 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2348 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2349 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2350 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2351 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2352 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2353 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2354 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2355 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2356 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2357 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2358 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2359 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2360 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2361 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2362 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2363 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2364 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2365 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2366 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2367 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2368 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2369 (org-defkey org-agenda-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock-from-agenda)
2371 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2372 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2373 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2374 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2375 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2376 (org-defkey org-agenda-mode-map "_" 'org-agenda-filter-by-effort)
2377 (org-defkey org-agenda-mode-map "=" 'org-agenda-filter-by-regexp)
2378 (org-defkey org-agenda-mode-map "|" 'org-agenda-filter-remove-all)
2379 (org-defkey org-agenda-mode-map "~" 'org-agenda-limit-interactively)
2380 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2381 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-headline)
2382 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2383 (org-defkey org-agenda-mode-map "\C-c\C-x_" 'org-timer-stop)
2384 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2385 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2386 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2388 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2389 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2391 (define-key org-agenda-mode-map [remap forward-paragraph] 'org-agenda-forward-block)
2392 (define-key org-agenda-mode-map [remap backward-paragraph] 'org-agenda-backward-block)
2394 (when org-agenda-mouse-1-follows-link
2395 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2396 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2397 '("Agenda"
2398 ("Agenda Files")
2399 "--"
2400 ("Agenda Dates"
2401 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda)]
2402 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2403 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2404 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2405 "--"
2406 ("View"
2407 ["Day View" org-agenda-day-view
2408 :active (org-agenda-check-type nil 'agenda)
2409 :style radio :selected (eq org-agenda-current-span 'day)
2410 :keys "v d (or just d)"]
2411 ["Week View" org-agenda-week-view
2412 :active (org-agenda-check-type nil 'agenda)
2413 :style radio :selected (eq org-agenda-current-span 'week)
2414 :keys "v w"]
2415 ["Fortnight View" org-agenda-fortnight-view
2416 :active (org-agenda-check-type nil 'agenda)
2417 :style radio :selected (eq org-agenda-current-span 'fortnight)
2418 :keys "v t"]
2419 ["Month View" org-agenda-month-view
2420 :active (org-agenda-check-type nil 'agenda)
2421 :style radio :selected (eq org-agenda-current-span 'month)
2422 :keys "v m"]
2423 ["Year View" org-agenda-year-view
2424 :active (org-agenda-check-type nil 'agenda)
2425 :style radio :selected (eq org-agenda-current-span 'year)
2426 :keys "v y"]
2427 "--"
2428 ["Include Diary" org-agenda-toggle-diary
2429 :style toggle :selected org-agenda-include-diary
2430 :active (org-agenda-check-type nil 'agenda)]
2431 ["Include Deadlines" org-agenda-toggle-deadlines
2432 :style toggle :selected org-agenda-include-deadlines
2433 :active (org-agenda-check-type nil 'agenda)]
2434 ["Use Time Grid" org-agenda-toggle-time-grid
2435 :style toggle :selected org-agenda-use-time-grid
2436 :active (org-agenda-check-type nil 'agenda)]
2437 "--"
2438 ["Show clock report" org-agenda-clockreport-mode
2439 :style toggle :selected org-agenda-clockreport-mode
2440 :active (org-agenda-check-type nil 'agenda)]
2441 ["Show some entry text" org-agenda-entry-text-mode
2442 :style toggle :selected org-agenda-entry-text-mode
2443 :active t]
2444 "--"
2445 ["Show Logbook entries" org-agenda-log-mode
2446 :style toggle :selected org-agenda-show-log
2447 :active (org-agenda-check-type nil 'agenda)
2448 :keys "v l (or just l)"]
2449 ["Include archived trees" org-agenda-archives-mode
2450 :style toggle :selected org-agenda-archives-mode :active t
2451 :keys "v a"]
2452 ["Include archive files" (org-agenda-archives-mode t)
2453 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2454 :keys "v A"]
2455 "--"
2456 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2457 ["Write view to file" org-agenda-write t]
2458 ["Rebuild buffer" org-agenda-redo t]
2459 ["Save all Org buffers" org-save-all-org-buffers t]
2460 "--"
2461 ["Show original entry" org-agenda-show t]
2462 ["Go To (other window)" org-agenda-goto t]
2463 ["Go To (this window)" org-agenda-switch-to t]
2464 ["Capture with cursor date" org-agenda-capture t]
2465 ["Follow Mode" org-agenda-follow-mode
2466 :style toggle :selected org-agenda-follow-mode :active t]
2467 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2468 "--"
2469 ("TODO"
2470 ["Cycle TODO" org-agenda-todo t]
2471 ["Next TODO set" org-agenda-todo-nextset t]
2472 ["Previous TODO set" org-agenda-todo-previousset t]
2473 ["Add note" org-agenda-add-note t])
2474 ("Archive/Refile/Delete"
2475 ["Archive default" org-agenda-archive-default t]
2476 ["Archive default" org-agenda-archive-default-with-confirmation t]
2477 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2478 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2479 ["Archive subtree" org-agenda-archive t]
2480 "--"
2481 ["Refile" org-agenda-refile t]
2482 "--"
2483 ["Delete subtree" org-agenda-kill t])
2484 ("Bulk action"
2485 ["Mark entry" org-agenda-bulk-mark t]
2486 ["Mark all" org-agenda-bulk-mark-all t]
2487 ["Unmark entry" org-agenda-bulk-unmark t]
2488 ["Unmark all" org-agenda-bulk-unmark-all :active t :keys "U"]
2489 ["Toggle mark" org-agenda-bulk-toggle t]
2490 ["Toggle all" org-agenda-bulk-toggle-all t]
2491 ["Mark regexp" org-agenda-bulk-mark-regexp t])
2492 ["Act on all marked" org-agenda-bulk-action t]
2493 "--"
2494 ("Tags and Properties"
2495 ["Show all Tags" org-agenda-show-tags t]
2496 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2497 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2498 "--"
2499 ["Column View" org-columns t])
2500 ("Deadline/Schedule"
2501 ["Schedule" org-agenda-schedule t]
2502 ["Set Deadline" org-agenda-deadline t]
2503 "--"
2504 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda)]
2505 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda)]
2506 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda) :keys "C-u S-right"]
2507 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda) :keys "C-u S-left"]
2508 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda) :keys "C-u C-u S-right"]
2509 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda) :keys "C-u C-u S-left"]
2510 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda)])
2511 ("Clock and Effort"
2512 ["Clock in" org-agenda-clock-in t]
2513 ["Clock out" org-agenda-clock-out t]
2514 ["Clock cancel" org-agenda-clock-cancel t]
2515 ["Goto running clock" org-clock-goto t]
2516 "--"
2517 ["Set Effort" org-agenda-set-effort t]
2518 ["Change clocked effort" org-clock-modify-effort-estimate
2519 (org-clock-is-active)])
2520 ("Priority"
2521 ["Set Priority" org-agenda-priority t]
2522 ["Increase Priority" org-agenda-priority-up t]
2523 ["Decrease Priority" org-agenda-priority-down t]
2524 ["Show Priority" org-show-priority t])
2525 ("Calendar/Diary"
2526 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda)]
2527 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda)]
2528 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda)]
2529 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda)]
2530 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda)]
2531 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda)]
2532 "--"
2533 ["Create iCalendar File" org-icalendar-combine-agenda-files t])
2534 "--"
2535 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2536 "--"
2537 ("MobileOrg"
2538 ["Push Files and Views" org-mobile-push t]
2539 ["Get Captured and Flagged" org-mobile-pull t]
2540 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "\\[org-agenda] ?"]
2541 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2542 "--"
2543 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2544 "--"
2545 ["Quit" org-agenda-quit t]
2546 ["Exit and Release Buffers" org-agenda-exit t]
2549 ;;; Agenda undo
2551 (defvar org-agenda-allow-remote-undo t
2552 "Non-nil means allow remote undo from the agenda buffer.")
2553 (defvar org-agenda-undo-has-started-in nil
2554 "Buffers that have already seen `undo-start' in the current undo sequence.")
2556 (defun org-agenda-undo ()
2557 "Undo a remote editing step in the agenda.
2558 This undoes changes both in the agenda buffer and in the remote buffer
2559 that have been changed along."
2560 (interactive)
2561 (or org-agenda-allow-remote-undo
2562 (user-error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2563 (if (not (eq this-command last-command))
2564 (setq org-agenda-undo-has-started-in nil
2565 org-agenda-pending-undo-list org-agenda-undo-list))
2566 (if (not org-agenda-pending-undo-list)
2567 (user-error "No further undo information"))
2568 (let* ((entry (pop org-agenda-pending-undo-list))
2569 buf line cmd rembuf)
2570 (setq cmd (pop entry) line (pop entry))
2571 (setq rembuf (nth 2 entry))
2572 (org-with-remote-undo rembuf
2573 (while (bufferp (setq buf (pop entry)))
2574 (if (pop entry)
2575 (with-current-buffer buf
2576 (let ((last-undo-buffer buf)
2577 (inhibit-read-only t))
2578 (unless (memq buf org-agenda-undo-has-started-in)
2579 (push buf org-agenda-undo-has-started-in)
2580 (make-local-variable 'pending-undo-list)
2581 (undo-start))
2582 (while (and pending-undo-list
2583 (listp pending-undo-list)
2584 (not (car pending-undo-list)))
2585 (pop pending-undo-list))
2586 (undo-more 1))))))
2587 (org-goto-line line)
2588 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2590 (defun org-verify-change-for-undo (l1 l2)
2591 "Verify that a real change occurred between the undo lists L1 and L2."
2592 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2593 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2594 (not (eq l1 l2)))
2596 ;;; Agenda dispatch
2598 (defvar org-agenda-restrict-begin (make-marker))
2599 (defvar org-agenda-restrict-end (make-marker))
2600 (defvar org-agenda-last-dispatch-buffer nil)
2601 (defvar org-agenda-overriding-restriction nil)
2603 (defcustom org-agenda-custom-commands-contexts nil
2604 "Alist of custom agenda keys and contextual rules.
2606 For example, if you have a custom agenda command \"p\" and you
2607 want this command to be accessible only from plain text files,
2608 use this:
2610 \\='((\"p\" ((in-file . \"\\\\.txt\\\\'\"))))
2612 Here are the available contexts definitions:
2614 in-file: command displayed only in matching files
2615 in-mode: command displayed only in matching modes
2616 not-in-file: command not displayed in matching files
2617 not-in-mode: command not displayed in matching modes
2618 in-buffer: command displayed only in matching buffers
2619 not-in-buffer: command not displayed in matching buffers
2620 [function]: a custom function taking no argument
2622 If you define several checks, the agenda command will be
2623 accessible if there is at least one valid check.
2625 You can also bind a key to another agenda custom command
2626 depending on contextual rules.
2628 \\='((\"p\" \"q\" ((in-file . \"\\\\.txt\\\\'\"))))
2630 Here it means: in .txt files, use \"p\" as the key for the
2631 agenda command otherwise associated with \"q\". (The command
2632 originally associated with \"q\" is not displayed to avoid
2633 duplicates.)"
2634 :version "24.3"
2635 :group 'org-agenda-custom-commands
2636 :type '(repeat (list :tag "Rule"
2637 (string :tag " Agenda key")
2638 (string :tag "Replace by command")
2639 (repeat :tag "Available when"
2640 (choice
2641 (cons :tag "Condition"
2642 (choice
2643 (const :tag "In file" in-file)
2644 (const :tag "Not in file" not-in-file)
2645 (const :tag "In buffer" in-buffer)
2646 (const :tag "Not in buffer" not-in-buffer)
2647 (const :tag "In mode" in-mode)
2648 (const :tag "Not in mode" not-in-mode))
2649 (regexp))
2650 (function :tag "Custom function"))))))
2652 (defcustom org-agenda-max-entries nil
2653 "Maximum number of entries to display in an agenda.
2654 This can be nil (no limit) or an integer or an alist of agenda
2655 types with an associated number of entries to display in this
2656 type."
2657 :version "24.4"
2658 :package-version '(Org . "8.0")
2659 :group 'org-agenda-custom-commands
2660 :type '(choice (symbol :tag "No limit" nil)
2661 (integer :tag "Max number of entries")
2662 (repeat
2663 (cons (choice :tag "Agenda type"
2664 (const agenda)
2665 (const todo)
2666 (const tags)
2667 (const search))
2668 (integer :tag "Max number of entries")))))
2670 (defcustom org-agenda-max-todos nil
2671 "Maximum number of TODOs to display in an agenda.
2672 This can be nil (no limit) or an integer or an alist of agenda
2673 types with an associated number of entries to display in this
2674 type."
2675 :version "24.4"
2676 :package-version '(Org . "8.0")
2677 :group 'org-agenda-custom-commands
2678 :type '(choice (symbol :tag "No limit" nil)
2679 (integer :tag "Max number of TODOs")
2680 (repeat
2681 (cons (choice :tag "Agenda type"
2682 (const agenda)
2683 (const todo)
2684 (const tags)
2685 (const search))
2686 (integer :tag "Max number of TODOs")))))
2688 (defcustom org-agenda-max-tags nil
2689 "Maximum number of tagged entries to display in an agenda.
2690 This can be nil (no limit) or an integer or an alist of agenda
2691 types with an associated number of entries to display in this
2692 type."
2693 :version "24.4"
2694 :package-version '(Org . "8.0")
2695 :group 'org-agenda-custom-commands
2696 :type '(choice (symbol :tag "No limit" nil)
2697 (integer :tag "Max number of tagged entries")
2698 (repeat
2699 (cons (choice :tag "Agenda type"
2700 (const agenda)
2701 (const todo)
2702 (const tags)
2703 (const search))
2704 (integer :tag "Max number of tagged entries")))))
2706 (defcustom org-agenda-max-effort nil
2707 "Maximum cumulated effort duration for the agenda.
2708 This can be nil (no limit) or a number of minutes (as an integer)
2709 or an alist of agenda types with an associated number of minutes
2710 to limit entries to in this type."
2711 :version "24.4"
2712 :package-version '(Org . "8.0")
2713 :group 'org-agenda-custom-commands
2714 :type '(choice (symbol :tag "No limit" nil)
2715 (integer :tag "Max number of minutes")
2716 (repeat
2717 (cons (choice :tag "Agenda type"
2718 (const agenda)
2719 (const todo)
2720 (const tags)
2721 (const search))
2722 (integer :tag "Max number of minutes")))))
2724 (defvar org-agenda-keep-restricted-file-list nil)
2725 (defvar org-keys nil)
2726 (defvar org-match nil)
2727 ;;;###autoload
2728 (defun org-agenda (&optional arg org-keys restriction)
2729 "Dispatch agenda commands to collect entries to the agenda buffer.
2730 Prompts for a command to execute. Any prefix arg will be passed
2731 on to the selected command. The default selections are:
2733 a Call `org-agenda-list' to display the agenda for current day or week.
2734 t Call `org-todo-list' to display the global todo list.
2735 T Call `org-todo-list' to display the global todo list, select only
2736 entries with a specific TODO keyword (the user gets a prompt).
2737 m Call `org-tags-view' to display headlines with tags matching
2738 a condition (the user is prompted for the condition).
2739 M Like `m', but select only TODO entries, no ordinary headlines.
2740 e Export views to associated files.
2741 s Search entries for keywords.
2742 S Search entries for keywords, only with TODO keywords.
2743 / Multi occur across all agenda files and also files listed
2744 in `org-agenda-text-search-extra-files'.
2745 < Restrict agenda commands to buffer, subtree, or region.
2746 Press several times to get the desired effect.
2747 > Remove a previous restriction.
2748 # List \"stuck\" projects.
2749 ! Configure what \"stuck\" means.
2750 C Configure custom agenda commands.
2752 More commands can be added by configuring the variable
2753 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2754 searches can be pre-defined in this way.
2756 If the current buffer is in Org mode and visiting a file, you can also
2757 first press `<' once to indicate that the agenda should be temporarily
2758 \(until the next use of `\\[org-agenda]') restricted to the current file.
2759 Pressing `<' twice means to restrict to the current subtree or region
2760 \(if active)."
2761 (interactive "P")
2762 (catch 'exit
2763 (let* ((prefix-descriptions nil)
2764 (org-agenda-buffer-name org-agenda-buffer-name)
2765 (org-agenda-window-setup (if (equal (buffer-name)
2766 org-agenda-buffer-name)
2767 'current-window
2768 org-agenda-window-setup))
2769 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2770 (org-agenda-custom-commands
2771 ;; normalize different versions
2772 (delq nil
2773 (mapcar
2774 (lambda (x)
2775 (cond ((stringp (cdr x))
2776 (push x prefix-descriptions)
2777 nil)
2778 ((stringp (nth 1 x)) x)
2779 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2780 (t (cons (car x) (cons "" (cdr x))))))
2781 org-agenda-custom-commands)))
2782 (org-agenda-custom-commands
2783 (org-contextualize-keys
2784 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2785 (buf (current-buffer))
2786 (bfn (buffer-file-name (buffer-base-buffer)))
2787 entry key type org-match lprops ans)
2788 ;; Turn off restriction unless there is an overriding one,
2789 (unless org-agenda-overriding-restriction
2790 (unless org-agenda-keep-restricted-file-list
2791 ;; There is a request to keep the file list in place
2792 (put 'org-agenda-files 'org-restrict nil))
2793 (setq org-agenda-restrict nil)
2794 (move-marker org-agenda-restrict-begin nil)
2795 (move-marker org-agenda-restrict-end nil))
2796 ;; Delete old local properties
2797 (put 'org-agenda-redo-command 'org-lprops nil)
2798 ;; Delete previously set last-arguments
2799 (put 'org-agenda-redo-command 'last-args nil)
2800 ;; Remember where this call originated
2801 (setq org-agenda-last-dispatch-buffer (current-buffer))
2802 (unless org-keys
2803 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2804 org-keys (car ans)
2805 restriction (cdr ans)))
2806 ;; If we have sticky agenda buffers, set a name for the buffer,
2807 ;; depending on the invoking keys. The user may still set this
2808 ;; as a command option, which will overwrite what we do here.
2809 (if org-agenda-sticky
2810 (setq org-agenda-buffer-name
2811 (format "*Org Agenda(%s)*" org-keys)))
2812 ;; Establish the restriction, if any
2813 (when (and (not org-agenda-overriding-restriction) restriction)
2814 (put 'org-agenda-files 'org-restrict (list bfn))
2815 (cond
2816 ((eq restriction 'region)
2817 (setq org-agenda-restrict (current-buffer))
2818 (move-marker org-agenda-restrict-begin (region-beginning))
2819 (move-marker org-agenda-restrict-end (region-end)))
2820 ((eq restriction 'subtree)
2821 (save-excursion
2822 (setq org-agenda-restrict (current-buffer))
2823 (org-back-to-heading t)
2824 (move-marker org-agenda-restrict-begin (point))
2825 (move-marker org-agenda-restrict-end
2826 (progn (org-end-of-subtree t)))))))
2828 ;; For example the todo list should not need it (but does...)
2829 (cond
2830 ((setq entry (assoc org-keys org-agenda-custom-commands))
2831 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2832 (progn
2833 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
2834 lprops (nth 4 entry))
2835 (if org-agenda-sticky
2836 (setq org-agenda-buffer-name
2837 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2838 (format "*Org Agenda(%s)*" org-keys))))
2839 (put 'org-agenda-redo-command 'org-lprops lprops)
2840 (cond
2841 ((eq type 'agenda)
2842 (org-let lprops '(org-agenda-list current-prefix-arg)))
2843 ((eq type 'agenda*)
2844 (org-let lprops '(org-agenda-list current-prefix-arg nil nil t)))
2845 ((eq type 'alltodo)
2846 (org-let lprops '(org-todo-list current-prefix-arg)))
2847 ((eq type 'search)
2848 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
2849 ((eq type 'stuck)
2850 (org-let lprops '(org-agenda-list-stuck-projects
2851 current-prefix-arg)))
2852 ((eq type 'tags)
2853 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2854 ((eq type 'tags-todo)
2855 (org-let lprops '(org-tags-view '(4) org-match)))
2856 ((eq type 'todo)
2857 (org-let lprops '(org-todo-list org-match)))
2858 ((eq type 'tags-tree)
2859 (org-check-for-org-mode)
2860 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
2861 ((eq type 'todo-tree)
2862 (org-check-for-org-mode)
2863 (org-let lprops
2864 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2865 (regexp-quote org-match) "\\>"))))
2866 ((eq type 'occur-tree)
2867 (org-check-for-org-mode)
2868 (org-let lprops '(org-occur org-match)))
2869 ((functionp type)
2870 (org-let lprops '(funcall type org-match)))
2871 ((fboundp type)
2872 (org-let lprops '(funcall type org-match)))
2873 (t (user-error "Invalid custom agenda command type %s" type))))
2874 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2875 ((equal org-keys "C")
2876 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2877 (customize-variable 'org-agenda-custom-commands))
2878 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2879 ((equal org-keys "s") (call-interactively 'org-search-view))
2880 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2881 ((equal org-keys "t") (call-interactively 'org-todo-list))
2882 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2883 ((equal org-keys "m") (call-interactively 'org-tags-view))
2884 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2885 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2886 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
2887 (add-hook
2888 'post-command-hook
2889 (lambda ()
2890 (unless (current-message)
2891 (let* ((m (org-agenda-get-any-marker))
2892 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2893 (when note
2894 (message (concat
2895 "FLAGGING-NOTE ([?] for more info): "
2896 (org-add-props
2897 (replace-regexp-in-string
2898 "\\\\n" "//"
2899 (copy-sequence note))
2900 nil 'face 'org-warning)))))))
2901 t t))
2902 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2903 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2904 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
2905 (t (user-error "Invalid agenda key"))))))
2907 (defvar org-agenda-multi)
2909 (defun org-agenda-append-agenda ()
2910 "Append another agenda view to the current one.
2911 This function allows interactive building of block agendas.
2912 Agenda views are separated by `org-agenda-block-separator'."
2913 (interactive)
2914 (unless (derived-mode-p 'org-agenda-mode)
2915 (user-error "Can only append from within agenda buffer"))
2916 (let ((org-agenda-multi t))
2917 (org-agenda)
2918 (widen)
2919 (org-agenda-finalize)
2920 (setq buffer-read-only t)
2921 (org-agenda-fit-window-to-buffer)))
2923 (defun org-agenda-normalize-custom-commands (cmds)
2924 "Normalize custom commands CMDS."
2925 (delq nil
2926 (mapcar
2927 (lambda (x)
2928 (cond ((stringp (cdr x)) nil)
2929 ((stringp (nth 1 x)) x)
2930 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2931 (t (cons (car x) (cons "" (cdr x))))))
2932 cmds)))
2934 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2935 "The user interface for selecting an agenda command."
2936 (catch 'exit
2937 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2938 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2939 (region-p (org-region-active-p))
2940 (custom org-agenda-custom-commands)
2941 (selstring "")
2942 restriction second-time
2943 c entry key type match prefixes rmheader header-end custom1 desc
2944 line lines left right n n1)
2945 (save-window-excursion
2946 (delete-other-windows)
2947 (org-switch-to-buffer-other-window " *Agenda Commands*")
2948 (erase-buffer)
2949 (insert (eval-when-compile
2950 (let ((header
2951 "Press key for an agenda command:
2952 -------------------------------- < Buffer, subtree/region restriction
2953 a Agenda for current week or day > Remove restriction
2954 t List of all TODO entries e Export agenda views
2955 m Match a TAGS/PROP/TODO query T Entries with special TODO kwd
2956 s Search for keywords M Like m, but only TODO entries
2957 / Multi-occur S Like s, but only TODO entries
2958 ? Find :FLAGGED: entries C Configure custom agenda commands
2959 * Toggle sticky agenda views # List stuck projects (!=configure)
2961 (start 0))
2962 (while (string-match
2963 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2964 header start)
2965 (setq start (match-end 0))
2966 (add-text-properties (match-beginning 2) (match-end 2)
2967 '(face bold) header))
2968 header)))
2969 (setq header-end (point-marker))
2970 (while t
2971 (setq custom1 custom)
2972 (when (eq rmheader t)
2973 (org-goto-line 1)
2974 (re-search-forward ":" nil t)
2975 (delete-region (match-end 0) (point-at-eol))
2976 (forward-char 1)
2977 (looking-at "-+")
2978 (delete-region (match-end 0) (point-at-eol))
2979 (move-marker header-end (match-end 0)))
2980 (goto-char header-end)
2981 (delete-region (point) (point-max))
2983 ;; Produce all the lines that describe custom commands and prefixes
2984 (setq lines nil)
2985 (while (setq entry (pop custom1))
2986 (setq key (car entry) desc (nth 1 entry)
2987 type (nth 2 entry)
2988 match (nth 3 entry))
2989 (if (> (length key) 1)
2990 (cl-pushnew (string-to-char key) prefixes :test #'equal)
2991 (setq line
2992 (format
2993 "%-4s%-14s"
2994 (org-add-props (copy-sequence key)
2995 '(face bold))
2996 (cond
2997 ((string-match "\\S-" desc) desc)
2998 ((eq type 'agenda) "Agenda for current week or day")
2999 ((eq type 'agenda*) "Appointments for current week or day")
3000 ((eq type 'alltodo) "List of all TODO entries")
3001 ((eq type 'search) "Word search")
3002 ((eq type 'stuck) "List of stuck projects")
3003 ((eq type 'todo) "TODO keyword")
3004 ((eq type 'tags) "Tags query")
3005 ((eq type 'tags-todo) "Tags (TODO)")
3006 ((eq type 'tags-tree) "Tags tree")
3007 ((eq type 'todo-tree) "TODO kwd tree")
3008 ((eq type 'occur-tree) "Occur tree")
3009 ((functionp type) (if (symbolp type)
3010 (symbol-name type)
3011 "Lambda expression"))
3012 (t "???"))))
3013 (if org-agenda-menu-show-matcher
3014 (setq line
3015 (concat line ": "
3016 (cond
3017 ((stringp match)
3018 (setq match (copy-sequence match))
3019 (org-add-props match nil 'face 'org-warning))
3020 ((listp type)
3021 (format "set of %d commands" (length type))))))
3022 (if (org-string-nw-p match)
3023 (add-text-properties
3024 0 (length line) (list 'help-echo
3025 (concat "Matcher: " match)) line)))
3026 (push line lines)))
3027 (setq lines (nreverse lines))
3028 (when prefixes
3029 (mapc (lambda (x)
3030 (push
3031 (format "%s %s"
3032 (org-add-props (char-to-string x)
3033 nil 'face 'bold)
3034 (or (cdr (assoc (concat selstring
3035 (char-to-string x))
3036 prefix-descriptions))
3037 "Prefix key"))
3038 lines))
3039 prefixes))
3041 ;; Check if we should display in two columns
3042 (if org-agenda-menu-two-columns
3043 (progn
3044 (setq n (length lines)
3045 n1 (+ (/ n 2) (mod n 2))
3046 right (nthcdr n1 lines)
3047 left (copy-sequence lines))
3048 (setcdr (nthcdr (1- n1) left) nil))
3049 (setq left lines right nil))
3050 (while left
3051 (insert "\n" (pop left))
3052 (when right
3053 (if (< (current-column) 40)
3054 (move-to-column 40 t)
3055 (insert " "))
3056 (insert (pop right))))
3058 ;; Make the window the right size
3059 (goto-char (point-min))
3060 (if second-time
3061 (if (not (pos-visible-in-window-p (point-max)))
3062 (org-fit-window-to-buffer))
3063 (setq second-time t)
3064 (org-fit-window-to-buffer))
3066 ;; Ask for selection
3067 (message "Press key for agenda command%s:"
3068 (if (or restrict-ok org-agenda-overriding-restriction)
3069 (if org-agenda-overriding-restriction
3070 " (restriction lock active)"
3071 (if restriction
3072 (format " (restricted to %s)" restriction)
3073 " (unrestricted)"))
3074 ""))
3075 (setq c (read-char-exclusive))
3076 (message "")
3077 (cond
3078 ((assoc (char-to-string c) custom)
3079 (setq selstring (concat selstring (char-to-string c)))
3080 (throw 'exit (cons selstring restriction)))
3081 ((memq c prefixes)
3082 (setq selstring (concat selstring (char-to-string c))
3083 prefixes nil
3084 rmheader (or rmheader t)
3085 custom (delq nil (mapcar
3086 (lambda (x)
3087 (if (or (= (length (car x)) 1)
3088 (/= (string-to-char (car x)) c))
3090 (cons (substring (car x) 1) (cdr x))))
3091 custom))))
3092 ((eq c ?*)
3093 (call-interactively 'org-toggle-sticky-agenda)
3094 (sit-for 2))
3095 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
3096 (message "Restriction is only possible in Org buffers")
3097 (ding) (sit-for 1))
3098 ((eq c ?1)
3099 (org-agenda-remove-restriction-lock 'noupdate)
3100 (setq restriction 'buffer))
3101 ((eq c ?0)
3102 (org-agenda-remove-restriction-lock 'noupdate)
3103 (setq restriction (if region-p 'region 'subtree)))
3104 ((eq c ?<)
3105 (org-agenda-remove-restriction-lock 'noupdate)
3106 (setq restriction
3107 (cond
3108 ((eq restriction 'buffer)
3109 (if region-p 'region 'subtree))
3110 ((memq restriction '(subtree region))
3111 nil)
3112 (t 'buffer))))
3113 ((eq c ?>)
3114 (org-agenda-remove-restriction-lock 'noupdate)
3115 (setq restriction nil))
3116 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
3117 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
3118 ((and (> (length selstring) 0) (eq c ?\d))
3119 (delete-window)
3120 (org-agenda-get-restriction-and-command prefix-descriptions))
3122 ((equal c ?q) (error "Abort"))
3123 (t (user-error "Invalid key %c" c))))))))
3125 (defun org-agenda-fit-window-to-buffer ()
3126 "Fit the window to the buffer size."
3127 (and (memq org-agenda-window-setup '(reorganize-frame))
3128 (fboundp 'fit-window-to-buffer)
3129 (if (and (= (cdr org-agenda-window-frame-fractions) 1.0)
3130 (= (car org-agenda-window-frame-fractions) 1.0))
3131 (delete-other-windows)
3132 (org-fit-window-to-buffer
3134 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3135 (floor (* (frame-height) (car org-agenda-window-frame-fractions)))))))
3137 (defvar org-cmd nil)
3138 (defvar org-agenda-overriding-cmd nil)
3139 (defvar org-agenda-overriding-arguments nil)
3140 (defvar org-agenda-overriding-cmd-arguments nil)
3141 (defun org-agenda-run-series (name series)
3142 "Run agenda NAME as a SERIES of agenda commands."
3143 (org-let (nth 1 series) '(org-agenda-prepare name))
3144 ;; We need to reset agenda markers here, because when constructing a
3145 ;; block agenda, the individual blocks do not do that.
3146 (org-agenda-reset-markers)
3147 (let* ((org-agenda-multi t)
3148 (redo (list 'org-agenda-run-series name (list 'quote series)))
3149 (cmds (car series))
3150 (gprops (nth 1 series))
3151 match ;; The byte compiler incorrectly complains about this. Keep it!
3152 org-cmd type lprops)
3153 (while (setq org-cmd (pop cmds))
3154 (setq type (car org-cmd))
3155 (setq match (eval (nth 1 org-cmd)))
3156 (setq lprops (nth 2 org-cmd))
3157 (let ((org-agenda-overriding-arguments
3158 (if (eq org-agenda-overriding-cmd org-cmd)
3159 (or org-agenda-overriding-arguments
3160 org-agenda-overriding-cmd-arguments))))
3161 (cond
3162 ((eq type 'agenda)
3163 (org-let2 gprops lprops
3164 '(call-interactively 'org-agenda-list)))
3165 ((eq type 'agenda*)
3166 (org-let2 gprops lprops
3167 '(funcall 'org-agenda-list nil nil t)))
3168 ((eq type 'alltodo)
3169 (org-let2 gprops lprops
3170 '(call-interactively 'org-todo-list)))
3171 ((eq type 'search)
3172 (org-let2 gprops lprops
3173 '(org-search-view current-prefix-arg match nil)))
3174 ((eq type 'stuck)
3175 (org-let2 gprops lprops
3176 '(call-interactively 'org-agenda-list-stuck-projects)))
3177 ((eq type 'tags)
3178 (org-let2 gprops lprops
3179 '(org-tags-view current-prefix-arg match)))
3180 ((eq type 'tags-todo)
3181 (org-let2 gprops lprops
3182 '(org-tags-view '(4) match)))
3183 ((eq type 'todo)
3184 (org-let2 gprops lprops
3185 '(org-todo-list match)))
3186 ((fboundp type)
3187 (org-let2 gprops lprops
3188 '(funcall type match)))
3189 (t (error "Invalid type in command series")))))
3190 (widen)
3191 (let ((inhibit-read-only t))
3192 (add-text-properties (point-min) (point-max)
3193 `(org-series t org-series-redo-cmd ,redo)))
3194 (setq org-agenda-redo-command redo)
3195 (goto-char (point-min)))
3196 (org-agenda-fit-window-to-buffer)
3197 (org-let (nth 1 series) '(org-agenda-finalize)))
3199 ;;;###autoload
3200 (defmacro org-batch-agenda (cmd-key &rest parameters)
3201 "Run an agenda command in batch mode and send the result to STDOUT.
3202 If CMD-KEY is a string of length 1, it is used as a key in
3203 `org-agenda-custom-commands' and triggers this command. If it is a
3204 longer string it is used as a tags/todo match string.
3205 Parameters are alternating variable names and values that will be bound
3206 before running the agenda command."
3207 (org-eval-in-environment (org-make-parameter-alist parameters)
3208 (let (org-agenda-sticky)
3209 (if (> (length cmd-key) 1)
3210 (org-tags-view nil cmd-key)
3211 (org-agenda nil cmd-key))))
3212 (set-buffer org-agenda-buffer-name)
3213 (princ (buffer-string)))
3215 (defvar org-agenda-info nil)
3217 ;;;###autoload
3218 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
3219 "Run an agenda command in batch mode and send the result to STDOUT.
3220 If CMD-KEY is a string of length 1, it is used as a key in
3221 `org-agenda-custom-commands' and triggers this command. If it is a
3222 longer string it is used as a tags/todo match string.
3223 Parameters are alternating variable names and values that will be bound
3224 before running the agenda command.
3226 The output gives a line for each selected agenda item. Each
3227 item is a list of comma-separated values, like this:
3229 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3231 category The category of the item
3232 head The headline, without TODO kwd, TAGS and PRIORITY
3233 type The type of the agenda entry, can be
3234 todo selected in TODO match
3235 tagsmatch selected in tags match
3236 diary imported from diary
3237 deadline a deadline on given date
3238 scheduled scheduled on given date
3239 timestamp entry has timestamp on given date
3240 closed entry was closed on given date
3241 upcoming-deadline warning about deadline
3242 past-scheduled forwarded scheduled item
3243 block entry has date block including g. date
3244 todo The todo keyword, if any
3245 tags All tags including inherited ones, separated by colons
3246 date The relevant date, like 2007-2-14
3247 time The time, like 15:00-16:50
3248 extra Sting with extra planning info
3249 priority-l The priority letter if any was given
3250 priority-n The computed numerical priority
3251 agenda-day The day in the agenda where this is listed"
3252 (org-eval-in-environment (append '((org-agenda-remove-tags t))
3253 (org-make-parameter-alist parameters))
3254 (if (> (length cmd-key) 2)
3255 (org-tags-view nil cmd-key)
3256 (org-agenda nil cmd-key)))
3257 (set-buffer org-agenda-buffer-name)
3258 (let* ((lines (org-split-string (buffer-string) "\n"))
3259 line)
3260 (while (setq line (pop lines))
3261 (catch 'next
3262 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
3263 (setq org-agenda-info
3264 (org-fix-agenda-info (text-properties-at 0 line)))
3265 (princ
3266 (mapconcat 'org-agenda-export-csv-mapper
3267 '(org-category txt type todo tags date time extra
3268 priority-letter priority agenda-day)
3269 ","))
3270 (princ "\n")))))
3272 (defun org-fix-agenda-info (props)
3273 "Make sure all properties on an agenda item have a canonical form.
3274 This ensures the export commands can easily use it."
3275 (let (tmp re)
3276 (when (setq tmp (plist-get props 'tags))
3277 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3278 (when (setq tmp (plist-get props 'date))
3279 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3280 (let ((calendar-date-display-form '(year "-" month "-" day)))
3281 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3283 (setq tmp (calendar-date-string tmp)))
3284 (setq props (plist-put props 'date tmp)))
3285 (when (setq tmp (plist-get props 'day))
3286 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3287 (let ((calendar-date-display-form '(year "-" month "-" day)))
3288 (setq tmp (calendar-date-string tmp)))
3289 (setq props (plist-put props 'day tmp))
3290 (setq props (plist-put props 'agenda-day tmp)))
3291 (when (setq tmp (plist-get props 'txt))
3292 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3293 (plist-put props 'priority-letter (match-string 1 tmp))
3294 (setq tmp (replace-match "" t t tmp)))
3295 (when (and (setq re (plist-get props 'org-todo-regexp))
3296 (setq re (concat "\\`\\.*" re " ?"))
3297 (let ((case-fold-search nil)) (string-match re tmp)))
3298 (plist-put props 'todo (match-string 1 tmp))
3299 (setq tmp (replace-match "" t t tmp)))
3300 (plist-put props 'txt tmp)))
3301 props)
3303 (defun org-agenda-export-csv-mapper (prop)
3304 (let ((res (plist-get org-agenda-info prop)))
3305 (setq res
3306 (cond
3307 ((not res) "")
3308 ((stringp res) res)
3309 (t (prin1-to-string res))))
3310 (org-trim (replace-regexp-in-string "," ";" res nil t))))
3312 ;;;###autoload
3313 (defun org-store-agenda-views (&rest parameters)
3314 "Store agenda views."
3315 (interactive)
3316 (eval (list 'org-batch-store-agenda-views)))
3318 ;;;###autoload
3319 (defmacro org-batch-store-agenda-views (&rest parameters)
3320 "Run all custom agenda commands that have a file argument."
3321 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3322 (pop-up-frames nil)
3323 (dir default-directory)
3324 (pars (org-make-parameter-alist parameters))
3325 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
3326 (save-window-excursion
3327 (while cmds
3328 (setq cmd (pop cmds)
3329 thiscmdkey (car cmd)
3330 thiscmdcmd (cdr cmd)
3331 match (nth 2 thiscmdcmd)
3332 bufname (if org-agenda-sticky
3333 (or (and (stringp match)
3334 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3335 (format "*Org Agenda(%s)*" thiscmdkey))
3336 org-agenda-buffer-name)
3337 cmd-or-set (nth 2 cmd)
3338 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3339 files (nth (if (listp cmd-or-set) 4 5) cmd))
3340 (if (stringp files) (setq files (list files)))
3341 (when files
3342 (org-eval-in-environment (append org-agenda-exporter-settings
3343 opts pars)
3344 (org-agenda nil thiscmdkey))
3345 (set-buffer bufname)
3346 (while files
3347 (org-eval-in-environment (append org-agenda-exporter-settings
3348 opts pars)
3349 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3350 (and (get-buffer bufname)
3351 (kill-buffer bufname)))))))
3353 (defvar org-agenda-current-span nil
3354 "The current span used in the agenda view.") ; local variable in the agenda buffer
3355 (defun org-agenda-mark-header-line (pos)
3356 "Mark the line at POS as an agenda structure header."
3357 (save-excursion
3358 (goto-char pos)
3359 (put-text-property (point-at-bol) (point-at-eol)
3360 'org-agenda-structural-header t)
3361 (when org-agenda-title-append
3362 (put-text-property (point-at-bol) (point-at-eol)
3363 'org-agenda-title-append org-agenda-title-append))))
3365 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3366 (defvar org-agenda-write-buffer-name "Agenda View")
3367 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3368 "Write the current buffer (an agenda view) as a file.
3370 Depending on the extension of the file name, plain text (.txt),
3371 HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
3372 If the extension is .ics, translate visible agenda into iCalendar
3373 format. If the extension is .org, collect all subtrees
3374 corresponding to the agenda entries and add them in an .org file.
3376 With prefix argument OPEN, open the new file immediately. If
3377 NOSETTINGS is given, do not scope the settings of
3378 `org-agenda-exporter-settings' into the export commands. This is
3379 used when the settings have already been scoped and we do not
3380 wish to overrule other, higher priority settings. If
3381 AGENDA-BUFFER-NAME is provided, use this as the buffer name for
3382 the agenda to write."
3383 (interactive "FWrite agenda to file: \nP")
3384 (if (or (not (file-writable-p file))
3385 (and (file-exists-p file)
3386 (if (called-interactively-p 'any)
3387 (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
3388 (user-error "Cannot write agenda to file %s" file))
3389 (org-let (if nosettings nil org-agenda-exporter-settings)
3390 '(save-excursion
3391 (save-window-excursion
3392 (let ((bs (copy-sequence (buffer-string)))
3393 (extension (file-name-extension file))
3394 (default-directory (file-name-directory file))
3395 beg content)
3396 (with-temp-buffer
3397 (rename-buffer org-agenda-write-buffer-name t)
3398 (set-buffer-modified-p nil)
3399 (insert bs)
3400 (org-agenda-remove-marked-text 'invisible 'org-filtered)
3401 (run-hooks 'org-agenda-before-write-hook)
3402 (cond
3403 ((bound-and-true-p org-mobile-creating-agendas)
3404 (org-mobile-write-agenda-for-mobile file))
3405 ((string= "org" extension)
3406 (let (content p m message-log-max)
3407 (goto-char (point-min))
3408 (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
3409 (goto-char p)
3410 (setq m (get-text-property (point) 'org-hd-marker))
3411 (when m
3412 (push (save-excursion
3413 (set-buffer (marker-buffer m))
3414 (goto-char m)
3415 (org-copy-subtree 1 nil t t)
3416 org-subtree-clip)
3417 content)))
3418 (find-file file)
3419 (erase-buffer)
3420 (dolist (s content) (org-paste-subtree 1 s))
3421 (write-file file)
3422 (kill-buffer (current-buffer))
3423 (message "Org file written to %s" file)))
3424 ((member extension '("html" "htm"))
3425 (or (require 'htmlize nil t)
3426 (error "Please install htmlize from https://github.com/hniksic/emacs-htmlize"))
3427 (set-buffer (htmlize-buffer (current-buffer)))
3428 (when org-agenda-export-html-style
3429 ;; replace <style> section with org-agenda-export-html-style
3430 (goto-char (point-min))
3431 (kill-region (- (search-forward "<style") 6)
3432 (search-forward "</style>"))
3433 (insert org-agenda-export-html-style))
3434 (write-file file)
3435 (kill-buffer (current-buffer))
3436 (message "HTML written to %s" file))
3437 ((string= "ps" extension)
3438 (require 'ps-print)
3439 (ps-print-buffer-with-faces file)
3440 (message "Postscript written to %s" file))
3441 ((string= "pdf" extension)
3442 (require 'ps-print)
3443 (ps-print-buffer-with-faces
3444 (concat (file-name-sans-extension file) ".ps"))
3445 (call-process "ps2pdf" nil nil nil
3446 (expand-file-name
3447 (concat (file-name-sans-extension file) ".ps"))
3448 (expand-file-name file))
3449 (delete-file (concat (file-name-sans-extension file) ".ps"))
3450 (message "PDF written to %s" file))
3451 ((string= "ics" extension)
3452 (require 'ox-icalendar)
3453 (org-icalendar-export-current-agenda (expand-file-name file)))
3455 (let ((bs (buffer-string)))
3456 (find-file file)
3457 (erase-buffer)
3458 (insert bs)
3459 (save-buffer 0)
3460 (kill-buffer (current-buffer))
3461 (message "Plain text written to %s" file))))))))
3462 (set-buffer (or agenda-bufname
3463 (and (called-interactively-p 'any) (buffer-name))
3464 org-agenda-buffer-name)))
3465 (when open (org-open-file file)))
3467 (defun org-agenda-remove-marked-text (property &optional value)
3468 "Delete all text marked with VALUE of PROPERTY.
3469 VALUE defaults to t."
3470 (let (beg)
3471 (setq value (or value t))
3472 (while (setq beg (text-property-any (point-min) (point-max)
3473 property value))
3474 (delete-region
3475 beg (or (next-single-property-change beg property)
3476 (point-max))))))
3478 (defun org-agenda-add-entry-text ()
3479 "Add entry text to agenda lines.
3480 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3481 entry text following headings shown in the agenda.
3482 Drawers will be excluded, also the line with scheduling/deadline info."
3483 (when (and (> org-agenda-add-entry-text-maxlines 0)
3484 (not (bound-and-true-p org-mobile-creating-agendas)))
3485 (let (m txt)
3486 (goto-char (point-min))
3487 (while (not (eobp))
3488 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3489 (beginning-of-line 2)
3490 (setq txt (org-agenda-get-some-entry-text
3491 m org-agenda-add-entry-text-maxlines " > "))
3492 (end-of-line 1)
3493 (if (string-match "\\S-" txt)
3494 (insert "\n" txt)
3495 (or (eobp) (forward-char 1))))))))
3497 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3498 &rest keep)
3499 "Extract entry text from MARKER, at most N-LINES lines.
3500 This will ignore drawers etc, just get the text.
3501 If INDENT is given, prefix every line with this string. If KEEP is
3502 given, it is a list of symbols, defining stuff that should not be
3503 removed from the entry content. Currently only `planning' is allowed here."
3504 (let (txt drawer-re kwd-time-re ind)
3505 (save-excursion
3506 (with-current-buffer (marker-buffer marker)
3507 (if (not (derived-mode-p 'org-mode))
3508 (setq txt "")
3509 (org-with-wide-buffer
3510 (goto-char marker)
3511 (end-of-line 1)
3512 (setq txt (buffer-substring
3513 (min (1+ (point)) (point-max))
3514 (progn (outline-next-heading) (point)))
3515 drawer-re org-drawer-regexp
3516 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3517 ".*\n?"))
3518 (with-temp-buffer
3519 (insert txt)
3520 (when org-agenda-add-entry-text-descriptive-links
3521 (goto-char (point-min))
3522 (while (org-activate-links (point-max))
3523 (add-text-properties (match-beginning 0) (match-end 0)
3524 '(face org-link))))
3525 (goto-char (point-min))
3526 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3527 (set-text-properties (match-beginning 0) (match-end 0)
3528 nil))
3529 (goto-char (point-min))
3530 (while (re-search-forward drawer-re nil t)
3531 (delete-region
3532 (match-beginning 0)
3533 (progn (re-search-forward
3534 "^[ \t]*:END:.*\n?" nil 'move)
3535 (point))))
3536 (unless (member 'planning keep)
3537 (goto-char (point-min))
3538 (while (re-search-forward kwd-time-re nil t)
3539 (replace-match "")))
3540 (goto-char (point-min))
3541 (when org-agenda-entry-text-exclude-regexps
3542 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3543 (while (setq re (pop re-list))
3544 (goto-char (point-min))
3545 (while (re-search-forward re nil t)
3546 (replace-match "")))))
3547 (goto-char (point-max))
3548 (skip-chars-backward " \t\n")
3549 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3551 ;; find and remove min common indentation
3552 (goto-char (point-min))
3553 (untabify (point-min) (point-max))
3554 (setq ind (org-get-indentation))
3555 (while (not (eobp))
3556 (unless (looking-at "[ \t]*$")
3557 (setq ind (min ind (org-get-indentation))))
3558 (beginning-of-line 2))
3559 (goto-char (point-min))
3560 (while (not (eobp))
3561 (unless (looking-at "[ \t]*$")
3562 (move-to-column ind)
3563 (delete-region (point-at-bol) (point)))
3564 (beginning-of-line 2))
3566 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3568 (goto-char (point-min))
3569 (when indent
3570 (while (and (not (eobp)) (re-search-forward "^" nil t))
3571 (replace-match indent t t)))
3572 (goto-char (point-min))
3573 (while (looking-at "[ \t]*\n") (replace-match ""))
3574 (goto-char (point-max))
3575 (when (> (org-current-line)
3576 n-lines)
3577 (org-goto-line (1+ n-lines))
3578 (backward-char 1))
3579 (setq txt (buffer-substring (point-min) (point))))))))
3580 txt))
3582 (defun org-check-for-org-mode ()
3583 "Make sure current buffer is in org-mode. Error if not."
3584 (or (derived-mode-p 'org-mode)
3585 (error "Cannot execute Org agenda command on buffer in %s"
3586 major-mode)))
3588 ;;; Agenda prepare and finalize
3590 (defvar org-agenda-multi nil) ; dynamically scoped
3591 (defvar org-agenda-pre-window-conf nil)
3592 (defvar org-agenda-columns-active nil)
3593 (defvar org-agenda-name nil)
3594 (defvar org-agenda-tag-filter nil)
3595 (defvar org-agenda-category-filter nil)
3596 (defvar org-agenda-regexp-filter nil)
3597 (defvar org-agenda-effort-filter nil)
3598 (defvar org-agenda-top-headline-filter nil)
3599 (defvar org-agenda-tag-filter-preset nil
3600 "A preset of the tags filter used for secondary agenda filtering.
3601 This must be a list of strings, each string must be a single tag preceded
3602 by \"+\" or \"-\".
3603 This variable should not be set directly, but agenda custom commands can
3604 bind it in the options section. The preset filter is a global property of
3605 the entire agenda view. In a block agenda, it will not work reliably to
3606 define a filter for one of the individual blocks. You need to set it in
3607 the global options and expect it to be applied to the entire view.")
3609 (defvar org-agenda-category-filter-preset nil
3610 "A preset of the category filter used for secondary agenda filtering.
3611 This must be a list of strings, each string must be a single category
3612 preceded by \"+\" or \"-\".
3613 This variable should not be set directly, but agenda custom commands can
3614 bind it in the options section. The preset filter is a global property of
3615 the entire agenda view. In a block agenda, it will not work reliably to
3616 define a filter for one of the individual blocks. You need to set it in
3617 the global options and expect it to be applied to the entire view.")
3619 (defvar org-agenda-regexp-filter-preset nil
3620 "A preset of the regexp filter used for secondary agenda filtering.
3621 This must be a list of strings, each string must be a single regexp
3622 preceded by \"+\" or \"-\".
3623 This variable should not be set directly, but agenda custom commands can
3624 bind it in the options section. The preset filter is a global property of
3625 the entire agenda view. In a block agenda, it will not work reliably to
3626 define a filter for one of the individual blocks. You need to set it in
3627 the global options and expect it to be applied to the entire view.")
3629 (defvar org-agenda-effort-filter-preset nil
3630 "A preset of the effort condition used for secondary agenda filtering.
3631 This must be a list of strings, each string must be a single regexp
3632 preceded by \"+\" or \"-\".
3633 This variable should not be set directly, but agenda custom commands can
3634 bind it in the options section. The preset filter is a global property of
3635 the entire agenda view. In a block agenda, it will not work reliably to
3636 define a filter for one of the individual blocks. You need to set it in
3637 the global options and expect it to be applied to the entire view.")
3639 (defun org-agenda-use-sticky-p ()
3640 "Return non-nil if an agenda buffer named
3641 `org-agenda-buffer-name' exists and should be shown instead of
3642 generating a new one."
3643 (and
3644 ;; turned off by user
3645 org-agenda-sticky
3646 ;; For multi-agenda buffer already exists
3647 (not org-agenda-multi)
3648 ;; buffer found
3649 (get-buffer org-agenda-buffer-name)
3650 ;; C-u parameter is same as last call
3651 (with-current-buffer (get-buffer org-agenda-buffer-name)
3652 (and
3653 (equal current-prefix-arg
3654 org-agenda-last-prefix-arg)
3655 ;; In case user turned stickiness on, while having existing
3656 ;; Agenda buffer active, don't reuse that buffer, because it
3657 ;; does not have org variables local
3658 org-agenda-this-buffer-is-sticky))))
3660 (defun org-agenda-prepare-window (abuf filter-alist)
3661 "Setup agenda buffer in the window.
3662 ABUF is the buffer for the agenda window.
3663 FILTER-ALIST is an alist of filters we need to apply when
3664 `org-agenda-persistent-filter' is non-nil."
3665 (let* ((awin (get-buffer-window abuf)) wconf)
3666 (cond
3667 ((equal (current-buffer) abuf) nil)
3668 (awin (select-window awin))
3669 ((not (setq wconf (current-window-configuration))))
3670 ((eq org-agenda-window-setup 'current-window)
3671 (pop-to-buffer-same-window abuf))
3672 ((eq org-agenda-window-setup 'other-window)
3673 (org-switch-to-buffer-other-window abuf))
3674 ((eq org-agenda-window-setup 'other-frame)
3675 (switch-to-buffer-other-frame abuf))
3676 ((eq org-agenda-window-setup 'only-window)
3677 (delete-other-windows)
3678 (pop-to-buffer-same-window abuf))
3679 ((eq org-agenda-window-setup 'reorganize-frame)
3680 (delete-other-windows)
3681 (org-switch-to-buffer-other-window abuf)))
3682 (setq org-agenda-tag-filter (cdr (assq 'tag filter-alist)))
3683 (setq org-agenda-category-filter (cdr (assq 'cat filter-alist)))
3684 (setq org-agenda-effort-filter (cdr (assq 'effort filter-alist)))
3685 (setq org-agenda-regexp-filter (cdr (assq 're filter-alist)))
3686 ;; Additional test in case agenda is invoked from within agenda
3687 ;; buffer via elisp link.
3688 (unless (equal (current-buffer) abuf)
3689 (pop-to-buffer-same-window abuf))
3690 (setq org-agenda-pre-window-conf
3691 (or wconf org-agenda-pre-window-conf))))
3693 (defun org-agenda-prepare (&optional name)
3694 (let ((filter-alist (if org-agenda-persistent-filter
3695 (with-current-buffer
3696 (get-buffer-create org-agenda-buffer-name)
3697 (list `(tag . ,org-agenda-tag-filter)
3698 `(re . ,org-agenda-regexp-filter)
3699 `(effort . ,org-agenda-effort-filter)
3700 `(cat . ,org-agenda-category-filter))))))
3701 (if (org-agenda-use-sticky-p)
3702 (progn
3703 (put 'org-agenda-tag-filter :preset-filter nil)
3704 (put 'org-agenda-category-filter :preset-filter nil)
3705 (put 'org-agenda-regexp-filter :preset-filter nil)
3706 ;; Popup existing buffer
3707 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name)
3708 filter-alist)
3709 (message "Sticky Agenda buffer, use `r' to refresh")
3710 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3711 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3712 (setq org-todo-keywords-for-agenda nil)
3713 (put 'org-agenda-tag-filter :preset-filter
3714 org-agenda-tag-filter-preset)
3715 (put 'org-agenda-category-filter :preset-filter
3716 org-agenda-category-filter-preset)
3717 (put 'org-agenda-regexp-filter :preset-filter
3718 org-agenda-regexp-filter-preset)
3719 (put 'org-agenda-effort-filter :preset-filter
3720 org-agenda-effort-filter-preset)
3721 (if org-agenda-multi
3722 (progn
3723 (setq buffer-read-only nil)
3724 (goto-char (point-max))
3725 (unless (or (bobp) org-agenda-compact-blocks
3726 (not org-agenda-block-separator))
3727 (insert "\n"
3728 (if (stringp org-agenda-block-separator)
3729 org-agenda-block-separator
3730 (make-string (window-width) org-agenda-block-separator))
3731 "\n"))
3732 (narrow-to-region (point) (point-max)))
3733 (setq org-done-keywords-for-agenda nil)
3734 ;; Setting any org variables that are in org-agenda-local-vars
3735 ;; list need to be done after the prepare call
3736 (org-agenda-prepare-window
3737 (get-buffer-create org-agenda-buffer-name) filter-alist)
3738 (setq buffer-read-only nil)
3739 (org-agenda-reset-markers)
3740 (let ((inhibit-read-only t)) (erase-buffer))
3741 (org-agenda-mode)
3742 (setq org-agenda-buffer (current-buffer))
3743 (setq org-agenda-contributing-files nil)
3744 (setq org-agenda-columns-active nil)
3745 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3746 (setq org-todo-keywords-for-agenda
3747 (org-uniquify org-todo-keywords-for-agenda))
3748 (setq org-done-keywords-for-agenda
3749 (org-uniquify org-done-keywords-for-agenda))
3750 (setq org-agenda-last-prefix-arg current-prefix-arg)
3751 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3752 (and name (not org-agenda-name)
3753 (setq-local org-agenda-name name)))
3754 (setq buffer-read-only nil))))
3756 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3757 (defun org-agenda-finalize ()
3758 "Finishing touch for the agenda buffer, called just before displaying it."
3759 (unless org-agenda-multi
3760 (save-excursion
3761 (let ((inhibit-read-only t))
3762 (goto-char (point-min))
3763 (save-excursion
3764 (while (org-activate-links (point-max))
3765 (add-text-properties (match-beginning 0) (match-end 0)
3766 '(face org-link))))
3767 (unless (eq org-agenda-remove-tags t)
3768 (org-agenda-align-tags))
3769 (unless org-agenda-with-colors
3770 (remove-text-properties (point-min) (point-max) '(face nil)))
3771 (if (and (boundp 'org-agenda-overriding-columns-format)
3772 org-agenda-overriding-columns-format)
3773 (setq-local org-agenda-overriding-columns-format
3774 org-agenda-overriding-columns-format))
3775 (if (and (boundp 'org-agenda-view-columns-initially)
3776 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 (if (and (functionp 'org-habit-insert-consistency-graphs)
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 (org-with-point-at mrk
3805 (delete-dups
3806 (mapcar 'downcase (org-get-tags-at))))))))))
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 (if (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) (if (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 (delq nil
4067 (mapcar (lambda (o)
4068 (if (eq (overlay-get o 'org-overlay-type)
4069 'agenda-entry-content)
4070 (progn (delete-overlay o) t)))
4071 (overlays-in (point-min) (point-max)))))
4073 (defun org-agenda-get-day-face (date)
4074 "Return the face DATE should be displayed with."
4075 (cond ((and (functionp org-agenda-day-face-function)
4076 (funcall org-agenda-day-face-function date)))
4077 ((org-agenda-today-p date) 'org-agenda-date-today)
4078 ((memq (calendar-day-of-week date) org-agenda-weekend-days)
4079 'org-agenda-date-weekend)
4080 (t 'org-agenda-date)))
4082 (defvar org-agenda-show-log-scoped)
4084 ;;; Agenda Daily/Weekly
4086 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4087 "Start day for the agenda view.
4088 Custom commands can set this variable in the options section.
4089 This is usually a string like \"2007-11-01\", \"+2d\" or any other
4090 input allowed when reading a date through the Org calendar.
4091 See the docstring of `org-read-date' for details.")
4092 (defvar org-starting-day nil) ; local variable in the agenda buffer
4093 (defvar org-arg-loc nil) ; local variable
4095 (defvar org-agenda-buffer-tmp-name nil)
4096 ;;;###autoload
4097 (defun org-agenda-list (&optional arg start-day span with-hour)
4098 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4099 The view will be for the current day or week, but from the overview buffer
4100 you will be able to go to other days/weeks.
4102 With a numeric prefix argument in an interactive call, the agenda will
4103 span ARG days. Lisp programs should instead specify SPAN to change
4104 the number of days. SPAN defaults to `org-agenda-span'.
4106 START-DAY defaults to TODAY, or to the most recent match for the weekday
4107 given in `org-agenda-start-on-weekday'.
4109 When WITH-HOUR is non-nil, only include scheduled and deadline
4110 items if they have an hour specification like [h]h:mm."
4111 (interactive "P")
4112 (if org-agenda-overriding-arguments
4113 (setq arg (car org-agenda-overriding-arguments)
4114 start-day (nth 1 org-agenda-overriding-arguments)
4115 span (nth 2 org-agenda-overriding-arguments)))
4116 (if (and (integerp arg) (> arg 0))
4117 (setq span arg arg nil))
4118 (catch 'exit
4119 (setq org-agenda-buffer-name
4120 (or org-agenda-buffer-tmp-name
4121 (and org-agenda-doing-sticky-redo org-agenda-buffer-name)
4122 (if org-agenda-sticky
4123 (cond ((and org-keys (stringp org-match))
4124 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4125 (org-keys
4126 (format "*Org Agenda(%s)*" org-keys))
4127 (t "*Org Agenda(a)*")))
4128 "*Org Agenda*"))
4129 (org-agenda-prepare "Day/Week")
4130 (setq start-day (or start-day org-agenda-start-day))
4131 (if (stringp start-day)
4132 ;; Convert to an absolute day number
4133 (setq start-day (time-to-days (org-read-date nil t start-day))))
4134 (org-compile-prefix-format 'agenda)
4135 (org-set-sorting-strategy 'agenda)
4136 (let* ((span (org-agenda-ndays-to-span (or span org-agenda-span)))
4137 (today (org-today))
4138 (sd (or start-day today))
4139 (ndays (org-agenda-span-to-ndays span sd))
4140 (org-agenda-start-on-weekday
4141 (if (or (eq ndays 7) (eq ndays 14))
4142 org-agenda-start-on-weekday))
4143 (thefiles (org-agenda-files nil 'ifmode))
4144 (files thefiles)
4145 (start (if (or (null org-agenda-start-on-weekday)
4146 (< ndays 7))
4148 (let* ((nt (calendar-day-of-week
4149 (calendar-gregorian-from-absolute sd)))
4150 (n1 org-agenda-start-on-weekday)
4151 (d (- nt n1)))
4152 (- sd (+ (if (< d 0) 7 0) d)))))
4153 (day-numbers (list start))
4154 (day-cnt 0)
4155 (inhibit-redisplay (not debug-on-error))
4156 (org-agenda-show-log-scoped org-agenda-show-log)
4157 s e rtn rtnall file date d start-pos end-pos todayp
4158 clocktable-start clocktable-end filter)
4159 (setq org-agenda-redo-command
4160 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4161 (dotimes (n (1- ndays))
4162 (push (1+ (car day-numbers)) day-numbers))
4163 (setq day-numbers (nreverse day-numbers))
4164 (setq clocktable-start (car day-numbers)
4165 clocktable-end (1+ (or (org-last day-numbers) 0)))
4166 (setq-local org-starting-day (car day-numbers))
4167 (setq-local org-arg-loc arg)
4168 (setq-local org-agenda-current-span (org-agenda-ndays-to-span span))
4169 (unless org-agenda-compact-blocks
4170 (let* ((d1 (car day-numbers))
4171 (d2 (org-last day-numbers))
4172 (w1 (org-days-to-iso-week d1))
4173 (w2 (org-days-to-iso-week d2)))
4174 (setq s (point))
4175 (org-agenda--insert-overriding-header
4176 (concat (org-agenda-span-name span)
4177 "-agenda"
4178 (cond ((<= 350 (- d2 d1)) "")
4179 ((= w1 w2) (format " (W%02d)" w1))
4180 (t (format " (W%02d-W%02d)" w1 w2)))
4181 ":\n")))
4182 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4183 'org-date-line t))
4184 (org-agenda-mark-header-line s))
4185 (while (setq d (pop day-numbers))
4186 (setq date (calendar-gregorian-from-absolute d)
4187 s (point))
4188 (if (or (setq todayp (= d today))
4189 (and (not start-pos) (= d sd)))
4190 (setq start-pos (point))
4191 (if (and start-pos (not end-pos))
4192 (setq end-pos (point))))
4193 (setq files thefiles
4194 rtnall nil)
4195 (while (setq file (pop files))
4196 (catch 'nextfile
4197 (org-check-agenda-file file)
4198 (let ((org-agenda-entry-types org-agenda-entry-types))
4199 ;; Starred types override non-starred equivalents
4200 (when (member :deadline* org-agenda-entry-types)
4201 (setq org-agenda-entry-types
4202 (delq :deadline org-agenda-entry-types)))
4203 (when (member :scheduled* org-agenda-entry-types)
4204 (setq org-agenda-entry-types
4205 (delq :scheduled org-agenda-entry-types)))
4206 ;; Honor with-hour
4207 (when with-hour
4208 (when (member :deadline org-agenda-entry-types)
4209 (setq org-agenda-entry-types
4210 (delq :deadline org-agenda-entry-types))
4211 (push :deadline* org-agenda-entry-types))
4212 (when (member :scheduled org-agenda-entry-types)
4213 (setq org-agenda-entry-types
4214 (delq :scheduled org-agenda-entry-types))
4215 (push :scheduled* org-agenda-entry-types)))
4216 (unless org-agenda-include-deadlines
4217 (setq org-agenda-entry-types
4218 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4219 (cond
4220 ((memq org-agenda-show-log-scoped '(only clockcheck))
4221 (setq rtn (org-agenda-get-day-entries
4222 file date :closed)))
4223 (org-agenda-show-log-scoped
4224 (setq rtn (apply 'org-agenda-get-day-entries
4225 file date
4226 (append '(:closed) org-agenda-entry-types))))
4228 (setq rtn (apply 'org-agenda-get-day-entries
4229 file date
4230 org-agenda-entry-types)))))
4231 (setq rtnall (append rtnall rtn)))) ;; all entries
4232 (if org-agenda-include-diary
4233 (let ((org-agenda-search-headline-for-time t))
4234 (require 'diary-lib)
4235 (setq rtn (org-get-entries-from-diary date))
4236 (setq rtnall (append rtnall rtn))))
4237 (if (or rtnall org-agenda-show-all-dates)
4238 (progn
4239 (setq day-cnt (1+ day-cnt))
4240 (insert
4241 (if (stringp org-agenda-format-date)
4242 (format-time-string org-agenda-format-date
4243 (org-time-from-absolute date))
4244 (funcall org-agenda-format-date date))
4245 "\n")
4246 (put-text-property s (1- (point)) 'face
4247 (org-agenda-get-day-face date))
4248 (put-text-property s (1- (point)) 'org-date-line t)
4249 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4250 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4251 (when todayp
4252 (put-text-property s (1- (point)) 'org-today t))
4253 (setq rtnall
4254 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4255 (if rtnall (insert ;; all entries
4256 (org-agenda-finalize-entries rtnall 'agenda)
4257 "\n"))
4258 (put-text-property s (1- (point)) 'day d)
4259 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4260 (when (and org-agenda-clockreport-mode clocktable-start)
4261 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4262 ;; the above line is to ensure the restricted range!
4263 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4264 tbl)
4265 (setq p (org-plist-delete p :block))
4266 (setq p (plist-put p :tstart clocktable-start))
4267 (setq p (plist-put p :tend clocktable-end))
4268 (setq p (plist-put p :scope 'agenda))
4269 (setq tbl (apply 'org-clock-get-clocktable p))
4270 (insert tbl)))
4271 (goto-char (point-min))
4272 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4273 (unless (and (pos-visible-in-window-p (point-min))
4274 (pos-visible-in-window-p (point-max)))
4275 (goto-char (1- (point-max)))
4276 (recenter -1)
4277 (if (not (pos-visible-in-window-p (or start-pos 1)))
4278 (progn
4279 (goto-char (or start-pos 1))
4280 (recenter 1))))
4281 (goto-char (or start-pos 1))
4282 (add-text-properties (point-min) (point-max)
4283 `(org-agenda-type agenda
4284 org-last-args (,arg ,start-day ,span)
4285 org-redo-cmd ,org-agenda-redo-command
4286 org-series-cmd ,org-cmd))
4287 (if (eq org-agenda-show-log-scoped 'clockcheck)
4288 (org-agenda-show-clocking-issues))
4289 (org-agenda-finalize)
4290 (setq buffer-read-only t)
4291 (message ""))))
4293 (defun org-agenda-ndays-to-span (n)
4294 "Return a span symbol for a span of N days, or N if none matches."
4295 (cond ((symbolp n) n)
4296 ((= n 1) 'day)
4297 ((= n 7) 'week)
4298 ((= n 14) 'fortnight)
4299 (t n)))
4301 (defun org-agenda-span-to-ndays (span &optional start-day)
4302 "Return ndays from SPAN, possibly starting at START-DAY.
4303 START-DAY is an absolute time value."
4304 (cond ((numberp span) span)
4305 ((eq span 'day) 1)
4306 ((eq span 'week) 7)
4307 ((eq span 'fortnight) 14)
4308 ((eq span 'month)
4309 (let ((date (calendar-gregorian-from-absolute start-day)))
4310 (calendar-last-day-of-month (car date) (cl-caddr date))))
4311 ((eq span 'year)
4312 (let ((date (calendar-gregorian-from-absolute start-day)))
4313 (if (calendar-leap-year-p (cl-caddr date)) 366 365)))))
4315 (defun org-agenda-span-name (span)
4316 "Return a SPAN name."
4317 (if (null span)
4319 (if (symbolp span)
4320 (capitalize (symbol-name span))
4321 (format "%d days" span))))
4323 ;;; Agenda word search
4325 (defvar org-agenda-search-history nil)
4327 (defvar org-search-syntax-table nil
4328 "Special syntax table for Org search.
4329 In this table, we have single quotes not as word constituents, to
4330 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4332 (defvar org-mode-syntax-table) ; From org.el
4333 (defun org-search-syntax-table ()
4334 (unless org-search-syntax-table
4335 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4336 (modify-syntax-entry ?' "." org-search-syntax-table)
4337 (modify-syntax-entry ?` "." org-search-syntax-table))
4338 org-search-syntax-table)
4340 (defvar org-agenda-last-search-view-search-was-boolean nil)
4342 ;;;###autoload
4343 (defun org-search-view (&optional todo-only string edit-at)
4344 "Show all entries that contain a phrase or words or regular expressions.
4346 With optional prefix argument TODO-ONLY, only consider entries that are
4347 TODO entries. The argument STRING can be used to pass a default search
4348 string into this function. If EDIT-AT is non-nil, it means that the
4349 user should get a chance to edit this string, with cursor at position
4350 EDIT-AT.
4352 The search string can be viewed either as a phrase that should be found as
4353 is, or it can be broken into a number of snippets, each of which must match
4354 in a Boolean way to select an entry. The default depends on the variable
4355 `org-agenda-search-view-always-boolean'.
4356 Even if this is turned off (the default) you can always switch to
4357 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4359 The default is a direct search of the whole phrase, where each space in
4360 the search string can expand to an arbitrary amount of whitespace,
4361 including newlines.
4363 If using a Boolean search, the search string is split on whitespace and
4364 each snippet is searched separately, with logical AND to select an entry.
4365 Words prefixed with a minus must *not* occur in the entry. Words without
4366 a prefix or prefixed with a plus must occur in the entry. Matching is
4367 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4368 match whole words, not parts of a word) if
4369 `org-agenda-search-view-force-full-words' is set (default is nil).
4371 Boolean search snippets enclosed by curly braces are interpreted as
4372 regular expressions that must or (when preceded with \"-\") must not
4373 match in the entry. Snippets enclosed into double quotes will be taken
4374 as a whole, to include whitespace.
4376 - If the search string starts with an asterisk, search only in headlines.
4377 - If (possibly after the leading star) the search string starts with an
4378 exclamation mark, this also means to look at TODO entries only, an effect
4379 that can also be achieved with a prefix argument.
4380 - If (possibly after star and exclamation mark) the search string starts
4381 with a colon, this will mean that the (non-regexp) snippets of the
4382 Boolean search must match as full words.
4384 This command searches the agenda files, and in addition the files
4385 listed in `org-agenda-text-search-extra-files' unless a restriction lock
4386 is active."
4387 (interactive "P")
4388 (if org-agenda-overriding-arguments
4389 (setq todo-only (car org-agenda-overriding-arguments)
4390 string (nth 1 org-agenda-overriding-arguments)
4391 edit-at (nth 2 org-agenda-overriding-arguments)))
4392 (let* ((props (list 'face nil
4393 'done-face 'org-agenda-done
4394 'org-not-done-regexp org-not-done-regexp
4395 'org-todo-regexp org-todo-regexp
4396 'org-complex-heading-regexp org-complex-heading-regexp
4397 'mouse-face 'highlight
4398 'help-echo (format "mouse-2 or RET jump to location")))
4399 (full-words org-agenda-search-view-force-full-words)
4400 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4401 regexp rtn rtnall files file pos inherited-tags
4402 marker category level tags c neg re boolean
4403 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4404 (unless (and (not edit-at)
4405 (stringp string)
4406 (string-match "\\S-" string))
4407 (setq string (read-string
4408 (if org-agenda-search-view-always-boolean
4409 "[+-]Word/{Regexp} ...: "
4410 "Phrase or [+-]Word/{Regexp} ...: ")
4411 (cond
4412 ((integerp edit-at) (cons string edit-at))
4413 (edit-at string))
4414 'org-agenda-search-history)))
4415 (catch 'exit
4416 (if org-agenda-sticky
4417 (setq org-agenda-buffer-name
4418 (if (stringp string)
4419 (format "*Org Agenda(%s:%s)*"
4420 (or org-keys (or (and todo-only "S") "s")) string)
4421 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4422 (org-agenda-prepare "SEARCH")
4423 (org-compile-prefix-format 'search)
4424 (org-set-sorting-strategy 'search)
4425 (setq org-agenda-redo-command
4426 (list 'org-search-view (if todo-only t nil)
4427 (list 'if 'current-prefix-arg nil string)))
4428 (setq org-agenda-query-string string)
4429 (if (equal (string-to-char string) ?*)
4430 (setq hdl-only t
4431 words (substring string 1))
4432 (setq words string))
4433 (when (equal (string-to-char words) ?!)
4434 (setq todo-only t
4435 words (substring words 1)))
4436 (when (equal (string-to-char words) ?:)
4437 (setq full-words t
4438 words (substring words 1)))
4439 (if (or org-agenda-search-view-always-boolean
4440 (member (string-to-char words) '(?- ?+ ?\{)))
4441 (setq boolean t))
4442 (setq words (split-string words))
4443 (let (www w)
4444 (while (setq w (pop words))
4445 (while (and (string-match "\\\\\\'" w) words)
4446 (setq w (concat (substring w 0 -1) " " (pop words))))
4447 (push w www))
4448 (setq words (nreverse www) www nil)
4449 (while (setq w (pop words))
4450 (when (and (string-match "\\`[-+]?{" w)
4451 (not (string-match "}\\'" w)))
4452 (while (and words (not (string-match "}\\'" (car words))))
4453 (setq w (concat w " " (pop words))))
4454 (setq w (concat w " " (pop words))))
4455 (push w www))
4456 (setq words (nreverse www)))
4457 (setq org-agenda-last-search-view-search-was-boolean boolean)
4458 (when boolean
4459 (let (wds w)
4460 (while (setq w (pop words))
4461 (if (or (equal (substring w 0 1) "\"")
4462 (and (> (length w) 1)
4463 (member (substring w 0 1) '("+" "-"))
4464 (equal (substring w 1 2) "\"")))
4465 (while (and words (not (equal (substring w -1) "\"")))
4466 (setq w (concat w " " (pop words)))))
4467 (and (string-match "\\`\\([-+]?\\)\"" w)
4468 (setq w (replace-match "\\1" nil nil w)))
4469 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4470 (push w wds))
4471 (setq words (nreverse wds))))
4472 (if boolean
4473 (mapc (lambda (w)
4474 (setq c (string-to-char w))
4475 (if (equal c ?-)
4476 (setq neg t w (substring w 1))
4477 (if (equal c ?+)
4478 (setq neg nil w (substring w 1))
4479 (setq neg nil)))
4480 (if (string-match "\\`{.*}\\'" w)
4481 (setq re (substring w 1 -1))
4482 (if full-words
4483 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4484 (setq re (regexp-quote (downcase w)))))
4485 (if neg (push re regexps-) (push re regexps+)))
4486 words)
4487 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4488 regexps+))
4489 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4490 (if (not regexps+)
4491 (setq regexp org-outline-regexp-bol)
4492 (setq regexp (pop regexps+))
4493 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4494 regexp))))
4495 (setq files (org-agenda-files nil 'ifmode))
4496 ;; Add `org-agenda-text-search-extra-files' unless there is some
4497 ;; restriction.
4498 (unless (get 'org-agenda-files 'org-restrict)
4499 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4500 (pop org-agenda-text-search-extra-files)
4501 (setq files (org-add-archive-files files))))
4502 ;; Uniquify files. However, let `org-check-agenda-file' handle
4503 ;; non-existent ones.
4504 (setq files (cl-remove-duplicates
4505 (append files org-agenda-text-search-extra-files)
4506 :test (lambda (a b)
4507 (and (file-exists-p a)
4508 (file-exists-p b)
4509 (file-equal-p a b))))
4510 rtnall nil)
4511 (while (setq file (pop files))
4512 (setq ee nil)
4513 (catch 'nextfile
4514 (org-check-agenda-file file)
4515 (setq buffer (if (file-exists-p file)
4516 (org-get-agenda-file-buffer file)
4517 (error "No such file %s" file)))
4518 (if (not buffer)
4519 ;; If file does not exist, make sure an error message is sent
4520 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4521 file))))
4522 (with-current-buffer buffer
4523 (with-syntax-table (org-search-syntax-table)
4524 (unless (derived-mode-p 'org-mode)
4525 (error "Agenda file %s is not in `org-mode'" file))
4526 (let ((case-fold-search t))
4527 (save-excursion
4528 (save-restriction
4529 (if (eq buffer org-agenda-restrict)
4530 (narrow-to-region org-agenda-restrict-begin
4531 org-agenda-restrict-end)
4532 (widen))
4533 (goto-char (point-min))
4534 (unless (or (org-at-heading-p)
4535 (outline-next-heading))
4536 (throw 'nextfile t))
4537 (goto-char (max (point-min) (1- (point))))
4538 (while (re-search-forward regexp nil t)
4539 (org-back-to-heading t)
4540 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4541 (> (org-reduced-level (org-outline-level))
4542 org-agenda-search-view-max-outline-level)
4543 (forward-line -1)
4544 (org-back-to-heading t)))
4545 (skip-chars-forward "* ")
4546 (setq beg (point-at-bol)
4547 beg1 (point)
4548 end (progn
4549 (outline-next-heading)
4550 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4551 (> (org-reduced-level (org-outline-level))
4552 org-agenda-search-view-max-outline-level)
4553 (forward-line 1)
4554 (outline-next-heading)))
4555 (point)))
4557 (catch :skip
4558 (goto-char beg)
4559 (org-agenda-skip)
4560 (setq str (buffer-substring-no-properties
4561 (point-at-bol)
4562 (if hdl-only (point-at-eol) end)))
4563 (mapc (lambda (wr) (when (string-match wr str)
4564 (goto-char (1- end))
4565 (throw :skip t)))
4566 regexps-)
4567 (mapc (lambda (wr) (unless (string-match wr str)
4568 (goto-char (1- end))
4569 (throw :skip t)))
4570 (if todo-only
4571 (cons (concat "^\\*+[ \t]+"
4572 org-not-done-regexp)
4573 regexps+)
4574 regexps+))
4575 (goto-char beg)
4576 (setq marker (org-agenda-new-marker (point))
4577 category (org-get-category)
4578 level (make-string (org-reduced-level (org-outline-level)) ? )
4579 inherited-tags
4580 (or (eq org-agenda-show-inherited-tags 'always)
4581 (and (listp org-agenda-show-inherited-tags)
4582 (memq 'todo org-agenda-show-inherited-tags))
4583 (and (eq org-agenda-show-inherited-tags t)
4584 (or (eq org-agenda-use-tag-inheritance t)
4585 (memq 'todo org-agenda-use-tag-inheritance))))
4586 tags (org-get-tags-at nil (not inherited-tags))
4587 txt (org-agenda-format-item
4589 (buffer-substring-no-properties
4590 beg1 (point-at-eol))
4591 level category tags t))
4592 (org-add-props txt props
4593 'org-marker marker 'org-hd-marker marker
4594 'org-todo-regexp org-todo-regexp
4595 'level level
4596 'org-complex-heading-regexp org-complex-heading-regexp
4597 'priority 1000
4598 'type "search")
4599 (push txt ee)
4600 (goto-char (1- end))))))))))
4601 (setq rtn (nreverse ee))
4602 (setq rtnall (append rtnall rtn)))
4603 (org-agenda--insert-overriding-header
4604 (with-temp-buffer
4605 (insert "Search words: ")
4606 (add-text-properties (point-min) (1- (point))
4607 (list 'face 'org-agenda-structure))
4608 (setq pos (point))
4609 (insert string "\n")
4610 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4611 (setq pos (point))
4612 (unless org-agenda-multi
4613 (insert (substitute-command-keys "\\<org-agenda-mode-map>\
4614 Press `\\[org-agenda-manipulate-query-add]', \
4615 `\\[org-agenda-manipulate-query-subtract]' to add/sub word, \
4616 `\\[org-agenda-manipulate-query-add-re]', \
4617 `\\[org-agenda-manipulate-query-subtract-re]' to add/sub regexp, \
4618 `\\[universal-argument] \\[org-agenda-redo]' to edit\n"))
4619 (add-text-properties pos (1- (point))
4620 (list 'face 'org-agenda-structure)))
4621 (buffer-string)))
4622 (org-agenda-mark-header-line (point-min))
4623 (when rtnall
4624 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4625 (goto-char (point-min))
4626 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4627 (add-text-properties (point-min) (point-max)
4628 `(org-agenda-type search
4629 org-last-args (,todo-only ,string ,edit-at)
4630 org-redo-cmd ,org-agenda-redo-command
4631 org-series-cmd ,org-cmd))
4632 (org-agenda-finalize)
4633 (setq buffer-read-only t))))
4635 ;;; Agenda TODO list
4637 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4638 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4639 (concat
4640 (if (or (equal keywords "ALL") (not keywords))
4641 (propertize "ALL" 'face 'warning)
4642 (mapconcat
4643 (lambda (kw)
4644 (propertize kw 'face (org-get-todo-face kw)))
4645 (org-split-string keywords "|")
4646 "|"))
4647 "\n"))
4649 (defvar org-select-this-todo-keyword nil)
4650 (defvar org-last-arg nil)
4652 ;;;###autoload
4653 (defun org-todo-list (&optional arg)
4654 "Show all (not done) TODO entries from all agenda file in a single list.
4655 The prefix arg can be used to select a specific TODO keyword and limit
4656 the list to these. When using `\\[universal-argument]', you will be prompted
4657 for a keyword. A numeric prefix directly selects the Nth keyword in
4658 `org-todo-keywords-1'."
4659 (interactive "P")
4660 (if org-agenda-overriding-arguments
4661 (setq arg org-agenda-overriding-arguments))
4662 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4663 (let* ((today (org-today))
4664 (date (calendar-gregorian-from-absolute today))
4665 (kwds org-todo-keywords-for-agenda)
4666 (completion-ignore-case t)
4667 (org-select-this-todo-keyword
4668 (if (stringp arg) arg
4669 (and arg (integerp arg) (> arg 0)
4670 (nth (1- arg) kwds))))
4671 rtn rtnall files file pos)
4672 (when (equal arg '(4))
4673 (setq org-select-this-todo-keyword
4674 (completing-read "Keyword (or KWD1|K2D2|...): "
4675 (mapcar #'list kwds) nil nil)))
4676 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4677 (catch 'exit
4678 (if org-agenda-sticky
4679 (setq org-agenda-buffer-name
4680 (if (stringp org-select-this-todo-keyword)
4681 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4682 org-select-this-todo-keyword)
4683 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4684 (org-agenda-prepare "TODO")
4685 (org-compile-prefix-format 'todo)
4686 (org-set-sorting-strategy 'todo)
4687 (setq org-agenda-redo-command
4688 `(org-todo-list (or (and (numberp current-prefix-arg)
4689 current-prefix-arg)
4690 ,org-select-this-todo-keyword
4691 current-prefix-arg ,arg)))
4692 (setq files (org-agenda-files nil 'ifmode)
4693 rtnall nil)
4694 (while (setq file (pop files))
4695 (catch 'nextfile
4696 (org-check-agenda-file file)
4697 (setq rtn (org-agenda-get-day-entries file date :todo))
4698 (setq rtnall (append rtnall rtn))))
4699 (org-agenda--insert-overriding-header
4700 (with-temp-buffer
4701 (insert "Global list of TODO items of type: ")
4702 (add-text-properties (point-min) (1- (point))
4703 (list 'face 'org-agenda-structure
4704 'short-heading
4705 (concat "ToDo: "
4706 (or org-select-this-todo-keyword "ALL"))))
4707 (org-agenda-mark-header-line (point-min))
4708 (insert (org-agenda-propertize-selected-todo-keywords
4709 org-select-this-todo-keyword))
4710 (setq pos (point))
4711 (unless org-agenda-multi
4712 (insert (substitute-command-keys "Available with \
4713 \\<org-agenda-mode-map>`N \\[org-agenda-redo]': (0)[ALL]"))
4714 (let ((n 0))
4715 (dolist (k kwds)
4716 (let ((s (format "(%d)%s" (cl-incf n) k)))
4717 (when (> (+ (current-column) (string-width s) 1) (window-width))
4718 (insert "\n "))
4719 (insert " " s))))
4720 (insert "\n"))
4721 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure))
4722 (buffer-string)))
4723 (org-agenda-mark-header-line (point-min))
4724 (when rtnall
4725 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4726 (goto-char (point-min))
4727 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4728 (add-text-properties (point-min) (point-max)
4729 `(org-agenda-type todo
4730 org-last-args ,arg
4731 org-redo-cmd ,org-agenda-redo-command
4732 org-series-cmd ,org-cmd))
4733 (org-agenda-finalize)
4734 (setq buffer-read-only t))))
4736 ;;; Agenda tags match
4738 ;;;###autoload
4739 (defun org-tags-view (&optional todo-only match)
4740 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4741 The prefix arg TODO-ONLY limits the search to TODO entries."
4742 (interactive "P")
4743 (if org-agenda-overriding-arguments
4744 (setq todo-only (car org-agenda-overriding-arguments)
4745 match (nth 1 org-agenda-overriding-arguments)))
4746 (let* ((org-tags-match-list-sublevels
4747 org-tags-match-list-sublevels)
4748 (completion-ignore-case t)
4749 (org--matcher-tags-todo-only todo-only)
4750 rtn rtnall files file pos matcher
4751 buffer)
4752 (when (and (stringp match) (not (string-match "\\S-" match)))
4753 (setq match nil))
4754 (catch 'exit
4755 (if org-agenda-sticky
4756 (setq org-agenda-buffer-name
4757 (if (stringp match)
4758 (format "*Org Agenda(%s:%s)*"
4759 (or org-keys (or (and todo-only "M") "m")) match)
4760 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4761 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4762 ;; expanding tags within `org-make-tags-matcher'
4763 (org-agenda-prepare (concat "TAGS " match))
4764 (setq matcher (org-make-tags-matcher match)
4765 match (car matcher)
4766 matcher (cdr matcher))
4767 (org-compile-prefix-format 'tags)
4768 (org-set-sorting-strategy 'tags)
4769 (setq org-agenda-query-string match)
4770 (setq org-agenda-redo-command
4771 (list 'org-tags-view
4772 `(quote ,org--matcher-tags-todo-only)
4773 `(if current-prefix-arg nil ,org-agenda-query-string)))
4774 (setq files (org-agenda-files nil 'ifmode)
4775 rtnall nil)
4776 (while (setq file (pop files))
4777 (catch 'nextfile
4778 (org-check-agenda-file file)
4779 (setq buffer (if (file-exists-p file)
4780 (org-get-agenda-file-buffer file)
4781 (error "No such file %s" file)))
4782 (if (not buffer)
4783 ;; If file does not exist, error message to agenda
4784 (setq rtn (list
4785 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4786 rtnall (append rtnall rtn))
4787 (with-current-buffer buffer
4788 (unless (derived-mode-p 'org-mode)
4789 (error "Agenda file %s is not in `org-mode'" file))
4790 (save-excursion
4791 (save-restriction
4792 (if (eq buffer org-agenda-restrict)
4793 (narrow-to-region org-agenda-restrict-begin
4794 org-agenda-restrict-end)
4795 (widen))
4796 (setq rtn (org-scan-tags 'agenda
4797 matcher
4798 org--matcher-tags-todo-only))
4799 (setq rtnall (append rtnall rtn))))))))
4800 (org-agenda--insert-overriding-header
4801 (with-temp-buffer
4802 (insert "Headlines with TAGS match: ")
4803 (add-text-properties (point-min) (1- (point))
4804 (list 'face 'org-agenda-structure
4805 'short-heading
4806 (concat "Match: " match)))
4807 (setq pos (point))
4808 (insert match "\n")
4809 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4810 (setq pos (point))
4811 (unless org-agenda-multi
4812 (insert (substitute-command-keys
4813 "Press \
4814 \\<org-agenda-mode-map>`\\[universal-argument] \\[org-agenda-redo]' \
4815 to search again with new search string\n")))
4816 (add-text-properties pos (1- (point))
4817 (list 'face 'org-agenda-structure))
4818 (buffer-string)))
4819 (org-agenda-mark-header-line (point-min))
4820 (when rtnall
4821 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4822 (goto-char (point-min))
4823 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4824 (add-text-properties
4825 (point-min) (point-max)
4826 `(org-agenda-type tags
4827 org-last-args (,org--matcher-tags-todo-only ,match)
4828 org-redo-cmd ,org-agenda-redo-command
4829 org-series-cmd ,org-cmd))
4830 (org-agenda-finalize)
4831 (setq buffer-read-only t))))
4833 ;;; Agenda Finding stuck projects
4835 (defvar org-agenda-skip-regexp nil
4836 "Regular expression used in skipping subtrees for the agenda.
4837 This is basically a temporary global variable that can be set and then
4838 used by user-defined selections using `org-agenda-skip-function'.")
4840 (defvar org-agenda-overriding-header nil
4841 "When set during agenda, todo and tags searches it replaces the header.
4842 If an empty string, no header will be inserted. If any other
4843 string, it will be inserted as a header. If nil, a header will
4844 be generated automatically according to the command. This
4845 variable should not be set directly, but custom commands can bind
4846 it in the options section.")
4848 (defun org-agenda-skip-entry-if (&rest conditions)
4849 "Skip entry if any of CONDITIONS is true.
4850 See `org-agenda-skip-if' for details."
4851 (org-agenda-skip-if nil conditions))
4853 (defun org-agenda-skip-subtree-if (&rest conditions)
4854 "Skip subtree if any of CONDITIONS is true.
4855 See `org-agenda-skip-if' for details."
4856 (org-agenda-skip-if t conditions))
4858 (defun org-agenda-skip-if (subtree conditions)
4859 "Checks current entity for CONDITIONS.
4860 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4861 the entry (i.e. the text before the next heading) is checked.
4863 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4864 from different tests. Valid conditions are:
4866 scheduled Check if there is a scheduled cookie
4867 notscheduled Check if there is no scheduled cookie
4868 deadline Check if there is a deadline
4869 notdeadline Check if there is no deadline
4870 timestamp Check if there is a timestamp (also deadline or scheduled)
4871 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4872 regexp Check if regexp matches
4873 notregexp Check if regexp does not match.
4874 todo Check if TODO keyword matches
4875 nottodo Check if TODO keyword does not match
4877 The regexp is taken from the conditions list, it must come right after
4878 the `regexp' or `notregexp' element.
4880 `todo' and `nottodo' accept as an argument a list of todo
4881 keywords, which may include \"*\" to match any todo keyword.
4883 (org-agenda-skip-entry-if \\='todo \\='(\"TODO\" \"WAITING\"))
4885 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4887 Instead of a list, a keyword class may be given. For example:
4889 (org-agenda-skip-entry-if \\='nottodo \\='done)
4891 would skip entries that haven't been marked with any of \"DONE\"
4892 keywords. Possible classes are: `todo', `done', `any'.
4894 If any of these conditions is met, this function returns the end point of
4895 the entity, causing the search to continue from there. This is a function
4896 that can be put into `org-agenda-skip-function' for the duration of a command."
4897 (org-back-to-heading t)
4898 (let* ((beg (point))
4899 (end (if subtree (save-excursion (org-end-of-subtree t) (point))
4900 (org-entry-end-position)))
4901 (planning-end (if subtree end (line-end-position 2)))
4903 (and
4904 (or (and (memq 'scheduled conditions)
4905 (re-search-forward org-scheduled-time-regexp planning-end t))
4906 (and (memq 'notscheduled conditions)
4907 (not
4908 (save-excursion
4909 (re-search-forward org-scheduled-time-regexp planning-end t))))
4910 (and (memq 'deadline conditions)
4911 (re-search-forward org-deadline-time-regexp planning-end t))
4912 (and (memq 'notdeadline conditions)
4913 (not
4914 (save-excursion
4915 (re-search-forward org-deadline-time-regexp planning-end t))))
4916 (and (memq 'timestamp conditions)
4917 (re-search-forward org-ts-regexp end t))
4918 (and (memq 'nottimestamp conditions)
4919 (not (save-excursion (re-search-forward org-ts-regexp end t))))
4920 (and (setq m (memq 'regexp conditions))
4921 (stringp (nth 1 m))
4922 (re-search-forward (nth 1 m) end t))
4923 (and (setq m (memq 'notregexp conditions))
4924 (stringp (nth 1 m))
4925 (not (save-excursion (re-search-forward (nth 1 m) end t))))
4926 (and (or
4927 (setq m (memq 'nottodo conditions))
4928 (setq m (memq 'todo-unblocked conditions))
4929 (setq m (memq 'nottodo-unblocked conditions))
4930 (setq m (memq 'todo conditions)))
4931 (org-agenda-skip-if-todo m end)))
4932 end)))
4934 (defun org-agenda-skip-if-todo (args end)
4935 "Helper function for `org-agenda-skip-if', do not use it directly.
4936 ARGS is a list with first element either `todo', `nottodo',
4937 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
4938 a list of TODO keywords, or a state symbol `todo' or `done' or
4939 `any'."
4940 (let ((todo-re
4941 (concat "^\\*+[ \t]+"
4942 (regexp-opt
4943 (pcase args
4944 (`(,_ todo)
4945 (org-delete-all org-done-keywords
4946 (copy-sequence org-todo-keywords-1)))
4947 (`(,_ done) org-done-keywords)
4948 (`(,_ any) org-todo-keywords-1)
4949 (`(,_ ,(pred atom))
4950 (error "Invalid TODO class or type: %S" args))
4951 (`(,_ ,(pred (member "*"))) org-todo-keywords-1)
4952 (`(,_ ,todo-list) todo-list))
4953 'words))))
4954 (pcase args
4955 (`(todo . ,_)
4956 (let (case-fold-search) (re-search-forward todo-re end t)))
4957 (`(nottodo . ,_)
4958 (not (let (case-fold-search) (re-search-forward todo-re end t))))
4959 (`(todo-unblocked . ,_)
4960 (catch :unblocked
4961 (while (let (case-fold-search) (re-search-forward todo-re end t))
4962 (when (org-entry-blocked-p) (throw :unblocked t)))
4963 nil))
4964 (`(nottodo-unblocked . ,_)
4965 (catch :unblocked
4966 (while (let (case-fold-search) (re-search-forward todo-re end t))
4967 (when (org-entry-blocked-p) (throw :unblocked nil)))
4969 (`(,type . ,_) (error "Unknown TODO skip type: %S" type)))))
4971 ;;;###autoload
4972 (defun org-agenda-list-stuck-projects (&rest ignore)
4973 "Create agenda view for projects that are stuck.
4974 Stuck projects are project that have no next actions. For the definitions
4975 of what a project is and how to check if it stuck, customize the variable
4976 `org-stuck-projects'."
4977 (interactive)
4978 (let* ((org-agenda-overriding-header
4979 (or org-agenda-overriding-header "List of stuck projects: "))
4980 (matcher (nth 0 org-stuck-projects))
4981 (todo (nth 1 org-stuck-projects))
4982 (tags (nth 2 org-stuck-projects))
4983 (gen-re (org-string-nw-p (nth 3 org-stuck-projects)))
4984 (todo-wds
4985 (if (not (member "*" todo)) todo
4986 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
4987 (org-delete-all org-done-keywords-for-agenda
4988 (copy-sequence org-todo-keywords-for-agenda))))
4989 (todo-re (and todo
4990 (format "^\\*+[ \t]+\\(%s\\)\\>"
4991 (mapconcat #'identity todo-wds "\\|"))))
4992 (tags-re (cond ((null tags) nil)
4993 ((member "*" tags)
4994 (eval-when-compile
4995 (concat org-outline-regexp-bol
4996 ".*:[[:alnum:]_@#%]+:[ \t]*$")))
4997 (tags (concat org-outline-regexp-bol
4998 ".*:\\("
4999 (mapconcat #'identity tags "\\|")
5000 "\\):[[:alnum:]_@#%:]*[ \t]*$"))
5001 (t nil)))
5002 (re-list (delq nil (list todo-re tags-re gen-re)))
5003 (skip-re
5004 (if (null re-list)
5005 (error "Missing information to identify unstuck projects")
5006 (mapconcat #'identity re-list "\\|")))
5007 (org-agenda-skip-function
5008 ;; Skip entry if `org-agenda-skip-regexp' matches anywhere
5009 ;; in the subtree.
5010 `(lambda ()
5011 (and (save-excursion
5012 (let ((case-fold-search nil))
5013 (re-search-forward
5014 ,skip-re (save-excursion (org-end-of-subtree t)) t)))
5015 (progn (outline-next-heading) (point))))))
5016 (org-tags-view nil matcher)
5017 (setq org-agenda-buffer-name (buffer-name))
5018 (with-current-buffer org-agenda-buffer-name
5019 (setq org-agenda-redo-command
5020 `(org-agenda-list-stuck-projects ,current-prefix-arg))
5021 (let ((inhibit-read-only t))
5022 (add-text-properties
5023 (point-min) (point-max)
5024 `(org-redo-cmd ,org-agenda-redo-command))))))
5026 ;;; Diary integration
5028 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
5029 (defvar diary-list-entries-hook)
5030 (defvar diary-time-regexp)
5031 (defun org-get-entries-from-diary (date)
5032 "Get the (Emacs Calendar) diary entries for DATE."
5033 (require 'diary-lib)
5034 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
5035 (diary-display-function 'diary-fancy-display)
5036 (pop-up-frames nil)
5037 (diary-list-entries-hook
5038 (cons 'org-diary-default-entry diary-list-entries-hook))
5039 (diary-file-name-prefix nil) ; turn this feature off
5040 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
5041 entries
5042 (org-disable-agenda-to-diary t))
5043 (save-excursion
5044 (save-window-excursion
5045 (funcall (if (fboundp 'diary-list-entries)
5046 'diary-list-entries 'list-diary-entries)
5047 date 1)))
5048 (if (not (get-buffer diary-fancy-buffer))
5049 (setq entries nil)
5050 (with-current-buffer diary-fancy-buffer
5051 (setq buffer-read-only nil)
5052 (if (zerop (buffer-size))
5053 ;; No entries
5054 (setq entries nil)
5055 ;; Omit the date and other unnecessary stuff
5056 (org-agenda-cleanup-fancy-diary)
5057 ;; Add prefix to each line and extend the text properties
5058 (if (zerop (buffer-size))
5059 (setq entries nil)
5060 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
5061 (setq entries
5062 (with-temp-buffer
5063 (insert entries) (goto-char (point-min))
5064 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5065 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5066 (replace-match (concat "; " (match-string 1)))))
5067 (buffer-string)))))
5068 (set-buffer-modified-p nil)
5069 (kill-buffer diary-fancy-buffer)))
5070 (when entries
5071 (setq entries (org-split-string entries "\n"))
5072 (setq entries
5073 (mapcar
5074 (lambda (x)
5075 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5076 ;; Extend the text properties to the beginning of the line
5077 (org-add-props x (text-properties-at (1- (length x)) x)
5078 'type "diary" 'date date 'face 'org-agenda-diary))
5079 entries)))))
5081 (defvar org-agenda-cleanup-fancy-diary-hook nil
5082 "Hook run when the fancy diary buffer is cleaned up.")
5084 (defun org-agenda-cleanup-fancy-diary ()
5085 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5086 This gets rid of the date, the underline under the date, and
5087 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5088 date. It also removes lines that contain only whitespace."
5089 (goto-char (point-min))
5090 (if (looking-at ".*?:[ \t]*")
5091 (progn
5092 (replace-match "")
5093 (re-search-forward "\n=+$" nil t)
5094 (replace-match "")
5095 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5096 (re-search-forward "\n=+$" nil t)
5097 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5098 (goto-char (point-min))
5099 (while (re-search-forward "^ +\n" nil t)
5100 (replace-match ""))
5101 (goto-char (point-min))
5102 (if (re-search-forward "^Org mode dummy\n?" nil t)
5103 (replace-match ""))
5104 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5106 ;; Make sure entries from the diary have the right text properties.
5107 (eval-after-load "diary-lib"
5108 '(if (boundp 'diary-modify-entry-list-string-function)
5109 ;; We can rely on the hook, nothing to do
5111 ;; Hook not available, must use advice to make this work
5112 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5113 "Make the position visible."
5114 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5115 (stringp string)
5116 buffer-file-name)
5117 (setq string (org-modify-diary-entry-string string))))))
5119 (defun org-modify-diary-entry-string (string)
5120 "Add text properties to string, allowing Org to act on it."
5121 (org-add-props string nil
5122 'mouse-face 'highlight
5123 'help-echo (if buffer-file-name
5124 (format "mouse-2 or RET jump to diary file %s"
5125 (abbreviate-file-name buffer-file-name))
5127 'org-agenda-diary-link t
5128 'org-marker (org-agenda-new-marker (point-at-bol))))
5130 (defun org-diary-default-entry ()
5131 "Add a dummy entry to the diary.
5132 Needed to avoid empty dates which mess up holiday display."
5133 ;; Catch the error if dealing with the new add-to-diary-alist
5134 (when org-disable-agenda-to-diary
5135 (condition-case nil
5136 (org-add-to-diary-list original-date "Org mode dummy" "")
5137 (error
5138 (org-add-to-diary-list original-date "Org mode dummy" "" nil)))))
5140 (defun org-add-to-diary-list (&rest args)
5141 (if (fboundp 'diary-add-to-list)
5142 (apply 'diary-add-to-list args)
5143 (apply 'add-to-diary-list args)))
5145 (defvar org-diary-last-run-time nil)
5147 ;;;###autoload
5148 (defun org-diary (&rest args)
5149 "Return diary information from org files.
5150 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5151 It accesses org files and extracts information from those files to be
5152 listed in the diary. The function accepts arguments specifying what
5153 items should be listed. For a list of arguments allowed here, see the
5154 variable `org-agenda-entry-types'.
5156 The call in the diary file should look like this:
5158 &%%(org-diary) ~/path/to/some/orgfile.org
5160 Use a separate line for each org file to check. Or, if you omit the file name,
5161 all files listed in `org-agenda-files' will be checked automatically:
5163 &%%(org-diary)
5165 If you don't give any arguments (as in the example above), the default value
5166 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5167 So the example above may also be written as
5169 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5171 The function expects the lisp variables `entry' and `date' to be provided
5172 by the caller, because this is how the calendar works. Don't use this
5173 function from a program - use `org-agenda-get-day-entries' instead."
5174 (when (> (- (float-time)
5175 org-agenda-last-marker-time)
5177 ;; I am not sure if this works with sticky agendas, because the marker
5178 ;; list is then no longer a global variable.
5179 (org-agenda-reset-markers))
5180 (org-compile-prefix-format 'agenda)
5181 (org-set-sorting-strategy 'agenda)
5182 (setq args (or args org-agenda-entry-types))
5183 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5184 (list entry)
5185 (org-agenda-files t)))
5186 (time (float-time))
5187 file rtn results)
5188 (when (or (not org-diary-last-run-time)
5189 (> (- time
5190 org-diary-last-run-time)
5192 (org-agenda-prepare-buffers files))
5193 (setq org-diary-last-run-time time)
5194 ;; If this is called during org-agenda, don't return any entries to
5195 ;; the calendar. Org Agenda will list these entries itself.
5196 (if org-disable-agenda-to-diary (setq files nil))
5197 (while (setq file (pop files))
5198 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5199 (setq results (append results rtn)))
5200 (when results
5201 (setq results
5202 (mapcar (lambda (i) (replace-regexp-in-string
5203 org-bracket-link-regexp "\\3" i)) results))
5204 (concat (org-agenda-finalize-entries results) "\n"))))
5206 ;;; Agenda entry finders
5208 (defun org-agenda--timestamp-to-absolute (&rest args)
5209 "Call `org-time-string-to-absolute' with ARGS.
5210 However, throw `:skip' whenever an error is raised."
5211 (condition-case e
5212 (apply #'org-time-string-to-absolute args)
5213 (org-diary-sexp-no-match (throw :skip nil))
5214 (error
5215 (message "%s; Skipping entry" (error-message-string e))
5216 (throw :skip nil))))
5218 (defun org-agenda-get-day-entries (file date &rest args)
5219 "Does the work for `org-diary' and `org-agenda'.
5220 FILE is the path to a file to be checked for entries. DATE is date like
5221 the one returned by `calendar-current-date'. ARGS are symbols indicating
5222 which kind of entries should be extracted. For details about these, see
5223 the documentation of `org-diary'."
5224 (let* ((org-startup-folded nil)
5225 (org-startup-align-all-tables nil)
5226 (buffer (if (file-exists-p file) (org-get-agenda-file-buffer file)
5227 (error "No such file %s" file))))
5228 (if (not buffer)
5229 ;; If file does not exist, signal it in diary nonetheless.
5230 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5231 (with-current-buffer buffer
5232 (unless (derived-mode-p 'org-mode)
5233 (error "Agenda file %s is not in `org-mode'" file))
5234 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5235 (setf org-agenda-current-date date)
5236 (save-excursion
5237 (save-restriction
5238 (if (eq buffer org-agenda-restrict)
5239 (narrow-to-region org-agenda-restrict-begin
5240 org-agenda-restrict-end)
5241 (widen))
5242 ;; Rationalize ARGS. Also make sure `:deadline' comes
5243 ;; first in order to populate DEADLINES before passing it.
5245 ;; We use `delq' since `org-uniquify' duplicates ARGS,
5246 ;; guarding us from modifying `org-agenda-entry-types'.
5247 (setf args (org-uniquify (or args org-agenda-entry-types)))
5248 (when (and (memq :scheduled args) (memq :scheduled* args))
5249 (setf args (delq :scheduled* args)))
5250 (cond
5251 ((memq :deadline args)
5252 (setf args (cons :deadline
5253 (delq :deadline (delq :deadline* args)))))
5254 ((memq :deadline* args)
5255 (setf args (cons :deadline* (delq :deadline* args)))))
5256 ;; Collect list of headlines. Return them flattened.
5257 (let ((case-fold-search nil) results deadlines)
5258 (dolist (arg args (apply #'nconc (nreverse results)))
5259 (pcase arg
5260 ((and :todo (guard (org-agenda-today-p date)))
5261 (push (org-agenda-get-todos) results))
5262 (:timestamp
5263 (push (org-agenda-get-blocks) results)
5264 (push (org-agenda-get-timestamps deadlines) results))
5265 (:sexp
5266 (push (org-agenda-get-sexps) results))
5267 (:scheduled
5268 (push (org-agenda-get-scheduled deadlines) results))
5269 (:scheduled*
5270 (push (org-agenda-get-scheduled deadlines t) results))
5271 (:closed
5272 (push (org-agenda-get-progress) results))
5273 (:deadline
5274 (setf deadlines (org-agenda-get-deadlines))
5275 (push deadlines results))
5276 (:deadline*
5277 (setf deadlines (org-agenda-get-deadlines t))
5278 (push deadlines results)))))))))))
5280 (defsubst org-em (x y list)
5281 "Is X or Y a member of LIST?"
5282 (or (memq x list) (memq y list)))
5284 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5285 (defvar org-agenda-sorting-strategy-selected nil)
5287 (defun org-agenda-entry-get-agenda-timestamp (pom)
5288 "Retrieve timestamp information for sorting agenda views.
5289 Given a point or marker POM, returns a cons cell of the timestamp
5290 and the timestamp type relevant for the sorting strategy in
5291 `org-agenda-sorting-strategy-selected'."
5292 (let (ts ts-date-type)
5293 (save-match-data
5294 (cond ((org-em 'scheduled-up 'scheduled-down
5295 org-agenda-sorting-strategy-selected)
5296 (setq ts (org-entry-get pom "SCHEDULED")
5297 ts-date-type " scheduled"))
5298 ((org-em 'deadline-up 'deadline-down
5299 org-agenda-sorting-strategy-selected)
5300 (setq ts (org-entry-get pom "DEADLINE")
5301 ts-date-type " deadline"))
5302 ((org-em 'ts-up 'ts-down
5303 org-agenda-sorting-strategy-selected)
5304 (setq ts (org-entry-get pom "TIMESTAMP")
5305 ts-date-type " timestamp"))
5306 ((org-em 'tsia-up 'tsia-down
5307 org-agenda-sorting-strategy-selected)
5308 (setq ts (org-entry-get pom "TIMESTAMP_IA")
5309 ts-date-type " timestamp_ia"))
5310 ((org-em 'timestamp-up 'timestamp-down
5311 org-agenda-sorting-strategy-selected)
5312 (setq ts (or (org-entry-get pom "SCHEDULED")
5313 (org-entry-get pom "DEADLINE")
5314 (org-entry-get pom "TIMESTAMP")
5315 (org-entry-get pom "TIMESTAMP_IA"))
5316 ts-date-type ""))
5317 (t (setq ts-date-type "")))
5318 (cons (when ts (ignore-errors (org-time-string-to-absolute ts)))
5319 ts-date-type))))
5321 (defun org-agenda-get-todos ()
5322 "Return the TODO information for agenda display."
5323 (let* ((props (list 'face nil
5324 'done-face 'org-agenda-done
5325 'org-not-done-regexp org-not-done-regexp
5326 'org-todo-regexp org-todo-regexp
5327 'org-complex-heading-regexp org-complex-heading-regexp
5328 'mouse-face 'highlight
5329 'help-echo
5330 (format "mouse-2 or RET jump to org file %s"
5331 (abbreviate-file-name buffer-file-name))))
5332 (case-fold-search nil)
5333 (regexp (format org-heading-keyword-regexp-format
5334 (cond
5335 ((and org-select-this-todo-keyword
5336 (equal org-select-this-todo-keyword "*"))
5337 org-todo-regexp)
5338 (org-select-this-todo-keyword
5339 (concat "\\("
5340 (mapconcat 'identity
5341 (org-split-string
5342 org-select-this-todo-keyword
5343 "|")
5344 "\\|") "\\)"))
5345 (t org-not-done-regexp))))
5346 marker priority category level tags todo-state
5347 ts-date ts-date-type ts-date-pair
5348 ee txt beg end inherited-tags todo-state-end-pos)
5349 (goto-char (point-min))
5350 (while (re-search-forward regexp nil t)
5351 (catch :skip
5352 (save-match-data
5353 (beginning-of-line)
5354 (org-agenda-skip)
5355 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5356 (unless (and (setq todo-state (org-get-todo-state))
5357 (setq todo-state-end-pos (match-end 2)))
5358 (goto-char end)
5359 (throw :skip nil))
5360 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5361 (goto-char (1+ beg))
5362 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5363 (throw :skip nil)))
5364 (goto-char (match-beginning 2))
5365 (setq marker (org-agenda-new-marker (match-beginning 0))
5366 category (org-get-category)
5367 ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
5368 ts-date (car ts-date-pair)
5369 ts-date-type (cdr ts-date-pair)
5370 txt (org-trim (buffer-substring (match-beginning 2) (match-end 0)))
5371 inherited-tags
5372 (or (eq org-agenda-show-inherited-tags 'always)
5373 (and (listp org-agenda-show-inherited-tags)
5374 (memq 'todo org-agenda-show-inherited-tags))
5375 (and (eq org-agenda-show-inherited-tags t)
5376 (or (eq org-agenda-use-tag-inheritance t)
5377 (memq 'todo org-agenda-use-tag-inheritance))))
5378 tags (org-get-tags-at nil (not inherited-tags))
5379 level (make-string (org-reduced-level (org-outline-level)) ? )
5380 txt (org-agenda-format-item "" txt level category tags t)
5381 priority (1+ (org-get-priority txt)))
5382 (org-add-props txt props
5383 'org-marker marker 'org-hd-marker marker
5384 'priority priority
5385 'level level
5386 'ts-date ts-date
5387 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5388 (push txt ee)
5389 (if org-agenda-todo-list-sublevels
5390 (goto-char todo-state-end-pos)
5391 (org-end-of-subtree 'invisible))))
5392 (nreverse ee)))
5394 (defun org-agenda-todo-custom-ignore-p (time n)
5395 "Check whether timestamp is farther away than n number of days.
5396 This function is invoked if `org-agenda-todo-ignore-deadlines',
5397 `org-agenda-todo-ignore-scheduled' or
5398 `org-agenda-todo-ignore-timestamp' is set to an integer."
5399 (let ((days (org-time-stamp-to-now
5400 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5401 (if (>= n 0)
5402 (>= days n)
5403 (<= days n))))
5405 ;;;###autoload
5406 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5407 (&optional end)
5408 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5409 (when (or org-agenda-todo-ignore-with-date
5410 org-agenda-todo-ignore-scheduled
5411 org-agenda-todo-ignore-deadlines
5412 org-agenda-todo-ignore-timestamp)
5413 (setq end (or end (save-excursion (outline-next-heading) (point))))
5414 (save-excursion
5415 (or (and org-agenda-todo-ignore-with-date
5416 (re-search-forward org-ts-regexp end t))
5417 (and org-agenda-todo-ignore-scheduled
5418 (re-search-forward org-scheduled-time-regexp end t)
5419 (cond
5420 ((eq org-agenda-todo-ignore-scheduled 'future)
5421 (> (org-time-stamp-to-now
5422 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5423 ((eq org-agenda-todo-ignore-scheduled 'past)
5424 (<= (org-time-stamp-to-now
5425 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5426 ((numberp org-agenda-todo-ignore-scheduled)
5427 (org-agenda-todo-custom-ignore-p
5428 (match-string 1) org-agenda-todo-ignore-scheduled))
5429 (t)))
5430 (and org-agenda-todo-ignore-deadlines
5431 (re-search-forward org-deadline-time-regexp end t)
5432 (cond
5433 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5434 ((eq org-agenda-todo-ignore-deadlines 'far)
5435 (not (org-deadline-close-p (match-string 1))))
5436 ((eq org-agenda-todo-ignore-deadlines 'future)
5437 (> (org-time-stamp-to-now
5438 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5439 ((eq org-agenda-todo-ignore-deadlines 'past)
5440 (<= (org-time-stamp-to-now
5441 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5442 ((numberp org-agenda-todo-ignore-deadlines)
5443 (org-agenda-todo-custom-ignore-p
5444 (match-string 1) org-agenda-todo-ignore-deadlines))
5445 (t (org-deadline-close-p (match-string 1)))))
5446 (and org-agenda-todo-ignore-timestamp
5447 (let ((buffer (current-buffer))
5448 (regexp
5449 (concat
5450 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5451 (start (point)))
5452 ;; Copy current buffer into a temporary one
5453 (with-temp-buffer
5454 (insert-buffer-substring buffer start end)
5455 (goto-char (point-min))
5456 ;; Delete SCHEDULED and DEADLINE items
5457 (while (re-search-forward regexp end t)
5458 (delete-region (match-beginning 0) (match-end 0)))
5459 (goto-char (point-min))
5460 ;; No search for timestamp left
5461 (when (re-search-forward org-ts-regexp nil t)
5462 (cond
5463 ((eq org-agenda-todo-ignore-timestamp 'future)
5464 (> (org-time-stamp-to-now
5465 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5466 ((eq org-agenda-todo-ignore-timestamp 'past)
5467 (<= (org-time-stamp-to-now
5468 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5469 ((numberp org-agenda-todo-ignore-timestamp)
5470 (org-agenda-todo-custom-ignore-p
5471 (match-string 1) org-agenda-todo-ignore-timestamp))
5472 (t))))))))))
5474 (defun org-agenda-get-timestamps (&optional deadlines)
5475 "Return the date stamp information for agenda display.
5476 Optional argument DEADLINES is a list of deadline items to be
5477 displayed in agenda view."
5478 (let* ((props (list 'face 'org-agenda-calendar-event
5479 'org-not-done-regexp org-not-done-regexp
5480 'org-todo-regexp org-todo-regexp
5481 'org-complex-heading-regexp org-complex-heading-regexp
5482 'mouse-face 'highlight
5483 'help-echo
5484 (format "mouse-2 or RET jump to Org file %s"
5485 (abbreviate-file-name buffer-file-name))))
5486 (current (calendar-absolute-from-gregorian date))
5487 (today (org-today))
5488 (deadline-position-alist
5489 (mapcar (lambda (d)
5490 (let ((m (get-text-property 0 'org-hd-marker d)))
5491 (and m (marker-position m))))
5492 deadlines))
5493 ;; Match time-stamps set to current date, time-stamps with
5494 ;; a repeater, and S-exp time-stamps.
5495 (regexp
5496 (concat
5497 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5498 (regexp-quote
5499 (substring
5500 (format-time-string
5501 (car org-time-stamp-formats)
5502 (apply #'encode-time ; DATE bound by calendar
5503 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5504 1 11))
5505 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5506 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5507 timestamp-items)
5508 (goto-char (point-min))
5509 (while (re-search-forward regexp nil t)
5510 ;; Skip date ranges, scheduled and deadlines, which are handled
5511 ;; specially. Also skip time-stamps before first headline as
5512 ;; there would be no entry to add to the agenda. Eventually,
5513 ;; ignore clock entries.
5514 (catch :skip
5515 (save-match-data
5516 (when (or (org-at-date-range-p)
5517 (org-at-planning-p)
5518 (org-before-first-heading-p)
5519 (and org-agenda-include-inactive-timestamps
5520 (org-at-clock-log-p)))
5521 (throw :skip nil))
5522 (org-agenda-skip))
5523 (let* ((pos (match-beginning 0))
5524 (repeat (match-string 1))
5525 (sexp-entry (match-string 3))
5526 (time-stamp (if (or repeat sexp-entry) (match-string 0)
5527 (save-excursion
5528 (goto-char pos)
5529 (looking-at org-ts-regexp-both)
5530 (match-string 0))))
5531 (todo-state (org-get-todo-state))
5532 (warntime (get-text-property (point) 'org-appt-warntime))
5533 (done? (member todo-state org-done-keywords)))
5534 ;; Possibly skip done tasks.
5535 (when (and done? org-agenda-skip-timestamp-if-done)
5536 (throw :skip t))
5537 ;; S-exp entry doesn't match current day: skip it.
5538 (when (and sexp-entry (not (org-diary-sexp-entry sexp-entry "" date)))
5539 (throw :skip nil))
5540 (when repeat
5541 (let* ((past
5542 ;; A repeating time stamp is shown at its base
5543 ;; date and every repeated date up to TODAY. If
5544 ;; `org-agenda-prefer-last-repeat' is non-nil,
5545 ;; however, only the last repeat before today
5546 ;; (inclusive) is shown.
5547 (org-agenda--timestamp-to-absolute
5548 repeat
5549 (if (or (> current today)
5550 (eq org-agenda-prefer-last-repeat t)
5551 (member todo-state org-agenda-prefer-last-repeat))
5552 today
5553 current)
5554 'past (current-buffer) pos))
5555 (future
5556 ;; Display every repeated date past TODAY
5557 ;; (exclusive) unless
5558 ;; `org-agenda-show-future-repeats' is nil. If
5559 ;; this variable is set to `next', only display
5560 ;; the first repeated date after TODAY
5561 ;; (exclusive).
5562 (cond
5563 ((<= current today) past)
5564 ((not org-agenda-show-future-repeats) past)
5566 (let ((base (if (eq org-agenda-show-future-repeats 'next)
5567 (1+ today)
5568 current)))
5569 (org-agenda--timestamp-to-absolute
5570 repeat base 'future (current-buffer) pos))))))
5571 (when (and (/= current past) (/= current future))
5572 (throw :skip nil))))
5573 (save-excursion
5574 (re-search-backward org-outline-regexp-bol nil t)
5575 ;; Possibly skip time-stamp when a deadline is set.
5576 (when (and org-agenda-skip-timestamp-if-deadline-is-shown
5577 (assq (point) deadline-position-alist))
5578 (throw :skip nil))
5579 (let* ((category (org-get-category pos))
5580 (inherited-tags
5581 (or (eq org-agenda-show-inherited-tags 'always)
5582 (and (consp org-agenda-show-inherited-tags)
5583 (memq 'agenda org-agenda-show-inherited-tags))
5584 (and (eq org-agenda-show-inherited-tags t)
5585 (or (eq org-agenda-use-tag-inheritance t)
5586 (memq 'agenda
5587 org-agenda-use-tag-inheritance)))))
5588 (tags (org-get-tags-at nil (not inherited-tags)))
5589 (level (make-string (org-reduced-level (org-outline-level))
5590 ?\s))
5591 (head (and (looking-at "\\*+[ \t]+\\(.*\\)")
5592 (match-string 1)))
5593 (inactive? (= (char-after pos) ?\[))
5594 (habit? (and (fboundp 'org-is-habit-p) (org-is-habit-p)))
5595 (item
5596 (org-agenda-format-item
5597 (and inactive? org-agenda-inactive-leader)
5598 head level category tags time-stamp org-ts-regexp habit?)))
5599 (org-add-props item props
5600 'priority (if habit?
5601 (org-habit-get-priority (org-habit-parse-todo))
5602 (org-get-priority item))
5603 'org-marker (org-agenda-new-marker pos)
5604 'org-hd-marker (org-agenda-new-marker)
5605 'date date
5606 'level level
5607 'ts-date (if repeat (org-agenda--timestamp-to-absolute repeat)
5608 current)
5609 'todo-state todo-state
5610 'warntime warntime
5611 'type "timestamp")
5612 (push item timestamp-items))))
5613 (when org-agenda-skip-additional-timestamps-same-entry
5614 (outline-next-heading))))
5615 (nreverse timestamp-items)))
5617 (defun org-agenda-get-sexps ()
5618 "Return the sexp information for agenda display."
5619 (require 'diary-lib)
5620 (let* ((props (list 'face 'org-agenda-calendar-sexp
5621 'mouse-face 'highlight
5622 'help-echo
5623 (format "mouse-2 or RET jump to org file %s"
5624 (abbreviate-file-name buffer-file-name))))
5625 (regexp "^&?%%(")
5626 marker category extra level ee txt tags entry
5627 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5628 (goto-char (point-min))
5629 (while (re-search-forward regexp nil t)
5630 (catch :skip
5631 (org-agenda-skip)
5632 (setq beg (match-beginning 0))
5633 (goto-char (1- (match-end 0)))
5634 (setq b (point))
5635 (forward-sexp 1)
5636 (setq sexp (buffer-substring b (point)))
5637 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5638 (org-trim (match-string 1))
5639 ""))
5640 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5641 (when result
5642 (setq marker (org-agenda-new-marker beg)
5643 level (make-string (org-reduced-level (org-outline-level)) ? )
5644 category (org-get-category beg)
5645 inherited-tags
5646 (or (eq org-agenda-show-inherited-tags 'always)
5647 (and (listp org-agenda-show-inherited-tags)
5648 (memq 'agenda org-agenda-show-inherited-tags))
5649 (and (eq org-agenda-show-inherited-tags t)
5650 (or (eq org-agenda-use-tag-inheritance t)
5651 (memq 'agenda org-agenda-use-tag-inheritance))))
5652 tags (org-get-tags-at nil (not inherited-tags))
5653 todo-state (org-get-todo-state)
5654 warntime (get-text-property (point) 'org-appt-warntime)
5655 extra nil)
5657 (dolist (r (if (stringp result)
5658 (list result)
5659 result)) ;; we expect a list here
5660 (when (and org-agenda-diary-sexp-prefix
5661 (string-match org-agenda-diary-sexp-prefix r))
5662 (setq extra (match-string 0 r)
5663 r (replace-match "" nil nil r)))
5664 (if (string-match "\\S-" r)
5665 (setq txt r)
5666 (setq txt "SEXP entry returned empty string"))
5667 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5668 (org-add-props txt props 'org-marker marker
5669 'date date 'todo-state todo-state
5670 'level level 'type "sexp" 'warntime warntime)
5671 (push txt ee)))))
5672 (nreverse ee)))
5674 ;; Calendar sanity: define some functions that are independent of
5675 ;; `calendar-date-style'.
5676 (defun org-anniversary (year month day &optional mark)
5677 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5678 (with-no-warnings
5679 (let ((calendar-date-style 'iso))
5680 (diary-anniversary year month day mark))))
5681 (defun org-cyclic (N year month day &optional mark)
5682 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5683 (with-no-warnings
5684 (let ((calendar-date-style 'iso))
5685 (diary-cyclic N year month day mark))))
5686 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5687 "Like `diary-block', but with fixed (ISO) order of arguments."
5688 (with-no-warnings
5689 (let ((calendar-date-style 'iso))
5690 (diary-block Y1 M1 D1 Y2 M2 D2 mark))))
5691 (defun org-date (year month day &optional mark)
5692 "Like `diary-date', but with fixed (ISO) order of arguments."
5693 (with-no-warnings
5694 (let ((calendar-date-style 'iso))
5695 (diary-date year month day mark))))
5697 ;; Define the `org-class' function
5698 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5699 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5700 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5701 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5702 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5703 `holidays', then any date that is known by the Emacs calendar to be a
5704 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5705 then those holidays will be skipped."
5706 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5707 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5708 (d (calendar-absolute-from-gregorian date))
5709 (h (when skip-weeks (calendar-check-holidays date))))
5710 (and
5711 (<= date1 d)
5712 (<= d date2)
5713 (= (calendar-day-of-week date) dayname)
5714 (or (not skip-weeks)
5715 (progn
5716 (require 'cal-iso)
5717 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5718 (not (or (and h (memq 'holidays skip-weeks))
5719 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5720 entry)))
5722 (defalias 'org-get-closed 'org-agenda-get-progress)
5723 (defun org-agenda-get-progress ()
5724 "Return the logged TODO entries for agenda display."
5725 (let* ((props (list 'mouse-face 'highlight
5726 'org-not-done-regexp org-not-done-regexp
5727 'org-todo-regexp org-todo-regexp
5728 'org-complex-heading-regexp org-complex-heading-regexp
5729 'help-echo
5730 (format "mouse-2 or RET jump to org file %s"
5731 (abbreviate-file-name buffer-file-name))))
5732 (items (if (consp org-agenda-show-log-scoped)
5733 org-agenda-show-log-scoped
5734 (if (eq org-agenda-show-log-scoped 'clockcheck)
5735 '(clock)
5736 org-agenda-log-mode-items)))
5737 (parts
5738 (delq nil
5739 (list
5740 (if (memq 'closed items) (concat "\\<" org-closed-string))
5741 (if (memq 'clock items) (concat "\\<" org-clock-string))
5742 (if (memq 'state items)
5743 (format "- State \"%s\".*?" org-todo-regexp)))))
5744 (parts-re (if parts (mapconcat 'identity parts "\\|")
5745 (error "`org-agenda-log-mode-items' is empty")))
5746 (regexp (concat
5747 "\\(" parts-re "\\)"
5748 " *\\["
5749 (regexp-quote
5750 (substring
5751 (format-time-string
5752 (car org-time-stamp-formats)
5753 (apply 'encode-time ; DATE bound by calendar
5754 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5755 1 11))))
5756 (org-agenda-search-headline-for-time nil)
5757 marker hdmarker priority category level tags closedp
5758 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5759 (goto-char (point-min))
5760 (while (re-search-forward regexp nil t)
5761 (catch :skip
5762 (org-agenda-skip)
5763 (setq marker (org-agenda-new-marker (match-beginning 0))
5764 closedp (equal (match-string 1) org-closed-string)
5765 statep (equal (string-to-char (match-string 1)) ?-)
5766 clockp (not (or closedp statep))
5767 state (and statep (match-string 2))
5768 category (org-get-category (match-beginning 0))
5769 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5770 (when (string-match "\\]" timestr)
5771 ;; substring should only run to end of time stamp
5772 (setq rest (substring timestr (match-end 0))
5773 timestr (substring timestr 0 (match-end 0)))
5774 (if (and (not closedp) (not statep)
5775 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5776 rest))
5777 (progn (setq timestr (concat (substring timestr 0 -1)
5778 "-" (match-string 1 rest) "]"))
5779 (setq clocked (match-string 2 rest)))
5780 (setq clocked "-")))
5781 (save-excursion
5782 (setq extra
5783 (cond
5784 ((not org-agenda-log-mode-add-notes) nil)
5785 (statep
5786 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5787 (match-string 1)))
5788 (clockp
5789 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5790 (match-string 1)))))
5791 (if (not (re-search-backward org-outline-regexp-bol nil t))
5792 (throw :skip nil)
5793 (goto-char (match-beginning 0))
5794 (setq hdmarker (org-agenda-new-marker)
5795 inherited-tags
5796 (or (eq org-agenda-show-inherited-tags 'always)
5797 (and (listp org-agenda-show-inherited-tags)
5798 (memq 'todo org-agenda-show-inherited-tags))
5799 (and (eq org-agenda-show-inherited-tags t)
5800 (or (eq org-agenda-use-tag-inheritance t)
5801 (memq 'todo org-agenda-use-tag-inheritance))))
5802 tags (org-get-tags-at nil (not inherited-tags))
5803 level (make-string (org-reduced-level (org-outline-level)) ? ))
5804 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5805 (setq txt (match-string 1))
5806 (when extra
5807 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5808 (setq txt (concat (substring txt 0 (match-beginning 1))
5809 " - " extra " " (match-string 2 txt)))
5810 (setq txt (concat txt " - " extra))))
5811 (setq txt (org-agenda-format-item
5812 (cond
5813 (closedp "Closed: ")
5814 (statep (concat "State: (" state ")"))
5815 (t (concat "Clocked: (" clocked ")")))
5816 txt level category tags timestr)))
5817 (setq priority 100000)
5818 (org-add-props txt props
5819 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5820 'priority priority 'level level
5821 'type "closed" 'date date
5822 'undone-face 'org-warning 'done-face 'org-agenda-done)
5823 (push txt ee))
5824 (goto-char (point-at-eol))))
5825 (nreverse ee)))
5827 (defun org-agenda-show-clocking-issues ()
5828 "Add overlays, showing issues with clocking.
5829 See also the user option `org-agenda-clock-consistency-checks'."
5830 (interactive)
5831 (let* ((pl org-agenda-clock-consistency-checks)
5832 (re (concat "^[ \t]*"
5833 org-clock-string
5834 "[ \t]+"
5835 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5836 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5837 (tlstart 0.)
5838 (tlend 0.)
5839 (maxtime (org-duration-to-minutes
5840 (or (plist-get pl :max-duration) "24:00")))
5841 (mintime (org-duration-to-minutes
5842 (or (plist-get pl :min-duration) 0)))
5843 (maxgap (org-duration-to-minutes
5844 ;; default 30:00 means never complain
5845 (or (plist-get pl :max-gap) "30:00")))
5846 (gapok (mapcar #'org-duration-to-minutes
5847 (plist-get pl :gap-ok-around)))
5848 (def-face (or (plist-get pl :default-face)
5849 '((:background "DarkRed") (:foreground "white"))))
5850 issue face m te ts dt ov)
5851 (goto-char (point-min))
5852 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5853 (setq issue nil face def-face)
5854 (catch 'next
5855 (setq m (org-get-at-bol 'org-marker)
5856 te nil ts nil)
5857 (unless (and m (markerp m))
5858 (setq issue "No valid clock line") (throw 'next t))
5859 (org-with-point-at m
5860 (save-excursion
5861 (goto-char (point-at-bol))
5862 (unless (looking-at re)
5863 (error "No valid Clock line")
5864 (throw 'next t))
5865 (unless (match-end 3)
5866 (setq issue "No end time"
5867 face (or (plist-get pl :no-end-time-face) face))
5868 (throw 'next t))
5869 (setq ts (match-string 1)
5870 te (match-string 3)
5871 ts (float-time
5872 (apply #'encode-time (org-parse-time-string ts)))
5873 te (float-time
5874 (apply #'encode-time (org-parse-time-string te)))
5875 dt (- te ts))))
5876 (cond
5877 ((> dt (* 60 maxtime))
5878 ;; a very long clocking chunk
5879 (setq issue (format "Clocking interval is very long: %s"
5880 (org-duration-from-minutes (floor (/ dt 60.))))
5881 face (or (plist-get pl :long-face) face)))
5882 ((< dt (* 60 mintime))
5883 ;; a very short clocking chunk
5884 (setq issue (format "Clocking interval is very short: %s"
5885 (org-duration-from-minutes (floor (/ dt 60.))))
5886 face (or (plist-get pl :short-face) face)))
5887 ((and (> tlend 0) (< ts tlend))
5888 ;; Two clock entries are overlapping
5889 (setq issue (format "Clocking overlap: %d minutes"
5890 (/ (- tlend ts) 60))
5891 face (or (plist-get pl :overlap-face) face)))
5892 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5893 ;; There is a gap, lets see if we need to report it
5894 (unless (org-agenda-check-clock-gap tlend ts gapok)
5895 (setq issue (format "Clocking gap: %d minutes"
5896 (/ (- ts tlend) 60))
5897 face (or (plist-get pl :gap-face) face))))
5898 (t nil)))
5899 (setq tlend (or te tlend) tlstart (or ts tlstart))
5900 (when issue
5901 ;; OK, there was some issue, add an overlay to show the issue
5902 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5903 (overlay-put ov 'before-string
5904 (concat
5905 (org-add-props
5906 (format "%-43s" (concat " " issue))
5908 'face face)
5909 "\n"))
5910 (overlay-put ov 'evaporate t)))))
5912 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5913 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5914 (catch 'exit
5915 (unless ok-list
5916 ;; there are no OK times for gaps...
5917 (throw 'exit nil))
5918 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5919 ;; This is more than 24 hours, so it is OK.
5920 ;; because we have at least one OK time, that must be in the
5921 ;; 24 hour interval.
5922 (throw 'exit t))
5923 ;; We have a shorter gap.
5924 ;; Now we have to get the minute of the day when these times are
5925 (let* ((t1dec (decode-time (seconds-to-time t1)))
5926 (t2dec (decode-time (seconds-to-time t2)))
5927 ;; compute the minute on the day
5928 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5929 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5930 (when (< min2 min1)
5931 ;; if min2 is smaller than min1, this means it is on the next day.
5932 ;; Wrap it to after midnight.
5933 (setq min2 (+ min2 1440)))
5934 ;; Now check if any of the OK times is in the gap
5935 (mapc (lambda (x)
5936 ;; Wrap the time to after midnight if necessary
5937 (if (< x min1) (setq x (+ x 1440)))
5938 ;; Check if in interval
5939 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5940 ok-list)
5941 ;; Nope, this gap is not OK
5942 nil)))
5944 (defun org-agenda-get-deadlines (&optional with-hour)
5945 "Return the deadline information for agenda display.
5946 When WITH-HOUR is non-nil, only return deadlines with an hour
5947 specification like [h]h:mm."
5948 (let* ((props (list 'mouse-face 'highlight
5949 'org-not-done-regexp org-not-done-regexp
5950 'org-todo-regexp org-todo-regexp
5951 'org-complex-heading-regexp org-complex-heading-regexp
5952 'help-echo
5953 (format "mouse-2 or RET jump to org file %s"
5954 (abbreviate-file-name buffer-file-name))))
5955 (regexp (if with-hour
5956 org-deadline-time-hour-regexp
5957 org-deadline-time-regexp))
5958 (today (org-today))
5959 (today? (org-agenda-today-p date)) ; DATE bound by calendar.
5960 (current (calendar-absolute-from-gregorian date))
5961 deadline-items)
5962 (goto-char (point-min))
5963 (while (re-search-forward regexp nil t)
5964 (catch :skip
5965 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
5966 (org-agenda-skip)
5967 (let* ((s (match-string 1))
5968 (pos (1- (match-beginning 1)))
5969 (todo-state (save-match-data (org-get-todo-state)))
5970 (done? (member todo-state org-done-keywords))
5971 (sexp? (string-prefix-p "%%" s))
5972 ;; DEADLINE is the deadline date for the entry. It is
5973 ;; either the base date or the last repeat, according
5974 ;; to `org-agenda-prefer-last-repeat'.
5975 (deadline
5976 (cond
5977 (sexp? (org-agenda--timestamp-to-absolute s current))
5978 ((or (eq org-agenda-prefer-last-repeat t)
5979 (member todo-state org-agenda-prefer-last-repeat))
5980 (org-agenda--timestamp-to-absolute
5981 s today 'past (current-buffer) pos))
5982 (t (org-agenda--timestamp-to-absolute s))))
5983 ;; REPEAT is the future repeat closest from CURRENT,
5984 ;; according to `org-agenda-show-future-repeats'. If
5985 ;; the latter is nil, or if the time stamp has no
5986 ;; repeat part, default to DEADLINE.
5987 (repeat
5988 (cond
5989 (sexp? deadline)
5990 ((<= current today) deadline)
5991 ((not org-agenda-show-future-repeats) deadline)
5993 (let ((base (if (eq org-agenda-show-future-repeats 'next)
5994 (1+ today)
5995 current)))
5996 (org-agenda--timestamp-to-absolute
5997 s base 'future (current-buffer) pos)))))
5998 (diff (- deadline current))
5999 (suppress-prewarning
6000 (let ((scheduled
6001 (and org-agenda-skip-deadline-prewarning-if-scheduled
6002 (org-entry-get nil "SCHEDULED"))))
6003 (cond
6004 ((not scheduled) nil)
6005 ;; The current item has a scheduled date, so
6006 ;; evaluate its prewarning lead time.
6007 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
6008 ;; Use global prewarning-restart lead time.
6009 org-agenda-skip-deadline-prewarning-if-scheduled)
6010 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
6011 'pre-scheduled)
6012 ;; Set pre-warning to no earlier than SCHEDULED.
6013 (min (- deadline
6014 (org-agenda--timestamp-to-absolute scheduled))
6015 org-deadline-warning-days))
6016 ;; Set pre-warning to deadline.
6017 (t 0))))
6018 (wdays (if suppress-prewarning
6019 (let ((org-deadline-warning-days suppress-prewarning))
6020 (org-get-wdays s))
6021 (org-get-wdays s))))
6022 (cond
6023 ;; Only display deadlines at their base date, at future
6024 ;; repeat occurrences or in today agenda.
6025 ((= current deadline) nil)
6026 ((= current repeat) nil)
6027 ((not today?) (throw :skip nil))
6028 ;; Upcoming deadline: display within warning period WDAYS.
6029 ((> deadline current) (when (> diff wdays) (throw :skip nil)))
6030 ;; Overdue deadline: warn about it for
6031 ;; `org-deadline-past-days' duration.
6032 (t (when (< org-deadline-past-days (- diff)) (throw :skip nil))))
6033 ;; Possibly skip done tasks.
6034 (when (and done?
6035 (or org-agenda-skip-deadline-if-done
6036 (/= deadline current)))
6037 (throw :skip nil))
6038 (save-excursion
6039 (re-search-backward "^\\*+[ \t]+" nil t)
6040 (goto-char (match-end 0))
6041 (let* ((category (org-get-category))
6042 (level (make-string (org-reduced-level (org-outline-level))
6043 ?\s))
6044 (head (buffer-substring (point) (line-end-position)))
6045 (inherited-tags
6046 (or (eq org-agenda-show-inherited-tags 'always)
6047 (and (listp org-agenda-show-inherited-tags)
6048 (memq 'agenda org-agenda-show-inherited-tags))
6049 (and (eq org-agenda-show-inherited-tags t)
6050 (or (eq org-agenda-use-tag-inheritance t)
6051 (memq 'agenda
6052 org-agenda-use-tag-inheritance)))))
6053 (tags (org-get-tags-at nil (not inherited-tags)))
6054 (time
6055 (cond
6056 ;; No time of day designation if it is only
6057 ;; a reminder.
6058 ((and (/= current deadline) (/= current repeat)) nil)
6059 ((string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6060 (concat (substring s (match-beginning 1)) " "))
6061 (t 'time)))
6062 (item
6063 (org-agenda-format-item
6064 ;; Insert appropriate suffixes before deadlines.
6065 ;; Those only apply to today agenda.
6066 (pcase-let ((`(,now ,future ,past)
6067 org-agenda-deadline-leaders))
6068 (cond
6069 ((and today? (< deadline today)) (format past (- diff)))
6070 ((and today? (> deadline today)) (format future diff))
6071 (t now)))
6072 head level category tags time))
6073 (face (org-agenda-deadline-face
6074 (- 1 (/ (float diff) (max wdays 1)))))
6075 (upcoming? (and today? (> deadline today)))
6076 (warntime (get-text-property (point) 'org-appt-warntime)))
6077 (org-add-props item props
6078 'org-marker (org-agenda-new-marker pos)
6079 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
6080 'warntime warntime
6081 'level level
6082 'ts-date deadline
6083 'priority
6084 ;; Adjust priority to today reminders about deadlines.
6085 ;; Overdue deadlines get the highest priority
6086 ;; increase, then imminent deadlines and eventually
6087 ;; more distant deadlines.
6088 (let ((adjust (if today? (- diff) 0)))
6089 (+ adjust (org-get-priority item)))
6090 'todo-state todo-state
6091 'type (if upcoming? "upcoming-deadline" "deadline")
6092 'date (if upcoming? date deadline)
6093 'face (if done? 'org-agenda-done face)
6094 'undone-face face
6095 'done-face 'org-agenda-done)
6096 (push item deadline-items))))))
6097 (nreverse deadline-items)))
6099 (defun org-agenda-deadline-face (fraction)
6100 "Return the face to displaying a deadline item.
6101 FRACTION is what fraction of the head-warning time has passed."
6102 (assoc-default fraction org-agenda-deadline-faces #'<=))
6104 (defun org-agenda-get-scheduled (&optional deadlines with-hour)
6105 "Return the scheduled information for agenda display.
6106 Optional argument DEADLINES is a list of deadline items to be
6107 displayed in agenda view. When WITH-HOUR is non-nil, only return
6108 scheduled items with an hour specification like [h]h:mm."
6109 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
6110 'org-todo-regexp org-todo-regexp
6111 'org-complex-heading-regexp org-complex-heading-regexp
6112 'done-face 'org-agenda-done
6113 'mouse-face 'highlight
6114 'help-echo
6115 (format "mouse-2 or RET jump to Org file %s"
6116 (abbreviate-file-name buffer-file-name))))
6117 (regexp (if with-hour
6118 org-scheduled-time-hour-regexp
6119 org-scheduled-time-regexp))
6120 (today (org-today))
6121 (todayp (org-agenda-today-p date)) ; DATE bound by calendar.
6122 (current (calendar-absolute-from-gregorian date))
6123 (deadline-pos
6124 (mapcar (lambda (d)
6125 (let ((m (get-text-property 0 'org-hd-marker d)))
6126 (and m (marker-position m))))
6127 deadlines))
6128 scheduled-items)
6129 (goto-char (point-min))
6130 (while (re-search-forward regexp nil t)
6131 (catch :skip
6132 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
6133 (org-agenda-skip)
6134 (let* ((s (match-string 1))
6135 (pos (1- (match-beginning 1)))
6136 (todo-state (save-match-data (org-get-todo-state)))
6137 (donep (member todo-state org-done-keywords))
6138 (sexp? (string-prefix-p "%%" s))
6139 ;; SCHEDULE is the scheduled date for the entry. It is
6140 ;; either the bare date or the last repeat, according
6141 ;; to `org-agenda-prefer-last-repeat'.
6142 (schedule
6143 (cond
6144 (sexp? (org-agenda--timestamp-to-absolute s current))
6145 ((or (eq org-agenda-prefer-last-repeat t)
6146 (member todo-state org-agenda-prefer-last-repeat))
6147 (org-agenda--timestamp-to-absolute
6148 s today 'past (current-buffer) pos))
6149 (t (org-agenda--timestamp-to-absolute s))))
6150 ;; REPEAT is the future repeat closest from CURRENT,
6151 ;; according to `org-agenda-show-future-repeats'. If
6152 ;; the latter is nil, or if the time stamp has no
6153 ;; repeat part, default to SCHEDULE.
6154 (repeat
6155 (cond
6156 (sexp? schedule)
6157 ((<= current today) schedule)
6158 ((not org-agenda-show-future-repeats) schedule)
6160 (let ((base (if (eq org-agenda-show-future-repeats 'next)
6161 (1+ today)
6162 current)))
6163 (org-agenda--timestamp-to-absolute
6164 s base 'future (current-buffer) pos)))))
6165 (diff (- current schedule))
6166 (warntime (get-text-property (point) 'org-appt-warntime))
6167 (pastschedp (< schedule today))
6168 (habitp (and (fboundp 'org-is-habit-p) (org-is-habit-p)))
6169 (suppress-delay
6170 (let ((deadline (and org-agenda-skip-scheduled-delay-if-deadline
6171 (org-entry-get nil "DEADLINE"))))
6172 (cond
6173 ((not deadline) nil)
6174 ;; The current item has a deadline date, so
6175 ;; evaluate its delay time.
6176 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6177 ;; Use global delay time.
6178 (- org-agenda-skip-scheduled-delay-if-deadline))
6179 ((eq org-agenda-skip-scheduled-delay-if-deadline
6180 'post-deadline)
6181 ;; Set delay to no later than DEADLINE.
6182 (min (- schedule
6183 (org-agenda--timestamp-to-absolute deadline))
6184 org-scheduled-delay-days))
6185 (t 0))))
6186 (ddays
6187 (cond
6188 ;; Nullify delay when a repeater triggered already
6189 ;; and the delay is of the form --Xd.
6190 ((and (string-match-p "--[0-9]+[hdwmy]" s)
6191 (> current schedule))
6193 (suppress-delay
6194 (let ((org-scheduled-delay-days suppress-delay))
6195 (org-get-wdays s t t)))
6196 (t (org-get-wdays s t)))))
6197 ;; Display scheduled items at base date (SCHEDULE), today if
6198 ;; scheduled before the current date, and at any repeat past
6199 ;; today. However, skip delayed items and items that have
6200 ;; been displayed for more than `org-scheduled-past-days'.
6201 (unless (and todayp
6202 habitp
6203 (bound-and-true-p org-habit-show-all-today))
6204 (when (or (and (> ddays 0) (< diff ddays))
6205 (> diff org-scheduled-past-days)
6206 (> schedule current)
6207 (and (/= current schedule)
6208 (/= current today)
6209 (/= current repeat)))
6210 (throw :skip nil)))
6211 ;; Possibly skip done tasks.
6212 (when (and donep
6213 (or org-agenda-skip-scheduled-if-done
6214 (/= schedule current)))
6215 (throw :skip nil))
6216 ;; Skip entry if it already appears as a deadline, per
6217 ;; `org-agenda-skip-scheduled-if-deadline-is-shown'. This
6218 ;; doesn't apply to habits.
6219 (when (pcase org-agenda-skip-scheduled-if-deadline-is-shown
6220 ((guard
6221 (or (not (memq (line-beginning-position 0) deadline-pos))
6222 habitp))
6223 nil)
6224 (`repeated-after-deadline
6225 (let ((deadline (time-to-days
6226 (org-get-deadline-time (point)))))
6227 (and (<= schedule deadline) (> current deadline))))
6228 (`not-today pastschedp)
6229 (`t t)
6230 (_ nil))
6231 (throw :skip nil))
6232 ;; Skip habits if `org-habit-show-habits' is nil, or if we
6233 ;; only show them for today. Also skip done habits.
6234 (when (and habitp
6235 (or donep
6236 (not (bound-and-true-p org-habit-show-habits))
6237 (and (not todayp)
6238 (bound-and-true-p
6239 org-habit-show-habits-only-for-today))))
6240 (throw :skip nil))
6241 (save-excursion
6242 (re-search-backward "^\\*+[ \t]+" nil t)
6243 (goto-char (match-end 0))
6244 (let* ((category (org-get-category))
6245 (inherited-tags
6246 (or (eq org-agenda-show-inherited-tags 'always)
6247 (and (listp org-agenda-show-inherited-tags)
6248 (memq 'agenda org-agenda-show-inherited-tags))
6249 (and (eq org-agenda-show-inherited-tags t)
6250 (or (eq org-agenda-use-tag-inheritance t)
6251 (memq 'agenda
6252 org-agenda-use-tag-inheritance)))))
6253 (tags (org-get-tags-at nil (not inherited-tags)))
6254 (level (make-string (org-reduced-level (org-outline-level))
6255 ?\s))
6256 (head (buffer-substring (point) (line-end-position)))
6257 (time
6258 (cond
6259 ;; No time of day designation if it is only
6260 ;; a reminder.
6261 ((and (/= current schedule) (/= current repeat)) nil)
6262 ((string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6263 (concat (substring s (match-beginning 1)) " "))
6264 (t 'time)))
6265 (item
6266 (org-agenda-format-item
6267 (pcase-let ((`(,first ,past) org-agenda-scheduled-leaders))
6268 ;; Show a reminder of a past scheduled today.
6269 (if (and todayp pastschedp)
6270 (format past diff)
6271 first))
6272 head level category tags time nil habitp))
6273 (face (cond ((and (not habitp) pastschedp)
6274 'org-scheduled-previously)
6275 (todayp 'org-scheduled-today)
6276 (t 'org-scheduled)))
6277 (habitp (and habitp (org-habit-parse-todo))))
6278 (org-add-props item props
6279 'undone-face face
6280 'face (if donep 'org-agenda-done face)
6281 'org-marker (org-agenda-new-marker pos)
6282 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
6283 'type (if pastschedp "past-scheduled" "scheduled")
6284 'date (if pastschedp schedule date)
6285 'ts-date schedule
6286 'warntime warntime
6287 'level level
6288 'priority (if habitp (org-habit-get-priority habitp)
6289 (+ 99 diff (org-get-priority item)))
6290 'org-habit-p habitp
6291 'todo-state todo-state)
6292 (push item scheduled-items))))))
6293 (nreverse scheduled-items)))
6295 (defun org-agenda-get-blocks ()
6296 "Return the date-range information for agenda display."
6297 (let* ((props (list 'face nil
6298 'org-not-done-regexp org-not-done-regexp
6299 'org-todo-regexp org-todo-regexp
6300 'org-complex-heading-regexp org-complex-heading-regexp
6301 'mouse-face 'highlight
6302 'help-echo
6303 (format "mouse-2 or RET jump to org file %s"
6304 (abbreviate-file-name buffer-file-name))))
6305 (regexp org-tr-regexp)
6306 (d0 (calendar-absolute-from-gregorian date))
6307 marker hdmarker ee txt d1 d2 s1 s2 category
6308 level todo-state tags pos head donep inherited-tags)
6309 (goto-char (point-min))
6310 (while (re-search-forward regexp nil t)
6311 (catch :skip
6312 (org-agenda-skip)
6313 (setq pos (point))
6314 (let ((start-time (match-string 1))
6315 (end-time (match-string 2)))
6316 (setq s1 (match-string 1)
6317 s2 (match-string 2)
6318 d1 (time-to-days
6319 (condition-case err
6320 (org-time-string-to-time s1)
6321 (error
6322 (error
6323 "Bad timestamp %S at %d in buffer %S\nError was: %s"
6326 (current-buffer)
6327 (error-message-string err)))))
6328 d2 (time-to-days
6329 (condition-case err
6330 (org-time-string-to-time s2)
6331 (error
6332 (error
6333 "Bad timestamp %S at %d in buffer %S\nError was: %s"
6336 (current-buffer)
6337 (error-message-string err))))))
6338 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6339 ;; Only allow days between the limits, because the normal
6340 ;; date stamps will catch the limits.
6341 (save-excursion
6342 (setq todo-state (org-get-todo-state))
6343 (setq donep (member todo-state org-done-keywords))
6344 (if (and donep org-agenda-skip-timestamp-if-done)
6345 (throw :skip t))
6346 (setq marker (org-agenda-new-marker (point))
6347 category (org-get-category))
6348 (if (not (re-search-backward org-outline-regexp-bol nil t))
6349 (throw :skip nil)
6350 (goto-char (match-beginning 0))
6351 (setq hdmarker (org-agenda-new-marker (point))
6352 inherited-tags
6353 (or (eq org-agenda-show-inherited-tags 'always)
6354 (and (listp org-agenda-show-inherited-tags)
6355 (memq 'agenda org-agenda-show-inherited-tags))
6356 (and (eq org-agenda-show-inherited-tags t)
6357 (or (eq org-agenda-use-tag-inheritance t)
6358 (memq 'agenda org-agenda-use-tag-inheritance))))
6360 tags (org-get-tags-at nil (not inherited-tags)))
6361 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6362 (looking-at "\\*+[ \t]+\\(.*\\)")
6363 (setq head (match-string 1))
6364 (let ((remove-re
6365 (if org-agenda-remove-timeranges-from-blocks
6366 (concat
6367 "<" (regexp-quote s1) ".*?>"
6368 "--"
6369 "<" (regexp-quote s2) ".*?>")
6370 nil)))
6371 (setq txt (org-agenda-format-item
6372 (format
6373 (nth (if (= d1 d2) 0 1)
6374 org-agenda-timerange-leaders)
6375 (1+ (- d0 d1)) (1+ (- d2 d1)))
6376 head level category tags
6377 (cond ((and (= d1 d0) (= d2 d0))
6378 (concat "<" start-time ">--<" end-time ">"))
6379 ((= d1 d0)
6380 (concat "<" start-time ">"))
6381 ((= d2 d0)
6382 (concat "<" end-time ">")))
6383 remove-re))))
6384 (org-add-props txt props
6385 'org-marker marker 'org-hd-marker hdmarker
6386 'type "block" 'date date
6387 'level level
6388 'todo-state todo-state
6389 'priority (org-get-priority txt))
6390 (push txt ee))))
6391 (goto-char pos)))
6392 ;; Sort the entries by expiration date.
6393 (nreverse ee)))
6395 ;;; Agenda presentation and sorting
6397 (defvar org-prefix-has-time nil
6398 "A flag, set by `org-compile-prefix-format'.
6399 The flag is set if the currently compiled format contains a `%t'.")
6400 (defvar org-prefix-has-tag nil
6401 "A flag, set by `org-compile-prefix-format'.
6402 The flag is set if the currently compiled format contains a `%T'.")
6403 (defvar org-prefix-has-effort nil
6404 "A flag, set by `org-compile-prefix-format'.
6405 The flag is set if the currently compiled format contains a `%e'.")
6406 (defvar org-prefix-has-breadcrumbs nil
6407 "A flag, set by `org-compile-prefix-format'.
6408 The flag is set if the currently compiled format contains a `%b'.")
6409 (defvar org-prefix-category-length nil
6410 "Used by `org-compile-prefix-format' to remember the category field width.")
6411 (defvar org-prefix-category-max-length nil
6412 "Used by `org-compile-prefix-format' to remember the category field width.")
6414 (defun org-agenda-get-category-icon (category)
6415 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6416 (cl-dolist (entry org-agenda-category-icon-alist)
6417 (when (string-match-p (car entry) category)
6418 (if (listp (cadr entry))
6419 (cl-return (cadr entry))
6420 (cl-return (apply #'create-image (cdr entry)))))))
6422 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6423 remove-re habitp)
6424 "Format TXT to be inserted into the agenda buffer.
6425 In particular, add the prefix and corresponding text properties.
6427 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6428 LEVEL may be a string to replace the `%l' specifier.
6429 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6430 category taken from local variable or file name. It will replace the `%c'
6431 specifier in the format.
6432 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6433 TXT for sorting of this entry, and for the `%t' specifier in the format.
6434 When DOTIME is a string, this string is searched for a time before TXT is.
6435 TAGS can be the tags of the headline.
6436 Any match of REMOVE-RE will be removed from TXT."
6437 ;; We keep the org-prefix-* variable values along with a compiled
6438 ;; formatter, so that multiple agendas existing at the same time do
6439 ;; not step on each other toes.
6441 ;; It was inconvenient to make these variables buffer local in
6442 ;; Agenda buffers, because this function expects to be called with
6443 ;; the buffer where item comes from being current, and not agenda
6444 ;; buffer
6445 (let* ((bindings (car org-prefix-format-compiled))
6446 (formatter (cadr org-prefix-format-compiled)))
6447 (cl-loop for (var value) in bindings
6448 do (set var value))
6449 (save-match-data
6450 ;; Diary entries sometimes have extra whitespace at the beginning
6451 (setq txt (org-trim txt))
6453 ;; Fix the tags part in txt
6454 (setq txt (org-agenda-fix-displayed-tags
6455 txt tags
6456 org-agenda-show-inherited-tags
6457 org-agenda-hide-tags-regexp))
6459 (let* ((category (or category
6460 (if buffer-file-name
6461 (file-name-sans-extension
6462 (file-name-nondirectory buffer-file-name))
6463 "")))
6464 (category-icon (org-agenda-get-category-icon category))
6465 (category-icon (if category-icon
6466 (propertize " " 'display category-icon)
6467 ""))
6468 (effort (and (not (string= txt ""))
6469 (get-text-property 1 'effort txt)))
6470 ;; time, tag, effort are needed for the eval of the prefix format
6471 (tag (if tags (nth (1- (length tags)) tags) ""))
6472 (time-grid-trailing-characters (nth 2 org-agenda-time-grid))
6473 time
6474 (ts (if dotime (concat
6475 (if (stringp dotime) dotime "")
6476 (and org-agenda-search-headline-for-time txt))))
6477 (time-of-day (and dotime (org-get-time-of-day ts)))
6478 stamp plain s0 s1 s2 rtn srp l
6479 duration breadcrumbs)
6480 (and (derived-mode-p 'org-mode) buffer-file-name
6481 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6482 (when (and dotime time-of-day)
6483 ;; Extract starting and ending time and move them to prefix
6484 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6485 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6486 (setq s0 (match-string 0 ts)
6487 srp (and stamp (match-end 3))
6488 s1 (match-string (if plain 1 2) ts)
6489 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6491 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6492 ;; them, we might want to remove them there to avoid duplication.
6493 ;; The user can turn this off with a variable.
6494 (if (and org-prefix-has-time
6495 org-agenda-remove-times-when-in-prefix (or stamp plain)
6496 (string-match (concat (regexp-quote s0) " *") txt)
6497 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6498 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6499 (= (match-beginning 0) 0)
6501 (setq txt (replace-match "" nil nil txt))))
6502 ;; Normalize the time(s) to 24 hour
6503 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6504 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6506 ;; Try to set s2 if s1 and
6507 ;; `org-agenda-default-appointment-duration' are set
6508 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6509 (setq s2
6510 (org-duration-from-minutes
6511 (+ (org-duration-to-minutes s1 t)
6512 org-agenda-default-appointment-duration)
6513 nil t)))
6515 ;; Compute the duration
6516 (when s2
6517 (setq duration (- (org-duration-to-minutes s2)
6518 (org-duration-to-minutes s1)))))
6520 (when (string-match "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" txt)
6521 ;; Tags are in the string
6522 (if (or (eq org-agenda-remove-tags t)
6523 (and org-agenda-remove-tags
6524 org-prefix-has-tag))
6525 (setq txt (replace-match "" t t txt))
6526 (setq txt (replace-match
6527 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6528 (match-string 2 txt))
6529 t t txt))))
6531 (when remove-re
6532 (while (string-match remove-re txt)
6533 (setq txt (replace-match "" t t txt))))
6535 ;; Set org-heading property on `txt' to mark the start of the
6536 ;; heading.
6537 (add-text-properties 0 (length txt) '(org-heading t) txt)
6539 ;; Prepare the variables needed in the eval of the compiled format
6540 (if org-prefix-has-breadcrumbs
6541 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6542 (let ((s (org-display-outline-path nil nil "->" t)))
6543 (if (eq "" s) "" (concat s "->"))))))
6544 (setq time (cond (s2 (concat
6545 (org-agenda-time-of-day-to-ampm-maybe s1)
6546 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6547 (if org-agenda-timegrid-use-ampm " ")))
6548 (s1 (concat
6549 (org-agenda-time-of-day-to-ampm-maybe s1)
6550 (if org-agenda-timegrid-use-ampm
6551 (concat time-grid-trailing-characters " ")
6552 time-grid-trailing-characters)))
6553 (t ""))
6554 extra (or (and (not habitp) extra) "")
6555 category (if (symbolp category) (symbol-name category) category)
6556 level (or level ""))
6557 (if (string-match org-bracket-link-regexp category)
6558 (progn
6559 (setq l (if (match-end 3)
6560 (- (match-end 3) (match-beginning 3))
6561 (- (match-end 1) (match-beginning 1))))
6562 (when (< l (or org-prefix-category-length 0))
6563 (setq category (copy-sequence category))
6564 (org-add-props category nil
6565 'extra-space (make-string
6566 (- org-prefix-category-length l 1) ?\ ))))
6567 (if (and org-prefix-category-max-length
6568 (>= (length category) org-prefix-category-max-length))
6569 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6570 ;; Evaluate the compiled format
6571 (setq rtn (concat (eval formatter) txt))
6573 ;; And finally add the text properties
6574 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6575 (org-add-props rtn nil
6576 'org-category category
6577 'tags (mapcar 'org-downcase-keep-props tags)
6578 'org-highest-priority org-highest-priority
6579 'org-lowest-priority org-lowest-priority
6580 'time-of-day time-of-day
6581 'duration duration
6582 'breadcrumbs breadcrumbs
6583 'txt txt
6584 'level level
6585 'time time
6586 'extra extra
6587 'format org-prefix-format-compiled
6588 'dotime dotime)))))
6590 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6591 "Remove tags string from TXT, and add a modified list of tags.
6592 The modified list may contain inherited tags, and tags matched by
6593 `org-agenda-hide-tags-regexp' will be removed."
6594 (when (or add-inherited hide-re)
6595 (if (string-match "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" txt)
6596 (setq txt (substring txt 0 (match-beginning 0))))
6597 (setq tags
6598 (delq nil
6599 (mapcar (lambda (tg)
6600 (if (or (and hide-re (string-match hide-re tg))
6601 (and (not add-inherited)
6602 (get-text-property 0 'inherited tg)))
6604 tg))
6605 tags)))
6606 (when tags
6607 (let ((have-i (get-text-property 0 'inherited (car tags)))
6609 (setq txt (concat txt " :"
6610 (mapconcat
6611 (lambda (x)
6612 (setq i (get-text-property 0 'inherited x))
6613 (if (and have-i (not i))
6614 (progn
6615 (setq have-i nil)
6616 (concat ":" x))
6618 tags ":")
6619 (if have-i "::" ":"))))))
6620 txt)
6622 (defun org-downcase-keep-props (s)
6623 (let ((props (text-properties-at 0 s)))
6624 (setq s (downcase s))
6625 (add-text-properties 0 (length s) props s)
6628 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6630 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6631 "Add a time-grid for agenda items which need it.
6633 LIST is the list of agenda items formatted by `org-agenda-list'.
6634 NDAYS is the span of the current agenda view.
6635 TODAYP is t when the current agenda view is on today."
6636 (catch 'exit
6637 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6638 ((and todayp (member 'today (car org-agenda-time-grid))))
6639 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6640 ((member 'weekly (car org-agenda-time-grid)))
6641 (t (throw 'exit list)))
6642 (let* ((have (delq nil (mapcar
6643 (lambda (x) (get-text-property 1 'time-of-day x))
6644 list)))
6645 (string (nth 3 org-agenda-time-grid))
6646 (gridtimes (nth 1 org-agenda-time-grid))
6647 (req (car org-agenda-time-grid))
6648 (remove (member 'remove-match req))
6649 new time)
6650 (if (and (member 'require-timed req) (not have))
6651 ;; don't show empty grid
6652 (throw 'exit list))
6653 (while (setq time (pop gridtimes))
6654 (unless (and remove (member time have))
6655 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6656 (push (org-agenda-format-item
6657 nil string nil "" nil
6658 (concat (substring time 0 -2) ":" (substring time -2)))
6659 new)
6660 (put-text-property
6661 2 (length (car new)) 'face 'org-time-grid (car new))))
6662 (when (and todayp org-agenda-show-current-time-in-grid)
6663 (push (org-agenda-format-item
6664 nil org-agenda-current-time-string nil "" nil
6665 (format-time-string "%H:%M "))
6666 new)
6667 (put-text-property
6668 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6670 (if (member 'time-up org-agenda-sorting-strategy-selected)
6671 (append new list)
6672 (append list new)))))
6674 (defun org-compile-prefix-format (key)
6675 "Compile the prefix format into a Lisp form that can be evaluated.
6676 The resulting form and associated variable bindings is returned
6677 and stored in the variable `org-prefix-format-compiled'."
6678 (setq org-prefix-has-time nil
6679 org-prefix-has-tag nil
6680 org-prefix-category-length nil
6681 org-prefix-has-effort nil
6682 org-prefix-has-breadcrumbs nil)
6683 (let ((s (cond
6684 ((stringp org-agenda-prefix-format)
6685 org-agenda-prefix-format)
6686 ((assq key org-agenda-prefix-format)
6687 (cdr (assq key org-agenda-prefix-format)))
6688 (t " %-12:c%?-12t% s")))
6689 (start 0)
6690 varform vars var e c f opt)
6691 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6692 s start)
6693 (setq var (or (cdr (assoc (match-string 4 s)
6694 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6695 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6696 'eval)
6697 c (or (match-string 3 s) "")
6698 opt (match-beginning 1)
6699 start (1+ (match-beginning 0)))
6700 (if (eq var 'time) (setq org-prefix-has-time t))
6701 (if (eq var 'tag) (setq org-prefix-has-tag t))
6702 (if (eq var 'effort) (setq org-prefix-has-effort t))
6703 (if (eq var 'breadcrumbs) (setq org-prefix-has-breadcrumbs t))
6704 (setq f (concat "%" (match-string 2 s) "s"))
6705 (when (eq var 'category)
6706 (setq org-prefix-category-length
6707 (floor (abs (string-to-number (match-string 2 s)))))
6708 (setq org-prefix-category-max-length
6709 (let ((x (match-string 2 s)))
6710 (save-match-data
6711 (if (string-match "\\.[0-9]+" x)
6712 (string-to-number (substring (match-string 0 x) 1)))))))
6713 (if (eq var 'eval)
6714 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6715 (if opt
6716 (setq varform
6717 `(if (or (equal "" ,var) (equal nil ,var))
6719 (format ,f (concat ,var ,c))))
6720 (setq varform
6721 `(format ,f (if (or (equal ,var "")
6722 (equal ,var nil)) ""
6723 (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6724 (setq s (replace-match "%s" t nil s))
6725 (push varform vars))
6726 (setq vars (nreverse vars))
6727 (with-current-buffer (or org-agenda-buffer (current-buffer))
6728 (setq org-prefix-format-compiled
6729 (list
6730 `((org-prefix-has-time ,org-prefix-has-time)
6731 (org-prefix-has-tag ,org-prefix-has-tag)
6732 (org-prefix-category-length ,org-prefix-category-length)
6733 (org-prefix-has-effort ,org-prefix-has-effort)
6734 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6735 `(format ,s ,@vars))))))
6737 (defun org-set-sorting-strategy (key)
6738 (if (symbolp (car org-agenda-sorting-strategy))
6739 ;; the old format
6740 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6741 (setq org-agenda-sorting-strategy-selected
6742 (or (cdr (assq key org-agenda-sorting-strategy))
6743 (cdr (assq 'agenda org-agenda-sorting-strategy))
6744 '(time-up category-keep priority-down)))))
6746 (defun org-get-time-of-day (s &optional string mod24)
6747 "Check string S for a time of day.
6748 If found, return it as a military time number between 0 and 2400.
6749 If not found, return nil.
6750 The optional STRING argument forces conversion into a 5 character wide string
6751 HH:MM."
6752 (save-match-data
6753 (when
6754 (and
6755 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6756 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6757 (not (eq (get-text-property 1 'face s) 'org-link)))
6758 (let* ((h (string-to-number (match-string 1 s)))
6759 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6760 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6761 (am-p (equal ampm "am"))
6762 (h1 (cond ((not ampm) h)
6763 ((= h 12) (if am-p 0 12))
6764 (t (+ h (if am-p 0 12)))))
6765 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6766 (mod h1 24) h1))
6767 (t0 (+ (* 100 h2) m))
6768 (t1 (concat (if (>= h1 24) "+" " ")
6769 (if (and org-agenda-time-leading-zero
6770 (< t0 1000)) "0" "")
6771 (if (< t0 100) "0" "")
6772 (if (< t0 10) "0" "")
6773 (int-to-string t0))))
6774 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6776 (defvar org-agenda-before-sorting-filter-function nil
6777 "Function to be applied to agenda items prior to sorting.
6778 Prior to sorting also means just before they are inserted into the agenda.
6780 To aid sorting, you may revisit the original entries and add more text
6781 properties which will later be used by the sorting functions.
6783 The function should take a string argument, an agenda line.
6784 It has access to the text properties in that line, which contain among
6785 other things, the property `org-hd-marker' that points to the entry
6786 where the line comes from. Note that not all lines going into the agenda
6787 have this property, only most.
6789 The function should return the modified string. It is probably best
6790 to ONLY change text properties.
6792 You can also use this function as a filter, by returning nil for lines
6793 you don't want to have in the agenda at all. For this application, you
6794 could bind the variable in the options section of a custom command.")
6796 (defun org-agenda-finalize-entries (list &optional type)
6797 "Sort, limit and concatenate the LIST of agenda items.
6798 The optional argument TYPE tells the agenda type."
6799 (let ((max-effort (cond ((listp org-agenda-max-effort)
6800 (cdr (assoc type org-agenda-max-effort)))
6801 (t org-agenda-max-effort)))
6802 (max-todo (cond ((listp org-agenda-max-todos)
6803 (cdr (assoc type org-agenda-max-todos)))
6804 (t org-agenda-max-todos)))
6805 (max-tags (cond ((listp org-agenda-max-tags)
6806 (cdr (assoc type org-agenda-max-tags)))
6807 (t org-agenda-max-tags)))
6808 (max-entries (cond ((listp org-agenda-max-entries)
6809 (cdr (assoc type org-agenda-max-entries)))
6810 (t org-agenda-max-entries))))
6811 (when org-agenda-before-sorting-filter-function
6812 (setq list
6813 (delq nil
6814 (mapcar
6815 org-agenda-before-sorting-filter-function list))))
6816 (setq list (mapcar 'org-agenda-highlight-todo list)
6817 list (mapcar 'identity (sort list 'org-entries-lessp)))
6818 (when max-effort
6819 (setq list (org-agenda-limit-entries
6820 list 'effort-minutes max-effort
6821 (lambda (e) (or e (if org-sort-agenda-noeffort-is-high
6822 32767 -1))))))
6823 (when max-todo
6824 (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
6825 (when max-tags
6826 (setq list (org-agenda-limit-entries list 'tags max-tags)))
6827 (when max-entries
6828 (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
6829 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
6830 (setq list (mapcar #'org-agenda--mark-blocked-entry list)))
6831 (mapconcat 'identity list "\n")))
6833 (defun org-agenda-limit-entries (list prop limit &optional fn)
6834 "Limit the number of agenda entries."
6835 (let ((include (and limit (< limit 0))))
6836 (if limit
6837 (let ((fun (or fn (lambda (p) (if p 1))))
6838 (lim 0))
6839 (delq nil
6840 (mapcar
6841 (lambda (e)
6842 (let ((pval (funcall
6843 fun (get-text-property (1- (length e))
6844 prop e))))
6845 (if pval (setq lim (+ lim pval)))
6846 (cond ((and pval (<= lim (abs limit))) e)
6847 ((and include (not pval)) e))))
6848 list)))
6849 list)))
6851 (defun org-agenda-limit-interactively (remove)
6852 "In agenda, interactively limit entries to various maximums."
6853 (interactive "P")
6854 (if remove
6855 (progn (setq org-agenda-max-entries nil
6856 org-agenda-max-todos nil
6857 org-agenda-max-tags nil
6858 org-agenda-max-effort nil)
6859 (org-agenda-redo))
6860 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6861 (msg (cond ((= max ?E) "How many minutes? ")
6862 ((= max ?e) "How many entries? ")
6863 ((= max ?t) "How many TODO entries? ")
6864 ((= max ?T) "How many tagged entries? ")
6865 (t (user-error "Wrong input"))))
6866 (num (string-to-number (read-from-minibuffer msg))))
6867 (cond ((equal max ?e)
6868 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6869 ((equal max ?t)
6870 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6871 ((equal max ?T)
6872 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6873 ((equal max ?E)
6874 (let ((org-agenda-max-effort num)) (org-agenda-redo))))))
6875 (org-agenda-fit-window-to-buffer))
6877 (defun org-agenda-highlight-todo (x)
6878 (let ((org-done-keywords org-done-keywords-for-agenda)
6879 (case-fold-search nil)
6881 (if (eq x 'line)
6882 (save-excursion
6883 (beginning-of-line 1)
6884 (setq re (org-get-at-bol 'org-todo-regexp))
6885 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6886 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6887 (add-text-properties (match-beginning 0) (match-end 1)
6888 (list 'face (org-get-todo-face 1)))
6889 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6890 (delete-region (match-beginning 1) (1- (match-end 0)))
6891 (goto-char (match-beginning 1))
6892 (insert (format org-agenda-todo-keyword-format s)))))
6893 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6894 (setq re (get-text-property 0 'org-todo-regexp x))
6895 (when (and re
6896 ;; Test `pl' because if there's no heading content,
6897 ;; there's no point matching to highlight. Note
6898 ;; that if we didn't test `pl' first, and there
6899 ;; happened to be no keyword from `org-todo-regexp'
6900 ;; on this heading line, then the `equal' comparison
6901 ;; afterwards would spuriously succeed in the case
6902 ;; where `pl' is nil -- causing an args-out-of-range
6903 ;; error when we try to add text properties to text
6904 ;; that isn't there.
6906 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6907 x pl) pl))
6908 (add-text-properties
6909 (or (match-end 1) (match-end 0)) (match-end 0)
6910 (list 'face (org-get-todo-face (match-string 2 x)))
6912 (when (match-end 1)
6913 (setq x
6914 (concat
6915 (substring x 0 (match-end 1))
6916 (format org-agenda-todo-keyword-format
6917 (match-string 2 x))
6918 ;; Remove `display' property as the icon could leak
6919 ;; on the white space.
6920 (org-add-props " " (org-plist-delete (text-properties-at 0 x)
6921 'display))
6922 (substring x (match-end 3)))))))
6923 x)))
6925 (defsubst org-cmp-values (a b property)
6926 "Compare the numeric value of text PROPERTY for string A and B."
6927 (let ((pa (or (get-text-property (1- (length a)) property a) 0))
6928 (pb (or (get-text-property (1- (length b)) property b) 0)))
6929 (cond ((> pa pb) +1)
6930 ((< pa pb) -1))))
6932 (defsubst org-cmp-effort (a b)
6933 "Compare the effort values of string A and B."
6934 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6935 ;; `effort-minutes' property is not directly accessible from
6936 ;; the strings, but is stored as a property in `txt'.
6937 (ea (or (get-text-property
6938 0 'effort-minutes (get-text-property 0 'txt a))
6939 def))
6940 (eb (or (get-text-property
6941 0 'effort-minutes (get-text-property 0 'txt b))
6942 def)))
6943 (cond ((> ea eb) +1)
6944 ((< ea eb) -1))))
6946 (defsubst org-cmp-category (a b)
6947 "Compare the string values of categories of strings A and B."
6948 (let ((ca (or (get-text-property (1- (length a)) 'org-category a) ""))
6949 (cb (or (get-text-property (1- (length b)) 'org-category b) "")))
6950 (cond ((string-lessp ca cb) -1)
6951 ((string-lessp cb ca) +1))))
6953 (defsubst org-cmp-todo-state (a b)
6954 "Compare the todo states of strings A and B."
6955 (let* ((ma (or (get-text-property 1 'org-marker a)
6956 (get-text-property 1 'org-hd-marker a)))
6957 (mb (or (get-text-property 1 'org-marker b)
6958 (get-text-property 1 'org-hd-marker b)))
6959 (fa (and ma (marker-buffer ma)))
6960 (fb (and mb (marker-buffer mb)))
6961 (todo-kwds
6962 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6963 (and fb (with-current-buffer fb org-todo-keywords-1))))
6964 (ta (or (get-text-property 1 'todo-state a) ""))
6965 (tb (or (get-text-property 1 'todo-state b) ""))
6966 (la (- (length (member ta todo-kwds))))
6967 (lb (- (length (member tb todo-kwds))))
6968 (donepa (member ta org-done-keywords-for-agenda))
6969 (donepb (member tb org-done-keywords-for-agenda)))
6970 (cond ((and donepa (not donepb)) -1)
6971 ((and (not donepa) donepb) +1)
6972 ((< la lb) -1)
6973 ((< lb la) +1))))
6975 (defsubst org-cmp-alpha (a b)
6976 "Compare the headlines, alphabetically."
6977 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6978 (plb (text-property-any 0 (length b) 'org-heading t b))
6979 (ta (and pla (substring a pla)))
6980 (tb (and plb (substring b plb)))
6981 (case-fold-search nil))
6982 (when pla
6983 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6984 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6985 (setq ta (substring ta (match-end 0))))
6986 (setq ta (downcase ta)))
6987 (when plb
6988 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6989 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6990 (setq tb (substring tb (match-end 0))))
6991 (setq tb (downcase tb)))
6992 (cond ((not ta) +1)
6993 ((not tb) -1)
6994 ((string-lessp ta tb) -1)
6995 ((string-lessp tb ta) +1))))
6997 (defsubst org-cmp-tag (a b)
6998 "Compare the string values of the first tags of A and B."
6999 (let ((ta (car (last (get-text-property 1 'tags a))))
7000 (tb (car (last (get-text-property 1 'tags b)))))
7001 (cond ((not ta) +1)
7002 ((not tb) -1)
7003 ((string-lessp ta tb) -1)
7004 ((string-lessp tb ta) +1))))
7006 (defsubst org-cmp-time (a b)
7007 "Compare the time-of-day values of strings A and B."
7008 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
7009 (ta (or (get-text-property 1 'time-of-day a) def))
7010 (tb (or (get-text-property 1 'time-of-day b) def)))
7011 (cond ((< ta tb) -1)
7012 ((< tb ta) +1))))
7014 (defsubst org-cmp-ts (a b type)
7015 "Compare the timestamps values of entries A and B.
7016 When TYPE is \"scheduled\", \"deadline\", \"timestamp\" or
7017 \"timestamp_ia\", compare within each of these type. When TYPE
7018 is the empty string, compare all timestamps without respect of
7019 their type."
7020 (let* ((def (if org-sort-agenda-notime-is-late most-positive-fixnum -1))
7021 (ta (or (and (string-match type (or (get-text-property 1 'type a) ""))
7022 (get-text-property 1 'ts-date a))
7023 def))
7024 (tb (or (and (string-match type (or (get-text-property 1 'type b) ""))
7025 (get-text-property 1 'ts-date b))
7026 def)))
7027 (cond ((< ta tb) -1)
7028 ((< tb ta) +1))))
7030 (defsubst org-cmp-habit-p (a b)
7031 "Compare the todo states of strings A and B."
7032 (let ((ha (get-text-property 1 'org-habit-p a))
7033 (hb (get-text-property 1 'org-habit-p b)))
7034 (cond ((and ha (not hb)) -1)
7035 ((and (not ha) hb) +1))))
7037 (defun org-entries-lessp (a b)
7038 "Predicate for sorting agenda entries."
7039 ;; The following variables will be used when the form is evaluated.
7040 ;; So even though the compiler complains, keep them.
7041 (let* ((ss org-agenda-sorting-strategy-selected)
7042 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
7043 (org-cmp-ts a b "")))
7044 (timestamp-down (if timestamp-up (- timestamp-up) nil))
7045 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
7046 (org-cmp-ts a b "scheduled")))
7047 (scheduled-down (if scheduled-up (- scheduled-up) nil))
7048 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
7049 (org-cmp-ts a b "deadline")))
7050 (deadline-down (if deadline-up (- deadline-up) nil))
7051 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
7052 (org-cmp-ts a b "timestamp_ia")))
7053 (tsia-down (if tsia-up (- tsia-up) nil))
7054 (ts-up (and (org-em 'ts-up 'ts-down ss)
7055 (org-cmp-ts a b "timestamp")))
7056 (ts-down (if ts-up (- ts-up) nil))
7057 (time-up (and (org-em 'time-up 'time-down ss)
7058 (org-cmp-time a b)))
7059 (time-down (if time-up (- time-up) nil))
7060 (stats-up (and (org-em 'stats-up 'stats-down ss)
7061 (org-cmp-values a b 'org-stats)))
7062 (stats-down (if stats-up (- stats-up) nil))
7063 (priority-up (and (org-em 'priority-up 'priority-down ss)
7064 (org-cmp-values a b 'priority)))
7065 (priority-down (if priority-up (- priority-up) nil))
7066 (effort-up (and (org-em 'effort-up 'effort-down ss)
7067 (org-cmp-effort a b)))
7068 (effort-down (if effort-up (- effort-up) nil))
7069 (category-up (and (or (org-em 'category-up 'category-down ss)
7070 (memq 'category-keep ss))
7071 (org-cmp-category a b)))
7072 (category-down (if category-up (- category-up) nil))
7073 (category-keep (if category-up +1 nil))
7074 (tag-up (and (org-em 'tag-up 'tag-down ss)
7075 (org-cmp-tag a b)))
7076 (tag-down (if tag-up (- tag-up) nil))
7077 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
7078 (org-cmp-todo-state a b)))
7079 (todo-state-down (if todo-state-up (- todo-state-up) nil))
7080 (habit-up (and (org-em 'habit-up 'habit-down ss)
7081 (org-cmp-habit-p a b)))
7082 (habit-down (if habit-up (- habit-up) nil))
7083 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
7084 (org-cmp-alpha a b)))
7085 (alpha-down (if alpha-up (- alpha-up) nil))
7086 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
7087 user-defined-up user-defined-down)
7088 (if (and need-user-cmp org-agenda-cmp-user-defined
7089 (functionp org-agenda-cmp-user-defined))
7090 (setq user-defined-up
7091 (funcall org-agenda-cmp-user-defined a b)
7092 user-defined-down (if user-defined-up (- user-defined-up) nil)))
7093 (cdr (assoc
7094 (eval (cons 'or org-agenda-sorting-strategy-selected))
7095 '((-1 . t) (1 . nil) (nil . nil))))))
7097 ;;; Agenda restriction lock
7099 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
7100 "Overlay to mark the headline to which agenda commands are restricted.")
7101 (overlay-put org-agenda-restriction-lock-overlay
7102 'face 'org-agenda-restriction-lock)
7103 (overlay-put org-agenda-restriction-lock-overlay
7104 'help-echo "Agendas are currently limited to this subtree.")
7105 (delete-overlay org-agenda-restriction-lock-overlay)
7107 (defun org-agenda-set-restriction-lock-from-agenda (arg)
7108 "Set the restriction lock to the agenda item at point from within the agenda.
7109 When called with a `\\[universal-argument]' prefix, restrict to
7110 the file which contains the item.
7111 Argument ARG is the prefix argument."
7112 (interactive "P")
7113 (unless (derived-mode-p 'org-agenda-mode)
7114 (user-error "Not in an Org agenda buffer"))
7115 (let* ((marker (or (org-get-at-bol 'org-marker)
7116 (org-agenda-error)))
7117 (buffer (marker-buffer marker))
7118 (pos (marker-position marker)))
7119 (with-current-buffer buffer
7120 (goto-char pos)
7121 (org-agenda-set-restriction-lock arg))))
7123 ;;;###autoload
7124 (defun org-agenda-set-restriction-lock (&optional type)
7125 "Set restriction lock for agenda, to current subtree or file.
7126 Restriction will be the file if TYPE is `file', or if type is the
7127 universal prefix \\='(4), or if the cursor is before the first headline
7128 in the file. Otherwise, restriction will be to the current subtree."
7129 (interactive "P")
7130 (org-agenda-remove-restriction-lock 'noupdate)
7131 (and (equal type '(4)) (setq type 'file))
7132 (setq type (cond
7133 (type type)
7134 ((org-at-heading-p) 'subtree)
7135 ((condition-case nil (org-back-to-heading t) (error nil))
7136 'subtree)
7137 (t 'file)))
7138 (if (eq type 'subtree)
7139 (progn
7140 (setq org-agenda-restrict (current-buffer))
7141 (setq org-agenda-overriding-restriction 'subtree)
7142 (put 'org-agenda-files 'org-restrict
7143 (list (buffer-file-name (buffer-base-buffer))))
7144 (org-back-to-heading t)
7145 (move-overlay org-agenda-restriction-lock-overlay
7146 (point)
7147 (if org-agenda-restriction-lock-highlight-subtree
7148 (save-excursion (org-end-of-subtree t t) (point))
7149 (point-at-eol)))
7150 (move-marker org-agenda-restrict-begin (point))
7151 (move-marker org-agenda-restrict-end
7152 (save-excursion (org-end-of-subtree t t)))
7153 (message "Locking agenda restriction to subtree"))
7154 (put 'org-agenda-files 'org-restrict
7155 (list (buffer-file-name (buffer-base-buffer))))
7156 (setq org-agenda-restrict nil)
7157 (setq org-agenda-overriding-restriction 'file)
7158 (move-marker org-agenda-restrict-begin nil)
7159 (move-marker org-agenda-restrict-end nil)
7160 (message "Locking agenda restriction to file"))
7161 (setq current-prefix-arg nil)
7162 (org-agenda-maybe-redo))
7164 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7165 "Remove the agenda restriction lock."
7166 (interactive "P")
7167 (delete-overlay org-agenda-restriction-lock-overlay)
7168 (delete-overlay org-speedbar-restriction-lock-overlay)
7169 (setq org-agenda-overriding-restriction nil)
7170 (setq org-agenda-restrict nil)
7171 (put 'org-agenda-files 'org-restrict nil)
7172 (move-marker org-agenda-restrict-begin nil)
7173 (move-marker org-agenda-restrict-end nil)
7174 (setq current-prefix-arg nil)
7175 (message "Agenda restriction lock removed")
7176 (or noupdate (org-agenda-maybe-redo)))
7178 (defun org-agenda-maybe-redo ()
7179 "If there is any window showing the agenda view, update it."
7180 (let ((w (get-buffer-window (or org-agenda-this-buffer-name
7181 org-agenda-buffer-name)
7183 (w0 (selected-window)))
7184 (when w
7185 (select-window w)
7186 (org-agenda-redo)
7187 (select-window w0)
7188 (if org-agenda-overriding-restriction
7189 (message "Agenda view shifted to new %s restriction"
7190 org-agenda-overriding-restriction)
7191 (message "Agenda restriction lock removed")))))
7193 ;;; Agenda commands
7195 (defun org-agenda-check-type (error &rest types)
7196 "Check if agenda buffer is of allowed type.
7197 If ERROR is non-nil, throw an error, otherwise just return nil.
7198 Allowed types are `agenda' `todo' `tags' `search'."
7199 (cond ((not org-agenda-type)
7200 (error "No Org agenda currently displayed"))
7201 ((memq org-agenda-type types) t)
7202 (error
7203 (error "Not allowed in %s-type agenda buffers" org-agenda-type))
7204 (t nil)))
7206 (defun org-agenda-Quit ()
7207 "Exit the agenda, killing the agenda buffer.
7208 Like `org-agenda-quit', but kill the buffer even when
7209 `org-agenda-sticky' is non-nil."
7210 (interactive)
7211 (org-agenda--quit))
7213 (defun org-agenda-quit ()
7214 "Exit the agenda.
7216 When `org-agenda-sticky' is non-nil, bury the agenda buffer
7217 instead of killing it.
7219 When `org-agenda-restore-windows-after-quit' is non-nil, restore
7220 the pre-agenda window configuration.
7222 When column view is active, exit column view instead of the
7223 agenda."
7224 (interactive)
7225 (org-agenda--quit org-agenda-sticky))
7227 (defun org-agenda--quit (&optional bury)
7228 (if org-agenda-columns-active
7229 (org-columns-quit)
7230 (let ((wconf org-agenda-pre-window-conf)
7231 (buf (current-buffer))
7232 (org-agenda-last-indirect-window
7233 (and (eq org-indirect-buffer-display 'other-window)
7234 org-agenda-last-indirect-buffer
7235 (get-buffer-window org-agenda-last-indirect-buffer))))
7236 (cond
7237 ((eq org-agenda-window-setup 'other-frame)
7238 (delete-frame))
7239 ((and org-agenda-restore-windows-after-quit
7240 wconf)
7241 ;; Maybe restore the pre-agenda window configuration. Reset
7242 ;; `org-agenda-pre-window-conf' before running
7243 ;; `set-window-configuration', which loses the current buffer.
7244 (setq org-agenda-pre-window-conf nil)
7245 (set-window-configuration wconf))
7247 (when org-agenda-last-indirect-window
7248 (delete-window org-agenda-last-indirect-window))
7249 (and (not (eq org-agenda-window-setup 'current-window))
7250 (not (one-window-p))
7251 (delete-window))))
7252 (if bury
7253 ;; Set the agenda buffer as the current buffer instead of
7254 ;; passing it as an argument to `bury-buffer' so that
7255 ;; `bury-buffer' removes it from the window.
7256 (with-current-buffer buf
7257 (bury-buffer))
7258 (kill-buffer buf)
7259 (setq org-agenda-archives-mode nil
7260 org-agenda-buffer nil)))))
7262 (defun org-agenda-exit ()
7263 "Exit the agenda, killing Org buffers loaded by the agenda.
7264 Like `org-agenda-Quit', but kill any buffers that were created by
7265 the agenda. Org buffers visited directly by the user will not be
7266 touched. Also, exit the agenda even if it is in column view."
7267 (interactive)
7268 (when org-agenda-columns-active
7269 (org-columns-quit))
7270 (org-release-buffers org-agenda-new-buffers)
7271 (setq org-agenda-new-buffers nil)
7272 (org-agenda-Quit))
7274 (defun org-agenda-kill-all-agenda-buffers ()
7275 "Kill all buffers in `org-agenda-mode'.
7276 This is used when toggling sticky agendas."
7277 (interactive)
7278 (let (blist)
7279 (dolist (buf (buffer-list))
7280 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7281 (push buf blist)))
7282 (mapc 'kill-buffer blist)))
7284 (defun org-agenda-execute (arg)
7285 "Execute another agenda command, keeping same window.
7286 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7287 in the agenda."
7288 (interactive "P")
7289 (let ((org-agenda-window-setup 'current-window))
7290 (org-agenda arg)))
7292 (defun org-agenda-redo (&optional all)
7293 "Rebuild possibly ALL agenda view(s) in the current buffer."
7294 (interactive "P")
7295 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7296 (cpa (unless (eq all t) current-prefix-arg))
7297 (org-agenda-doing-sticky-redo org-agenda-sticky)
7298 (org-agenda-sticky nil)
7299 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7300 org-agenda-buffer-name))
7301 (org-agenda-keep-modes t)
7302 (tag-filter org-agenda-tag-filter)
7303 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7304 (top-hl-filter org-agenda-top-headline-filter)
7305 (cat-filter org-agenda-category-filter)
7306 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7307 (re-filter org-agenda-regexp-filter)
7308 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7309 (effort-filter org-agenda-effort-filter)
7310 (effort-preset (get 'org-agenda-effort-filter :preset-filter))
7311 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7312 (cols org-agenda-columns-active)
7313 (line (org-current-line))
7314 (window-line (- line (org-current-line (window-start))))
7315 (lprops (get 'org-agenda-redo-command 'org-lprops))
7316 (redo-cmd (get-text-property p 'org-redo-cmd))
7317 (last-args (get-text-property p 'org-last-args))
7318 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7319 (org-agenda-overriding-cmd-arguments
7320 (unless (eq all t)
7321 (cond ((listp last-args)
7322 (cons (or cpa (car last-args)) (cdr last-args)))
7323 ((stringp last-args)
7324 last-args))))
7325 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7326 (put 'org-agenda-tag-filter :preset-filter nil)
7327 (put 'org-agenda-category-filter :preset-filter nil)
7328 (put 'org-agenda-regexp-filter :preset-filter nil)
7329 (put 'org-agenda-effort-filter :preset-filter nil)
7330 (and cols (org-columns-quit))
7331 (message "Rebuilding agenda buffer...")
7332 (if series-redo-cmd
7333 (eval series-redo-cmd)
7334 (org-let lprops redo-cmd))
7335 (setq org-agenda-undo-list nil
7336 org-agenda-pending-undo-list nil
7337 org-agenda-tag-filter tag-filter
7338 org-agenda-category-filter cat-filter
7339 org-agenda-regexp-filter re-filter
7340 org-agenda-effort-filter effort-filter
7341 org-agenda-top-headline-filter top-hl-filter)
7342 (message "Rebuilding agenda buffer...done")
7343 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7344 (put 'org-agenda-category-filter :preset-filter cat-preset)
7345 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7346 (put 'org-agenda-effort-filter :preset-filter effort-preset)
7347 (let ((tag (or tag-filter tag-preset))
7348 (cat (or cat-filter cat-preset))
7349 (effort (or effort-filter effort-preset))
7350 (re (or re-filter re-preset)))
7351 (when tag (org-agenda-filter-apply tag 'tag t))
7352 (when cat (org-agenda-filter-apply cat 'category))
7353 (when effort (org-agenda-filter-apply effort 'effort))
7354 (when re (org-agenda-filter-apply re 'regexp)))
7355 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7356 (and cols (called-interactively-p 'any) (org-agenda-columns))
7357 (org-goto-line line)
7358 (recenter window-line)))
7360 (defun org-agenda-redo-all (&optional exhaustive)
7361 "Rebuild all agenda views in the current buffer.
7362 With a prefix argument, do so in all agenda buffers."
7363 (interactive "P")
7364 (if exhaustive
7365 (dolist (buffer (buffer-list))
7366 (with-current-buffer buffer
7367 (when (derived-mode-p 'org-agenda-mode)
7368 (org-agenda-redo t))))
7369 (org-agenda-redo t)))
7371 (defvar org-global-tags-completion-table nil)
7372 (defvar org-agenda-filter-form nil)
7373 (defvar org-agenda-filtered-by-category nil)
7375 (defun org-agenda-filter-by-category (strip)
7376 "Filter lines in the agenda buffer that have a specific category.
7377 The category is that of the current line.
7378 Without prefix argument, keep only the lines of that category.
7379 With a prefix argument, exclude the lines of that category.
7381 (interactive "P")
7382 (if (and org-agenda-filtered-by-category
7383 org-agenda-category-filter)
7384 (org-agenda-filter-show-all-cat)
7385 (let ((cat (org-no-properties (org-get-at-eol 'org-category 1))))
7386 (cond
7387 ((and cat strip)
7388 (org-agenda-filter-apply
7389 (push (concat "-" cat) org-agenda-category-filter) 'category))
7390 (cat
7391 (org-agenda-filter-apply
7392 (setq org-agenda-category-filter
7393 (list (concat "+" cat))) 'category))
7394 (t (error "No category at point"))))))
7396 (defun org-find-top-headline (&optional pos)
7397 "Find the topmost parent headline and return it.
7398 POS when non-nil is the marker or buffer position to start the
7399 search from."
7400 (save-excursion
7401 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
7402 (when pos (goto-char pos))
7403 ;; Skip up to the topmost parent.
7404 (while (org-up-heading-safe))
7405 (ignore-errors (nth 4 (org-heading-components))))))
7407 (defvar org-agenda-filtered-by-top-headline nil)
7408 (defun org-agenda-filter-by-top-headline (strip)
7409 "Keep only those lines that are descendants from the same top headline.
7410 The top headline is that of the current line."
7411 (interactive "P")
7412 (if org-agenda-filtered-by-top-headline
7413 (progn
7414 (setq org-agenda-filtered-by-top-headline nil
7415 org-agenda-top-headline-filter nil)
7416 (org-agenda-filter-show-all-top-filter))
7417 (let ((toph (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7418 (if toph (org-agenda-filter-top-headline-apply toph strip)
7419 (error "No top-level headline at point")))))
7421 (defvar org-agenda-regexp-filter nil)
7422 (defun org-agenda-filter-by-regexp (strip)
7423 "Filter agenda entries by a regular expression.
7424 Regexp filters are cumulative.
7425 With no prefix argument, keep entries matching the regexp.
7426 With one prefix argument, filter out entries matching the regexp.
7427 With two prefix arguments, remove the regexp filters."
7428 (interactive "P")
7429 (if (not (equal strip '(16)))
7430 (let ((flt (concat (if (equal strip '(4)) "-" "+")
7431 (read-from-minibuffer
7432 (if (equal strip '(4))
7433 "Filter out entries matching regexp: "
7434 "Narrow to entries matching regexp: ")))))
7435 (push flt org-agenda-regexp-filter)
7436 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
7437 (org-agenda-filter-show-all-re)
7438 (message "Regexp filter removed")))
7440 (defvar org-agenda-effort-filter nil)
7441 (defun org-agenda-filter-by-effort (strip)
7442 "Filter agenda entries by effort.
7443 With no prefix argument, keep entries matching the effort condition.
7444 With one prefix argument, filter out entries matching the condition.
7445 With two prefix arguments, remove the effort filters."
7446 (interactive "P")
7447 (cond
7448 ((member strip '(nil 4))
7449 (let* ((efforts (split-string
7450 (or (cdr (assoc (concat org-effort-property "_ALL")
7451 org-global-properties))
7452 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00")))
7453 ;; XXX: the following handles only up to 10 different
7454 ;; effort values.
7455 (allowed-keys (if (null efforts) nil
7456 (mapcar (lambda (n) (mod n 10)) ;turn 10 into 0
7457 (number-sequence 1 (length efforts)))))
7458 (op nil))
7459 (while (not (memq op '(?< ?> ?=)))
7460 (setq op (read-char-exclusive "Effort operator? (> = or <)")))
7461 ;; Select appropriate duration. Ignore non-digit characters.
7462 (let ((prompt
7463 (apply #'format
7464 (concat "Effort %c "
7465 (mapconcat (lambda (s) (concat "[%d]" s))
7466 efforts
7467 " "))
7468 op allowed-keys))
7469 (eff -1))
7470 (while (not (memq eff allowed-keys))
7471 (message prompt)
7472 (setq eff (- (read-char-exclusive) 48)))
7473 (setq org-agenda-effort-filter
7474 (list (concat (if strip "-" "+")
7475 (char-to-string op)
7476 ;; Numbering is 1 2 3 ... 9 0, but we want
7477 ;; 0 1 2 ... 8 9.
7478 (nth (mod (1- eff) 10) efforts)))))
7479 (org-agenda-filter-apply org-agenda-effort-filter 'effort)))
7480 (t (org-agenda-filter-show-all-effort)
7481 (message "Effort filter removed"))))
7483 (defun org-agenda-filter-remove-all ()
7484 "Remove all filters from the current agenda buffer."
7485 (interactive)
7486 (when org-agenda-tag-filter
7487 (org-agenda-filter-show-all-tag))
7488 (when org-agenda-category-filter
7489 (org-agenda-filter-show-all-cat))
7490 (when org-agenda-regexp-filter
7491 (org-agenda-filter-show-all-re))
7492 (when org-agenda-top-headline-filter
7493 (org-agenda-filter-show-all-top-filter))
7494 (when org-agenda-effort-filter
7495 (org-agenda-filter-show-all-effort))
7496 (org-agenda-finalize))
7498 (defun org-agenda-filter-by-tag (arg &optional char exclude)
7499 "Keep only those lines in the agenda buffer that have a specific tag.
7501 The tag is selected with its fast selection letter, as configured.
7503 With a `\\[universal-argument]' prefix, exclude the agenda search.
7505 With a `\\[universal-argument] \\[universal-argument]' prefix, filter the literal tag, \
7506 i.e. don't
7507 filter on all its group members.
7509 A lisp caller can specify CHAR. EXCLUDE means that the new tag
7510 should be used to exclude the search - the interactive user can
7511 also press `-' or `+' to switch between filtering and excluding."
7512 (interactive "P")
7513 (let* ((alist org-tag-alist-for-agenda)
7514 (tag-chars (mapconcat
7515 (lambda (x) (if (and (not (symbolp (car x)))
7516 (cdr x))
7517 (char-to-string (cdr x))
7518 ""))
7519 org-tag-alist-for-agenda ""))
7520 (valid-char-list (append '(?\t ?\r ?/ ?. ?\s ?q)
7521 (string-to-list tag-chars)))
7522 (exclude (or exclude (equal arg '(4))))
7523 (expand (not (equal arg '(16))))
7524 (inhibit-read-only t)
7525 (current org-agenda-tag-filter)
7526 a n tag)
7527 (unless char
7528 (while (not (memq char valid-char-list))
7529 (message
7530 "%s by tag [%s ]:tag-char, [TAB]:tag, %s[/]:off, [+/-]:filter/exclude%s, [q]:quit"
7531 (if exclude "Exclude" "Filter")
7532 tag-chars
7533 (if org-agenda-auto-exclude-function "[RET], " "")
7534 (if expand "" ", no grouptag expand"))
7535 (setq char (read-char-exclusive))
7536 ;; Excluding or filtering down
7537 (cond ((eq char ?-) (setq exclude t))
7538 ((eq char ?+) (setq exclude nil)))))
7539 (when (eq char ?\t)
7540 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7541 (setq-local org-global-tags-completion-table
7542 (org-global-tags-completion-table)))
7543 (let ((completion-ignore-case t))
7544 (setq tag (completing-read
7545 "Tag: " org-global-tags-completion-table nil t))))
7546 (cond
7547 ((eq char ?\r)
7548 (org-agenda-filter-show-all-tag)
7549 (when org-agenda-auto-exclude-function
7550 (setq org-agenda-tag-filter nil)
7551 (dolist (tag (org-agenda-get-represented-tags))
7552 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7553 (if modifier
7554 (push modifier org-agenda-tag-filter))))
7555 (if (not (null org-agenda-tag-filter))
7556 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))))
7557 ((eq char ?/)
7558 (org-agenda-filter-show-all-tag)
7559 (when (get 'org-agenda-tag-filter :preset-filter)
7560 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand)))
7561 ((eq char ?.)
7562 (setq org-agenda-tag-filter
7563 (mapcar (lambda(tag) (concat "+" tag))
7564 (org-get-at-bol 'tags)))
7565 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7566 ((eq char ?q)) ;If q, abort (even if there is a q-key for a tag...)
7567 ((or (eq char ?\s)
7568 (setq a (rassoc char alist))
7569 (and tag (setq a (cons tag nil))))
7570 (org-agenda-filter-show-all-tag)
7571 (setq tag (car a))
7572 (setq org-agenda-tag-filter
7573 (cons (concat (if exclude "-" "+") tag)
7574 current))
7575 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7576 (t (error "Invalid tag selection character %c" char)))))
7578 (defun org-agenda-get-represented-tags ()
7579 "Get a list of all tags currently represented in the agenda."
7580 (let (p tags)
7581 (save-excursion
7582 (goto-char (point-min))
7583 (while (setq p (next-single-property-change (point) 'tags))
7584 (goto-char p)
7585 (mapc (lambda (x) (add-to-list 'tags x))
7586 (get-text-property (point) 'tags))))
7587 tags))
7590 (defun org-agenda-filter-make-matcher (filter type &optional expand)
7591 "Create the form that tests a line for agenda filter. Optional
7592 argument EXPAND can be used for the TYPE tag and will expand the
7593 tags in the FILTER if any of the tags in FILTER are grouptags."
7594 (let (f f1)
7595 (cond
7596 ;; Tag filter
7597 ((eq type 'tag)
7598 (setq filter
7599 (delete-dups
7600 (append (get 'org-agenda-tag-filter :preset-filter)
7601 filter)))
7602 (dolist (x filter)
7603 (let ((op (string-to-char x)))
7604 (if expand (setq x (org-agenda-filter-expand-tags (list x) t))
7605 (setq x (list x)))
7606 (setq f1 (org-agenda-filter-make-matcher-tag-exp x op))
7607 (push f1 f))))
7608 ;; Category filter
7609 ((eq type 'category)
7610 (setq filter
7611 (delete-dups
7612 (append (get 'org-agenda-category-filter :preset-filter)
7613 filter)))
7614 (dolist (x filter)
7615 (if (equal "-" (substring x 0 1))
7616 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7617 (setq f1 (list 'equal (substring x 1) 'cat)))
7618 (push f1 f)))
7619 ;; Regexp filter
7620 ((eq type 'regexp)
7621 (setq filter
7622 (delete-dups
7623 (append (get 'org-agenda-regexp-filter :preset-filter)
7624 filter)))
7625 (dolist (x filter)
7626 (if (equal "-" (substring x 0 1))
7627 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7628 (setq f1 (list 'string-match (substring x 1) 'txt)))
7629 (push f1 f)))
7630 ;; Effort filter
7631 ((eq type 'effort)
7632 (setq filter
7633 (delete-dups
7634 (append (get 'org-agenda-effort-filter :preset-filter)
7635 filter)))
7636 (dolist (x filter)
7637 (push (org-agenda-filter-effort-form x) f))))
7638 (cons 'and (nreverse f))))
7640 (defun org-agenda-filter-make-matcher-tag-exp (tags op)
7641 "Return a form associated to tag-expression TAGS.
7642 Build a form testing a line for agenda filter for
7643 tag-expressions. OP is an operator of type CHAR that allows the
7644 function to set the right switches in the returned form."
7645 (let (form)
7646 ;; Any of the expressions can match if OP is +, all must match if
7647 ;; the operator is -.
7648 (dolist (x tags (cons (if (eq op ?-) 'and 'or) form))
7649 (let* ((tag (substring x 1))
7650 (f (cond
7651 ((string= "" tag) '(not tags))
7652 ((and (string-match-p "\\`{" tag) (string-match-p "}\\'" tag))
7653 ;; TAG is a regexp.
7654 (list 'org-match-any-p (substring tag 1 -1) 'tags))
7655 (t (list 'member (downcase tag) 'tags)))))
7656 (push (if (eq op ?-) (list 'not f) f) form)))))
7658 (defun org-agenda-filter-effort-form (e)
7659 "Return the form to compare the effort of the current line with what E says.
7660 E looks like \"+<2:25\"."
7661 (let (op)
7662 (setq e (substring e 1))
7663 (setq op (string-to-char e) e (substring e 1))
7664 (setq op (cond ((equal op ?<) '<=)
7665 ((equal op ?>) '>=)
7666 ((equal op ??) op)
7667 (t '=)))
7668 (list 'org-agenda-compare-effort (list 'quote op)
7669 (org-duration-to-minutes e))))
7671 (defun org-agenda-compare-effort (op value)
7672 "Compare the effort of the current line with VALUE, using OP.
7673 If the line does not have an effort defined, return nil."
7674 ;; `effort-minutes' property cannot be extracted directly from
7675 ;; current line but is stored as a property in `txt'.
7676 (let ((effort (get-text-property 0 'effort-minutes (org-get-at-bol 'txt))))
7677 (funcall op
7678 (or effort (if org-sort-agenda-noeffort-is-high 32767 -1))
7679 value)))
7681 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7682 "Expand group tags in FILTER for the agenda.
7683 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7684 (if org-group-tags
7685 (let ((case-fold-search t) rtn)
7686 (mapc
7687 (lambda (f)
7688 (let (f0 dir)
7689 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7690 (setq dir (match-string 1 f) f0 (match-string 2 f))
7691 (setq dir (if no-operator "" "+") f0 f))
7692 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7693 (org-tags-expand f0 t t))
7694 rtn))))
7695 filter)
7696 (reverse rtn))
7697 filter))
7699 (defun org-agenda-filter-apply (filter type &optional expand)
7700 "Set FILTER as the new agenda filter and apply it. Optional
7701 argument EXPAND can be used for the TYPE tag and will expand the
7702 tags in the FILTER if any of the tags in FILTER are grouptags."
7703 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7704 (if org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7705 (let (tags cat txt)
7706 (setq org-agenda-filter-form (org-agenda-filter-make-matcher filter type expand))
7707 ;; Only set `org-agenda-filtered-by-category' to t when a unique
7708 ;; category is used as the filter:
7709 (setq org-agenda-filtered-by-category
7710 (and (eq type 'category)
7711 (not (equal (substring (car filter) 0 1) "-"))))
7712 (org-agenda-set-mode-name)
7713 (save-excursion
7714 (goto-char (point-min))
7715 (while (not (eobp))
7716 (if (org-get-at-bol 'org-marker)
7717 (progn
7718 (setq tags (org-get-at-bol 'tags)
7719 cat (org-get-at-eol 'org-category 1)
7720 txt (org-get-at-bol 'txt))
7721 (if (not (eval org-agenda-filter-form))
7722 (org-agenda-filter-hide-line type))
7723 (beginning-of-line 2))
7724 (beginning-of-line 2))))
7725 (if (get-char-property (point) 'invisible)
7726 (ignore-errors (org-agenda-previous-line)))))
7728 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7729 "Filter by top headline HL."
7730 (org-agenda-set-mode-name)
7731 (save-excursion
7732 (goto-char (point-min))
7733 (while (not (eobp))
7734 (let* ((pos (org-get-at-bol 'org-hd-marker))
7735 (tophl (and pos (org-find-top-headline pos))))
7736 (if (and tophl (funcall (if negative 'identity 'not)
7737 (string= hl tophl)))
7738 (org-agenda-filter-hide-line 'top-headline)))
7739 (beginning-of-line 2)))
7740 (if (get-char-property (point) 'invisible)
7741 (org-agenda-previous-line))
7742 (setq org-agenda-top-headline-filter hl
7743 org-agenda-filtered-by-top-headline t))
7745 (defun org-agenda-filter-hide-line (type)
7746 "Hide lines with TYPE in the agenda buffer."
7747 (let* ((b (max (point-min) (1- (point-at-bol))))
7748 (e (point-at-eol)))
7749 (let ((inhibit-read-only t))
7750 (add-text-properties
7751 b e `(invisible org-filtered org-filter-type ,type)))))
7753 (defun org-agenda-remove-filter (type)
7754 (interactive)
7755 "Remove filter of type TYPE from the agenda buffer."
7756 (save-excursion
7757 (goto-char (point-min))
7758 (let ((inhibit-read-only t) pos)
7759 (while (setq pos (text-property-any (point) (point-max) 'org-filter-type type))
7760 (goto-char pos)
7761 (remove-text-properties
7762 (point) (next-single-property-change (point) 'org-filter-type)
7763 `(invisible org-filtered org-filter-type ,type))))
7764 (set (intern (format "org-agenda-%s-filter" (intern-soft type))) nil)
7765 (setq org-agenda-filter-form nil)
7766 (org-agenda-set-mode-name)
7767 (org-agenda-finalize)))
7769 (defun org-agenda-filter-show-all-tag nil
7770 (org-agenda-remove-filter 'tag))
7771 (defun org-agenda-filter-show-all-re nil
7772 (org-agenda-remove-filter 'regexp))
7773 (defun org-agenda-filter-show-all-effort nil
7774 (org-agenda-remove-filter 'effort))
7775 (defun org-agenda-filter-show-all-cat nil
7776 (org-agenda-remove-filter 'category))
7777 (defun org-agenda-filter-show-all-top-filter nil
7778 (org-agenda-remove-filter 'top-headline))
7780 (defun org-agenda-manipulate-query-add ()
7781 "Manipulate the query by adding a search term with positive selection.
7782 Positive selection means the term must be matched for selection of an entry."
7783 (interactive)
7784 (org-agenda-manipulate-query ?\[))
7785 (defun org-agenda-manipulate-query-subtract ()
7786 "Manipulate the query by adding a search term with negative selection.
7787 Negative selection means term must not be matched for selection of an entry."
7788 (interactive)
7789 (org-agenda-manipulate-query ?\]))
7790 (defun org-agenda-manipulate-query-add-re ()
7791 "Manipulate the query by adding a search regexp with positive selection.
7792 Positive selection means the regexp must match for selection of an entry."
7793 (interactive)
7794 (org-agenda-manipulate-query ?\{))
7795 (defun org-agenda-manipulate-query-subtract-re ()
7796 "Manipulate the query by adding a search regexp with negative selection.
7797 Negative selection means regexp must not match for selection of an entry."
7798 (interactive)
7799 (org-agenda-manipulate-query ?\}))
7800 (defun org-agenda-manipulate-query (char)
7801 (cond
7802 ((eq org-agenda-type 'agenda)
7803 (let ((org-agenda-include-inactive-timestamps t))
7804 (org-agenda-redo))
7805 (message "Display now includes inactive timestamps as well"))
7806 ((eq org-agenda-type 'search)
7807 (org-add-to-string
7808 'org-agenda-query-string
7809 (if org-agenda-last-search-view-search-was-boolean
7810 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7811 (?\{ . " +{}") (?\} . " -{}"))))
7812 " "))
7813 (setq org-agenda-redo-command
7814 (list 'org-search-view
7815 (car (get-text-property (min (1- (point-max)) (point))
7816 'org-last-args))
7817 org-agenda-query-string
7818 (+ (length org-agenda-query-string)
7819 (if (member char '(?\{ ?\})) 0 1))))
7820 (set-register org-agenda-query-register org-agenda-query-string)
7821 (let ((org-agenda-overriding-arguments
7822 (cdr org-agenda-redo-command)))
7823 (org-agenda-redo)))
7824 (t (error "Cannot manipulate query for %s-type agenda buffers"
7825 org-agenda-type))))
7827 (defun org-add-to-string (var string)
7828 (set var (concat (symbol-value var) string)))
7830 (defun org-agenda-goto-date (span)
7831 "Jump to DATE in agenda."
7832 (interactive "P")
7833 (let* ((org-read-date-prefer-future
7834 (eval org-agenda-jump-prefer-future))
7835 (date (org-read-date))
7836 (day (time-to-days (org-time-string-to-time date)))
7837 (org-agenda-sticky-orig org-agenda-sticky)
7838 (org-agenda-buffer-tmp-name (buffer-name))
7839 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7840 (0-arg (or current-prefix-arg (car args)))
7841 (2-arg (nth 2 args))
7842 (with-hour-p (nth 4 org-agenda-redo-command))
7843 (newcmd (list 'org-agenda-list 0-arg date
7844 (org-agenda-span-to-ndays
7845 2-arg (org-time-string-to-absolute date))
7846 with-hour-p))
7847 (newargs (cdr newcmd))
7848 (inhibit-read-only t)
7849 org-agenda-sticky)
7850 (if (not (org-agenda-check-type t 'agenda))
7851 (error "Not available in non-agenda views")
7852 (add-text-properties (point-min) (point-max)
7853 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7854 (org-agenda-redo)
7855 (goto-char (point-min))
7856 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7857 (save-excursion (move-beginning-of-line 2) (eobp))))
7858 (move-beginning-of-line 2))
7859 (setq org-agenda-sticky org-agenda-sticky-orig
7860 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7862 (defun org-agenda-goto-today ()
7863 "Go to today."
7864 (interactive)
7865 (org-agenda-check-type t 'agenda)
7866 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7867 (curspan (nth 2 args))
7868 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7869 (cond
7870 (tdpos (goto-char tdpos))
7871 ((eq org-agenda-type 'agenda)
7872 (let* ((sd (org-agenda-compute-starting-span
7873 (org-today) (or curspan org-agenda-span)))
7874 (org-agenda-overriding-arguments args))
7875 (setf (nth 1 org-agenda-overriding-arguments) sd)
7876 (org-agenda-redo)
7877 (org-agenda-find-same-or-today-or-agenda)))
7878 (t (error "Cannot find today")))))
7880 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7881 (goto-char
7882 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7883 (text-property-any (point-min) (point-max) 'org-today t)
7884 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7885 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7886 (org-agenda-backward-block))
7887 (point-min))))
7889 (defun org-agenda-backward-block ()
7890 "Move backward by one agenda block."
7891 (interactive)
7892 (org-agenda-forward-block 'backward))
7894 (defun org-agenda-forward-block (&optional backward)
7895 "Move forward by one agenda block.
7896 When optional argument BACKWARD is set, go backward"
7897 (interactive)
7898 (cond ((not (derived-mode-p 'org-agenda-mode))
7899 (user-error
7900 "Cannot execute this command outside of org-agenda-mode buffers"))
7901 ((looking-at (if backward "\\`" "\\'"))
7902 (message "Already at the %s block" (if backward "first" "last")))
7903 (t (let ((pos (prog1 (point)
7904 (ignore-errors (if backward (backward-char 1)
7905 (move-end-of-line 1)))))
7906 (f (if backward
7907 'previous-single-property-change
7908 'next-single-property-change))
7909 moved dest)
7910 (while (and (setq dest (funcall
7911 f (point) 'org-agenda-structural-header))
7912 (not (get-text-property
7913 (point) 'org-agenda-structural-header)))
7914 (setq moved t)
7915 (goto-char dest))
7916 (if moved (move-beginning-of-line 1)
7917 (goto-char (if backward (point-min) (point-max)))
7918 (move-beginning-of-line 1)
7919 (message "No %s block" (if backward "previous" "further")))))))
7921 (defun org-agenda-later (arg)
7922 "Go forward in time by the current span.
7923 With prefix ARG, go forward that many times the current span."
7924 (interactive "p")
7925 (org-agenda-check-type t 'agenda)
7926 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7927 (span (or (nth 2 args) org-agenda-current-span))
7928 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7929 (greg (calendar-gregorian-from-absolute sd))
7930 (cnt (org-get-at-bol 'org-day-cnt))
7931 greg2)
7932 (cond
7933 ((numberp span)
7934 (setq sd (+ (* span arg) sd)))
7935 ((eq span 'day)
7936 (setq sd (+ arg sd)))
7937 ((eq span 'week)
7938 (setq sd (+ (* 7 arg) sd)))
7939 ((eq span 'fortnight)
7940 (setq sd (+ (* 14 arg) sd)))
7941 ((eq span 'month)
7942 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7943 sd (calendar-absolute-from-gregorian greg2))
7944 (setcar greg2 (1+ (car greg2))))
7945 ((eq span 'year)
7946 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7947 sd (calendar-absolute-from-gregorian greg2))
7948 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7950 (setq sd (+ (* span arg) sd))))
7951 (let ((org-agenda-overriding-cmd
7952 ;; `cmd' may have been set by `org-agenda-run-series' which
7953 ;; uses `org-agenda-overriding-cmd' to decide whether
7954 ;; overriding is allowed for `cmd'
7955 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7956 (org-agenda-overriding-arguments
7957 (list (car args) sd span)))
7958 (org-agenda-redo)
7959 (org-agenda-find-same-or-today-or-agenda cnt))))
7961 (defun org-agenda-earlier (arg)
7962 "Go backward in time by the current span.
7963 With prefix ARG, go backward that many times the current span."
7964 (interactive "p")
7965 (org-agenda-later (- arg)))
7967 (defun org-agenda-view-mode-dispatch ()
7968 "Call one of the view mode commands."
7969 (interactive)
7970 (message "View: [d]ay [w]eek for[t]night [m]onth [y]ear [SPC]reset [q]uit/abort
7971 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7972 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7973 (pcase (read-char-exclusive)
7974 (?\ (call-interactively 'org-agenda-reset-view))
7975 (?d (call-interactively 'org-agenda-day-view))
7976 (?w (call-interactively 'org-agenda-week-view))
7977 (?t (call-interactively 'org-agenda-fortnight-view))
7978 (?m (call-interactively 'org-agenda-month-view))
7979 (?y (call-interactively 'org-agenda-year-view))
7980 (?l (call-interactively 'org-agenda-log-mode))
7981 (?L (org-agenda-log-mode '(4)))
7982 (?c (org-agenda-log-mode 'clockcheck))
7983 ((or ?F ?f) (call-interactively 'org-agenda-follow-mode))
7984 (?a (call-interactively 'org-agenda-archives-mode))
7985 (?A (org-agenda-archives-mode 'files))
7986 ((or ?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7987 ((or ?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7988 (?G (call-interactively 'org-agenda-toggle-time-grid))
7989 (?D (call-interactively 'org-agenda-toggle-diary))
7990 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7991 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7992 (org-agenda-check-type t 'agenda)
7993 (org-agenda-redo))
7994 (message "Display now includes inactive timestamps as well"))
7995 (?q (message "Abort"))
7996 (key (user-error "Invalid key: %s" key))))
7998 (defun org-agenda-reset-view ()
7999 "Switch to default view for agenda."
8000 (interactive)
8001 (org-agenda-change-time-span org-agenda-span))
8003 (defun org-agenda-day-view (&optional day-of-month)
8004 "Switch to daily view for agenda.
8005 With argument DAY-OF-MONTH, switch to that day of the month."
8006 (interactive "P")
8007 (org-agenda-change-time-span 'day day-of-month))
8009 (defun org-agenda-week-view (&optional iso-week)
8010 "Switch to weekly view for agenda.
8011 With argument ISO-WEEK, switch to the corresponding ISO week.
8012 If ISO-WEEK has more then 2 digits, only the last two encode
8013 the week. Any digits before this encode a year. So 200712
8014 means week 12 of year 2007. Years ranging from 70 years ago
8015 to 30 years in the future can also be written as 2-digit years."
8016 (interactive "P")
8017 (org-agenda-change-time-span 'week iso-week))
8019 (defun org-agenda-fortnight-view (&optional iso-week)
8020 "Switch to fortnightly view for agenda.
8021 With argument ISO-WEEK, switch to the corresponding ISO week.
8022 If ISO-WEEK has more then 2 digits, only the last two encode
8023 the week. Any digits before this encode a year. So 200712
8024 means week 12 of year 2007. Years ranging from 70 years ago
8025 to 30 years in the future can also be written as 2-digit years."
8026 (interactive "P")
8027 (org-agenda-change-time-span 'fortnight iso-week))
8029 (defun org-agenda-month-view (&optional month)
8030 "Switch to monthly view for agenda.
8031 With argument MONTH, switch to that month. If MONTH has more
8032 then 2 digits, only the last two encode the month. Any digits
8033 before this encode a year. So 200712 means December year 2007.
8034 Years ranging from 70 years ago to 30 years in the future can
8035 also be written as 2-digit years."
8036 (interactive "P")
8037 (org-agenda-change-time-span 'month month))
8039 (defun org-agenda-year-view (&optional year)
8040 "Switch to yearly view for agenda.
8041 With argument YEAR, switch to that year. Years ranging from 70
8042 years ago to 30 years in the future can also be written as
8043 2-digit years."
8044 (interactive "P")
8045 (when year
8046 (setq year (org-small-year-to-year year)))
8047 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
8048 (org-agenda-change-time-span 'year year)
8049 (error "Abort")))
8051 (defun org-agenda-change-time-span (span &optional n)
8052 "Change the agenda view to SPAN.
8053 SPAN may be `day', `week', `fortnight', `month', `year'."
8054 (org-agenda-check-type t 'agenda)
8055 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
8056 (curspan (nth 2 args)))
8057 (if (and (not n) (equal curspan span))
8058 (error "Viewing span is already \"%s\"" span))
8059 (let* ((sd (or (org-get-at-bol 'day)
8060 (nth 1 args)
8061 org-starting-day))
8062 (sd (org-agenda-compute-starting-span sd span n))
8063 (org-agenda-overriding-cmd
8064 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
8065 (org-agenda-overriding-arguments
8066 (list (car args) sd span)))
8067 (org-agenda-redo)
8068 (org-agenda-find-same-or-today-or-agenda))
8069 (org-agenda-set-mode-name)
8070 (message "Switched to %s view" span)))
8072 (defun org-agenda-compute-starting-span (sd span &optional n)
8073 "Compute starting date for agenda.
8074 SPAN may be `day', `week', `fortnight', `month', `year'. The return value
8075 is a cons cell with the starting date and the number of days,
8076 so that the date SD will be in that range."
8077 (let* ((greg (calendar-gregorian-from-absolute sd))
8078 (dg (nth 1 greg))
8079 (mg (car greg))
8080 (yg (nth 2 greg)))
8081 (cond
8082 ((eq span 'day)
8083 (when n
8084 (setq sd (+ (calendar-absolute-from-gregorian
8085 (list mg 1 yg))
8086 n -1))))
8087 ((or (eq span 'week) (eq span 'fortnight))
8088 (let* ((nt (calendar-day-of-week
8089 (calendar-gregorian-from-absolute sd)))
8090 (d (if org-agenda-start-on-weekday
8091 (- nt org-agenda-start-on-weekday)
8094 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
8095 (when n
8096 (require 'cal-iso)
8097 (when (> n 99)
8098 (setq y1 (org-small-year-to-year (/ n 100))
8099 n (mod n 100)))
8100 (setq sd
8101 (calendar-iso-to-absolute
8102 (list n 1
8103 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
8104 ((eq span 'month)
8105 (let (y1)
8106 (when (and n (> n 99))
8107 (setq y1 (org-small-year-to-year (/ n 100))
8108 n (mod n 100)))
8109 (setq sd (calendar-absolute-from-gregorian
8110 (list (or n mg) 1 (or y1 yg))))))
8111 ((eq span 'year)
8112 (setq sd (calendar-absolute-from-gregorian
8113 (list 1 1 (or n yg))))))
8114 sd))
8116 (defun org-agenda-next-date-line (&optional arg)
8117 "Jump to the next line indicating a date in agenda buffer."
8118 (interactive "p")
8119 (org-agenda-check-type t 'agenda)
8120 (beginning-of-line 1)
8121 ;; This does not work if user makes date format that starts with a blank
8122 (if (looking-at "^\\S-") (forward-char 1))
8123 (if (not (re-search-forward "^\\S-" nil t arg))
8124 (progn
8125 (backward-char 1)
8126 (error "No next date after this line in this buffer")))
8127 (goto-char (match-beginning 0)))
8129 (defun org-agenda-previous-date-line (&optional arg)
8130 "Jump to the previous line indicating a date in agenda buffer."
8131 (interactive "p")
8132 (org-agenda-check-type t 'agenda)
8133 (beginning-of-line 1)
8134 (if (not (re-search-backward "^\\S-" nil t arg))
8135 (error "No previous date before this line in this buffer")))
8137 ;; Initialize the highlight
8138 (defvar org-hl (make-overlay 1 1))
8139 (overlay-put org-hl 'face 'highlight)
8141 (defun org-highlight (begin end &optional buffer)
8142 "Highlight a region with overlay."
8143 (move-overlay org-hl begin end (or buffer (current-buffer))))
8145 (defun org-unhighlight ()
8146 "Detach overlay INDEX."
8147 (delete-overlay org-hl))
8149 (defun org-unhighlight-once ()
8150 "Remove the highlight from its position, and this function from the hook."
8151 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8152 (org-unhighlight))
8154 (defvar org-agenda-pre-follow-window-conf nil)
8155 (defun org-agenda-follow-mode ()
8156 "Toggle follow mode in an agenda buffer."
8157 (interactive)
8158 (unless org-agenda-follow-mode
8159 (setq org-agenda-pre-follow-window-conf
8160 (current-window-configuration)))
8161 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8162 (unless org-agenda-follow-mode
8163 (set-window-configuration org-agenda-pre-follow-window-conf))
8164 (org-agenda-set-mode-name)
8165 (org-agenda-do-context-action)
8166 (message "Follow mode is %s"
8167 (if org-agenda-follow-mode "on" "off")))
8169 (defun org-agenda-entry-text-mode (&optional arg)
8170 "Toggle entry text mode in an agenda buffer."
8171 (interactive "P")
8172 (if (or org-agenda-tag-filter
8173 org-agenda-category-filter
8174 org-agenda-regexp-filter
8175 org-agenda-top-headline-filter)
8176 (user-error "Can't show entry text in filtered views")
8177 (setq org-agenda-entry-text-mode (or (integerp arg)
8178 (not org-agenda-entry-text-mode)))
8179 (org-agenda-entry-text-hide)
8180 (and org-agenda-entry-text-mode
8181 (let ((org-agenda-entry-text-maxlines
8182 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8183 (org-agenda-entry-text-show)))
8184 (org-agenda-set-mode-name)
8185 (message "Entry text mode is %s%s"
8186 (if org-agenda-entry-text-mode "on" "off")
8187 (if (not org-agenda-entry-text-mode) ""
8188 (format " (maximum number of lines is %d)"
8189 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8191 (defun org-agenda-clockreport-mode ()
8192 "Toggle clocktable mode in an agenda buffer."
8193 (interactive)
8194 (org-agenda-check-type t 'agenda)
8195 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
8196 (setq org-agenda-start-with-clockreport-mode org-agenda-clockreport-mode)
8197 (org-agenda-set-mode-name)
8198 (org-agenda-redo)
8199 (message "Clocktable mode is %s"
8200 (if org-agenda-clockreport-mode "on" "off")))
8202 (defun org-agenda-log-mode (&optional special)
8203 "Toggle log mode in an agenda buffer.
8205 With argument SPECIAL, show all possible log items, not only the ones
8206 configured in `org-agenda-log-mode-items'.
8208 With a `\\[universal-argument] \\[universal-argument]' prefix, show *only* \
8209 log items, nothing else."
8210 (interactive "P")
8211 (org-agenda-check-type t 'agenda)
8212 (setq org-agenda-show-log
8213 (cond
8214 ((equal special '(16)) 'only)
8215 ((eq special 'clockcheck)
8216 (if (eq org-agenda-show-log 'clockcheck)
8217 nil 'clockcheck))
8218 (special '(closed clock state))
8219 (t (not org-agenda-show-log))))
8220 (setq org-agenda-start-with-log-mode org-agenda-show-log)
8221 (org-agenda-set-mode-name)
8222 (org-agenda-redo)
8223 (message "Log mode is %s" (if org-agenda-show-log "on" "off")))
8225 (defun org-agenda-archives-mode (&optional with-files)
8226 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8227 When called with a prefix argument, include all archive files as well."
8228 (interactive "P")
8229 (setq org-agenda-archives-mode
8230 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8231 (org-agenda-set-mode-name)
8232 (org-agenda-redo)
8233 (message
8234 "%s"
8235 (cond
8236 ((eq org-agenda-archives-mode nil)
8237 "No archives are included")
8238 ((eq org-agenda-archives-mode 'trees)
8239 (format "Trees with :%s: tag are included" org-archive-tag))
8240 ((eq org-agenda-archives-mode t)
8241 (format "Trees with :%s: tag and all active archive files are included"
8242 org-archive-tag)))))
8244 (defun org-agenda-toggle-diary ()
8245 "Toggle diary inclusion in an agenda buffer."
8246 (interactive)
8247 (org-agenda-check-type t 'agenda)
8248 (setq org-agenda-include-diary (not org-agenda-include-diary))
8249 (org-agenda-redo)
8250 (org-agenda-set-mode-name)
8251 (message "Diary inclusion turned %s"
8252 (if org-agenda-include-diary "on" "off")))
8254 (defun org-agenda-toggle-deadlines ()
8255 "Toggle inclusion of entries with a deadline in an agenda buffer."
8256 (interactive)
8257 (org-agenda-check-type t 'agenda)
8258 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8259 (org-agenda-redo)
8260 (org-agenda-set-mode-name)
8261 (message "Deadlines inclusion turned %s"
8262 (if org-agenda-include-deadlines "on" "off")))
8264 (defun org-agenda-toggle-time-grid ()
8265 "Toggle time grid in an agenda buffer."
8266 (interactive)
8267 (org-agenda-check-type t 'agenda)
8268 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8269 (org-agenda-redo)
8270 (org-agenda-set-mode-name)
8271 (message "Time-grid turned %s"
8272 (if org-agenda-use-time-grid "on" "off")))
8274 (defun org-agenda-set-mode-name ()
8275 "Set the mode name to indicate all the small mode settings."
8276 (setq mode-name
8277 (list "Org-Agenda"
8278 (if (get 'org-agenda-files 'org-restrict) " []" "")
8280 '(:eval (org-agenda-span-name org-agenda-current-span))
8281 (if org-agenda-follow-mode " Follow" "")
8282 (if org-agenda-entry-text-mode " ETxt" "")
8283 (if org-agenda-include-diary " Diary" "")
8284 (if org-agenda-include-deadlines " Ddl" "")
8285 (if org-agenda-use-time-grid " Grid" "")
8286 (if (and (boundp 'org-habit-show-habits)
8287 org-habit-show-habits) " Habit" "")
8288 (cond
8289 ((consp org-agenda-show-log) " LogAll")
8290 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8291 (org-agenda-show-log " Log")
8292 (t ""))
8293 (if (or org-agenda-category-filter
8294 (get 'org-agenda-category-filter :preset-filter))
8295 '(:eval (propertize
8296 (concat " <"
8297 (mapconcat
8298 'identity
8299 (append
8300 (get 'org-agenda-category-filter :preset-filter)
8301 org-agenda-category-filter)
8303 ">")
8304 'face 'org-agenda-filter-category
8305 'help-echo "Category used in filtering")) "")
8306 (if (or org-agenda-tag-filter
8307 (get 'org-agenda-tag-filter :preset-filter))
8308 '(:eval (propertize
8309 (concat " {"
8310 (mapconcat
8311 'identity
8312 (append
8313 (get 'org-agenda-tag-filter :preset-filter)
8314 org-agenda-tag-filter)
8316 "}")
8317 'face 'org-agenda-filter-tags
8318 'help-echo "Tags used in filtering")) "")
8319 (if (or org-agenda-effort-filter
8320 (get 'org-agenda-effort-filter :preset-filter))
8321 '(:eval (propertize
8322 (concat " {"
8323 (mapconcat
8324 'identity
8325 (append
8326 (get 'org-agenda-effort-filter :preset-filter)
8327 org-agenda-effort-filter)
8329 "}")
8330 'face 'org-agenda-filter-effort
8331 'help-echo "Effort conditions used in filtering")) "")
8332 (if (or org-agenda-regexp-filter
8333 (get 'org-agenda-regexp-filter :preset-filter))
8334 '(:eval (propertize
8335 (concat " ["
8336 (mapconcat
8337 'identity
8338 (append
8339 (get 'org-agenda-regexp-filter :preset-filter)
8340 org-agenda-regexp-filter)
8342 "]")
8343 'face 'org-agenda-filter-regexp
8344 'help-echo "Regexp used in filtering")) "")
8345 (if org-agenda-archives-mode
8346 (if (eq org-agenda-archives-mode t)
8347 " Archives"
8348 (format " :%s:" org-archive-tag))
8350 (if org-agenda-clockreport-mode " Clock" "")))
8351 (force-mode-line-update))
8353 (defun org-agenda-update-agenda-type ()
8354 "Update the agenda type after each command."
8355 (setq org-agenda-type
8356 (or (get-text-property (point) 'org-agenda-type)
8357 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8359 (defun org-agenda-next-line ()
8360 "Move cursor to the next line, and show if follow mode is active."
8361 (interactive)
8362 (call-interactively 'next-line)
8363 (org-agenda-do-context-action))
8365 (defun org-agenda-previous-line ()
8366 "Move cursor to the previous line, and show if follow-mode is active."
8367 (interactive)
8368 (call-interactively 'previous-line)
8369 (org-agenda-do-context-action))
8371 (defun org-agenda-next-item (n)
8372 "Move cursor to next agenda item."
8373 (interactive "p")
8374 (let ((col (current-column)))
8375 (dotimes (c n)
8376 (when (next-single-property-change (point-at-eol) 'org-marker)
8377 (move-end-of-line 1)
8378 (goto-char (next-single-property-change (point) 'org-marker))))
8379 (org-move-to-column col))
8380 (org-agenda-do-context-action))
8382 (defun org-agenda-previous-item (n)
8383 "Move cursor to next agenda item."
8384 (interactive "p")
8385 (dotimes (c n)
8386 (let ((col (current-column))
8387 (goto (save-excursion
8388 (move-end-of-line 0)
8389 (previous-single-property-change (point) 'org-marker))))
8390 (if goto (goto-char goto))
8391 (org-move-to-column col)))
8392 (org-agenda-do-context-action))
8394 (defun org-agenda-do-context-action ()
8395 "Show outline path and, maybe, follow mode window."
8396 (let ((m (org-get-at-bol 'org-marker)))
8397 (when (and (markerp m) (marker-buffer m))
8398 (and org-agenda-follow-mode
8399 (if org-agenda-follow-indirect
8400 (org-agenda-tree-to-indirect-buffer nil)
8401 (org-agenda-show)))
8402 (and org-agenda-show-outline-path
8403 (org-with-point-at m (org-display-outline-path t))))))
8405 (defun org-agenda-show-tags ()
8406 "Show the tags applicable to the current item."
8407 (interactive)
8408 (let* ((tags (org-get-at-bol 'tags)))
8409 (if tags
8410 (message "Tags are :%s:"
8411 (org-no-properties (mapconcat 'identity tags ":")))
8412 (message "No tags associated with this line"))))
8414 (defun org-agenda-goto (&optional highlight)
8415 "Go to the entry at point in the corresponding Org file."
8416 (interactive)
8417 (let* ((marker (or (org-get-at-bol 'org-marker)
8418 (org-agenda-error)))
8419 (buffer (marker-buffer marker))
8420 (pos (marker-position marker)))
8421 (switch-to-buffer-other-window buffer)
8422 (widen)
8423 (push-mark)
8424 (goto-char pos)
8425 (when (derived-mode-p 'org-mode)
8426 (org-show-context 'agenda)
8427 (recenter (/ (window-height) 2))
8428 (org-back-to-heading t)
8429 (let ((case-fold-search nil))
8430 (when (re-search-forward org-complex-heading-regexp nil t)
8431 (goto-char (match-beginning 4)))))
8432 (run-hooks 'org-agenda-after-show-hook)
8433 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8435 (defvar org-agenda-after-show-hook nil
8436 "Normal hook run after an item has been shown from the agenda.
8437 Point is in the buffer where the item originated.")
8439 (defun org-agenda-kill ()
8440 "Kill the entry or subtree belonging to the current agenda entry."
8441 (interactive)
8442 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8443 (let* ((bufname-orig (buffer-name))
8444 (marker (or (org-get-at-bol 'org-marker)
8445 (org-agenda-error)))
8446 (buffer (marker-buffer marker))
8447 (pos (marker-position marker))
8448 (type (org-get-at-bol 'type))
8449 dbeg dend (n 0) conf)
8450 (org-with-remote-undo buffer
8451 (with-current-buffer buffer
8452 (save-excursion
8453 (goto-char pos)
8454 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8455 (setq dbeg (progn (org-back-to-heading t) (point))
8456 dend (org-end-of-subtree t t))
8457 (setq dbeg (point-at-bol)
8458 dend (min (point-max) (1+ (point-at-eol)))))
8459 (goto-char dbeg)
8460 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8461 (setq conf (or (eq t org-agenda-confirm-kill)
8462 (and (numberp org-agenda-confirm-kill)
8463 (> n org-agenda-confirm-kill))))
8464 (and conf
8465 (not (y-or-n-p
8466 (format "Delete entry with %d lines in buffer \"%s\"? "
8467 n (buffer-name buffer))))
8468 (error "Abort"))
8469 (let ((org-agenda-buffer-name bufname-orig))
8470 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8471 (with-current-buffer buffer (delete-region dbeg dend))
8472 (message "Agenda item and source killed"))))
8474 (defvar org-archive-default-command) ; defined in org-archive.el
8475 (defun org-agenda-archive-default ()
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))
8481 (defun org-agenda-archive-default-with-confirmation ()
8482 "Archive the entry or subtree belonging to the current agenda entry."
8483 (interactive)
8484 (require 'org-archive)
8485 (org-agenda-archive-with org-archive-default-command 'confirm))
8487 (defun org-agenda-archive ()
8488 "Archive the entry or subtree belonging to the current agenda entry."
8489 (interactive)
8490 (org-agenda-archive-with 'org-archive-subtree))
8492 (defun org-agenda-archive-to-archive-sibling ()
8493 "Move the entry to the archive sibling."
8494 (interactive)
8495 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8497 (defun org-agenda-archive-with (cmd &optional confirm)
8498 "Move the entry to the archive sibling."
8499 (interactive)
8500 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8501 (let* ((bufname-orig (buffer-name))
8502 (marker (or (org-get-at-bol 'org-marker)
8503 (org-agenda-error)))
8504 (buffer (marker-buffer marker))
8505 (pos (marker-position marker)))
8506 (org-with-remote-undo buffer
8507 (with-current-buffer buffer
8508 (if (derived-mode-p 'org-mode)
8509 (if (and confirm
8510 (not (y-or-n-p "Archive this subtree or entry? ")))
8511 (error "Abort")
8512 (save-window-excursion
8513 (goto-char pos)
8514 (let ((org-agenda-buffer-name bufname-orig))
8515 (org-remove-subtree-entries-from-agenda))
8516 (org-back-to-heading t)
8517 (funcall cmd)))
8518 (error "Archiving works only in Org files"))))))
8520 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8521 "Remove all lines in the agenda that correspond to a given subtree.
8522 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8523 If this information is not given, the function uses the tree at point."
8524 (let ((buf (or buf (current-buffer))) m p)
8525 (save-excursion
8526 (unless (and beg end)
8527 (org-back-to-heading t)
8528 (setq beg (point))
8529 (org-end-of-subtree t)
8530 (setq end (point)))
8531 (set-buffer (get-buffer org-agenda-buffer-name))
8532 (save-excursion
8533 (goto-char (point-max))
8534 (beginning-of-line 1)
8535 (while (not (bobp))
8536 (when (and (setq m (org-get-at-bol 'org-marker))
8537 (equal buf (marker-buffer m))
8538 (setq p (marker-position m))
8539 (>= p beg)
8540 (< p end))
8541 (let ((inhibit-read-only t))
8542 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8543 (beginning-of-line 0))))))
8545 (defun org-agenda-refile (&optional goto rfloc no-update)
8546 "Refile the item at point.
8548 When called with `\\[universal-argument] \\[universal-argument]', \
8549 go to the location of the last
8550 refiled item.
8552 When called with `\\[universal-argument] \\[universal-argument] \
8553 \\[universal-argument]' prefix or when GOTO is 0, clear
8554 the refile cache.
8556 RFLOC can be a refile location obtained in a different way.
8558 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8559 (interactive "P")
8560 (cond
8561 ((member goto '(0 (64)))
8562 (org-refile-cache-clear))
8563 ((equal goto '(16))
8564 (org-refile-goto-last-stored))
8566 (let* ((buffer-orig (buffer-name))
8567 (marker (or (org-get-at-bol 'org-hd-marker)
8568 (org-agenda-error)))
8569 (buffer (marker-buffer marker))
8570 (pos (marker-position marker))
8571 (rfloc (or rfloc
8572 (org-refile-get-location
8573 (if goto "Goto" "Refile to") buffer
8574 org-refile-allow-creating-parent-nodes))))
8575 (with-current-buffer buffer
8576 (org-with-wide-buffer
8577 (goto-char marker)
8578 (let ((org-agenda-buffer-name buffer-orig))
8579 (org-remove-subtree-entries-from-agenda))
8580 (org-refile goto buffer rfloc))))
8581 (unless no-update (org-agenda-redo)))))
8583 (defun org-agenda-open-link (&optional arg)
8584 "Open the link(s) in the current entry, if any.
8585 This looks for a link in the displayed line in the agenda.
8586 It also looks at the text of the entry itself."
8587 (interactive "P")
8588 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8589 (org-get-at-bol 'org-marker)))
8590 (buffer (and marker (marker-buffer marker)))
8591 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8592 (lkall (and buffer (org-offer-links-in-entry
8593 buffer marker arg prefix)))
8594 (lk0 (car lkall))
8595 (lk (if (stringp lk0) (list lk0) lk0))
8596 (lkend (cdr lkall))
8597 trg)
8598 (cond
8599 ((and buffer lk)
8600 (mapcar (lambda(l)
8601 (with-current-buffer buffer
8602 (setq trg (and (string-match org-bracket-link-regexp l)
8603 (match-string 1 l)))
8604 (if (or (not trg) (string-match org-any-link-re trg))
8605 (org-with-wide-buffer
8606 (goto-char marker)
8607 (when (search-forward l nil lkend)
8608 (goto-char (match-beginning 0))
8609 (org-open-at-point)))
8610 ;; This is an internal link, widen the buffer
8611 (switch-to-buffer-other-window buffer)
8612 (widen)
8613 (goto-char marker)
8614 (when (search-forward l nil lkend)
8615 (goto-char (match-beginning 0))
8616 (org-open-at-point)))))
8617 lk))
8618 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8619 (save-excursion
8620 (beginning-of-line 1)
8621 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8622 (org-open-link-from-string (match-string 1)))
8623 (t (message "No link to open here")))))
8625 (defun org-agenda-copy-local-variable (var)
8626 "Get a variable from a referenced buffer and install it here."
8627 (let ((m (org-get-at-bol 'org-marker)))
8628 (when (and m (buffer-live-p (marker-buffer m)))
8629 (set (make-local-variable var)
8630 (with-current-buffer (marker-buffer m)
8631 (symbol-value var))))))
8633 (defun org-agenda-switch-to (&optional delete-other-windows)
8634 "Go to the Org mode file which contains the item at point.
8635 When optional argument DELETE-OTHER-WINDOWS is non-nil, the
8636 displayed Org file fills the frame."
8637 (interactive)
8638 (if (and org-return-follows-link
8639 (not (org-get-at-bol 'org-marker))
8640 (org-in-regexp org-bracket-link-regexp))
8641 (org-open-link-from-string (match-string 0))
8642 (let* ((marker (or (org-get-at-bol 'org-marker)
8643 (org-agenda-error)))
8644 (buffer (marker-buffer marker))
8645 (pos (marker-position marker)))
8646 (unless buffer (user-error "Trying to switch to non-existent buffer"))
8647 (pop-to-buffer-same-window buffer)
8648 (when delete-other-windows (delete-other-windows))
8649 (widen)
8650 (goto-char pos)
8651 (when (derived-mode-p 'org-mode)
8652 (org-show-context 'agenda)
8653 (run-hooks 'org-agenda-after-show-hook)))))
8655 (defun org-agenda-goto-mouse (ev)
8656 "Go to the Org file which contains the item at the mouse click."
8657 (interactive "e")
8658 (mouse-set-point ev)
8659 (org-agenda-goto))
8661 (defun org-agenda-show (&optional full-entry)
8662 "Display the Org file which contains the item at point.
8663 With prefix argument FULL-ENTRY, make the entire entry visible
8664 if it was hidden in the outline."
8665 (interactive "P")
8666 (let ((win (selected-window)))
8667 (org-agenda-goto t)
8668 (when full-entry (org-show-entry))
8669 (select-window win)))
8671 (defvar org-agenda-show-window nil)
8672 (defun org-agenda-show-and-scroll-up (&optional arg)
8673 "Display the Org file which contains the item at point.
8675 When called repeatedly, scroll the window that is displaying the buffer.
8677 With a `\\[universal-argument]' prefix, use `org-show-entry' instead of \
8678 `outline-show-subtree'
8679 to display the item, so that drawers and logbooks stay folded."
8680 (interactive "P")
8681 (let ((win (selected-window)))
8682 (if (and (window-live-p org-agenda-show-window)
8683 (eq this-command last-command))
8684 (progn
8685 (select-window org-agenda-show-window)
8686 (ignore-errors (scroll-up)))
8687 (org-agenda-goto t)
8688 (if arg (org-show-entry) (outline-show-subtree))
8689 (setq org-agenda-show-window (selected-window)))
8690 (select-window win)))
8692 (defun org-agenda-show-scroll-down ()
8693 "Scroll down the window showing the agenda."
8694 (interactive)
8695 (let ((win (selected-window)))
8696 (when (window-live-p org-agenda-show-window)
8697 (select-window org-agenda-show-window)
8698 (ignore-errors (scroll-down))
8699 (select-window win))))
8701 (defun org-agenda-show-1 (&optional more)
8702 "Display the Org file which contains the item at point.
8703 The prefix arg selects the amount of information to display:
8705 0 hide the subtree
8706 1 just show the entry according to defaults.
8707 2 show the children view
8708 3 show the subtree view
8709 4 show the entire subtree and any LOGBOOK drawers
8710 5 show the entire subtree and any drawers
8711 With prefix argument FULL-ENTRY, make the entire entry visible
8712 if it was hidden in the outline."
8713 (interactive "p")
8714 (let ((win (selected-window)))
8715 (org-agenda-goto t)
8716 (org-back-to-heading)
8717 (set-window-start (selected-window) (point-at-bol))
8718 (cond
8719 ((= more 0)
8720 (outline-hide-subtree)
8721 (save-excursion
8722 (org-back-to-heading)
8723 (run-hook-with-args 'org-cycle-hook 'folded))
8724 (message "Remote: FOLDED"))
8725 ((and (called-interactively-p 'any) (= more 1))
8726 (message "Remote: show with default settings"))
8727 ((= more 2)
8728 (outline-show-entry)
8729 (org-show-children)
8730 (save-excursion
8731 (org-back-to-heading)
8732 (run-hook-with-args 'org-cycle-hook 'children))
8733 (message "Remote: CHILDREN"))
8734 ((= more 3)
8735 (outline-show-subtree)
8736 (save-excursion
8737 (org-back-to-heading)
8738 (run-hook-with-args 'org-cycle-hook 'subtree))
8739 (message "Remote: SUBTREE"))
8740 ((= more 4)
8741 (outline-show-subtree)
8742 (save-excursion
8743 (org-back-to-heading)
8744 (org-cycle-hide-drawers 'subtree '("LOGBOOK")))
8745 (message "Remote: SUBTREE AND LOGBOOK"))
8746 ((> more 4)
8747 (outline-show-subtree)
8748 (message "Remote: SUBTREE AND ALL DRAWERS")))
8749 (select-window win)))
8751 (defvar org-agenda-cycle-counter nil)
8752 (defun org-agenda-cycle-show (&optional n)
8753 "Show the current entry in another window, with default settings.
8755 Default settings are taken from `org-show-context-detail'. When
8756 use repeatedly in immediate succession, the remote entry will
8757 cycle through visibility
8759 children -> subtree -> folded
8761 When called with a numeric prefix arg, that arg will be passed through to
8762 `org-agenda-show-1'. For the interpretation of that argument, see the
8763 docstring of `org-agenda-show-1'."
8764 (interactive "P")
8765 (if (integerp n)
8766 (setq org-agenda-cycle-counter n)
8767 (if (not (eq last-command this-command))
8768 (setq org-agenda-cycle-counter 1)
8769 (if (equal org-agenda-cycle-counter 0)
8770 (setq org-agenda-cycle-counter 2)
8771 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8772 (if (> org-agenda-cycle-counter 3)
8773 (setq org-agenda-cycle-counter 0)))))
8774 (org-agenda-show-1 org-agenda-cycle-counter))
8776 (defun org-agenda-recenter (arg)
8777 "Display the Org file which contains the item at point and recenter."
8778 (interactive "P")
8779 (let ((win (selected-window)))
8780 (org-agenda-goto t)
8781 (recenter arg)
8782 (select-window win)))
8784 (defun org-agenda-show-mouse (ev)
8785 "Display the Org file which contains the item at the mouse click."
8786 (interactive "e")
8787 (mouse-set-point ev)
8788 (org-agenda-show))
8790 (defun org-agenda-check-no-diary ()
8791 "Check if the entry is a diary link and abort if yes."
8792 (if (org-get-at-bol 'org-agenda-diary-link)
8793 (org-agenda-error)))
8795 (defun org-agenda-error ()
8796 "Throw an error when a command is not allowed in the agenda."
8797 (user-error "Command not allowed in this line"))
8799 (defun org-agenda-tree-to-indirect-buffer (arg)
8800 "Show the subtree corresponding to the current entry in an indirect buffer.
8801 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8803 With a numerical prefix ARG, go up to this level and then take that tree.
8804 With a negative numeric ARG, go up by this number of levels.
8806 With a `\\[universal-argument]' prefix, make a separate frame for this tree, \
8807 i.e. don't use
8808 the dedicated frame."
8809 (interactive "P")
8810 (if current-prefix-arg
8811 (org-agenda-do-tree-to-indirect-buffer arg)
8812 (let ((agenda-buffer (buffer-name))
8813 (agenda-window (selected-window))
8814 (indirect-window
8815 (and org-last-indirect-buffer
8816 (get-buffer-window org-last-indirect-buffer))))
8817 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8818 (unless (or (eq org-indirect-buffer-display 'new-frame)
8819 (eq org-indirect-buffer-display 'dedicated-frame))
8820 (unwind-protect
8821 (unless (and indirect-window (window-live-p indirect-window))
8822 (setq indirect-window (split-window agenda-window)))
8823 (and indirect-window (select-window indirect-window))
8824 (switch-to-buffer org-last-indirect-buffer :norecord)
8825 (fit-window-to-buffer indirect-window)))
8826 (select-window (get-buffer-window agenda-buffer))
8827 (setq org-agenda-last-indirect-buffer org-last-indirect-buffer))))
8829 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8830 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8831 (org-agenda-check-no-diary)
8832 (let* ((marker (or (org-get-at-bol 'org-marker)
8833 (org-agenda-error)))
8834 (buffer (marker-buffer marker))
8835 (pos (marker-position marker)))
8836 (with-current-buffer buffer
8837 (save-excursion
8838 (goto-char pos)
8839 (funcall 'org-tree-to-indirect-buffer arg)))))
8841 (defvar org-last-heading-marker (make-marker)
8842 "Marker pointing to the headline that last changed its TODO state
8843 by a remote command from the agenda.")
8845 (defun org-agenda-todo-nextset ()
8846 "Switch TODO entry to next sequence."
8847 (interactive)
8848 (org-agenda-todo 'nextset))
8850 (defun org-agenda-todo-previousset ()
8851 "Switch TODO entry to previous sequence."
8852 (interactive)
8853 (org-agenda-todo 'previousset))
8855 (defun org-agenda-todo (&optional arg)
8856 "Cycle TODO state of line at point, also in Org file.
8857 This changes the line at point, all other lines in the agenda referring to
8858 the same tree node, and the headline of the tree node in the Org file."
8859 (interactive "P")
8860 (org-agenda-check-no-diary)
8861 (let* ((col (current-column))
8862 (marker (or (org-get-at-bol 'org-marker)
8863 (org-agenda-error)))
8864 (buffer (marker-buffer marker))
8865 (pos (marker-position marker))
8866 (hdmarker (org-get-at-bol 'org-hd-marker))
8867 (todayp (org-agenda-today-p (org-get-at-bol 'day)))
8868 (inhibit-read-only t)
8869 org-agenda-headline-snapshot-before-repeat newhead just-one)
8870 (org-with-remote-undo buffer
8871 (with-current-buffer buffer
8872 (widen)
8873 (goto-char pos)
8874 (org-show-context 'agenda)
8875 (let ((current-prefix-arg arg))
8876 (call-interactively 'org-todo))
8877 (and (bolp) (forward-char 1))
8878 (setq newhead (org-get-heading))
8879 (when (and (bound-and-true-p
8880 org-agenda-headline-snapshot-before-repeat)
8881 (not (equal org-agenda-headline-snapshot-before-repeat
8882 newhead))
8883 todayp)
8884 (setq newhead org-agenda-headline-snapshot-before-repeat
8885 just-one t))
8886 (save-excursion
8887 (org-back-to-heading)
8888 (move-marker org-last-heading-marker (point))))
8889 (beginning-of-line 1)
8890 (save-window-excursion
8891 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8892 (when (bound-and-true-p org-clock-out-when-done)
8893 (string-match (concat "^" (regexp-opt org-done-keywords-for-agenda))
8894 newhead)
8895 (org-agenda-unmark-clocking-task))
8896 (org-move-to-column col)
8897 (org-agenda-mark-clocking-task))))
8899 (defun org-agenda-add-note (&optional arg)
8900 "Add a time-stamped note to the entry at point."
8901 (interactive "P")
8902 (org-agenda-check-no-diary)
8903 (let* ((marker (or (org-get-at-bol 'org-marker)
8904 (org-agenda-error)))
8905 (buffer (marker-buffer marker))
8906 (pos (marker-position marker))
8907 (hdmarker (org-get-at-bol 'org-hd-marker))
8908 (inhibit-read-only t))
8909 (with-current-buffer buffer
8910 (widen)
8911 (goto-char pos)
8912 (org-show-context 'agenda)
8913 (org-add-note))))
8915 (defun org-agenda-change-all-lines (newhead hdmarker
8916 &optional fixface just-this)
8917 "Change all lines in the agenda buffer which match HDMARKER.
8918 The new content of the line will be NEWHEAD (as modified by
8919 `org-agenda-format-item'). HDMARKER is checked with
8920 `equal' against all `org-hd-marker' text properties in the file.
8921 If FIXFACE is non-nil, the face of each item is modified according to
8922 the new TODO state.
8923 If JUST-THIS is non-nil, change just the current line, not all.
8924 If FORCE-TAGS is non nil, the car of it returns the new tags."
8925 (let* ((inhibit-read-only t)
8926 (line (org-current-line))
8927 (org-agenda-buffer (current-buffer))
8928 (thetags (with-current-buffer (marker-buffer hdmarker)
8929 (org-with-wide-buffer
8930 (goto-char hdmarker)
8931 (org-get-tags-at))))
8932 props m pl undone-face done-face finish new dotime level cat tags)
8933 (save-excursion
8934 (goto-char (point-max))
8935 (beginning-of-line 1)
8936 (while (not finish)
8937 (setq finish (bobp))
8938 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8939 (or (not just-this) (= (org-current-line) line))
8940 (equal m hdmarker))
8941 (setq props (text-properties-at (point))
8942 dotime (org-get-at-bol 'dotime)
8943 cat (org-get-at-eol 'org-category 1)
8944 level (org-get-at-bol 'level)
8945 tags thetags
8947 (let ((org-prefix-format-compiled
8948 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8949 org-prefix-format-compiled))
8950 (extra (org-get-at-bol 'extra)))
8951 (with-current-buffer (marker-buffer hdmarker)
8952 (org-with-wide-buffer
8953 (org-agenda-format-item extra newhead level cat tags dotime))))
8954 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8955 undone-face (org-get-at-bol 'undone-face)
8956 done-face (org-get-at-bol 'done-face))
8957 (beginning-of-line 1)
8958 (cond
8959 ((equal new "") (delete-region (point) (line-beginning-position 2)))
8960 ((looking-at ".*")
8961 ;; When replacing the whole line, preserve bulk mark
8962 ;; overlay, if any.
8963 (let ((mark (catch :overlay
8964 (dolist (o (overlays-in (point) (+ 2 (point))))
8965 (when (eq (overlay-get o 'type)
8966 'org-marked-entry-overlay)
8967 (throw :overlay o))))))
8968 (replace-match new t t)
8969 (beginning-of-line)
8970 (when mark (move-overlay mark (point) (+ 2 (point)))))
8971 (add-text-properties (point-at-bol) (point-at-eol) props)
8972 (when fixface
8973 (add-text-properties
8974 (point-at-bol) (point-at-eol)
8975 (list 'face
8976 (if org-last-todo-state-is-todo
8977 undone-face done-face))))
8978 (org-agenda-highlight-todo 'line)
8979 (beginning-of-line 1))
8980 (t (error "Line update did not work")))
8981 (save-restriction
8982 (narrow-to-region (point-at-bol) (point-at-eol))
8983 (org-agenda-finalize)))
8984 (beginning-of-line 0)))))
8986 (defun org-agenda-align-tags (&optional line)
8987 "Align all tags in agenda items to `org-agenda-tags-column'."
8988 (let ((inhibit-read-only t)
8989 (org-agenda-tags-column (if (eq 'auto org-agenda-tags-column)
8990 (- (window-text-width))
8991 org-agenda-tags-column))
8992 l c)
8993 (save-excursion
8994 (goto-char (if line (point-at-bol) (point-min)))
8995 (while (re-search-forward "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"
8996 (if line (point-at-eol) nil) t)
8997 (add-text-properties
8998 (match-beginning 2) (match-end 2)
8999 (list 'face (delq nil (let ((prop (get-text-property
9000 (match-beginning 2) 'face)))
9001 (or (listp prop) (setq prop (list prop)))
9002 (if (memq 'org-tag prop)
9003 prop
9004 (cons 'org-tag prop))))))
9005 (setq l (- (match-end 2) (match-beginning 2))
9006 c (if (< org-agenda-tags-column 0)
9007 (- (abs org-agenda-tags-column) l)
9008 org-agenda-tags-column))
9009 (delete-region (match-beginning 1) (match-end 1))
9010 (goto-char (match-beginning 1))
9011 (insert (org-add-props
9012 (make-string (max 1 (- c (current-column))) ?\ )
9013 (plist-put (copy-sequence (text-properties-at (point)))
9014 'face nil))))
9015 (goto-char (point-min))
9016 (org-font-lock-add-tag-faces (point-max)))))
9018 (defun org-agenda-priority-up ()
9019 "Increase the priority of line at point, also in Org file."
9020 (interactive)
9021 (org-agenda-priority 'up))
9023 (defun org-agenda-priority-down ()
9024 "Decrease the priority of line at point, also in Org file."
9025 (interactive)
9026 (org-agenda-priority 'down))
9028 (defun org-agenda-priority (&optional force-direction)
9029 "Set the priority of line at point, also in Org file.
9030 This changes the line at point, all other lines in the agenda referring to
9031 the same tree node, and the headline of the tree node in the Org file.
9032 Called with a universal prefix arg, show the priority instead of setting it."
9033 (interactive "P")
9034 (if (equal force-direction '(4))
9035 (org-show-priority)
9036 (unless org-enable-priority-commands
9037 (error "Priority commands are disabled"))
9038 (org-agenda-check-no-diary)
9039 (let* ((col (current-column))
9040 (marker (or (org-get-at-bol 'org-marker)
9041 (org-agenda-error)))
9042 (hdmarker (org-get-at-bol 'org-hd-marker))
9043 (buffer (marker-buffer hdmarker))
9044 (pos (marker-position hdmarker))
9045 (inhibit-read-only t)
9046 newhead)
9047 (org-with-remote-undo buffer
9048 (with-current-buffer buffer
9049 (widen)
9050 (goto-char pos)
9051 (org-show-context 'agenda)
9052 (funcall 'org-priority force-direction)
9053 (end-of-line 1)
9054 (setq newhead (org-get-heading)))
9055 (org-agenda-change-all-lines newhead hdmarker)
9056 (org-move-to-column col)))))
9058 ;; FIXME: should fix the tags property of the agenda line.
9059 (defun org-agenda-set-tags (&optional tag onoff)
9060 "Set tags for the current headline."
9061 (interactive)
9062 (org-agenda-check-no-diary)
9063 (if (and (org-region-active-p) (called-interactively-p 'any))
9064 (call-interactively 'org-change-tag-in-region)
9065 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9066 (org-agenda-error)))
9067 (buffer (marker-buffer hdmarker))
9068 (pos (marker-position hdmarker))
9069 (inhibit-read-only t)
9070 newhead)
9071 (org-with-remote-undo buffer
9072 (with-current-buffer buffer
9073 (widen)
9074 (goto-char pos)
9075 (org-show-context 'agenda)
9076 (if tag
9077 (org-toggle-tag tag onoff)
9078 (call-interactively 'org-set-tags))
9079 (end-of-line 1)
9080 (setq newhead (org-get-heading)))
9081 (org-agenda-change-all-lines newhead hdmarker)
9082 (beginning-of-line 1)))))
9084 (defun org-agenda-set-property ()
9085 "Set a property for the current headline."
9086 (interactive)
9087 (org-agenda-check-no-diary)
9088 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9089 (org-agenda-error)))
9090 (buffer (marker-buffer hdmarker))
9091 (pos (marker-position hdmarker))
9092 (inhibit-read-only t)
9093 newhead)
9094 (org-with-remote-undo buffer
9095 (with-current-buffer buffer
9096 (widen)
9097 (goto-char pos)
9098 (org-show-context 'agenda)
9099 (call-interactively 'org-set-property)))))
9101 (defun org-agenda-set-effort ()
9102 "Set the effort property for the current headline."
9103 (interactive)
9104 (org-agenda-check-no-diary)
9105 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9106 (org-agenda-error)))
9107 (buffer (marker-buffer hdmarker))
9108 (pos (marker-position hdmarker))
9109 (inhibit-read-only t)
9110 newhead)
9111 (org-with-remote-undo buffer
9112 (with-current-buffer buffer
9113 (widen)
9114 (goto-char pos)
9115 (org-show-context 'agenda)
9116 (call-interactively 'org-set-effort)
9117 (end-of-line 1)
9118 (setq newhead (org-get-heading)))
9119 (org-agenda-change-all-lines newhead hdmarker))))
9121 (defun org-agenda-toggle-archive-tag ()
9122 "Toggle the archive tag for the current entry."
9123 (interactive)
9124 (org-agenda-check-no-diary)
9125 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9126 (org-agenda-error)))
9127 (buffer (marker-buffer hdmarker))
9128 (pos (marker-position hdmarker))
9129 (inhibit-read-only t)
9130 newhead)
9131 (org-with-remote-undo buffer
9132 (with-current-buffer buffer
9133 (widen)
9134 (goto-char pos)
9135 (org-show-context 'agenda)
9136 (call-interactively 'org-toggle-archive-tag)
9137 (end-of-line 1)
9138 (setq newhead (org-get-heading)))
9139 (org-agenda-change-all-lines newhead hdmarker)
9140 (beginning-of-line 1))))
9142 (defun org-agenda-do-date-later (arg)
9143 (interactive "P")
9144 (cond
9145 ((or (equal arg '(16))
9146 (memq last-command
9147 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9148 (setq this-command 'org-agenda-date-later-minutes)
9149 (org-agenda-date-later-minutes 1))
9150 ((or (equal arg '(4))
9151 (memq last-command
9152 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9153 (setq this-command 'org-agenda-date-later-hours)
9154 (org-agenda-date-later-hours 1))
9156 (org-agenda-date-later (prefix-numeric-value arg)))))
9158 (defun org-agenda-do-date-earlier (arg)
9159 (interactive "P")
9160 (cond
9161 ((or (equal arg '(16))
9162 (memq last-command
9163 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9164 (setq this-command 'org-agenda-date-earlier-minutes)
9165 (org-agenda-date-earlier-minutes 1))
9166 ((or (equal arg '(4))
9167 (memq last-command
9168 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9169 (setq this-command 'org-agenda-date-earlier-hours)
9170 (org-agenda-date-earlier-hours 1))
9172 (org-agenda-date-earlier (prefix-numeric-value arg)))))
9174 (defun org-agenda-date-later (arg &optional what)
9175 "Change the date of this item to ARG day(s) later."
9176 (interactive "p")
9177 (org-agenda-check-type t 'agenda)
9178 (org-agenda-check-no-diary)
9179 (let* ((marker (or (org-get-at-bol 'org-marker)
9180 (org-agenda-error)))
9181 (buffer (marker-buffer marker))
9182 (pos (marker-position marker))
9183 cdate today)
9184 (org-with-remote-undo buffer
9185 (with-current-buffer buffer
9186 (widen)
9187 (goto-char pos)
9188 (unless (org-at-timestamp-p 'lax) (error "Cannot find time stamp"))
9189 (when (and org-agenda-move-date-from-past-immediately-to-today
9190 (equal arg 1)
9191 (or (not what) (eq what 'day))
9192 (not (save-match-data (org-at-date-range-p))))
9193 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9194 cdate (calendar-absolute-from-gregorian
9195 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9196 today (org-today))
9197 (if (> today cdate)
9198 ;; immediately shift to today
9199 (setq arg (- today cdate))))
9200 (org-timestamp-change arg (or what 'day))
9201 (when (and (org-at-date-range-p)
9202 (re-search-backward org-tr-regexp-both (point-at-bol)))
9203 (let ((end org-last-changed-timestamp))
9204 (org-timestamp-change arg (or what 'day))
9205 (setq org-last-changed-timestamp
9206 (concat org-last-changed-timestamp "--" end)))))
9207 (org-agenda-show-new-time marker org-last-changed-timestamp))
9208 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9210 (defun org-agenda-date-earlier (arg &optional what)
9211 "Change the date of this item to ARG day(s) earlier."
9212 (interactive "p")
9213 (org-agenda-date-later (- arg) what))
9215 (defun org-agenda-date-later-minutes (arg)
9216 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9217 (interactive "p")
9218 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9219 (org-agenda-date-later arg 'minute))
9221 (defun org-agenda-date-earlier-minutes (arg)
9222 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9223 (interactive "p")
9224 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9225 (org-agenda-date-earlier arg 'minute))
9227 (defun org-agenda-date-later-hours (arg)
9228 "Change the time of this item, in hour steps."
9229 (interactive "p")
9230 (org-agenda-date-later arg 'hour))
9232 (defun org-agenda-date-earlier-hours (arg)
9233 "Change the time of this item, in hour steps."
9234 (interactive "p")
9235 (org-agenda-date-earlier arg 'hour))
9237 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9238 "Show new date stamp via text properties."
9239 ;; We use text properties to make this undoable
9240 (let ((inhibit-read-only t))
9241 (setq stamp (concat prefix " => " stamp " "))
9242 (save-excursion
9243 (goto-char (point-max))
9244 (while (not (bobp))
9245 (when (equal marker (org-get-at-bol 'org-marker))
9246 (remove-text-properties (point-at-bol) (point-at-eol) '(display))
9247 (org-move-to-column (- (window-width) (length stamp)) t)
9248 (add-text-properties
9249 (1- (point)) (point-at-eol)
9250 (list 'display (org-add-props stamp nil
9251 'face '(secondary-selection default))))
9252 (beginning-of-line 1))
9253 (beginning-of-line 0)))))
9255 (defun org-agenda-date-prompt (arg)
9256 "Change the date of this item. Date is prompted for, with default today.
9257 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9258 be used to request time specification in the time stamp."
9259 (interactive "P")
9260 (org-agenda-check-type t 'agenda)
9261 (org-agenda-check-no-diary)
9262 (let* ((marker (or (org-get-at-bol 'org-marker)
9263 (org-agenda-error)))
9264 (buffer (marker-buffer marker))
9265 (pos (marker-position marker)))
9266 (org-with-remote-undo buffer
9267 (with-current-buffer buffer
9268 (widen)
9269 (goto-char pos)
9270 (unless (org-at-timestamp-p 'lax) (error "Cannot find time stamp"))
9271 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
9272 (org-agenda-show-new-time marker org-last-changed-timestamp))
9273 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9275 (defun org-agenda-schedule (arg &optional time)
9276 "Schedule the item at point.
9277 ARG is passed through to `org-schedule'."
9278 (interactive "P")
9279 (org-agenda-check-type t 'agenda 'todo 'tags 'search)
9280 (org-agenda-check-no-diary)
9281 (let* ((marker (or (org-get-at-bol 'org-marker)
9282 (org-agenda-error)))
9283 (type (marker-insertion-type marker))
9284 (buffer (marker-buffer marker))
9285 (pos (marker-position marker))
9287 (set-marker-insertion-type marker t)
9288 (org-with-remote-undo buffer
9289 (with-current-buffer buffer
9290 (widen)
9291 (goto-char pos)
9292 (setq ts (org-schedule arg time)))
9293 (org-agenda-show-new-time marker ts " S"))
9294 (message "%s" ts)))
9296 (defun org-agenda-deadline (arg &optional time)
9297 "Schedule the item at point.
9298 ARG is passed through to `org-deadline'."
9299 (interactive "P")
9300 (org-agenda-check-type t 'agenda 'todo 'tags 'search)
9301 (org-agenda-check-no-diary)
9302 (let* ((marker (or (org-get-at-bol 'org-marker)
9303 (org-agenda-error)))
9304 (buffer (marker-buffer marker))
9305 (pos (marker-position marker))
9307 (org-with-remote-undo buffer
9308 (with-current-buffer buffer
9309 (widen)
9310 (goto-char pos)
9311 (setq ts (org-deadline arg time)))
9312 (org-agenda-show-new-time marker ts " D"))
9313 (message "%s" ts)))
9315 (defun org-agenda-clock-in (&optional arg)
9316 "Start the clock on the currently selected item."
9317 (interactive "P")
9318 (org-agenda-check-no-diary)
9319 (if (equal arg '(4))
9320 (org-clock-in arg)
9321 (let* ((marker (or (org-get-at-bol 'org-marker)
9322 (org-agenda-error)))
9323 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9324 (pos (marker-position marker))
9325 (col (current-column))
9326 newhead)
9327 (org-with-remote-undo (marker-buffer marker)
9328 (with-current-buffer (marker-buffer marker)
9329 (widen)
9330 (goto-char pos)
9331 (org-show-context 'agenda)
9332 (org-cycle-hide-drawers 'children)
9333 (org-clock-in arg)
9334 (setq newhead (org-get-heading)))
9335 (org-agenda-change-all-lines newhead hdmarker))
9336 (org-move-to-column col))))
9338 (defun org-agenda-clock-out ()
9339 "Stop the currently running clock."
9340 (interactive)
9341 (unless (marker-buffer org-clock-marker)
9342 (error "No running clock"))
9343 (let ((marker (make-marker)) (col (current-column)) newhead)
9344 (org-with-remote-undo (marker-buffer org-clock-marker)
9345 (with-current-buffer (marker-buffer org-clock-marker)
9346 (org-with-wide-buffer
9347 (goto-char org-clock-marker)
9348 (org-back-to-heading t)
9349 (move-marker marker (point))
9350 (org-clock-out)
9351 (setq newhead (org-get-heading)))))
9352 (org-agenda-change-all-lines newhead marker)
9353 (move-marker marker nil)
9354 (org-move-to-column col)
9355 (org-agenda-unmark-clocking-task)))
9357 (defun org-agenda-clock-cancel (&optional arg)
9358 "Cancel the currently running clock."
9359 (interactive "P")
9360 (unless (marker-buffer org-clock-marker)
9361 (user-error "No running clock"))
9362 (org-with-remote-undo (marker-buffer org-clock-marker)
9363 (org-clock-cancel)))
9365 (defun org-agenda-clock-goto ()
9366 "Jump to the currently clocked in task within the agenda.
9367 If the currently clocked in task is not listed in the agenda
9368 buffer, display it in another window."
9369 (interactive)
9370 (let (pos)
9371 (mapc (lambda (o)
9372 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
9373 (setq pos (overlay-start o))))
9374 (overlays-in (point-min) (point-max)))
9375 (cond (pos (goto-char pos))
9376 ;; If the currently clocked entry is not in the agenda
9377 ;; buffer, we visit it in another window:
9378 ((bound-and-true-p org-clock-current-task)
9379 (org-switch-to-buffer-other-window (org-clock-goto)))
9380 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9382 (defun org-agenda-diary-entry-in-org-file ()
9383 "Make a diary entry in the file `org-agenda-diary-file'."
9384 (let (d1 d2 char (text "") dp1 dp2)
9385 (if (equal (buffer-name) "*Calendar*")
9386 (setq d1 (calendar-cursor-to-date t)
9387 d2 (car calendar-mark-ring))
9388 (setq dp1 (get-text-property (point-at-bol) 'day))
9389 (unless dp1 (user-error "No date defined in current line"))
9390 (setq d1 (calendar-gregorian-from-absolute dp1)
9391 d2 (and (ignore-errors (mark))
9392 (save-excursion
9393 (goto-char (mark))
9394 (setq dp2 (get-text-property (point-at-bol) 'day)))
9395 (calendar-gregorian-from-absolute dp2))))
9396 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9397 (setq char (read-char-exclusive))
9398 (cond
9399 ((equal char ?d)
9400 (setq text (read-string "Day entry: "))
9401 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9402 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9403 ((equal char ?a)
9404 (setq d1 (list (car d1) (nth 1 d1)
9405 (read-number (format "Reference year [%d]: " (nth 2 d1))
9406 (nth 2 d1))))
9407 (setq text (read-string "Anniversary (use %d to show years): "))
9408 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9409 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9410 ((equal char ?b)
9411 (setq text (read-string "Block entry: "))
9412 (unless (and d1 d2 (not (equal d1 d2)))
9413 (user-error "No block of days selected"))
9414 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9415 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9416 ((equal char ?j)
9417 (org-switch-to-buffer-other-window
9418 (find-file-noselect org-agenda-diary-file))
9419 (require 'org-datetree)
9420 (org-datetree-find-date-create d1)
9421 (org-reveal t))
9422 (t (user-error "Invalid selection character `%c'" char)))))
9424 (defcustom org-agenda-insert-diary-strategy 'date-tree
9425 "Where in `org-agenda-diary-file' should new entries be added?
9426 Valid values:
9428 date-tree in the date tree, as first child of the date
9429 date-tree-last in the date tree, as last child of the date
9430 top-level as top-level entries at the end of the file."
9431 :group 'org-agenda
9432 :type '(choice
9433 (const :tag "first in a date tree" date-tree)
9434 (const :tag "last in a date tree" date-tree-last)
9435 (const :tag "as top level at end of file" top-level)))
9437 (defcustom org-agenda-insert-diary-extract-time nil
9438 "Non-nil means extract any time specification from the diary entry."
9439 :group 'org-agenda
9440 :version "24.1"
9441 :type 'boolean)
9443 (defcustom org-agenda-bulk-mark-char ">"
9444 "A single-character string to be used as the bulk mark."
9445 :group 'org-agenda
9446 :version "24.1"
9447 :type 'string)
9449 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9450 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9451 If TEXT is not empty, it will become the headline of the new entry, and
9452 the resulting entry will not be shown. When TEXT is empty, switch to
9453 `org-agenda-diary-file' and let the user finish the entry there."
9454 (let ((cw (current-window-configuration)))
9455 (org-switch-to-buffer-other-window
9456 (find-file-noselect org-agenda-diary-file))
9457 (widen)
9458 (goto-char (point-min))
9459 (cond
9460 ((eq type 'anniversary)
9461 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
9462 (progn
9463 (or (org-at-heading-p t)
9464 (progn
9465 (outline-next-heading)
9466 (insert "* Anniversaries\n\n")
9467 (beginning-of-line -1)))))
9468 (outline-next-heading)
9469 (org-back-over-empty-lines)
9470 (backward-char 1)
9471 (insert "\n")
9472 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9473 (nth 2 d1) (car d1) (nth 1 d1) text)))
9474 ((eq type 'day)
9475 (let ((org-prefix-has-time t)
9476 (org-agenda-time-leading-zero t)
9477 fmt time time2)
9478 (if org-agenda-insert-diary-extract-time
9479 ;; Use org-agenda-format-item to parse text for a time-range and
9480 ;; remove it. FIXME: This is a hack, we should refactor
9481 ;; that function to make time extraction available separately
9482 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9483 time (get-text-property 0 'time fmt)
9484 time2 (if (> (length time) 0)
9485 ;; split-string removes trailing ...... if
9486 ;; no end time given. First space
9487 ;; separates time from date.
9488 (concat " " (car (split-string time "\\.")))
9489 nil)
9490 text (get-text-property 0 'txt fmt)))
9491 (if (eq org-agenda-insert-diary-strategy 'top-level)
9492 (org-agenda-insert-diary-as-top-level text)
9493 (require 'org-datetree)
9494 (org-datetree-find-date-create d1)
9495 (org-agenda-insert-diary-make-new-entry text))
9496 (org-insert-time-stamp (org-time-from-absolute
9497 (calendar-absolute-from-gregorian d1))
9498 nil nil nil nil time2))
9499 (end-of-line 0))
9500 ((eq type 'block)
9501 (if (> (calendar-absolute-from-gregorian d1)
9502 (calendar-absolute-from-gregorian d2))
9503 (setq d1 (prog1 d2 (setq d2 d1))))
9504 (if (eq org-agenda-insert-diary-strategy 'top-level)
9505 (org-agenda-insert-diary-as-top-level text)
9506 (require 'org-datetree)
9507 (org-datetree-find-date-create d1)
9508 (org-agenda-insert-diary-make-new-entry text))
9509 (org-insert-time-stamp (org-time-from-absolute
9510 (calendar-absolute-from-gregorian d1)))
9511 (insert "--")
9512 (org-insert-time-stamp (org-time-from-absolute
9513 (calendar-absolute-from-gregorian d2)))
9514 (end-of-line 0)))
9515 (if (string-match "\\S-" text)
9516 (progn
9517 (set-window-configuration cw)
9518 (message "%s entry added to %s"
9519 (capitalize (symbol-name type))
9520 (abbreviate-file-name org-agenda-diary-file)))
9521 (org-reveal t)
9522 (message "Please finish entry here"))))
9524 (defun org-agenda-insert-diary-as-top-level (text)
9525 "Make new entry as a top-level entry at the end of the file.
9526 Add TEXT as headline, and position the cursor in the second line so that
9527 a timestamp can be added there."
9528 (widen)
9529 (goto-char (point-max))
9530 (unless (bolp) (insert "\n"))
9531 (org-insert-heading nil t t)
9532 (insert text)
9533 (org-end-of-meta-data)
9534 (unless (bolp) (insert "\n"))
9535 (when org-adapt-indentation (indent-to-column 2)))
9537 (defun org-agenda-insert-diary-make-new-entry (text)
9538 "Make a new entry with TEXT as a child of the current subtree.
9539 Position the point in the heading's first body line so that
9540 a timestamp can be added there."
9541 (cond
9542 ((eq org-agenda-insert-diary-strategy 'date-tree-last)
9543 (end-of-line)
9544 (org-insert-heading '(4) t)
9545 (org-do-demote))
9547 (outline-next-heading)
9548 (org-back-over-empty-lines)
9549 (unless (looking-at "[ \t]*$") (save-excursion (insert "\n")))
9550 (org-insert-heading nil t)
9551 (org-do-demote)))
9552 (let ((col (current-column)))
9553 (insert text)
9554 (org-end-of-meta-data)
9555 ;; Ensure point is left on a blank line, at proper indentation.
9556 (unless (bolp) (insert "\n"))
9557 (unless (looking-at-p "^[ \t]*$") (save-excursion (insert "\n")))
9558 (when org-adapt-indentation (indent-to-column col)))
9559 (org-show-set-visibility 'lineage))
9561 (defun org-agenda-diary-entry ()
9562 "Make a diary entry, like the `i' command from the calendar.
9563 All the standard commands work: block, weekly etc.
9564 When `org-agenda-diary-file' points to a file,
9565 `org-agenda-diary-entry-in-org-file' is called instead to create
9566 entries in that Org file."
9567 (interactive)
9568 (if (not (eq org-agenda-diary-file 'diary-file))
9569 (org-agenda-diary-entry-in-org-file)
9570 (require 'diary-lib)
9571 (let* ((char (progn
9572 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
9573 (read-char-exclusive)))
9574 (cmd (cdr (assoc char
9575 '((?d . diary-insert-entry)
9576 (?w . diary-insert-weekly-entry)
9577 (?m . diary-insert-monthly-entry)
9578 (?y . diary-insert-yearly-entry)
9579 (?a . diary-insert-anniversary-entry)
9580 (?b . diary-insert-block-entry)
9581 (?c . diary-insert-cyclic-entry)))))
9582 (oldf (symbol-function 'calendar-cursor-to-date))
9583 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9584 (point (point))
9585 (mark (or (mark t) (point))))
9586 (unless cmd
9587 (user-error "No command associated with <%c>" char))
9588 (unless (and (get-text-property point 'day)
9589 (or (not (equal ?b char))
9590 (get-text-property mark 'day)))
9591 (user-error "Don't know which date to use for diary entry"))
9592 ;; We implement this by hacking the `calendar-cursor-to-date' function
9593 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9594 (let ((calendar-mark-ring
9595 (list (calendar-gregorian-from-absolute
9596 (or (get-text-property mark 'day)
9597 (get-text-property point 'day))))))
9598 (unwind-protect
9599 (progn
9600 (fset 'calendar-cursor-to-date
9601 (lambda (&optional error dummy)
9602 (calendar-gregorian-from-absolute
9603 (get-text-property point 'day))))
9604 (call-interactively cmd))
9605 (fset 'calendar-cursor-to-date oldf))))))
9607 (defun org-agenda-execute-calendar-command (cmd)
9608 "Execute a calendar command from the agenda with date from cursor."
9609 (org-agenda-check-type t 'agenda)
9610 (require 'diary-lib)
9611 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9612 (user-error "Don't know which date to use for the calendar command"))
9613 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9614 (point (point))
9615 (date (calendar-gregorian-from-absolute
9616 (get-text-property point 'day)))
9617 ;; the following 2 vars are needed in the calendar
9618 (displayed-month (car date))
9619 (displayed-year (nth 2 date)))
9620 (unwind-protect
9621 (progn
9622 (fset 'calendar-cursor-to-date
9623 (lambda (&optional error dummy)
9624 (calendar-gregorian-from-absolute
9625 (get-text-property point 'day))))
9626 (call-interactively cmd))
9627 (fset 'calendar-cursor-to-date oldf))))
9629 (defun org-agenda-phases-of-moon ()
9630 "Display the phases of the moon for the 3 months around the cursor date."
9631 (interactive)
9632 (org-agenda-execute-calendar-command 'calendar-lunar-phases))
9634 (defun org-agenda-holidays ()
9635 "Display the holidays for the 3 months around the cursor date."
9636 (interactive)
9637 (org-agenda-execute-calendar-command 'calendar-list-holidays))
9639 (defvar calendar-longitude) ; defined in calendar.el
9640 (defvar calendar-latitude) ; defined in calendar.el
9641 (defvar calendar-location-name) ; defined in calendar.el
9643 (defun org-agenda-sunrise-sunset (arg)
9644 "Display sunrise and sunset for the cursor date.
9645 Latitude and longitude can be specified with the variables
9646 `calendar-latitude' and `calendar-longitude'. When called with prefix
9647 argument, latitude and longitude will be prompted for."
9648 (interactive "P")
9649 (require 'solar)
9650 (let ((calendar-longitude (if arg nil calendar-longitude))
9651 (calendar-latitude (if arg nil calendar-latitude))
9652 (calendar-location-name
9653 (if arg "the given coordinates" calendar-location-name)))
9654 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9656 (defun org-agenda-goto-calendar ()
9657 "Open the Emacs calendar with the date at the cursor."
9658 (interactive)
9659 (org-agenda-check-type t 'agenda)
9660 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9661 (user-error "Don't know which date to open in calendar")))
9662 (date (calendar-gregorian-from-absolute day))
9663 (calendar-move-hook nil)
9664 (calendar-view-holidays-initially-flag nil)
9665 (calendar-view-diary-initially-flag nil))
9666 (calendar)
9667 (calendar-goto-date date)))
9669 ;;;###autoload
9670 (defun org-calendar-goto-agenda ()
9671 "Compute the Org agenda for the calendar date displayed at the cursor.
9672 This is a command that has to be installed in `calendar-mode-map'."
9673 (interactive)
9674 ;; Temporarily disable sticky agenda since user clearly wants to
9675 ;; refresh view anyway.
9676 (let ((org-agenda-buffer-tmp-name "*Org Agenda(a)*")
9677 (org-agenda-sticky nil))
9678 (org-agenda-list nil (calendar-absolute-from-gregorian
9679 (calendar-cursor-to-date))
9680 nil)))
9682 (defun org-agenda-convert-date ()
9683 (interactive)
9684 (org-agenda-check-type t 'agenda)
9685 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9686 date s)
9687 (unless day
9688 (user-error "Don't know which date to convert"))
9689 (setq date (calendar-gregorian-from-absolute day))
9690 (setq s (concat
9691 "Gregorian: " (calendar-date-string date) "\n"
9692 "ISO: " (calendar-iso-date-string date) "\n"
9693 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9694 "Julian: " (calendar-julian-date-string date) "\n"
9695 "Astron. JD: " (calendar-astro-date-string date)
9696 " (Julian date number at noon UTC)\n"
9697 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9698 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9699 "French: " (calendar-french-date-string date) "\n"
9700 "Bahá’í: " (calendar-bahai-date-string date) " (until sunset)\n"
9701 "Mayan: " (calendar-mayan-date-string date) "\n"
9702 "Coptic: " (calendar-coptic-date-string date) "\n"
9703 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9704 "Persian: " (calendar-persian-date-string date) "\n"
9705 "Chinese: " (calendar-chinese-date-string date) "\n"))
9706 (with-output-to-temp-buffer "*Dates*"
9707 (princ s))
9708 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9710 ;;; Bulk commands
9712 (defun org-agenda-bulk-marked-p ()
9713 "Non-nil when current entry is marked for bulk action."
9714 (eq (get-char-property (point-at-bol) 'type)
9715 'org-marked-entry-overlay))
9717 (defun org-agenda-bulk-mark (&optional arg)
9718 "Mark entries for future bulk action.
9720 When ARG is nil or one and region is not active then mark the
9721 entry at point.
9723 When ARG is nil or one and region is active then mark the entries
9724 in the region.
9726 When ARG is greater than one mark ARG lines."
9727 (interactive "p")
9728 (when (and (or (not arg) (= arg 1)) (use-region-p))
9729 (setq arg (count-lines (region-beginning) (region-end)))
9730 (goto-char (region-beginning))
9731 (deactivate-mark))
9732 (dotimes (i (or arg 1))
9733 (unless (org-get-at-bol 'org-agenda-diary-link)
9734 (let* ((m (org-get-at-bol 'org-hd-marker))
9736 (unless (org-agenda-bulk-marked-p)
9737 (unless m (user-error "Nothing to mark at point"))
9738 (push m org-agenda-bulk-marked-entries)
9739 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9740 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9741 (org-get-todo-face "TODO")
9742 'evaporate)
9743 (overlay-put ov 'type 'org-marked-entry-overlay))
9744 (end-of-line 1)
9745 (or (ignore-errors
9746 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9747 (beginning-of-line 2))
9748 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9749 (beginning-of-line 2))
9750 (message "%d entries marked for bulk action"
9751 (length org-agenda-bulk-marked-entries))))))
9753 (defun org-agenda-bulk-mark-all ()
9754 "Mark all entries for future agenda bulk action."
9755 (interactive)
9756 (org-agenda-bulk-mark-regexp "."))
9758 (defun org-agenda-bulk-mark-regexp (regexp)
9759 "Mark entries matching REGEXP for future agenda bulk action."
9760 (interactive "sMark entries matching regexp: ")
9761 (let ((entries-marked 0) txt-at-point)
9762 (save-excursion
9763 (goto-char (point-min))
9764 (goto-char (next-single-property-change (point) 'org-hd-marker))
9765 (while (and (re-search-forward regexp nil t)
9766 (setq txt-at-point (get-text-property (point) 'txt)))
9767 (if (get-char-property (point) 'invisible)
9768 (beginning-of-line 2)
9769 (when (string-match regexp txt-at-point)
9770 (setq entries-marked (1+ entries-marked))
9771 (call-interactively 'org-agenda-bulk-mark)))))
9773 (if (not entries-marked)
9774 (message "No entry matching this regexp."))))
9776 (defun org-agenda-bulk-unmark (&optional arg)
9777 "Unmark the entry at point for future bulk action."
9778 (interactive "P")
9779 (if arg
9780 (org-agenda-bulk-unmark-all)
9781 (cond ((org-agenda-bulk-marked-p)
9782 (org-agenda-bulk-remove-overlays
9783 (point-at-bol) (+ 2 (point-at-bol)))
9784 (setq org-agenda-bulk-marked-entries
9785 (delete (org-get-at-bol 'org-hd-marker)
9786 org-agenda-bulk-marked-entries))
9787 (end-of-line 1)
9788 (or (ignore-errors
9789 (goto-char (next-single-property-change (point) 'txt)))
9790 (beginning-of-line 2))
9791 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9792 (beginning-of-line 2))
9793 (message "%d entries left marked for bulk action"
9794 (length org-agenda-bulk-marked-entries)))
9795 (t (message "No entry to unmark here")))))
9797 (defun org-agenda-bulk-toggle-all ()
9798 "Toggle all marks for bulk action."
9799 (interactive)
9800 (save-excursion
9801 (goto-char (point-min))
9802 (while (ignore-errors
9803 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9804 (org-agenda-bulk-toggle))))
9806 (defun org-agenda-bulk-toggle ()
9807 "Toggle the mark at point for bulk action."
9808 (interactive)
9809 (if (org-agenda-bulk-marked-p)
9810 (org-agenda-bulk-unmark)
9811 (org-agenda-bulk-mark)))
9813 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9814 "Remove the mark overlays between BEG and END in the agenda buffer.
9815 BEG and END default to the buffer limits.
9817 This only removes the overlays, it does not remove the markers
9818 from the list in `org-agenda-bulk-marked-entries'."
9819 (interactive)
9820 (mapc (lambda (ov)
9821 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9822 (delete-overlay ov)))
9823 (overlays-in (or beg (point-min)) (or end (point-max)))))
9825 (defun org-agenda-bulk-unmark-all ()
9826 "Remove all marks in the agenda buffer.
9827 This will remove the markers and the overlays."
9828 (interactive)
9829 (if (null org-agenda-bulk-marked-entries)
9830 (message "No entry to unmark")
9831 (setq org-agenda-bulk-marked-entries nil)
9832 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9834 (defcustom org-agenda-persistent-marks nil
9835 "Non-nil means marked items will stay marked after a bulk action.
9836 You can toggle this interactively by typing `p' when prompted for a
9837 bulk action."
9838 :group 'org-agenda
9839 :version "24.1"
9840 :type 'boolean)
9842 (defun org-agenda-bulk-action (&optional arg)
9843 "Execute an remote-editing action on all marked entries.
9844 The prefix arg is passed through to the command if possible."
9845 (interactive "P")
9846 ;; Make sure we have markers, and only valid ones.
9847 (unless org-agenda-bulk-marked-entries (user-error "No entries are marked"))
9848 (dolist (m org-agenda-bulk-marked-entries)
9849 (unless (and (markerp m)
9850 (marker-buffer m)
9851 (buffer-live-p (marker-buffer m))
9852 (marker-position m))
9853 (user-error "Marker %s for bulk command is invalid" m)))
9855 ;; Prompt for the bulk command.
9856 (message
9857 (concat (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")
9858 "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9859 "[S]catter [f]unction "
9860 (and org-agenda-bulk-custom-functions
9861 (format " Custom: [%s]"
9862 (mapconcat (lambda (f) (char-to-string (car f)))
9863 org-agenda-bulk-custom-functions
9864 "")))))
9865 (catch 'exit
9866 (let* ((org-log-refile (if org-log-refile 'time nil))
9867 (entries (reverse org-agenda-bulk-marked-entries))
9868 (org-overriding-default-time
9869 (and (get-text-property (point) 'org-agenda-date-header)
9870 (org-get-cursor-date)))
9871 redo-at-end
9872 cmd)
9873 (pcase (read-char-exclusive)
9875 (let ((org-agenda-persistent-marks
9876 (not org-agenda-persistent-marks)))
9877 (org-agenda-bulk-action)
9878 (throw 'exit nil)))
9881 (setq cmd #'org-agenda-archive))
9884 (setq cmd #'org-agenda-archive-to-archive-sibling))
9886 ((or ?r ?w)
9887 (let ((refile-location
9888 (org-refile-get-location
9889 "Refile to"
9890 (marker-buffer (car entries))
9891 org-refile-allow-creating-parent-nodes)))
9892 (when (nth 3 refile-location)
9893 (setcar (nthcdr 3 refile-location)
9894 (move-marker
9895 (make-marker)
9896 (nth 3 refile-location)
9897 (or (get-file-buffer (nth 1 refile-location))
9898 (find-buffer-visiting (nth 1 refile-location))
9899 (error "This should not happen")))))
9901 (setq cmd `(lambda () (org-agenda-refile nil ',refile-location t)))
9902 (setq redo-at-end t)))
9905 (let ((state (completing-read
9906 "Todo state: "
9907 (with-current-buffer (marker-buffer (car entries))
9908 (mapcar #'list org-todo-keywords-1)))))
9909 (setq cmd `(lambda ()
9910 (let ((org-inhibit-blocking t)
9911 (org-inhibit-logging 'note))
9912 (org-agenda-todo ,state))))))
9914 ((and (or ?- ?+) action)
9915 (let ((tag (completing-read
9916 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9917 (with-current-buffer (marker-buffer (car entries))
9918 (delq nil
9919 (mapcar (lambda (x) (and (stringp (car x)) x))
9920 org-current-tag-alist))))))
9921 (setq cmd
9922 `(lambda ()
9923 (org-agenda-set-tags ,tag
9924 ,(if (eq action ?+) ''on ''off))))))
9927 (let ((time
9928 (and (not arg)
9929 (org-read-date nil nil nil "(Re)Schedule to"
9930 org-overriding-default-time))))
9931 ;; Make sure to not prompt for a note when bulk
9932 ;; rescheduling as Org cannot cope with simultaneous notes.
9933 ;; Besides, it could be annoying depending on the number of
9934 ;; items re-scheduled.
9935 (setq cmd
9936 `(lambda ()
9937 (let ((org-log-reschedule (and org-log-reschedule 'time)))
9938 (org-agenda-schedule arg ,time))))))
9940 (let ((time
9941 (and (not arg)
9942 (org-read-date nil nil nil "(Re)Set Deadline to"
9943 org-overriding-default-time))))
9944 ;; Make sure to not prompt for a note when bulk
9945 ;; rescheduling as Org cannot cope with simultaneous
9946 ;; notes. Besides, it could be annoying depending on the
9947 ;; number of items re-scheduled.
9948 (setq cmd
9949 `(lambda ()
9950 (let ((org-log-redeadline (and org-log-redeadline 'time)))
9951 (org-agenda-deadline arg ,time))))))
9954 (unless (org-agenda-check-type nil 'agenda 'todo)
9955 (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type))
9956 (let ((days (read-number
9957 (format "Scatter tasks across how many %sdays: "
9958 (if arg "week" ""))
9959 7)))
9960 (setq cmd
9961 `(lambda ()
9962 (let ((distance (1+ (random ,days))))
9963 (when arg
9964 (let ((dist distance)
9965 (day-of-week
9966 (calendar-day-of-week
9967 (calendar-gregorian-from-absolute (org-today)))))
9968 (dotimes (i (1+ dist))
9969 (while (member day-of-week org-agenda-weekend-days)
9970 (cl-incf distance)
9971 (cl-incf day-of-week)
9972 (when (= day-of-week 7)
9973 (setq day-of-week 0)))
9974 (cl-incf day-of-week)
9975 (when (= day-of-week 7)
9976 (setq day-of-week 0)))))
9977 ;; Silently fail when try to replan a sexp entry.
9978 (ignore-errors
9979 (let* ((date (calendar-gregorian-from-absolute
9980 (+ (org-today) distance)))
9981 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9982 (nth 2 date))))
9983 (org-agenda-schedule nil time))))))))
9986 (setq cmd
9987 (intern
9988 (completing-read "Function: " obarray #'fboundp t nil nil))))
9990 (action
9991 (pcase (assoc action org-agenda-bulk-custom-functions)
9992 (`(,_ ,f) (setq cmd f) (setq redo-at-end t))
9993 (_ (user-error "Invalid bulk action: %c" action)))))
9995 ;; Sort the markers, to make sure that parents are handled
9996 ;; before children.
9997 (setq entries (sort entries
9998 (lambda (a b)
9999 (cond
10000 ((eq (marker-buffer a) (marker-buffer b))
10001 (< (marker-position a) (marker-position b)))
10003 (string< (buffer-name (marker-buffer a))
10004 (buffer-name (marker-buffer b))))))))
10006 ;; Now loop over all markers and apply CMD.
10007 (let ((processed 0)
10008 (skipped 0))
10009 (dolist (e entries)
10010 (let ((pos (text-property-any (point-min) (point-max) 'org-hd-marker e)))
10011 (if (not pos)
10012 (progn (message "Skipping removed entry at %s" e)
10013 (cl-incf skipped))
10014 (goto-char pos)
10015 (let (org-loop-over-headlines-in-active-region) (funcall cmd))
10016 ;; `post-command-hook' is not run yet. We make sure any
10017 ;; pending log note is processed.
10018 (when (or (memq 'org-add-log-note (default-value 'post-command-hook))
10019 (memq 'org-add-log-note post-command-hook))
10020 (org-add-log-note))
10021 (cl-incf processed))))
10022 (when redo-at-end (org-agenda-redo))
10023 (unless org-agenda-persistent-marks (org-agenda-bulk-unmark-all))
10024 (message "Acted on %d entries%s%s"
10025 processed
10026 (if (= skipped 0)
10028 (format ", skipped %d (disappeared before their turn)"
10029 skipped))
10030 (if (not org-agenda-persistent-marks) "" " (kept marked)"))))))
10032 (defun org-agenda-capture (&optional with-time)
10033 "Call `org-capture' with the date at point.
10034 With a `C-1' prefix, use the HH:MM value at point (if any) or the
10035 current HH:MM time."
10036 (interactive "P")
10037 (if (not (eq major-mode 'org-agenda-mode))
10038 (user-error "You cannot do this outside of agenda buffers")
10039 (let ((org-overriding-default-time
10040 (org-get-cursor-date (equal with-time 1))))
10041 (call-interactively 'org-capture))))
10043 ;;; Dragging agenda lines forward/backward
10045 (defun org-agenda-reapply-filters ()
10046 "Re-apply all agenda filters."
10047 (mapcar
10048 (lambda(f) (when (car f) (org-agenda-filter-apply (car f) (cadr f) t)))
10049 `((,org-agenda-tag-filter tag)
10050 (,org-agenda-category-filter category)
10051 (,org-agenda-regexp-filter regexp)
10052 (,org-agenda-effort-filter effort)
10053 (,(get 'org-agenda-tag-filter :preset-filter) tag)
10054 (,(get 'org-agenda-category-filter :preset-filter) category)
10055 (,(get 'org-agenda-effort-filter :preset-filter) effort)
10056 (,(get 'org-agenda-regexp-filter :preset-filter) regexp))))
10058 (defun org-agenda-drag-line-forward (arg &optional backward)
10059 "Drag an agenda line forward by ARG lines.
10060 When the optional argument `backward' is non-nil, move backward."
10061 (interactive "p")
10062 (let ((inhibit-read-only t) lst line)
10063 (if (or (not (get-text-property (point) 'txt))
10064 (save-excursion
10065 (dotimes (n arg)
10066 (move-beginning-of-line (if backward 0 2))
10067 (push (not (get-text-property (point) 'txt)) lst))
10068 (delq nil lst)))
10069 (message "Cannot move line forward")
10070 (let ((end (save-excursion (move-beginning-of-line 2) (point))))
10071 (move-beginning-of-line 1)
10072 (setq line (buffer-substring (point) end))
10073 (delete-region (point) end)
10074 (move-beginning-of-line (funcall (if backward '1- '1+) arg))
10075 (insert line)
10076 (org-agenda-reapply-filters)
10077 (org-agenda-mark-clocking-task)
10078 (move-beginning-of-line 0)))))
10080 (defun org-agenda-drag-line-backward (arg)
10081 "Drag an agenda line backward by ARG lines."
10082 (interactive "p")
10083 (org-agenda-drag-line-forward arg t))
10085 ;;; Flagging notes
10087 (defun org-agenda-show-the-flagging-note ()
10088 "Display the flagging note in the other window.
10089 When called a second time in direct sequence, offer to remove the FLAGGING
10090 tag and (if present) the flagging note."
10091 (interactive)
10092 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
10093 (win (selected-window))
10094 note heading newhead)
10095 (unless hdmarker
10096 (user-error "No linked entry at point"))
10097 (if (and (eq this-command last-command)
10098 (y-or-n-p "Unflag and remove any flagging note? "))
10099 (progn
10100 (org-agenda-remove-flag hdmarker)
10101 (let ((win (get-buffer-window "*Flagging Note*")))
10102 (and win (delete-window win)))
10103 (message "Entry unflagged"))
10104 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
10105 (unless note
10106 (user-error "No flagging note"))
10107 (org-kill-new note)
10108 (org-switch-to-buffer-other-window "*Flagging Note*")
10109 (erase-buffer)
10110 (insert note)
10111 (goto-char (point-min))
10112 (while (re-search-forward "\\\\n" nil t)
10113 (replace-match "\n" t t))
10114 (goto-char (point-min))
10115 (select-window win)
10116 (message "%s" (substitute-command-keys "Flagging note pushed to \
10117 kill ring. Press `\\[org-agenda-show-the-flagging-note]' again to remove \
10118 tag and note")))))
10120 (defun org-agenda-remove-flag (marker)
10121 "Remove the FLAGGED tag and any flagging note in the entry."
10122 (let (newhead)
10123 (org-with-point-at marker
10124 (org-toggle-tag "FLAGGED" 'off)
10125 (org-entry-delete nil "THEFLAGGINGNOTE")
10126 (setq newhead (org-get-heading)))
10127 (org-agenda-change-all-lines newhead marker)
10128 (message "Entry unflagged")))
10130 (defun org-agenda-get-any-marker (&optional pos)
10131 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
10132 (get-text-property (or pos (point-at-bol)) 'org-marker)))
10134 ;;; Appointment reminders
10136 (defvar appt-time-msg-list) ; defined in appt.el
10138 ;;;###autoload
10139 (defun org-agenda-to-appt (&optional refresh filter &rest args)
10140 "Activate appointments found in `org-agenda-files'.
10142 With a `\\[universal-argument]' prefix, refresh the list of \
10143 appointments.
10145 If FILTER is t, interactively prompt the user for a regular
10146 expression, and filter out entries that don't match it.
10148 If FILTER is a string, use this string as a regular expression
10149 for filtering entries out.
10151 If FILTER is a function, filter out entries against which
10152 calling the function returns nil. This function takes one
10153 argument: an entry from `org-agenda-get-day-entries'.
10155 FILTER can also be an alist with the car of each cell being
10156 either `headline' or `category'. For example:
10158 \\='((headline \"IMPORTANT\")
10159 (category \"Work\"))
10161 will only add headlines containing IMPORTANT or headlines
10162 belonging to the \"Work\" category.
10164 ARGS are symbols indicating what kind of entries to consider.
10165 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
10166 \(i.e., deadlines and scheduled items with a hh:mm specification)
10167 and :timestamp entries. See the docstring of `org-diary' for
10168 details and examples.
10170 If an entry has a APPT_WARNTIME property, its value will be used
10171 to override `appt-message-warning-time'."
10172 (interactive "P")
10173 (if refresh (setq appt-time-msg-list nil))
10174 (if (eq filter t)
10175 (setq filter (read-from-minibuffer "Regexp filter: ")))
10176 (let* ((cnt 0) ; count added events
10177 (scope (or args '(:deadline* :scheduled* :timestamp)))
10178 (org-agenda-new-buffers nil)
10179 (org-deadline-warning-days 0)
10180 ;; Do not use `org-today' here because appt only takes
10181 ;; time and without date as argument, so it may pass wrong
10182 ;; information otherwise
10183 (today (org-date-to-gregorian
10184 (time-to-days (current-time))))
10185 (org-agenda-restrict nil)
10186 (files (org-agenda-files 'unrestricted)) entries file
10187 (org-agenda-buffer nil))
10188 ;; Get all entries which may contain an appt
10189 (org-agenda-prepare-buffers files)
10190 (while (setq file (pop files))
10191 (setq entries
10192 (delq nil
10193 (append entries
10194 (apply 'org-agenda-get-day-entries
10195 file today scope)))))
10196 ;; Map thru entries and find if we should filter them out
10197 (mapc
10198 (lambda (x)
10199 (let* ((evt (org-trim
10200 (replace-regexp-in-string
10201 org-bracket-link-regexp "\\3"
10202 (or (get-text-property 1 'txt x) ""))))
10203 (cat (get-text-property (1- (length x)) 'org-category x))
10204 (tod (get-text-property 1 'time-of-day x))
10205 (ok (or (null filter)
10206 (and (stringp filter) (string-match filter evt))
10207 (and (functionp filter) (funcall filter x))
10208 (and (listp filter)
10209 (let ((cat-filter (cadr (assq 'category filter)))
10210 (evt-filter (cadr (assq 'headline filter))))
10211 (or (and (stringp cat-filter)
10212 (string-match cat-filter cat))
10213 (and (stringp evt-filter)
10214 (string-match evt-filter evt)))))))
10215 (wrn (get-text-property 1 'warntime x)))
10216 ;; FIXME: Shall we remove text-properties for the appt text?
10217 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
10218 (when (and ok tod (not (string-match "\\`DONE\\|CANCELLED" evt)))
10219 (setq tod (concat "00" (number-to-string tod)))
10220 (setq tod (when (string-match
10221 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
10222 (concat (match-string 1 tod) ":"
10223 (match-string 2 tod))))
10224 (when (appt-add tod evt wrn)
10225 (setq cnt (1+ cnt))))))
10226 entries)
10227 (org-release-buffers org-agenda-new-buffers)
10228 (if (eq cnt 0)
10229 (message "No event to add")
10230 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
10232 (defun org-agenda-today-p (date)
10233 "Non nil when DATE means today.
10234 DATE is either a list of the form (month day year) or a number of
10235 days as returned by `calendar-absolute-from-gregorian' or
10236 `org-today'. This function considers `org-extend-today-until'
10237 when defining today."
10238 (eq (org-today)
10239 (if (consp date) (calendar-absolute-from-gregorian date) date)))
10241 (defun org-agenda-todo-yesterday (&optional arg)
10242 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
10243 (interactive "P")
10244 (let* ((org-use-effective-time t)
10245 (hour (nth 2 (decode-time (org-current-time))))
10246 (org-extend-today-until (1+ hour)))
10247 (org-agenda-todo arg)))
10249 (provide 'org-agenda)
10251 ;;; org-agenda.el ends here