Merge branch 'maint'
[org-mode.git] / lisp / org-agenda.el
blob2fd33b38ee87f8795db46a54c1f3a6561e566125
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 <http://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'. `:deadline' must appear before
281 `:scheduled' if the setting of
282 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
283 any effect.
285 :deadline* Same as above, but only include the deadline if it has an
286 hour specification as [h]h:mm.
288 :scheduled List all items which are scheduled for the given date.
289 The diary for *today* also contains items which were
290 scheduled earlier and are not yet marked DONE.
292 :scheduled* Same as above, but only include the scheduled item if it
293 has an hour specification as [h]h:mm.
295 By default, all four non-starred types are turned on.
297 When :scheduled* or :deadline* are included, :schedule or :deadline
298 will be ignored.
300 Never set this variable globally using `setq', because then it
301 will apply to all future agenda commands. Instead, bind it with
302 `let' to scope it dynamically into the agenda-constructing
303 command. A good way to set it is through options in
304 `org-agenda-custom-commands'. For a more flexible (though
305 somewhat less efficient) way of determining what is included in
306 the daily/weekly agenda, see `org-agenda-skip-function'.")
308 (defconst org-agenda-custom-commands-local-options
309 `(repeat :tag "Local settings for this command. Remember to quote values"
310 (choice :tag "Setting"
311 (list :tag "Heading for this block"
312 (const org-agenda-overriding-header)
313 (string :tag "Headline"))
314 (list :tag "Files to be searched"
315 (const org-agenda-files)
316 (list
317 (const :format "" quote)
318 (repeat (file))))
319 (list :tag "Sorting strategy"
320 (const org-agenda-sorting-strategy)
321 (list
322 (const :format "" quote)
323 (repeat
324 ,org-sorting-choice)))
325 (list :tag "Prefix format"
326 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
327 (string))
328 (list :tag "Number of days in agenda"
329 (const org-agenda-span)
330 (choice (const :tag "Day" day)
331 (const :tag "Week" week)
332 (const :tag "Fortnight" fortnight)
333 (const :tag "Month" month)
334 (const :tag "Year" year)
335 (integer :tag "Custom")))
336 (list :tag "Fixed starting date"
337 (const org-agenda-start-day)
338 (string :value "2007-11-01"))
339 (list :tag "Start on day of week"
340 (const org-agenda-start-on-weekday)
341 (choice :value 1
342 (const :tag "Today" nil)
343 (integer :tag "Weekday No.")))
344 (list :tag "Include data from diary"
345 (const org-agenda-include-diary)
346 (boolean))
347 (list :tag "Deadline Warning days"
348 (const org-deadline-warning-days)
349 (integer :value 1))
350 (list :tag "Category filter preset"
351 (const org-agenda-category-filter-preset)
352 (list
353 (const :format "" quote)
354 (repeat
355 (string :tag "+category or -category"))))
356 (list :tag "Tags filter preset"
357 (const org-agenda-tag-filter-preset)
358 (list
359 (const :format "" quote)
360 (repeat
361 (string :tag "+tag or -tag"))))
362 (list :tag "Effort filter preset"
363 (const org-agenda-effort-filter-preset)
364 (list
365 (const :format "" quote)
366 (repeat
367 (string :tag "+=10 or -=10 or +<10 or ->10"))))
368 (list :tag "Regexp filter preset"
369 (const org-agenda-regexp-filter-preset)
370 (list
371 (const :format "" quote)
372 (repeat
373 (string :tag "+regexp or -regexp"))))
374 (list :tag "Set daily/weekly entry types"
375 (const org-agenda-entry-types)
376 (list
377 (const :format "" quote)
378 (set :greedy t :value ,org-agenda-entry-types
379 (const :deadline)
380 (const :scheduled)
381 (const :deadline*)
382 (const :scheduled*)
383 (const :timestamp)
384 (const :sexp))))
385 (list :tag "Standard skipping condition"
386 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
387 (const org-agenda-skip-function)
388 (list
389 (const :format "" quote)
390 (list
391 (choice
392 :tag "Skipping range"
393 (const :tag "Skip entry" org-agenda-skip-entry-if)
394 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
395 (repeat :inline t :tag "Conditions for skipping"
396 (choice
397 :tag "Condition type"
398 (list :tag "Regexp matches" :inline t
399 (const :format "" 'regexp)
400 (regexp))
401 (list :tag "Regexp does not match" :inline t
402 (const :format "" 'notregexp)
403 (regexp))
404 (list :tag "TODO state is" :inline t
405 (const 'todo)
406 (choice
407 (const :tag "Any not-done state" 'todo)
408 (const :tag "Any done state" 'done)
409 (const :tag "Any state" 'any)
410 (list :tag "Keyword list"
411 (const :format "" quote)
412 (repeat (string :tag "Keyword")))))
413 (list :tag "TODO state is not" :inline t
414 (const 'nottodo)
415 (choice
416 (const :tag "Any not-done state" 'todo)
417 (const :tag "Any done state" 'done)
418 (const :tag "Any state" 'any)
419 (list :tag "Keyword list"
420 (const :format "" quote)
421 (repeat (string :tag "Keyword")))))
422 (const :tag "scheduled" 'scheduled)
423 (const :tag "not scheduled" 'notscheduled)
424 (const :tag "deadline" 'deadline)
425 (const :tag "no deadline" 'notdeadline)
426 (const :tag "timestamp" 'timestamp)
427 (const :tag "no timestamp" 'nottimestamp))))))
428 (list :tag "Non-standard skipping condition"
429 :value (org-agenda-skip-function)
430 (const org-agenda-skip-function)
431 (sexp :tag "Function or form (quoted!)"))
432 (list :tag "Any variable"
433 (variable :tag "Variable")
434 (sexp :tag "Value (sexp)"))))
435 "Selection of examples for agenda command settings.
436 This will be spliced into the custom type of
437 `org-agenda-custom-commands'.")
440 (defcustom org-agenda-custom-commands
441 '(("n" "Agenda and all TODOs" ((agenda "") (alltodo ""))))
442 "Custom commands for the agenda.
443 \\<org-mode-map>
444 These commands will be offered on the splash screen displayed by the
445 agenda dispatcher `\\[org-agenda]'. Each entry is a list like this:
447 (key desc type match settings files)
449 key The key (one or more characters as a string) to be associated
450 with the command.
451 desc A description of the command, when omitted or nil, a default
452 description is built using MATCH.
453 type The command type, any of the following symbols:
454 agenda The daily/weekly agenda.
455 todo Entries with a specific TODO keyword, in all agenda files.
456 search Entries containing search words entry or headline.
457 tags Tags/Property/TODO match in all agenda files.
458 tags-todo Tags/P/T match in all agenda files, TODO entries only.
459 todo-tree Sparse tree of specific TODO keyword in *current* file.
460 tags-tree Sparse tree with all tags matches in *current* file.
461 occur-tree Occur sparse tree for *current* file.
462 ... A user-defined function.
463 match What to search for:
464 - a single keyword for TODO keyword searches
465 - a tags match expression for tags searches
466 - a word search expression for text searches.
467 - a regular expression for occur searches
468 For all other commands, this should be the empty string.
469 settings A list of option settings, similar to that in a let form, so like
470 this: ((opt1 val1) (opt2 val2) ...). The values will be
471 evaluated at the moment of execution, so quote them when needed.
472 files A list of files to write the produced agenda buffer to with
473 the command `org-store-agenda-views'.
474 If a file name ends in \".html\", an HTML version of the buffer
475 is written out. If it ends in \".ps\", a postscript version is
476 produced. Otherwise, only the plain text is written to the file.
478 You can also define a set of commands, to create a composite agenda buffer.
479 In this case, an entry looks like this:
481 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
483 where
485 desc A description string to be displayed in the dispatcher menu.
486 cmd An agenda command, similar to the above. However, tree commands
487 are not allowed, but instead you can get agenda and global todo list.
488 So valid commands for a set are:
489 (agenda \"\" settings)
490 (alltodo \"\" settings)
491 (stuck \"\" settings)
492 (todo \"match\" settings files)
493 (search \"match\" settings files)
494 (tags \"match\" settings files)
495 (tags-todo \"match\" settings files)
497 Each command can carry a list of options, and another set of options can be
498 given for the whole set of commands. Individual command options take
499 precedence over the general options.
501 When using several characters as key to a command, the first characters
502 are prefix commands. For the dispatcher to display useful information, you
503 should provide a description for the prefix, like
505 (setq org-agenda-custom-commands
506 \\='((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
507 (\"hl\" tags \"+HOME+Lisa\")
508 (\"hp\" tags \"+HOME+Peter\")
509 (\"hk\" tags \"+HOME+Kim\")))"
510 :group 'org-agenda-custom-commands
511 :type `(repeat
512 (choice :value ("x" "Describe command here" tags "" nil)
513 (list :tag "Single command"
514 (string :tag "Access Key(s) ")
515 (option (string :tag "Description"))
516 (choice
517 (const :tag "Agenda" agenda)
518 (const :tag "TODO list" alltodo)
519 (const :tag "Search words" search)
520 (const :tag "Stuck projects" stuck)
521 (const :tag "Tags/Property match (all agenda files)" tags)
522 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
523 (const :tag "TODO keyword search (all agenda files)" todo)
524 (const :tag "Tags sparse tree (current buffer)" tags-tree)
525 (const :tag "TODO keyword tree (current buffer)" todo-tree)
526 (const :tag "Occur tree (current buffer)" occur-tree)
527 (sexp :tag "Other, user-defined function"))
528 (string :tag "Match (only for some commands)")
529 ,org-agenda-custom-commands-local-options
530 (option (repeat :tag "Export" (file :tag "Export to"))))
531 (list :tag "Command series, all agenda files"
532 (string :tag "Access Key(s)")
533 (string :tag "Description ")
534 (repeat :tag "Component"
535 (choice
536 (list :tag "Agenda"
537 (const :format "" agenda)
538 (const :tag "" :format "" "")
539 ,org-agenda-custom-commands-local-options)
540 (list :tag "TODO list (all keywords)"
541 (const :format "" alltodo)
542 (const :tag "" :format "" "")
543 ,org-agenda-custom-commands-local-options)
544 (list :tag "Search words"
545 (const :format "" search)
546 (string :tag "Match")
547 ,org-agenda-custom-commands-local-options)
548 (list :tag "Stuck projects"
549 (const :format "" stuck)
550 (const :tag "" :format "" "")
551 ,org-agenda-custom-commands-local-options)
552 (list :tag "Tags search"
553 (const :format "" tags)
554 (string :tag "Match")
555 ,org-agenda-custom-commands-local-options)
556 (list :tag "Tags search, TODO entries only"
557 (const :format "" tags-todo)
558 (string :tag "Match")
559 ,org-agenda-custom-commands-local-options)
560 (list :tag "TODO keyword search"
561 (const :format "" todo)
562 (string :tag "Match")
563 ,org-agenda-custom-commands-local-options)
564 (list :tag "Other, user-defined function"
565 (symbol :tag "function")
566 (string :tag "Match")
567 ,org-agenda-custom-commands-local-options)))
569 (repeat :tag "Settings for entire command set"
570 (list (variable :tag "Any variable")
571 (sexp :tag "Value")))
572 (option (repeat :tag "Export" (file :tag "Export to"))))
573 (cons :tag "Prefix key documentation"
574 (string :tag "Access Key(s)")
575 (string :tag "Description ")))))
577 (defcustom org-agenda-query-register ?o
578 "The register holding the current query string.
579 The purpose of this is that if you construct a query string interactively,
580 you can then use it to define a custom command."
581 :group 'org-agenda-custom-commands
582 :type 'character)
584 (defcustom org-stuck-projects
585 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
586 "How to identify stuck projects.
587 This is a list of four items:
588 1. A tags/todo/property matcher string that is used to identify a project.
589 See the manual for a description of tag and property searches.
590 The entire tree below a headline matched by this is considered one project.
591 2. A list of TODO keywords identifying non-stuck projects.
592 If the project subtree contains any headline with one of these todo
593 keywords, the project is considered to be not stuck. If you specify
594 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
595 3. A list of tags identifying non-stuck projects.
596 If the project subtree contains any headline with one of these tags,
597 the project is considered to be not stuck. If you specify \"*\" as
598 a tag, any tag will mark the project unstuck. Note that this is about
599 the explicit presence of a tag somewhere in the subtree, inherited
600 tags do not count here. If inherited tags make a project not stuck,
601 use \"-TAG\" in the tags part of the matcher under (1.) above.
602 4. An arbitrary regular expression matching non-stuck projects.
604 If the project turns out to be not stuck, search continues also in the
605 subtree to see if any of the subtasks have project status.
607 See also the variable `org-tags-match-list-sublevels' which applies
608 to projects matched by this search as well.
610 After defining this variable, you may use `\\[org-agenda-list-stuck-projects]'
611 \(bound to `C-c a #') to produce the list."
612 :group 'org-agenda-custom-commands
613 :type '(list
614 (string :tag "Tags/TODO match to identify a project")
615 (repeat :tag "Projects are *not* stuck if they have an entry with \
616 TODO keyword any of" (string))
617 (repeat :tag "Projects are *not* stuck if they have an entry with \
618 TAG being any of" (string))
619 (regexp :tag "Projects are *not* stuck if this regexp matches inside \
620 the subtree")))
622 (defgroup org-agenda-skip nil
623 "Options concerning skipping parts of agenda files."
624 :tag "Org Agenda Skip"
625 :group 'org-agenda)
627 (defcustom org-agenda-skip-function-global nil
628 "Function to be called at each match during agenda construction.
629 If this function returns nil, the current match should not be skipped.
630 If the function decided to skip an agenda match, is must return the
631 buffer position from which the search should be continued.
632 This may also be a Lisp form, which will be evaluated.
634 This variable will be applied to every agenda match, including
635 tags/property searches and TODO lists. So try to make the test function
636 do its checking as efficiently as possible. To implement a skipping
637 condition just for specific agenda commands, use the variable
638 `org-agenda-skip-function' which can be set in the options section
639 of custom agenda commands."
640 :group 'org-agenda-skip
641 :type 'sexp)
643 (defgroup org-agenda-daily/weekly nil
644 "Options concerning the daily/weekly agenda."
645 :tag "Org Agenda Daily/Weekly"
646 :group 'org-agenda)
647 (defgroup org-agenda-todo-list nil
648 "Options concerning the global todo list agenda view."
649 :tag "Org Agenda Todo List"
650 :group 'org-agenda)
651 (defgroup org-agenda-match-view nil
652 "Options concerning the general tags/property/todo match agenda view."
653 :tag "Org Agenda Match View"
654 :group 'org-agenda)
655 (defgroup org-agenda-search-view nil
656 "Options concerning the search agenda view."
657 :tag "Org Agenda Search View"
658 :group 'org-agenda)
660 (defvar org-agenda-archives-mode nil
661 "Non-nil means the agenda will include archived items.
662 If this is the symbol `trees', trees in the selected agenda scope
663 that are marked with the ARCHIVE tag will be included anyway. When this is
664 t, also all archive files associated with the current selection of agenda
665 files will be included.")
667 (defcustom org-agenda-restriction-lock-highlight-subtree t
668 "Non-nil means highlight the whole subtree when restriction is active.
669 Otherwise only highlight the headline. Highlighting the whole subtree is
670 useful to ensure no edits happen beyond the restricted region."
671 :group 'org-agenda
672 :type 'boolean)
674 (defcustom org-agenda-skip-comment-trees t
675 "Non-nil means skip trees that start with the COMMENT keyword.
676 When nil, these trees are also scanned by agenda commands."
677 :group 'org-agenda-skip
678 :type 'boolean)
680 (defcustom org-agenda-todo-list-sublevels t
681 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
682 When nil, the sublevels of a TODO entry are not checked, resulting in
683 potentially much shorter TODO lists."
684 :group 'org-agenda-skip
685 :group 'org-agenda-todo-list
686 :type 'boolean)
688 (defcustom org-agenda-todo-ignore-with-date nil
689 "Non-nil means don't show entries with a date in the global todo list.
690 You can use this if you prefer to mark mere appointments with a TODO keyword,
691 but don't want them to show up in the TODO list.
692 When this is set, it also covers deadlines and scheduled items, the settings
693 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
694 will be ignored.
695 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
696 :group 'org-agenda-skip
697 :group 'org-agenda-todo-list
698 :type 'boolean)
700 (defcustom org-agenda-todo-ignore-timestamp nil
701 "Non-nil means don't show entries with a timestamp.
702 This applies when creating the global todo list.
703 Valid values are:
705 past Don't show entries for today or in the past.
707 future Don't show entries with a timestamp in the future.
708 The idea behind this is that if it has a future
709 timestamp, you don't want to think about it until the
710 date.
712 all Don't show any entries with a timestamp in the global todo list.
713 The idea behind this is that by setting a timestamp, you
714 have already \"taken care\" of this item.
716 This variable can also have an integer as a value. If positive (N),
717 todos with a timestamp N or more days in the future will be ignored. If
718 negative (-N), todos with a timestamp N or more days in the past will be
719 ignored. If 0, todos with a timestamp either today or in the future will
720 be ignored. For example, a value of -1 will exclude todos with a
721 timestamp in the past (yesterday or earlier), while a value of 7 will
722 exclude todos with a timestamp a week or more in the future.
724 See also `org-agenda-todo-ignore-with-date'.
725 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
726 to make his option also apply to the tags-todo list."
727 :group 'org-agenda-skip
728 :group 'org-agenda-todo-list
729 :version "24.1"
730 :type '(choice
731 (const :tag "Ignore future timestamp todos" future)
732 (const :tag "Ignore past or present timestamp todos" past)
733 (const :tag "Ignore all timestamp todos" all)
734 (const :tag "Show timestamp todos" nil)
735 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
737 (defcustom org-agenda-todo-ignore-scheduled nil
738 "Non-nil means, ignore some scheduled TODO items when making TODO list.
739 This applies when creating the global todo list.
740 Valid values are:
742 past Don't show entries scheduled today or in the past.
744 future Don't show entries scheduled in the future.
745 The idea behind this is that by scheduling it, you don't want to
746 think about it until the scheduled date.
748 all Don't show any scheduled entries in the global todo list.
749 The idea behind this is that by scheduling it, you have already
750 \"taken care\" of this item.
752 t Same as `all', for backward compatibility.
754 This variable can also have an integer as a value. See
755 `org-agenda-todo-ignore-timestamp' for more details.
757 See also `org-agenda-todo-ignore-with-date'.
758 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
759 to make his option also apply to the tags-todo list."
760 :group 'org-agenda-skip
761 :group 'org-agenda-todo-list
762 :type '(choice
763 (const :tag "Ignore future-scheduled todos" future)
764 (const :tag "Ignore past- or present-scheduled todos" past)
765 (const :tag "Ignore all scheduled todos" all)
766 (const :tag "Ignore all scheduled todos (compatibility)" t)
767 (const :tag "Show scheduled todos" nil)
768 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
770 (defcustom org-agenda-todo-ignore-deadlines nil
771 "Non-nil means ignore some deadline TODO items when making TODO list.
773 There are different motivations for using different values, please think
774 carefully when configuring this variable.
776 This applies when creating the global TODO list.
778 Valid values are:
780 near Don't show near deadline entries. A deadline is near when it is
781 closer than `org-deadline-warning-days' days. The idea behind this
782 is that such items will appear in the agenda anyway.
784 far Don't show TODO entries where a deadline has been defined, but
785 is not going to happen anytime soon. This is useful if you want to use
786 the TODO list to figure out what to do now.
788 past Don't show entries with a deadline timestamp for today or in the past.
790 future Don't show entries with a deadline timestamp in the future, not even
791 when they become `near' ones. Use it with caution.
793 all Ignore all TODO entries that do have a deadline.
795 t Same as `near', for backward compatibility.
797 This variable can also have an integer as a value. See
798 `org-agenda-todo-ignore-timestamp' for more details.
800 See also `org-agenda-todo-ignore-with-date'.
801 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
802 to make his option also apply to the tags-todo list."
803 :group 'org-agenda-skip
804 :group 'org-agenda-todo-list
805 :type '(choice
806 (const :tag "Ignore near deadlines" near)
807 (const :tag "Ignore near deadlines (compatibility)" t)
808 (const :tag "Ignore far deadlines" far)
809 (const :tag "Ignore all TODOs with a deadlines" all)
810 (const :tag "Show all TODOs, even if they have a deadline" nil)
811 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
813 (defcustom org-agenda-todo-ignore-time-comparison-use-seconds nil
814 "Time unit to use when possibly ignoring an agenda item.
816 See the docstring of various `org-agenda-todo-ignore-*' options.
817 The default is to compare time stamps using days. An item is thus
818 considered to be in the future if it is at least one day after today.
819 Non-nil means to compare time stamps using seconds. An item is then
820 considered future if it has a time value later than current time."
821 :group 'org-agenda-skip
822 :group 'org-agenda-todo-list
823 :version "24.4"
824 :package-version '(Org . "8.0")
825 :type '(choice
826 (const :tag "Compare time with days" nil)
827 (const :tag "Compare time with seconds" t)))
829 (defcustom org-agenda-tags-todo-honor-ignore-options nil
830 "Non-nil means honor todo-list ignores options also in tags-todo search.
831 The variables
832 `org-agenda-todo-ignore-with-date',
833 `org-agenda-todo-ignore-timestamp',
834 `org-agenda-todo-ignore-scheduled',
835 `org-agenda-todo-ignore-deadlines'
836 make the global TODO list skip entries that have time stamps of certain
837 kinds. If this option is set, the same options will also apply for the
838 tags-todo search, which is the general tags/property matcher
839 restricted to unfinished TODO entries only."
840 :group 'org-agenda-skip
841 :group 'org-agenda-todo-list
842 :group 'org-agenda-match-view
843 :type 'boolean)
845 (defcustom org-agenda-skip-scheduled-if-done nil
846 "Non-nil means don't show scheduled items in agenda when they are done.
847 This is relevant for the daily/weekly agenda, not for the TODO list. It
848 applies only to the actual date of the scheduling. Warnings about an item
849 with a past scheduling dates are always turned off when the item is DONE."
850 :group 'org-agenda-skip
851 :group 'org-agenda-daily/weekly
852 :type 'boolean)
854 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
855 "Non-nil means skip scheduling line if same entry shows because of deadline.
857 In the agenda of today, an entry can show up multiple times
858 because it is both scheduled and has a nearby deadline, and maybe
859 a plain time stamp as well.
861 When this variable is nil, the entry will be shown several times.
863 When set to t, then only the deadline is shown and the fact that
864 the entry is scheduled today or was scheduled previously is not
865 shown.
867 When set to the symbol `not-today', skip scheduled previously,
868 but not scheduled today.
870 When set to the symbol `repeated-after-deadline', skip scheduled
871 items if they are repeated beyond the current deadline."
872 :group 'org-agenda-skip
873 :group 'org-agenda-daily/weekly
874 :type '(choice
875 (const :tag "Never" nil)
876 (const :tag "Always" t)
877 (const :tag "Not when scheduled today" not-today)
878 (const :tag "When repeated past deadline" repeated-after-deadline)))
880 (defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
881 "Non-nil means skip timestamp line if same entry shows because of deadline.
882 In the agenda of today, an entry can show up multiple times
883 because it has both a plain timestamp and has a nearby deadline.
884 When this variable is t, then only the deadline is shown and the
885 fact that the entry has a timestamp for or including today is not
886 shown. When this variable is nil, the entry will be shown
887 several times."
888 :group 'org-agenda-skip
889 :group 'org-agenda-daily/weekly
890 :version "24.1"
891 :type '(choice
892 (const :tag "Never" nil)
893 (const :tag "Always" t)))
895 (defcustom org-agenda-skip-deadline-if-done nil
896 "Non-nil means don't show deadlines when the corresponding item is done.
897 When nil, the deadline is still shown and should give you a happy feeling.
898 This is relevant for the daily/weekly agenda. It applies only to the
899 actual date of the deadline. Warnings about approaching and past-due
900 deadlines are always turned off when the item is DONE."
901 :group 'org-agenda-skip
902 :group 'org-agenda-daily/weekly
903 :type 'boolean)
905 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
906 "Non-nil means skip deadline prewarning when entry is also scheduled.
907 This will apply on all days where a prewarning for the deadline would
908 be shown, but not at the day when the entry is actually due. On that day,
909 the deadline will be shown anyway.
910 This variable may be set to nil, t, the symbol `pre-scheduled',
911 or a number which will then give the number of days before the actual
912 deadline when the prewarnings should resume. The symbol `pre-scheduled'
913 eliminates the deadline prewarning only prior to the scheduled date.
914 This can be used in a workflow where the first showing of the deadline will
915 trigger you to schedule it, and then you don't want to be reminded of it
916 because you will take care of it on the day when scheduled."
917 :group 'org-agenda-skip
918 :group 'org-agenda-daily/weekly
919 :version "24.1"
920 :type '(choice
921 (const :tag "Always show prewarning" nil)
922 (const :tag "Remove prewarning prior to scheduled date" pre-scheduled)
923 (const :tag "Remove prewarning if entry is scheduled" t)
924 (integer :tag "Restart prewarning N days before deadline")))
926 (defcustom org-agenda-skip-scheduled-delay-if-deadline nil
927 "Non-nil means skip scheduled delay when entry also has a deadline.
928 This variable may be set to nil, t, the symbol `post-deadline',
929 or a number which will then give the number of days after the actual
930 scheduled date when the delay should expire. The symbol `post-deadline'
931 eliminates the schedule delay when the date is posterior to the deadline."
932 :group 'org-agenda-skip
933 :group 'org-agenda-daily/weekly
934 :version "24.4"
935 :package-version '(Org . "8.0")
936 :type '(choice
937 (const :tag "Always honor delay" nil)
938 (const :tag "Ignore delay if posterior to the deadline" post-deadline)
939 (const :tag "Ignore delay if entry has a deadline" t)
940 (integer :tag "Honor delay up until N days after the scheduled date")))
942 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
943 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
944 When non-nil, after the search for timestamps has matched once in an
945 entry, the rest of the entry will not be searched."
946 :group 'org-agenda-skip
947 :type 'boolean)
949 (defcustom org-agenda-skip-timestamp-if-done nil
950 "Non-nil means don't select item by timestamp or -range if it is DONE."
951 :group 'org-agenda-skip
952 :group 'org-agenda-daily/weekly
953 :type 'boolean)
955 (defcustom org-agenda-dim-blocked-tasks t
956 "Non-nil means dim blocked tasks in the agenda display.
957 This causes some overhead during agenda construction, but if you
958 have turned on `org-enforce-todo-dependencies',
959 `org-enforce-todo-checkbox-dependencies', or any other blocking
960 mechanism, this will create useful feedback in the agenda.
962 Instead of t, this variable can also have the value `invisible'.
963 Then blocked tasks will be invisible and only become visible when
964 they become unblocked. An exemption to this behavior is when a task is
965 blocked because of unchecked checkboxes below it. Since checkboxes do
966 not show up in the agenda views, making this task invisible you remove any
967 trace from agenda views that there is something to do. Therefore, a task
968 that is blocked because of checkboxes will never be made invisible, it
969 will only be dimmed."
970 :group 'org-agenda-daily/weekly
971 :group 'org-agenda-todo-list
972 :version "24.3"
973 :type '(choice
974 (const :tag "Do not dim" nil)
975 (const :tag "Dim to a gray face" t)
976 (const :tag "Make invisible" invisible)))
978 (defgroup org-agenda-startup nil
979 "Options concerning initial settings in the Agenda in Org Mode."
980 :tag "Org Agenda Startup"
981 :group 'org-agenda)
983 (defcustom org-agenda-menu-show-matcher t
984 "Non-nil means show the match string in the agenda dispatcher menu.
985 When nil, the matcher string is not shown, but is put into the help-echo
986 property so than moving the mouse over the command shows it.
987 Setting it to nil is good if matcher strings are very long and/or if
988 you want to use two-columns display (see `org-agenda-menu-two-columns')."
989 :group 'org-agenda
990 :version "24.1"
991 :type 'boolean)
993 (defcustom org-agenda-menu-two-columns nil
994 "Non-nil means, use two columns to show custom commands in the dispatcher.
995 If you use this, you probably want to set `org-agenda-menu-show-matcher'
996 to nil."
997 :group 'org-agenda
998 :version "24.1"
999 :type 'boolean)
1001 (defcustom org-agenda-finalize-hook nil
1002 "Hook run just before displaying an agenda buffer.
1003 The buffer is still writable when the hook is called.
1005 You can modify some of the buffer substrings but you should be
1006 extra careful not to modify the text properties of the agenda
1007 headlines as the agenda display heavily relies on them."
1008 :group 'org-agenda-startup
1009 :type 'hook)
1011 (defcustom org-agenda-mouse-1-follows-link nil
1012 "Non-nil means mouse-1 on a link will follow the link in the agenda.
1013 A longer mouse click will still set point. Needs to be set
1014 before org.el is loaded."
1015 :group 'org-agenda-startup
1016 :type 'boolean)
1018 (defcustom org-agenda-start-with-follow-mode nil
1019 "The initial value of follow mode in a newly created agenda window."
1020 :group 'org-agenda-startup
1021 :type 'boolean)
1023 (defcustom org-agenda-follow-indirect nil
1024 "Non-nil means `org-agenda-follow-mode' displays only the
1025 current item's tree, in an indirect buffer."
1026 :group 'org-agenda
1027 :version "24.1"
1028 :type 'boolean)
1030 (defcustom org-agenda-show-outline-path t
1031 "Non-nil means show outline path in echo area after line motion."
1032 :group 'org-agenda-startup
1033 :type 'boolean)
1035 (defcustom org-agenda-start-with-entry-text-mode nil
1036 "The initial value of entry-text-mode in a newly created agenda window."
1037 :group 'org-agenda-startup
1038 :type 'boolean)
1040 (defcustom org-agenda-entry-text-maxlines 5
1041 "Number of text lines to be added when `E' is pressed in the agenda.
1043 Note that this variable only used during agenda display. To add entry text
1044 when exporting the agenda, configure the variable
1045 `org-agenda-add-entry-text-maxlines'."
1046 :group 'org-agenda
1047 :type 'integer)
1049 (defcustom org-agenda-entry-text-exclude-regexps nil
1050 "List of regular expressions to clean up entry text.
1051 The complete matches of all regular expressions in this list will be
1052 removed from entry text before it is shown in the agenda."
1053 :group 'org-agenda
1054 :type '(repeat (regexp)))
1056 (defcustom org-agenda-entry-text-leaders " > "
1057 "Text prepended to the entry text in agenda buffers."
1058 :version "24.4"
1059 :package-version '(Org . "8.0")
1060 :group 'org-agenda
1061 :type 'string)
1063 (defvar org-agenda-entry-text-cleanup-hook nil
1064 "Hook that is run after basic cleanup of entry text to be shown in agenda.
1065 This cleanup is done in a temporary buffer, so the function may inspect and
1066 change the entire buffer.
1067 Some default stuff like drawers and scheduling/deadline dates will already
1068 have been removed when this is called, as will any matches for regular
1069 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
1071 (defvar org-agenda-include-inactive-timestamps nil
1072 "Non-nil means include inactive time stamps in agenda and timeline.
1073 Dynamically scoped.")
1075 (defgroup org-agenda-windows nil
1076 "Options concerning the windows used by the Agenda in Org Mode."
1077 :tag "Org Agenda Windows"
1078 :group 'org-agenda)
1080 (defcustom org-agenda-window-setup 'reorganize-frame
1081 "How the agenda buffer should be displayed.
1082 Possible values for this option are:
1084 current-window Show agenda in the current window, keeping all other windows.
1085 other-window Use `switch-to-buffer-other-window' to display agenda.
1086 only-window Show agenda, deleting all other windows.
1087 reorganize-frame Show only two windows on the current frame, the current
1088 window and the agenda.
1089 other-frame Use `switch-to-buffer-other-frame' to display agenda.
1090 Also, when exiting the agenda, kill that frame.
1091 See also the variable `org-agenda-restore-windows-after-quit'."
1092 :group 'org-agenda-windows
1093 :type '(choice
1094 (const current-window)
1095 (const other-frame)
1096 (const other-window)
1097 (const only-window)
1098 (const reorganize-frame)))
1100 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
1101 "The min and max height of the agenda window as a fraction of frame height.
1102 The value of the variable is a cons cell with two numbers between 0 and 1.
1103 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
1104 :group 'org-agenda-windows
1105 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
1107 (defcustom org-agenda-restore-windows-after-quit nil
1108 "Non-nil means restore window configuration upon exiting agenda.
1109 Before the window configuration is changed for displaying the agenda,
1110 the current status is recorded. When the agenda is exited with
1111 `q' or `x' and this option is set, the old state is restored. If
1112 `org-agenda-window-setup' is `other-frame', the value of this
1113 option will be ignored."
1114 :group 'org-agenda-windows
1115 :type 'boolean)
1117 (defcustom org-agenda-span 'week
1118 "Number of days to include in overview display.
1119 Can be day, week, month, year, or any number of days.
1120 Custom commands can set this variable in the options section."
1121 :group 'org-agenda-daily/weekly
1122 :type '(choice (const :tag "Day" day)
1123 (const :tag "Week" week)
1124 (const :tag "Fortnight" fortnight)
1125 (const :tag "Month" month)
1126 (const :tag "Year" year)
1127 (integer :tag "Custom")))
1129 (defcustom org-agenda-start-on-weekday 1
1130 "Non-nil means start the overview always on the specified weekday.
1131 0 denotes Sunday, 1 denotes Monday, etc.
1132 When nil, always start on the current day.
1133 Custom commands can set this variable in the options section."
1134 :group 'org-agenda-daily/weekly
1135 :type '(choice (const :tag "Today" nil)
1136 (integer :tag "Weekday No.")))
1138 (defcustom org-agenda-show-all-dates t
1139 "Non-nil means `org-agenda' shows every day in the selected range.
1140 When nil, only the days which actually have entries are shown."
1141 :group 'org-agenda-daily/weekly
1142 :type 'boolean)
1144 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1145 "Format string for displaying dates in the agenda.
1146 Used by the daily/weekly agenda and by the timeline. This should be
1147 a format string understood by `format-time-string', or a function returning
1148 the formatted date as a string. The function must take a single argument,
1149 a calendar-style date list like (month day year)."
1150 :group 'org-agenda-daily/weekly
1151 :type '(choice
1152 (string :tag "Format string")
1153 (function :tag "Function")))
1155 (defun org-agenda-format-date-aligned (date)
1156 "Format a DATE string for display in the daily/weekly agenda, or timeline.
1157 This function makes sure that dates are aligned for easy reading."
1158 (require 'cal-iso)
1159 (let* ((dayname (calendar-day-name date))
1160 (day (cadr date))
1161 (day-of-week (calendar-day-of-week date))
1162 (month (car date))
1163 (monthname (calendar-month-name month))
1164 (year (nth 2 date))
1165 (iso-week (org-days-to-iso-week
1166 (calendar-absolute-from-gregorian date)))
1167 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1168 (1- year))
1169 ((and (= month 12) (<= iso-week 1))
1170 (1+ year))
1171 (t year)))
1172 (weekstring (if (= day-of-week 1)
1173 (format " W%02d" iso-week)
1174 "")))
1175 (format "%-10s %2d %s %4d%s"
1176 dayname day monthname year weekstring)))
1178 (defcustom org-agenda-time-leading-zero nil
1179 "Non-nil means use leading zero for military times in agenda.
1180 For example, 9:30am would become 09:30 rather than 9:30."
1181 :group 'org-agenda-daily/weekly
1182 :version "24.1"
1183 :type 'boolean)
1185 (defcustom org-agenda-timegrid-use-ampm nil
1186 "When set, show AM/PM style timestamps on the timegrid."
1187 :group 'org-agenda
1188 :version "24.1"
1189 :type 'boolean)
1191 (defun org-agenda-time-of-day-to-ampm (time)
1192 "Convert TIME of a string like \"13:45\" to an AM/PM style time string."
1193 (let* ((hour-number (string-to-number (substring time 0 -3)))
1194 (minute (substring time -2))
1195 (ampm "am"))
1196 (cond
1197 ((equal hour-number 12)
1198 (setq ampm "pm"))
1199 ((> hour-number 12)
1200 (setq ampm "pm")
1201 (setq hour-number (- hour-number 12))))
1202 (concat
1203 (if org-agenda-time-leading-zero
1204 (format "%02d" hour-number)
1205 (format "%02s" (number-to-string hour-number)))
1206 ":" minute ampm)))
1208 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1209 "Conditionally convert TIME to AM/PM format based on `org-agenda-timegrid-use-ampm'."
1210 (if org-agenda-timegrid-use-ampm
1211 (org-agenda-time-of-day-to-ampm time)
1212 time))
1214 (defcustom org-agenda-weekend-days '(6 0)
1215 "Which days are weekend?
1216 These days get the special face `org-agenda-date-weekend' in the agenda
1217 and timeline buffers."
1218 :group 'org-agenda-daily/weekly
1219 :type '(set :greedy t
1220 (const :tag "Monday" 1)
1221 (const :tag "Tuesday" 2)
1222 (const :tag "Wednesday" 3)
1223 (const :tag "Thursday" 4)
1224 (const :tag "Friday" 5)
1225 (const :tag "Saturday" 6)
1226 (const :tag "Sunday" 0)))
1228 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1229 "Non-nil means jump to today when moving a past date forward in time.
1230 When using S-right in the agenda to move a a date forward, and the date
1231 stamp currently points to the past, the first key press will move it
1232 to today. WHen nil, just move one day forward even if the date stays
1233 in the past."
1234 :group 'org-agenda-daily/weekly
1235 :version "24.1"
1236 :type 'boolean)
1238 (defcustom org-agenda-include-diary nil
1239 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1240 Custom commands can set this variable in the options section."
1241 :group 'org-agenda-daily/weekly
1242 :type 'boolean)
1244 (defcustom org-agenda-include-deadlines t
1245 "If non-nil, include entries within their deadline warning period.
1246 Custom commands can set this variable in the options section."
1247 :group 'org-agenda-daily/weekly
1248 :version "24.1"
1249 :type 'boolean)
1251 (defcustom org-agenda-show-future-repeats t
1252 "Non-nil shows repeated entries in the future part of the agenda.
1253 When set to the symbol `next' only the first future repeat is shown."
1254 :group 'org-agenda-daily/weekly
1255 :type '(choice
1256 (const :tag "Show all repeated entries" t)
1257 (const :tag "Show next repeated entry" next)
1258 (const :tag "Do not show repeated entries" nil))
1259 :version "26.1"
1260 :package-version '(Org . "9.1")
1261 :safe #'symbolp)
1263 (defcustom org-agenda-prefer-last-repeat nil
1264 "Non-nil sets date for repeated entries to their last repeat.
1266 When nil, display SCHEDULED and DEADLINE dates at their base
1267 date, and in today's agenda, as a reminder. Display plain
1268 time-stamps, on the other hand, at every repeat date in the past
1269 in addition to the base date.
1271 When non-nil, show a repeated entry at its latest repeat date,
1272 possibly being today even if it wasn't marked as done. This
1273 setting is useful if you do not always mark repeated entries as
1274 done and, yet, consider that reaching repeat date starts the task
1275 anew.
1277 When set to a list of strings, prefer last repeats only for
1278 entries with these TODO keywords."
1279 :group 'org-agenda-daily/weekly
1280 :type '(choice
1281 (const :tag "Prefer last repeat" t)
1282 (const :tag "Prefer base date" nil)
1283 (repeat :tag "Prefer last repeat for entries with these TODO keywords"
1284 (string :tag "TODO keyword")))
1285 :version "26.1"
1286 :package-version '(Org . "9.1")
1287 :safe (lambda (x) (or (booleanp x) (consp x))))
1289 (defcustom org-scheduled-past-days 10000
1290 "Number of days to continue listing scheduled items not marked DONE.
1291 When an item is scheduled on a date, it shows up in the agenda on
1292 this day and will be listed until it is marked done or for the
1293 number of days given here."
1294 :group 'org-agenda-daily/weekly
1295 :type 'integer
1296 :safe 'integerp)
1298 (defcustom org-deadline-past-days 10000
1299 "Number of days to warn about missed deadlines.
1300 When an item has deadline on a date, it shows up in the agenda on
1301 this day and will appear as a reminder until it is marked DONE or
1302 for the number of days given here."
1303 :group 'org-agenda-daily/weekly
1304 :type 'integer
1305 :version "26.1"
1306 :package-version '(Org . "9.1")
1307 :safe 'integerp)
1309 (defcustom org-agenda-log-mode-items '(closed clock)
1310 "List of items that should be shown in agenda log mode.
1311 \\<org-agenda-mode-map>\
1312 This list may contain the following symbols:
1314 closed Show entries that have been closed on that day.
1315 clock Show entries that have received clocked time on that day.
1316 state Show all logged state changes.
1317 Note that instead of changing this variable, you can also press \
1318 `\\[universal-argument] \\[org-agenda-log-mode]' in
1319 the agenda to display all available LOG items temporarily."
1320 :group 'org-agenda-daily/weekly
1321 :type '(set :greedy t (const closed) (const clock) (const state)))
1323 (defcustom org-agenda-clock-consistency-checks
1324 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1325 :gap-ok-around ("4:00")
1326 :default-face ((:background "DarkRed") (:foreground "white"))
1327 :overlap-face nil :gap-face nil :no-end-time-face nil
1328 :long-face nil :short-face nil)
1329 "This is a property list, with the following keys:
1331 :max-duration Mark clocking chunks that are longer than this time.
1332 This is a time string like \"HH:MM\", or the number
1333 of minutes as an integer.
1335 :min-duration Mark clocking chunks that are shorter that this.
1336 This is a time string like \"HH:MM\", or the number
1337 of minutes as an integer.
1339 :max-gap Mark gaps between clocking chunks that are longer than
1340 this duration. A number of minutes, or a string
1341 like \"HH:MM\".
1343 :gap-ok-around List of times during the day which are usually not working
1344 times. When a gap is detected, but the gap contains any
1345 of these times, the gap is *not* reported. For example,
1346 if this is (\"4:00\" \"13:00\") then gaps that contain
1347 4:00 in the morning (i.e. the night) and 13:00
1348 (i.e. a typical lunch time) do not cause a warning.
1349 You should have at least one time during the night in this
1350 list, or otherwise the first task each morning will trigger
1351 a warning because it follows a long gap.
1353 Furthermore, the following properties can be used to define faces for
1354 issue display.
1356 :default-face the default face, if the specific face is undefined
1357 :overlap-face face for overlapping clocks
1358 :gap-face face for gaps between clocks
1359 :no-end-time-face face for incomplete clocks
1360 :long-face face for clock intervals that are too long
1361 :short-face face for clock intervals that are too short"
1362 :group 'org-agenda-daily/weekly
1363 :group 'org-clock
1364 :version "24.1"
1365 :type 'plist)
1367 (defcustom org-agenda-log-mode-add-notes t
1368 "Non-nil means add first line of notes to log entries in agenda views.
1369 If a log item like a state change or a clock entry is associated with
1370 notes, the first line of these notes will be added to the entry in the
1371 agenda display."
1372 :group 'org-agenda-daily/weekly
1373 :type 'boolean)
1375 (defcustom org-agenda-start-with-log-mode nil
1376 "The initial value of log-mode in a newly created agenda window.
1377 See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
1378 explanations on the possible values."
1379 :group 'org-agenda-startup
1380 :group 'org-agenda-daily/weekly
1381 :type '(choice (const :tag "Don't show log items" nil)
1382 (const :tag "Show only log items" only)
1383 (const :tag "Show all possible log items" clockcheck)
1384 (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
1385 (choice (const :tag "Show closed log items" closed)
1386 (const :tag "Show clocked log items" clock)
1387 (const :tag "Show all logged state changes" state)))))
1389 (defcustom org-agenda-start-with-clockreport-mode nil
1390 "The initial value of clockreport-mode in a newly created agenda window."
1391 :group 'org-agenda-startup
1392 :group 'org-agenda-daily/weekly
1393 :type 'boolean)
1395 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1396 "Property list with parameters for the clocktable in clockreport mode.
1397 This is the display mode that shows a clock table in the daily/weekly
1398 agenda, the properties for this dynamic block can be set here.
1399 The usual clocktable parameters are allowed here, but you cannot set
1400 the properties :name, :tstart, :tend, :block, and :scope - these will
1401 be overwritten to make sure the content accurately reflects the
1402 current display in the agenda."
1403 :group 'org-agenda-daily/weekly
1404 :type 'plist)
1406 (defcustom org-agenda-search-view-always-boolean nil
1407 "Non-nil means the search string is interpreted as individual parts.
1409 The search string for search view can either be interpreted as a phrase,
1410 or as a list of snippets that define a boolean search for a number of
1411 strings.
1413 When this is non-nil, the string will be split on whitespace, and each
1414 snippet will be searched individually, and all must match in order to
1415 select an entry. A snippet is then a single string of non-white
1416 characters, or a string in double quotes, or a regexp in {} braces.
1417 If a snippet is preceded by \"-\", the snippet must *not* match.
1418 \"+\" is syntactic sugar for positive selection. Each snippet may
1419 be found as a full word or a partial word, but see the variable
1420 `org-agenda-search-view-force-full-words'.
1422 When this is nil, search will look for the entire search phrase as one,
1423 with each space character matching any amount of whitespace, including
1424 line breaks.
1426 Even when this is nil, you can still switch to Boolean search dynamically
1427 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1428 is a regexp marked with braces like \"{abc}\", this will also switch to
1429 boolean search."
1430 :group 'org-agenda-search-view
1431 :version "24.1"
1432 :type 'boolean)
1434 (defvaralias 'org-agenda-search-view-search-words-only
1435 'org-agenda-search-view-always-boolean)
1437 (defcustom org-agenda-search-view-force-full-words nil
1438 "Non-nil means, search words must be matches as complete words.
1439 When nil, they may also match part of a word."
1440 :group 'org-agenda-search-view
1441 :version "24.1"
1442 :type 'boolean)
1444 (defcustom org-agenda-search-view-max-outline-level 0
1445 "Maximum outline level to display in search view.
1446 E.g. when this is set to 1, the search view will only
1447 show headlines of level 1. When set to 0, the default
1448 value, don't limit agenda view by outline level."
1449 :group 'org-agenda-search-view
1450 :version "24.4"
1451 :package-version '(Org . "8.3")
1452 :type 'integer)
1454 (defgroup org-agenda-time-grid nil
1455 "Options concerning the time grid in the Org Agenda."
1456 :tag "Org Agenda Time Grid"
1457 :group 'org-agenda)
1459 (defcustom org-agenda-search-headline-for-time t
1460 "Non-nil means search headline for a time-of-day.
1461 If the headline contains a time-of-day in one format or another, it will
1462 be used to sort the entry into the time sequence of items for a day.
1463 Some people have time stamps in the headline that refer to the creation
1464 time or so, and then this produces an unwanted side effect. If this is
1465 the case for your, use this variable to turn off searching the headline
1466 for a time."
1467 :group 'org-agenda-time-grid
1468 :type 'boolean)
1470 (defcustom org-agenda-use-time-grid t
1471 "Non-nil means show a time grid in the agenda schedule.
1472 A time grid is a set of lines for specific times (like every two hours between
1473 8:00 and 20:00). The items scheduled for a day at specific times are
1474 sorted in between these lines.
1475 For details about when the grid will be shown, and what it will look like, see
1476 the variable `org-agenda-time-grid'."
1477 :group 'org-agenda-time-grid
1478 :type 'boolean)
1480 (defcustom org-agenda-time-grid
1481 '((daily today require-timed)
1482 "----------------"
1483 (800 1000 1200 1400 1600 1800 2000))
1485 "The settings for time grid for agenda display.
1486 This is a list of three items. The first item is again a list. It contains
1487 symbols specifying conditions when the grid should be displayed:
1489 daily if the agenda shows a single day
1490 weekly if the agenda shows an entire week
1491 today show grid on current date, independent of daily/weekly display
1492 require-timed show grid only if at least one item has a time specification
1493 remove-match skip grid times already present in an entry
1495 The second item is a string which will be placed behind the grid time.
1497 The third item is a list of integers, indicating the times that should have
1498 a grid line."
1499 :group 'org-agenda-time-grid
1500 :type
1501 '(list
1502 (set :greedy t :tag "Grid Display Options"
1503 (const :tag "Show grid in single day agenda display" daily)
1504 (const :tag "Show grid in weekly agenda display" weekly)
1505 (const :tag "Always show grid for today" today)
1506 (const :tag "Show grid only if any timed entries are present"
1507 require-timed)
1508 (const :tag "Skip grid times already present in an entry"
1509 remove-match))
1510 (string :tag "Grid String")
1511 (repeat :tag "Grid Times" (integer :tag "Time"))))
1513 (defcustom org-agenda-show-current-time-in-grid t
1514 "Non-nil means show the current time in the time grid."
1515 :group 'org-agenda-time-grid
1516 :version "24.1"
1517 :type 'boolean)
1519 (defcustom org-agenda-current-time-string
1520 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1521 "The string for the current time marker in the agenda."
1522 :group 'org-agenda-time-grid
1523 :version "24.1"
1524 :type 'string)
1526 (defgroup org-agenda-sorting nil
1527 "Options concerning sorting in the Org Agenda."
1528 :tag "Org Agenda Sorting"
1529 :group 'org-agenda)
1531 (defcustom org-agenda-sorting-strategy
1532 '((agenda habit-down time-up priority-down category-keep)
1533 (todo priority-down category-keep)
1534 (tags priority-down category-keep)
1535 (search category-keep))
1536 "Sorting structure for the agenda items of a single day.
1537 This is a list of symbols which will be used in sequence to determine
1538 if an entry should be listed before another entry. The following
1539 symbols are recognized:
1541 time-up Put entries with time-of-day indications first, early first
1542 time-down Put entries with time-of-day indications first, late first
1543 timestamp-up Sort by any timestamp, early first
1544 timestamp-down Sort by any timestamp, late first
1545 scheduled-up Sort by scheduled timestamp, early first
1546 scheduled-down Sort by scheduled timestamp, late first
1547 deadline-up Sort by deadline timestamp, early first
1548 deadline-down Sort by deadline timestamp, late first
1549 ts-up Sort by active timestamp, early first
1550 ts-down Sort by active timestamp, late first
1551 tsia-up Sort by inactive timestamp, early first
1552 tsia-down Sort by inactive timestamp, late first
1553 category-keep Keep the default order of categories, corresponding to the
1554 sequence in `org-agenda-files'.
1555 category-up Sort alphabetically by category, A-Z.
1556 category-down Sort alphabetically by category, Z-A.
1557 tag-up Sort alphabetically by last tag, A-Z.
1558 tag-down Sort alphabetically by last tag, Z-A.
1559 priority-up Sort numerically by priority, high priority last.
1560 priority-down Sort numerically by priority, high priority first.
1561 todo-state-up Sort by todo state, tasks that are done last.
1562 todo-state-down Sort by todo state, tasks that are done first.
1563 effort-up Sort numerically by estimated effort, high effort last.
1564 effort-down Sort numerically by estimated effort, high effort first.
1565 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1566 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1567 habit-up Put entries that are habits first
1568 habit-down Put entries that are habits last
1569 alpha-up Sort headlines alphabetically
1570 alpha-down Sort headlines alphabetically, reversed
1572 The different possibilities will be tried in sequence, and testing stops
1573 if one comparison returns a \"not-equal\". For example, the default
1574 '(time-up category-keep priority-down)
1575 means: Pull out all entries having a specified time of day and sort them,
1576 in order to make a time schedule for the current day the first thing in the
1577 agenda listing for the day. Of the entries without a time indication, keep
1578 the grouped in categories, don't sort the categories, but keep them in
1579 the sequence given in `org-agenda-files'. Within each category sort by
1580 priority.
1582 Leaving out `category-keep' would mean that items will be sorted across
1583 categories by priority.
1585 Instead of a single list, this can also be a set of list for specific
1586 contents, with a context symbol in the car of the list, any of
1587 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1589 Custom commands can bind this variable in the options section."
1590 :group 'org-agenda-sorting
1591 :type `(choice
1592 (repeat :tag "General" ,org-sorting-choice)
1593 (list :tag "Individually"
1594 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1595 (repeat ,org-sorting-choice))
1596 (cons (const :tag "Strategy for TODO lists" todo)
1597 (repeat ,org-sorting-choice))
1598 (cons (const :tag "Strategy for Tags matches" tags)
1599 (repeat ,org-sorting-choice))
1600 (cons (const :tag "Strategy for search matches" search)
1601 (repeat ,org-sorting-choice)))))
1603 (defcustom org-agenda-cmp-user-defined nil
1604 "A function to define the comparison `user-defined'.
1605 This function must receive two arguments, agenda entry a and b.
1606 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1607 the user comparison, return nil.
1608 When this is defined, you can make `user-defined-up' and `user-defined-down'
1609 part of an agenda sorting strategy."
1610 :group 'org-agenda-sorting
1611 :type 'symbol)
1613 (defcustom org-sort-agenda-notime-is-late t
1614 "Non-nil means items without time are considered late.
1615 This is only relevant for sorting. When t, items which have no explicit
1616 time like 15:30 will be considered as 99:01, i.e. later than any items which
1617 do have a time. When nil, the default time is before 0:00. You can use this
1618 option to decide if the schedule for today should come before or after timeless
1619 agenda entries."
1620 :group 'org-agenda-sorting
1621 :type 'boolean)
1623 (defcustom org-sort-agenda-noeffort-is-high t
1624 "Non-nil means items without effort estimate are sorted as high effort.
1625 This also applies when filtering an agenda view with respect to the
1626 < or > effort operator. Then, tasks with no effort defined will be treated
1627 as tasks with high effort.
1628 When nil, such items are sorted as 0 minutes effort."
1629 :group 'org-agenda-sorting
1630 :type 'boolean)
1632 (defgroup org-agenda-line-format nil
1633 "Options concerning the entry prefix in the Org agenda display."
1634 :tag "Org Agenda Line Format"
1635 :group 'org-agenda)
1637 (defcustom org-agenda-prefix-format
1638 '((agenda . " %i %-12:c%?-12t% s")
1639 (timeline . " % s")
1640 (todo . " %i %-12:c")
1641 (tags . " %i %-12:c")
1642 (search . " %i %-12:c"))
1643 "Format specifications for the prefix of items in the agenda views.
1644 An alist with five entries, each for the different agenda types. The
1645 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1646 The values are format strings.
1648 This format works similar to a printf format, with the following meaning:
1650 %c the category of the item, \"Diary\" for entries from the diary,
1651 or as given by the CATEGORY keyword or derived from the file name
1652 %e the effort required by the item
1653 %l the level of the item (insert X space(s) if item is of level X)
1654 %i the icon category of the item, see `org-agenda-category-icon-alist'
1655 %T the last tag of the item (ignore inherited tags, which come first)
1656 %t the HH:MM time-of-day specification if one applies to the entry
1657 %s Scheduling/Deadline information, a short string
1658 %b show breadcrumbs, i.e., the names of the higher levels
1659 %(expression) Eval EXPRESSION and replace the control string
1660 by the result
1662 All specifiers work basically like the standard `%s' of printf, but may
1663 contain two additional characters: a question mark just after the `%'
1664 and a whitespace/punctuation character just before the final letter.
1666 If the first character after `%' is a question mark, the entire field
1667 will only be included if the corresponding value applies to the current
1668 entry. This is useful for fields which should have fixed width when
1669 present, but zero width when absent. For example, \"%?-12t\" will
1670 result in a 12 character time field if a time of the day is specified,
1671 but will completely disappear in entries which do not contain a time.
1673 If there is punctuation or whitespace character just before the
1674 final format letter, this character will be appended to the field
1675 value if the value is not empty. For example, the format
1676 \"%-12:c\" leads to \"Diary: \" if the category is \"Diary\". If
1677 the category is empty, no additional colon is inserted.
1679 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1680 which means:
1682 - Indent the line with two space characters
1683 - Give the category a 12 chars wide field, padded with whitespace on
1684 the right (because of `-'). Append a colon if there is a category
1685 (because of `:').
1686 - If there is a time-of-day, put it into a 12 chars wide field. If no
1687 time, don't put in an empty field, just skip it (because of '?').
1688 - Finally, put the scheduling information.
1690 See also the variables `org-agenda-remove-times-when-in-prefix' and
1691 `org-agenda-remove-tags'.
1693 Custom commands can set this variable in the options section."
1694 :type '(choice
1695 (string :tag "General format")
1696 (list :greedy t :tag "View dependent"
1697 (cons (const agenda) (string :tag "Format"))
1698 (cons (const timeline) (string :tag "Format"))
1699 (cons (const todo) (string :tag "Format"))
1700 (cons (const tags) (string :tag "Format"))
1701 (cons (const search) (string :tag "Format"))))
1702 :group 'org-agenda-line-format)
1704 (defvar org-prefix-format-compiled nil
1705 "The compiled prefix format and associated variables.
1706 This is a list where first element is a list of variable bindings, and second
1707 element is the compiled format expression. See the variable
1708 `org-agenda-prefix-format'.")
1710 (defcustom org-agenda-todo-keyword-format "%-1s"
1711 "Format for the TODO keyword in agenda lines.
1712 Set this to something like \"%-12s\" if you want all TODO keywords
1713 to occupy a fixed space in the agenda display."
1714 :group 'org-agenda-line-format
1715 :type 'string)
1717 (defcustom org-agenda-diary-sexp-prefix nil
1718 "A regexp that matches part of a diary sexp entry
1719 which should be treated as scheduling/deadline information in
1720 `org-agenda'.
1722 For example, you can use this to extract the `diary-remind-message' from
1723 `diary-remind' entries."
1724 :group 'org-agenda-line-format
1725 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1727 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1728 "Text preceding timerange entries in the agenda view.
1729 This is a list with two strings. The first applies when the range
1730 is entirely on one day. The second applies if the range spans several days.
1731 The strings may have two \"%d\" format specifiers which will be filled
1732 with the sequence number of the days, and the total number of days in the
1733 range, respectively."
1734 :group 'org-agenda-line-format
1735 :type '(list
1736 (string :tag "Deadline today ")
1737 (choice :tag "Deadline relative"
1738 (string :tag "Format string")
1739 (function))))
1741 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1742 "Text preceding scheduled items in the agenda view.
1743 This is a list with two strings. The first applies when the item is
1744 scheduled on the current day. The second applies when it has been scheduled
1745 previously, it may contain a %d indicating that this is the nth time that
1746 this item is scheduled, due to automatic rescheduling of unfinished items
1747 for the following day. So this number is one larger than the number of days
1748 that passed since this item was scheduled first."
1749 :group 'org-agenda-line-format
1750 :version "24.4"
1751 :package-version '(Org . "8.0")
1752 :type '(list
1753 (string :tag "Scheduled today ")
1754 (string :tag "Scheduled previously")))
1756 (defcustom org-agenda-inactive-leader "["
1757 "Text preceding item pulled into the agenda by inactive time stamps.
1758 These entries are added to the agenda when pressing \"[\"."
1759 :group 'org-agenda-line-format
1760 :version "24.1"
1761 :type 'string)
1763 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: " "%2d d. ago: ")
1764 "Text preceding deadline items in the agenda view.
1765 This is a list with three strings. The first applies when the item has its
1766 deadline on the current day. The second applies when the deadline is in the
1767 future, the third one when it is in the past. The strings may contain %d
1768 to capture the number of days."
1769 :group 'org-agenda-line-format
1770 :version "24.4"
1771 :package-version '(Org . "8.0")
1772 :type '(list
1773 (string :tag "Deadline today ")
1774 (string :tag "Deadline in the future ")
1775 (string :tag "Deadline in the past ")))
1777 (defcustom org-agenda-remove-times-when-in-prefix t
1778 "Non-nil means remove duplicate time specifications in agenda items.
1779 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1780 time-of-day specification in a headline or diary entry is extracted and
1781 placed into the prefix. If this option is non-nil, the original specification
1782 \(a timestamp or -range, or just a plain time(range) specification like
1783 11:30-4pm) will be removed for agenda display. This makes the agenda less
1784 cluttered.
1785 The option can be t or nil. It may also be the symbol `beg', indicating
1786 that the time should only be removed when it is located at the beginning of
1787 the headline/diary entry."
1788 :group 'org-agenda-line-format
1789 :type '(choice
1790 (const :tag "Always" t)
1791 (const :tag "Never" nil)
1792 (const :tag "When at beginning of entry" beg)))
1794 (defcustom org-agenda-remove-timeranges-from-blocks nil
1795 "Non-nil means remove time ranges specifications in agenda
1796 items that span on several days."
1797 :group 'org-agenda-line-format
1798 :version "24.1"
1799 :type 'boolean)
1801 (defcustom org-agenda-default-appointment-duration nil
1802 "Default duration for appointments that only have a starting time.
1803 When nil, no duration is specified in such cases.
1804 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1805 :group 'org-agenda-line-format
1806 :type '(choice
1807 (integer :tag "Minutes")
1808 (const :tag "No default duration")))
1810 (defcustom org-agenda-show-inherited-tags t
1811 "Non-nil means show inherited tags in each agenda line.
1813 When this option is set to `always', it takes precedence over
1814 `org-agenda-use-tag-inheritance' and inherited tags are shown
1815 in every agenda.
1817 When this option is set to t (the default), inherited tags are
1818 shown when they are available, i.e. when the value of
1819 `org-agenda-use-tag-inheritance' enables tag inheritance for the
1820 given agenda type.
1822 This can be set to a list of agenda types in which the agenda
1823 must display the inherited tags. Available types are `todo',
1824 `agenda', `search' and `timeline'.
1826 When set to nil, never show inherited tags in agenda lines."
1827 :group 'org-agenda-line-format
1828 :group 'org-agenda
1829 :version "24.3"
1830 :type '(choice
1831 (const :tag "Show inherited tags when available" t)
1832 (const :tag "Always show inherited tags" always)
1833 (repeat :tag "Show inherited tags only in selected agenda types"
1834 (symbol :tag "Agenda type"))))
1836 (defcustom org-agenda-use-tag-inheritance '(todo search timeline agenda)
1837 "List of agenda view types where to use tag inheritance.
1839 In tags/tags-todo/tags-tree agenda views, tag inheritance is
1840 controlled by `org-use-tag-inheritance'. In other agenda types,
1841 `org-use-tag-inheritance' is not used for the selection of the
1842 agenda entries. Still, you may want the agenda to be aware of
1843 the inherited tags anyway, e.g. for later tag filtering.
1845 Allowed value are `todo', `search', `timeline' and `agenda'.
1847 This variable has no effect if `org-agenda-show-inherited-tags'
1848 is set to `always'. In that case, the agenda is aware of those
1849 tags.
1851 The default value sets tags in every agenda type. Setting this
1852 option to nil will speed up non-tags agenda view a lot."
1853 :group 'org-agenda
1854 :version "24.3"
1855 :type '(choice
1856 (const :tag "Use tag inheritance in all agenda types" t)
1857 (repeat :tag "Use tag inheritance in selected agenda types"
1858 (symbol :tag "Agenda type"))))
1860 (defcustom org-agenda-hide-tags-regexp nil
1861 "Regular expression used to filter away specific tags in agenda views.
1862 This means that these tags will be present, but not be shown in the agenda
1863 line. Secondary filtering will still work on the hidden tags.
1864 Nil means don't hide any tags."
1865 :group 'org-agenda-line-format
1866 :type '(choice
1867 (const :tag "Hide none" nil)
1868 (string :tag "Regexp ")))
1870 (defcustom org-agenda-remove-tags nil
1871 "Non-nil means remove the tags from the headline copy in the agenda.
1872 When this is the symbol `prefix', only remove tags when
1873 `org-agenda-prefix-format' contains a `%T' specifier."
1874 :group 'org-agenda-line-format
1875 :type '(choice
1876 (const :tag "Always" t)
1877 (const :tag "Never" nil)
1878 (const :tag "When prefix format contains %T" prefix)))
1880 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1881 'org-agenda-remove-tags)
1883 (defcustom org-agenda-tags-column -80
1884 "Shift tags in agenda items to this column.
1885 If this number is positive, it specifies the column. If it is negative,
1886 it means that the tags should be flushright to that column. For example,
1887 -80 works well for a normal 80 character screen."
1888 :group 'org-agenda-line-format
1889 :type 'integer)
1891 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column)
1893 (defcustom org-agenda-fontify-priorities 'cookies
1894 "Non-nil means highlight low and high priorities in agenda.
1895 When t, the highest priority entries are bold, lowest priority italic.
1896 However, settings in `org-priority-faces' will overrule these faces.
1897 When this variable is the symbol `cookies', only fontify the
1898 cookies, not the entire task.
1899 This may also be an association list of priority faces, whose
1900 keys are the character values of `org-highest-priority',
1901 `org-default-priority', and `org-lowest-priority' (the default values
1902 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1903 color as a string, or a list like `(:background \"Red\")'.
1904 If it is a color, the variable `org-faces-easy-properties'
1905 determines if it is a foreground or a background color."
1906 :group 'org-agenda-line-format
1907 :type '(choice
1908 (const :tag "Never" nil)
1909 (const :tag "Defaults" t)
1910 (const :tag "Cookies only" cookies)
1911 (repeat :tag "Specify"
1912 (list (character :tag "Priority" :value ?A)
1913 (choice :tag "Face "
1914 (string :tag "Color")
1915 (sexp :tag "Face"))))))
1917 (defcustom org-agenda-day-face-function nil
1918 "Function called to determine what face should be used to display a day.
1919 The only argument passed to that function is the day. It should
1920 returns a face, or nil if does not want to specify a face and let
1921 the normal rules apply."
1922 :group 'org-agenda-line-format
1923 :version "24.1"
1924 :type '(choice (const nil) (function)))
1926 (defcustom org-agenda-category-icon-alist nil
1927 "Alist of category icon to be displayed in agenda views.
1929 Each entry should have the following format:
1931 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1933 Where CATEGORY-REGEXP is a regexp matching the categories where
1934 the icon should be displayed.
1935 FILE-OR-DATA either a file path or a string containing image data.
1937 The other fields can be omitted safely if not needed:
1938 TYPE indicates the image type.
1939 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1940 image data.
1941 PROPS are additional image attributes to assign to the image,
1942 like, e.g. `:ascent center'.
1944 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1946 If you want to set the display properties yourself, just put a
1947 list as second element:
1949 (CATEGORY-REGEXP (MY PROPERTY LIST))
1951 For example, to display a 16px horizontal space for Emacs
1952 category, you can use:
1954 (\"Emacs\" \\='(space . (:width (16))))"
1955 :group 'org-agenda-line-format
1956 :version "24.1"
1957 :type '(alist :key-type (string :tag "Regexp matching category")
1958 :value-type (choice (list :tag "Icon"
1959 (string :tag "File or data")
1960 (symbol :tag "Type")
1961 (boolean :tag "Data?")
1962 (repeat :tag "Extra image properties" :inline t symbol))
1963 (list :tag "Display properties" sexp))))
1965 (defgroup org-agenda-column-view nil
1966 "Options concerning column view in the agenda."
1967 :tag "Org Agenda Column View"
1968 :group 'org-agenda)
1970 (defcustom org-agenda-view-columns-initially nil
1971 "When non-nil, switch to columns view right after creating the agenda."
1972 :group 'org-agenda-column-view
1973 :type 'boolean
1974 :version "26.1"
1975 :package-version '(Org . "9.0")
1976 :safe #'booleanp)
1978 (defcustom org-agenda-columns-show-summaries t
1979 "Non-nil means show summaries for columns displayed in the agenda view."
1980 :group 'org-agenda-column-view
1981 :type 'boolean)
1983 (defcustom org-agenda-columns-compute-summary-properties t
1984 "Non-nil means recompute all summary properties before column view.
1985 When column view in the agenda is listing properties that have a summary
1986 operator, it can go to all relevant buffers and recompute the summaries
1987 there. This can mean overhead for the agenda column view, but is necessary
1988 to have thing up to date.
1989 As a special case, a CLOCKSUM property also makes sure that the clock
1990 computations are current."
1991 :group 'org-agenda-column-view
1992 :type 'boolean)
1994 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1995 "Non-nil means the duration of an appointment will add to day effort.
1996 The property to which appointment durations will be added is the one given
1997 in the option `org-effort-property'. If an appointment does not have
1998 an end time, `org-agenda-default-appointment-duration' will be used. If that
1999 is not set, an appointment without end time will not contribute to the time
2000 estimate."
2001 :group 'org-agenda-column-view
2002 :type 'boolean)
2004 (defcustom org-agenda-auto-exclude-function nil
2005 "A function called with a tag to decide if it is filtered on \
2006 \\<org-agenda-mode-map>`\\[org-agenda-filter-by-tag] RET'.
2007 The sole argument to the function, which is called once for each
2008 possible tag, is a string giving the name of the tag. The
2009 function should return either nil if the tag should be included
2010 as normal, or \"-<TAG>\" to exclude the tag.
2011 Note that for the purpose of tag filtering, only the lower-case version of
2012 all tags will be considered, so that this function will only ever see
2013 the lower-case version of all tags."
2014 :group 'org-agenda
2015 :type '(choice (const nil) (function)))
2017 (defcustom org-agenda-bulk-custom-functions nil
2018 "Alist of characters and custom functions for bulk actions.
2019 For example, this value makes those two functions available:
2021 \\='((?R set-category)
2022 (?C bulk-cut))
2024 With selected entries in an agenda buffer, `B R' will call
2025 the custom function `set-category' on the selected entries.
2026 Note that functions in this alist don't need to be quoted."
2027 :type '(alist :key-type character :value-type (group function))
2028 :version "24.1"
2029 :group 'org-agenda)
2031 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
2032 "Execute BODY with point at location given by `org-hd-marker' property.
2033 If STRING is non-nil, the text property will be fetched from position 0
2034 in that string. If STRING is nil, it will be fetched from the beginning
2035 of the current line."
2036 (org-with-gensyms (marker)
2037 `(let ((,marker (get-text-property (if ,string 0 (point-at-bol))
2038 'org-hd-marker ,string)))
2039 (with-current-buffer (marker-buffer ,marker)
2040 (save-excursion
2041 (goto-char ,marker)
2042 ,@body)))))
2043 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
2045 (defun org-add-agenda-custom-command (entry)
2046 "Replace or add a command in `org-agenda-custom-commands'.
2047 This is mostly for hacking and trying a new command - once the command
2048 works you probably want to add it to `org-agenda-custom-commands' for good."
2049 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
2050 (if ass
2051 (setcdr ass (cdr entry))
2052 (push entry org-agenda-custom-commands))))
2054 ;;; Define the org-agenda-mode
2056 (defvar org-agenda-mode-map (make-sparse-keymap)
2057 "Keymap for `org-agenda-mode'.")
2058 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map)
2060 (defvar org-agenda-menu) ; defined later in this file.
2061 (defvar org-agenda-restrict nil) ; defined later in this file.
2062 (defvar org-agenda-follow-mode nil)
2063 (defvar org-agenda-entry-text-mode nil)
2064 (defvar org-agenda-clockreport-mode nil)
2065 (defvar org-agenda-show-log nil)
2066 (defvar org-agenda-redo-command nil)
2067 (defvar org-agenda-query-string nil)
2068 (defvar org-agenda-mode-hook nil
2069 "Hook run after `org-agenda-mode' is turned on.
2070 The buffer is still writable when this hook is called.")
2071 (defvar org-agenda-type nil)
2072 (defvar org-agenda-force-single-file nil)
2073 (defvar org-agenda-bulk-marked-entries nil
2074 "List of markers that refer to marked entries in the agenda.")
2075 (defvar org-agenda-current-date nil
2076 "Active date when building the agenda.")
2078 ;;; Multiple agenda buffers support
2080 (defcustom org-agenda-sticky nil
2081 "Non-nil means agenda q key will bury agenda buffers.
2082 Agenda commands will then show existing buffer instead of generating new ones.
2083 When nil, `q' will kill the single agenda buffer."
2084 :group 'org-agenda
2085 :version "24.3"
2086 :type 'boolean)
2089 ;;;###autoload
2090 (defun org-toggle-sticky-agenda (&optional arg)
2091 "Toggle `org-agenda-sticky'."
2092 (interactive "P")
2093 (let ((new-value (if arg
2094 (> (prefix-numeric-value arg) 0)
2095 (not org-agenda-sticky))))
2096 (if (equal new-value org-agenda-sticky)
2097 (and (called-interactively-p 'interactive)
2098 (message "Sticky agenda was already %s"
2099 (if org-agenda-sticky "enabled" "disabled")))
2100 (setq org-agenda-sticky new-value)
2101 (org-agenda-kill-all-agenda-buffers)
2102 (and (called-interactively-p 'interactive)
2103 (message "Sticky agenda %s"
2104 (if org-agenda-sticky "enabled" "disabled"))))))
2106 (defvar org-agenda-buffer nil
2107 "Agenda buffer currently being generated.")
2109 (defvar org-agenda-last-prefix-arg nil)
2110 (defvar org-agenda-this-buffer-name nil)
2111 (defvar org-agenda-doing-sticky-redo nil)
2112 (defvar org-agenda-this-buffer-is-sticky nil)
2113 (defvar org-agenda-last-indirect-buffer nil
2114 "Last buffer loaded by `org-agenda-tree-to-indirect-buffer'.")
2116 (defconst org-agenda-local-vars
2117 '(org-agenda-this-buffer-name
2118 org-agenda-undo-list
2119 org-agenda-pending-undo-list
2120 org-agenda-follow-mode
2121 org-agenda-entry-text-mode
2122 org-agenda-clockreport-mode
2123 org-agenda-show-log
2124 org-agenda-redo-command
2125 org-agenda-query-string
2126 org-agenda-type
2127 org-agenda-bulk-marked-entries
2128 org-agenda-undo-has-started-in
2129 org-agenda-info
2130 org-agenda-pre-window-conf
2131 org-agenda-columns-active
2132 org-agenda-tag-filter
2133 org-agenda-category-filter
2134 org-agenda-top-headline-filter
2135 org-agenda-regexp-filter
2136 org-agenda-effort-filter
2137 org-agenda-markers
2138 org-agenda-last-search-view-search-was-boolean
2139 org-agenda-last-indirect-buffer
2140 org-agenda-filtered-by-category
2141 org-agenda-filter-form
2142 org-agenda-cycle-counter
2143 org-agenda-last-prefix-arg)
2144 "Variables that must be local in agenda buffers to allow multiple buffers.")
2146 (defun org-agenda-mode ()
2147 "Mode for time-sorted view on action items in Org files.
2149 The following commands are available:
2151 \\{org-agenda-mode-map}"
2152 (interactive)
2153 (cond (org-agenda-doing-sticky-redo
2154 ;; Refreshing sticky agenda-buffer
2156 ;; Preserve the value of `org-agenda-local-vars' variables,
2157 ;; while letting `kill-all-local-variables' kill the rest
2158 (let ((save (buffer-local-variables)))
2159 (kill-all-local-variables)
2160 (mapc #'make-local-variable org-agenda-local-vars)
2161 (dolist (elem save)
2162 (pcase elem
2163 (`(,var . ,val) ;ignore unbound variables
2164 (when (and val (memq var org-agenda-local-vars))
2165 (set var val))))))
2166 (setq-local org-agenda-this-buffer-is-sticky t))
2167 (org-agenda-sticky
2168 ;; Creating a sticky Agenda buffer for the first time
2169 (kill-all-local-variables)
2170 (mapc 'make-local-variable org-agenda-local-vars)
2171 (setq-local org-agenda-this-buffer-is-sticky t))
2173 ;; Creating a non-sticky agenda buffer
2174 (kill-all-local-variables)
2175 (setq-local org-agenda-this-buffer-is-sticky nil)))
2176 (setq org-agenda-undo-list nil
2177 org-agenda-pending-undo-list nil
2178 org-agenda-bulk-marked-entries nil)
2179 (setq major-mode 'org-agenda-mode)
2180 ;; Keep global-font-lock-mode from turning on font-lock-mode
2181 (setq-local font-lock-global-modes (list 'not major-mode))
2182 (setq mode-name "Org-Agenda")
2183 (setq indent-tabs-mode nil)
2184 (use-local-map org-agenda-mode-map)
2185 (easy-menu-add org-agenda-menu)
2186 (if org-startup-truncated (setq truncate-lines t))
2187 (setq-local line-move-visual nil)
2188 (add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
2189 (add-hook 'pre-command-hook 'org-unhighlight nil 'local)
2190 ;; Make sure properties are removed when copying text
2191 (add-hook 'filter-buffer-substring-functions
2192 (lambda (fun start end delete)
2193 (substring-no-properties (funcall fun start end delete)))
2194 nil t)
2195 (unless org-agenda-keep-modes
2196 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
2197 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode))
2198 (setq org-agenda-show-log org-agenda-start-with-log-mode)
2199 (setq org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode)
2200 (add-to-invisibility-spec '(org-filtered))
2201 (add-to-invisibility-spec '(org-link))
2202 (easy-menu-change
2203 '("Agenda") "Agenda Files"
2204 (append
2205 (list
2206 (vector
2207 (if (get 'org-agenda-files 'org-restrict)
2208 "Restricted to single file"
2209 "Edit File List")
2210 '(org-edit-agenda-file-list)
2211 (not (get 'org-agenda-files 'org-restrict)))
2212 "--")
2213 (mapcar 'org-file-menu-entry (org-agenda-files))))
2214 (org-agenda-set-mode-name)
2215 (apply
2216 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2217 (list 'org-agenda-mode-hook)))
2219 (substitute-key-definition 'undo 'org-agenda-undo
2220 org-agenda-mode-map global-map)
2221 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2222 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2223 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2224 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2225 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2226 (org-defkey org-agenda-mode-map [(meta down)] 'org-agenda-drag-line-forward)
2227 (org-defkey org-agenda-mode-map [(meta up)] 'org-agenda-drag-line-backward)
2228 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2229 (org-defkey org-agenda-mode-map "\M-m" 'org-agenda-bulk-toggle)
2230 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2231 (org-defkey org-agenda-mode-map "\M-*" 'org-agenda-bulk-toggle-all)
2232 (org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
2233 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2234 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2235 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2236 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2237 (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2238 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2239 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2240 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2241 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2242 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2243 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2244 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2245 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2246 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2247 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2248 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2249 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2250 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2251 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2252 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2253 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2254 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2255 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2256 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2257 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2258 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2259 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2260 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2261 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2262 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2263 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2264 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2265 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2266 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2267 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2268 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2269 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2270 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2272 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2273 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2274 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2275 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2276 (while l (org-defkey org-agenda-mode-map
2277 (int-to-string (pop l)) 'digit-argument)))
2279 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2280 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2281 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2282 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2283 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2284 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2285 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2286 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2287 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2288 (org-defkey org-agenda-mode-map "g" 'org-agenda-redo-all)
2289 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2290 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2291 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2292 'org-clock-modify-effort-estimate)
2293 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2294 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2295 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2296 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2297 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2298 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2299 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2300 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2301 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2302 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2303 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2304 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2305 (substitute-key-definition 'next-line 'org-agenda-next-line
2306 org-agenda-mode-map global-map)
2307 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2308 org-agenda-mode-map global-map)
2309 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2310 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2311 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2312 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2313 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2314 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2315 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2316 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2317 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2318 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2319 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2320 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2321 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2322 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2323 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2324 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2325 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2326 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2327 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2328 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2329 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2330 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2331 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2332 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2333 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2334 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2335 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2336 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2337 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2338 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2339 (org-defkey org-agenda-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock-from-agenda)
2341 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2342 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2343 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2344 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2345 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2346 (org-defkey org-agenda-mode-map "_" 'org-agenda-filter-by-effort)
2347 (org-defkey org-agenda-mode-map "=" 'org-agenda-filter-by-regexp)
2348 (org-defkey org-agenda-mode-map "|" 'org-agenda-filter-remove-all)
2349 (org-defkey org-agenda-mode-map "~" 'org-agenda-limit-interactively)
2350 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2351 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-headline)
2352 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2353 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2354 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2355 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2357 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2358 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2360 (define-key org-agenda-mode-map [remap forward-paragraph] 'org-agenda-forward-block)
2361 (define-key org-agenda-mode-map [remap backward-paragraph] 'org-agenda-backward-block)
2363 (when org-agenda-mouse-1-follows-link
2364 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2365 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2366 '("Agenda"
2367 ("Agenda Files")
2368 "--"
2369 ("Agenda Dates"
2370 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2371 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2372 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2373 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2374 "--"
2375 ("View"
2376 ["Day View" org-agenda-day-view
2377 :active (org-agenda-check-type nil 'agenda)
2378 :style radio :selected (eq org-agenda-current-span 'day)
2379 :keys "v d (or just d)"]
2380 ["Week View" org-agenda-week-view
2381 :active (org-agenda-check-type nil 'agenda)
2382 :style radio :selected (eq org-agenda-current-span 'week)
2383 :keys "v w"]
2384 ["Fortnight View" org-agenda-fortnight-view
2385 :active (org-agenda-check-type nil 'agenda)
2386 :style radio :selected (eq org-agenda-current-span 'fortnight)
2387 :keys "v t"]
2388 ["Month View" org-agenda-month-view
2389 :active (org-agenda-check-type nil 'agenda)
2390 :style radio :selected (eq org-agenda-current-span 'month)
2391 :keys "v m"]
2392 ["Year View" org-agenda-year-view
2393 :active (org-agenda-check-type nil 'agenda)
2394 :style radio :selected (eq org-agenda-current-span 'year)
2395 :keys "v y"]
2396 "--"
2397 ["Include Diary" org-agenda-toggle-diary
2398 :style toggle :selected org-agenda-include-diary
2399 :active (org-agenda-check-type nil 'agenda)]
2400 ["Include Deadlines" org-agenda-toggle-deadlines
2401 :style toggle :selected org-agenda-include-deadlines
2402 :active (org-agenda-check-type nil 'agenda)]
2403 ["Use Time Grid" org-agenda-toggle-time-grid
2404 :style toggle :selected org-agenda-use-time-grid
2405 :active (org-agenda-check-type nil 'agenda)]
2406 "--"
2407 ["Show clock report" org-agenda-clockreport-mode
2408 :style toggle :selected org-agenda-clockreport-mode
2409 :active (org-agenda-check-type nil 'agenda)]
2410 ["Show some entry text" org-agenda-entry-text-mode
2411 :style toggle :selected org-agenda-entry-text-mode
2412 :active t]
2413 "--"
2414 ["Show Logbook entries" org-agenda-log-mode
2415 :style toggle :selected org-agenda-show-log
2416 :active (org-agenda-check-type nil 'agenda 'timeline)
2417 :keys "v l (or just l)"]
2418 ["Include archived trees" org-agenda-archives-mode
2419 :style toggle :selected org-agenda-archives-mode :active t
2420 :keys "v a"]
2421 ["Include archive files" (org-agenda-archives-mode t)
2422 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2423 :keys "v A"]
2424 "--"
2425 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2426 ["Write view to file" org-agenda-write t]
2427 ["Rebuild buffer" org-agenda-redo t]
2428 ["Save all Org buffers" org-save-all-org-buffers t]
2429 "--"
2430 ["Show original entry" org-agenda-show t]
2431 ["Go To (other window)" org-agenda-goto t]
2432 ["Go To (this window)" org-agenda-switch-to t]
2433 ["Capture with cursor date" org-agenda-capture t]
2434 ["Follow Mode" org-agenda-follow-mode
2435 :style toggle :selected org-agenda-follow-mode :active t]
2436 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2437 "--"
2438 ("TODO"
2439 ["Cycle TODO" org-agenda-todo t]
2440 ["Next TODO set" org-agenda-todo-nextset t]
2441 ["Previous TODO set" org-agenda-todo-previousset t]
2442 ["Add note" org-agenda-add-note t])
2443 ("Archive/Refile/Delete"
2444 ["Archive default" org-agenda-archive-default t]
2445 ["Archive default" org-agenda-archive-default-with-confirmation t]
2446 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2447 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2448 ["Archive subtree" org-agenda-archive t]
2449 "--"
2450 ["Refile" org-agenda-refile t]
2451 "--"
2452 ["Delete subtree" org-agenda-kill t])
2453 ("Bulk action"
2454 ["Mark entry" org-agenda-bulk-mark t]
2455 ["Mark all" org-agenda-bulk-mark-all t]
2456 ["Unmark entry" org-agenda-bulk-unmark t]
2457 ["Unmark all" org-agenda-bulk-unmark-all :active t :keys "U"]
2458 ["Toggle mark" org-agenda-bulk-toggle t]
2459 ["Toggle all" org-agenda-bulk-toggle-all t]
2460 ["Mark regexp" org-agenda-bulk-mark-regexp t])
2461 ["Act on all marked" org-agenda-bulk-action t]
2462 "--"
2463 ("Tags and Properties"
2464 ["Show all Tags" org-agenda-show-tags t]
2465 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2466 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2467 "--"
2468 ["Column View" org-columns t])
2469 ("Deadline/Schedule"
2470 ["Schedule" org-agenda-schedule t]
2471 ["Set Deadline" org-agenda-deadline t]
2472 "--"
2473 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2474 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2475 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2476 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2477 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2478 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2479 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2480 ("Clock and Effort"
2481 ["Clock in" org-agenda-clock-in t]
2482 ["Clock out" org-agenda-clock-out t]
2483 ["Clock cancel" org-agenda-clock-cancel t]
2484 ["Goto running clock" org-clock-goto t]
2485 "--"
2486 ["Set Effort" org-agenda-set-effort t]
2487 ["Change clocked effort" org-clock-modify-effort-estimate
2488 (org-clock-is-active)])
2489 ("Priority"
2490 ["Set Priority" org-agenda-priority t]
2491 ["Increase Priority" org-agenda-priority-up t]
2492 ["Decrease Priority" org-agenda-priority-down t]
2493 ["Show Priority" org-show-priority t])
2494 ("Calendar/Diary"
2495 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2496 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2497 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2498 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2499 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2500 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2501 "--"
2502 ["Create iCalendar File" org-icalendar-combine-agenda-files t])
2503 "--"
2504 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2505 "--"
2506 ("MobileOrg"
2507 ["Push Files and Views" org-mobile-push t]
2508 ["Get Captured and Flagged" org-mobile-pull t]
2509 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2510 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2511 "--"
2512 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2513 "--"
2514 ["Quit" org-agenda-quit t]
2515 ["Exit and Release Buffers" org-agenda-exit t]
2518 ;;; Agenda undo
2520 (defvar org-agenda-allow-remote-undo t
2521 "Non-nil means allow remote undo from the agenda buffer.")
2522 (defvar org-agenda-undo-has-started-in nil
2523 "Buffers that have already seen `undo-start' in the current undo sequence.")
2525 (defun org-agenda-undo ()
2526 "Undo a remote editing step in the agenda.
2527 This undoes changes both in the agenda buffer and in the remote buffer
2528 that have been changed along."
2529 (interactive)
2530 (or org-agenda-allow-remote-undo
2531 (user-error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2532 (if (not (eq this-command last-command))
2533 (setq org-agenda-undo-has-started-in nil
2534 org-agenda-pending-undo-list org-agenda-undo-list))
2535 (if (not org-agenda-pending-undo-list)
2536 (user-error "No further undo information"))
2537 (let* ((entry (pop org-agenda-pending-undo-list))
2538 buf line cmd rembuf)
2539 (setq cmd (pop entry) line (pop entry))
2540 (setq rembuf (nth 2 entry))
2541 (org-with-remote-undo rembuf
2542 (while (bufferp (setq buf (pop entry)))
2543 (if (pop entry)
2544 (with-current-buffer buf
2545 (let ((last-undo-buffer buf)
2546 (inhibit-read-only t))
2547 (unless (memq buf org-agenda-undo-has-started-in)
2548 (push buf org-agenda-undo-has-started-in)
2549 (make-local-variable 'pending-undo-list)
2550 (undo-start))
2551 (while (and pending-undo-list
2552 (listp pending-undo-list)
2553 (not (car pending-undo-list)))
2554 (pop pending-undo-list))
2555 (undo-more 1))))))
2556 (org-goto-line line)
2557 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2559 (defun org-verify-change-for-undo (l1 l2)
2560 "Verify that a real change occurred between the undo lists L1 and L2."
2561 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2562 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2563 (not (eq l1 l2)))
2565 ;;; Agenda dispatch
2567 (defvar org-agenda-restrict-begin (make-marker))
2568 (defvar org-agenda-restrict-end (make-marker))
2569 (defvar org-agenda-last-dispatch-buffer nil)
2570 (defvar org-agenda-overriding-restriction nil)
2572 (defcustom org-agenda-custom-commands-contexts nil
2573 "Alist of custom agenda keys and contextual rules.
2575 For example, if you have a custom agenda command \"p\" and you
2576 want this command to be accessible only from plain text files,
2577 use this:
2579 \\='((\"p\" ((in-file . \"\\\\.txt\\\\'\"))))
2581 Here are the available contexts definitions:
2583 in-file: command displayed only in matching files
2584 in-mode: command displayed only in matching modes
2585 not-in-file: command not displayed in matching files
2586 not-in-mode: command not displayed in matching modes
2587 in-buffer: command displayed only in matching buffers
2588 not-in-buffer: command not displayed in matching buffers
2589 [function]: a custom function taking no argument
2591 If you define several checks, the agenda command will be
2592 accessible if there is at least one valid check.
2594 You can also bind a key to another agenda custom command
2595 depending on contextual rules.
2597 \\='((\"p\" \"q\" ((in-file . \"\\\\.txt\\\\'\"))))
2599 Here it means: in .txt files, use \"p\" as the key for the
2600 agenda command otherwise associated with \"q\". (The command
2601 originally associated with \"q\" is not displayed to avoid
2602 duplicates.)"
2603 :version "24.3"
2604 :group 'org-agenda-custom-commands
2605 :type '(repeat (list :tag "Rule"
2606 (string :tag " Agenda key")
2607 (string :tag "Replace by command")
2608 (repeat :tag "Available when"
2609 (choice
2610 (cons :tag "Condition"
2611 (choice
2612 (const :tag "In file" in-file)
2613 (const :tag "Not in file" not-in-file)
2614 (const :tag "In buffer" in-buffer)
2615 (const :tag "Not in buffer" not-in-buffer)
2616 (const :tag "In mode" in-mode)
2617 (const :tag "Not in mode" not-in-mode))
2618 (regexp))
2619 (function :tag "Custom function"))))))
2621 (defcustom org-agenda-max-entries nil
2622 "Maximum number of entries to display in an agenda.
2623 This can be nil (no limit) or an integer or an alist of agenda
2624 types with an associated number of entries to display in this
2625 type."
2626 :version "24.4"
2627 :package-version '(Org . "8.0")
2628 :group 'org-agenda-custom-commands
2629 :type '(choice (symbol :tag "No limit" nil)
2630 (integer :tag "Max number of entries")
2631 (repeat
2632 (cons (choice :tag "Agenda type"
2633 (const agenda)
2634 (const todo)
2635 (const tags)
2636 (const search)
2637 (const timeline))
2638 (integer :tag "Max number of entries")))))
2640 (defcustom org-agenda-max-todos nil
2641 "Maximum number of TODOs to display in an agenda.
2642 This can be nil (no limit) or an integer or an alist of agenda
2643 types with an associated number of entries to display in this
2644 type."
2645 :version "24.4"
2646 :package-version '(Org . "8.0")
2647 :group 'org-agenda-custom-commands
2648 :type '(choice (symbol :tag "No limit" nil)
2649 (integer :tag "Max number of TODOs")
2650 (repeat
2651 (cons (choice :tag "Agenda type"
2652 (const agenda)
2653 (const todo)
2654 (const tags)
2655 (const search)
2656 (const timeline))
2657 (integer :tag "Max number of TODOs")))))
2659 (defcustom org-agenda-max-tags nil
2660 "Maximum number of tagged entries to display in an agenda.
2661 This can be nil (no limit) or an integer or an alist of agenda
2662 types with an associated number of entries to display in this
2663 type."
2664 :version "24.4"
2665 :package-version '(Org . "8.0")
2666 :group 'org-agenda-custom-commands
2667 :type '(choice (symbol :tag "No limit" nil)
2668 (integer :tag "Max number of tagged entries")
2669 (repeat
2670 (cons (choice :tag "Agenda type"
2671 (const agenda)
2672 (const todo)
2673 (const tags)
2674 (const search)
2675 (const timeline))
2676 (integer :tag "Max number of tagged entries")))))
2678 (defcustom org-agenda-max-effort nil
2679 "Maximum cumulated effort duration for the agenda.
2680 This can be nil (no limit) or a number of minutes (as an integer)
2681 or an alist of agenda types with an associated number of minutes
2682 to limit entries to in this type."
2683 :version "24.4"
2684 :package-version '(Org . "8.0")
2685 :group 'org-agenda-custom-commands
2686 :type '(choice (symbol :tag "No limit" nil)
2687 (integer :tag "Max number of minutes")
2688 (repeat
2689 (cons (choice :tag "Agenda type"
2690 (const agenda)
2691 (const todo)
2692 (const tags)
2693 (const search)
2694 (const timeline))
2695 (integer :tag "Max number of minutes")))))
2697 (defvar org-agenda-keep-restricted-file-list nil)
2698 (defvar org-keys nil)
2699 (defvar org-match nil)
2700 ;;;###autoload
2701 (defun org-agenda (&optional arg org-keys restriction)
2702 "Dispatch agenda commands to collect entries to the agenda buffer.
2703 Prompts for a command to execute. Any prefix arg will be passed
2704 on to the selected command. The default selections are:
2706 a Call `org-agenda-list' to display the agenda for current day or week.
2707 t Call `org-todo-list' to display the global todo list.
2708 T Call `org-todo-list' to display the global todo list, select only
2709 entries with a specific TODO keyword (the user gets a prompt).
2710 m Call `org-tags-view' to display headlines with tags matching
2711 a condition (the user is prompted for the condition).
2712 M Like `m', but select only TODO entries, no ordinary headlines.
2713 e Export views to associated files.
2714 s Search entries for keywords.
2715 S Search entries for keywords, only with TODO keywords.
2716 / Multi occur across all agenda files and also files listed
2717 in `org-agenda-text-search-extra-files'.
2718 < Restrict agenda commands to buffer, subtree, or region.
2719 Press several times to get the desired effect.
2720 > Remove a previous restriction.
2721 # List \"stuck\" projects.
2722 ! Configure what \"stuck\" means.
2723 C Configure custom agenda commands.
2725 More commands can be added by configuring the variable
2726 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2727 searches can be pre-defined in this way.
2729 If the current buffer is in Org mode and visiting a file, you can also
2730 first press `<' once to indicate that the agenda should be temporarily
2731 \(until the next use of `\\[org-agenda]') restricted to the current file.
2732 Pressing `<' twice means to restrict to the current subtree or region
2733 \(if active)."
2734 (interactive "P")
2735 (catch 'exit
2736 (let* ((prefix-descriptions nil)
2737 (org-agenda-buffer-name org-agenda-buffer-name)
2738 (org-agenda-window-setup (if (equal (buffer-name)
2739 org-agenda-buffer-name)
2740 'current-window
2741 org-agenda-window-setup))
2742 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2743 (org-agenda-custom-commands
2744 ;; normalize different versions
2745 (delq nil
2746 (mapcar
2747 (lambda (x)
2748 (cond ((stringp (cdr x))
2749 (push x prefix-descriptions)
2750 nil)
2751 ((stringp (nth 1 x)) x)
2752 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2753 (t (cons (car x) (cons "" (cdr x))))))
2754 org-agenda-custom-commands)))
2755 (org-agenda-custom-commands
2756 (org-contextualize-keys
2757 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2758 (buf (current-buffer))
2759 (bfn (buffer-file-name (buffer-base-buffer)))
2760 entry key type org-match lprops ans)
2761 ;; Turn off restriction unless there is an overriding one,
2762 (unless org-agenda-overriding-restriction
2763 (unless org-agenda-keep-restricted-file-list
2764 ;; There is a request to keep the file list in place
2765 (put 'org-agenda-files 'org-restrict nil))
2766 (setq org-agenda-restrict nil)
2767 (move-marker org-agenda-restrict-begin nil)
2768 (move-marker org-agenda-restrict-end nil))
2769 ;; Delete old local properties
2770 (put 'org-agenda-redo-command 'org-lprops nil)
2771 ;; Delete previously set last-arguments
2772 (put 'org-agenda-redo-command 'last-args nil)
2773 ;; Remember where this call originated
2774 (setq org-agenda-last-dispatch-buffer (current-buffer))
2775 (unless org-keys
2776 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2777 org-keys (car ans)
2778 restriction (cdr ans)))
2779 ;; If we have sticky agenda buffers, set a name for the buffer,
2780 ;; depending on the invoking keys. The user may still set this
2781 ;; as a command option, which will overwrite what we do here.
2782 (if org-agenda-sticky
2783 (setq org-agenda-buffer-name
2784 (format "*Org Agenda(%s)*" org-keys)))
2785 ;; Establish the restriction, if any
2786 (when (and (not org-agenda-overriding-restriction) restriction)
2787 (put 'org-agenda-files 'org-restrict (list bfn))
2788 (cond
2789 ((eq restriction 'region)
2790 (setq org-agenda-restrict (current-buffer))
2791 (move-marker org-agenda-restrict-begin (region-beginning))
2792 (move-marker org-agenda-restrict-end (region-end)))
2793 ((eq restriction 'subtree)
2794 (save-excursion
2795 (setq org-agenda-restrict (current-buffer))
2796 (org-back-to-heading t)
2797 (move-marker org-agenda-restrict-begin (point))
2798 (move-marker org-agenda-restrict-end
2799 (progn (org-end-of-subtree t)))))))
2801 ;; For example the todo list should not need it (but does...)
2802 (cond
2803 ((setq entry (assoc org-keys org-agenda-custom-commands))
2804 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2805 (progn
2806 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
2807 lprops (nth 4 entry))
2808 (if org-agenda-sticky
2809 (setq org-agenda-buffer-name
2810 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2811 (format "*Org Agenda(%s)*" org-keys))))
2812 (put 'org-agenda-redo-command 'org-lprops lprops)
2813 (cond
2814 ((eq type 'agenda)
2815 (org-let lprops '(org-agenda-list current-prefix-arg)))
2816 ((eq type 'agenda*)
2817 (org-let lprops '(org-agenda-list current-prefix-arg nil nil t)))
2818 ((eq type 'alltodo)
2819 (org-let lprops '(org-todo-list current-prefix-arg)))
2820 ((eq type 'search)
2821 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
2822 ((eq type 'stuck)
2823 (org-let lprops '(org-agenda-list-stuck-projects
2824 current-prefix-arg)))
2825 ((eq type 'tags)
2826 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2827 ((eq type 'tags-todo)
2828 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2829 ((eq type 'todo)
2830 (org-let lprops '(org-todo-list org-match)))
2831 ((eq type 'tags-tree)
2832 (org-check-for-org-mode)
2833 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
2834 ((eq type 'todo-tree)
2835 (org-check-for-org-mode)
2836 (org-let lprops
2837 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2838 (regexp-quote org-match) "\\>"))))
2839 ((eq type 'occur-tree)
2840 (org-check-for-org-mode)
2841 (org-let lprops '(org-occur org-match)))
2842 ((functionp type)
2843 (org-let lprops '(funcall type org-match)))
2844 ((fboundp type)
2845 (org-let lprops '(funcall type org-match)))
2846 (t (user-error "Invalid custom agenda command type %s" type))))
2847 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2848 ((equal org-keys "C")
2849 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2850 (customize-variable 'org-agenda-custom-commands))
2851 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2852 ((equal org-keys "s") (call-interactively 'org-search-view))
2853 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2854 ((equal org-keys "t") (call-interactively 'org-todo-list))
2855 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2856 ((equal org-keys "m") (call-interactively 'org-tags-view))
2857 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2858 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2859 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
2860 (add-hook
2861 'post-command-hook
2862 (lambda ()
2863 (unless (current-message)
2864 (let* ((m (org-agenda-get-any-marker))
2865 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2866 (when note
2867 (message (concat
2868 "FLAGGING-NOTE ([?] for more info): "
2869 (org-add-props
2870 (replace-regexp-in-string
2871 "\\\\n" "//"
2872 (copy-sequence note))
2873 nil 'face 'org-warning)))))))
2874 t t))
2875 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2876 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2877 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
2878 (t (user-error "Invalid agenda key"))))))
2880 (defvar org-agenda-multi)
2882 (defun org-agenda-append-agenda ()
2883 "Append another agenda view to the current one.
2884 This function allows interactive building of block agendas.
2885 Agenda views are separated by `org-agenda-block-separator'."
2886 (interactive)
2887 (unless (derived-mode-p 'org-agenda-mode)
2888 (user-error "Can only append from within agenda buffer"))
2889 (let ((org-agenda-multi t))
2890 (org-agenda)
2891 (widen)
2892 (org-agenda-finalize)
2893 (setq buffer-read-only t)
2894 (org-agenda-fit-window-to-buffer)))
2896 (defun org-agenda-normalize-custom-commands (cmds)
2897 "Normalize custom commands CMDS."
2898 (delq nil
2899 (mapcar
2900 (lambda (x)
2901 (cond ((stringp (cdr x)) nil)
2902 ((stringp (nth 1 x)) x)
2903 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2904 (t (cons (car x) (cons "" (cdr x))))))
2905 cmds)))
2907 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2908 "The user interface for selecting an agenda command."
2909 (catch 'exit
2910 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2911 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2912 (region-p (org-region-active-p))
2913 (custom org-agenda-custom-commands)
2914 (selstring "")
2915 restriction second-time
2916 c entry key type match prefixes rmheader header-end custom1 desc
2917 line lines left right n n1)
2918 (save-window-excursion
2919 (delete-other-windows)
2920 (org-switch-to-buffer-other-window " *Agenda Commands*")
2921 (erase-buffer)
2922 (insert (eval-when-compile
2923 (let ((header
2924 "Press key for an agenda command:
2925 -------------------------------- < Buffer, subtree/region restriction
2926 a Agenda for current week or day > Remove restriction
2927 t List of all TODO entries e Export agenda views
2928 m Match a TAGS/PROP/TODO query T Entries with special TODO kwd
2929 s Search for keywords M Like m, but only TODO entries
2930 / Multi-occur S Like s, but only TODO entries
2931 ? Find :FLAGGED: entries C Configure custom agenda commands
2932 * Toggle sticky agenda views # List stuck projects (!=configure)
2934 (start 0))
2935 (while (string-match
2936 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2937 header start)
2938 (setq start (match-end 0))
2939 (add-text-properties (match-beginning 2) (match-end 2)
2940 '(face bold) header))
2941 header)))
2942 (setq header-end (point-marker))
2943 (while t
2944 (setq custom1 custom)
2945 (when (eq rmheader t)
2946 (org-goto-line 1)
2947 (re-search-forward ":" nil t)
2948 (delete-region (match-end 0) (point-at-eol))
2949 (forward-char 1)
2950 (looking-at "-+")
2951 (delete-region (match-end 0) (point-at-eol))
2952 (move-marker header-end (match-end 0)))
2953 (goto-char header-end)
2954 (delete-region (point) (point-max))
2956 ;; Produce all the lines that describe custom commands and prefixes
2957 (setq lines nil)
2958 (while (setq entry (pop custom1))
2959 (setq key (car entry) desc (nth 1 entry)
2960 type (nth 2 entry)
2961 match (nth 3 entry))
2962 (if (> (length key) 1)
2963 (cl-pushnew (string-to-char key) prefixes :test #'equal)
2964 (setq line
2965 (format
2966 "%-4s%-14s"
2967 (org-add-props (copy-sequence key)
2968 '(face bold))
2969 (cond
2970 ((string-match "\\S-" desc) desc)
2971 ((eq type 'agenda) "Agenda for current week or day")
2972 ((eq type 'agenda*) "Appointments for current week or day")
2973 ((eq type 'alltodo) "List of all TODO entries")
2974 ((eq type 'search) "Word search")
2975 ((eq type 'stuck) "List of stuck projects")
2976 ((eq type 'todo) "TODO keyword")
2977 ((eq type 'tags) "Tags query")
2978 ((eq type 'tags-todo) "Tags (TODO)")
2979 ((eq type 'tags-tree) "Tags tree")
2980 ((eq type 'todo-tree) "TODO kwd tree")
2981 ((eq type 'occur-tree) "Occur tree")
2982 ((functionp type) (if (symbolp type)
2983 (symbol-name type)
2984 "Lambda expression"))
2985 (t "???"))))
2986 (if org-agenda-menu-show-matcher
2987 (setq line
2988 (concat line ": "
2989 (cond
2990 ((stringp match)
2991 (setq match (copy-sequence match))
2992 (org-add-props match nil 'face 'org-warning))
2993 ((listp type)
2994 (format "set of %d commands" (length type))))))
2995 (if (org-string-nw-p match)
2996 (add-text-properties
2997 0 (length line) (list 'help-echo
2998 (concat "Matcher: " match)) line)))
2999 (push line lines)))
3000 (setq lines (nreverse lines))
3001 (when prefixes
3002 (mapc (lambda (x)
3003 (push
3004 (format "%s %s"
3005 (org-add-props (char-to-string x)
3006 nil 'face 'bold)
3007 (or (cdr (assoc (concat selstring
3008 (char-to-string x))
3009 prefix-descriptions))
3010 "Prefix key"))
3011 lines))
3012 prefixes))
3014 ;; Check if we should display in two columns
3015 (if org-agenda-menu-two-columns
3016 (progn
3017 (setq n (length lines)
3018 n1 (+ (/ n 2) (mod n 2))
3019 right (nthcdr n1 lines)
3020 left (copy-sequence lines))
3021 (setcdr (nthcdr (1- n1) left) nil))
3022 (setq left lines right nil))
3023 (while left
3024 (insert "\n" (pop left))
3025 (when right
3026 (if (< (current-column) 40)
3027 (move-to-column 40 t)
3028 (insert " "))
3029 (insert (pop right))))
3031 ;; Make the window the right size
3032 (goto-char (point-min))
3033 (if second-time
3034 (if (not (pos-visible-in-window-p (point-max)))
3035 (org-fit-window-to-buffer))
3036 (setq second-time t)
3037 (org-fit-window-to-buffer))
3039 ;; Ask for selection
3040 (message "Press key for agenda command%s:"
3041 (if (or restrict-ok org-agenda-overriding-restriction)
3042 (if org-agenda-overriding-restriction
3043 " (restriction lock active)"
3044 (if restriction
3045 (format " (restricted to %s)" restriction)
3046 " (unrestricted)"))
3047 ""))
3048 (setq c (read-char-exclusive))
3049 (message "")
3050 (cond
3051 ((assoc (char-to-string c) custom)
3052 (setq selstring (concat selstring (char-to-string c)))
3053 (throw 'exit (cons selstring restriction)))
3054 ((memq c prefixes)
3055 (setq selstring (concat selstring (char-to-string c))
3056 prefixes nil
3057 rmheader (or rmheader t)
3058 custom (delq nil (mapcar
3059 (lambda (x)
3060 (if (or (= (length (car x)) 1)
3061 (/= (string-to-char (car x)) c))
3063 (cons (substring (car x) 1) (cdr x))))
3064 custom))))
3065 ((eq c ?*)
3066 (call-interactively 'org-toggle-sticky-agenda)
3067 (sit-for 2))
3068 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
3069 (message "Restriction is only possible in Org buffers")
3070 (ding) (sit-for 1))
3071 ((eq c ?1)
3072 (org-agenda-remove-restriction-lock 'noupdate)
3073 (setq restriction 'buffer))
3074 ((eq c ?0)
3075 (org-agenda-remove-restriction-lock 'noupdate)
3076 (setq restriction (if region-p 'region 'subtree)))
3077 ((eq c ?<)
3078 (org-agenda-remove-restriction-lock 'noupdate)
3079 (setq restriction
3080 (cond
3081 ((eq restriction 'buffer)
3082 (if region-p 'region 'subtree))
3083 ((memq restriction '(subtree region))
3084 nil)
3085 (t 'buffer))))
3086 ((eq c ?>)
3087 (org-agenda-remove-restriction-lock 'noupdate)
3088 (setq restriction nil))
3089 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
3090 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
3091 ((and (> (length selstring) 0) (eq c ?\d))
3092 (delete-window)
3093 (org-agenda-get-restriction-and-command prefix-descriptions))
3095 ((equal c ?q) (error "Abort"))
3096 (t (user-error "Invalid key %c" c))))))))
3098 (defun org-agenda-fit-window-to-buffer ()
3099 "Fit the window to the buffer size."
3100 (and (memq org-agenda-window-setup '(reorganize-frame))
3101 (fboundp 'fit-window-to-buffer)
3102 (if (and (= (cdr org-agenda-window-frame-fractions) 1.0)
3103 (= (car org-agenda-window-frame-fractions) 1.0))
3104 (delete-other-windows)
3105 (org-fit-window-to-buffer
3107 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3108 (floor (* (frame-height) (car org-agenda-window-frame-fractions)))))))
3110 (defvar org-cmd nil)
3111 (defvar org-agenda-overriding-cmd nil)
3112 (defvar org-agenda-overriding-arguments nil)
3113 (defvar org-agenda-overriding-cmd-arguments nil)
3114 (defun org-agenda-run-series (name series)
3115 "Run agenda NAME as a SERIES of agenda commands."
3116 (org-let (nth 1 series) '(org-agenda-prepare name))
3117 ;; We need to reset agenda markers here, because when constructing a
3118 ;; block agenda, the individual blocks do not do that.
3119 (org-agenda-reset-markers)
3120 (let* ((org-agenda-multi t)
3121 (redo (list 'org-agenda-run-series name (list 'quote series)))
3122 (cmds (car series))
3123 (gprops (nth 1 series))
3124 match ;; The byte compiler incorrectly complains about this. Keep it!
3125 org-cmd type lprops)
3126 (while (setq org-cmd (pop cmds))
3127 (setq type (car org-cmd))
3128 (setq match (eval (nth 1 org-cmd)))
3129 (setq lprops (nth 2 org-cmd))
3130 (let ((org-agenda-overriding-arguments
3131 (if (eq org-agenda-overriding-cmd org-cmd)
3132 (or org-agenda-overriding-arguments
3133 org-agenda-overriding-cmd-arguments))))
3134 (cond
3135 ((eq type 'agenda)
3136 (org-let2 gprops lprops
3137 '(call-interactively 'org-agenda-list)))
3138 ((eq type 'agenda*)
3139 (org-let2 gprops lprops
3140 '(funcall 'org-agenda-list nil nil t)))
3141 ((eq type 'alltodo)
3142 (org-let2 gprops lprops
3143 '(call-interactively 'org-todo-list)))
3144 ((eq type 'search)
3145 (org-let2 gprops lprops
3146 '(org-search-view current-prefix-arg match nil)))
3147 ((eq type 'stuck)
3148 (org-let2 gprops lprops
3149 '(call-interactively 'org-agenda-list-stuck-projects)))
3150 ((eq type 'tags)
3151 (org-let2 gprops lprops
3152 '(org-tags-view current-prefix-arg match)))
3153 ((eq type 'tags-todo)
3154 (org-let2 gprops lprops
3155 '(org-tags-view current-prefix-arg match)))
3156 ((eq type 'todo)
3157 (org-let2 gprops lprops
3158 '(org-todo-list match)))
3159 ((fboundp type)
3160 (org-let2 gprops lprops
3161 '(funcall type match)))
3162 (t (error "Invalid type in command series")))))
3163 (widen)
3164 (let ((inhibit-read-only t))
3165 (add-text-properties (point-min) (point-max)
3166 `(org-series t org-series-redo-cmd ,redo)))
3167 (setq org-agenda-redo-command redo)
3168 (goto-char (point-min)))
3169 (org-agenda-fit-window-to-buffer)
3170 (org-let (nth 1 series) '(org-agenda-finalize)))
3172 ;;;###autoload
3173 (defmacro org-batch-agenda (cmd-key &rest parameters)
3174 "Run an agenda command in batch mode and send the result to STDOUT.
3175 If CMD-KEY is a string of length 1, it is used as a key in
3176 `org-agenda-custom-commands' and triggers this command. If it is a
3177 longer string it is used as a tags/todo match string.
3178 Parameters are alternating variable names and values that will be bound
3179 before running the agenda command."
3180 (org-eval-in-environment (org-make-parameter-alist parameters)
3181 (let (org-agenda-sticky)
3182 (if (> (length cmd-key) 1)
3183 (org-tags-view nil cmd-key)
3184 (org-agenda nil cmd-key))))
3185 (set-buffer org-agenda-buffer-name)
3186 (princ (buffer-string)))
3188 (defvar org-agenda-info nil)
3190 ;;;###autoload
3191 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
3192 "Run an agenda command in batch mode and send the result to STDOUT.
3193 If CMD-KEY is a string of length 1, it is used as a key in
3194 `org-agenda-custom-commands' and triggers this command. If it is a
3195 longer string it is used as a tags/todo match string.
3196 Parameters are alternating variable names and values that will be bound
3197 before running the agenda command.
3199 The output gives a line for each selected agenda item. Each
3200 item is a list of comma-separated values, like this:
3202 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3204 category The category of the item
3205 head The headline, without TODO kwd, TAGS and PRIORITY
3206 type The type of the agenda entry, can be
3207 todo selected in TODO match
3208 tagsmatch selected in tags match
3209 diary imported from diary
3210 deadline a deadline on given date
3211 scheduled scheduled on given date
3212 timestamp entry has timestamp on given date
3213 closed entry was closed on given date
3214 upcoming-deadline warning about deadline
3215 past-scheduled forwarded scheduled item
3216 block entry has date block including g. date
3217 todo The todo keyword, if any
3218 tags All tags including inherited ones, separated by colons
3219 date The relevant date, like 2007-2-14
3220 time The time, like 15:00-16:50
3221 extra Sting with extra planning info
3222 priority-l The priority letter if any was given
3223 priority-n The computed numerical priority
3224 agenda-day The day in the agenda where this is listed"
3225 (org-eval-in-environment (append '((org-agenda-remove-tags t))
3226 (org-make-parameter-alist parameters))
3227 (if (> (length cmd-key) 2)
3228 (org-tags-view nil cmd-key)
3229 (org-agenda nil cmd-key)))
3230 (set-buffer org-agenda-buffer-name)
3231 (let* ((lines (org-split-string (buffer-string) "\n"))
3232 line)
3233 (while (setq line (pop lines))
3234 (catch 'next
3235 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
3236 (setq org-agenda-info
3237 (org-fix-agenda-info (text-properties-at 0 line)))
3238 (princ
3239 (mapconcat 'org-agenda-export-csv-mapper
3240 '(org-category txt type todo tags date time extra
3241 priority-letter priority agenda-day)
3242 ","))
3243 (princ "\n")))))
3245 (defun org-fix-agenda-info (props)
3246 "Make sure all properties on an agenda item have a canonical form.
3247 This ensures the export commands can easily use it."
3248 (let (tmp re)
3249 (when (setq tmp (plist-get props 'tags))
3250 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3251 (when (setq tmp (plist-get props 'date))
3252 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3253 (let ((calendar-date-display-form '(year "-" month "-" day)))
3254 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3256 (setq tmp (calendar-date-string tmp)))
3257 (setq props (plist-put props 'date tmp)))
3258 (when (setq tmp (plist-get props 'day))
3259 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3260 (let ((calendar-date-display-form '(year "-" month "-" day)))
3261 (setq tmp (calendar-date-string tmp)))
3262 (setq props (plist-put props 'day tmp))
3263 (setq props (plist-put props 'agenda-day tmp)))
3264 (when (setq tmp (plist-get props 'txt))
3265 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3266 (plist-put props 'priority-letter (match-string 1 tmp))
3267 (setq tmp (replace-match "" t t tmp)))
3268 (when (and (setq re (plist-get props 'org-todo-regexp))
3269 (setq re (concat "\\`\\.*" re " ?"))
3270 (let ((case-fold-search nil)) (string-match re tmp)))
3271 (plist-put props 'todo (match-string 1 tmp))
3272 (setq tmp (replace-match "" t t tmp)))
3273 (plist-put props 'txt tmp)))
3274 props)
3276 (defun org-agenda-export-csv-mapper (prop)
3277 (let ((res (plist-get org-agenda-info prop)))
3278 (setq res
3279 (cond
3280 ((not res) "")
3281 ((stringp res) res)
3282 (t (prin1-to-string res))))
3283 (org-trim (replace-regexp-in-string "," ";" res nil t))))
3285 ;;;###autoload
3286 (defun org-store-agenda-views (&rest parameters)
3287 "Store agenda views."
3288 (interactive)
3289 (eval (list 'org-batch-store-agenda-views)))
3291 ;;;###autoload
3292 (defmacro org-batch-store-agenda-views (&rest parameters)
3293 "Run all custom agenda commands that have a file argument."
3294 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3295 (pop-up-frames nil)
3296 (dir default-directory)
3297 (pars (org-make-parameter-alist parameters))
3298 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
3299 (save-window-excursion
3300 (while cmds
3301 (setq cmd (pop cmds)
3302 thiscmdkey (car cmd)
3303 thiscmdcmd (cdr cmd)
3304 match (nth 2 thiscmdcmd)
3305 bufname (if org-agenda-sticky
3306 (or (and (stringp match)
3307 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3308 (format "*Org Agenda(%s)*" thiscmdkey))
3309 org-agenda-buffer-name)
3310 cmd-or-set (nth 2 cmd)
3311 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3312 files (nth (if (listp cmd-or-set) 4 5) cmd))
3313 (if (stringp files) (setq files (list files)))
3314 (when files
3315 (org-eval-in-environment (append org-agenda-exporter-settings
3316 opts pars)
3317 (org-agenda nil thiscmdkey))
3318 (set-buffer bufname)
3319 (while files
3320 (org-eval-in-environment (append org-agenda-exporter-settings
3321 opts pars)
3322 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3323 (and (get-buffer bufname)
3324 (kill-buffer bufname)))))))
3326 (defvar org-agenda-current-span nil
3327 "The current span used in the agenda view.") ; local variable in the agenda buffer
3328 (defun org-agenda-mark-header-line (pos)
3329 "Mark the line at POS as an agenda structure header."
3330 (save-excursion
3331 (goto-char pos)
3332 (put-text-property (point-at-bol) (point-at-eol)
3333 'org-agenda-structural-header t)
3334 (when org-agenda-title-append
3335 (put-text-property (point-at-bol) (point-at-eol)
3336 'org-agenda-title-append org-agenda-title-append))))
3338 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3339 (defvar org-agenda-write-buffer-name "Agenda View")
3340 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3341 "Write the current buffer (an agenda view) as a file.
3343 Depending on the extension of the file name, plain text (.txt),
3344 HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
3345 If the extension is .ics, translate visible agenda into iCalendar
3346 format. If the extension is .org, collect all subtrees
3347 corresponding to the agenda entries and add them in an .org file.
3349 With prefix argument OPEN, open the new file immediately. If
3350 NOSETTINGS is given, do not scope the settings of
3351 `org-agenda-exporter-settings' into the export commands. This is
3352 used when the settings have already been scoped and we do not
3353 wish to overrule other, higher priority settings. If
3354 AGENDA-BUFFER-NAME is provided, use this as the buffer name for
3355 the agenda to write."
3356 (interactive "FWrite agenda to file: \nP")
3357 (if (or (not (file-writable-p file))
3358 (and (file-exists-p file)
3359 (if (called-interactively-p 'any)
3360 (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
3361 (user-error "Cannot write agenda to file %s" file))
3362 (org-let (if nosettings nil org-agenda-exporter-settings)
3363 '(save-excursion
3364 (save-window-excursion
3365 (let ((bs (copy-sequence (buffer-string)))
3366 (extension (file-name-extension file))
3367 (default-directory (file-name-directory file))
3368 beg content)
3369 (with-temp-buffer
3370 (rename-buffer org-agenda-write-buffer-name t)
3371 (set-buffer-modified-p nil)
3372 (insert bs)
3373 (org-agenda-remove-marked-text 'invisible 'org-filtered)
3374 (run-hooks 'org-agenda-before-write-hook)
3375 (cond
3376 ((bound-and-true-p org-mobile-creating-agendas)
3377 (org-mobile-write-agenda-for-mobile file))
3378 ((string= "org" extension)
3379 (let (content p m message-log-max)
3380 (goto-char (point-min))
3381 (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
3382 (goto-char p)
3383 (setq m (get-text-property (point) 'org-hd-marker))
3384 (when m
3385 (push (save-excursion
3386 (set-buffer (marker-buffer m))
3387 (goto-char m)
3388 (org-copy-subtree 1 nil t t)
3389 org-subtree-clip)
3390 content)))
3391 (find-file file)
3392 (erase-buffer)
3393 (dolist (s content) (org-paste-subtree 1 s))
3394 (write-file file)
3395 (kill-buffer (current-buffer))
3396 (message "Org file written to %s" file)))
3397 ((member extension '("html" "htm"))
3398 (require 'htmlize)
3399 (set-buffer (htmlize-buffer (current-buffer)))
3400 (when org-agenda-export-html-style
3401 ;; replace <style> section with org-agenda-export-html-style
3402 (goto-char (point-min))
3403 (kill-region (- (search-forward "<style") 6)
3404 (search-forward "</style>"))
3405 (insert org-agenda-export-html-style))
3406 (write-file file)
3407 (kill-buffer (current-buffer))
3408 (message "HTML written to %s" file))
3409 ((string= "ps" extension)
3410 (require 'ps-print)
3411 (ps-print-buffer-with-faces file)
3412 (message "Postscript written to %s" file))
3413 ((string= "pdf" extension)
3414 (require 'ps-print)
3415 (ps-print-buffer-with-faces
3416 (concat (file-name-sans-extension file) ".ps"))
3417 (call-process "ps2pdf" nil nil nil
3418 (expand-file-name
3419 (concat (file-name-sans-extension file) ".ps"))
3420 (expand-file-name file))
3421 (delete-file (concat (file-name-sans-extension file) ".ps"))
3422 (message "PDF written to %s" file))
3423 ((string= "ics" extension)
3424 (require 'ox-icalendar)
3425 (org-icalendar-export-current-agenda (expand-file-name file)))
3427 (let ((bs (buffer-string)))
3428 (find-file file)
3429 (erase-buffer)
3430 (insert bs)
3431 (save-buffer 0)
3432 (kill-buffer (current-buffer))
3433 (message "Plain text written to %s" file))))))))
3434 (set-buffer (or agenda-bufname
3435 (and (called-interactively-p 'any) (buffer-name))
3436 org-agenda-buffer-name)))
3437 (when open (org-open-file file)))
3439 (defun org-agenda-remove-marked-text (property &optional value)
3440 "Delete all text marked with VALUE of PROPERTY.
3441 VALUE defaults to t."
3442 (let (beg)
3443 (setq value (or value t))
3444 (while (setq beg (text-property-any (point-min) (point-max)
3445 property value))
3446 (delete-region
3447 beg (or (next-single-property-change beg property)
3448 (point-max))))))
3450 (defun org-agenda-add-entry-text ()
3451 "Add entry text to agenda lines.
3452 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3453 entry text following headings shown in the agenda.
3454 Drawers will be excluded, also the line with scheduling/deadline info."
3455 (when (and (> org-agenda-add-entry-text-maxlines 0)
3456 (not (bound-and-true-p org-mobile-creating-agendas)))
3457 (let (m txt)
3458 (goto-char (point-min))
3459 (while (not (eobp))
3460 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3461 (beginning-of-line 2)
3462 (setq txt (org-agenda-get-some-entry-text
3463 m org-agenda-add-entry-text-maxlines " > "))
3464 (end-of-line 1)
3465 (if (string-match "\\S-" txt)
3466 (insert "\n" txt)
3467 (or (eobp) (forward-char 1))))))))
3469 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3470 &rest keep)
3471 "Extract entry text from MARKER, at most N-LINES lines.
3472 This will ignore drawers etc, just get the text.
3473 If INDENT is given, prefix every line with this string. If KEEP is
3474 given, it is a list of symbols, defining stuff that should not be
3475 removed from the entry content. Currently only `planning' is allowed here."
3476 (let (txt drawer-re kwd-time-re ind)
3477 (save-excursion
3478 (with-current-buffer (marker-buffer marker)
3479 (if (not (derived-mode-p 'org-mode))
3480 (setq txt "")
3481 (org-with-wide-buffer
3482 (goto-char marker)
3483 (end-of-line 1)
3484 (setq txt (buffer-substring
3485 (min (1+ (point)) (point-max))
3486 (progn (outline-next-heading) (point)))
3487 drawer-re org-drawer-regexp
3488 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3489 ".*\n?"))
3490 (with-temp-buffer
3491 (insert txt)
3492 (when org-agenda-add-entry-text-descriptive-links
3493 (goto-char (point-min))
3494 (while (org-activate-links (point-max))
3495 (add-text-properties (match-beginning 0) (match-end 0)
3496 '(face org-link))))
3497 (goto-char (point-min))
3498 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3499 (set-text-properties (match-beginning 0) (match-end 0)
3500 nil))
3501 (goto-char (point-min))
3502 (while (re-search-forward drawer-re nil t)
3503 (delete-region
3504 (match-beginning 0)
3505 (progn (re-search-forward
3506 "^[ \t]*:END:.*\n?" nil 'move)
3507 (point))))
3508 (unless (member 'planning keep)
3509 (goto-char (point-min))
3510 (while (re-search-forward kwd-time-re nil t)
3511 (replace-match "")))
3512 (goto-char (point-min))
3513 (when org-agenda-entry-text-exclude-regexps
3514 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3515 (while (setq re (pop re-list))
3516 (goto-char (point-min))
3517 (while (re-search-forward re nil t)
3518 (replace-match "")))))
3519 (goto-char (point-max))
3520 (skip-chars-backward " \t\n")
3521 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3523 ;; find and remove min common indentation
3524 (goto-char (point-min))
3525 (untabify (point-min) (point-max))
3526 (setq ind (org-get-indentation))
3527 (while (not (eobp))
3528 (unless (looking-at "[ \t]*$")
3529 (setq ind (min ind (org-get-indentation))))
3530 (beginning-of-line 2))
3531 (goto-char (point-min))
3532 (while (not (eobp))
3533 (unless (looking-at "[ \t]*$")
3534 (move-to-column ind)
3535 (delete-region (point-at-bol) (point)))
3536 (beginning-of-line 2))
3538 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3540 (goto-char (point-min))
3541 (when indent
3542 (while (and (not (eobp)) (re-search-forward "^" nil t))
3543 (replace-match indent t t)))
3544 (goto-char (point-min))
3545 (while (looking-at "[ \t]*\n") (replace-match ""))
3546 (goto-char (point-max))
3547 (when (> (org-current-line)
3548 n-lines)
3549 (org-goto-line (1+ n-lines))
3550 (backward-char 1))
3551 (setq txt (buffer-substring (point-min) (point))))))))
3552 txt))
3554 (defun org-check-for-org-mode ()
3555 "Make sure current buffer is in org-mode. Error if not."
3556 (or (derived-mode-p 'org-mode)
3557 (error "Cannot execute Org agenda command on buffer in %s"
3558 major-mode)))
3560 ;;; Agenda prepare and finalize
3562 (defvar org-agenda-multi nil) ; dynamically scoped
3563 (defvar org-agenda-pre-window-conf nil)
3564 (defvar org-agenda-columns-active nil)
3565 (defvar org-agenda-name nil)
3566 (defvar org-agenda-tag-filter nil)
3567 (defvar org-agenda-category-filter nil)
3568 (defvar org-agenda-regexp-filter nil)
3569 (defvar org-agenda-effort-filter nil)
3570 (defvar org-agenda-top-headline-filter nil)
3571 (defvar org-agenda-tag-filter-preset nil
3572 "A preset of the tags filter used for secondary agenda filtering.
3573 This must be a list of strings, each string must be a single tag preceded
3574 by \"+\" or \"-\".
3575 This variable should not be set directly, but agenda custom commands can
3576 bind it in the options section. The preset filter is a global property of
3577 the entire agenda view. In a block agenda, it will not work reliably to
3578 define a filter for one of the individual blocks. You need to set it in
3579 the global options and expect it to be applied to the entire view.")
3581 (defvar org-agenda-category-filter-preset nil
3582 "A preset of the category filter used for secondary agenda filtering.
3583 This must be a list of strings, each string must be a single category
3584 preceded by \"+\" or \"-\".
3585 This variable should not be set directly, but agenda custom commands can
3586 bind it in the options section. The preset filter is a global property of
3587 the entire agenda view. In a block agenda, it will not work reliably to
3588 define a filter for one of the individual blocks. You need to set it in
3589 the global options and expect it to be applied to the entire view.")
3591 (defvar org-agenda-regexp-filter-preset nil
3592 "A preset of the regexp filter used for secondary agenda filtering.
3593 This must be a list of strings, each string must be a single regexp
3594 preceded by \"+\" or \"-\".
3595 This variable should not be set directly, but agenda custom commands can
3596 bind it in the options section. The preset filter is a global property of
3597 the entire agenda view. In a block agenda, it will not work reliably to
3598 define a filter for one of the individual blocks. You need to set it in
3599 the global options and expect it to be applied to the entire view.")
3601 (defvar org-agenda-effort-filter-preset nil
3602 "A preset of the effort condition used for secondary agenda filtering.
3603 This must be a list of strings, each string must be a single regexp
3604 preceded by \"+\" or \"-\".
3605 This variable should not be set directly, but agenda custom commands can
3606 bind it in the options section. The preset filter is a global property of
3607 the entire agenda view. In a block agenda, it will not work reliably to
3608 define a filter for one of the individual blocks. You need to set it in
3609 the global options and expect it to be applied to the entire view.")
3611 (defun org-agenda-use-sticky-p ()
3612 "Return non-nil if an agenda buffer named
3613 `org-agenda-buffer-name' exists and should be shown instead of
3614 generating a new one."
3615 (and
3616 ;; turned off by user
3617 org-agenda-sticky
3618 ;; For multi-agenda buffer already exists
3619 (not org-agenda-multi)
3620 ;; buffer found
3621 (get-buffer org-agenda-buffer-name)
3622 ;; C-u parameter is same as last call
3623 (with-current-buffer (get-buffer org-agenda-buffer-name)
3624 (and
3625 (equal current-prefix-arg
3626 org-agenda-last-prefix-arg)
3627 ;; In case user turned stickiness on, while having existing
3628 ;; Agenda buffer active, don't reuse that buffer, because it
3629 ;; does not have org variables local
3630 org-agenda-this-buffer-is-sticky))))
3632 (defun org-agenda-prepare-window (abuf filter-alist)
3633 "Setup agenda buffer in the window.
3634 ABUF is the buffer for the agenda window.
3635 FILTER-ALIST is an alist of filters we need to apply when
3636 `org-agenda-persistent-filter' is non-nil."
3637 (let* ((awin (get-buffer-window abuf)) wconf)
3638 (cond
3639 ((equal (current-buffer) abuf) nil)
3640 (awin (select-window awin))
3641 ((not (setq wconf (current-window-configuration))))
3642 ((eq org-agenda-window-setup 'current-window)
3643 (pop-to-buffer-same-window abuf))
3644 ((eq org-agenda-window-setup 'other-window)
3645 (org-switch-to-buffer-other-window abuf))
3646 ((eq org-agenda-window-setup 'other-frame)
3647 (switch-to-buffer-other-frame abuf))
3648 ((eq org-agenda-window-setup 'only-window)
3649 (delete-other-windows)
3650 (pop-to-buffer-same-window abuf))
3651 ((eq org-agenda-window-setup 'reorganize-frame)
3652 (delete-other-windows)
3653 (org-switch-to-buffer-other-window abuf)))
3654 (setq org-agenda-tag-filter (cdr (assq 'tag filter-alist)))
3655 (setq org-agenda-category-filter (cdr (assq 'cat filter-alist)))
3656 (setq org-agenda-effort-filter (cdr (assq 'effort filter-alist)))
3657 (setq org-agenda-regexp-filter (cdr (assq 're filter-alist)))
3658 ;; Additional test in case agenda is invoked from within agenda
3659 ;; buffer via elisp link.
3660 (unless (equal (current-buffer) abuf)
3661 (pop-to-buffer-same-window abuf))
3662 (setq org-agenda-pre-window-conf
3663 (or wconf org-agenda-pre-window-conf))))
3665 (defun org-agenda-prepare (&optional name)
3666 (let ((filter-alist (if org-agenda-persistent-filter
3667 (with-current-buffer
3668 (get-buffer-create org-agenda-buffer-name)
3669 (list `(tag . ,org-agenda-tag-filter)
3670 `(re . ,org-agenda-regexp-filter)
3671 `(effort . ,org-agenda-effort-filter)
3672 `(cat . ,org-agenda-category-filter))))))
3673 (if (org-agenda-use-sticky-p)
3674 (progn
3675 (put 'org-agenda-tag-filter :preset-filter nil)
3676 (put 'org-agenda-category-filter :preset-filter nil)
3677 (put 'org-agenda-regexp-filter :preset-filter nil)
3678 ;; Popup existing buffer
3679 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name)
3680 filter-alist)
3681 (message "Sticky Agenda buffer, use `r' to refresh")
3682 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3683 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3684 (setq org-todo-keywords-for-agenda nil)
3685 (put 'org-agenda-tag-filter :preset-filter
3686 org-agenda-tag-filter-preset)
3687 (put 'org-agenda-category-filter :preset-filter
3688 org-agenda-category-filter-preset)
3689 (put 'org-agenda-regexp-filter :preset-filter
3690 org-agenda-regexp-filter-preset)
3691 (put 'org-agenda-effort-filter :preset-filter
3692 org-agenda-effort-filter-preset)
3693 (if org-agenda-multi
3694 (progn
3695 (setq buffer-read-only nil)
3696 (goto-char (point-max))
3697 (unless (or (bobp) org-agenda-compact-blocks
3698 (not org-agenda-block-separator))
3699 (insert "\n"
3700 (if (stringp org-agenda-block-separator)
3701 org-agenda-block-separator
3702 (make-string (window-width) org-agenda-block-separator))
3703 "\n"))
3704 (narrow-to-region (point) (point-max)))
3705 (setq org-done-keywords-for-agenda nil)
3706 ;; Setting any org variables that are in org-agenda-local-vars
3707 ;; list need to be done after the prepare call
3708 (org-agenda-prepare-window
3709 (get-buffer-create org-agenda-buffer-name) filter-alist)
3710 (setq buffer-read-only nil)
3711 (org-agenda-reset-markers)
3712 (let ((inhibit-read-only t)) (erase-buffer))
3713 (org-agenda-mode)
3714 (setq org-agenda-buffer (current-buffer))
3715 (setq org-agenda-contributing-files nil)
3716 (setq org-agenda-columns-active nil)
3717 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3718 (setq org-todo-keywords-for-agenda
3719 (org-uniquify org-todo-keywords-for-agenda))
3720 (setq org-done-keywords-for-agenda
3721 (org-uniquify org-done-keywords-for-agenda))
3722 (setq org-agenda-last-prefix-arg current-prefix-arg)
3723 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3724 (and name (not org-agenda-name)
3725 (setq-local org-agenda-name name)))
3726 (setq buffer-read-only nil))))
3728 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3729 (defun org-agenda-finalize ()
3730 "Finishing touch for the agenda buffer, called just before displaying it."
3731 (unless org-agenda-multi
3732 (save-excursion
3733 (let ((inhibit-read-only t))
3734 (goto-char (point-min))
3735 (save-excursion
3736 (while (org-activate-links (point-max))
3737 (add-text-properties (match-beginning 0) (match-end 0)
3738 '(face org-link))))
3739 (unless (eq org-agenda-remove-tags t)
3740 (org-agenda-align-tags))
3741 (unless org-agenda-with-colors
3742 (remove-text-properties (point-min) (point-max) '(face nil)))
3743 (if (and (boundp 'org-agenda-overriding-columns-format)
3744 org-agenda-overriding-columns-format)
3745 (setq-local org-agenda-overriding-columns-format
3746 org-agenda-overriding-columns-format))
3747 (if (and (boundp 'org-agenda-view-columns-initially)
3748 org-agenda-view-columns-initially)
3749 (org-agenda-columns))
3750 (when org-agenda-fontify-priorities
3751 (org-agenda-fontify-priorities))
3752 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3753 (org-agenda-dim-blocked-tasks))
3754 (org-agenda-mark-clocking-task)
3755 (when org-agenda-entry-text-mode
3756 (org-agenda-entry-text-hide)
3757 (org-agenda-entry-text-show))
3758 (if (and (functionp 'org-habit-insert-consistency-graphs)
3759 (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
3760 (org-habit-insert-consistency-graphs))
3761 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3762 (unless (or (eq org-agenda-show-inherited-tags 'always)
3763 (and (listp org-agenda-show-inherited-tags)
3764 (memq org-agenda-type org-agenda-show-inherited-tags))
3765 (and (eq org-agenda-show-inherited-tags t)
3766 (or (eq org-agenda-use-tag-inheritance t)
3767 (and (listp org-agenda-use-tag-inheritance)
3768 (not (memq org-agenda-type
3769 org-agenda-use-tag-inheritance))))))
3770 (let (mrk)
3771 (save-excursion
3772 (goto-char (point-min))
3773 (while (equal (forward-line) 0)
3774 (when (setq mrk (get-text-property (point) 'org-hd-marker))
3775 (put-text-property (point-at-bol) (point-at-eol)
3776 'tags (org-with-point-at mrk
3777 (delete-dups
3778 (mapcar 'downcase (org-get-tags-at))))))))))
3779 (run-hooks 'org-agenda-finalize-hook)
3780 (when org-agenda-top-headline-filter
3781 (org-agenda-filter-top-headline-apply
3782 org-agenda-top-headline-filter))
3783 (when org-agenda-tag-filter
3784 (org-agenda-filter-apply org-agenda-tag-filter 'tag t))
3785 (when (get 'org-agenda-tag-filter :preset-filter)
3786 (org-agenda-filter-apply
3787 (get 'org-agenda-tag-filter :preset-filter) 'tag t))
3788 (when org-agenda-category-filter
3789 (org-agenda-filter-apply org-agenda-category-filter 'category))
3790 (when (get 'org-agenda-category-filter :preset-filter)
3791 (org-agenda-filter-apply
3792 (get 'org-agenda-category-filter :preset-filter) 'category))
3793 (when org-agenda-regexp-filter
3794 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
3795 (when (get 'org-agenda-regexp-filter :preset-filter)
3796 (org-agenda-filter-apply
3797 (get 'org-agenda-regexp-filter :preset-filter) 'regexp))
3798 (when org-agenda-effort-filter
3799 (org-agenda-filter-apply org-agenda-effort-filter 'effort))
3800 (when (get 'org-agenda-effort-filter :preset-filter)
3801 (org-agenda-filter-apply
3802 (get 'org-agenda-effort-filter :preset-filter) 'effort))
3803 (add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)))))
3805 (defun org-agenda-mark-clocking-task ()
3806 "Mark the current clock entry in the agenda if it is present."
3807 ;; We need to widen when `org-agenda-finalize' is called from
3808 ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
3809 (when (bound-and-true-p org-clock-current-task)
3810 (save-restriction
3811 (widen)
3812 (org-agenda-unmark-clocking-task)
3813 (when (marker-buffer org-clock-hd-marker)
3814 (save-excursion
3815 (goto-char (point-min))
3816 (let (s ov)
3817 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3818 (goto-char s)
3819 (when (equal (org-get-at-bol 'org-hd-marker)
3820 org-clock-hd-marker)
3821 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3822 (overlay-put ov 'type 'org-agenda-clocking)
3823 (overlay-put ov 'face 'org-agenda-clocking)
3824 (overlay-put ov 'help-echo
3825 "The clock is running in this item")))))))))
3827 (defun org-agenda-unmark-clocking-task ()
3828 "Unmark the current clocking task."
3829 (mapc (lambda (o)
3830 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3831 (delete-overlay o)))
3832 (overlays-in (point-min) (point-max))))
3834 (defun org-agenda-fontify-priorities ()
3835 "Make highest priority lines bold, and lowest italic."
3836 (interactive)
3837 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3838 (delete-overlay o)))
3839 (overlays-in (point-min) (point-max)))
3840 (save-excursion
3841 (let (b e p ov h l)
3842 (goto-char (point-min))
3843 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3844 (setq h (or (get-char-property (point) 'org-highest-priority)
3845 org-highest-priority)
3846 l (or (get-char-property (point) 'org-lowest-priority)
3847 org-lowest-priority)
3848 p (string-to-char (match-string 1))
3849 b (match-beginning 0)
3850 e (if (eq org-agenda-fontify-priorities 'cookies)
3851 (match-end 0)
3852 (point-at-eol))
3853 ov (make-overlay b e))
3854 (overlay-put
3855 ov 'face
3856 (let ((special-face
3857 (cond ((org-face-from-face-or-color
3858 'priority 'org-priority
3859 (cdr (assoc p org-priority-faces))))
3860 ((and (listp org-agenda-fontify-priorities)
3861 (org-face-from-face-or-color
3862 'priority 'org-priority
3863 (cdr (assoc p org-agenda-fontify-priorities)))))
3864 ((equal p l) 'italic)
3865 ((equal p h) 'bold))))
3866 (if special-face (list special-face 'org-priority) 'org-priority)))
3867 (overlay-put ov 'org-type 'org-priority)))))
3869 (defvar org-depend-tag-blocked)
3871 (defun org-agenda-dim-blocked-tasks (&optional invisible)
3872 "Dim currently blocked TODOs in the agenda display.
3873 When INVISIBLE is non-nil, hide currently blocked TODO instead of
3874 dimming them."
3875 (interactive "P")
3876 (when (called-interactively-p 'interactive)
3877 (message "Dim or hide blocked tasks..."))
3878 (dolist (o (overlays-in (point-min) (point-max)))
3879 (when (eq (overlay-get o 'org-type) 'org-blocked-todo)
3880 (delete-overlay o)))
3881 (save-excursion
3882 (let ((inhibit-read-only t)
3883 (org-depend-tag-blocked nil)
3884 org-blocked-by-checkboxes)
3885 (goto-char (point-min))
3886 (while (let ((pos (text-property-not-all
3887 (point) (point-max) 'todo-state nil)))
3888 (when pos (goto-char pos)))
3889 (setq org-blocked-by-checkboxes nil)
3890 (let ((marker (org-get-at-bol 'org-hd-marker)))
3891 (when (and (markerp marker)
3892 (with-current-buffer (marker-buffer marker)
3893 (save-excursion (goto-char marker)
3894 (org-entry-blocked-p))))
3895 ;; Entries blocked by checkboxes cannot be made invisible.
3896 ;; See `org-agenda-dim-blocked-tasks' for details.
3897 (let* ((really-invisible
3898 (and (not org-blocked-by-checkboxes)
3899 (or invisible (eq org-agenda-dim-blocked-tasks
3900 'invisible))))
3901 (ov (make-overlay (if really-invisible (line-end-position 0)
3902 (line-beginning-position))
3903 (line-end-position))))
3904 (if really-invisible (overlay-put ov 'invisible t)
3905 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3906 (overlay-put ov 'org-type 'org-blocked-todo))))
3907 (forward-line))))
3908 (when (called-interactively-p 'interactive)
3909 (message "Dim or hide blocked tasks...done")))
3911 (defvar org-agenda-skip-function nil
3912 "Function to be called at each match during agenda construction.
3913 If this function returns nil, the current match should not be skipped.
3914 Otherwise, the function must return a position from where the search
3915 should be continued.
3916 This may also be a Lisp form, it will be evaluated.
3917 Never set this variable using `setq' or so, because then it will apply
3918 to all future agenda commands. If you do want a global skipping condition,
3919 use the option `org-agenda-skip-function-global' instead.
3920 The correct usage for `org-agenda-skip-function' is to bind it with
3921 `let' to scope it dynamically into the agenda-constructing command.
3922 A good way to set it is through options in `org-agenda-custom-commands'.")
3924 (defun org-agenda-skip ()
3925 "Throw to `:skip' in places that should be skipped.
3926 Also moves point to the end of the skipped region, so that search can
3927 continue from there."
3928 (let ((p (point-at-bol)) to)
3929 (when (or
3930 (save-excursion (goto-char p) (looking-at comment-start-skip))
3931 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3932 (get-text-property p :org-archived)
3933 (org-end-of-subtree t))
3934 (and org-agenda-skip-comment-trees
3935 (get-text-property p :org-comment)
3936 (org-end-of-subtree t))
3937 (and (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3938 (org-agenda-skip-eval org-agenda-skip-function)))
3939 (goto-char to))
3940 (org-in-src-block-p t))
3941 (throw :skip t))))
3943 (defun org-agenda-skip-eval (form)
3944 "If FORM is a function or a list, call (or eval) it and return the result.
3945 `save-excursion' and `save-match-data' are wrapped around the call, so point
3946 and match data are returned to the previous state no matter what these
3947 functions do."
3948 (let (fp)
3949 (and form
3950 (or (setq fp (functionp form))
3951 (consp form))
3952 (save-excursion
3953 (save-match-data
3954 (if fp
3955 (funcall form)
3956 (eval form)))))))
3958 (defvar org-agenda-markers nil
3959 "List of all currently active markers created by `org-agenda'.")
3960 (defvar org-agenda-last-marker-time (float-time)
3961 "Creation time of the last agenda marker.")
3963 (defun org-agenda-new-marker (&optional pos)
3964 "Return a new agenda marker.
3965 Maker is at point, or at POS if non-nil. Org mode keeps a list of
3966 these markers and resets them when they are no longer in use."
3967 (let ((m (copy-marker (or pos (point)) t)))
3968 (setq org-agenda-last-marker-time (float-time))
3969 (if org-agenda-buffer
3970 (with-current-buffer org-agenda-buffer
3971 (push m org-agenda-markers))
3972 (push m org-agenda-markers))
3975 (defun org-agenda-reset-markers ()
3976 "Reset markers created by `org-agenda'."
3977 (while org-agenda-markers
3978 (move-marker (pop org-agenda-markers) nil)))
3980 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3981 "Save relative positions of markers in region.
3982 This check for agenda markers in all agenda buffers currently active."
3983 (dolist (buf (buffer-list))
3984 (with-current-buffer buf
3985 (when (eq major-mode 'org-agenda-mode)
3986 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3987 org-agenda-markers)))))
3989 ;;; Entry text mode
3991 (defun org-agenda-entry-text-show-here ()
3992 "Add some text from the entry as context to the current line."
3993 (let (m txt o)
3994 (setq m (org-get-at-bol 'org-hd-marker))
3995 (unless (marker-buffer m)
3996 (error "No marker points to an entry here"))
3997 (setq txt (concat "\n" (org-no-properties
3998 (org-agenda-get-some-entry-text
3999 m org-agenda-entry-text-maxlines
4000 org-agenda-entry-text-leaders))))
4001 (when (string-match "\\S-" txt)
4002 (setq o (make-overlay (point-at-bol) (point-at-eol)))
4003 (overlay-put o 'evaporate t)
4004 (overlay-put o 'org-overlay-type 'agenda-entry-content)
4005 (overlay-put o 'after-string txt))))
4007 (defun org-agenda-entry-text-show ()
4008 "Add entry context for all agenda lines."
4009 (interactive)
4010 (save-excursion
4011 (goto-char (point-max))
4012 (beginning-of-line 1)
4013 (while (not (bobp))
4014 (when (org-get-at-bol 'org-hd-marker)
4015 (org-agenda-entry-text-show-here))
4016 (beginning-of-line 0))))
4018 (defun org-agenda-entry-text-hide ()
4019 "Remove any shown entry context."
4020 (delq nil
4021 (mapcar (lambda (o)
4022 (if (eq (overlay-get o 'org-overlay-type)
4023 'agenda-entry-content)
4024 (progn (delete-overlay o) t)))
4025 (overlays-in (point-min) (point-max)))))
4027 (defun org-agenda-get-day-face (date)
4028 "Return the face DATE should be displayed with."
4029 (cond ((and (functionp org-agenda-day-face-function)
4030 (funcall org-agenda-day-face-function date)))
4031 ((org-agenda-today-p date) 'org-agenda-date-today)
4032 ((memq (calendar-day-of-week date) org-agenda-weekend-days)
4033 'org-agenda-date-weekend)
4034 (t 'org-agenda-date)))
4036 (defvar org-agenda-show-log-scoped)
4038 ;;; Agenda Daily/Weekly
4040 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4041 "Start day for the agenda view.
4042 Custom commands can set this variable in the options section.
4043 This is usually a string like \"2007-11-01\", \"+2d\" or any other
4044 input allowed when reading a date through the Org calendar.
4045 See the docstring of `org-read-date' for details.")
4046 (defvar org-starting-day nil) ; local variable in the agenda buffer
4047 (defvar org-arg-loc nil) ; local variable
4049 (defvar org-agenda-buffer-tmp-name nil)
4050 ;;;###autoload
4051 (defun org-agenda-list (&optional arg start-day span with-hour)
4052 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4053 The view will be for the current day or week, but from the overview buffer
4054 you will be able to go to other days/weeks.
4056 With a numeric prefix argument in an interactive call, the agenda will
4057 span ARG days. Lisp programs should instead specify SPAN to change
4058 the number of days. SPAN defaults to `org-agenda-span'.
4060 START-DAY defaults to TODAY, or to the most recent match for the weekday
4061 given in `org-agenda-start-on-weekday'.
4063 When WITH-HOUR is non-nil, only include scheduled and deadline
4064 items if they have an hour specification like [h]h:mm."
4065 (interactive "P")
4066 (if org-agenda-overriding-arguments
4067 (setq arg (car org-agenda-overriding-arguments)
4068 start-day (nth 1 org-agenda-overriding-arguments)
4069 span (nth 2 org-agenda-overriding-arguments)))
4070 (if (and (integerp arg) (> arg 0))
4071 (setq span arg arg nil))
4072 (catch 'exit
4073 (setq org-agenda-buffer-name
4074 (or org-agenda-buffer-tmp-name
4075 (and org-agenda-doing-sticky-redo org-agenda-buffer-name)
4076 (if org-agenda-sticky
4077 (cond ((and org-keys (stringp org-match))
4078 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4079 (org-keys
4080 (format "*Org Agenda(%s)*" org-keys))
4081 (t "*Org Agenda(a)*")))
4082 "*Org Agenda*"))
4083 (org-agenda-prepare "Day/Week")
4084 (setq start-day (or start-day org-agenda-start-day))
4085 (if (stringp start-day)
4086 ;; Convert to an absolute day number
4087 (setq start-day (time-to-days (org-read-date nil t start-day))))
4088 (org-compile-prefix-format 'agenda)
4089 (org-set-sorting-strategy 'agenda)
4090 (let* ((span (org-agenda-ndays-to-span (or span org-agenda-span)))
4091 (today (org-today))
4092 (sd (or start-day today))
4093 (ndays (org-agenda-span-to-ndays span sd))
4094 (org-agenda-start-on-weekday
4095 (if (or (eq ndays 7) (eq ndays 14))
4096 org-agenda-start-on-weekday))
4097 (thefiles (org-agenda-files nil 'ifmode))
4098 (files thefiles)
4099 (start (if (or (null org-agenda-start-on-weekday)
4100 (< ndays 7))
4102 (let* ((nt (calendar-day-of-week
4103 (calendar-gregorian-from-absolute sd)))
4104 (n1 org-agenda-start-on-weekday)
4105 (d (- nt n1)))
4106 (- sd (+ (if (< d 0) 7 0) d)))))
4107 (day-numbers (list start))
4108 (day-cnt 0)
4109 (inhibit-redisplay (not debug-on-error))
4110 (org-agenda-show-log-scoped org-agenda-show-log)
4111 s e rtn rtnall file date d start-pos end-pos todayp
4112 clocktable-start clocktable-end filter)
4113 (setq org-agenda-redo-command
4114 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4115 (dotimes (n (1- ndays))
4116 (push (1+ (car day-numbers)) day-numbers))
4117 (setq day-numbers (nreverse day-numbers))
4118 (setq clocktable-start (car day-numbers)
4119 clocktable-end (1+ (or (org-last day-numbers) 0)))
4120 (setq-local org-starting-day (car day-numbers))
4121 (setq-local org-arg-loc arg)
4122 (setq-local org-agenda-current-span (org-agenda-ndays-to-span span))
4123 (unless org-agenda-compact-blocks
4124 (let* ((d1 (car day-numbers))
4125 (d2 (org-last day-numbers))
4126 (w1 (org-days-to-iso-week d1))
4127 (w2 (org-days-to-iso-week d2)))
4128 (setq s (point))
4129 (if org-agenda-overriding-header
4130 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4131 nil 'face 'org-agenda-structure) "\n")
4132 (insert (org-agenda-span-name span)
4133 "-agenda"
4134 (if (< (- d2 d1) 350)
4135 (if (= w1 w2)
4136 (format " (W%02d)" w1)
4137 (format " (W%02d-W%02d)" w1 w2))
4139 ":\n")))
4140 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4141 'org-date-line t))
4142 (org-agenda-mark-header-line s))
4143 (while (setq d (pop day-numbers))
4144 (setq date (calendar-gregorian-from-absolute d)
4145 s (point))
4146 (if (or (setq todayp (= d today))
4147 (and (not start-pos) (= d sd)))
4148 (setq start-pos (point))
4149 (if (and start-pos (not end-pos))
4150 (setq end-pos (point))))
4151 (setq files thefiles
4152 rtnall nil)
4153 (while (setq file (pop files))
4154 (catch 'nextfile
4155 (org-check-agenda-file file)
4156 (let ((org-agenda-entry-types org-agenda-entry-types))
4157 ;; Starred types override non-starred equivalents
4158 (when (member :deadline* org-agenda-entry-types)
4159 (setq org-agenda-entry-types
4160 (delq :deadline org-agenda-entry-types)))
4161 (when (member :scheduled* org-agenda-entry-types)
4162 (setq org-agenda-entry-types
4163 (delq :scheduled org-agenda-entry-types)))
4164 ;; Honor with-hour
4165 (when with-hour
4166 (when (member :deadline org-agenda-entry-types)
4167 (setq org-agenda-entry-types
4168 (delq :deadline org-agenda-entry-types))
4169 (push :deadline* org-agenda-entry-types))
4170 (when (member :scheduled org-agenda-entry-types)
4171 (setq org-agenda-entry-types
4172 (delq :scheduled org-agenda-entry-types))
4173 (push :scheduled* org-agenda-entry-types)))
4174 (unless org-agenda-include-deadlines
4175 (setq org-agenda-entry-types
4176 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4177 (cond
4178 ((memq org-agenda-show-log-scoped '(only clockcheck))
4179 (setq rtn (org-agenda-get-day-entries
4180 file date :closed)))
4181 (org-agenda-show-log-scoped
4182 (setq rtn (apply 'org-agenda-get-day-entries
4183 file date
4184 (append '(:closed) org-agenda-entry-types))))
4186 (setq rtn (apply 'org-agenda-get-day-entries
4187 file date
4188 org-agenda-entry-types)))))
4189 (setq rtnall (append rtnall rtn)))) ;; all entries
4190 (if org-agenda-include-diary
4191 (let ((org-agenda-search-headline-for-time t))
4192 (require 'diary-lib)
4193 (setq rtn (org-get-entries-from-diary date))
4194 (setq rtnall (append rtnall rtn))))
4195 (if (or rtnall org-agenda-show-all-dates)
4196 (progn
4197 (setq day-cnt (1+ day-cnt))
4198 (insert
4199 (if (stringp org-agenda-format-date)
4200 (format-time-string org-agenda-format-date
4201 (org-time-from-absolute date))
4202 (funcall org-agenda-format-date date))
4203 "\n")
4204 (put-text-property s (1- (point)) 'face
4205 (org-agenda-get-day-face date))
4206 (put-text-property s (1- (point)) 'org-date-line t)
4207 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4208 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4209 (when todayp
4210 (put-text-property s (1- (point)) 'org-today t))
4211 (setq rtnall
4212 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4213 (if rtnall (insert ;; all entries
4214 (org-agenda-finalize-entries rtnall 'agenda)
4215 "\n"))
4216 (put-text-property s (1- (point)) 'day d)
4217 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4218 (when (and org-agenda-clockreport-mode clocktable-start)
4219 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4220 ;; the above line is to ensure the restricted range!
4221 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4222 tbl)
4223 (setq p (org-plist-delete p :block))
4224 (setq p (plist-put p :tstart clocktable-start))
4225 (setq p (plist-put p :tend clocktable-end))
4226 (setq p (plist-put p :scope 'agenda))
4227 (setq tbl (apply 'org-clock-get-clocktable p))
4228 (insert tbl)))
4229 (goto-char (point-min))
4230 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4231 (unless (and (pos-visible-in-window-p (point-min))
4232 (pos-visible-in-window-p (point-max)))
4233 (goto-char (1- (point-max)))
4234 (recenter -1)
4235 (if (not (pos-visible-in-window-p (or start-pos 1)))
4236 (progn
4237 (goto-char (or start-pos 1))
4238 (recenter 1))))
4239 (goto-char (or start-pos 1))
4240 (add-text-properties (point-min) (point-max)
4241 `(org-agenda-type agenda
4242 org-last-args (,arg ,start-day ,span)
4243 org-redo-cmd ,org-agenda-redo-command
4244 org-series-cmd ,org-cmd))
4245 (if (eq org-agenda-show-log-scoped 'clockcheck)
4246 (org-agenda-show-clocking-issues))
4247 (org-agenda-finalize)
4248 (setq buffer-read-only t)
4249 (message ""))))
4251 (defun org-agenda-ndays-to-span (n)
4252 "Return a span symbol for a span of N days, or N if none matches."
4253 (cond ((symbolp n) n)
4254 ((= n 1) 'day)
4255 ((= n 7) 'week)
4256 ((= n 14) 'fortnight)
4257 (t n)))
4259 (defun org-agenda-span-to-ndays (span &optional start-day)
4260 "Return ndays from SPAN, possibly starting at START-DAY.
4261 START-DAY is an absolute time value."
4262 (cond ((numberp span) span)
4263 ((eq span 'day) 1)
4264 ((eq span 'week) 7)
4265 ((eq span 'fortnight) 14)
4266 ((eq span 'month)
4267 (let ((date (calendar-gregorian-from-absolute start-day)))
4268 (calendar-last-day-of-month (car date) (cl-caddr date))))
4269 ((eq span 'year)
4270 (let ((date (calendar-gregorian-from-absolute start-day)))
4271 (if (calendar-leap-year-p (cl-caddr date)) 366 365)))))
4273 (defun org-agenda-span-name (span)
4274 "Return a SPAN name."
4275 (if (null span)
4277 (if (symbolp span)
4278 (capitalize (symbol-name span))
4279 (format "%d days" span))))
4281 ;;; Agenda word search
4283 (defvar org-agenda-search-history nil)
4285 (defvar org-search-syntax-table nil
4286 "Special syntax table for Org search.
4287 In this table, we have single quotes not as word constituents, to
4288 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4290 (defvar org-mode-syntax-table) ; From org.el
4291 (defun org-search-syntax-table ()
4292 (unless org-search-syntax-table
4293 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4294 (modify-syntax-entry ?' "." org-search-syntax-table)
4295 (modify-syntax-entry ?` "." org-search-syntax-table))
4296 org-search-syntax-table)
4298 (defvar org-agenda-last-search-view-search-was-boolean nil)
4300 ;;;###autoload
4301 (defun org-search-view (&optional todo-only string edit-at)
4302 "Show all entries that contain a phrase or words or regular expressions.
4304 With optional prefix argument TODO-ONLY, only consider entries that are
4305 TODO entries. The argument STRING can be used to pass a default search
4306 string into this function. If EDIT-AT is non-nil, it means that the
4307 user should get a chance to edit this string, with cursor at position
4308 EDIT-AT.
4310 The search string can be viewed either as a phrase that should be found as
4311 is, or it can be broken into a number of snippets, each of which must match
4312 in a Boolean way to select an entry. The default depends on the variable
4313 `org-agenda-search-view-always-boolean'.
4314 Even if this is turned off (the default) you can always switch to
4315 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4317 The default is a direct search of the whole phrase, where each space in
4318 the search string can expand to an arbitrary amount of whitespace,
4319 including newlines.
4321 If using a Boolean search, the search string is split on whitespace and
4322 each snippet is searched separately, with logical AND to select an entry.
4323 Words prefixed with a minus must *not* occur in the entry. Words without
4324 a prefix or prefixed with a plus must occur in the entry. Matching is
4325 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4326 match whole words, not parts of a word) if
4327 `org-agenda-search-view-force-full-words' is set (default is nil).
4329 Boolean search snippets enclosed by curly braces are interpreted as
4330 regular expressions that must or (when preceded with \"-\") must not
4331 match in the entry. Snippets enclosed into double quotes will be taken
4332 as a whole, to include whitespace.
4334 - If the search string starts with an asterisk, search only in headlines.
4335 - If (possibly after the leading star) the search string starts with an
4336 exclamation mark, this also means to look at TODO entries only, an effect
4337 that can also be achieved with a prefix argument.
4338 - If (possibly after star and exclamation mark) the search string starts
4339 with a colon, this will mean that the (non-regexp) snippets of the
4340 Boolean search must match as full words.
4342 This command searches the agenda files, and in addition the files listed
4343 in `org-agenda-text-search-extra-files'."
4344 (interactive "P")
4345 (if org-agenda-overriding-arguments
4346 (setq todo-only (car org-agenda-overriding-arguments)
4347 string (nth 1 org-agenda-overriding-arguments)
4348 edit-at (nth 2 org-agenda-overriding-arguments)))
4349 (let* ((props (list 'face nil
4350 'done-face 'org-agenda-done
4351 'org-not-done-regexp org-not-done-regexp
4352 'org-todo-regexp org-todo-regexp
4353 'org-complex-heading-regexp org-complex-heading-regexp
4354 'mouse-face 'highlight
4355 'help-echo (format "mouse-2 or RET jump to location")))
4356 (full-words org-agenda-search-view-force-full-words)
4357 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4358 regexp rtn rtnall files file pos inherited-tags
4359 marker category level tags c neg re boolean
4360 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4361 (unless (and (not edit-at)
4362 (stringp string)
4363 (string-match "\\S-" string))
4364 (setq string (read-string
4365 (if org-agenda-search-view-always-boolean
4366 "[+-]Word/{Regexp} ...: "
4367 "Phrase or [+-]Word/{Regexp} ...: ")
4368 (cond
4369 ((integerp edit-at) (cons string edit-at))
4370 (edit-at string))
4371 'org-agenda-search-history)))
4372 (catch 'exit
4373 (if org-agenda-sticky
4374 (setq org-agenda-buffer-name
4375 (if (stringp string)
4376 (format "*Org Agenda(%s:%s)*"
4377 (or org-keys (or (and todo-only "S") "s")) string)
4378 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4379 (org-agenda-prepare "SEARCH")
4380 (org-compile-prefix-format 'search)
4381 (org-set-sorting-strategy 'search)
4382 (setq org-agenda-redo-command
4383 (list 'org-search-view (if todo-only t nil)
4384 (list 'if 'current-prefix-arg nil string)))
4385 (setq org-agenda-query-string string)
4386 (if (equal (string-to-char string) ?*)
4387 (setq hdl-only t
4388 words (substring string 1))
4389 (setq words string))
4390 (when (equal (string-to-char words) ?!)
4391 (setq todo-only t
4392 words (substring words 1)))
4393 (when (equal (string-to-char words) ?:)
4394 (setq full-words t
4395 words (substring words 1)))
4396 (if (or org-agenda-search-view-always-boolean
4397 (member (string-to-char words) '(?- ?+ ?\{)))
4398 (setq boolean t))
4399 (setq words (org-split-string words))
4400 (let (www w)
4401 (while (setq w (pop words))
4402 (while (and (string-match "\\\\\\'" w) words)
4403 (setq w (concat (substring w 0 -1) " " (pop words))))
4404 (push w www))
4405 (setq words (nreverse www) www nil)
4406 (while (setq w (pop words))
4407 (when (and (string-match "\\`[-+]?{" w)
4408 (not (string-match "}\\'" w)))
4409 (while (and words (not (string-match "}\\'" (car words))))
4410 (setq w (concat w " " (pop words))))
4411 (setq w (concat w " " (pop words))))
4412 (push w www))
4413 (setq words (nreverse www)))
4414 (setq org-agenda-last-search-view-search-was-boolean boolean)
4415 (when boolean
4416 (let (wds w)
4417 (while (setq w (pop words))
4418 (if (or (equal (substring w 0 1) "\"")
4419 (and (> (length w) 1)
4420 (member (substring w 0 1) '("+" "-"))
4421 (equal (substring w 1 2) "\"")))
4422 (while (and words (not (equal (substring w -1) "\"")))
4423 (setq w (concat w " " (pop words)))))
4424 (and (string-match "\\`\\([-+]?\\)\"" w)
4425 (setq w (replace-match "\\1" nil nil w)))
4426 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4427 (push w wds))
4428 (setq words (nreverse wds))))
4429 (if boolean
4430 (mapc (lambda (w)
4431 (setq c (string-to-char w))
4432 (if (equal c ?-)
4433 (setq neg t w (substring w 1))
4434 (if (equal c ?+)
4435 (setq neg nil w (substring w 1))
4436 (setq neg nil)))
4437 (if (string-match "\\`{.*}\\'" w)
4438 (setq re (substring w 1 -1))
4439 (if full-words
4440 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4441 (setq re (regexp-quote (downcase w)))))
4442 (if neg (push re regexps-) (push re regexps+)))
4443 words)
4444 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4445 regexps+))
4446 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4447 (if (not regexps+)
4448 (setq regexp org-outline-regexp-bol)
4449 (setq regexp (pop regexps+))
4450 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4451 regexp))))
4452 (setq files (org-agenda-files nil 'ifmode))
4453 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4454 (pop org-agenda-text-search-extra-files)
4455 (setq files (org-add-archive-files files)))
4456 (setq files (append files org-agenda-text-search-extra-files)
4457 rtnall nil)
4458 (while (setq file (pop files))
4459 (setq ee nil)
4460 (catch 'nextfile
4461 (org-check-agenda-file file)
4462 (setq buffer (if (file-exists-p file)
4463 (org-get-agenda-file-buffer file)
4464 (error "No such file %s" file)))
4465 (if (not buffer)
4466 ;; If file does not exist, make sure an error message is sent
4467 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4468 file))))
4469 (with-current-buffer buffer
4470 (with-syntax-table (org-search-syntax-table)
4471 (unless (derived-mode-p 'org-mode)
4472 (error "Agenda file %s is not in `org-mode'" file))
4473 (let ((case-fold-search t))
4474 (save-excursion
4475 (save-restriction
4476 (if (eq buffer org-agenda-restrict)
4477 (narrow-to-region org-agenda-restrict-begin
4478 org-agenda-restrict-end)
4479 (widen))
4480 (goto-char (point-min))
4481 (unless (or (org-at-heading-p)
4482 (outline-next-heading))
4483 (throw 'nextfile t))
4484 (goto-char (max (point-min) (1- (point))))
4485 (while (re-search-forward regexp nil t)
4486 (org-back-to-heading t)
4487 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4488 (> (org-reduced-level (org-outline-level))
4489 org-agenda-search-view-max-outline-level)
4490 (forward-line -1)
4491 (org-back-to-heading t)))
4492 (skip-chars-forward "* ")
4493 (setq beg (point-at-bol)
4494 beg1 (point)
4495 end (progn
4496 (outline-next-heading)
4497 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4498 (> (org-reduced-level (org-outline-level))
4499 org-agenda-search-view-max-outline-level)
4500 (forward-line 1)
4501 (outline-next-heading)))
4502 (point)))
4504 (catch :skip
4505 (goto-char beg)
4506 (org-agenda-skip)
4507 (setq str (buffer-substring-no-properties
4508 (point-at-bol)
4509 (if hdl-only (point-at-eol) end)))
4510 (mapc (lambda (wr) (when (string-match wr str)
4511 (goto-char (1- end))
4512 (throw :skip t)))
4513 regexps-)
4514 (mapc (lambda (wr) (unless (string-match wr str)
4515 (goto-char (1- end))
4516 (throw :skip t)))
4517 (if todo-only
4518 (cons (concat "^\\*+[ \t]+"
4519 org-not-done-regexp)
4520 regexps+)
4521 regexps+))
4522 (goto-char beg)
4523 (setq marker (org-agenda-new-marker (point))
4524 category (org-get-category)
4525 level (make-string (org-reduced-level (org-outline-level)) ? )
4526 inherited-tags
4527 (or (eq org-agenda-show-inherited-tags 'always)
4528 (and (listp org-agenda-show-inherited-tags)
4529 (memq 'todo org-agenda-show-inherited-tags))
4530 (and (eq org-agenda-show-inherited-tags t)
4531 (or (eq org-agenda-use-tag-inheritance t)
4532 (memq 'todo org-agenda-use-tag-inheritance))))
4533 tags (org-get-tags-at nil (not inherited-tags))
4534 txt (org-agenda-format-item
4536 (buffer-substring-no-properties
4537 beg1 (point-at-eol))
4538 level category tags t))
4539 (org-add-props txt props
4540 'org-marker marker 'org-hd-marker marker
4541 'org-todo-regexp org-todo-regexp
4542 'level level
4543 'org-complex-heading-regexp org-complex-heading-regexp
4544 'priority 1000
4545 'type "search")
4546 (push txt ee)
4547 (goto-char (1- end))))))))))
4548 (setq rtn (nreverse ee))
4549 (setq rtnall (append rtnall rtn)))
4550 (if org-agenda-overriding-header
4551 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4552 nil 'face 'org-agenda-structure) "\n")
4553 (insert "Search words: ")
4554 (add-text-properties (point-min) (1- (point))
4555 (list 'face 'org-agenda-structure))
4556 (setq pos (point))
4557 (insert string "\n")
4558 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4559 (setq pos (point))
4560 (unless org-agenda-multi
4561 (insert (substitute-command-keys "\
4562 Press `\\[org-agenda-manipulate-query-add]', \
4563 `\\[org-agenda-manipulate-query-subtract]' to add/sub word, \
4564 `\\[org-agenda-manipulate-query-add-re]', \
4565 `\\[org-agenda-manipulate-query-subtract-re]' to add/sub regexp, \
4566 `\\[universal-argument] \\[org-agenda-redo]' to edit\n"))
4567 (add-text-properties pos (1- (point))
4568 (list 'face 'org-agenda-structure))))
4569 (org-agenda-mark-header-line (point-min))
4570 (when rtnall
4571 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4572 (goto-char (point-min))
4573 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4574 (add-text-properties (point-min) (point-max)
4575 `(org-agenda-type search
4576 org-last-args (,todo-only ,string ,edit-at)
4577 org-redo-cmd ,org-agenda-redo-command
4578 org-series-cmd ,org-cmd))
4579 (org-agenda-finalize)
4580 (setq buffer-read-only t))))
4582 ;;; Agenda TODO list
4584 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4585 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4586 (concat
4587 (if (or (equal keywords "ALL") (not keywords))
4588 (propertize "ALL" 'face 'warning)
4589 (mapconcat
4590 (lambda (kw)
4591 (propertize kw 'face (org-get-todo-face kw)))
4592 (org-split-string keywords "|")
4593 "|"))
4594 "\n"))
4596 (defvar org-select-this-todo-keyword nil)
4597 (defvar org-last-arg nil)
4599 ;;;###autoload
4600 (defun org-todo-list (&optional arg)
4601 "Show all (not done) TODO entries from all agenda file in a single list.
4602 The prefix arg can be used to select a specific TODO keyword and limit
4603 the list to these. When using `\\[universal-argument]', you will be prompted
4604 for a keyword. A numeric prefix directly selects the Nth keyword in
4605 `org-todo-keywords-1'."
4606 (interactive "P")
4607 (if org-agenda-overriding-arguments
4608 (setq arg org-agenda-overriding-arguments))
4609 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4610 (let* ((today (org-today))
4611 (date (calendar-gregorian-from-absolute today))
4612 (kwds org-todo-keywords-for-agenda)
4613 (completion-ignore-case t)
4614 (org-select-this-todo-keyword
4615 (if (stringp arg) arg
4616 (and arg (integerp arg) (> arg 0)
4617 (nth (1- arg) kwds))))
4618 rtn rtnall files file pos)
4619 (when (equal arg '(4))
4620 (setq org-select-this-todo-keyword
4621 (completing-read "Keyword (or KWD1|K2D2|...): "
4622 (mapcar #'list kwds) nil nil)))
4623 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4624 (catch 'exit
4625 (if org-agenda-sticky
4626 (setq org-agenda-buffer-name
4627 (if (stringp org-select-this-todo-keyword)
4628 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4629 org-select-this-todo-keyword)
4630 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4631 (org-agenda-prepare "TODO")
4632 (org-compile-prefix-format 'todo)
4633 (org-set-sorting-strategy 'todo)
4634 (setq org-agenda-redo-command
4635 `(org-todo-list (or (and (numberp current-prefix-arg)
4636 current-prefix-arg)
4637 ,org-select-this-todo-keyword
4638 current-prefix-arg ,arg)))
4639 (setq files (org-agenda-files nil 'ifmode)
4640 rtnall nil)
4641 (while (setq file (pop files))
4642 (catch 'nextfile
4643 (org-check-agenda-file file)
4644 (setq rtn (org-agenda-get-day-entries file date :todo))
4645 (setq rtnall (append rtnall rtn))))
4646 (if org-agenda-overriding-header
4647 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4648 nil 'face 'org-agenda-structure) "\n")
4649 (insert "Global list of TODO items of type: ")
4650 (add-text-properties (point-min) (1- (point))
4651 (list 'face 'org-agenda-structure
4652 'short-heading
4653 (concat "ToDo: "
4654 (or org-select-this-todo-keyword "ALL"))))
4655 (org-agenda-mark-header-line (point-min))
4656 (insert (org-agenda-propertize-selected-todo-keywords
4657 org-select-this-todo-keyword))
4658 (setq pos (point))
4659 (unless org-agenda-multi
4660 (insert (substitute-command-keys "Available with \
4661 `N \\[org-agenda-redo]': (0)[ALL]"))
4662 (let ((n 0) s)
4663 (mapc (lambda (x)
4664 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4665 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4666 (insert "\n "))
4667 (insert " " s))
4668 kwds))
4669 (insert "\n"))
4670 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4671 (org-agenda-mark-header-line (point-min))
4672 (when rtnall
4673 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4674 (goto-char (point-min))
4675 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4676 (add-text-properties (point-min) (point-max)
4677 `(org-agenda-type todo
4678 org-last-args ,arg
4679 org-redo-cmd ,org-agenda-redo-command
4680 org-series-cmd ,org-cmd))
4681 (org-agenda-finalize)
4682 (setq buffer-read-only t))))
4684 ;;; Agenda tags match
4686 ;;;###autoload
4687 (defun org-tags-view (&optional todo-only match)
4688 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4689 The prefix arg TODO-ONLY limits the search to TODO entries."
4690 (interactive "P")
4691 (if org-agenda-overriding-arguments
4692 (setq todo-only (car org-agenda-overriding-arguments)
4693 match (nth 1 org-agenda-overriding-arguments)))
4694 (let* ((org-tags-match-list-sublevels
4695 org-tags-match-list-sublevels)
4696 (completion-ignore-case t)
4697 (org--matcher-tags-todo-only todo-only)
4698 rtn rtnall files file pos matcher
4699 buffer)
4700 (when (and (stringp match) (not (string-match "\\S-" match)))
4701 (setq match nil))
4702 (catch 'exit
4703 (if org-agenda-sticky
4704 (setq org-agenda-buffer-name
4705 (if (stringp match)
4706 (format "*Org Agenda(%s:%s)*"
4707 (or org-keys (or (and todo-only "M") "m")) match)
4708 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4709 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4710 ;; expanding tags within `org-make-tags-matcher'
4711 (org-agenda-prepare (concat "TAGS " match))
4712 (setq matcher (org-make-tags-matcher match)
4713 match (car matcher)
4714 matcher (cdr matcher))
4715 (org-compile-prefix-format 'tags)
4716 (org-set-sorting-strategy 'tags)
4717 (setq org-agenda-query-string match)
4718 (setq org-agenda-redo-command
4719 (list 'org-tags-view
4720 `(quote ,org--matcher-tags-todo-only)
4721 `(if current-prefix-arg nil ,org-agenda-query-string)))
4722 (setq files (org-agenda-files nil 'ifmode)
4723 rtnall nil)
4724 (while (setq file (pop files))
4725 (catch 'nextfile
4726 (org-check-agenda-file file)
4727 (setq buffer (if (file-exists-p file)
4728 (org-get-agenda-file-buffer file)
4729 (error "No such file %s" file)))
4730 (if (not buffer)
4731 ;; If file does not exist, error message to agenda
4732 (setq rtn (list
4733 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4734 rtnall (append rtnall rtn))
4735 (with-current-buffer buffer
4736 (unless (derived-mode-p 'org-mode)
4737 (error "Agenda file %s is not in `org-mode'" file))
4738 (save-excursion
4739 (save-restriction
4740 (if (eq buffer org-agenda-restrict)
4741 (narrow-to-region org-agenda-restrict-begin
4742 org-agenda-restrict-end)
4743 (widen))
4744 (setq rtn (org-scan-tags 'agenda
4745 matcher
4746 org--matcher-tags-todo-only))
4747 (setq rtnall (append rtnall rtn))))))))
4748 (if org-agenda-overriding-header
4749 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4750 nil 'face 'org-agenda-structure) "\n")
4751 (insert "Headlines with TAGS match: ")
4752 (add-text-properties (point-min) (1- (point))
4753 (list 'face 'org-agenda-structure
4754 'short-heading
4755 (concat "Match: " match)))
4756 (setq pos (point))
4757 (insert match "\n")
4758 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4759 (setq pos (point))
4760 (unless org-agenda-multi
4761 (insert (substitute-command-keys
4762 "Press `\\[universal-argument] \\[org-agenda-redo]' \
4763 to search again with new search string\n")))
4764 (add-text-properties pos (1- (point))
4765 (list 'face 'org-agenda-structure)))
4766 (org-agenda-mark-header-line (point-min))
4767 (when rtnall
4768 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4769 (goto-char (point-min))
4770 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4771 (add-text-properties
4772 (point-min) (point-max)
4773 `(org-agenda-type tags
4774 org-last-args (,org--matcher-tags-todo-only ,match)
4775 org-redo-cmd ,org-agenda-redo-command
4776 org-series-cmd ,org-cmd))
4777 (org-agenda-finalize)
4778 (setq buffer-read-only t))))
4780 ;;; Agenda Finding stuck projects
4782 (defvar org-agenda-skip-regexp nil
4783 "Regular expression used in skipping subtrees for the agenda.
4784 This is basically a temporary global variable that can be set and then
4785 used by user-defined selections using `org-agenda-skip-function'.")
4787 (defvar org-agenda-overriding-header nil
4788 "When set during agenda, todo and tags searches it replaces the header.
4789 This variable should not be set directly, but custom commands can bind it
4790 in the options section.")
4792 (defun org-agenda-skip-entry-if (&rest conditions)
4793 "Skip entry if any of CONDITIONS is true.
4794 See `org-agenda-skip-if' for details."
4795 (org-agenda-skip-if nil conditions))
4797 (defun org-agenda-skip-subtree-if (&rest conditions)
4798 "Skip subtree if any of CONDITIONS is true.
4799 See `org-agenda-skip-if' for details."
4800 (org-agenda-skip-if t conditions))
4802 (defun org-agenda-skip-if (subtree conditions)
4803 "Checks current entity for CONDITIONS.
4804 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4805 the entry (i.e. the text before the next heading) is checked.
4807 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4808 from different tests. Valid conditions are:
4810 scheduled Check if there is a scheduled cookie
4811 notscheduled Check if there is no scheduled cookie
4812 deadline Check if there is a deadline
4813 notdeadline Check if there is no deadline
4814 timestamp Check if there is a timestamp (also deadline or scheduled)
4815 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4816 regexp Check if regexp matches
4817 notregexp Check if regexp does not match.
4818 todo Check if TODO keyword matches
4819 nottodo Check if TODO keyword does not match
4821 The regexp is taken from the conditions list, it must come right after
4822 the `regexp' or `notregexp' element.
4824 `todo' and `nottodo' accept as an argument a list of todo
4825 keywords, which may include \"*\" to match any todo keyword.
4827 (org-agenda-skip-entry-if \\='todo \\='(\"TODO\" \"WAITING\"))
4829 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4831 Instead of a list, a keyword class may be given. For example:
4833 (org-agenda-skip-entry-if \\='nottodo \\='done)
4835 would skip entries that haven't been marked with any of \"DONE\"
4836 keywords. Possible classes are: `todo', `done', `any'.
4838 If any of these conditions is met, this function returns the end point of
4839 the entity, causing the search to continue from there. This is a function
4840 that can be put into `org-agenda-skip-function' for the duration of a command."
4841 (let (beg end m)
4842 (org-back-to-heading t)
4843 (setq beg (point)
4844 end (if subtree
4845 (progn (org-end-of-subtree t) (point))
4846 (progn (outline-next-heading) (1- (point)))))
4847 (goto-char beg)
4848 (and
4850 (and (memq 'scheduled conditions)
4851 (re-search-forward org-scheduled-time-regexp end t))
4852 (and (memq 'notscheduled conditions)
4853 (not (re-search-forward org-scheduled-time-regexp end t)))
4854 (and (memq 'deadline conditions)
4855 (re-search-forward org-deadline-time-regexp end t))
4856 (and (memq 'notdeadline conditions)
4857 (not (re-search-forward org-deadline-time-regexp end t)))
4858 (and (memq 'timestamp conditions)
4859 (re-search-forward org-ts-regexp end t))
4860 (and (memq 'nottimestamp conditions)
4861 (not (re-search-forward org-ts-regexp end t)))
4862 (and (setq m (memq 'regexp conditions))
4863 (stringp (nth 1 m))
4864 (re-search-forward (nth 1 m) end t))
4865 (and (setq m (memq 'notregexp conditions))
4866 (stringp (nth 1 m))
4867 (not (re-search-forward (nth 1 m) end t)))
4868 (and (or
4869 (setq m (memq 'nottodo conditions))
4870 (setq m (memq 'todo-unblocked conditions))
4871 (setq m (memq 'nottodo-unblocked conditions))
4872 (setq m (memq 'todo conditions)))
4873 (org-agenda-skip-if-todo m end)))
4874 end)))
4876 (defun org-agenda-skip-if-todo (args end)
4877 "Helper function for `org-agenda-skip-if', do not use it directly.
4878 ARGS is a list with first element either `todo', `nottodo',
4879 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
4880 a list of TODO keywords, or a state symbol `todo' or `done' or
4881 `any'."
4882 (let ((kw (car args))
4883 (arg (cadr args))
4884 todo-wds todo-re)
4885 (setq todo-wds
4886 (org-uniquify
4887 (cond
4888 ((listp arg) ;; list of keywords
4889 (if (member "*" arg)
4890 (mapcar 'substring-no-properties org-todo-keywords-1)
4891 arg))
4892 ((symbolp arg) ;; keyword class name
4893 (cond
4894 ((eq arg 'todo)
4895 (org-delete-all org-done-keywords
4896 (mapcar 'substring-no-properties
4897 org-todo-keywords-1)))
4898 ((eq arg 'done) org-done-keywords)
4899 ((eq arg 'any)
4900 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4901 (setq todo-re
4902 (concat "^\\*+[ \t]+\\<\\("
4903 (mapconcat 'identity todo-wds "\\|")
4904 "\\)\\>"))
4905 (cond
4906 ((eq kw 'todo) (re-search-forward todo-re end t))
4907 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
4908 ((eq kw 'todo-unblocked)
4909 (catch 'unblocked
4910 (while (re-search-forward todo-re end t)
4911 (or (org-entry-blocked-p) (throw 'unblocked t)))
4912 nil))
4913 ((eq kw 'nottodo-unblocked)
4914 (catch 'unblocked
4915 (while (re-search-forward todo-re end t)
4916 (or (org-entry-blocked-p) (throw 'unblocked nil)))
4920 ;;;###autoload
4921 (defun org-agenda-list-stuck-projects (&rest ignore)
4922 "Create agenda view for projects that are stuck.
4923 Stuck projects are project that have no next actions. For the definitions
4924 of what a project is and how to check if it stuck, customize the variable
4925 `org-stuck-projects'."
4926 (interactive)
4927 (let* ((org-agenda-overriding-header
4928 (or org-agenda-overriding-header "List of stuck projects: "))
4929 (matcher (nth 0 org-stuck-projects))
4930 (todo (nth 1 org-stuck-projects))
4931 (tags (nth 2 org-stuck-projects))
4932 (gen-re (org-string-nw-p (nth 3 org-stuck-projects)))
4933 (todo-wds
4934 (if (not (member "*" todo)) todo
4935 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
4936 (org-delete-all org-done-keywords-for-agenda
4937 (copy-sequence org-todo-keywords-for-agenda))))
4938 (todo-re (and todo
4939 (format "^\\*+[ \t]+\\(%s\\)\\>"
4940 (mapconcat #'identity todo-wds "\\|"))))
4941 (tags-re (cond ((null tags) nil)
4942 ((member "*" tags)
4943 (eval-when-compile
4944 (concat org-outline-regexp-bol
4945 ".*:[[:alnum:]_@#%]+:[ \t]*$")))
4946 (tags (concat org-outline-regexp-bol
4947 ".*:\\("
4948 (mapconcat #'identity tags "\\|")
4949 "\\):[[:alnum:]_@#%:]*[ \t]*$"))
4950 (t nil)))
4951 (re-list (delq nil (list todo-re tags-re gen-re)))
4952 (skip-re
4953 (if (null re-list)
4954 (error "Missing information to identify unstuck projects")
4955 (mapconcat #'identity re-list "\\|")))
4956 (org-agenda-skip-function
4957 ;; Skip entry if `org-agenda-skip-regexp' matches anywhere
4958 ;; in the subtree.
4959 `(lambda ()
4960 (and (save-excursion
4961 (let ((case-fold-search nil))
4962 (re-search-forward
4963 ,skip-re (save-excursion (org-end-of-subtree t)) t)))
4964 (progn (outline-next-heading) (point))))))
4965 (org-tags-view nil matcher)
4966 (setq org-agenda-buffer-name (buffer-name))
4967 (with-current-buffer org-agenda-buffer-name
4968 (setq org-agenda-redo-command
4969 `(org-agenda-list-stuck-projects ,current-prefix-arg))
4970 (let ((inhibit-read-only t))
4971 (add-text-properties
4972 (point-min) (point-max)
4973 `(org-redo-cmd ,org-agenda-redo-command))))))
4975 ;;; Diary integration
4977 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
4978 (defvar diary-list-entries-hook)
4979 (defvar diary-time-regexp)
4980 (defun org-get-entries-from-diary (date)
4981 "Get the (Emacs Calendar) diary entries for DATE."
4982 (require 'diary-lib)
4983 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
4984 (diary-display-function 'diary-fancy-display)
4985 (pop-up-frames nil)
4986 (diary-list-entries-hook
4987 (cons 'org-diary-default-entry diary-list-entries-hook))
4988 (diary-file-name-prefix nil) ; turn this feature off
4989 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
4990 entries
4991 (org-disable-agenda-to-diary t))
4992 (save-excursion
4993 (save-window-excursion
4994 (funcall (if (fboundp 'diary-list-entries)
4995 'diary-list-entries 'list-diary-entries)
4996 date 1)))
4997 (if (not (get-buffer diary-fancy-buffer))
4998 (setq entries nil)
4999 (with-current-buffer diary-fancy-buffer
5000 (setq buffer-read-only nil)
5001 (if (zerop (buffer-size))
5002 ;; No entries
5003 (setq entries nil)
5004 ;; Omit the date and other unnecessary stuff
5005 (org-agenda-cleanup-fancy-diary)
5006 ;; Add prefix to each line and extend the text properties
5007 (if (zerop (buffer-size))
5008 (setq entries nil)
5009 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
5010 (setq entries
5011 (with-temp-buffer
5012 (insert entries) (goto-char (point-min))
5013 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5014 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5015 (replace-match (concat "; " (match-string 1)))))
5016 (buffer-string)))))
5017 (set-buffer-modified-p nil)
5018 (kill-buffer diary-fancy-buffer)))
5019 (when entries
5020 (setq entries (org-split-string entries "\n"))
5021 (setq entries
5022 (mapcar
5023 (lambda (x)
5024 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5025 ;; Extend the text properties to the beginning of the line
5026 (org-add-props x (text-properties-at (1- (length x)) x)
5027 'type "diary" 'date date 'face 'org-agenda-diary))
5028 entries)))))
5030 (defvar org-agenda-cleanup-fancy-diary-hook nil
5031 "Hook run when the fancy diary buffer is cleaned up.")
5033 (defun org-agenda-cleanup-fancy-diary ()
5034 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5035 This gets rid of the date, the underline under the date, and
5036 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5037 date. It also removes lines that contain only whitespace."
5038 (goto-char (point-min))
5039 (if (looking-at ".*?:[ \t]*")
5040 (progn
5041 (replace-match "")
5042 (re-search-forward "\n=+$" nil t)
5043 (replace-match "")
5044 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5045 (re-search-forward "\n=+$" nil t)
5046 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5047 (goto-char (point-min))
5048 (while (re-search-forward "^ +\n" nil t)
5049 (replace-match ""))
5050 (goto-char (point-min))
5051 (if (re-search-forward "^Org mode dummy\n?" nil t)
5052 (replace-match ""))
5053 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5055 ;; Make sure entries from the diary have the right text properties.
5056 (eval-after-load "diary-lib"
5057 '(if (boundp 'diary-modify-entry-list-string-function)
5058 ;; We can rely on the hook, nothing to do
5060 ;; Hook not available, must use advice to make this work
5061 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5062 "Make the position visible."
5063 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5064 (stringp string)
5065 buffer-file-name)
5066 (setq string (org-modify-diary-entry-string string))))))
5068 (defun org-modify-diary-entry-string (string)
5069 "Add text properties to string, allowing Org to act on it."
5070 (org-add-props string nil
5071 'mouse-face 'highlight
5072 'help-echo (if buffer-file-name
5073 (format "mouse-2 or RET jump to diary file %s"
5074 (abbreviate-file-name buffer-file-name))
5076 'org-agenda-diary-link t
5077 'org-marker (org-agenda-new-marker (point-at-bol))))
5079 (defun org-diary-default-entry ()
5080 "Add a dummy entry to the diary.
5081 Needed to avoid empty dates which mess up holiday display."
5082 ;; Catch the error if dealing with the new add-to-diary-alist
5083 (when org-disable-agenda-to-diary
5084 (condition-case nil
5085 (org-add-to-diary-list original-date "Org mode dummy" "")
5086 (error
5087 (org-add-to-diary-list original-date "Org mode dummy" "" nil)))))
5089 (defun org-add-to-diary-list (&rest args)
5090 (if (fboundp 'diary-add-to-list)
5091 (apply 'diary-add-to-list args)
5092 (apply 'add-to-diary-list args)))
5094 (defvar org-diary-last-run-time nil)
5096 ;;;###autoload
5097 (defun org-diary (&rest args)
5098 "Return diary information from org files.
5099 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5100 It accesses org files and extracts information from those files to be
5101 listed in the diary. The function accepts arguments specifying what
5102 items should be listed. For a list of arguments allowed here, see the
5103 variable `org-agenda-entry-types'.
5105 The call in the diary file should look like this:
5107 &%%(org-diary) ~/path/to/some/orgfile.org
5109 Use a separate line for each org file to check. Or, if you omit the file name,
5110 all files listed in `org-agenda-files' will be checked automatically:
5112 &%%(org-diary)
5114 If you don't give any arguments (as in the example above), the default value
5115 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5116 So the example above may also be written as
5118 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5120 The function expects the lisp variables `entry' and `date' to be provided
5121 by the caller, because this is how the calendar works. Don't use this
5122 function from a program - use `org-agenda-get-day-entries' instead."
5123 (when (> (- (float-time)
5124 org-agenda-last-marker-time)
5126 ;; I am not sure if this works with sticky agendas, because the marker
5127 ;; list is then no longer a global variable.
5128 (org-agenda-reset-markers))
5129 (org-compile-prefix-format 'agenda)
5130 (org-set-sorting-strategy 'agenda)
5131 (setq args (or args org-agenda-entry-types))
5132 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5133 (list entry)
5134 (org-agenda-files t)))
5135 (time (float-time))
5136 file rtn results)
5137 (when (or (not org-diary-last-run-time)
5138 (> (- time
5139 org-diary-last-run-time)
5141 (org-agenda-prepare-buffers files))
5142 (setq org-diary-last-run-time time)
5143 ;; If this is called during org-agenda, don't return any entries to
5144 ;; the calendar. Org Agenda will list these entries itself.
5145 (if org-disable-agenda-to-diary (setq files nil))
5146 (while (setq file (pop files))
5147 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5148 (setq results (append results rtn)))
5149 (when results
5150 (setq results
5151 (mapcar (lambda (i) (replace-regexp-in-string
5152 org-bracket-link-regexp "\\3" i)) results))
5153 (concat (org-agenda-finalize-entries results) "\n"))))
5155 ;;; Agenda entry finders
5157 (defun org-agenda--timestamp-to-absolute (&rest args)
5158 "Call `org-time-string-to-absolute' with ARGS.
5159 However, throw `:skip' whenever an error is raised."
5160 (condition-case e
5161 (apply #'org-time-string-to-absolute args)
5162 (org-diary-sexp-no-match (throw :skip nil))
5163 (error
5164 (message "%s; Skipping entry" (error-message-string e))
5165 (throw :skip nil))))
5167 (defun org-agenda-get-day-entries (file date &rest args)
5168 "Does the work for `org-diary' and `org-agenda'.
5169 FILE is the path to a file to be checked for entries. DATE is date like
5170 the one returned by `calendar-current-date'. ARGS are symbols indicating
5171 which kind of entries should be extracted. For details about these, see
5172 the documentation of `org-diary'."
5173 (let* ((org-startup-folded nil)
5174 (org-startup-align-all-tables nil)
5175 (buffer (if (file-exists-p file) (org-get-agenda-file-buffer file)
5176 (error "No such file %s" file))))
5177 (if (not buffer)
5178 ;; If file does not exist, signal it in diary nonetheless.
5179 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5180 (with-current-buffer buffer
5181 (unless (derived-mode-p 'org-mode)
5182 (error "Agenda file %s is not in `org-mode'" file))
5183 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5184 (setf org-agenda-current-date date)
5185 (save-excursion
5186 (save-restriction
5187 (if (eq buffer org-agenda-restrict)
5188 (narrow-to-region org-agenda-restrict-begin
5189 org-agenda-restrict-end)
5190 (widen))
5191 ;; Rationalize ARGS. Also make sure `:deadline' comes
5192 ;; first in order to populate DEADLINES before passing it.
5194 ;; We use `delq' since `org-uniquify' duplicates ARGS,
5195 ;; guarding us from modifying `org-agenda-entry-types'.
5196 (setf args (org-uniquify (or args org-agenda-entry-types)))
5197 (when (and (memq :scheduled args) (memq :scheduled* args))
5198 (setf args (delq :scheduled* args)))
5199 (cond
5200 ((memq :deadline args)
5201 (setf args (cons :deadline
5202 (delq :deadline (delq :deadline* args)))))
5203 ((memq :deadline* args)
5204 (setf args (cons :deadline* (delq :deadline* args)))))
5205 ;; Collect list of headlines. Return them flattened.
5206 (let ((case-fold-search nil) results deadlines)
5207 (dolist (arg args (apply #'nconc (nreverse results)))
5208 (pcase arg
5209 ((and :todo (guard (org-agenda-today-p date)))
5210 (push (org-agenda-get-todos) results))
5211 (:timestamp
5212 (push (org-agenda-get-blocks) results)
5213 (push (org-agenda-get-timestamps deadlines) results))
5214 (:sexp
5215 (push (org-agenda-get-sexps) results))
5216 (:scheduled
5217 (push (org-agenda-get-scheduled deadlines) results))
5218 (:scheduled*
5219 (push (org-agenda-get-scheduled deadlines t) results))
5220 (:closed
5221 (push (org-agenda-get-progress) results))
5222 (:deadline
5223 (setf deadlines (org-agenda-get-deadlines))
5224 (push deadlines results))
5225 (:deadline*
5226 (setf deadlines (org-agenda-get-deadlines t))
5227 (push deadlines results)))))))))))
5229 (defsubst org-em (x y list)
5230 "Is X or Y a member of LIST?"
5231 (or (memq x list) (memq y list)))
5233 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5234 (defvar org-agenda-sorting-strategy-selected nil)
5236 (defun org-agenda-entry-get-agenda-timestamp (pom)
5237 "Retrieve timestamp information for sorting agenda views.
5238 Given a point or marker POM, returns a cons cell of the timestamp
5239 and the timestamp type relevant for the sorting strategy in
5240 `org-agenda-sorting-strategy-selected'."
5241 (let (ts ts-date-type)
5242 (save-match-data
5243 (cond ((org-em 'scheduled-up 'scheduled-down
5244 org-agenda-sorting-strategy-selected)
5245 (setq ts (org-entry-get pom "SCHEDULED")
5246 ts-date-type " scheduled"))
5247 ((org-em 'deadline-up 'deadline-down
5248 org-agenda-sorting-strategy-selected)
5249 (setq ts (org-entry-get pom "DEADLINE")
5250 ts-date-type " deadline"))
5251 ((org-em 'ts-up 'ts-down
5252 org-agenda-sorting-strategy-selected)
5253 (setq ts (org-entry-get pom "TIMESTAMP")
5254 ts-date-type " timestamp"))
5255 ((org-em 'tsia-up 'tsia-down
5256 org-agenda-sorting-strategy-selected)
5257 (setq ts (org-entry-get pom "TIMESTAMP_IA")
5258 ts-date-type " timestamp_ia"))
5259 ((org-em 'timestamp-up 'timestamp-down
5260 org-agenda-sorting-strategy-selected)
5261 (setq ts (or (org-entry-get pom "SCHEDULED")
5262 (org-entry-get pom "DEADLINE")
5263 (org-entry-get pom "TIMESTAMP")
5264 (org-entry-get pom "TIMESTAMP_IA"))
5265 ts-date-type ""))
5266 (t (setq ts-date-type "")))
5267 (cons (when ts (ignore-errors (org-time-string-to-absolute ts)))
5268 ts-date-type))))
5270 (defun org-agenda-get-todos ()
5271 "Return the TODO information for agenda display."
5272 (let* ((props (list 'face nil
5273 'done-face 'org-agenda-done
5274 'org-not-done-regexp org-not-done-regexp
5275 'org-todo-regexp org-todo-regexp
5276 'org-complex-heading-regexp org-complex-heading-regexp
5277 'mouse-face 'highlight
5278 'help-echo
5279 (format "mouse-2 or RET jump to org file %s"
5280 (abbreviate-file-name buffer-file-name))))
5281 (case-fold-search nil)
5282 (regexp (format org-heading-keyword-regexp-format
5283 (cond
5284 ((and org-select-this-todo-keyword
5285 (equal org-select-this-todo-keyword "*"))
5286 org-todo-regexp)
5287 (org-select-this-todo-keyword
5288 (concat "\\("
5289 (mapconcat 'identity
5290 (org-split-string
5291 org-select-this-todo-keyword
5292 "|")
5293 "\\|") "\\)"))
5294 (t org-not-done-regexp))))
5295 marker priority category level tags todo-state
5296 ts-date ts-date-type ts-date-pair
5297 ee txt beg end inherited-tags todo-state-end-pos)
5298 (goto-char (point-min))
5299 (while (re-search-forward regexp nil t)
5300 (catch :skip
5301 (save-match-data
5302 (beginning-of-line)
5303 (org-agenda-skip)
5304 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5305 (unless (and (setq todo-state (org-get-todo-state))
5306 (setq todo-state-end-pos (match-end 2)))
5307 (goto-char end)
5308 (throw :skip nil))
5309 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5310 (goto-char (1+ beg))
5311 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5312 (throw :skip nil)))
5313 (goto-char (match-beginning 2))
5314 (setq marker (org-agenda-new-marker (match-beginning 0))
5315 category (org-get-category)
5316 ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
5317 ts-date (car ts-date-pair)
5318 ts-date-type (cdr ts-date-pair)
5319 txt (org-trim (buffer-substring (match-beginning 2) (match-end 0)))
5320 inherited-tags
5321 (or (eq org-agenda-show-inherited-tags 'always)
5322 (and (listp org-agenda-show-inherited-tags)
5323 (memq 'todo org-agenda-show-inherited-tags))
5324 (and (eq org-agenda-show-inherited-tags t)
5325 (or (eq org-agenda-use-tag-inheritance t)
5326 (memq 'todo org-agenda-use-tag-inheritance))))
5327 tags (org-get-tags-at nil (not inherited-tags))
5328 level (make-string (org-reduced-level (org-outline-level)) ? )
5329 txt (org-agenda-format-item "" txt level category tags t)
5330 priority (1+ (org-get-priority txt)))
5331 (org-add-props txt props
5332 'org-marker marker 'org-hd-marker marker
5333 'priority priority
5334 'level level
5335 'ts-date ts-date
5336 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5337 (push txt ee)
5338 (if org-agenda-todo-list-sublevels
5339 (goto-char todo-state-end-pos)
5340 (org-end-of-subtree 'invisible))))
5341 (nreverse ee)))
5343 (defun org-agenda-todo-custom-ignore-p (time n)
5344 "Check whether timestamp is farther away than n number of days.
5345 This function is invoked if `org-agenda-todo-ignore-deadlines',
5346 `org-agenda-todo-ignore-scheduled' or
5347 `org-agenda-todo-ignore-timestamp' is set to an integer."
5348 (let ((days (org-time-stamp-to-now
5349 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5350 (if (>= n 0)
5351 (>= days n)
5352 (<= days n))))
5354 ;;;###autoload
5355 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5356 (&optional end)
5357 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5358 (when (or org-agenda-todo-ignore-with-date
5359 org-agenda-todo-ignore-scheduled
5360 org-agenda-todo-ignore-deadlines
5361 org-agenda-todo-ignore-timestamp)
5362 (setq end (or end (save-excursion (outline-next-heading) (point))))
5363 (save-excursion
5364 (or (and org-agenda-todo-ignore-with-date
5365 (re-search-forward org-ts-regexp end t))
5366 (and org-agenda-todo-ignore-scheduled
5367 (re-search-forward org-scheduled-time-regexp end t)
5368 (cond
5369 ((eq org-agenda-todo-ignore-scheduled 'future)
5370 (> (org-time-stamp-to-now
5371 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5372 ((eq org-agenda-todo-ignore-scheduled 'past)
5373 (<= (org-time-stamp-to-now
5374 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5375 ((numberp org-agenda-todo-ignore-scheduled)
5376 (org-agenda-todo-custom-ignore-p
5377 (match-string 1) org-agenda-todo-ignore-scheduled))
5378 (t)))
5379 (and org-agenda-todo-ignore-deadlines
5380 (re-search-forward org-deadline-time-regexp end t)
5381 (cond
5382 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5383 ((eq org-agenda-todo-ignore-deadlines 'far)
5384 (not (org-deadline-close-p (match-string 1))))
5385 ((eq org-agenda-todo-ignore-deadlines 'future)
5386 (> (org-time-stamp-to-now
5387 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5388 ((eq org-agenda-todo-ignore-deadlines 'past)
5389 (<= (org-time-stamp-to-now
5390 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5391 ((numberp org-agenda-todo-ignore-deadlines)
5392 (org-agenda-todo-custom-ignore-p
5393 (match-string 1) org-agenda-todo-ignore-deadlines))
5394 (t (org-deadline-close-p (match-string 1)))))
5395 (and org-agenda-todo-ignore-timestamp
5396 (let ((buffer (current-buffer))
5397 (regexp
5398 (concat
5399 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5400 (start (point)))
5401 ;; Copy current buffer into a temporary one
5402 (with-temp-buffer
5403 (insert-buffer-substring buffer start end)
5404 (goto-char (point-min))
5405 ;; Delete SCHEDULED and DEADLINE items
5406 (while (re-search-forward regexp end t)
5407 (delete-region (match-beginning 0) (match-end 0)))
5408 (goto-char (point-min))
5409 ;; No search for timestamp left
5410 (when (re-search-forward org-ts-regexp nil t)
5411 (cond
5412 ((eq org-agenda-todo-ignore-timestamp 'future)
5413 (> (org-time-stamp-to-now
5414 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5415 ((eq org-agenda-todo-ignore-timestamp 'past)
5416 (<= (org-time-stamp-to-now
5417 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5418 ((numberp org-agenda-todo-ignore-timestamp)
5419 (org-agenda-todo-custom-ignore-p
5420 (match-string 1) org-agenda-todo-ignore-timestamp))
5421 (t))))))))))
5423 (defun org-agenda-get-timestamps (&optional deadlines)
5424 "Return the date stamp information for agenda display.
5425 Optional argument DEADLINES is a list of deadline items to be
5426 displayed in agenda view."
5427 (let* ((props (list 'face 'org-agenda-calendar-event
5428 'org-not-done-regexp org-not-done-regexp
5429 'org-todo-regexp org-todo-regexp
5430 'org-complex-heading-regexp org-complex-heading-regexp
5431 'mouse-face 'highlight
5432 'help-echo
5433 (format "mouse-2 or RET jump to Org file %s"
5434 (abbreviate-file-name buffer-file-name))))
5435 (current (calendar-absolute-from-gregorian date))
5436 (today (org-today))
5437 (deadline-position-alist
5438 (mapcar (lambda (d)
5439 (let ((m (get-text-property 0 'org-hd-marker d)))
5440 (and m (marker-position m))))
5441 deadlines))
5442 ;; Match time-stamps set to current date, time-stamps with
5443 ;; a repeater, and S-exp time-stamps.
5444 (regexp
5445 (concat
5446 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5447 (regexp-quote
5448 (substring
5449 (format-time-string
5450 (car org-time-stamp-formats)
5451 (apply #'encode-time ; DATE bound by calendar
5452 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5453 1 11))
5454 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5455 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5456 timestamp-items)
5457 (goto-char (point-min))
5458 (while (re-search-forward regexp nil t)
5459 ;; Skip date ranges, scheduled and deadlines, which are handled
5460 ;; specially. Also skip time-stamps before first headline as
5461 ;; there would be no entry to add to the agenda. Eventually,
5462 ;; ignore clock entries.
5463 (catch :skip
5464 (save-match-data
5465 (when (or (org-at-date-range-p)
5466 (org-at-planning-p)
5467 (org-before-first-heading-p)
5468 (and org-agenda-include-inactive-timestamps
5469 (org-at-clock-log-p)))
5470 (throw :skip nil))
5471 (org-agenda-skip))
5472 (let* ((pos (match-beginning 0))
5473 (repeat (match-string 1))
5474 (sexp-entry (match-string 3))
5475 (time-stamp (if (or repeat sexp-entry) (match-string 0)
5476 (save-excursion
5477 (goto-char pos)
5478 (looking-at org-ts-regexp-both)
5479 (match-string 0))))
5480 (todo-state (org-get-todo-state))
5481 (warntime (get-text-property (point) 'org-appt-warntime))
5482 (done? (member todo-state org-done-keywords)))
5483 ;; Possibly skip done tasks.
5484 (when (and done? org-agenda-skip-timestamp-if-done)
5485 (throw :skip t))
5486 ;; S-exp entry doesn't match current day: skip it.
5487 (when (and sexp-entry (not (org-diary-sexp-entry sexp-entry "" date)))
5488 (throw :skip nil))
5489 (when repeat
5490 (let* ((past
5491 ;; A repeating time stamp is shown at its base
5492 ;; date and every repeated date up to TODAY. If
5493 ;; `org-agenda-prefer-last-repeat' is non-nil,
5494 ;; however, only the last repeat before today
5495 ;; (inclusive) is shown.
5496 (org-agenda--timestamp-to-absolute
5497 repeat
5498 (if (or (> current today)
5499 (eq org-agenda-prefer-last-repeat t)
5500 (member todo-state org-agenda-prefer-last-repeat))
5501 today
5502 current)
5503 'past (current-buffer) pos))
5504 (future
5505 ;; Display every repeated date past TODAY
5506 ;; (exclusive) unless
5507 ;; `org-agenda-show-future-repeats' is nil. If
5508 ;; this variable is set to `next', only display
5509 ;; the first repeated date after TODAY
5510 ;; (exclusive).
5511 (cond
5512 ((<= current today) past)
5513 ((not org-agenda-show-future-repeats) past)
5515 (let ((base (if (eq org-agenda-show-future-repeats 'next)
5516 (1+ today)
5517 current)))
5518 (org-agenda--timestamp-to-absolute
5519 repeat base 'future (current-buffer) pos))))))
5520 (when (and (/= current past) (/= current future))
5521 (throw :skip nil))))
5522 (save-excursion
5523 (re-search-backward org-outline-regexp-bol nil t)
5524 ;; Possibly skip time-stamp when a deadline is set.
5525 (when (and org-agenda-skip-timestamp-if-deadline-is-shown
5526 (assq (point) deadline-position-alist))
5527 (throw :skip nil))
5528 (let* ((category (org-get-category pos))
5529 (inherited-tags
5530 (or (eq org-agenda-show-inherited-tags 'always)
5531 (and (consp org-agenda-show-inherited-tags)
5532 (memq 'agenda org-agenda-show-inherited-tags))
5533 (and (eq org-agenda-show-inherited-tags t)
5534 (or (eq org-agenda-use-tag-inheritance t)
5535 (memq 'agenda
5536 org-agenda-use-tag-inheritance)))))
5537 (tags (org-get-tags-at nil (not inherited-tags)))
5538 (level (make-string (org-reduced-level (org-outline-level))
5539 ?\s))
5540 (head (and (looking-at "\\*+[ \t]+\\(.*\\)")
5541 (match-string 1)))
5542 (inactive? (= (char-after pos) ?\[))
5543 (habit? (and (fboundp 'org-is-habit-p) (org-is-habit-p)))
5544 (item
5545 (org-agenda-format-item
5546 (and inactive? org-agenda-inactive-leader)
5547 head level category tags time-stamp org-ts-regexp habit?)))
5548 (org-add-props item props
5549 'priority (if habit?
5550 (org-habit-get-priority (org-habit-parse-todo))
5551 (org-get-priority item))
5552 'org-marker (org-agenda-new-marker pos)
5553 'org-hd-marker (org-agenda-new-marker)
5554 'date date
5555 'level level
5556 'ts-date (if repeat (org-agenda--timestamp-to-absolute repeat)
5557 current)
5558 'todo-state todo-state
5559 'warntime warntime
5560 'type "timestamp")
5561 (push item timestamp-items))))
5562 (when org-agenda-skip-additional-timestamps-same-entry
5563 (outline-next-heading))))
5564 (nreverse timestamp-items)))
5566 (defun org-agenda-get-sexps ()
5567 "Return the sexp information for agenda display."
5568 (require 'diary-lib)
5569 (let* ((props (list 'face 'org-agenda-calendar-sexp
5570 'mouse-face 'highlight
5571 'help-echo
5572 (format "mouse-2 or RET jump to org file %s"
5573 (abbreviate-file-name buffer-file-name))))
5574 (regexp "^&?%%(")
5575 marker category extra level ee txt tags entry
5576 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5577 (goto-char (point-min))
5578 (while (re-search-forward regexp nil t)
5579 (catch :skip
5580 (org-agenda-skip)
5581 (setq beg (match-beginning 0))
5582 (goto-char (1- (match-end 0)))
5583 (setq b (point))
5584 (forward-sexp 1)
5585 (setq sexp (buffer-substring b (point)))
5586 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5587 (org-trim (match-string 1))
5588 ""))
5589 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5590 (when result
5591 (setq marker (org-agenda-new-marker beg)
5592 level (make-string (org-reduced-level (org-outline-level)) ? )
5593 category (org-get-category beg)
5594 inherited-tags
5595 (or (eq org-agenda-show-inherited-tags 'always)
5596 (and (listp org-agenda-show-inherited-tags)
5597 (memq 'agenda org-agenda-show-inherited-tags))
5598 (and (eq org-agenda-show-inherited-tags t)
5599 (or (eq org-agenda-use-tag-inheritance t)
5600 (memq 'agenda org-agenda-use-tag-inheritance))))
5601 tags (org-get-tags-at nil (not inherited-tags))
5602 todo-state (org-get-todo-state)
5603 warntime (get-text-property (point) 'org-appt-warntime)
5604 extra nil)
5606 (dolist (r (if (stringp result)
5607 (list result)
5608 result)) ;; we expect a list here
5609 (when (and org-agenda-diary-sexp-prefix
5610 (string-match org-agenda-diary-sexp-prefix r))
5611 (setq extra (match-string 0 r)
5612 r (replace-match "" nil nil r)))
5613 (if (string-match "\\S-" r)
5614 (setq txt r)
5615 (setq txt "SEXP entry returned empty string"))
5616 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5617 (org-add-props txt props 'org-marker marker
5618 'date date 'todo-state todo-state
5619 'level level 'type "sexp" 'warntime warntime)
5620 (push txt ee)))))
5621 (nreverse ee)))
5623 ;; Calendar sanity: define some functions that are independent of
5624 ;; `calendar-date-style'.
5625 (defun org-anniversary (year month day &optional mark)
5626 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5627 (with-no-warnings
5628 (let ((calendar-date-style 'iso))
5629 (diary-anniversary year month day mark))))
5630 (defun org-cyclic (N year month day &optional mark)
5631 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5632 (with-no-warnings
5633 (let ((calendar-date-style 'iso))
5634 (diary-cyclic N year month day mark))))
5635 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5636 "Like `diary-block', but with fixed (ISO) order of arguments."
5637 (with-no-warnings
5638 (let ((calendar-date-style 'iso))
5639 (diary-block Y1 M1 D1 Y2 M2 D2 mark))))
5640 (defun org-date (year month day &optional mark)
5641 "Like `diary-date', but with fixed (ISO) order of arguments."
5642 (with-no-warnings
5643 (let ((calendar-date-style 'iso))
5644 (diary-date year month day mark))))
5646 ;; Define the `org-class' function
5647 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5648 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5649 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5650 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5651 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5652 `holidays', then any date that is known by the Emacs calendar to be a
5653 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5654 then those holidays will be skipped."
5655 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5656 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5657 (d (calendar-absolute-from-gregorian date))
5658 (h (when skip-weeks (calendar-check-holidays date))))
5659 (and
5660 (<= date1 d)
5661 (<= d date2)
5662 (= (calendar-day-of-week date) dayname)
5663 (or (not skip-weeks)
5664 (progn
5665 (require 'cal-iso)
5666 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5667 (not (or (and h (memq 'holidays skip-weeks))
5668 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5669 entry)))
5671 (defalias 'org-get-closed 'org-agenda-get-progress)
5672 (defun org-agenda-get-progress ()
5673 "Return the logged TODO entries for agenda display."
5674 (let* ((props (list 'mouse-face 'highlight
5675 'org-not-done-regexp org-not-done-regexp
5676 'org-todo-regexp org-todo-regexp
5677 'org-complex-heading-regexp org-complex-heading-regexp
5678 'help-echo
5679 (format "mouse-2 or RET jump to org file %s"
5680 (abbreviate-file-name buffer-file-name))))
5681 (items (if (consp org-agenda-show-log-scoped)
5682 org-agenda-show-log-scoped
5683 (if (eq org-agenda-show-log-scoped 'clockcheck)
5684 '(clock)
5685 org-agenda-log-mode-items)))
5686 (parts
5687 (delq nil
5688 (list
5689 (if (memq 'closed items) (concat "\\<" org-closed-string))
5690 (if (memq 'clock items) (concat "\\<" org-clock-string))
5691 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5692 (parts-re (if parts (mapconcat 'identity parts "\\|")
5693 (error "`org-agenda-log-mode-items' is empty")))
5694 (regexp (concat
5695 "\\(" parts-re "\\)"
5696 " *\\["
5697 (regexp-quote
5698 (substring
5699 (format-time-string
5700 (car org-time-stamp-formats)
5701 (apply 'encode-time ; DATE bound by calendar
5702 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5703 1 11))))
5704 (org-agenda-search-headline-for-time nil)
5705 marker hdmarker priority category level tags closedp
5706 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5707 (goto-char (point-min))
5708 (while (re-search-forward regexp nil t)
5709 (catch :skip
5710 (org-agenda-skip)
5711 (setq marker (org-agenda-new-marker (match-beginning 0))
5712 closedp (equal (match-string 1) org-closed-string)
5713 statep (equal (string-to-char (match-string 1)) ?-)
5714 clockp (not (or closedp statep))
5715 state (and statep (match-string 2))
5716 category (org-get-category (match-beginning 0))
5717 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5718 (when (string-match "\\]" timestr)
5719 ;; substring should only run to end of time stamp
5720 (setq rest (substring timestr (match-end 0))
5721 timestr (substring timestr 0 (match-end 0)))
5722 (if (and (not closedp) (not statep)
5723 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5724 rest))
5725 (progn (setq timestr (concat (substring timestr 0 -1)
5726 "-" (match-string 1 rest) "]"))
5727 (setq clocked (match-string 2 rest)))
5728 (setq clocked "-")))
5729 (save-excursion
5730 (setq extra
5731 (cond
5732 ((not org-agenda-log-mode-add-notes) nil)
5733 (statep
5734 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5735 (match-string 1)))
5736 (clockp
5737 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5738 (match-string 1)))))
5739 (if (not (re-search-backward org-outline-regexp-bol nil t))
5740 (throw :skip nil)
5741 (goto-char (match-beginning 0))
5742 (setq hdmarker (org-agenda-new-marker)
5743 inherited-tags
5744 (or (eq org-agenda-show-inherited-tags 'always)
5745 (and (listp org-agenda-show-inherited-tags)
5746 (memq 'todo org-agenda-show-inherited-tags))
5747 (and (eq org-agenda-show-inherited-tags t)
5748 (or (eq org-agenda-use-tag-inheritance t)
5749 (memq 'todo org-agenda-use-tag-inheritance))))
5750 tags (org-get-tags-at nil (not inherited-tags))
5751 level (make-string (org-reduced-level (org-outline-level)) ? ))
5752 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5753 (setq txt (match-string 1))
5754 (when extra
5755 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5756 (setq txt (concat (substring txt 0 (match-beginning 1))
5757 " - " extra " " (match-string 2 txt)))
5758 (setq txt (concat txt " - " extra))))
5759 (setq txt (org-agenda-format-item
5760 (cond
5761 (closedp "Closed: ")
5762 (statep (concat "State: (" state ")"))
5763 (t (concat "Clocked: (" clocked ")")))
5764 txt level category tags timestr)))
5765 (setq priority 100000)
5766 (org-add-props txt props
5767 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5768 'priority priority 'level level
5769 'type "closed" 'date date
5770 'undone-face 'org-warning 'done-face 'org-agenda-done)
5771 (push txt ee))
5772 (goto-char (point-at-eol))))
5773 (nreverse ee)))
5775 (defun org-agenda-show-clocking-issues ()
5776 "Add overlays, showing issues with clocking.
5777 See also the user option `org-agenda-clock-consistency-checks'."
5778 (interactive)
5779 (let* ((pl org-agenda-clock-consistency-checks)
5780 (re (concat "^[ \t]*"
5781 org-clock-string
5782 "[ \t]+"
5783 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5784 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5785 (tlstart 0.)
5786 (tlend 0.)
5787 (maxtime (org-duration-to-minutes
5788 (or (plist-get pl :max-duration) "24:00")))
5789 (mintime (org-duration-to-minutes
5790 (or (plist-get pl :min-duration) 0)))
5791 (maxgap (org-duration-to-minutes
5792 ;; default 30:00 means never complain
5793 (or (plist-get pl :max-gap) "30:00")))
5794 (gapok (mapcar #'org-duration-to-minutes
5795 (plist-get pl :gap-ok-around)))
5796 (def-face (or (plist-get pl :default-face)
5797 '((:background "DarkRed") (:foreground "white"))))
5798 issue face m te ts dt ov)
5799 (goto-char (point-min))
5800 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5801 (setq issue nil face def-face)
5802 (catch 'next
5803 (setq m (org-get-at-bol 'org-marker)
5804 te nil ts nil)
5805 (unless (and m (markerp m))
5806 (setq issue "No valid clock line") (throw 'next t))
5807 (org-with-point-at m
5808 (save-excursion
5809 (goto-char (point-at-bol))
5810 (unless (looking-at re)
5811 (error "No valid Clock line")
5812 (throw 'next t))
5813 (unless (match-end 3)
5814 (setq issue "No end time"
5815 face (or (plist-get pl :no-end-time-face) face))
5816 (throw 'next t))
5817 (setq ts (match-string 1)
5818 te (match-string 3)
5819 ts (float-time
5820 (apply #'encode-time (org-parse-time-string ts)))
5821 te (float-time
5822 (apply #'encode-time (org-parse-time-string te)))
5823 dt (- te ts))))
5824 (cond
5825 ((> dt (* 60 maxtime))
5826 ;; a very long clocking chunk
5827 (setq issue (format "Clocking interval is very long: %s"
5828 (org-duration-from-minutes (floor (/ dt 60.))))
5829 face (or (plist-get pl :long-face) face)))
5830 ((< dt (* 60 mintime))
5831 ;; a very short clocking chunk
5832 (setq issue (format "Clocking interval is very short: %s"
5833 (org-duration-from-minutes (floor (/ dt 60.))))
5834 face (or (plist-get pl :short-face) face)))
5835 ((and (> tlend 0) (< ts tlend))
5836 ;; Two clock entries are overlapping
5837 (setq issue (format "Clocking overlap: %d minutes"
5838 (/ (- tlend ts) 60))
5839 face (or (plist-get pl :overlap-face) face)))
5840 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5841 ;; There is a gap, lets see if we need to report it
5842 (unless (org-agenda-check-clock-gap tlend ts gapok)
5843 (setq issue (format "Clocking gap: %d minutes"
5844 (/ (- ts tlend) 60))
5845 face (or (plist-get pl :gap-face) face))))
5846 (t nil)))
5847 (setq tlend (or te tlend) tlstart (or ts tlstart))
5848 (when issue
5849 ;; OK, there was some issue, add an overlay to show the issue
5850 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5851 (overlay-put ov 'before-string
5852 (concat
5853 (org-add-props
5854 (format "%-43s" (concat " " issue))
5856 'face face)
5857 "\n"))
5858 (overlay-put ov 'evaporate t)))))
5860 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5861 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5862 (catch 'exit
5863 (unless ok-list
5864 ;; there are no OK times for gaps...
5865 (throw 'exit nil))
5866 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5867 ;; This is more than 24 hours, so it is OK.
5868 ;; because we have at least one OK time, that must be in the
5869 ;; 24 hour interval.
5870 (throw 'exit t))
5871 ;; We have a shorter gap.
5872 ;; Now we have to get the minute of the day when these times are
5873 (let* ((t1dec (decode-time (seconds-to-time t1)))
5874 (t2dec (decode-time (seconds-to-time t2)))
5875 ;; compute the minute on the day
5876 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5877 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5878 (when (< min2 min1)
5879 ;; if min2 is smaller than min1, this means it is on the next day.
5880 ;; Wrap it to after midnight.
5881 (setq min2 (+ min2 1440)))
5882 ;; Now check if any of the OK times is in the gap
5883 (mapc (lambda (x)
5884 ;; Wrap the time to after midnight if necessary
5885 (if (< x min1) (setq x (+ x 1440)))
5886 ;; Check if in interval
5887 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5888 ok-list)
5889 ;; Nope, this gap is not OK
5890 nil)))
5892 (defun org-agenda-get-deadlines (&optional with-hour)
5893 "Return the deadline information for agenda display.
5894 When WITH-HOUR is non-nil, only return deadlines with an hour
5895 specification like [h]h:mm."
5896 (let* ((props (list 'mouse-face 'highlight
5897 'org-not-done-regexp org-not-done-regexp
5898 'org-todo-regexp org-todo-regexp
5899 'org-complex-heading-regexp org-complex-heading-regexp
5900 'help-echo
5901 (format "mouse-2 or RET jump to org file %s"
5902 (abbreviate-file-name buffer-file-name))))
5903 (regexp (if with-hour
5904 org-deadline-time-hour-regexp
5905 org-deadline-time-regexp))
5906 (today (org-today))
5907 (today? (org-agenda-today-p date)) ; DATE bound by calendar.
5908 (current (calendar-absolute-from-gregorian date))
5909 deadline-items)
5910 (goto-char (point-min))
5911 (while (re-search-forward regexp nil t)
5912 (catch :skip
5913 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
5914 (org-agenda-skip)
5915 (let* ((s (match-string 1))
5916 (pos (1- (match-beginning 1)))
5917 (todo-state (save-match-data (org-get-todo-state)))
5918 (done? (member todo-state org-done-keywords))
5919 (sexp? (string-prefix-p "%%" s))
5920 ;; DEADLINE is the deadline date for the entry. It is
5921 ;; either the base date or the last repeat, according
5922 ;; to `org-agenda-prefer-last-repeat'.
5923 (deadline
5924 (cond
5925 (sexp? (org-agenda--timestamp-to-absolute s current))
5926 ((or (eq org-agenda-prefer-last-repeat t)
5927 (member todo-state org-agenda-prefer-last-repeat))
5928 (org-agenda--timestamp-to-absolute
5929 s today 'past (current-buffer) pos))
5930 (t (org-agenda--timestamp-to-absolute s))))
5931 ;; REPEAT is the future repeat closest from CURRENT,
5932 ;; according to `org-agenda-show-future-repeats'. If
5933 ;; the latter is nil, or if the time stamp has no
5934 ;; repeat part, default to DEADLINE.
5935 (repeat
5936 (cond
5937 (sexp? deadline)
5938 ((<= current today) deadline)
5939 ((not org-agenda-show-future-repeats) deadline)
5941 (let ((base (if (eq org-agenda-show-future-repeats 'next)
5942 (1+ today)
5943 current)))
5944 (org-agenda--timestamp-to-absolute
5945 s base 'future (current-buffer) pos)))))
5946 (diff (- deadline current))
5947 (suppress-prewarning
5948 (let ((scheduled
5949 (and org-agenda-skip-deadline-prewarning-if-scheduled
5950 (org-entry-get nil "SCHEDULED"))))
5951 (cond
5952 ((not scheduled) nil)
5953 ;; The current item has a scheduled date, so
5954 ;; evaluate its prewarning lead time.
5955 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
5956 ;; Use global prewarning-restart lead time.
5957 org-agenda-skip-deadline-prewarning-if-scheduled)
5958 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
5959 'pre-scheduled)
5960 ;; Set pre-warning to no earlier than SCHEDULED.
5961 (min (- deadline
5962 (org-agenda--timestamp-to-absolute scheduled))
5963 org-deadline-warning-days))
5964 ;; Set pre-warning to deadline.
5965 (t 0))))
5966 (wdays (if suppress-prewarning
5967 (let ((org-deadline-warning-days suppress-prewarning))
5968 (org-get-wdays s))
5969 (org-get-wdays s))))
5970 (cond
5971 ;; Only display deadlines at their base date, at future
5972 ;; repeat occurrences or in today agenda.
5973 ((= current deadline) nil)
5974 ((= current repeat) nil)
5975 ((not today?) (throw :skip nil))
5976 ;; Upcoming deadline: display within warning period WDAYS.
5977 ((> deadline current) (when (> diff wdays) (throw :skip nil)))
5978 ;; Overdue deadline: warn about it for
5979 ;; `org-deadline-past-days' duration.
5980 (t (when (< org-deadline-past-days (- diff)) (throw :skip nil))))
5981 ;; Possibly skip done tasks.
5982 (when (and done?
5983 (or org-agenda-skip-deadline-if-done
5984 (/= deadline current)))
5985 (throw :skip nil))
5986 (save-excursion
5987 (re-search-backward "^\\*+[ \t]+" nil t)
5988 (goto-char (match-end 0))
5989 (let* ((category (org-get-category))
5990 (level (make-string (org-reduced-level (org-outline-level))
5991 ?\s))
5992 (head (buffer-substring (point) (line-end-position)))
5993 (inherited-tags
5994 (or (eq org-agenda-show-inherited-tags 'always)
5995 (and (listp org-agenda-show-inherited-tags)
5996 (memq 'agenda org-agenda-show-inherited-tags))
5997 (and (eq org-agenda-show-inherited-tags t)
5998 (or (eq org-agenda-use-tag-inheritance t)
5999 (memq 'agenda
6000 org-agenda-use-tag-inheritance)))))
6001 (tags (org-get-tags-at nil (not inherited-tags)))
6002 (time
6003 (cond
6004 ;; No time of day designation if it is only
6005 ;; a reminder.
6006 ((and (/= current deadline) (/= current repeat)) nil)
6007 ((string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6008 (concat (substring s (match-beginning 1)) " "))
6009 (t 'time)))
6010 (item
6011 (org-agenda-format-item
6012 ;; Insert appropriate suffixes before deadlines.
6013 ;; Those only apply to today agenda.
6014 (pcase-let ((`(,now ,future ,past)
6015 org-agenda-deadline-leaders))
6016 (cond
6017 ((and today? (< deadline today)) (format past (- diff)))
6018 ((and today? (> deadline today)) (format future diff))
6019 (t now)))
6020 head level category tags time))
6021 (face (org-agenda-deadline-face
6022 (- 1 (/ (float diff) (max wdays 1)))))
6023 (upcoming? (and today? (> deadline today)))
6024 (warntime (get-text-property (point) 'org-appt-warntime)))
6025 (org-add-props item props
6026 'org-marker (org-agenda-new-marker pos)
6027 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
6028 'warntime warntime
6029 'level level
6030 'ts-date deadline
6031 'priority
6032 ;; Adjust priority to today reminders about deadlines.
6033 ;; Overdue deadlines get the highest priority
6034 ;; increase, then imminent deadlines and eventually
6035 ;; more distant deadlines.
6036 (let ((adjust (if today? (- diff) 0)))
6037 (+ adjust (org-get-priority item)))
6038 'todo-state todo-state
6039 'type (if upcoming? "upcoming-deadline" "deadline")
6040 'date (if upcoming? date deadline)
6041 'face (if done? 'org-agenda-done face)
6042 'undone-face face
6043 'done-face 'org-agenda-done)
6044 (push item deadline-items))))))
6045 (nreverse deadline-items)))
6047 (defun org-agenda-deadline-face (fraction)
6048 "Return the face to displaying a deadline item.
6049 FRACTION is what fraction of the head-warning time has passed."
6050 (assoc-default fraction org-agenda-deadline-faces #'<=))
6052 (defun org-agenda-get-scheduled (&optional deadlines with-hour)
6053 "Return the scheduled information for agenda display.
6054 Optional argument DEADLINES is a list of deadline items to be
6055 displayed in agenda view. When WITH-HOUR is non-nil, only return
6056 scheduled items with an hour specification like [h]h:mm."
6057 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
6058 'org-todo-regexp org-todo-regexp
6059 'org-complex-heading-regexp org-complex-heading-regexp
6060 'done-face 'org-agenda-done
6061 'mouse-face 'highlight
6062 'help-echo
6063 (format "mouse-2 or RET jump to Org file %s"
6064 (abbreviate-file-name buffer-file-name))))
6065 (regexp (if with-hour
6066 org-scheduled-time-hour-regexp
6067 org-scheduled-time-regexp))
6068 (today (org-today))
6069 (todayp (org-agenda-today-p date)) ; DATE bound by calendar.
6070 (current (calendar-absolute-from-gregorian date))
6071 (deadline-pos
6072 (mapcar (lambda (d)
6073 (let ((m (get-text-property 0 'org-hd-marker d)))
6074 (and m (marker-position m))))
6075 deadlines))
6076 scheduled-items)
6077 (goto-char (point-min))
6078 (while (re-search-forward regexp nil t)
6079 (catch :skip
6080 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
6081 (org-agenda-skip)
6082 (let* ((s (match-string 1))
6083 (pos (1- (match-beginning 1)))
6084 (todo-state (save-match-data (org-get-todo-state)))
6085 (donep (member todo-state org-done-keywords))
6086 (sexp? (string-prefix-p "%%" s))
6087 ;; SCHEDULE is the scheduled date for the entry. It is
6088 ;; either the bare date or the last repeat, according
6089 ;; to `org-agenda-prefer-last-repeat'.
6090 (schedule
6091 (cond
6092 (sexp? (org-agenda--timestamp-to-absolute s current))
6093 ((or (eq org-agenda-prefer-last-repeat t)
6094 (member todo-state org-agenda-prefer-last-repeat))
6095 (org-agenda--timestamp-to-absolute
6096 s today 'past (current-buffer) pos))
6097 (t (org-agenda--timestamp-to-absolute s))))
6098 ;; REPEAT is the future repeat closest from CURRENT,
6099 ;; according to `org-agenda-show-future-repeats'. If
6100 ;; the latter is nil, or if the time stamp has no
6101 ;; repeat part, default to SCHEDULE.
6102 (repeat
6103 (cond
6104 (sexp? schedule)
6105 ((<= current today) schedule)
6106 ((not org-agenda-show-future-repeats) schedule)
6108 (let ((base (if (eq org-agenda-show-future-repeats 'next)
6109 (1+ today)
6110 current)))
6111 (org-agenda--timestamp-to-absolute
6112 s base 'future (current-buffer) pos)))))
6113 (diff (- current schedule))
6114 (warntime (get-text-property (point) 'org-appt-warntime))
6115 (pastschedp (< schedule today))
6116 (habitp (and (fboundp 'org-is-habit-p) (org-is-habit-p)))
6117 (suppress-delay
6118 (let ((deadline (and org-agenda-skip-scheduled-delay-if-deadline
6119 (org-entry-get nil "DEADLINE"))))
6120 (cond
6121 ((not deadline) nil)
6122 ;; The current item has a deadline date, so
6123 ;; evaluate its delay time.
6124 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6125 ;; Use global delay time.
6126 (- org-agenda-skip-scheduled-delay-if-deadline))
6127 ((eq org-agenda-skip-scheduled-delay-if-deadline
6128 'post-deadline)
6129 ;; Set delay to no later than DEADLINE.
6130 (min (- schedule
6131 (org-agenda--timestamp-to-absolute deadline))
6132 org-scheduled-delay-days))
6133 (t 0))))
6134 (ddays
6135 (cond
6136 ;; Nullify delay when a repeater triggered already
6137 ;; and the delay is of the form --Xd.
6138 ((and (string-match-p "--[0-9]+[hdwmy]" s)
6139 (> current schedule))
6141 (suppress-delay
6142 (let ((org-scheduled-delay-days suppress-delay))
6143 (org-get-wdays s t t)))
6144 (t (org-get-wdays s t)))))
6145 ;; Display scheduled items at base date (SCHEDULE), today if
6146 ;; scheduled before the current date, and at any repeat past
6147 ;; today. However, skip delayed items and items that have
6148 ;; been displayed for more than `org-scheduled-past-days'.
6149 (unless (and todayp
6150 habitp
6151 (bound-and-true-p org-habit-show-all-today))
6152 (when (or (and (> ddays 0) (< diff ddays))
6153 (> diff org-scheduled-past-days)
6154 (> schedule current)
6155 (and (/= current schedule)
6156 (/= current today)
6157 (/= current repeat)))
6158 (throw :skip nil)))
6159 ;; Possibly skip done tasks.
6160 (when (and donep
6161 (or org-agenda-skip-scheduled-if-done
6162 (/= schedule current)))
6163 (throw :skip nil))
6164 ;; Skip entry if it already appears as a deadline, per
6165 ;; `org-agenda-skip-scheduled-if-deadline-is-shown'. This
6166 ;; doesn't apply to habits.
6167 (when (pcase org-agenda-skip-scheduled-if-deadline-is-shown
6168 ((guard
6169 (or (not (memq (line-beginning-position 0) deadline-pos))
6170 habitp))
6171 nil)
6172 (`repeated-after-deadline
6173 (let ((deadline (time-to-days
6174 (org-get-deadline-time (point)))))
6175 (and (<= schedule deadline) (> current deadline))))
6176 (`not-today pastschedp)
6177 (`t t)
6178 (_ nil))
6179 (throw :skip nil))
6180 ;; Skip habits if `org-habit-show-habits' is nil, or if we
6181 ;; only show them for today. Also skip done habits.
6182 (when (and habitp
6183 (or donep
6184 (not (bound-and-true-p org-habit-show-habits))
6185 (and (not todayp)
6186 (bound-and-true-p
6187 org-habit-show-habits-only-for-today))))
6188 (throw :skip nil))
6189 (save-excursion
6190 (re-search-backward "^\\*+[ \t]+" nil t)
6191 (goto-char (match-end 0))
6192 (let* ((category (org-get-category))
6193 (inherited-tags
6194 (or (eq org-agenda-show-inherited-tags 'always)
6195 (and (listp org-agenda-show-inherited-tags)
6196 (memq 'agenda org-agenda-show-inherited-tags))
6197 (and (eq org-agenda-show-inherited-tags t)
6198 (or (eq org-agenda-use-tag-inheritance t)
6199 (memq 'agenda
6200 org-agenda-use-tag-inheritance)))))
6201 (tags (org-get-tags-at nil (not inherited-tags)))
6202 (level (make-string (org-reduced-level (org-outline-level))
6203 ?\s))
6204 (head (buffer-substring (point) (line-end-position)))
6205 (time
6206 (cond
6207 ;; No time of day designation if it is only
6208 ;; a reminder.
6209 ((and (/= current schedule) (/= current repeat)) nil)
6210 ((string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6211 (concat (substring s (match-beginning 1)) " "))
6212 (t 'time)))
6213 (item
6214 (org-agenda-format-item
6215 (pcase-let ((`(,first ,past) org-agenda-scheduled-leaders))
6216 ;; Show a reminder of a past scheduled today.
6217 (if (and todayp pastschedp)
6218 (format past diff)
6219 first))
6220 head level category tags time nil habitp))
6221 (face (cond ((and (not habitp) pastschedp)
6222 'org-scheduled-previously)
6223 (todayp 'org-scheduled-today)
6224 (t 'org-scheduled)))
6225 (habitp (and habitp (org-habit-parse-todo))))
6226 (org-add-props item props
6227 'undone-face face
6228 'face (if donep 'org-agenda-done face)
6229 'org-marker (org-agenda-new-marker pos)
6230 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
6231 'type (if pastschedp "past-scheduled" "scheduled")
6232 'date (if pastschedp schedule date)
6233 'ts-date schedule
6234 'warntime warntime
6235 'level level
6236 'priority (if habitp (org-habit-get-priority habitp)
6237 (+ 99 diff (org-get-priority item)))
6238 'org-habit-p habitp
6239 'todo-state todo-state)
6240 (push item scheduled-items))))))
6241 (nreverse scheduled-items)))
6243 (defun org-agenda-get-blocks ()
6244 "Return the date-range information for agenda display."
6245 (let* ((props (list 'face nil
6246 'org-not-done-regexp org-not-done-regexp
6247 'org-todo-regexp org-todo-regexp
6248 'org-complex-heading-regexp org-complex-heading-regexp
6249 'mouse-face 'highlight
6250 'help-echo
6251 (format "mouse-2 or RET jump to org file %s"
6252 (abbreviate-file-name buffer-file-name))))
6253 (regexp org-tr-regexp)
6254 (d0 (calendar-absolute-from-gregorian date))
6255 marker hdmarker ee txt d1 d2 s1 s2 category
6256 level todo-state tags pos head donep inherited-tags)
6257 (goto-char (point-min))
6258 (while (re-search-forward regexp nil t)
6259 (catch :skip
6260 (org-agenda-skip)
6261 (setq pos (point))
6262 (let ((start-time (match-string 1))
6263 (end-time (match-string 2)))
6264 (setq s1 (match-string 1)
6265 s2 (match-string 2)
6266 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
6267 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
6268 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6269 ;; Only allow days between the limits, because the normal
6270 ;; date stamps will catch the limits.
6271 (save-excursion
6272 (setq todo-state (org-get-todo-state))
6273 (setq donep (member todo-state org-done-keywords))
6274 (if (and donep org-agenda-skip-timestamp-if-done)
6275 (throw :skip t))
6276 (setq marker (org-agenda-new-marker (point))
6277 category (org-get-category))
6278 (if (not (re-search-backward org-outline-regexp-bol nil t))
6279 (throw :skip nil)
6280 (goto-char (match-beginning 0))
6281 (setq hdmarker (org-agenda-new-marker (point))
6282 inherited-tags
6283 (or (eq org-agenda-show-inherited-tags 'always)
6284 (and (listp org-agenda-show-inherited-tags)
6285 (memq 'agenda org-agenda-show-inherited-tags))
6286 (and (eq org-agenda-show-inherited-tags t)
6287 (or (eq org-agenda-use-tag-inheritance t)
6288 (memq 'agenda org-agenda-use-tag-inheritance))))
6290 tags (org-get-tags-at nil (not inherited-tags)))
6291 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6292 (looking-at "\\*+[ \t]+\\(.*\\)")
6293 (setq head (match-string 1))
6294 (let ((remove-re
6295 (if org-agenda-remove-timeranges-from-blocks
6296 (concat
6297 "<" (regexp-quote s1) ".*?>"
6298 "--"
6299 "<" (regexp-quote s2) ".*?>")
6300 nil)))
6301 (setq txt (org-agenda-format-item
6302 (format
6303 (nth (if (= d1 d2) 0 1)
6304 org-agenda-timerange-leaders)
6305 (1+ (- d0 d1)) (1+ (- d2 d1)))
6306 head level category tags
6307 (cond ((and (= d1 d0) (= d2 d0))
6308 (concat "<" start-time ">--<" end-time ">"))
6309 ((= d1 d0)
6310 (concat "<" start-time ">"))
6311 ((= d2 d0)
6312 (concat "<" end-time ">")))
6313 remove-re))))
6314 (org-add-props txt props
6315 'org-marker marker 'org-hd-marker hdmarker
6316 'type "block" 'date date
6317 'level level
6318 'todo-state todo-state
6319 'priority (org-get-priority txt))
6320 (push txt ee))))
6321 (goto-char pos)))
6322 ;; Sort the entries by expiration date.
6323 (nreverse ee)))
6325 ;;; Agenda presentation and sorting
6327 (defvar org-prefix-has-time nil
6328 "A flag, set by `org-compile-prefix-format'.
6329 The flag is set if the currently compiled format contains a `%t'.")
6330 (defvar org-prefix-has-tag nil
6331 "A flag, set by `org-compile-prefix-format'.
6332 The flag is set if the currently compiled format contains a `%T'.")
6333 (defvar org-prefix-has-effort nil
6334 "A flag, set by `org-compile-prefix-format'.
6335 The flag is set if the currently compiled format contains a `%e'.")
6336 (defvar org-prefix-has-breadcrumbs nil
6337 "A flag, set by `org-compile-prefix-format'.
6338 The flag is set if the currently compiled format contains a `%b'.")
6339 (defvar org-prefix-category-length nil
6340 "Used by `org-compile-prefix-format' to remember the category field width.")
6341 (defvar org-prefix-category-max-length nil
6342 "Used by `org-compile-prefix-format' to remember the category field width.")
6344 (defun org-agenda-get-category-icon (category)
6345 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6346 (cl-dolist (entry org-agenda-category-icon-alist)
6347 (when (string-match-p (car entry) category)
6348 (if (listp (cadr entry))
6349 (cl-return (cadr entry))
6350 (cl-return (apply #'create-image (cdr entry)))))))
6352 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6353 remove-re habitp)
6354 "Format TXT to be inserted into the agenda buffer.
6355 In particular, add the prefix and corresponding text properties.
6357 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6358 LEVEL may be a string to replace the `%l' specifier.
6359 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6360 category taken from local variable or file name. It will replace the `%c'
6361 specifier in the format.
6362 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6363 TXT for sorting of this entry, and for the `%t' specifier in the format.
6364 When DOTIME is a string, this string is searched for a time before TXT is.
6365 TAGS can be the tags of the headline.
6366 Any match of REMOVE-RE will be removed from TXT."
6367 ;; We keep the org-prefix-* variable values along with a compiled
6368 ;; formatter, so that multiple agendas existing at the same time do
6369 ;; not step on each other toes.
6371 ;; It was inconvenient to make these variables buffer local in
6372 ;; Agenda buffers, because this function expects to be called with
6373 ;; the buffer where item comes from being current, and not agenda
6374 ;; buffer
6375 (let* ((bindings (car org-prefix-format-compiled))
6376 (formatter (cadr org-prefix-format-compiled)))
6377 (cl-loop for (var value) in bindings
6378 do (set var value))
6379 (save-match-data
6380 ;; Diary entries sometimes have extra whitespace at the beginning
6381 (setq txt (org-trim txt))
6383 ;; Fix the tags part in txt
6384 (setq txt (org-agenda-fix-displayed-tags
6385 txt tags
6386 org-agenda-show-inherited-tags
6387 org-agenda-hide-tags-regexp))
6389 (let* ((category (or category
6390 (if buffer-file-name
6391 (file-name-sans-extension
6392 (file-name-nondirectory buffer-file-name))
6393 "")))
6394 (category-icon (org-agenda-get-category-icon category))
6395 (category-icon (if category-icon
6396 (propertize " " 'display category-icon)
6397 ""))
6398 (effort (and (not (string= txt ""))
6399 (get-text-property 1 'effort txt)))
6400 ;; time, tag, effort are needed for the eval of the prefix format
6401 (tag (if tags (nth (1- (length tags)) tags) ""))
6402 time
6403 (ts (if dotime (concat
6404 (if (stringp dotime) dotime "")
6405 (and org-agenda-search-headline-for-time txt))))
6406 (time-of-day (and dotime (org-get-time-of-day ts)))
6407 stamp plain s0 s1 s2 rtn srp l
6408 duration breadcrumbs)
6409 (and (derived-mode-p 'org-mode) buffer-file-name
6410 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6411 (when (and dotime time-of-day)
6412 ;; Extract starting and ending time and move them to prefix
6413 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6414 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6415 (setq s0 (match-string 0 ts)
6416 srp (and stamp (match-end 3))
6417 s1 (match-string (if plain 1 2) ts)
6418 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6420 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6421 ;; them, we might want to remove them there to avoid duplication.
6422 ;; The user can turn this off with a variable.
6423 (if (and org-prefix-has-time
6424 org-agenda-remove-times-when-in-prefix (or stamp plain)
6425 (string-match (concat (regexp-quote s0) " *") txt)
6426 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6427 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6428 (= (match-beginning 0) 0)
6430 (setq txt (replace-match "" nil nil txt))))
6431 ;; Normalize the time(s) to 24 hour
6432 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6433 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6435 ;; Try to set s2 if s1 and
6436 ;; `org-agenda-default-appointment-duration' are set
6437 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6438 (setq s2
6439 (org-duration-from-minutes
6440 (+ (org-duration-to-minutes s1 t)
6441 org-agenda-default-appointment-duration)
6442 nil t)))
6444 ;; Compute the duration
6445 (when s2
6446 (setq duration (- (org-duration-to-minutes s2)
6447 (org-duration-to-minutes s1)))))
6449 (when (string-match "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" txt)
6450 ;; Tags are in the string
6451 (if (or (eq org-agenda-remove-tags t)
6452 (and org-agenda-remove-tags
6453 org-prefix-has-tag))
6454 (setq txt (replace-match "" t t txt))
6455 (setq txt (replace-match
6456 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6457 (match-string 2 txt))
6458 t t txt))))
6460 (when remove-re
6461 (while (string-match remove-re txt)
6462 (setq txt (replace-match "" t t txt))))
6464 ;; Set org-heading property on `txt' to mark the start of the
6465 ;; heading.
6466 (add-text-properties 0 (length txt) '(org-heading t) txt)
6468 ;; Prepare the variables needed in the eval of the compiled format
6469 (if org-prefix-has-breadcrumbs
6470 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6471 (let ((s (org-display-outline-path nil nil "->" t)))
6472 (if (eq "" s) "" (concat s "->"))))))
6473 (setq time (cond (s2 (concat
6474 (org-agenda-time-of-day-to-ampm-maybe s1)
6475 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6476 (if org-agenda-timegrid-use-ampm " ")))
6477 (s1 (concat
6478 (org-agenda-time-of-day-to-ampm-maybe s1)
6479 (if org-agenda-timegrid-use-ampm
6480 "........ "
6481 "......")))
6482 (t ""))
6483 extra (or (and (not habitp) extra) "")
6484 category (if (symbolp category) (symbol-name category) category)
6485 level (or level ""))
6486 (if (string-match org-bracket-link-regexp category)
6487 (progn
6488 (setq l (if (match-end 3)
6489 (- (match-end 3) (match-beginning 3))
6490 (- (match-end 1) (match-beginning 1))))
6491 (when (< l (or org-prefix-category-length 0))
6492 (setq category (copy-sequence category))
6493 (org-add-props category nil
6494 'extra-space (make-string
6495 (- org-prefix-category-length l 1) ?\ ))))
6496 (if (and org-prefix-category-max-length
6497 (>= (length category) org-prefix-category-max-length))
6498 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6499 ;; Evaluate the compiled format
6500 (setq rtn (concat (eval formatter) txt))
6502 ;; And finally add the text properties
6503 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6504 (org-add-props rtn nil
6505 'org-category category
6506 'tags (mapcar 'org-downcase-keep-props tags)
6507 'org-highest-priority org-highest-priority
6508 'org-lowest-priority org-lowest-priority
6509 'time-of-day time-of-day
6510 'duration duration
6511 'breadcrumbs breadcrumbs
6512 'txt txt
6513 'level level
6514 'time time
6515 'extra extra
6516 'format org-prefix-format-compiled
6517 'dotime dotime)))))
6519 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6520 "Remove tags string from TXT, and add a modified list of tags.
6521 The modified list may contain inherited tags, and tags matched by
6522 `org-agenda-hide-tags-regexp' will be removed."
6523 (when (or add-inherited hide-re)
6524 (if (string-match "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" txt)
6525 (setq txt (substring txt 0 (match-beginning 0))))
6526 (setq tags
6527 (delq nil
6528 (mapcar (lambda (tg)
6529 (if (or (and hide-re (string-match hide-re tg))
6530 (and (not add-inherited)
6531 (get-text-property 0 'inherited tg)))
6533 tg))
6534 tags)))
6535 (when tags
6536 (let ((have-i (get-text-property 0 'inherited (car tags)))
6538 (setq txt (concat txt " :"
6539 (mapconcat
6540 (lambda (x)
6541 (setq i (get-text-property 0 'inherited x))
6542 (if (and have-i (not i))
6543 (progn
6544 (setq have-i nil)
6545 (concat ":" x))
6547 tags ":")
6548 (if have-i "::" ":"))))))
6549 txt)
6551 (defun org-downcase-keep-props (s)
6552 (let ((props (text-properties-at 0 s)))
6553 (setq s (downcase s))
6554 (add-text-properties 0 (length s) props s)
6557 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6559 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6560 "Add a time-grid for agenda items which need it.
6562 LIST is the list of agenda items formatted by `org-agenda-list'.
6563 NDAYS is the span of the current agenda view.
6564 TODAYP is t when the current agenda view is on today."
6565 (catch 'exit
6566 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6567 ((and todayp (member 'today (car org-agenda-time-grid))))
6568 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6569 ((member 'weekly (car org-agenda-time-grid)))
6570 (t (throw 'exit list)))
6571 (let* ((have (delq nil (mapcar
6572 (lambda (x) (get-text-property 1 'time-of-day x))
6573 list)))
6574 (string (nth 1 org-agenda-time-grid))
6575 (gridtimes (nth 2 org-agenda-time-grid))
6576 (req (car org-agenda-time-grid))
6577 (remove (member 'remove-match req))
6578 new time)
6579 (if (and (member 'require-timed req) (not have))
6580 ;; don't show empty grid
6581 (throw 'exit list))
6582 (while (setq time (pop gridtimes))
6583 (unless (and remove (member time have))
6584 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6585 (push (org-agenda-format-item
6586 nil string nil "" nil
6587 (concat (substring time 0 -2) ":" (substring time -2)))
6588 new)
6589 (put-text-property
6590 2 (length (car new)) 'face 'org-time-grid (car new))))
6591 (when (and todayp org-agenda-show-current-time-in-grid)
6592 (push (org-agenda-format-item
6593 nil org-agenda-current-time-string nil "" nil
6594 (format-time-string "%H:%M "))
6595 new)
6596 (put-text-property
6597 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6599 (if (member 'time-up org-agenda-sorting-strategy-selected)
6600 (append new list)
6601 (append list new)))))
6603 (defun org-compile-prefix-format (key)
6604 "Compile the prefix format into a Lisp form that can be evaluated.
6605 The resulting form and associated variable bindings is returned
6606 and stored in the variable `org-prefix-format-compiled'."
6607 (setq org-prefix-has-time nil
6608 org-prefix-has-tag nil
6609 org-prefix-category-length nil
6610 org-prefix-has-effort nil
6611 org-prefix-has-breadcrumbs nil)
6612 (let ((s (cond
6613 ((stringp org-agenda-prefix-format)
6614 org-agenda-prefix-format)
6615 ((assq key org-agenda-prefix-format)
6616 (cdr (assq key org-agenda-prefix-format)))
6617 (t " %-12:c%?-12t% s")))
6618 (start 0)
6619 varform vars var e c f opt)
6620 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6621 s start)
6622 (setq var (or (cdr (assoc (match-string 4 s)
6623 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6624 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6625 'eval)
6626 c (or (match-string 3 s) "")
6627 opt (match-beginning 1)
6628 start (1+ (match-beginning 0)))
6629 (if (eq var 'time) (setq org-prefix-has-time t))
6630 (if (eq var 'tag) (setq org-prefix-has-tag t))
6631 (if (eq var 'effort) (setq org-prefix-has-effort t))
6632 (if (eq var 'breadcrumbs) (setq org-prefix-has-breadcrumbs t))
6633 (setq f (concat "%" (match-string 2 s) "s"))
6634 (when (eq var 'category)
6635 (setq org-prefix-category-length
6636 (floor (abs (string-to-number (match-string 2 s)))))
6637 (setq org-prefix-category-max-length
6638 (let ((x (match-string 2 s)))
6639 (save-match-data
6640 (if (string-match "\\.[0-9]+" x)
6641 (string-to-number (substring (match-string 0 x) 1)))))))
6642 (if (eq var 'eval)
6643 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6644 (if opt
6645 (setq varform
6646 `(if (or (equal "" ,var) (equal nil ,var))
6648 (format ,f (concat ,var ,c))))
6649 (setq varform
6650 `(format ,f (if (or (equal ,var "")
6651 (equal ,var nil)) ""
6652 (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6653 (setq s (replace-match "%s" t nil s))
6654 (push varform vars))
6655 (setq vars (nreverse vars))
6656 (with-current-buffer (or org-agenda-buffer (current-buffer))
6657 (setq org-prefix-format-compiled
6658 (list
6659 `((org-prefix-has-time ,org-prefix-has-time)
6660 (org-prefix-has-tag ,org-prefix-has-tag)
6661 (org-prefix-category-length ,org-prefix-category-length)
6662 (org-prefix-has-effort ,org-prefix-has-effort)
6663 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6664 `(format ,s ,@vars))))))
6666 (defun org-set-sorting-strategy (key)
6667 (if (symbolp (car org-agenda-sorting-strategy))
6668 ;; the old format
6669 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6670 (setq org-agenda-sorting-strategy-selected
6671 (or (cdr (assq key org-agenda-sorting-strategy))
6672 (cdr (assq 'agenda org-agenda-sorting-strategy))
6673 '(time-up category-keep priority-down)))))
6675 (defun org-get-time-of-day (s &optional string mod24)
6676 "Check string S for a time of day.
6677 If found, return it as a military time number between 0 and 2400.
6678 If not found, return nil.
6679 The optional STRING argument forces conversion into a 5 character wide string
6680 HH:MM."
6681 (save-match-data
6682 (when
6683 (and
6684 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6685 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6686 (not (eq (get-text-property 1 'face s) 'org-link)))
6687 (let* ((h (string-to-number (match-string 1 s)))
6688 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6689 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6690 (am-p (equal ampm "am"))
6691 (h1 (cond ((not ampm) h)
6692 ((= h 12) (if am-p 0 12))
6693 (t (+ h (if am-p 0 12)))))
6694 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6695 (mod h1 24) h1))
6696 (t0 (+ (* 100 h2) m))
6697 (t1 (concat (if (>= h1 24) "+" " ")
6698 (if (and org-agenda-time-leading-zero
6699 (< t0 1000)) "0" "")
6700 (if (< t0 100) "0" "")
6701 (if (< t0 10) "0" "")
6702 (int-to-string t0))))
6703 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6705 (defvar org-agenda-before-sorting-filter-function nil
6706 "Function to be applied to agenda items prior to sorting.
6707 Prior to sorting also means just before they are inserted into the agenda.
6709 To aid sorting, you may revisit the original entries and add more text
6710 properties which will later be used by the sorting functions.
6712 The function should take a string argument, an agenda line.
6713 It has access to the text properties in that line, which contain among
6714 other things, the property `org-hd-marker' that points to the entry
6715 where the line comes from. Note that not all lines going into the agenda
6716 have this property, only most.
6718 The function should return the modified string. It is probably best
6719 to ONLY change text properties.
6721 You can also use this function as a filter, by returning nil for lines
6722 you don't want to have in the agenda at all. For this application, you
6723 could bind the variable in the options section of a custom command.")
6725 (defun org-agenda-finalize-entries (list &optional type)
6726 "Sort, limit and concatenate the LIST of agenda items.
6727 The optional argument TYPE tells the agenda type."
6728 (let ((max-effort (cond ((listp org-agenda-max-effort)
6729 (cdr (assoc type org-agenda-max-effort)))
6730 (t org-agenda-max-effort)))
6731 (max-todo (cond ((listp org-agenda-max-todos)
6732 (cdr (assoc type org-agenda-max-todos)))
6733 (t org-agenda-max-todos)))
6734 (max-tags (cond ((listp org-agenda-max-tags)
6735 (cdr (assoc type org-agenda-max-tags)))
6736 (t org-agenda-max-tags)))
6737 (max-entries (cond ((listp org-agenda-max-entries)
6738 (cdr (assoc type org-agenda-max-entries)))
6739 (t org-agenda-max-entries))))
6740 (when org-agenda-before-sorting-filter-function
6741 (setq list
6742 (delq nil
6743 (mapcar
6744 org-agenda-before-sorting-filter-function list))))
6745 (setq list (mapcar 'org-agenda-highlight-todo list)
6746 list (mapcar 'identity (sort list 'org-entries-lessp)))
6747 (when max-effort
6748 (setq list (org-agenda-limit-entries
6749 list 'effort-minutes max-effort
6750 (lambda (e) (or e (if org-sort-agenda-noeffort-is-high
6751 32767 -1))))))
6752 (when max-todo
6753 (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
6754 (when max-tags
6755 (setq list (org-agenda-limit-entries list 'tags max-tags)))
6756 (when max-entries
6757 (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
6758 (mapconcat 'identity list "\n")))
6760 (defun org-agenda-limit-entries (list prop limit &optional fn)
6761 "Limit the number of agenda entries."
6762 (let ((include (and limit (< limit 0))))
6763 (if limit
6764 (let ((fun (or fn (lambda (p) (if p 1))))
6765 (lim 0))
6766 (delq nil
6767 (mapcar
6768 (lambda (e)
6769 (let ((pval (funcall
6770 fun (get-text-property (1- (length e))
6771 prop e))))
6772 (if pval (setq lim (+ lim pval)))
6773 (cond ((and pval (<= lim (abs limit))) e)
6774 ((and include (not pval)) e))))
6775 list)))
6776 list)))
6778 (defun org-agenda-limit-interactively (remove)
6779 "In agenda, interactively limit entries to various maximums."
6780 (interactive "P")
6781 (if remove
6782 (progn (setq org-agenda-max-entries nil
6783 org-agenda-max-todos nil
6784 org-agenda-max-tags nil
6785 org-agenda-max-effort nil)
6786 (org-agenda-redo))
6787 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6788 (msg (cond ((= max ?E) "How many minutes? ")
6789 ((= max ?e) "How many entries? ")
6790 ((= max ?t) "How many TODO entries? ")
6791 ((= max ?T) "How many tagged entries? ")
6792 (t (user-error "Wrong input"))))
6793 (num (string-to-number (read-from-minibuffer msg))))
6794 (cond ((equal max ?e)
6795 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6796 ((equal max ?t)
6797 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6798 ((equal max ?T)
6799 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6800 ((equal max ?E)
6801 (let ((org-agenda-max-effort num)) (org-agenda-redo))))))
6802 (org-agenda-fit-window-to-buffer))
6804 (defun org-agenda-highlight-todo (x)
6805 (let ((org-done-keywords org-done-keywords-for-agenda)
6806 (case-fold-search nil)
6808 (if (eq x 'line)
6809 (save-excursion
6810 (beginning-of-line 1)
6811 (setq re (org-get-at-bol 'org-todo-regexp))
6812 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6813 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6814 (add-text-properties (match-beginning 0) (match-end 1)
6815 (list 'face (org-get-todo-face 1)))
6816 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6817 (delete-region (match-beginning 1) (1- (match-end 0)))
6818 (goto-char (match-beginning 1))
6819 (insert (format org-agenda-todo-keyword-format s)))))
6820 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6821 (setq re (get-text-property 0 'org-todo-regexp x))
6822 (when (and re
6823 ;; Test `pl' because if there's no heading content,
6824 ;; there's no point matching to highlight. Note
6825 ;; that if we didn't test `pl' first, and there
6826 ;; happened to be no keyword from `org-todo-regexp'
6827 ;; on this heading line, then the `equal' comparison
6828 ;; afterwards would spuriously succeed in the case
6829 ;; where `pl' is nil -- causing an args-out-of-range
6830 ;; error when we try to add text properties to text
6831 ;; that isn't there.
6833 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6834 x pl) pl))
6835 (add-text-properties
6836 (or (match-end 1) (match-end 0)) (match-end 0)
6837 (list 'face (org-get-todo-face (match-string 2 x)))
6839 (when (match-end 1)
6840 (setq x (concat (substring x 0 (match-end 1))
6841 (format org-agenda-todo-keyword-format
6842 (match-string 2 x))
6843 (org-add-props " " (text-properties-at 0 x))
6844 (substring x (match-end 3)))))))
6845 x)))
6847 (defsubst org-cmp-values (a b property)
6848 "Compare the numeric value of text PROPERTY for string A and B."
6849 (let ((pa (or (get-text-property (1- (length a)) property a) 0))
6850 (pb (or (get-text-property (1- (length b)) property b) 0)))
6851 (cond ((> pa pb) +1)
6852 ((< pa pb) -1))))
6854 (defsubst org-cmp-effort (a b)
6855 "Compare the effort values of string A and B."
6856 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6857 ;; `effort-minutes' property is not directly accessible from
6858 ;; the strings, but is stored as a property in `txt'.
6859 (ea (or (get-text-property
6860 0 'effort-minutes (get-text-property 0 'txt a))
6861 def))
6862 (eb (or (get-text-property
6863 0 'effort-minutes (get-text-property 0 'txt b))
6864 def)))
6865 (cond ((> ea eb) +1)
6866 ((< ea eb) -1))))
6868 (defsubst org-cmp-category (a b)
6869 "Compare the string values of categories of strings A and B."
6870 (let ((ca (or (get-text-property (1- (length a)) 'org-category a) ""))
6871 (cb (or (get-text-property (1- (length b)) 'org-category b) "")))
6872 (cond ((string-lessp ca cb) -1)
6873 ((string-lessp cb ca) +1))))
6875 (defsubst org-cmp-todo-state (a b)
6876 "Compare the todo states of strings A and B."
6877 (let* ((ma (or (get-text-property 1 'org-marker a)
6878 (get-text-property 1 'org-hd-marker a)))
6879 (mb (or (get-text-property 1 'org-marker b)
6880 (get-text-property 1 'org-hd-marker b)))
6881 (fa (and ma (marker-buffer ma)))
6882 (fb (and mb (marker-buffer mb)))
6883 (todo-kwds
6884 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6885 (and fb (with-current-buffer fb org-todo-keywords-1))))
6886 (ta (or (get-text-property 1 'todo-state a) ""))
6887 (tb (or (get-text-property 1 'todo-state b) ""))
6888 (la (- (length (member ta todo-kwds))))
6889 (lb (- (length (member tb todo-kwds))))
6890 (donepa (member ta org-done-keywords-for-agenda))
6891 (donepb (member tb org-done-keywords-for-agenda)))
6892 (cond ((and donepa (not donepb)) -1)
6893 ((and (not donepa) donepb) +1)
6894 ((< la lb) -1)
6895 ((< lb la) +1))))
6897 (defsubst org-cmp-alpha (a b)
6898 "Compare the headlines, alphabetically."
6899 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6900 (plb (text-property-any 0 (length b) 'org-heading t b))
6901 (ta (and pla (substring a pla)))
6902 (tb (and plb (substring b plb)))
6903 (case-fold-search nil))
6904 (when pla
6905 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6906 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6907 (setq ta (substring ta (match-end 0))))
6908 (setq ta (downcase ta)))
6909 (when plb
6910 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6911 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6912 (setq tb (substring tb (match-end 0))))
6913 (setq tb (downcase tb)))
6914 (cond ((not ta) +1)
6915 ((not tb) -1)
6916 ((string-lessp ta tb) -1)
6917 ((string-lessp tb ta) +1))))
6919 (defsubst org-cmp-tag (a b)
6920 "Compare the string values of the first tags of A and B."
6921 (let ((ta (car (last (get-text-property 1 'tags a))))
6922 (tb (car (last (get-text-property 1 'tags b)))))
6923 (cond ((not ta) +1)
6924 ((not tb) -1)
6925 ((string-lessp ta tb) -1)
6926 ((string-lessp tb ta) +1))))
6928 (defsubst org-cmp-time (a b)
6929 "Compare the time-of-day values of strings A and B."
6930 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6931 (ta (or (get-text-property 1 'time-of-day a) def))
6932 (tb (or (get-text-property 1 'time-of-day b) def)))
6933 (cond ((< ta tb) -1)
6934 ((< tb ta) +1))))
6936 (defsubst org-cmp-ts (a b type)
6937 "Compare the timestamps values of entries A and B.
6938 When TYPE is \"scheduled\", \"deadline\", \"timestamp\" or
6939 \"timestamp_ia\", compare within each of these type. When TYPE
6940 is the empty string, compare all timestamps without respect of
6941 their type."
6942 (let* ((def (if org-sort-agenda-notime-is-late most-positive-fixnum -1))
6943 (ta (or (and (string-match type (or (get-text-property 1 'type a) ""))
6944 (get-text-property 1 'ts-date a))
6945 def))
6946 (tb (or (and (string-match type (or (get-text-property 1 'type b) ""))
6947 (get-text-property 1 'ts-date b))
6948 def)))
6949 (cond ((< ta tb) -1)
6950 ((< tb ta) +1))))
6952 (defsubst org-cmp-habit-p (a b)
6953 "Compare the todo states of strings A and B."
6954 (let ((ha (get-text-property 1 'org-habit-p a))
6955 (hb (get-text-property 1 'org-habit-p b)))
6956 (cond ((and ha (not hb)) -1)
6957 ((and (not ha) hb) +1))))
6959 (defun org-entries-lessp (a b)
6960 "Predicate for sorting agenda entries."
6961 ;; The following variables will be used when the form is evaluated.
6962 ;; So even though the compiler complains, keep them.
6963 (let* ((ss org-agenda-sorting-strategy-selected)
6964 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
6965 (org-cmp-ts a b "")))
6966 (timestamp-down (if timestamp-up (- timestamp-up) nil))
6967 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
6968 (org-cmp-ts a b "scheduled")))
6969 (scheduled-down (if scheduled-up (- scheduled-up) nil))
6970 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
6971 (org-cmp-ts a b "deadline")))
6972 (deadline-down (if deadline-up (- deadline-up) nil))
6973 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
6974 (org-cmp-ts a b "timestamp_ia")))
6975 (tsia-down (if tsia-up (- tsia-up) nil))
6976 (ts-up (and (org-em 'ts-up 'ts-down ss)
6977 (org-cmp-ts a b "timestamp")))
6978 (ts-down (if ts-up (- ts-up) nil))
6979 (time-up (and (org-em 'time-up 'time-down ss)
6980 (org-cmp-time a b)))
6981 (time-down (if time-up (- time-up) nil))
6982 (stats-up (and (org-em 'stats-up 'stats-down ss)
6983 (org-cmp-values a b 'org-stats)))
6984 (stats-down (if stats-up (- stats-up) nil))
6985 (priority-up (and (org-em 'priority-up 'priority-down ss)
6986 (org-cmp-values a b 'priority)))
6987 (priority-down (if priority-up (- priority-up) nil))
6988 (effort-up (and (org-em 'effort-up 'effort-down ss)
6989 (org-cmp-effort a b)))
6990 (effort-down (if effort-up (- effort-up) nil))
6991 (category-up (and (or (org-em 'category-up 'category-down ss)
6992 (memq 'category-keep ss))
6993 (org-cmp-category a b)))
6994 (category-down (if category-up (- category-up) nil))
6995 (category-keep (if category-up +1 nil))
6996 (tag-up (and (org-em 'tag-up 'tag-down ss)
6997 (org-cmp-tag a b)))
6998 (tag-down (if tag-up (- tag-up) nil))
6999 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
7000 (org-cmp-todo-state a b)))
7001 (todo-state-down (if todo-state-up (- todo-state-up) nil))
7002 (habit-up (and (org-em 'habit-up 'habit-down ss)
7003 (org-cmp-habit-p a b)))
7004 (habit-down (if habit-up (- habit-up) nil))
7005 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
7006 (org-cmp-alpha a b)))
7007 (alpha-down (if alpha-up (- alpha-up) nil))
7008 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
7009 user-defined-up user-defined-down)
7010 (if (and need-user-cmp org-agenda-cmp-user-defined
7011 (functionp org-agenda-cmp-user-defined))
7012 (setq user-defined-up
7013 (funcall org-agenda-cmp-user-defined a b)
7014 user-defined-down (if user-defined-up (- user-defined-up) nil)))
7015 (cdr (assoc
7016 (eval (cons 'or org-agenda-sorting-strategy-selected))
7017 '((-1 . t) (1 . nil) (nil . nil))))))
7019 ;;; Agenda restriction lock
7021 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
7022 "Overlay to mark the headline to which agenda commands are restricted.")
7023 (overlay-put org-agenda-restriction-lock-overlay
7024 'face 'org-agenda-restriction-lock)
7025 (overlay-put org-agenda-restriction-lock-overlay
7026 'help-echo "Agendas are currently limited to this subtree.")
7027 (delete-overlay org-agenda-restriction-lock-overlay)
7029 (defun org-agenda-set-restriction-lock-from-agenda (arg)
7030 "Set the restriction lock to the agenda item at point from within the agenda.
7031 When called with a `\\[universal-argument]' prefix, restrict to
7032 the file which contains the item.
7033 Argument ARG is the prefix argument."
7034 (interactive "P")
7035 (unless (derived-mode-p 'org-agenda-mode)
7036 (user-error "Not in an Org agenda buffer"))
7037 (let* ((marker (or (org-get-at-bol 'org-marker)
7038 (org-agenda-error)))
7039 (buffer (marker-buffer marker))
7040 (pos (marker-position marker)))
7041 (with-current-buffer buffer
7042 (goto-char pos)
7043 (org-agenda-set-restriction-lock arg))))
7045 ;;;###autoload
7046 (defun org-agenda-set-restriction-lock (&optional type)
7047 "Set restriction lock for agenda, to current subtree or file.
7048 Restriction will be the file if TYPE is `file', or if type is the
7049 universal prefix \\='(4), or if the cursor is before the first headline
7050 in the file. Otherwise, restriction will be to the current subtree."
7051 (interactive "P")
7052 (org-agenda-remove-restriction-lock 'noupdate)
7053 (and (equal type '(4)) (setq type 'file))
7054 (setq type (cond
7055 (type type)
7056 ((org-at-heading-p) 'subtree)
7057 ((condition-case nil (org-back-to-heading t) (error nil))
7058 'subtree)
7059 (t 'file)))
7060 (if (eq type 'subtree)
7061 (progn
7062 (setq org-agenda-restrict (current-buffer))
7063 (setq org-agenda-overriding-restriction 'subtree)
7064 (put 'org-agenda-files 'org-restrict
7065 (list (buffer-file-name (buffer-base-buffer))))
7066 (org-back-to-heading t)
7067 (move-overlay org-agenda-restriction-lock-overlay
7068 (point)
7069 (if org-agenda-restriction-lock-highlight-subtree
7070 (save-excursion (org-end-of-subtree t t) (point))
7071 (point-at-eol)))
7072 (move-marker org-agenda-restrict-begin (point))
7073 (move-marker org-agenda-restrict-end
7074 (save-excursion (org-end-of-subtree t t)))
7075 (message "Locking agenda restriction to subtree"))
7076 (put 'org-agenda-files 'org-restrict
7077 (list (buffer-file-name (buffer-base-buffer))))
7078 (setq org-agenda-restrict nil)
7079 (setq org-agenda-overriding-restriction 'file)
7080 (move-marker org-agenda-restrict-begin nil)
7081 (move-marker org-agenda-restrict-end nil)
7082 (message "Locking agenda restriction to file"))
7083 (setq current-prefix-arg nil)
7084 (org-agenda-maybe-redo))
7086 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7087 "Remove the agenda restriction lock."
7088 (interactive "P")
7089 (delete-overlay org-agenda-restriction-lock-overlay)
7090 (delete-overlay org-speedbar-restriction-lock-overlay)
7091 (setq org-agenda-overriding-restriction nil)
7092 (setq org-agenda-restrict nil)
7093 (put 'org-agenda-files 'org-restrict nil)
7094 (move-marker org-agenda-restrict-begin nil)
7095 (move-marker org-agenda-restrict-end nil)
7096 (setq current-prefix-arg nil)
7097 (message "Agenda restriction lock removed")
7098 (or noupdate (org-agenda-maybe-redo)))
7100 (defun org-agenda-maybe-redo ()
7101 "If there is any window showing the agenda view, update it."
7102 (let ((w (get-buffer-window (or org-agenda-this-buffer-name
7103 org-agenda-buffer-name)
7105 (w0 (selected-window)))
7106 (when w
7107 (select-window w)
7108 (org-agenda-redo)
7109 (select-window w0)
7110 (if org-agenda-overriding-restriction
7111 (message "Agenda view shifted to new %s restriction"
7112 org-agenda-overriding-restriction)
7113 (message "Agenda restriction lock removed")))))
7115 ;;; Agenda commands
7117 (defun org-agenda-check-type (error &rest types)
7118 "Check if agenda buffer is of allowed type.
7119 If ERROR is non-nil, throw an error, otherwise just return nil.
7120 Allowed types are `agenda' `timeline' `todo' `tags' `search'."
7121 (if (not org-agenda-type)
7122 (error "No Org agenda currently displayed")
7123 (if (memq org-agenda-type types)
7125 (if error
7126 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
7127 nil))))
7129 (defun org-agenda-Quit ()
7130 "Exit the agenda, killing the agenda buffer.
7131 Like `org-agenda-quit', but kill the buffer even when
7132 `org-agenda-sticky' is non-nil."
7133 (interactive)
7134 (org-agenda--quit))
7136 (defun org-agenda-quit ()
7137 "Exit the agenda.
7139 When `org-agenda-sticky' is non-nil, bury the agenda buffer
7140 instead of killing it.
7142 When `org-agenda-restore-windows-after-quit' is non-nil, restore
7143 the pre-agenda window configuration.
7145 When column view is active, exit column view instead of the
7146 agenda."
7147 (interactive)
7148 (org-agenda--quit org-agenda-sticky))
7150 (defun org-agenda--quit (&optional bury)
7151 (if org-agenda-columns-active
7152 (org-columns-quit)
7153 (let ((wconf org-agenda-pre-window-conf)
7154 (buf (current-buffer))
7155 (org-agenda-last-indirect-window
7156 (and (eq org-indirect-buffer-display 'other-window)
7157 org-agenda-last-indirect-buffer
7158 (get-buffer-window org-agenda-last-indirect-buffer))))
7159 (cond
7160 ((eq org-agenda-window-setup 'other-frame)
7161 (delete-frame))
7162 ((and org-agenda-restore-windows-after-quit
7163 wconf)
7164 ;; Maybe restore the pre-agenda window configuration. Reset
7165 ;; `org-agenda-pre-window-conf' before running
7166 ;; `set-window-configuration', which loses the current buffer.
7167 (setq org-agenda-pre-window-conf nil)
7168 (set-window-configuration wconf))
7170 (when org-agenda-last-indirect-window
7171 (delete-window org-agenda-last-indirect-window))
7172 (and (not (eq org-agenda-window-setup 'current-window))
7173 (not (one-window-p))
7174 (delete-window))))
7175 (if bury
7176 ;; Set the agenda buffer as the current buffer instead of
7177 ;; passing it as an argument to `bury-buffer' so that
7178 ;; `bury-buffer' removes it from the window.
7179 (with-current-buffer buf
7180 (bury-buffer))
7181 (kill-buffer buf)
7182 (setq org-agenda-archives-mode nil
7183 org-agenda-buffer nil)))))
7185 (defun org-agenda-exit ()
7186 "Exit the agenda, killing Org buffers loaded by the agenda.
7187 Like `org-agenda-Quit', but kill any buffers that were created by
7188 the agenda. Org buffers visited directly by the user will not be
7189 touched. Also, exit the agenda even if it is in column view."
7190 (interactive)
7191 (when org-agenda-columns-active
7192 (org-columns-quit))
7193 (org-release-buffers org-agenda-new-buffers)
7194 (setq org-agenda-new-buffers nil)
7195 (org-agenda-Quit))
7197 (defun org-agenda-kill-all-agenda-buffers ()
7198 "Kill all buffers in `org-agenda-mode'.
7199 This is used when toggling sticky agendas."
7200 (interactive)
7201 (let (blist)
7202 (dolist (buf (buffer-list))
7203 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7204 (push buf blist)))
7205 (mapc 'kill-buffer blist)))
7207 (defun org-agenda-execute (arg)
7208 "Execute another agenda command, keeping same window.
7209 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7210 in the agenda."
7211 (interactive "P")
7212 (let ((org-agenda-window-setup 'current-window))
7213 (org-agenda arg)))
7215 (defun org-agenda-redo (&optional all)
7216 "Rebuild possibly ALL agenda view(s) in the current buffer."
7217 (interactive "P")
7218 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7219 (cpa (unless (eq all t) current-prefix-arg))
7220 (org-agenda-doing-sticky-redo org-agenda-sticky)
7221 (org-agenda-sticky nil)
7222 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7223 org-agenda-buffer-name))
7224 (org-agenda-keep-modes t)
7225 (tag-filter org-agenda-tag-filter)
7226 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7227 (top-hl-filter org-agenda-top-headline-filter)
7228 (cat-filter org-agenda-category-filter)
7229 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7230 (re-filter org-agenda-regexp-filter)
7231 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7232 (effort-filter org-agenda-effort-filter)
7233 (effort-preset (get 'org-agenda-effort-filter :preset-filter))
7234 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7235 (cols org-agenda-columns-active)
7236 (line (org-current-line))
7237 (window-line (- line (org-current-line (window-start))))
7238 (lprops (get 'org-agenda-redo-command 'org-lprops))
7239 (redo-cmd (get-text-property p 'org-redo-cmd))
7240 (last-args (get-text-property p 'org-last-args))
7241 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7242 (org-agenda-overriding-cmd-arguments
7243 (unless (eq all t)
7244 (cond ((listp last-args)
7245 (cons (or cpa (car last-args)) (cdr last-args)))
7246 ((stringp last-args)
7247 last-args))))
7248 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7249 (put 'org-agenda-tag-filter :preset-filter nil)
7250 (put 'org-agenda-category-filter :preset-filter nil)
7251 (put 'org-agenda-regexp-filter :preset-filter nil)
7252 (put 'org-agenda-effort-filter :preset-filter nil)
7253 (and cols (org-columns-quit))
7254 (message "Rebuilding agenda buffer...")
7255 (if series-redo-cmd
7256 (eval series-redo-cmd)
7257 (org-let lprops redo-cmd))
7258 (setq org-agenda-undo-list nil
7259 org-agenda-pending-undo-list nil
7260 org-agenda-tag-filter tag-filter
7261 org-agenda-category-filter cat-filter
7262 org-agenda-regexp-filter re-filter
7263 org-agenda-effort-filter effort-filter
7264 org-agenda-top-headline-filter top-hl-filter)
7265 (message "Rebuilding agenda buffer...done")
7266 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7267 (put 'org-agenda-category-filter :preset-filter cat-preset)
7268 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7269 (put 'org-agenda-effort-filter :preset-filter effort-preset)
7270 (let ((tag (or tag-filter tag-preset))
7271 (cat (or cat-filter cat-preset))
7272 (effort (or effort-filter effort-preset))
7273 (re (or re-filter re-preset)))
7274 (when tag (org-agenda-filter-apply tag 'tag t))
7275 (when cat (org-agenda-filter-apply cat 'category))
7276 (when effort (org-agenda-filter-apply effort 'effort))
7277 (when re (org-agenda-filter-apply re 'regexp)))
7278 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7279 (and cols (called-interactively-p 'any) (org-agenda-columns))
7280 (org-goto-line line)
7281 (recenter window-line)))
7283 (defun org-agenda-redo-all (&optional exhaustive)
7284 "Rebuild all agenda views in the current buffer.
7285 With a prefix argument, do so in all agenda buffers."
7286 (interactive "P")
7287 (if exhaustive
7288 (dolist (buffer (buffer-list))
7289 (with-current-buffer buffer
7290 (when (derived-mode-p 'org-agenda-mode)
7291 (org-agenda-redo t))))
7292 (org-agenda-redo t)))
7294 (defvar org-global-tags-completion-table nil)
7295 (defvar org-agenda-filter-form nil)
7296 (defvar org-agenda-filtered-by-category nil)
7298 (defun org-agenda-filter-by-category (strip)
7299 "Filter lines in the agenda buffer that have a specific category.
7300 The category is that of the current line.
7301 Without prefix argument, keep only the lines of that category.
7302 With a prefix argument, exclude the lines of that category.
7304 (interactive "P")
7305 (if (and org-agenda-filtered-by-category
7306 org-agenda-category-filter)
7307 (org-agenda-filter-show-all-cat)
7308 (let ((cat (org-no-properties (org-get-at-eol 'org-category 1))))
7309 (cond
7310 ((and cat strip)
7311 (org-agenda-filter-apply
7312 (push (concat "-" cat) org-agenda-category-filter) 'category))
7313 (cat
7314 (org-agenda-filter-apply
7315 (setq org-agenda-category-filter
7316 (list (concat "+" cat))) 'category))
7317 (t (error "No category at point"))))))
7319 (defun org-find-top-headline (&optional pos)
7320 "Find the topmost parent headline and return it.
7321 POS when non-nil is the marker or buffer position to start the
7322 search from."
7323 (save-excursion
7324 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
7325 (when pos (goto-char pos))
7326 ;; Skip up to the topmost parent.
7327 (while (org-up-heading-safe))
7328 (ignore-errors (nth 4 (org-heading-components))))))
7330 (defvar org-agenda-filtered-by-top-headline nil)
7331 (defun org-agenda-filter-by-top-headline (strip)
7332 "Keep only those lines that are descendants from the same top headline.
7333 The top headline is that of the current line."
7334 (interactive "P")
7335 (if org-agenda-filtered-by-top-headline
7336 (progn
7337 (setq org-agenda-filtered-by-top-headline nil
7338 org-agenda-top-headline-filter nil)
7339 (org-agenda-filter-show-all-top-filter))
7340 (let ((toph (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7341 (if toph (org-agenda-filter-top-headline-apply toph strip)
7342 (error "No top-level headline at point")))))
7344 (defvar org-agenda-regexp-filter nil)
7345 (defun org-agenda-filter-by-regexp (strip)
7346 "Filter agenda entries by a regular expression.
7347 Regexp filters are cumulative.
7348 With no prefix argument, keep entries matching the regexp.
7349 With one prefix argument, filter out entries matching the regexp.
7350 With two prefix arguments, remove the regexp filters."
7351 (interactive "P")
7352 (if (not (equal strip '(16)))
7353 (let ((flt (concat (if (equal strip '(4)) "-" "+")
7354 (read-from-minibuffer
7355 (if (equal strip '(4))
7356 "Filter out entries matching regexp: "
7357 "Narrow to entries matching regexp: ")))))
7358 (push flt org-agenda-regexp-filter)
7359 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
7360 (org-agenda-filter-show-all-re)
7361 (message "Regexp filter removed")))
7363 (defvar org-agenda-effort-filter nil)
7364 (defun org-agenda-filter-by-effort (strip)
7365 "Filter agenda entries by effort.
7366 With no prefix argument, keep entries matching the effort condition.
7367 With one prefix argument, filter out entries matching the condition.
7368 With two prefix arguments, remove the effort filters."
7369 (interactive "P")
7370 (cond
7371 ((member strip '(nil 4))
7372 (let* ((efforts (split-string
7373 (or (cdr (assoc (concat org-effort-property "_ALL")
7374 org-global-properties))
7375 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00")))
7376 ;; XXX: the following handles only up to 10 different
7377 ;; effort values.
7378 (allowed-keys (if (null efforts) nil
7379 (mapcar (lambda (n) (mod n 10)) ;turn 10 into 0
7380 (number-sequence 1 (length efforts)))))
7381 (op nil))
7382 (while (not (memq op '(?< ?> ?=)))
7383 (setq op (read-char-exclusive "Effort operator? (> = or <)")))
7384 ;; Select appropriate duration. Ignore non-digit characters.
7385 (let ((prompt
7386 (apply #'format
7387 (concat "Effort %c "
7388 (mapconcat (lambda (s) (concat "[%d]" s))
7389 efforts
7390 " "))
7391 op allowed-keys))
7392 (eff -1))
7393 (while (not (memq eff allowed-keys))
7394 (message prompt)
7395 (setq eff (- (read-char-exclusive) 48)))
7396 (setq org-agenda-effort-filter
7397 (list (concat (if strip "-" "+")
7398 (char-to-string op)
7399 ;; Numbering is 1 2 3 ... 9 0, but we want
7400 ;; 0 1 2 ... 8 9.
7401 (nth (mod (1- eff) 10) efforts)))))
7402 (org-agenda-filter-apply org-agenda-effort-filter 'effort)))
7403 (t (org-agenda-filter-show-all-effort)
7404 (message "Effort filter removed"))))
7406 (defun org-agenda-filter-remove-all ()
7407 "Remove all filters from the current agenda buffer."
7408 (interactive)
7409 (when org-agenda-tag-filter
7410 (org-agenda-filter-show-all-tag))
7411 (when org-agenda-category-filter
7412 (org-agenda-filter-show-all-cat))
7413 (when org-agenda-regexp-filter
7414 (org-agenda-filter-show-all-re))
7415 (when org-agenda-top-headline-filter
7416 (org-agenda-filter-show-all-top-filter))
7417 (when org-agenda-effort-filter
7418 (org-agenda-filter-show-all-effort))
7419 (org-agenda-finalize))
7421 (defun org-agenda-filter-by-tag (arg &optional char exclude)
7422 "Keep only those lines in the agenda buffer that have a specific tag.
7424 The tag is selected with its fast selection letter, as configured.
7426 With a `\\[universal-argument]' prefix, exclude the agenda search.
7428 With a `\\[universal-argument] \\[universal-argument]' prefix, filter the literal tag, \
7429 i.e. don't
7430 filter on all its group members.
7432 A lisp caller can specify CHAR. EXCLUDE means that the new tag
7433 should be used to exclude the search - the interactive user can
7434 also press `-' or `+' to switch between filtering and excluding."
7435 (interactive "P")
7436 (let* ((alist org-tag-alist-for-agenda)
7437 (tag-chars (mapconcat
7438 (lambda (x) (if (and (not (symbolp (car x)))
7439 (cdr x))
7440 (char-to-string (cdr x))
7441 ""))
7442 org-tag-alist-for-agenda ""))
7443 (valid-char-list (append '(?\t ?\r ?/ ?. ?\s ?q)
7444 (string-to-list tag-chars)))
7445 (exclude (or exclude (equal arg '(4))))
7446 (expand (not (equal arg '(16))))
7447 (inhibit-read-only t)
7448 (current org-agenda-tag-filter)
7449 a n tag)
7450 (unless char
7451 (while (not (memq char valid-char-list))
7452 (message
7453 "%s by tag [%s ]:tag-char, [TAB]:tag, %s[/]:off, [+/-]:filter/exclude%s, [q]:quit"
7454 (if exclude "Exclude" "Filter")
7455 tag-chars
7456 (if org-agenda-auto-exclude-function "[RET], " "")
7457 (if expand "" ", no grouptag expand"))
7458 (setq char (read-char-exclusive))
7459 ;; Excluding or filtering down
7460 (cond ((eq char ?-) (setq exclude t))
7461 ((eq char ?+) (setq exclude nil)))))
7462 (when (eq char ?\t)
7463 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7464 (setq-local org-global-tags-completion-table
7465 (org-global-tags-completion-table)))
7466 (let ((completion-ignore-case t))
7467 (setq tag (completing-read
7468 "Tag: " org-global-tags-completion-table))))
7469 (cond
7470 ((eq char ?\r)
7471 (org-agenda-filter-show-all-tag)
7472 (when org-agenda-auto-exclude-function
7473 (setq org-agenda-tag-filter nil)
7474 (dolist (tag (org-agenda-get-represented-tags))
7475 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7476 (if modifier
7477 (push modifier org-agenda-tag-filter))))
7478 (if (not (null org-agenda-tag-filter))
7479 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))))
7480 ((eq char ?/)
7481 (org-agenda-filter-show-all-tag)
7482 (when (get 'org-agenda-tag-filter :preset-filter)
7483 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand)))
7484 ((eq char ?.)
7485 (setq org-agenda-tag-filter
7486 (mapcar (lambda(tag) (concat "+" tag))
7487 (org-get-at-bol 'tags)))
7488 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7489 ((eq char ?q)) ;If q, abort (even if there is a q-key for a tag...)
7490 ((or (eq char ?\s)
7491 (setq a (rassoc char alist))
7492 (and tag (setq a (cons tag nil))))
7493 (org-agenda-filter-show-all-tag)
7494 (setq tag (car a))
7495 (setq org-agenda-tag-filter
7496 (cons (concat (if exclude "-" "+") tag)
7497 current))
7498 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7499 (t (error "Invalid tag selection character %c" char)))))
7501 (defun org-agenda-get-represented-tags ()
7502 "Get a list of all tags currently represented in the agenda."
7503 (let (p tags)
7504 (save-excursion
7505 (goto-char (point-min))
7506 (while (setq p (next-single-property-change (point) 'tags))
7507 (goto-char p)
7508 (mapc (lambda (x) (add-to-list 'tags x))
7509 (get-text-property (point) 'tags))))
7510 tags))
7513 (defun org-agenda-filter-make-matcher (filter type &optional expand)
7514 "Create the form that tests a line for agenda filter. Optional
7515 argument EXPAND can be used for the TYPE tag and will expand the
7516 tags in the FILTER if any of the tags in FILTER are grouptags."
7517 (let (f f1)
7518 (cond
7519 ;; Tag filter
7520 ((eq type 'tag)
7521 (setq filter
7522 (delete-dups
7523 (append (get 'org-agenda-tag-filter :preset-filter)
7524 filter)))
7525 (dolist (x filter)
7526 (let ((op (string-to-char x)))
7527 (if expand (setq x (org-agenda-filter-expand-tags (list x) t))
7528 (setq x (list x)))
7529 (setq f1 (org-agenda-filter-make-matcher-tag-exp x op))
7530 (push f1 f))))
7531 ;; Category filter
7532 ((eq type 'category)
7533 (setq filter
7534 (delete-dups
7535 (append (get 'org-agenda-category-filter :preset-filter)
7536 filter)))
7537 (dolist (x filter)
7538 (if (equal "-" (substring x 0 1))
7539 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7540 (setq f1 (list 'equal (substring x 1) 'cat)))
7541 (push f1 f)))
7542 ;; Regexp filter
7543 ((eq type 'regexp)
7544 (setq filter
7545 (delete-dups
7546 (append (get 'org-agenda-regexp-filter :preset-filter)
7547 filter)))
7548 (dolist (x filter)
7549 (if (equal "-" (substring x 0 1))
7550 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7551 (setq f1 (list 'string-match (substring x 1) 'txt)))
7552 (push f1 f)))
7553 ;; Effort filter
7554 ((eq type 'effort)
7555 (setq filter
7556 (delete-dups
7557 (append (get 'org-agenda-effort-filter :preset-filter)
7558 filter)))
7559 (dolist (x filter)
7560 (push (org-agenda-filter-effort-form x) f))))
7561 (cons 'and (nreverse f))))
7563 (defun org-agenda-filter-make-matcher-tag-exp (tags op)
7564 "Return a form associated to tag-expression TAGS.
7565 Build a form testing a line for agenda filter for
7566 tag-expressions. OP is an operator of type CHAR that allows the
7567 function to set the right switches in the returned form."
7568 (let (form)
7569 ;; Any of the expressions can match if OP is +, all must match if
7570 ;; the operator is -.
7571 (dolist (x tags (cons (if (eq op ?-) 'and 'or) form))
7572 (let* ((tag (substring x 1))
7573 (f (cond
7574 ((string= "" tag) '(not tags))
7575 ((and (string-match-p "\\`{" tag) (string-match-p "}\\'" tag))
7576 ;; TAG is a regexp.
7577 (list 'org-match-any-p (substring tag 1 -1) 'tags))
7578 (t (list 'member (downcase tag) 'tags)))))
7579 (push (if (eq op ?-) (list 'not f) f) form)))))
7581 (defun org-agenda-filter-effort-form (e)
7582 "Return the form to compare the effort of the current line with what E says.
7583 E looks like \"+<2:25\"."
7584 (let (op)
7585 (setq e (substring e 1))
7586 (setq op (string-to-char e) e (substring e 1))
7587 (setq op (cond ((equal op ?<) '<=)
7588 ((equal op ?>) '>=)
7589 ((equal op ??) op)
7590 (t '=)))
7591 (list 'org-agenda-compare-effort (list 'quote op)
7592 (org-duration-to-minutes e))))
7594 (defun org-agenda-compare-effort (op value)
7595 "Compare the effort of the current line with VALUE, using OP.
7596 If the line does not have an effort defined, return nil."
7597 ;; `effort-minutes' property cannot be extracted directly from
7598 ;; current line but is stored as a property in `txt'.
7599 (let ((effort (get-text-property 0 'effort-minutes (org-get-at-bol 'txt))))
7600 (funcall op
7601 (or effort (if org-sort-agenda-noeffort-is-high 32767 -1))
7602 value)))
7604 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7605 "Expand group tags in FILTER for the agenda.
7606 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7607 (if org-group-tags
7608 (let ((case-fold-search t) rtn)
7609 (mapc
7610 (lambda (f)
7611 (let (f0 dir)
7612 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7613 (setq dir (match-string 1 f) f0 (match-string 2 f))
7614 (setq dir (if no-operator "" "+") f0 f))
7615 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7616 (org-tags-expand f0 t t))
7617 rtn))))
7618 filter)
7619 (reverse rtn))
7620 filter))
7622 (defun org-agenda-filter-apply (filter type &optional expand)
7623 "Set FILTER as the new agenda filter and apply it. Optional
7624 argument EXPAND can be used for the TYPE tag and will expand the
7625 tags in the FILTER if any of the tags in FILTER are grouptags."
7626 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7627 (if org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7628 (let (tags cat txt)
7629 (setq org-agenda-filter-form (org-agenda-filter-make-matcher filter type expand))
7630 ;; Only set `org-agenda-filtered-by-category' to t when a unique
7631 ;; category is used as the filter:
7632 (setq org-agenda-filtered-by-category
7633 (and (eq type 'category)
7634 (not (equal (substring (car filter) 0 1) "-"))))
7635 (org-agenda-set-mode-name)
7636 (save-excursion
7637 (goto-char (point-min))
7638 (while (not (eobp))
7639 (if (org-get-at-bol 'org-marker)
7640 (progn
7641 (setq tags (org-get-at-bol 'tags)
7642 cat (org-get-at-eol 'org-category 1)
7643 txt (org-get-at-bol 'txt))
7644 (if (not (eval org-agenda-filter-form))
7645 (org-agenda-filter-hide-line type))
7646 (beginning-of-line 2))
7647 (beginning-of-line 2))))
7648 (if (get-char-property (point) 'invisible)
7649 (ignore-errors (org-agenda-previous-line)))))
7651 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7652 "Filter by top headline HL."
7653 (org-agenda-set-mode-name)
7654 (save-excursion
7655 (goto-char (point-min))
7656 (while (not (eobp))
7657 (let* ((pos (org-get-at-bol 'org-hd-marker))
7658 (tophl (and pos (org-find-top-headline pos))))
7659 (if (and tophl (funcall (if negative 'identity 'not)
7660 (string= hl tophl)))
7661 (org-agenda-filter-hide-line 'top-headline)))
7662 (beginning-of-line 2)))
7663 (if (get-char-property (point) 'invisible)
7664 (org-agenda-previous-line))
7665 (setq org-agenda-top-headline-filter hl
7666 org-agenda-filtered-by-top-headline t))
7668 (defun org-agenda-filter-hide-line (type)
7669 "Hide lines with TYPE in the agenda buffer."
7670 (let* ((b (max (point-min) (1- (point-at-bol))))
7671 (e (point-at-eol)))
7672 (let ((inhibit-read-only t))
7673 (add-text-properties
7674 b e `(invisible org-filtered org-filter-type ,type)))))
7676 (defun org-agenda-remove-filter (type)
7677 (interactive)
7678 "Remove filter of type TYPE from the agenda buffer."
7679 (save-excursion
7680 (goto-char (point-min))
7681 (let ((inhibit-read-only t) pos)
7682 (while (setq pos (text-property-any (point) (point-max) 'org-filter-type type))
7683 (goto-char pos)
7684 (remove-text-properties
7685 (point) (next-single-property-change (point) 'org-filter-type)
7686 `(invisible org-filtered org-filter-type ,type))))
7687 (set (intern (format "org-agenda-%s-filter" (intern-soft type))) nil)
7688 (setq org-agenda-filter-form nil)
7689 (org-agenda-set-mode-name)
7690 (org-agenda-finalize)))
7692 (defun org-agenda-filter-show-all-tag nil
7693 (org-agenda-remove-filter 'tag))
7694 (defun org-agenda-filter-show-all-re nil
7695 (org-agenda-remove-filter 'regexp))
7696 (defun org-agenda-filter-show-all-effort nil
7697 (org-agenda-remove-filter 'effort))
7698 (defun org-agenda-filter-show-all-cat nil
7699 (org-agenda-remove-filter 'category))
7700 (defun org-agenda-filter-show-all-top-filter nil
7701 (org-agenda-remove-filter 'top-headline))
7703 (defun org-agenda-manipulate-query-add ()
7704 "Manipulate the query by adding a search term with positive selection.
7705 Positive selection means the term must be matched for selection of an entry."
7706 (interactive)
7707 (org-agenda-manipulate-query ?\[))
7708 (defun org-agenda-manipulate-query-subtract ()
7709 "Manipulate the query by adding a search term with negative selection.
7710 Negative selection means term must not be matched for selection of an entry."
7711 (interactive)
7712 (org-agenda-manipulate-query ?\]))
7713 (defun org-agenda-manipulate-query-add-re ()
7714 "Manipulate the query by adding a search regexp with positive selection.
7715 Positive selection means the regexp must match for selection of an entry."
7716 (interactive)
7717 (org-agenda-manipulate-query ?\{))
7718 (defun org-agenda-manipulate-query-subtract-re ()
7719 "Manipulate the query by adding a search regexp with negative selection.
7720 Negative selection means regexp must not match for selection of an entry."
7721 (interactive)
7722 (org-agenda-manipulate-query ?\}))
7723 (defun org-agenda-manipulate-query (char)
7724 (cond
7725 ((memq org-agenda-type '(timeline agenda))
7726 (let ((org-agenda-include-inactive-timestamps t))
7727 (org-agenda-redo))
7728 (message "Display now includes inactive timestamps as well"))
7729 ((eq org-agenda-type 'search)
7730 (org-add-to-string
7731 'org-agenda-query-string
7732 (if org-agenda-last-search-view-search-was-boolean
7733 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7734 (?\{ . " +{}") (?\} . " -{}"))))
7735 " "))
7736 (setq org-agenda-redo-command
7737 (list 'org-search-view
7738 (car (get-text-property (min (1- (point-max)) (point))
7739 'org-last-args))
7740 org-agenda-query-string
7741 (+ (length org-agenda-query-string)
7742 (if (member char '(?\{ ?\})) 0 1))))
7743 (set-register org-agenda-query-register org-agenda-query-string)
7744 (let ((org-agenda-overriding-arguments
7745 (cdr org-agenda-redo-command)))
7746 (org-agenda-redo)))
7747 (t (error "Cannot manipulate query for %s-type agenda buffers"
7748 org-agenda-type))))
7750 (defun org-add-to-string (var string)
7751 (set var (concat (symbol-value var) string)))
7753 (defun org-agenda-goto-date (span)
7754 "Jump to DATE in agenda."
7755 (interactive "P")
7756 (let* ((org-read-date-prefer-future
7757 (eval org-agenda-jump-prefer-future))
7758 (date (org-read-date))
7759 (day (time-to-days (org-time-string-to-time date)))
7760 (org-agenda-sticky-orig org-agenda-sticky)
7761 (org-agenda-buffer-tmp-name (buffer-name))
7762 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7763 (0-arg (or current-prefix-arg (car args)))
7764 (2-arg (nth 2 args))
7765 (with-hour-p (nth 4 org-agenda-redo-command))
7766 (newcmd (list 'org-agenda-list 0-arg date
7767 (org-agenda-span-to-ndays
7768 2-arg (org-time-string-to-absolute date))
7769 with-hour-p))
7770 (newargs (cdr newcmd))
7771 (inhibit-read-only t)
7772 org-agenda-sticky)
7773 (if (not (org-agenda-check-type t 'agenda))
7774 (error "Not available in non-agenda views")
7775 (add-text-properties (point-min) (point-max)
7776 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7777 (org-agenda-redo)
7778 (goto-char (point-min))
7779 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7780 (save-excursion (move-beginning-of-line 2) (eobp))))
7781 (move-beginning-of-line 2))
7782 (setq org-agenda-sticky org-agenda-sticky-orig
7783 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7785 (defun org-agenda-goto-today ()
7786 "Go to today."
7787 (interactive)
7788 (org-agenda-check-type t 'timeline 'agenda)
7789 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7790 (curspan (nth 2 args))
7791 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7792 (cond
7793 (tdpos (goto-char tdpos))
7794 ((eq org-agenda-type 'agenda)
7795 (let* ((sd (org-agenda-compute-starting-span
7796 (org-today) (or curspan org-agenda-span)))
7797 (org-agenda-overriding-arguments args))
7798 (setf (nth 1 org-agenda-overriding-arguments) sd)
7799 (org-agenda-redo)
7800 (org-agenda-find-same-or-today-or-agenda)))
7801 (t (error "Cannot find today")))))
7803 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7804 (goto-char
7805 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7806 (text-property-any (point-min) (point-max) 'org-today t)
7807 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7808 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7809 (org-agenda-backward-block))
7810 (point-min))))
7812 (defun org-agenda-backward-block ()
7813 "Move backward by one agenda block."
7814 (interactive)
7815 (org-agenda-forward-block 'backward))
7817 (defun org-agenda-forward-block (&optional backward)
7818 "Move forward by one agenda block.
7819 When optional argument BACKWARD is set, go backward"
7820 (interactive)
7821 (cond ((not (derived-mode-p 'org-agenda-mode))
7822 (user-error
7823 "Cannot execute this command outside of org-agenda-mode buffers"))
7824 ((looking-at (if backward "\\`" "\\'"))
7825 (message "Already at the %s block" (if backward "first" "last")))
7826 (t (let ((pos (prog1 (point)
7827 (ignore-errors (if backward (backward-char 1)
7828 (move-end-of-line 1)))))
7829 (f (if backward
7830 'previous-single-property-change
7831 'next-single-property-change))
7832 moved dest)
7833 (while (and (setq dest (funcall
7834 f (point) 'org-agenda-structural-header))
7835 (not (get-text-property
7836 (point) 'org-agenda-structural-header)))
7837 (setq moved t)
7838 (goto-char dest))
7839 (if moved (move-beginning-of-line 1)
7840 (goto-char (if backward (point-min) (point-max)))
7841 (move-beginning-of-line 1)
7842 (message "No %s block" (if backward "previous" "further")))))))
7844 (defun org-agenda-later (arg)
7845 "Go forward in time by the current span.
7846 With prefix ARG, go forward that many times the current span."
7847 (interactive "p")
7848 (org-agenda-check-type t 'agenda)
7849 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7850 (span (or (nth 2 args) org-agenda-current-span))
7851 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7852 (greg (calendar-gregorian-from-absolute sd))
7853 (cnt (org-get-at-bol 'org-day-cnt))
7854 greg2)
7855 (cond
7856 ((numberp span)
7857 (setq sd (+ (* span arg) sd)))
7858 ((eq span 'day)
7859 (setq sd (+ arg sd)))
7860 ((eq span 'week)
7861 (setq sd (+ (* 7 arg) sd)))
7862 ((eq span 'fortnight)
7863 (setq sd (+ (* 14 arg) sd)))
7864 ((eq span 'month)
7865 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7866 sd (calendar-absolute-from-gregorian greg2))
7867 (setcar greg2 (1+ (car greg2))))
7868 ((eq span 'year)
7869 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7870 sd (calendar-absolute-from-gregorian greg2))
7871 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7873 (setq sd (+ (* span arg) sd))))
7874 (let ((org-agenda-overriding-cmd
7875 ;; `cmd' may have been set by `org-agenda-run-series' which
7876 ;; uses `org-agenda-overriding-cmd' to decide whether
7877 ;; overriding is allowed for `cmd'
7878 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7879 (org-agenda-overriding-arguments
7880 (list (car args) sd span)))
7881 (org-agenda-redo)
7882 (org-agenda-find-same-or-today-or-agenda cnt))))
7884 (defun org-agenda-earlier (arg)
7885 "Go backward in time by the current span.
7886 With prefix ARG, go backward that many times the current span."
7887 (interactive "p")
7888 (org-agenda-later (- arg)))
7890 (defun org-agenda-view-mode-dispatch ()
7891 "Call one of the view mode commands."
7892 (interactive)
7893 (message "View: [d]ay [w]eek for[t]night [m]onth [y]ear [SPC]reset [q]uit/abort
7894 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7895 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7896 (pcase (read-char-exclusive)
7897 (?\ (call-interactively 'org-agenda-reset-view))
7898 (?d (call-interactively 'org-agenda-day-view))
7899 (?w (call-interactively 'org-agenda-week-view))
7900 (?t (call-interactively 'org-agenda-fortnight-view))
7901 (?m (call-interactively 'org-agenda-month-view))
7902 (?y (call-interactively 'org-agenda-year-view))
7903 (?l (call-interactively 'org-agenda-log-mode))
7904 (?L (org-agenda-log-mode '(4)))
7905 (?c (org-agenda-log-mode 'clockcheck))
7906 ((or ?F ?f) (call-interactively 'org-agenda-follow-mode))
7907 (?a (call-interactively 'org-agenda-archives-mode))
7908 (?A (org-agenda-archives-mode 'files))
7909 ((or ?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7910 ((or ?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7911 (?G (call-interactively 'org-agenda-toggle-time-grid))
7912 (?D (call-interactively 'org-agenda-toggle-diary))
7913 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7914 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7915 (org-agenda-check-type t 'timeline 'agenda)
7916 (org-agenda-redo))
7917 (message "Display now includes inactive timestamps as well"))
7918 (?q (message "Abort"))
7919 (key (user-error "Invalid key: %s" key))))
7921 (defun org-agenda-reset-view ()
7922 "Switch to default view for agenda."
7923 (interactive)
7924 (org-agenda-change-time-span org-agenda-span))
7926 (defun org-agenda-day-view (&optional day-of-month)
7927 "Switch to daily view for agenda.
7928 With argument DAY-OF-MONTH, switch to that day of the month."
7929 (interactive "P")
7930 (org-agenda-change-time-span 'day day-of-month))
7932 (defun org-agenda-week-view (&optional iso-week)
7933 "Switch to weekly view for agenda.
7934 With argument ISO-WEEK, switch to the corresponding ISO week.
7935 If ISO-WEEK has more then 2 digits, only the last two encode
7936 the week. Any digits before this encode a year. So 200712
7937 means week 12 of year 2007. Years ranging from 70 years ago
7938 to 30 years in the future can also be written as 2-digit years."
7939 (interactive "P")
7940 (org-agenda-change-time-span 'week iso-week))
7942 (defun org-agenda-fortnight-view (&optional iso-week)
7943 "Switch to fortnightly view for agenda.
7944 With argument ISO-WEEK, switch to the corresponding ISO week.
7945 If ISO-WEEK has more then 2 digits, only the last two encode
7946 the week. Any digits before this encode a year. So 200712
7947 means week 12 of year 2007. Years ranging from 70 years ago
7948 to 30 years in the future can also be written as 2-digit years."
7949 (interactive "P")
7950 (org-agenda-change-time-span 'fortnight iso-week))
7952 (defun org-agenda-month-view (&optional month)
7953 "Switch to monthly view for agenda.
7954 With argument MONTH, switch to that month. If MONTH has more
7955 then 2 digits, only the last two encode the month. Any digits
7956 before this encode a year. So 200712 means December year 2007.
7957 Years ranging from 70 years ago to 30 years in the future can
7958 also be written as 2-digit years."
7959 (interactive "P")
7960 (org-agenda-change-time-span 'month month))
7962 (defun org-agenda-year-view (&optional year)
7963 "Switch to yearly view for agenda.
7964 With argument YEAR, switch to that year. Years ranging from 70
7965 years ago to 30 years in the future can also be written as
7966 2-digit years."
7967 (interactive "P")
7968 (when year
7969 (setq year (org-small-year-to-year year)))
7970 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
7971 (org-agenda-change-time-span 'year year)
7972 (error "Abort")))
7974 (defun org-agenda-change-time-span (span &optional n)
7975 "Change the agenda view to SPAN.
7976 SPAN may be `day', `week', `fortnight', `month', `year'."
7977 (org-agenda-check-type t 'agenda)
7978 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7979 (curspan (nth 2 args)))
7980 (if (and (not n) (equal curspan span))
7981 (error "Viewing span is already \"%s\"" span))
7982 (let* ((sd (or (org-get-at-bol 'day)
7983 (nth 1 args)
7984 org-starting-day))
7985 (sd (org-agenda-compute-starting-span sd span n))
7986 (org-agenda-overriding-cmd
7987 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7988 (org-agenda-overriding-arguments
7989 (list (car args) sd span)))
7990 (org-agenda-redo)
7991 (org-agenda-find-same-or-today-or-agenda))
7992 (org-agenda-set-mode-name)
7993 (message "Switched to %s view" span)))
7995 (defun org-agenda-compute-starting-span (sd span &optional n)
7996 "Compute starting date for agenda.
7997 SPAN may be `day', `week', `fortnight', `month', `year'. The return value
7998 is a cons cell with the starting date and the number of days,
7999 so that the date SD will be in that range."
8000 (let* ((greg (calendar-gregorian-from-absolute sd))
8001 (dg (nth 1 greg))
8002 (mg (car greg))
8003 (yg (nth 2 greg)))
8004 (cond
8005 ((eq span 'day)
8006 (when n
8007 (setq sd (+ (calendar-absolute-from-gregorian
8008 (list mg 1 yg))
8009 n -1))))
8010 ((or (eq span 'week) (eq span 'fortnight))
8011 (let* ((nt (calendar-day-of-week
8012 (calendar-gregorian-from-absolute sd)))
8013 (d (if org-agenda-start-on-weekday
8014 (- nt org-agenda-start-on-weekday)
8017 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
8018 (when n
8019 (require 'cal-iso)
8020 (when (> n 99)
8021 (setq y1 (org-small-year-to-year (/ n 100))
8022 n (mod n 100)))
8023 (setq sd
8024 (calendar-iso-to-absolute
8025 (list n 1
8026 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
8027 ((eq span 'month)
8028 (let (y1)
8029 (when (and n (> n 99))
8030 (setq y1 (org-small-year-to-year (/ n 100))
8031 n (mod n 100)))
8032 (setq sd (calendar-absolute-from-gregorian
8033 (list (or n mg) 1 (or y1 yg))))))
8034 ((eq span 'year)
8035 (setq sd (calendar-absolute-from-gregorian
8036 (list 1 1 (or n yg))))))
8037 sd))
8039 (defun org-agenda-next-date-line (&optional arg)
8040 "Jump to the next line indicating a date in agenda buffer."
8041 (interactive "p")
8042 (org-agenda-check-type t 'agenda 'timeline)
8043 (beginning-of-line 1)
8044 ;; This does not work if user makes date format that starts with a blank
8045 (if (looking-at "^\\S-") (forward-char 1))
8046 (if (not (re-search-forward "^\\S-" nil t arg))
8047 (progn
8048 (backward-char 1)
8049 (error "No next date after this line in this buffer")))
8050 (goto-char (match-beginning 0)))
8052 (defun org-agenda-previous-date-line (&optional arg)
8053 "Jump to the previous line indicating a date in agenda buffer."
8054 (interactive "p")
8055 (org-agenda-check-type t 'agenda 'timeline)
8056 (beginning-of-line 1)
8057 (if (not (re-search-backward "^\\S-" nil t arg))
8058 (error "No previous date before this line in this buffer")))
8060 ;; Initialize the highlight
8061 (defvar org-hl (make-overlay 1 1))
8062 (overlay-put org-hl 'face 'highlight)
8064 (defun org-highlight (begin end &optional buffer)
8065 "Highlight a region with overlay."
8066 (move-overlay org-hl begin end (or buffer (current-buffer))))
8068 (defun org-unhighlight ()
8069 "Detach overlay INDEX."
8070 (delete-overlay org-hl))
8072 (defun org-unhighlight-once ()
8073 "Remove the highlight from its position, and this function from the hook."
8074 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8075 (org-unhighlight))
8077 (defvar org-agenda-pre-follow-window-conf nil)
8078 (defun org-agenda-follow-mode ()
8079 "Toggle follow mode in an agenda buffer."
8080 (interactive)
8081 (unless org-agenda-follow-mode
8082 (setq org-agenda-pre-follow-window-conf
8083 (current-window-configuration)))
8084 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8085 (unless org-agenda-follow-mode
8086 (set-window-configuration org-agenda-pre-follow-window-conf))
8087 (org-agenda-set-mode-name)
8088 (org-agenda-do-context-action)
8089 (message "Follow mode is %s"
8090 (if org-agenda-follow-mode "on" "off")))
8092 (defun org-agenda-entry-text-mode (&optional arg)
8093 "Toggle entry text mode in an agenda buffer."
8094 (interactive "P")
8095 (if (or org-agenda-tag-filter
8096 org-agenda-category-filter
8097 org-agenda-regexp-filter
8098 org-agenda-top-headline-filter)
8099 (user-error "Can't show entry text in filtered views")
8100 (setq org-agenda-entry-text-mode (or (integerp arg)
8101 (not org-agenda-entry-text-mode)))
8102 (org-agenda-entry-text-hide)
8103 (and org-agenda-entry-text-mode
8104 (let ((org-agenda-entry-text-maxlines
8105 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8106 (org-agenda-entry-text-show)))
8107 (org-agenda-set-mode-name)
8108 (message "Entry text mode is %s%s"
8109 (if org-agenda-entry-text-mode "on" "off")
8110 (if (not org-agenda-entry-text-mode) ""
8111 (format " (maximum number of lines is %d)"
8112 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8114 (defun org-agenda-clockreport-mode ()
8115 "Toggle clocktable mode in an agenda buffer."
8116 (interactive)
8117 (org-agenda-check-type t 'agenda)
8118 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
8119 (setq org-agenda-start-with-clockreport-mode org-agenda-clockreport-mode)
8120 (org-agenda-set-mode-name)
8121 (org-agenda-redo)
8122 (message "Clocktable mode is %s"
8123 (if org-agenda-clockreport-mode "on" "off")))
8125 (defun org-agenda-log-mode (&optional special)
8126 "Toggle log mode in an agenda buffer.
8128 With argument SPECIAL, show all possible log items, not only the ones
8129 configured in `org-agenda-log-mode-items'.
8131 With a `\\[universal-argument] \\[universal-argument]' prefix, show *only* \
8132 log items, nothing else."
8133 (interactive "P")
8134 (org-agenda-check-type t 'agenda 'timeline)
8135 (setq org-agenda-show-log
8136 (cond
8137 ((equal special '(16)) 'only)
8138 ((eq special 'clockcheck)
8139 (if (eq org-agenda-show-log 'clockcheck)
8140 nil 'clockcheck))
8141 (special '(closed clock state))
8142 (t (not org-agenda-show-log))))
8143 (setq org-agenda-start-with-log-mode org-agenda-show-log)
8144 (org-agenda-set-mode-name)
8145 (org-agenda-redo)
8146 (message "Log mode is %s" (if org-agenda-show-log "on" "off")))
8148 (defun org-agenda-archives-mode (&optional with-files)
8149 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8150 When called with a prefix argument, include all archive files as well."
8151 (interactive "P")
8152 (setq org-agenda-archives-mode
8153 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8154 (org-agenda-set-mode-name)
8155 (org-agenda-redo)
8156 (message
8157 "%s"
8158 (cond
8159 ((eq org-agenda-archives-mode nil)
8160 "No archives are included")
8161 ((eq org-agenda-archives-mode 'trees)
8162 (format "Trees with :%s: tag are included" org-archive-tag))
8163 ((eq org-agenda-archives-mode t)
8164 (format "Trees with :%s: tag and all active archive files are included"
8165 org-archive-tag)))))
8167 (defun org-agenda-toggle-diary ()
8168 "Toggle diary inclusion in an agenda buffer."
8169 (interactive)
8170 (org-agenda-check-type t 'agenda)
8171 (setq org-agenda-include-diary (not org-agenda-include-diary))
8172 (org-agenda-redo)
8173 (org-agenda-set-mode-name)
8174 (message "Diary inclusion turned %s"
8175 (if org-agenda-include-diary "on" "off")))
8177 (defun org-agenda-toggle-deadlines ()
8178 "Toggle inclusion of entries with a deadline in an agenda buffer."
8179 (interactive)
8180 (org-agenda-check-type t 'agenda)
8181 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8182 (org-agenda-redo)
8183 (org-agenda-set-mode-name)
8184 (message "Deadlines inclusion turned %s"
8185 (if org-agenda-include-deadlines "on" "off")))
8187 (defun org-agenda-toggle-time-grid ()
8188 "Toggle time grid in an agenda buffer."
8189 (interactive)
8190 (org-agenda-check-type t 'agenda)
8191 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8192 (org-agenda-redo)
8193 (org-agenda-set-mode-name)
8194 (message "Time-grid turned %s"
8195 (if org-agenda-use-time-grid "on" "off")))
8197 (defun org-agenda-set-mode-name ()
8198 "Set the mode name to indicate all the small mode settings."
8199 (setq mode-name
8200 (list "Org-Agenda"
8201 (if (get 'org-agenda-files 'org-restrict) " []" "")
8203 '(:eval (org-agenda-span-name org-agenda-current-span))
8204 (if org-agenda-follow-mode " Follow" "")
8205 (if org-agenda-entry-text-mode " ETxt" "")
8206 (if org-agenda-include-diary " Diary" "")
8207 (if org-agenda-include-deadlines " Ddl" "")
8208 (if org-agenda-use-time-grid " Grid" "")
8209 (if (and (boundp 'org-habit-show-habits)
8210 org-habit-show-habits) " Habit" "")
8211 (cond
8212 ((consp org-agenda-show-log) " LogAll")
8213 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8214 (org-agenda-show-log " Log")
8215 (t ""))
8216 (if (or org-agenda-category-filter
8217 (get 'org-agenda-category-filter :preset-filter))
8218 '(:eval (propertize
8219 (concat " <"
8220 (mapconcat
8221 'identity
8222 (append
8223 (get 'org-agenda-category-filter :preset-filter)
8224 org-agenda-category-filter)
8226 ">")
8227 'face 'org-agenda-filter-category
8228 'help-echo "Category used in filtering")) "")
8229 (if (or org-agenda-tag-filter
8230 (get 'org-agenda-tag-filter :preset-filter))
8231 '(:eval (propertize
8232 (concat " {"
8233 (mapconcat
8234 'identity
8235 (append
8236 (get 'org-agenda-tag-filter :preset-filter)
8237 org-agenda-tag-filter)
8239 "}")
8240 'face 'org-agenda-filter-tags
8241 'help-echo "Tags used in filtering")) "")
8242 (if (or org-agenda-effort-filter
8243 (get 'org-agenda-effort-filter :preset-filter))
8244 '(:eval (propertize
8245 (concat " {"
8246 (mapconcat
8247 'identity
8248 (append
8249 (get 'org-agenda-effort-filter :preset-filter)
8250 org-agenda-effort-filter)
8252 "}")
8253 'face 'org-agenda-filter-effort
8254 'help-echo "Effort conditions used in filtering")) "")
8255 (if (or org-agenda-regexp-filter
8256 (get 'org-agenda-regexp-filter :preset-filter))
8257 '(:eval (propertize
8258 (concat " ["
8259 (mapconcat
8260 'identity
8261 (append
8262 (get 'org-agenda-regexp-filter :preset-filter)
8263 org-agenda-regexp-filter)
8265 "]")
8266 'face 'org-agenda-filter-regexp
8267 'help-echo "Regexp used in filtering")) "")
8268 (if org-agenda-archives-mode
8269 (if (eq org-agenda-archives-mode t)
8270 " Archives"
8271 (format " :%s:" org-archive-tag))
8273 (if org-agenda-clockreport-mode " Clock" "")))
8274 (force-mode-line-update))
8276 (defun org-agenda-update-agenda-type ()
8277 "Update the agenda type after each command."
8278 (setq org-agenda-type
8279 (or (get-text-property (point) 'org-agenda-type)
8280 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8282 (defun org-agenda-next-line ()
8283 "Move cursor to the next line, and show if follow mode is active."
8284 (interactive)
8285 (call-interactively 'next-line)
8286 (org-agenda-do-context-action))
8288 (defun org-agenda-previous-line ()
8289 "Move cursor to the previous line, and show if follow-mode is active."
8290 (interactive)
8291 (call-interactively 'previous-line)
8292 (org-agenda-do-context-action))
8294 (defun org-agenda-next-item (n)
8295 "Move cursor to next agenda item."
8296 (interactive "p")
8297 (let ((col (current-column)))
8298 (dotimes (c n)
8299 (when (next-single-property-change (point-at-eol) 'org-marker)
8300 (move-end-of-line 1)
8301 (goto-char (next-single-property-change (point) 'org-marker))))
8302 (org-move-to-column col))
8303 (org-agenda-do-context-action))
8305 (defun org-agenda-previous-item (n)
8306 "Move cursor to next agenda item."
8307 (interactive "p")
8308 (dotimes (c n)
8309 (let ((col (current-column))
8310 (goto (save-excursion
8311 (move-end-of-line 0)
8312 (previous-single-property-change (point) 'org-marker))))
8313 (if goto (goto-char goto))
8314 (org-move-to-column col)))
8315 (org-agenda-do-context-action))
8317 (defun org-agenda-do-context-action ()
8318 "Show outline path and, maybe, follow mode window."
8319 (let ((m (org-get-at-bol 'org-marker)))
8320 (when (and (markerp m) (marker-buffer m))
8321 (and org-agenda-follow-mode
8322 (if org-agenda-follow-indirect
8323 (org-agenda-tree-to-indirect-buffer nil)
8324 (org-agenda-show)))
8325 (and org-agenda-show-outline-path
8326 (org-with-point-at m (org-display-outline-path t))))))
8328 (defun org-agenda-show-tags ()
8329 "Show the tags applicable to the current item."
8330 (interactive)
8331 (let* ((tags (org-get-at-bol 'tags)))
8332 (if tags
8333 (message "Tags are :%s:"
8334 (org-no-properties (mapconcat 'identity tags ":")))
8335 (message "No tags associated with this line"))))
8337 (defun org-agenda-goto (&optional highlight)
8338 "Go to the entry at point in the corresponding Org file."
8339 (interactive)
8340 (let* ((marker (or (org-get-at-bol 'org-marker)
8341 (org-agenda-error)))
8342 (buffer (marker-buffer marker))
8343 (pos (marker-position marker)))
8344 (switch-to-buffer-other-window buffer)
8345 (widen)
8346 (push-mark)
8347 (goto-char pos)
8348 (when (derived-mode-p 'org-mode)
8349 (org-show-context 'agenda)
8350 (recenter (/ (window-height) 2))
8351 (org-back-to-heading t)
8352 (let ((case-fold-search nil))
8353 (when (re-search-forward org-complex-heading-regexp nil t)
8354 (goto-char (match-beginning 4)))))
8355 (run-hooks 'org-agenda-after-show-hook)
8356 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8358 (defvar org-agenda-after-show-hook nil
8359 "Normal hook run after an item has been shown from the agenda.
8360 Point is in the buffer where the item originated.")
8362 (defun org-agenda-kill ()
8363 "Kill the entry or subtree belonging to the current agenda entry."
8364 (interactive)
8365 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8366 (let* ((bufname-orig (buffer-name))
8367 (marker (or (org-get-at-bol 'org-marker)
8368 (org-agenda-error)))
8369 (buffer (marker-buffer marker))
8370 (pos (marker-position marker))
8371 (type (org-get-at-bol 'type))
8372 dbeg dend (n 0) conf)
8373 (org-with-remote-undo buffer
8374 (with-current-buffer buffer
8375 (save-excursion
8376 (goto-char pos)
8377 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8378 (setq dbeg (progn (org-back-to-heading t) (point))
8379 dend (org-end-of-subtree t t))
8380 (setq dbeg (point-at-bol)
8381 dend (min (point-max) (1+ (point-at-eol)))))
8382 (goto-char dbeg)
8383 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8384 (setq conf (or (eq t org-agenda-confirm-kill)
8385 (and (numberp org-agenda-confirm-kill)
8386 (> n org-agenda-confirm-kill))))
8387 (and conf
8388 (not (y-or-n-p
8389 (format "Delete entry with %d lines in buffer \"%s\"? "
8390 n (buffer-name buffer))))
8391 (error "Abort"))
8392 (let ((org-agenda-buffer-name bufname-orig))
8393 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8394 (with-current-buffer buffer (delete-region dbeg dend))
8395 (message "Agenda item and source killed"))))
8397 (defvar org-archive-default-command) ; defined in org-archive.el
8398 (defun org-agenda-archive-default ()
8399 "Archive the entry or subtree belonging to the current agenda entry."
8400 (interactive)
8401 (require 'org-archive)
8402 (org-agenda-archive-with org-archive-default-command))
8404 (defun org-agenda-archive-default-with-confirmation ()
8405 "Archive the entry or subtree belonging to the current agenda entry."
8406 (interactive)
8407 (require 'org-archive)
8408 (org-agenda-archive-with org-archive-default-command 'confirm))
8410 (defun org-agenda-archive ()
8411 "Archive the entry or subtree belonging to the current agenda entry."
8412 (interactive)
8413 (org-agenda-archive-with 'org-archive-subtree))
8415 (defun org-agenda-archive-to-archive-sibling ()
8416 "Move the entry to the archive sibling."
8417 (interactive)
8418 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8420 (defun org-agenda-archive-with (cmd &optional confirm)
8421 "Move the entry to the archive sibling."
8422 (interactive)
8423 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8424 (let* ((bufname-orig (buffer-name))
8425 (marker (or (org-get-at-bol 'org-marker)
8426 (org-agenda-error)))
8427 (buffer (marker-buffer marker))
8428 (pos (marker-position marker)))
8429 (org-with-remote-undo buffer
8430 (with-current-buffer buffer
8431 (if (derived-mode-p 'org-mode)
8432 (if (and confirm
8433 (not (y-or-n-p "Archive this subtree or entry? ")))
8434 (error "Abort")
8435 (save-window-excursion
8436 (goto-char pos)
8437 (let ((org-agenda-buffer-name bufname-orig))
8438 (org-remove-subtree-entries-from-agenda))
8439 (org-back-to-heading t)
8440 (funcall cmd)))
8441 (error "Archiving works only in Org files"))))))
8443 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8444 "Remove all lines in the agenda that correspond to a given subtree.
8445 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8446 If this information is not given, the function uses the tree at point."
8447 (let ((buf (or buf (current-buffer))) m p)
8448 (save-excursion
8449 (unless (and beg end)
8450 (org-back-to-heading t)
8451 (setq beg (point))
8452 (org-end-of-subtree t)
8453 (setq end (point)))
8454 (set-buffer (get-buffer org-agenda-buffer-name))
8455 (save-excursion
8456 (goto-char (point-max))
8457 (beginning-of-line 1)
8458 (while (not (bobp))
8459 (when (and (setq m (org-get-at-bol 'org-marker))
8460 (equal buf (marker-buffer m))
8461 (setq p (marker-position m))
8462 (>= p beg)
8463 (< p end))
8464 (let ((inhibit-read-only t))
8465 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8466 (beginning-of-line 0))))))
8468 (defun org-agenda-refile (&optional goto rfloc no-update)
8469 "Refile the item at point.
8471 When called with `\\[universal-argument] \\[universal-argument]', \
8472 go to the location of the last
8473 refiled item.
8475 When called with `\\[universal-argument] \\[universal-argument] \
8476 \\[universal-argument]' prefix or when GOTO is 0, clear
8477 the refile cache.
8479 RFLOC can be a refile location obtained in a different way.
8481 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8482 (interactive "P")
8483 (cond
8484 ((member goto '(0 (64)))
8485 (org-refile-cache-clear))
8486 ((equal goto '(16))
8487 (org-refile-goto-last-stored))
8489 (let* ((buffer-orig (buffer-name))
8490 (marker (or (org-get-at-bol 'org-hd-marker)
8491 (org-agenda-error)))
8492 (buffer (marker-buffer marker))
8493 (pos (marker-position marker))
8494 (rfloc (or rfloc
8495 (org-refile-get-location
8496 (if goto "Goto" "Refile to") buffer
8497 org-refile-allow-creating-parent-nodes))))
8498 (with-current-buffer buffer
8499 (org-with-wide-buffer
8500 (goto-char marker)
8501 (let ((org-agenda-buffer-name buffer-orig))
8502 (org-remove-subtree-entries-from-agenda))
8503 (org-refile goto buffer rfloc))))
8504 (unless no-update (org-agenda-redo)))))
8506 (defun org-agenda-open-link (&optional arg)
8507 "Open the link(s) in the current entry, if any.
8508 This looks for a link in the displayed line in the agenda.
8509 It also looks at the text of the entry itself."
8510 (interactive "P")
8511 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8512 (org-get-at-bol 'org-marker)))
8513 (buffer (and marker (marker-buffer marker)))
8514 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8515 (lkall (and buffer (org-offer-links-in-entry
8516 buffer marker arg prefix)))
8517 (lk0 (car lkall))
8518 (lk (if (stringp lk0) (list lk0) lk0))
8519 (lkend (cdr lkall))
8520 trg)
8521 (cond
8522 ((and buffer lk)
8523 (mapcar (lambda(l)
8524 (with-current-buffer buffer
8525 (setq trg (and (string-match org-bracket-link-regexp l)
8526 (match-string 1 l)))
8527 (if (or (not trg) (string-match org-any-link-re trg))
8528 (org-with-wide-buffer
8529 (goto-char marker)
8530 (when (search-forward l nil lkend)
8531 (goto-char (match-beginning 0))
8532 (org-open-at-point)))
8533 ;; This is an internal link, widen the buffer
8534 (switch-to-buffer-other-window buffer)
8535 (widen)
8536 (goto-char marker)
8537 (when (search-forward l nil lkend)
8538 (goto-char (match-beginning 0))
8539 (org-open-at-point)))))
8540 lk))
8541 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8542 (save-excursion
8543 (beginning-of-line 1)
8544 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8545 (org-open-link-from-string (match-string 1)))
8546 (t (message "No link to open here")))))
8548 (defun org-agenda-copy-local-variable (var)
8549 "Get a variable from a referenced buffer and install it here."
8550 (let ((m (org-get-at-bol 'org-marker)))
8551 (when (and m (buffer-live-p (marker-buffer m)))
8552 (set (make-local-variable var)
8553 (with-current-buffer (marker-buffer m)
8554 (symbol-value var))))))
8556 (defun org-agenda-switch-to (&optional delete-other-windows)
8557 "Go to the Org mode file which contains the item at point.
8558 When optional argument DELETE-OTHER-WINDOWS is non-nil, the
8559 displayed Org file fills the frame."
8560 (interactive)
8561 (if (and org-return-follows-link
8562 (not (org-get-at-bol 'org-marker))
8563 (org-in-regexp org-bracket-link-regexp))
8564 (org-open-link-from-string (match-string 0))
8565 (let* ((marker (or (org-get-at-bol 'org-marker)
8566 (org-agenda-error)))
8567 (buffer (marker-buffer marker))
8568 (pos (marker-position marker)))
8569 (unless buffer (user-error "Trying to switch to non-existent buffer"))
8570 (pop-to-buffer-same-window buffer)
8571 (when delete-other-windows (delete-other-windows))
8572 (widen)
8573 (goto-char pos)
8574 (when (derived-mode-p 'org-mode)
8575 (org-show-context 'agenda)
8576 (run-hooks 'org-agenda-after-show-hook)))))
8578 (defun org-agenda-goto-mouse (ev)
8579 "Go to the Org file which contains the item at the mouse click."
8580 (interactive "e")
8581 (mouse-set-point ev)
8582 (org-agenda-goto))
8584 (defun org-agenda-show (&optional full-entry)
8585 "Display the Org file which contains the item at point.
8586 With prefix argument FULL-ENTRY, make the entire entry visible
8587 if it was hidden in the outline."
8588 (interactive "P")
8589 (let ((win (selected-window)))
8590 (org-agenda-goto t)
8591 (when full-entry (org-show-entry))
8592 (select-window win)))
8594 (defvar org-agenda-show-window nil)
8595 (defun org-agenda-show-and-scroll-up (&optional arg)
8596 "Display the Org file which contains the item at point.
8598 When called repeatedly, scroll the window that is displaying the buffer.
8600 With a `\\[universal-argument]' prefix, use `org-show-entry' instead of \
8601 `outline-show-subtree'
8602 to display the item, so that drawers and logbooks stay folded."
8603 (interactive "P")
8604 (let ((win (selected-window)))
8605 (if (and (window-live-p org-agenda-show-window)
8606 (eq this-command last-command))
8607 (progn
8608 (select-window org-agenda-show-window)
8609 (ignore-errors (scroll-up)))
8610 (org-agenda-goto t)
8611 (if arg (org-show-entry) (outline-show-subtree))
8612 (setq org-agenda-show-window (selected-window)))
8613 (select-window win)))
8615 (defun org-agenda-show-scroll-down ()
8616 "Scroll down the window showing the agenda."
8617 (interactive)
8618 (let ((win (selected-window)))
8619 (when (window-live-p org-agenda-show-window)
8620 (select-window org-agenda-show-window)
8621 (ignore-errors (scroll-down))
8622 (select-window win))))
8624 (defun org-agenda-show-1 (&optional more)
8625 "Display the Org file which contains the item at point.
8626 The prefix arg selects the amount of information to display:
8628 0 hide the subtree
8629 1 just show the entry according to defaults.
8630 2 show the children view
8631 3 show the subtree view
8632 4 show the entire subtree and any LOGBOOK drawers
8633 5 show the entire subtree and any drawers
8634 With prefix argument FULL-ENTRY, make the entire entry visible
8635 if it was hidden in the outline."
8636 (interactive "p")
8637 (let ((win (selected-window)))
8638 (org-agenda-goto t)
8639 (org-back-to-heading)
8640 (set-window-start (selected-window) (point-at-bol))
8641 (cond
8642 ((= more 0)
8643 (outline-hide-subtree)
8644 (save-excursion
8645 (org-back-to-heading)
8646 (run-hook-with-args 'org-cycle-hook 'folded))
8647 (message "Remote: FOLDED"))
8648 ((and (called-interactively-p 'any) (= more 1))
8649 (message "Remote: show with default settings"))
8650 ((= more 2)
8651 (outline-show-entry)
8652 (org-show-children)
8653 (save-excursion
8654 (org-back-to-heading)
8655 (run-hook-with-args 'org-cycle-hook 'children))
8656 (message "Remote: CHILDREN"))
8657 ((= more 3)
8658 (outline-show-subtree)
8659 (save-excursion
8660 (org-back-to-heading)
8661 (run-hook-with-args 'org-cycle-hook 'subtree))
8662 (message "Remote: SUBTREE"))
8663 ((= more 4)
8664 (outline-show-subtree)
8665 (save-excursion
8666 (org-back-to-heading)
8667 (org-cycle-hide-drawers 'subtree '("LOGBOOK")))
8668 (message "Remote: SUBTREE AND LOGBOOK"))
8669 ((> more 4)
8670 (outline-show-subtree)
8671 (message "Remote: SUBTREE AND ALL DRAWERS")))
8672 (select-window win)))
8674 (defvar org-agenda-cycle-counter nil)
8675 (defun org-agenda-cycle-show (&optional n)
8676 "Show the current entry in another window, with default settings.
8678 Default settings are taken from `org-show-context-detail'. When
8679 use repeatedly in immediate succession, the remote entry will
8680 cycle through visibility
8682 children -> subtree -> folded
8684 When called with a numeric prefix arg, that arg will be passed through to
8685 `org-agenda-show-1'. For the interpretation of that argument, see the
8686 docstring of `org-agenda-show-1'."
8687 (interactive "P")
8688 (if (integerp n)
8689 (setq org-agenda-cycle-counter n)
8690 (if (not (eq last-command this-command))
8691 (setq org-agenda-cycle-counter 1)
8692 (if (equal org-agenda-cycle-counter 0)
8693 (setq org-agenda-cycle-counter 2)
8694 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8695 (if (> org-agenda-cycle-counter 3)
8696 (setq org-agenda-cycle-counter 0)))))
8697 (org-agenda-show-1 org-agenda-cycle-counter))
8699 (defun org-agenda-recenter (arg)
8700 "Display the Org file which contains the item at point and recenter."
8701 (interactive "P")
8702 (let ((win (selected-window)))
8703 (org-agenda-goto t)
8704 (recenter arg)
8705 (select-window win)))
8707 (defun org-agenda-show-mouse (ev)
8708 "Display the Org file which contains the item at the mouse click."
8709 (interactive "e")
8710 (mouse-set-point ev)
8711 (org-agenda-show))
8713 (defun org-agenda-check-no-diary ()
8714 "Check if the entry is a diary link and abort if yes."
8715 (if (org-get-at-bol 'org-agenda-diary-link)
8716 (org-agenda-error)))
8718 (defun org-agenda-error ()
8719 "Throw an error when a command is not allowed in the agenda."
8720 (user-error "Command not allowed in this line"))
8722 (defun org-agenda-tree-to-indirect-buffer (arg)
8723 "Show the subtree corresponding to the current entry in an indirect buffer.
8724 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8726 With a numerical prefix ARG, go up to this level and then take that tree.
8727 With a negative numeric ARG, go up by this number of levels.
8729 With a `\\[universal-argument]' prefix, make a separate frame for this tree, \
8730 i.e. don't use
8731 the dedicated frame."
8732 (interactive "P")
8733 (if current-prefix-arg
8734 (org-agenda-do-tree-to-indirect-buffer arg)
8735 (let ((agenda-buffer (buffer-name))
8736 (agenda-window (selected-window))
8737 (indirect-window
8738 (and org-last-indirect-buffer
8739 (get-buffer-window org-last-indirect-buffer))))
8740 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8741 (unless (or (eq org-indirect-buffer-display 'new-frame)
8742 (eq org-indirect-buffer-display 'dedicated-frame))
8743 (unwind-protect
8744 (unless (and indirect-window (window-live-p indirect-window))
8745 (setq indirect-window (split-window agenda-window)))
8746 (and indirect-window (select-window indirect-window))
8747 (switch-to-buffer org-last-indirect-buffer :norecord)
8748 (fit-window-to-buffer indirect-window)))
8749 (select-window (get-buffer-window agenda-buffer))
8750 (setq org-agenda-last-indirect-buffer org-last-indirect-buffer))))
8752 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8753 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8754 (org-agenda-check-no-diary)
8755 (let* ((marker (or (org-get-at-bol 'org-marker)
8756 (org-agenda-error)))
8757 (buffer (marker-buffer marker))
8758 (pos (marker-position marker)))
8759 (with-current-buffer buffer
8760 (save-excursion
8761 (goto-char pos)
8762 (funcall 'org-tree-to-indirect-buffer arg)))))
8764 (defvar org-last-heading-marker (make-marker)
8765 "Marker pointing to the headline that last changed its TODO state
8766 by a remote command from the agenda.")
8768 (defun org-agenda-todo-nextset ()
8769 "Switch TODO entry to next sequence."
8770 (interactive)
8771 (org-agenda-todo 'nextset))
8773 (defun org-agenda-todo-previousset ()
8774 "Switch TODO entry to previous sequence."
8775 (interactive)
8776 (org-agenda-todo 'previousset))
8778 (defun org-agenda-todo (&optional arg)
8779 "Cycle TODO state of line at point, also in Org file.
8780 This changes the line at point, all other lines in the agenda referring to
8781 the same tree node, and the headline of the tree node in the Org file."
8782 (interactive "P")
8783 (org-agenda-check-no-diary)
8784 (let* ((col (current-column))
8785 (marker (or (org-get-at-bol 'org-marker)
8786 (org-agenda-error)))
8787 (buffer (marker-buffer marker))
8788 (pos (marker-position marker))
8789 (hdmarker (org-get-at-bol 'org-hd-marker))
8790 (todayp (org-agenda-today-p (org-get-at-bol 'day)))
8791 (inhibit-read-only t)
8792 org-agenda-headline-snapshot-before-repeat newhead just-one)
8793 (org-with-remote-undo buffer
8794 (with-current-buffer buffer
8795 (widen)
8796 (goto-char pos)
8797 (org-show-context 'agenda)
8798 (let ((current-prefix-arg arg))
8799 (call-interactively 'org-todo))
8800 (and (bolp) (forward-char 1))
8801 (setq newhead (org-get-heading))
8802 (when (and (bound-and-true-p
8803 org-agenda-headline-snapshot-before-repeat)
8804 (not (equal org-agenda-headline-snapshot-before-repeat
8805 newhead))
8806 todayp)
8807 (setq newhead org-agenda-headline-snapshot-before-repeat
8808 just-one t))
8809 (save-excursion
8810 (org-back-to-heading)
8811 (move-marker org-last-heading-marker (point))))
8812 (beginning-of-line 1)
8813 (save-window-excursion
8814 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8815 (when (bound-and-true-p org-clock-out-when-done)
8816 (string-match (concat "^" (regexp-opt org-done-keywords-for-agenda))
8817 newhead)
8818 (org-agenda-unmark-clocking-task))
8819 (org-move-to-column col)
8820 (org-agenda-mark-clocking-task))))
8822 (defun org-agenda-add-note (&optional arg)
8823 "Add a time-stamped note to the entry at point."
8824 (interactive "P")
8825 (org-agenda-check-no-diary)
8826 (let* ((marker (or (org-get-at-bol 'org-marker)
8827 (org-agenda-error)))
8828 (buffer (marker-buffer marker))
8829 (pos (marker-position marker))
8830 (hdmarker (org-get-at-bol 'org-hd-marker))
8831 (inhibit-read-only t))
8832 (with-current-buffer buffer
8833 (widen)
8834 (goto-char pos)
8835 (org-show-context 'agenda)
8836 (org-add-note))))
8838 (defun org-agenda-change-all-lines (newhead hdmarker
8839 &optional fixface just-this)
8840 "Change all lines in the agenda buffer which match HDMARKER.
8841 The new content of the line will be NEWHEAD (as modified by
8842 `org-agenda-format-item'). HDMARKER is checked with
8843 `equal' against all `org-hd-marker' text properties in the file.
8844 If FIXFACE is non-nil, the face of each item is modified according to
8845 the new TODO state.
8846 If JUST-THIS is non-nil, change just the current line, not all.
8847 If FORCE-TAGS is non nil, the car of it returns the new tags."
8848 (let* ((inhibit-read-only t)
8849 (line (org-current-line))
8850 (org-agenda-buffer (current-buffer))
8851 (thetags (with-current-buffer (marker-buffer hdmarker)
8852 (org-with-wide-buffer
8853 (goto-char hdmarker)
8854 (org-get-tags-at))))
8855 props m pl undone-face done-face finish new dotime level cat tags)
8856 (save-excursion
8857 (goto-char (point-max))
8858 (beginning-of-line 1)
8859 (while (not finish)
8860 (setq finish (bobp))
8861 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8862 (or (not just-this) (= (org-current-line) line))
8863 (equal m hdmarker))
8864 (setq props (text-properties-at (point))
8865 dotime (org-get-at-bol 'dotime)
8866 cat (org-get-at-eol 'org-category 1)
8867 level (org-get-at-bol 'level)
8868 tags thetags
8870 (let ((org-prefix-format-compiled
8871 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8872 org-prefix-format-compiled))
8873 (extra (org-get-at-bol 'extra)))
8874 (with-current-buffer (marker-buffer hdmarker)
8875 (org-with-wide-buffer
8876 (org-agenda-format-item extra newhead level cat tags dotime))))
8877 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8878 undone-face (org-get-at-bol 'undone-face)
8879 done-face (org-get-at-bol 'done-face))
8880 (beginning-of-line 1)
8881 (cond
8882 ((equal new "") (delete-region (point) (line-beginning-position 2)))
8883 ((looking-at ".*")
8884 ;; When replacing the whole line, preserve bulk mark
8885 ;; overlay, if any.
8886 (let ((mark (catch :overlay
8887 (dolist (o (overlays-in (point) (+ 2 (point))))
8888 (when (eq (overlay-get o 'type)
8889 'org-marked-entry-overlay)
8890 (throw :overlay o))))))
8891 (replace-match new t t)
8892 (beginning-of-line)
8893 (when mark (move-overlay mark (point) (+ 2 (point)))))
8894 (add-text-properties (point-at-bol) (point-at-eol) props)
8895 (when fixface
8896 (add-text-properties
8897 (point-at-bol) (point-at-eol)
8898 (list 'face
8899 (if org-last-todo-state-is-todo
8900 undone-face done-face))))
8901 (org-agenda-highlight-todo 'line)
8902 (beginning-of-line 1))
8903 (t (error "Line update did not work")))
8904 (save-restriction
8905 (narrow-to-region (point-at-bol) (point-at-eol))
8906 (org-agenda-finalize)))
8907 (beginning-of-line 0)))))
8909 (defun org-agenda-align-tags (&optional line)
8910 "Align all tags in agenda items to `org-agenda-tags-column'."
8911 (let ((inhibit-read-only t) l c)
8912 (save-excursion
8913 (goto-char (if line (point-at-bol) (point-min)))
8914 (while (re-search-forward "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"
8915 (if line (point-at-eol) nil) t)
8916 (add-text-properties
8917 (match-beginning 2) (match-end 2)
8918 (list 'face (delq nil (let ((prop (get-text-property
8919 (match-beginning 2) 'face)))
8920 (or (listp prop) (setq prop (list prop)))
8921 (if (memq 'org-tag prop)
8922 prop
8923 (cons 'org-tag prop))))))
8924 (setq l (- (match-end 2) (match-beginning 2))
8925 c (if (< org-agenda-tags-column 0)
8926 (- (abs org-agenda-tags-column) l)
8927 org-agenda-tags-column))
8928 (delete-region (match-beginning 1) (match-end 1))
8929 (goto-char (match-beginning 1))
8930 (insert (org-add-props
8931 (make-string (max 1 (- c (current-column))) ?\ )
8932 (plist-put (copy-sequence (text-properties-at (point)))
8933 'face nil))))
8934 (goto-char (point-min))
8935 (org-font-lock-add-tag-faces (point-max)))))
8937 (defun org-agenda-priority-up ()
8938 "Increase the priority of line at point, also in Org file."
8939 (interactive)
8940 (org-agenda-priority 'up))
8942 (defun org-agenda-priority-down ()
8943 "Decrease the priority of line at point, also in Org file."
8944 (interactive)
8945 (org-agenda-priority 'down))
8947 (defun org-agenda-priority (&optional force-direction)
8948 "Set the priority of line at point, also in Org file.
8949 This changes the line at point, all other lines in the agenda referring to
8950 the same tree node, and the headline of the tree node in the Org file.
8951 Called with a universal prefix arg, show the priority instead of setting it."
8952 (interactive "P")
8953 (if (equal force-direction '(4))
8954 (org-show-priority)
8955 (unless org-enable-priority-commands
8956 (error "Priority commands are disabled"))
8957 (org-agenda-check-no-diary)
8958 (let* ((col (current-column))
8959 (marker (or (org-get-at-bol 'org-marker)
8960 (org-agenda-error)))
8961 (hdmarker (org-get-at-bol 'org-hd-marker))
8962 (buffer (marker-buffer hdmarker))
8963 (pos (marker-position hdmarker))
8964 (inhibit-read-only t)
8965 newhead)
8966 (org-with-remote-undo buffer
8967 (with-current-buffer buffer
8968 (widen)
8969 (goto-char pos)
8970 (org-show-context 'agenda)
8971 (funcall 'org-priority force-direction)
8972 (end-of-line 1)
8973 (setq newhead (org-get-heading)))
8974 (org-agenda-change-all-lines newhead hdmarker)
8975 (org-move-to-column col)))))
8977 ;; FIXME: should fix the tags property of the agenda line.
8978 (defun org-agenda-set-tags (&optional tag onoff)
8979 "Set tags for the current headline."
8980 (interactive)
8981 (org-agenda-check-no-diary)
8982 (if (and (org-region-active-p) (called-interactively-p 'any))
8983 (call-interactively 'org-change-tag-in-region)
8984 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8985 (org-agenda-error)))
8986 (buffer (marker-buffer hdmarker))
8987 (pos (marker-position hdmarker))
8988 (inhibit-read-only t)
8989 newhead)
8990 (org-with-remote-undo buffer
8991 (with-current-buffer buffer
8992 (widen)
8993 (goto-char pos)
8994 (org-show-context 'agenda)
8995 (if tag
8996 (org-toggle-tag tag onoff)
8997 (call-interactively 'org-set-tags))
8998 (end-of-line 1)
8999 (setq newhead (org-get-heading)))
9000 (org-agenda-change-all-lines newhead hdmarker)
9001 (beginning-of-line 1)))))
9003 (defun org-agenda-set-property ()
9004 "Set a property for the current headline."
9005 (interactive)
9006 (org-agenda-check-no-diary)
9007 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9008 (org-agenda-error)))
9009 (buffer (marker-buffer hdmarker))
9010 (pos (marker-position hdmarker))
9011 (inhibit-read-only t)
9012 newhead)
9013 (org-with-remote-undo buffer
9014 (with-current-buffer buffer
9015 (widen)
9016 (goto-char pos)
9017 (org-show-context 'agenda)
9018 (call-interactively 'org-set-property)))))
9020 (defun org-agenda-set-effort ()
9021 "Set the effort property for the current headline."
9022 (interactive)
9023 (org-agenda-check-no-diary)
9024 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9025 (org-agenda-error)))
9026 (buffer (marker-buffer hdmarker))
9027 (pos (marker-position hdmarker))
9028 (inhibit-read-only t)
9029 newhead)
9030 (org-with-remote-undo buffer
9031 (with-current-buffer buffer
9032 (widen)
9033 (goto-char pos)
9034 (org-show-context 'agenda)
9035 (call-interactively 'org-set-effort)
9036 (end-of-line 1)
9037 (setq newhead (org-get-heading)))
9038 (org-agenda-change-all-lines newhead hdmarker))))
9040 (defun org-agenda-toggle-archive-tag ()
9041 "Toggle the archive tag for the current entry."
9042 (interactive)
9043 (org-agenda-check-no-diary)
9044 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9045 (org-agenda-error)))
9046 (buffer (marker-buffer hdmarker))
9047 (pos (marker-position hdmarker))
9048 (inhibit-read-only t)
9049 newhead)
9050 (org-with-remote-undo buffer
9051 (with-current-buffer buffer
9052 (widen)
9053 (goto-char pos)
9054 (org-show-context 'agenda)
9055 (call-interactively 'org-toggle-archive-tag)
9056 (end-of-line 1)
9057 (setq newhead (org-get-heading)))
9058 (org-agenda-change-all-lines newhead hdmarker)
9059 (beginning-of-line 1))))
9061 (defun org-agenda-do-date-later (arg)
9062 (interactive "P")
9063 (cond
9064 ((or (equal arg '(16))
9065 (memq last-command
9066 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9067 (setq this-command 'org-agenda-date-later-minutes)
9068 (org-agenda-date-later-minutes 1))
9069 ((or (equal arg '(4))
9070 (memq last-command
9071 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9072 (setq this-command 'org-agenda-date-later-hours)
9073 (org-agenda-date-later-hours 1))
9075 (org-agenda-date-later (prefix-numeric-value arg)))))
9077 (defun org-agenda-do-date-earlier (arg)
9078 (interactive "P")
9079 (cond
9080 ((or (equal arg '(16))
9081 (memq last-command
9082 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9083 (setq this-command 'org-agenda-date-earlier-minutes)
9084 (org-agenda-date-earlier-minutes 1))
9085 ((or (equal arg '(4))
9086 (memq last-command
9087 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9088 (setq this-command 'org-agenda-date-earlier-hours)
9089 (org-agenda-date-earlier-hours 1))
9091 (org-agenda-date-earlier (prefix-numeric-value arg)))))
9093 (defun org-agenda-date-later (arg &optional what)
9094 "Change the date of this item to ARG day(s) later."
9095 (interactive "p")
9096 (org-agenda-check-type t 'agenda 'timeline)
9097 (org-agenda-check-no-diary)
9098 (let* ((marker (or (org-get-at-bol 'org-marker)
9099 (org-agenda-error)))
9100 (buffer (marker-buffer marker))
9101 (pos (marker-position marker))
9102 cdate today)
9103 (org-with-remote-undo buffer
9104 (with-current-buffer buffer
9105 (widen)
9106 (goto-char pos)
9107 (unless (org-at-timestamp-p 'lax) (error "Cannot find time stamp"))
9108 (when (and org-agenda-move-date-from-past-immediately-to-today
9109 (equal arg 1)
9110 (or (not what) (eq what 'day))
9111 (not (save-match-data (org-at-date-range-p))))
9112 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9113 cdate (calendar-absolute-from-gregorian
9114 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9115 today (org-today))
9116 (if (> today cdate)
9117 ;; immediately shift to today
9118 (setq arg (- today cdate))))
9119 (org-timestamp-change arg (or what 'day))
9120 (when (and (org-at-date-range-p)
9121 (re-search-backward org-tr-regexp-both (point-at-bol)))
9122 (let ((end org-last-changed-timestamp))
9123 (org-timestamp-change arg (or what 'day))
9124 (setq org-last-changed-timestamp
9125 (concat org-last-changed-timestamp "--" end)))))
9126 (org-agenda-show-new-time marker org-last-changed-timestamp))
9127 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9129 (defun org-agenda-date-earlier (arg &optional what)
9130 "Change the date of this item to ARG day(s) earlier."
9131 (interactive "p")
9132 (org-agenda-date-later (- arg) what))
9134 (defun org-agenda-date-later-minutes (arg)
9135 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9136 (interactive "p")
9137 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9138 (org-agenda-date-later arg 'minute))
9140 (defun org-agenda-date-earlier-minutes (arg)
9141 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9142 (interactive "p")
9143 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9144 (org-agenda-date-earlier arg 'minute))
9146 (defun org-agenda-date-later-hours (arg)
9147 "Change the time of this item, in hour steps."
9148 (interactive "p")
9149 (org-agenda-date-later arg 'hour))
9151 (defun org-agenda-date-earlier-hours (arg)
9152 "Change the time of this item, in hour steps."
9153 (interactive "p")
9154 (org-agenda-date-earlier arg 'hour))
9156 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9157 "Show new date stamp via text properties."
9158 ;; We use text properties to make this undoable
9159 (let ((inhibit-read-only t))
9160 (setq stamp (concat prefix " => " stamp " "))
9161 (save-excursion
9162 (goto-char (point-max))
9163 (while (not (bobp))
9164 (when (equal marker (org-get-at-bol 'org-marker))
9165 (remove-text-properties (point-at-bol) (point-at-eol) '(display))
9166 (org-move-to-column (- (window-width) (length stamp)) t)
9167 (add-text-properties
9168 (1- (point)) (point-at-eol)
9169 (list 'display (org-add-props stamp nil
9170 'face '(secondary-selection default))))
9171 (beginning-of-line 1))
9172 (beginning-of-line 0)))))
9174 (defun org-agenda-date-prompt (arg)
9175 "Change the date of this item. Date is prompted for, with default today.
9176 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9177 be used to request time specification in the time stamp."
9178 (interactive "P")
9179 (org-agenda-check-type t 'agenda 'timeline)
9180 (org-agenda-check-no-diary)
9181 (let* ((marker (or (org-get-at-bol 'org-marker)
9182 (org-agenda-error)))
9183 (buffer (marker-buffer marker))
9184 (pos (marker-position marker)))
9185 (org-with-remote-undo buffer
9186 (with-current-buffer buffer
9187 (widen)
9188 (goto-char pos)
9189 (unless (org-at-timestamp-p 'lax) (error "Cannot find time stamp"))
9190 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
9191 (org-agenda-show-new-time marker org-last-changed-timestamp))
9192 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9194 (defun org-agenda-schedule (arg &optional time)
9195 "Schedule the item at point.
9196 ARG is passed through to `org-schedule'."
9197 (interactive "P")
9198 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9199 (org-agenda-check-no-diary)
9200 (let* ((marker (or (org-get-at-bol 'org-marker)
9201 (org-agenda-error)))
9202 (type (marker-insertion-type marker))
9203 (buffer (marker-buffer marker))
9204 (pos (marker-position marker))
9206 (set-marker-insertion-type marker t)
9207 (org-with-remote-undo buffer
9208 (with-current-buffer buffer
9209 (widen)
9210 (goto-char pos)
9211 (setq ts (org-schedule arg time)))
9212 (org-agenda-show-new-time marker ts " S"))
9213 (message "%s" ts)))
9215 (defun org-agenda-deadline (arg &optional time)
9216 "Schedule the item at point.
9217 ARG is passed through to `org-deadline'."
9218 (interactive "P")
9219 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9220 (org-agenda-check-no-diary)
9221 (let* ((marker (or (org-get-at-bol 'org-marker)
9222 (org-agenda-error)))
9223 (buffer (marker-buffer marker))
9224 (pos (marker-position marker))
9226 (org-with-remote-undo buffer
9227 (with-current-buffer buffer
9228 (widen)
9229 (goto-char pos)
9230 (setq ts (org-deadline arg time)))
9231 (org-agenda-show-new-time marker ts " D"))
9232 (message "%s" ts)))
9234 (defun org-agenda-clock-in (&optional arg)
9235 "Start the clock on the currently selected item."
9236 (interactive "P")
9237 (org-agenda-check-no-diary)
9238 (if (equal arg '(4))
9239 (org-clock-in arg)
9240 (let* ((marker (or (org-get-at-bol 'org-marker)
9241 (org-agenda-error)))
9242 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9243 (pos (marker-position marker))
9244 (col (current-column))
9245 newhead)
9246 (org-with-remote-undo (marker-buffer marker)
9247 (with-current-buffer (marker-buffer marker)
9248 (widen)
9249 (goto-char pos)
9250 (org-show-context 'agenda)
9251 (org-cycle-hide-drawers 'children)
9252 (org-clock-in arg)
9253 (setq newhead (org-get-heading)))
9254 (org-agenda-change-all-lines newhead hdmarker))
9255 (org-move-to-column col))))
9257 (defun org-agenda-clock-out ()
9258 "Stop the currently running clock."
9259 (interactive)
9260 (unless (marker-buffer org-clock-marker)
9261 (error "No running clock"))
9262 (let ((marker (make-marker)) (col (current-column)) newhead)
9263 (org-with-remote-undo (marker-buffer org-clock-marker)
9264 (with-current-buffer (marker-buffer org-clock-marker)
9265 (org-with-wide-buffer
9266 (goto-char org-clock-marker)
9267 (org-back-to-heading t)
9268 (move-marker marker (point))
9269 (org-clock-out)
9270 (setq newhead (org-get-heading)))))
9271 (org-agenda-change-all-lines newhead marker)
9272 (move-marker marker nil)
9273 (org-move-to-column col)
9274 (org-agenda-unmark-clocking-task)))
9276 (defun org-agenda-clock-cancel (&optional arg)
9277 "Cancel the currently running clock."
9278 (interactive "P")
9279 (unless (marker-buffer org-clock-marker)
9280 (user-error "No running clock"))
9281 (org-with-remote-undo (marker-buffer org-clock-marker)
9282 (org-clock-cancel)))
9284 (defun org-agenda-clock-goto ()
9285 "Jump to the currently clocked in task within the agenda.
9286 If the currently clocked in task is not listed in the agenda
9287 buffer, display it in another window."
9288 (interactive)
9289 (let (pos)
9290 (mapc (lambda (o)
9291 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
9292 (setq pos (overlay-start o))))
9293 (overlays-in (point-min) (point-max)))
9294 (cond (pos (goto-char pos))
9295 ;; If the currently clocked entry is not in the agenda
9296 ;; buffer, we visit it in another window:
9297 ((bound-and-true-p org-clock-current-task)
9298 (org-switch-to-buffer-other-window (org-clock-goto)))
9299 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9301 (defun org-agenda-diary-entry-in-org-file ()
9302 "Make a diary entry in the file `org-agenda-diary-file'."
9303 (let (d1 d2 char (text "") dp1 dp2)
9304 (if (equal (buffer-name) "*Calendar*")
9305 (setq d1 (calendar-cursor-to-date t)
9306 d2 (car calendar-mark-ring))
9307 (setq dp1 (get-text-property (point-at-bol) 'day))
9308 (unless dp1 (user-error "No date defined in current line"))
9309 (setq d1 (calendar-gregorian-from-absolute dp1)
9310 d2 (and (ignore-errors (mark))
9311 (save-excursion
9312 (goto-char (mark))
9313 (setq dp2 (get-text-property (point-at-bol) 'day)))
9314 (calendar-gregorian-from-absolute dp2))))
9315 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9316 (setq char (read-char-exclusive))
9317 (cond
9318 ((equal char ?d)
9319 (setq text (read-string "Day entry: "))
9320 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9321 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9322 ((equal char ?a)
9323 (setq d1 (list (car d1) (nth 1 d1)
9324 (read-number (format "Reference year [%d]: " (nth 2 d1))
9325 (nth 2 d1))))
9326 (setq text (read-string "Anniversary (use %d to show years): "))
9327 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9328 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9329 ((equal char ?b)
9330 (setq text (read-string "Block entry: "))
9331 (unless (and d1 d2 (not (equal d1 d2)))
9332 (user-error "No block of days selected"))
9333 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9334 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9335 ((equal char ?j)
9336 (org-switch-to-buffer-other-window
9337 (find-file-noselect org-agenda-diary-file))
9338 (require 'org-datetree)
9339 (org-datetree-find-date-create d1)
9340 (org-reveal t))
9341 (t (user-error "Invalid selection character `%c'" char)))))
9343 (defcustom org-agenda-insert-diary-strategy 'date-tree
9344 "Where in `org-agenda-diary-file' should new entries be added?
9345 Valid values:
9347 date-tree in the date tree, as first child of the date
9348 date-tree-last in the date tree, as last child of the date
9349 top-level as top-level entries at the end of the file."
9350 :group 'org-agenda
9351 :type '(choice
9352 (const :tag "first in a date tree" date-tree)
9353 (const :tag "last in a date tree" date-tree-last)
9354 (const :tag "as top level at end of file" top-level)))
9356 (defcustom org-agenda-insert-diary-extract-time nil
9357 "Non-nil means extract any time specification from the diary entry."
9358 :group 'org-agenda
9359 :version "24.1"
9360 :type 'boolean)
9362 (defcustom org-agenda-bulk-mark-char ">"
9363 "A single-character string to be used as the bulk mark."
9364 :group 'org-agenda
9365 :version "24.1"
9366 :type 'string)
9368 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9369 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9370 If TEXT is not empty, it will become the headline of the new entry, and
9371 the resulting entry will not be shown. When TEXT is empty, switch to
9372 `org-agenda-diary-file' and let the user finish the entry there."
9373 (let ((cw (current-window-configuration)))
9374 (org-switch-to-buffer-other-window
9375 (find-file-noselect org-agenda-diary-file))
9376 (widen)
9377 (goto-char (point-min))
9378 (cond
9379 ((eq type 'anniversary)
9380 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
9381 (progn
9382 (or (org-at-heading-p t)
9383 (progn
9384 (outline-next-heading)
9385 (insert "* Anniversaries\n\n")
9386 (beginning-of-line -1)))))
9387 (outline-next-heading)
9388 (org-back-over-empty-lines)
9389 (backward-char 1)
9390 (insert "\n")
9391 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9392 (nth 2 d1) (car d1) (nth 1 d1) text)))
9393 ((eq type 'day)
9394 (let ((org-prefix-has-time t)
9395 (org-agenda-time-leading-zero t)
9396 fmt time time2)
9397 (if org-agenda-insert-diary-extract-time
9398 ;; Use org-agenda-format-item to parse text for a time-range and
9399 ;; remove it. FIXME: This is a hack, we should refactor
9400 ;; that function to make time extraction available separately
9401 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9402 time (get-text-property 0 'time fmt)
9403 time2 (if (> (length time) 0)
9404 ;; split-string removes trailing ...... if
9405 ;; no end time given. First space
9406 ;; separates time from date.
9407 (concat " " (car (split-string time "\\.")))
9408 nil)
9409 text (get-text-property 0 'txt fmt)))
9410 (if (eq org-agenda-insert-diary-strategy 'top-level)
9411 (org-agenda-insert-diary-as-top-level text)
9412 (require 'org-datetree)
9413 (org-datetree-find-date-create d1)
9414 (org-agenda-insert-diary-make-new-entry text))
9415 (org-insert-time-stamp (org-time-from-absolute
9416 (calendar-absolute-from-gregorian d1))
9417 nil nil nil nil time2))
9418 (end-of-line 0))
9419 ((eq type 'block)
9420 (if (> (calendar-absolute-from-gregorian d1)
9421 (calendar-absolute-from-gregorian d2))
9422 (setq d1 (prog1 d2 (setq d2 d1))))
9423 (if (eq org-agenda-insert-diary-strategy 'top-level)
9424 (org-agenda-insert-diary-as-top-level text)
9425 (require 'org-datetree)
9426 (org-datetree-find-date-create d1)
9427 (org-agenda-insert-diary-make-new-entry text))
9428 (org-insert-time-stamp (org-time-from-absolute
9429 (calendar-absolute-from-gregorian d1)))
9430 (insert "--")
9431 (org-insert-time-stamp (org-time-from-absolute
9432 (calendar-absolute-from-gregorian d2)))
9433 (end-of-line 0)))
9434 (if (string-match "\\S-" text)
9435 (progn
9436 (set-window-configuration cw)
9437 (message "%s entry added to %s"
9438 (capitalize (symbol-name type))
9439 (abbreviate-file-name org-agenda-diary-file)))
9440 (org-reveal t)
9441 (message "Please finish entry here"))))
9443 (defun org-agenda-insert-diary-as-top-level (text)
9444 "Make new entry as a top-level entry at the end of the file.
9445 Add TEXT as headline, and position the cursor in the second line so that
9446 a timestamp can be added there."
9447 (widen)
9448 (goto-char (point-max))
9449 (unless (bolp) (insert "\n"))
9450 (org-insert-heading nil t t)
9451 (insert text)
9452 (org-end-of-meta-data)
9453 (unless (bolp) (insert "\n"))
9454 (when org-adapt-indentation (indent-to-column 2)))
9456 (defun org-agenda-insert-diary-make-new-entry (text)
9457 "Make a new entry with TEXT as a child of the current subtree.
9458 Position the point in the heading's first body line so that
9459 a timestamp can be added there."
9460 (cond
9461 ((eq org-agenda-insert-diary-strategy 'date-tree-last)
9462 (end-of-line)
9463 (org-insert-heading '(4) t)
9464 (org-do-demote))
9466 (outline-next-heading)
9467 (org-back-over-empty-lines)
9468 (unless (looking-at "[ \t]*$") (save-excursion (insert "\n")))
9469 (org-insert-heading nil t)
9470 (org-do-demote)))
9471 (let ((col (current-column)))
9472 (insert text)
9473 (org-end-of-meta-data)
9474 ;; Ensure point is left on a blank line, at proper indentation.
9475 (unless (bolp) (insert "\n"))
9476 (unless (looking-at-p "^[ \t]*$") (save-excursion (insert "\n")))
9477 (when org-adapt-indentation (indent-to-column col)))
9478 (org-show-set-visibility 'lineage))
9480 (defun org-agenda-diary-entry ()
9481 "Make a diary entry, like the `i' command from the calendar.
9482 All the standard commands work: block, weekly etc.
9483 When `org-agenda-diary-file' points to a file,
9484 `org-agenda-diary-entry-in-org-file' is called instead to create
9485 entries in that Org file."
9486 (interactive)
9487 (if (not (eq org-agenda-diary-file 'diary-file))
9488 (org-agenda-diary-entry-in-org-file)
9489 (require 'diary-lib)
9490 (let* ((char (progn
9491 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
9492 (read-char-exclusive)))
9493 (cmd (cdr (assoc char
9494 '((?d . diary-insert-entry)
9495 (?w . diary-insert-weekly-entry)
9496 (?m . diary-insert-monthly-entry)
9497 (?y . diary-insert-yearly-entry)
9498 (?a . diary-insert-anniversary-entry)
9499 (?b . diary-insert-block-entry)
9500 (?c . diary-insert-cyclic-entry)))))
9501 (oldf (symbol-function 'calendar-cursor-to-date))
9502 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9503 (point (point))
9504 (mark (or (mark t) (point))))
9505 (unless cmd
9506 (user-error "No command associated with <%c>" char))
9507 (unless (and (get-text-property point 'day)
9508 (or (not (equal ?b char))
9509 (get-text-property mark 'day)))
9510 (user-error "Don't know which date to use for diary entry"))
9511 ;; We implement this by hacking the `calendar-cursor-to-date' function
9512 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9513 (let ((calendar-mark-ring
9514 (list (calendar-gregorian-from-absolute
9515 (or (get-text-property mark 'day)
9516 (get-text-property point 'day))))))
9517 (unwind-protect
9518 (progn
9519 (fset 'calendar-cursor-to-date
9520 (lambda (&optional error dummy)
9521 (calendar-gregorian-from-absolute
9522 (get-text-property point 'day))))
9523 (call-interactively cmd))
9524 (fset 'calendar-cursor-to-date oldf))))))
9526 (defun org-agenda-execute-calendar-command (cmd)
9527 "Execute a calendar command from the agenda with date from cursor."
9528 (org-agenda-check-type t 'agenda 'timeline)
9529 (require 'diary-lib)
9530 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9531 (user-error "Don't know which date to use for the calendar command"))
9532 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9533 (point (point))
9534 (date (calendar-gregorian-from-absolute
9535 (get-text-property point 'day)))
9536 ;; the following 2 vars are needed in the calendar
9537 (displayed-month (car date))
9538 (displayed-year (nth 2 date)))
9539 (unwind-protect
9540 (progn
9541 (fset 'calendar-cursor-to-date
9542 (lambda (&optional error dummy)
9543 (calendar-gregorian-from-absolute
9544 (get-text-property point 'day))))
9545 (call-interactively cmd))
9546 (fset 'calendar-cursor-to-date oldf))))
9548 (defun org-agenda-phases-of-moon ()
9549 "Display the phases of the moon for the 3 months around the cursor date."
9550 (interactive)
9551 (org-agenda-execute-calendar-command 'calendar-lunar-phases))
9553 (defun org-agenda-holidays ()
9554 "Display the holidays for the 3 months around the cursor date."
9555 (interactive)
9556 (org-agenda-execute-calendar-command 'calendar-list-holidays))
9558 (defvar calendar-longitude) ; defined in calendar.el
9559 (defvar calendar-latitude) ; defined in calendar.el
9560 (defvar calendar-location-name) ; defined in calendar.el
9562 (defun org-agenda-sunrise-sunset (arg)
9563 "Display sunrise and sunset for the cursor date.
9564 Latitude and longitude can be specified with the variables
9565 `calendar-latitude' and `calendar-longitude'. When called with prefix
9566 argument, latitude and longitude will be prompted for."
9567 (interactive "P")
9568 (require 'solar)
9569 (let ((calendar-longitude (if arg nil calendar-longitude))
9570 (calendar-latitude (if arg nil calendar-latitude))
9571 (calendar-location-name
9572 (if arg "the given coordinates" calendar-location-name)))
9573 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9575 (defun org-agenda-goto-calendar ()
9576 "Open the Emacs calendar with the date at the cursor."
9577 (interactive)
9578 (org-agenda-check-type t 'agenda 'timeline)
9579 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9580 (user-error "Don't know which date to open in calendar")))
9581 (date (calendar-gregorian-from-absolute day))
9582 (calendar-move-hook nil)
9583 (calendar-view-holidays-initially-flag nil)
9584 (calendar-view-diary-initially-flag nil))
9585 (calendar)
9586 (calendar-goto-date date)))
9588 ;;;###autoload
9589 (defun org-calendar-goto-agenda ()
9590 "Compute the Org agenda for the calendar date displayed at the cursor.
9591 This is a command that has to be installed in `calendar-mode-map'."
9592 (interactive)
9593 ;; Temporarily disable sticky agenda since user clearly wants to
9594 ;; refresh view anyway.
9595 (let ((org-agenda-buffer-tmp-name "*Org Agenda(a)*")
9596 (org-agenda-sticky nil))
9597 (org-agenda-list nil (calendar-absolute-from-gregorian
9598 (calendar-cursor-to-date))
9599 nil)))
9601 (defun org-agenda-convert-date ()
9602 (interactive)
9603 (org-agenda-check-type t 'agenda 'timeline)
9604 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9605 date s)
9606 (unless day
9607 (user-error "Don't know which date to convert"))
9608 (setq date (calendar-gregorian-from-absolute day))
9609 (setq s (concat
9610 "Gregorian: " (calendar-date-string date) "\n"
9611 "ISO: " (calendar-iso-date-string date) "\n"
9612 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9613 "Julian: " (calendar-julian-date-string date) "\n"
9614 "Astron. JD: " (calendar-astro-date-string date)
9615 " (Julian date number at noon UTC)\n"
9616 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9617 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9618 "French: " (calendar-french-date-string date) "\n"
9619 "Bahá’í: " (calendar-bahai-date-string date) " (until sunset)\n"
9620 "Mayan: " (calendar-mayan-date-string date) "\n"
9621 "Coptic: " (calendar-coptic-date-string date) "\n"
9622 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9623 "Persian: " (calendar-persian-date-string date) "\n"
9624 "Chinese: " (calendar-chinese-date-string date) "\n"))
9625 (with-output-to-temp-buffer "*Dates*"
9626 (princ s))
9627 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9629 ;;; Bulk commands
9631 (defun org-agenda-bulk-marked-p ()
9632 "Non-nil when current entry is marked for bulk action."
9633 (eq (get-char-property (point-at-bol) 'type)
9634 'org-marked-entry-overlay))
9636 (defun org-agenda-bulk-mark (&optional arg)
9637 "Mark the entry at point for future bulk action."
9638 (interactive "p")
9639 (dotimes (i (or arg 1))
9640 (unless (org-get-at-bol 'org-agenda-diary-link)
9641 (let* ((m (org-get-at-bol 'org-hd-marker))
9643 (unless (org-agenda-bulk-marked-p)
9644 (unless m (user-error "Nothing to mark at point"))
9645 (push m org-agenda-bulk-marked-entries)
9646 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9647 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9648 (org-get-todo-face "TODO")
9649 'evaporate)
9650 (overlay-put ov 'type 'org-marked-entry-overlay))
9651 (end-of-line 1)
9652 (or (ignore-errors
9653 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9654 (beginning-of-line 2))
9655 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9656 (beginning-of-line 2))
9657 (message "%d entries marked for bulk action"
9658 (length org-agenda-bulk-marked-entries))))))
9660 (defun org-agenda-bulk-mark-all ()
9661 "Mark all entries for future agenda bulk action."
9662 (interactive)
9663 (org-agenda-bulk-mark-regexp "."))
9665 (defun org-agenda-bulk-mark-regexp (regexp)
9666 "Mark entries matching REGEXP for future agenda bulk action."
9667 (interactive "sMark entries matching regexp: ")
9668 (let ((entries-marked 0) txt-at-point)
9669 (save-excursion
9670 (goto-char (point-min))
9671 (goto-char (next-single-property-change (point) 'org-hd-marker))
9672 (while (and (re-search-forward regexp nil t)
9673 (setq txt-at-point (get-text-property (point) 'txt)))
9674 (if (get-char-property (point) 'invisible)
9675 (beginning-of-line 2)
9676 (when (string-match regexp txt-at-point)
9677 (setq entries-marked (1+ entries-marked))
9678 (call-interactively 'org-agenda-bulk-mark)))))
9680 (if (not entries-marked)
9681 (message "No entry matching this regexp."))))
9683 (defun org-agenda-bulk-unmark (&optional arg)
9684 "Unmark the entry at point for future bulk action."
9685 (interactive "P")
9686 (if arg
9687 (org-agenda-bulk-unmark-all)
9688 (cond ((org-agenda-bulk-marked-p)
9689 (org-agenda-bulk-remove-overlays
9690 (point-at-bol) (+ 2 (point-at-bol)))
9691 (setq org-agenda-bulk-marked-entries
9692 (delete (org-get-at-bol 'org-hd-marker)
9693 org-agenda-bulk-marked-entries))
9694 (end-of-line 1)
9695 (or (ignore-errors
9696 (goto-char (next-single-property-change (point) 'txt)))
9697 (beginning-of-line 2))
9698 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9699 (beginning-of-line 2))
9700 (message "%d entries left marked for bulk action"
9701 (length org-agenda-bulk-marked-entries)))
9702 (t (message "No entry to unmark here")))))
9704 (defun org-agenda-bulk-toggle-all ()
9705 "Toggle all marks for bulk action."
9706 (interactive)
9707 (save-excursion
9708 (goto-char (point-min))
9709 (while (ignore-errors
9710 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9711 (org-agenda-bulk-toggle))))
9713 (defun org-agenda-bulk-toggle ()
9714 "Toggle the mark at point for bulk action."
9715 (interactive)
9716 (if (org-agenda-bulk-marked-p)
9717 (org-agenda-bulk-unmark)
9718 (org-agenda-bulk-mark)))
9720 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9721 "Remove the mark overlays between BEG and END in the agenda buffer.
9722 BEG and END default to the buffer limits.
9724 This only removes the overlays, it does not remove the markers
9725 from the list in `org-agenda-bulk-marked-entries'."
9726 (interactive)
9727 (mapc (lambda (ov)
9728 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9729 (delete-overlay ov)))
9730 (overlays-in (or beg (point-min)) (or end (point-max)))))
9732 (defun org-agenda-bulk-unmark-all ()
9733 "Remove all marks in the agenda buffer.
9734 This will remove the markers and the overlays."
9735 (interactive)
9736 (if (null org-agenda-bulk-marked-entries)
9737 (message "No entry to unmark")
9738 (setq org-agenda-bulk-marked-entries nil)
9739 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9741 (defcustom org-agenda-persistent-marks nil
9742 "Non-nil means marked items will stay marked after a bulk action.
9743 You can toggle this interactively by typing `p' when prompted for a
9744 bulk action."
9745 :group 'org-agenda
9746 :version "24.1"
9747 :type 'boolean)
9749 (defun org-agenda-bulk-action (&optional arg)
9750 "Execute an remote-editing action on all marked entries.
9751 The prefix arg is passed through to the command if possible."
9752 (interactive "P")
9753 ;; Make sure we have markers, and only valid ones.
9754 (unless org-agenda-bulk-marked-entries (user-error "No entries are marked"))
9755 (dolist (m org-agenda-bulk-marked-entries)
9756 (unless (and (markerp m)
9757 (marker-buffer m)
9758 (buffer-live-p (marker-buffer m))
9759 (marker-position m))
9760 (user-error "Marker %s for bulk command is invalid" m)))
9762 ;; Prompt for the bulk command.
9763 (message
9764 (concat (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")
9765 "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9766 "[S]catter [f]unction "
9767 (and org-agenda-bulk-custom-functions
9768 (format " Custom: [%s]"
9769 (mapconcat (lambda (f) (char-to-string (car f)))
9770 org-agenda-bulk-custom-functions
9771 "")))))
9772 (catch 'exit
9773 (let* ((org-log-refile (if org-log-refile 'time nil))
9774 (entries (reverse org-agenda-bulk-marked-entries))
9775 (org-overriding-default-time
9776 (and (get-text-property (point) 'org-agenda-date-header)
9777 (org-get-cursor-date)))
9778 redo-at-end
9779 cmd)
9780 (pcase (read-char-exclusive)
9782 (let ((org-agenda-persistent-marks
9783 (not org-agenda-persistent-marks)))
9784 (org-agenda-bulk-action)
9785 (throw 'exit nil)))
9788 (setq cmd #'org-agenda-archive))
9791 (setq cmd #'org-agenda-archive-to-archive-sibling))
9793 ((or ?r ?w)
9794 (let ((refile-location
9795 (org-refile-get-location
9796 "Refile to"
9797 (marker-buffer (car entries))
9798 org-refile-allow-creating-parent-nodes)))
9799 (when (nth 3 refile-location)
9800 (setcar (nthcdr 3 refile-location)
9801 (move-marker
9802 (make-marker)
9803 (nth 3 refile-location)
9804 (or (get-file-buffer (nth 1 refile-location))
9805 (find-buffer-visiting (nth 1 refile-location))
9806 (error "This should not happen")))))
9808 (setq cmd `(lambda () (org-agenda-refile nil ',refile-location t)))
9809 (setq redo-at-end t)))
9812 (let ((state (completing-read
9813 "Todo state: "
9814 (with-current-buffer (marker-buffer (car entries))
9815 (mapcar #'list org-todo-keywords-1)))))
9816 (setq cmd `(lambda ()
9817 (let ((org-inhibit-blocking t)
9818 (org-inhibit-logging 'note))
9819 (org-agenda-todo ,state))))))
9821 ((and (or ?- ?+) action)
9822 (let ((tag (completing-read
9823 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9824 (with-current-buffer (marker-buffer (car entries))
9825 (delq nil
9826 (mapcar (lambda (x) (and (stringp (car x)) x))
9827 org-current-tag-alist))))))
9828 (setq cmd
9829 `(lambda ()
9830 (org-agenda-set-tags ,tag
9831 ,(if (eq action ?+) ''on ''off))))))
9834 (let ((time
9835 (or arg
9836 (org-read-date nil nil nil "(Re)Schedule to"
9837 org-overriding-default-time))))
9838 ;; Make sure to not prompt for a note when bulk
9839 ;; rescheduling as Org cannot cope with simultaneous notes.
9840 ;; Besides, it could be annoying depending on the number of
9841 ;; items re-scheduled.
9842 (setq cmd
9843 `(lambda ()
9844 (let ((org-log-reschedule (and org-log-reschedule 'time)))
9845 (org-agenda-schedule arg ,time))))))
9847 (let ((time
9848 (or arg
9849 (org-read-date nil nil nil "(Re)Set Deadline to"
9850 org-overriding-default-time))))
9851 ;; Make sure to not prompt for a note when bulk
9852 ;; rescheduling as Org cannot cope with simultaneous
9853 ;; notes. Besides, it could be annoying depending on the
9854 ;; number of items re-scheduled.
9855 (setq cmd
9856 `(lambda ()
9857 (let ((org-log-redeadline (and org-log-redeadline 'time)))
9858 (org-agenda-deadline arg ,time))))))
9861 (unless (org-agenda-check-type nil 'agenda 'timeline 'todo)
9862 (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type))
9863 (let ((days (read-number
9864 (format "Scatter tasks across how many %sdays: "
9865 (if arg "week" ""))
9866 7)))
9867 (setq cmd
9868 `(lambda ()
9869 (let ((distance (1+ (random ,days))))
9870 (when arg
9871 (let ((dist distance)
9872 (day-of-week
9873 (calendar-day-of-week
9874 (calendar-gregorian-from-absolute (org-today)))))
9875 (dotimes (i (1+ dist))
9876 (while (member day-of-week org-agenda-weekend-days)
9877 (cl-incf distance)
9878 (cl-incf day-of-week)
9879 (when (= day-of-week 7)
9880 (setq day-of-week 0)))
9881 (cl-incf day-of-week)
9882 (when (= day-of-week 7)
9883 (setq day-of-week 0)))))
9884 ;; Silently fail when try to replan a sexp entry.
9885 (ignore-errors
9886 (let* ((date (calendar-gregorian-from-absolute
9887 (+ (org-today) distance)))
9888 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9889 (nth 2 date))))
9890 (org-agenda-schedule nil time))))))))
9893 (setq cmd
9894 (intern
9895 (completing-read "Function: " obarray #'fboundp t nil nil))))
9897 (action
9898 (pcase (assoc action org-agenda-bulk-custom-functions)
9899 (`(,_ ,f) (setq cmd f) (setq redo-at-end t))
9900 (_ (user-error "Invalid bulk action: %c" action)))))
9902 ;; Sort the markers, to make sure that parents are handled
9903 ;; before children.
9904 (setq entries (sort entries
9905 (lambda (a b)
9906 (cond
9907 ((eq (marker-buffer a) (marker-buffer b))
9908 (< (marker-position a) (marker-position b)))
9910 (string< (buffer-name (marker-buffer a))
9911 (buffer-name (marker-buffer b))))))))
9913 ;; Now loop over all markers and apply CMD.
9914 (let ((processed 0)
9915 (skipped 0))
9916 (dolist (e entries)
9917 (let ((pos (text-property-any (point-min) (point-max) 'org-hd-marker e)))
9918 (if (not pos)
9919 (progn (message "Skipping removed entry at %s" e)
9920 (cl-incf skipped))
9921 (goto-char pos)
9922 (let (org-loop-over-headlines-in-active-region) (funcall cmd))
9923 ;; `post-command-hook' is not run yet. We make sure any
9924 ;; pending log note is processed.
9925 (when (or (memq 'org-add-log-note (default-value 'post-command-hook))
9926 (memq 'org-add-log-note post-command-hook))
9927 (org-add-log-note))
9928 (cl-incf processed))))
9929 (when redo-at-end (org-agenda-redo))
9930 (unless org-agenda-persistent-marks (org-agenda-bulk-unmark-all))
9931 (message "Acted on %d entries%s%s"
9932 processed
9933 (if (= skipped 0)
9935 (format ", skipped %d (disappeared before their turn)"
9936 skipped))
9937 (if (not org-agenda-persistent-marks) "" " (kept marked)"))))))
9939 (defun org-agenda-capture (&optional with-time)
9940 "Call `org-capture' with the date at point.
9941 With a `C-1' prefix, use the HH:MM value at point (if any) or the
9942 current HH:MM time."
9943 (interactive "P")
9944 (if (not (eq major-mode 'org-agenda-mode))
9945 (user-error "You cannot do this outside of agenda buffers")
9946 (let ((org-overriding-default-time
9947 (org-get-cursor-date (equal with-time 1))))
9948 (call-interactively 'org-capture))))
9950 ;;; Dragging agenda lines forward/backward
9952 (defun org-agenda-reapply-filters ()
9953 "Re-apply all agenda filters."
9954 (mapcar
9955 (lambda(f) (when (car f) (org-agenda-filter-apply (car f) (cadr f) t)))
9956 `((,org-agenda-tag-filter tag)
9957 (,org-agenda-category-filter category)
9958 (,org-agenda-regexp-filter regexp)
9959 (,org-agenda-effort-filter effort)
9960 (,(get 'org-agenda-tag-filter :preset-filter) tag)
9961 (,(get 'org-agenda-category-filter :preset-filter) category)
9962 (,(get 'org-agenda-effort-filter :preset-filter) effort)
9963 (,(get 'org-agenda-regexp-filter :preset-filter) regexp))))
9965 (defun org-agenda-drag-line-forward (arg &optional backward)
9966 "Drag an agenda line forward by ARG lines.
9967 When the optional argument `backward' is non-nil, move backward."
9968 (interactive "p")
9969 (let ((inhibit-read-only t) lst line)
9970 (if (or (not (get-text-property (point) 'txt))
9971 (save-excursion
9972 (dotimes (n arg)
9973 (move-beginning-of-line (if backward 0 2))
9974 (push (not (get-text-property (point) 'txt)) lst))
9975 (delq nil lst)))
9976 (message "Cannot move line forward")
9977 (let ((end (save-excursion (move-beginning-of-line 2) (point))))
9978 (move-beginning-of-line 1)
9979 (setq line (buffer-substring (point) end))
9980 (delete-region (point) end)
9981 (move-beginning-of-line (funcall (if backward '1- '1+) arg))
9982 (insert line)
9983 (org-agenda-reapply-filters)
9984 (org-agenda-mark-clocking-task)
9985 (move-beginning-of-line 0)))))
9987 (defun org-agenda-drag-line-backward (arg)
9988 "Drag an agenda line backward by ARG lines."
9989 (interactive "p")
9990 (org-agenda-drag-line-forward arg t))
9992 ;;; Flagging notes
9994 (defun org-agenda-show-the-flagging-note ()
9995 "Display the flagging note in the other window.
9996 When called a second time in direct sequence, offer to remove the FLAGGING
9997 tag and (if present) the flagging note."
9998 (interactive)
9999 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
10000 (win (selected-window))
10001 note heading newhead)
10002 (unless hdmarker
10003 (user-error "No linked entry at point"))
10004 (if (and (eq this-command last-command)
10005 (y-or-n-p "Unflag and remove any flagging note? "))
10006 (progn
10007 (org-agenda-remove-flag hdmarker)
10008 (let ((win (get-buffer-window "*Flagging Note*")))
10009 (and win (delete-window win)))
10010 (message "Entry unflagged"))
10011 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
10012 (unless note
10013 (user-error "No flagging note"))
10014 (org-kill-new note)
10015 (org-switch-to-buffer-other-window "*Flagging Note*")
10016 (erase-buffer)
10017 (insert note)
10018 (goto-char (point-min))
10019 (while (re-search-forward "\\\\n" nil t)
10020 (replace-match "\n" t t))
10021 (goto-char (point-min))
10022 (select-window win)
10023 (message "%s" (substitute-command-keys "Flagging note pushed to \
10024 kill ring. Press `\\[org-agenda-show-the-flagging-note]' again to remove \
10025 tag and note")))))
10027 (defun org-agenda-remove-flag (marker)
10028 "Remove the FLAGGED tag and any flagging note in the entry."
10029 (let (newhead)
10030 (org-with-point-at marker
10031 (org-toggle-tag "FLAGGED" 'off)
10032 (org-entry-delete nil "THEFLAGGINGNOTE")
10033 (setq newhead (org-get-heading)))
10034 (org-agenda-change-all-lines newhead marker)
10035 (message "Entry unflagged")))
10037 (defun org-agenda-get-any-marker (&optional pos)
10038 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
10039 (get-text-property (or pos (point-at-bol)) 'org-marker)))
10041 ;;; Appointment reminders
10043 (defvar appt-time-msg-list) ; defined in appt.el
10045 ;;;###autoload
10046 (defun org-agenda-to-appt (&optional refresh filter &rest args)
10047 "Activate appointments found in `org-agenda-files'.
10049 With a `\\[universal-argument]' prefix, refresh the list of \
10050 appointments.
10052 If FILTER is t, interactively prompt the user for a regular
10053 expression, and filter out entries that don't match it.
10055 If FILTER is a string, use this string as a regular expression
10056 for filtering entries out.
10058 If FILTER is a function, filter out entries against which
10059 calling the function returns nil. This function takes one
10060 argument: an entry from `org-agenda-get-day-entries'.
10062 FILTER can also be an alist with the car of each cell being
10063 either `headline' or `category'. For example:
10065 \\='((headline \"IMPORTANT\")
10066 (category \"Work\"))
10068 will only add headlines containing IMPORTANT or headlines
10069 belonging to the \"Work\" category.
10071 ARGS are symbols indicating what kind of entries to consider.
10072 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
10073 \(i.e., deadlines and scheduled items with a hh:mm specification)
10074 and :timestamp entries. See the docstring of `org-diary' for
10075 details and examples.
10077 If an entry has a APPT_WARNTIME property, its value will be used
10078 to override `appt-message-warning-time'."
10079 (interactive "P")
10080 (if refresh (setq appt-time-msg-list nil))
10081 (if (eq filter t)
10082 (setq filter (read-from-minibuffer "Regexp filter: ")))
10083 (let* ((cnt 0) ; count added events
10084 (scope (or args '(:deadline* :scheduled* :timestamp)))
10085 (org-agenda-new-buffers nil)
10086 (org-deadline-warning-days 0)
10087 ;; Do not use `org-today' here because appt only takes
10088 ;; time and without date as argument, so it may pass wrong
10089 ;; information otherwise
10090 (today (org-date-to-gregorian
10091 (time-to-days (current-time))))
10092 (org-agenda-restrict nil)
10093 (files (org-agenda-files 'unrestricted)) entries file
10094 (org-agenda-buffer nil))
10095 ;; Get all entries which may contain an appt
10096 (org-agenda-prepare-buffers files)
10097 (while (setq file (pop files))
10098 (setq entries
10099 (delq nil
10100 (append entries
10101 (apply 'org-agenda-get-day-entries
10102 file today scope)))))
10103 ;; Map thru entries and find if we should filter them out
10104 (mapc
10105 (lambda (x)
10106 (let* ((evt (org-trim
10107 (replace-regexp-in-string
10108 org-bracket-link-regexp "\\3"
10109 (or (get-text-property 1 'txt x) ""))))
10110 (cat (get-text-property (1- (length x)) 'org-category x))
10111 (tod (get-text-property 1 'time-of-day x))
10112 (ok (or (null filter)
10113 (and (stringp filter) (string-match filter evt))
10114 (and (functionp filter) (funcall filter x))
10115 (and (listp filter)
10116 (let ((cat-filter (cadr (assq 'category filter)))
10117 (evt-filter (cadr (assq 'headline filter))))
10118 (or (and (stringp cat-filter)
10119 (string-match cat-filter cat))
10120 (and (stringp evt-filter)
10121 (string-match evt-filter evt)))))))
10122 (wrn (get-text-property 1 'warntime x)))
10123 ;; FIXME: Shall we remove text-properties for the appt text?
10124 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
10125 (when (and ok tod (not (string-match "\\`DONE\\|CANCELLED" evt)))
10126 (setq tod (concat "00" (number-to-string tod)))
10127 (setq tod (when (string-match
10128 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
10129 (concat (match-string 1 tod) ":"
10130 (match-string 2 tod))))
10131 (when (if (version< emacs-version "23.3")
10132 (appt-add tod evt)
10133 (appt-add tod evt wrn))
10134 (setq cnt (1+ cnt))))))
10135 entries)
10136 (org-release-buffers org-agenda-new-buffers)
10137 (if (eq cnt 0)
10138 (message "No event to add")
10139 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
10141 (defun org-agenda-today-p (date)
10142 "Non nil when DATE means today.
10143 DATE is either a list of the form (month day year) or a number of
10144 days as returned by `calendar-absolute-from-gregorian' or
10145 `org-today'. This function considers `org-extend-today-until'
10146 when defining today."
10147 (eq (org-today)
10148 (if (consp date) (calendar-absolute-from-gregorian date) date)))
10150 (defun org-agenda-todo-yesterday (&optional arg)
10151 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
10152 (interactive "P")
10153 (let* ((org-use-effective-time t)
10154 (hour (nth 2 (decode-time (org-current-time))))
10155 (org-extend-today-until (1+ hour)))
10156 (org-agenda-todo arg)))
10158 (provide 'org-agenda)
10160 ;;; org-agenda.el ends here