Merge branch 'maint'
[org-mode/org-tableheadings.git] / lisp / org-agenda.el
blob750228be9f139412815f20f88a239638d18f499b
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)
1297 (defcustom org-agenda-log-mode-items '(closed clock)
1298 "List of items that should be shown in agenda log mode.
1299 \\<org-agenda-mode-map>\
1300 This list may contain the following symbols:
1302 closed Show entries that have been closed on that day.
1303 clock Show entries that have received clocked time on that day.
1304 state Show all logged state changes.
1305 Note that instead of changing this variable, you can also press \
1306 `\\[universal-argument] \\[org-agenda-log-mode]' in
1307 the agenda to display all available LOG items temporarily."
1308 :group 'org-agenda-daily/weekly
1309 :type '(set :greedy t (const closed) (const clock) (const state)))
1311 (defcustom org-agenda-clock-consistency-checks
1312 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1313 :gap-ok-around ("4:00")
1314 :default-face ((:background "DarkRed") (:foreground "white"))
1315 :overlap-face nil :gap-face nil :no-end-time-face nil
1316 :long-face nil :short-face nil)
1317 "This is a property list, with the following keys:
1319 :max-duration Mark clocking chunks that are longer than this time.
1320 This is a time string like \"HH:MM\", or the number
1321 of minutes as an integer.
1323 :min-duration Mark clocking chunks that are shorter that this.
1324 This is a time string like \"HH:MM\", or the number
1325 of minutes as an integer.
1327 :max-gap Mark gaps between clocking chunks that are longer than
1328 this duration. A number of minutes, or a string
1329 like \"HH:MM\".
1331 :gap-ok-around List of times during the day which are usually not working
1332 times. When a gap is detected, but the gap contains any
1333 of these times, the gap is *not* reported. For example,
1334 if this is (\"4:00\" \"13:00\") then gaps that contain
1335 4:00 in the morning (i.e. the night) and 13:00
1336 (i.e. a typical lunch time) do not cause a warning.
1337 You should have at least one time during the night in this
1338 list, or otherwise the first task each morning will trigger
1339 a warning because it follows a long gap.
1341 Furthermore, the following properties can be used to define faces for
1342 issue display.
1344 :default-face the default face, if the specific face is undefined
1345 :overlap-face face for overlapping clocks
1346 :gap-face face for gaps between clocks
1347 :no-end-time-face face for incomplete clocks
1348 :long-face face for clock intervals that are too long
1349 :short-face face for clock intervals that are too short"
1350 :group 'org-agenda-daily/weekly
1351 :group 'org-clock
1352 :version "24.1"
1353 :type 'plist)
1355 (defcustom org-agenda-log-mode-add-notes t
1356 "Non-nil means add first line of notes to log entries in agenda views.
1357 If a log item like a state change or a clock entry is associated with
1358 notes, the first line of these notes will be added to the entry in the
1359 agenda display."
1360 :group 'org-agenda-daily/weekly
1361 :type 'boolean)
1363 (defcustom org-agenda-start-with-log-mode nil
1364 "The initial value of log-mode in a newly created agenda window.
1365 See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
1366 explanations on the possible values."
1367 :group 'org-agenda-startup
1368 :group 'org-agenda-daily/weekly
1369 :type '(choice (const :tag "Don't show log items" nil)
1370 (const :tag "Show only log items" only)
1371 (const :tag "Show all possible log items" clockcheck)
1372 (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
1373 (choice (const :tag "Show closed log items" closed)
1374 (const :tag "Show clocked log items" clock)
1375 (const :tag "Show all logged state changes" state)))))
1377 (defcustom org-agenda-start-with-clockreport-mode nil
1378 "The initial value of clockreport-mode in a newly created agenda window."
1379 :group 'org-agenda-startup
1380 :group 'org-agenda-daily/weekly
1381 :type 'boolean)
1383 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1384 "Property list with parameters for the clocktable in clockreport mode.
1385 This is the display mode that shows a clock table in the daily/weekly
1386 agenda, the properties for this dynamic block can be set here.
1387 The usual clocktable parameters are allowed here, but you cannot set
1388 the properties :name, :tstart, :tend, :block, and :scope - these will
1389 be overwritten to make sure the content accurately reflects the
1390 current display in the agenda."
1391 :group 'org-agenda-daily/weekly
1392 :type 'plist)
1394 (defcustom org-agenda-search-view-always-boolean nil
1395 "Non-nil means the search string is interpreted as individual parts.
1397 The search string for search view can either be interpreted as a phrase,
1398 or as a list of snippets that define a boolean search for a number of
1399 strings.
1401 When this is non-nil, the string will be split on whitespace, and each
1402 snippet will be searched individually, and all must match in order to
1403 select an entry. A snippet is then a single string of non-white
1404 characters, or a string in double quotes, or a regexp in {} braces.
1405 If a snippet is preceded by \"-\", the snippet must *not* match.
1406 \"+\" is syntactic sugar for positive selection. Each snippet may
1407 be found as a full word or a partial word, but see the variable
1408 `org-agenda-search-view-force-full-words'.
1410 When this is nil, search will look for the entire search phrase as one,
1411 with each space character matching any amount of whitespace, including
1412 line breaks.
1414 Even when this is nil, you can still switch to Boolean search dynamically
1415 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1416 is a regexp marked with braces like \"{abc}\", this will also switch to
1417 boolean search."
1418 :group 'org-agenda-search-view
1419 :version "24.1"
1420 :type 'boolean)
1422 (defvaralias 'org-agenda-search-view-search-words-only
1423 'org-agenda-search-view-always-boolean)
1425 (defcustom org-agenda-search-view-force-full-words nil
1426 "Non-nil means, search words must be matches as complete words.
1427 When nil, they may also match part of a word."
1428 :group 'org-agenda-search-view
1429 :version "24.1"
1430 :type 'boolean)
1432 (defcustom org-agenda-search-view-max-outline-level 0
1433 "Maximum outline level to display in search view.
1434 E.g. when this is set to 1, the search view will only
1435 show headlines of level 1. When set to 0, the default
1436 value, don't limit agenda view by outline level."
1437 :group 'org-agenda-search-view
1438 :version "24.4"
1439 :package-version '(Org . "8.3")
1440 :type 'integer)
1442 (defgroup org-agenda-time-grid nil
1443 "Options concerning the time grid in the Org Agenda."
1444 :tag "Org Agenda Time Grid"
1445 :group 'org-agenda)
1447 (defcustom org-agenda-search-headline-for-time t
1448 "Non-nil means search headline for a time-of-day.
1449 If the headline contains a time-of-day in one format or another, it will
1450 be used to sort the entry into the time sequence of items for a day.
1451 Some people have time stamps in the headline that refer to the creation
1452 time or so, and then this produces an unwanted side effect. If this is
1453 the case for your, use this variable to turn off searching the headline
1454 for a time."
1455 :group 'org-agenda-time-grid
1456 :type 'boolean)
1458 (defcustom org-agenda-use-time-grid t
1459 "Non-nil means show a time grid in the agenda schedule.
1460 A time grid is a set of lines for specific times (like every two hours between
1461 8:00 and 20:00). The items scheduled for a day at specific times are
1462 sorted in between these lines.
1463 For details about when the grid will be shown, and what it will look like, see
1464 the variable `org-agenda-time-grid'."
1465 :group 'org-agenda-time-grid
1466 :type 'boolean)
1468 (defcustom org-agenda-time-grid
1469 '((daily today require-timed)
1470 "----------------"
1471 (800 1000 1200 1400 1600 1800 2000))
1473 "The settings for time grid for agenda display.
1474 This is a list of three items. The first item is again a list. It contains
1475 symbols specifying conditions when the grid should be displayed:
1477 daily if the agenda shows a single day
1478 weekly if the agenda shows an entire week
1479 today show grid on current date, independent of daily/weekly display
1480 require-timed show grid only if at least one item has a time specification
1481 remove-match skip grid times already present in an entry
1483 The second item is a string which will be placed behind the grid time.
1485 The third item is a list of integers, indicating the times that should have
1486 a grid line."
1487 :group 'org-agenda-time-grid
1488 :type
1489 '(list
1490 (set :greedy t :tag "Grid Display Options"
1491 (const :tag "Show grid in single day agenda display" daily)
1492 (const :tag "Show grid in weekly agenda display" weekly)
1493 (const :tag "Always show grid for today" today)
1494 (const :tag "Show grid only if any timed entries are present"
1495 require-timed)
1496 (const :tag "Skip grid times already present in an entry"
1497 remove-match))
1498 (string :tag "Grid String")
1499 (repeat :tag "Grid Times" (integer :tag "Time"))))
1501 (defcustom org-agenda-show-current-time-in-grid t
1502 "Non-nil means show the current time in the time grid."
1503 :group 'org-agenda-time-grid
1504 :version "24.1"
1505 :type 'boolean)
1507 (defcustom org-agenda-current-time-string
1508 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1509 "The string for the current time marker in the agenda."
1510 :group 'org-agenda-time-grid
1511 :version "24.1"
1512 :type 'string)
1514 (defgroup org-agenda-sorting nil
1515 "Options concerning sorting in the Org Agenda."
1516 :tag "Org Agenda Sorting"
1517 :group 'org-agenda)
1519 (defcustom org-agenda-sorting-strategy
1520 '((agenda habit-down time-up priority-down category-keep)
1521 (todo priority-down category-keep)
1522 (tags priority-down category-keep)
1523 (search category-keep))
1524 "Sorting structure for the agenda items of a single day.
1525 This is a list of symbols which will be used in sequence to determine
1526 if an entry should be listed before another entry. The following
1527 symbols are recognized:
1529 time-up Put entries with time-of-day indications first, early first
1530 time-down Put entries with time-of-day indications first, late first
1531 timestamp-up Sort by any timestamp, early first
1532 timestamp-down Sort by any timestamp, late first
1533 scheduled-up Sort by scheduled timestamp, early first
1534 scheduled-down Sort by scheduled timestamp, late first
1535 deadline-up Sort by deadline timestamp, early first
1536 deadline-down Sort by deadline timestamp, late first
1537 ts-up Sort by active timestamp, early first
1538 ts-down Sort by active timestamp, late first
1539 tsia-up Sort by inactive timestamp, early first
1540 tsia-down Sort by inactive timestamp, late first
1541 category-keep Keep the default order of categories, corresponding to the
1542 sequence in `org-agenda-files'.
1543 category-up Sort alphabetically by category, A-Z.
1544 category-down Sort alphabetically by category, Z-A.
1545 tag-up Sort alphabetically by last tag, A-Z.
1546 tag-down Sort alphabetically by last tag, Z-A.
1547 priority-up Sort numerically by priority, high priority last.
1548 priority-down Sort numerically by priority, high priority first.
1549 todo-state-up Sort by todo state, tasks that are done last.
1550 todo-state-down Sort by todo state, tasks that are done first.
1551 effort-up Sort numerically by estimated effort, high effort last.
1552 effort-down Sort numerically by estimated effort, high effort first.
1553 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1554 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1555 habit-up Put entries that are habits first
1556 habit-down Put entries that are habits last
1557 alpha-up Sort headlines alphabetically
1558 alpha-down Sort headlines alphabetically, reversed
1560 The different possibilities will be tried in sequence, and testing stops
1561 if one comparison returns a \"not-equal\". For example, the default
1562 '(time-up category-keep priority-down)
1563 means: Pull out all entries having a specified time of day and sort them,
1564 in order to make a time schedule for the current day the first thing in the
1565 agenda listing for the day. Of the entries without a time indication, keep
1566 the grouped in categories, don't sort the categories, but keep them in
1567 the sequence given in `org-agenda-files'. Within each category sort by
1568 priority.
1570 Leaving out `category-keep' would mean that items will be sorted across
1571 categories by priority.
1573 Instead of a single list, this can also be a set of list for specific
1574 contents, with a context symbol in the car of the list, any of
1575 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1577 Custom commands can bind this variable in the options section."
1578 :group 'org-agenda-sorting
1579 :type `(choice
1580 (repeat :tag "General" ,org-sorting-choice)
1581 (list :tag "Individually"
1582 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1583 (repeat ,org-sorting-choice))
1584 (cons (const :tag "Strategy for TODO lists" todo)
1585 (repeat ,org-sorting-choice))
1586 (cons (const :tag "Strategy for Tags matches" tags)
1587 (repeat ,org-sorting-choice))
1588 (cons (const :tag "Strategy for search matches" search)
1589 (repeat ,org-sorting-choice)))))
1591 (defcustom org-agenda-cmp-user-defined nil
1592 "A function to define the comparison `user-defined'.
1593 This function must receive two arguments, agenda entry a and b.
1594 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1595 the user comparison, return nil.
1596 When this is defined, you can make `user-defined-up' and `user-defined-down'
1597 part of an agenda sorting strategy."
1598 :group 'org-agenda-sorting
1599 :type 'symbol)
1601 (defcustom org-sort-agenda-notime-is-late t
1602 "Non-nil means items without time are considered late.
1603 This is only relevant for sorting. When t, items which have no explicit
1604 time like 15:30 will be considered as 99:01, i.e. later than any items which
1605 do have a time. When nil, the default time is before 0:00. You can use this
1606 option to decide if the schedule for today should come before or after timeless
1607 agenda entries."
1608 :group 'org-agenda-sorting
1609 :type 'boolean)
1611 (defcustom org-sort-agenda-noeffort-is-high t
1612 "Non-nil means items without effort estimate are sorted as high effort.
1613 This also applies when filtering an agenda view with respect to the
1614 < or > effort operator. Then, tasks with no effort defined will be treated
1615 as tasks with high effort.
1616 When nil, such items are sorted as 0 minutes effort."
1617 :group 'org-agenda-sorting
1618 :type 'boolean)
1620 (defgroup org-agenda-line-format nil
1621 "Options concerning the entry prefix in the Org agenda display."
1622 :tag "Org Agenda Line Format"
1623 :group 'org-agenda)
1625 (defcustom org-agenda-prefix-format
1626 '((agenda . " %i %-12:c%?-12t% s")
1627 (timeline . " % s")
1628 (todo . " %i %-12:c")
1629 (tags . " %i %-12:c")
1630 (search . " %i %-12:c"))
1631 "Format specifications for the prefix of items in the agenda views.
1632 An alist with five entries, each for the different agenda types. The
1633 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1634 The values are format strings.
1636 This format works similar to a printf format, with the following meaning:
1638 %c the category of the item, \"Diary\" for entries from the diary,
1639 or as given by the CATEGORY keyword or derived from the file name
1640 %e the effort required by the item
1641 %l the level of the item (insert X space(s) if item is of level X)
1642 %i the icon category of the item, see `org-agenda-category-icon-alist'
1643 %T the last tag of the item (ignore inherited tags, which come first)
1644 %t the HH:MM time-of-day specification if one applies to the entry
1645 %s Scheduling/Deadline information, a short string
1646 %b show breadcrumbs, i.e., the names of the higher levels
1647 %(expression) Eval EXPRESSION and replace the control string
1648 by the result
1650 All specifiers work basically like the standard `%s' of printf, but may
1651 contain two additional characters: a question mark just after the `%'
1652 and a whitespace/punctuation character just before the final letter.
1654 If the first character after `%' is a question mark, the entire field
1655 will only be included if the corresponding value applies to the current
1656 entry. This is useful for fields which should have fixed width when
1657 present, but zero width when absent. For example, \"%?-12t\" will
1658 result in a 12 character time field if a time of the day is specified,
1659 but will completely disappear in entries which do not contain a time.
1661 If there is punctuation or whitespace character just before the
1662 final format letter, this character will be appended to the field
1663 value if the value is not empty. For example, the format
1664 \"%-12:c\" leads to \"Diary: \" if the category is \"Diary\". If
1665 the category is empty, no additional colon is inserted.
1667 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1668 which means:
1670 - Indent the line with two space characters
1671 - Give the category a 12 chars wide field, padded with whitespace on
1672 the right (because of `-'). Append a colon if there is a category
1673 (because of `:').
1674 - If there is a time-of-day, put it into a 12 chars wide field. If no
1675 time, don't put in an empty field, just skip it (because of '?').
1676 - Finally, put the scheduling information.
1678 See also the variables `org-agenda-remove-times-when-in-prefix' and
1679 `org-agenda-remove-tags'.
1681 Custom commands can set this variable in the options section."
1682 :type '(choice
1683 (string :tag "General format")
1684 (list :greedy t :tag "View dependent"
1685 (cons (const agenda) (string :tag "Format"))
1686 (cons (const timeline) (string :tag "Format"))
1687 (cons (const todo) (string :tag "Format"))
1688 (cons (const tags) (string :tag "Format"))
1689 (cons (const search) (string :tag "Format"))))
1690 :group 'org-agenda-line-format)
1692 (defvar org-prefix-format-compiled nil
1693 "The compiled prefix format and associated variables.
1694 This is a list where first element is a list of variable bindings, and second
1695 element is the compiled format expression. See the variable
1696 `org-agenda-prefix-format'.")
1698 (defcustom org-agenda-todo-keyword-format "%-1s"
1699 "Format for the TODO keyword in agenda lines.
1700 Set this to something like \"%-12s\" if you want all TODO keywords
1701 to occupy a fixed space in the agenda display."
1702 :group 'org-agenda-line-format
1703 :type 'string)
1705 (defcustom org-agenda-diary-sexp-prefix nil
1706 "A regexp that matches part of a diary sexp entry
1707 which should be treated as scheduling/deadline information in
1708 `org-agenda'.
1710 For example, you can use this to extract the `diary-remind-message' from
1711 `diary-remind' entries."
1712 :group 'org-agenda-line-format
1713 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1715 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1716 "Text preceding timerange entries in the agenda view.
1717 This is a list with two strings. The first applies when the range
1718 is entirely on one day. The second applies if the range spans several days.
1719 The strings may have two \"%d\" format specifiers which will be filled
1720 with the sequence number of the days, and the total number of days in the
1721 range, respectively."
1722 :group 'org-agenda-line-format
1723 :type '(list
1724 (string :tag "Deadline today ")
1725 (choice :tag "Deadline relative"
1726 (string :tag "Format string")
1727 (function))))
1729 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1730 "Text preceding scheduled items in the agenda view.
1731 This is a list with two strings. The first applies when the item is
1732 scheduled on the current day. The second applies when it has been scheduled
1733 previously, it may contain a %d indicating that this is the nth time that
1734 this item is scheduled, due to automatic rescheduling of unfinished items
1735 for the following day. So this number is one larger than the number of days
1736 that passed since this item was scheduled first."
1737 :group 'org-agenda-line-format
1738 :version "24.4"
1739 :package-version '(Org . "8.0")
1740 :type '(list
1741 (string :tag "Scheduled today ")
1742 (string :tag "Scheduled previously")))
1744 (defcustom org-agenda-inactive-leader "["
1745 "Text preceding item pulled into the agenda by inactive time stamps.
1746 These entries are added to the agenda when pressing \"[\"."
1747 :group 'org-agenda-line-format
1748 :version "24.1"
1749 :type 'string)
1751 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: " "%2d d. ago: ")
1752 "Text preceding deadline items in the agenda view.
1753 This is a list with three strings. The first applies when the item has its
1754 deadline on the current day. The second applies when the deadline is in the
1755 future, the third one when it is in the past. The strings may contain %d
1756 to capture the number of days."
1757 :group 'org-agenda-line-format
1758 :version "24.4"
1759 :package-version '(Org . "8.0")
1760 :type '(list
1761 (string :tag "Deadline today ")
1762 (string :tag "Deadline in the future ")
1763 (string :tag "Deadline in the past ")))
1765 (defcustom org-agenda-remove-times-when-in-prefix t
1766 "Non-nil means remove duplicate time specifications in agenda items.
1767 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1768 time-of-day specification in a headline or diary entry is extracted and
1769 placed into the prefix. If this option is non-nil, the original specification
1770 \(a timestamp or -range, or just a plain time(range) specification like
1771 11:30-4pm) will be removed for agenda display. This makes the agenda less
1772 cluttered.
1773 The option can be t or nil. It may also be the symbol `beg', indicating
1774 that the time should only be removed when it is located at the beginning of
1775 the headline/diary entry."
1776 :group 'org-agenda-line-format
1777 :type '(choice
1778 (const :tag "Always" t)
1779 (const :tag "Never" nil)
1780 (const :tag "When at beginning of entry" beg)))
1782 (defcustom org-agenda-remove-timeranges-from-blocks nil
1783 "Non-nil means remove time ranges specifications in agenda
1784 items that span on several days."
1785 :group 'org-agenda-line-format
1786 :version "24.1"
1787 :type 'boolean)
1789 (defcustom org-agenda-default-appointment-duration nil
1790 "Default duration for appointments that only have a starting time.
1791 When nil, no duration is specified in such cases.
1792 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1793 :group 'org-agenda-line-format
1794 :type '(choice
1795 (integer :tag "Minutes")
1796 (const :tag "No default duration")))
1798 (defcustom org-agenda-show-inherited-tags t
1799 "Non-nil means show inherited tags in each agenda line.
1801 When this option is set to `always', it take precedences over
1802 `org-agenda-use-tag-inheritance' and inherited tags are shown
1803 in every agenda.
1805 When this option is set to t (the default), inherited tags are
1806 shown when they are available, i.e. when the value of
1807 `org-agenda-use-tag-inheritance' enables tag inheritance for the
1808 given agenda type.
1810 This can be set to a list of agenda types in which the agenda
1811 must display the inherited tags. Available types are `todo',
1812 `agenda', `search' and `timeline'.
1814 When set to nil, never show inherited tags in agenda lines."
1815 :group 'org-agenda-line-format
1816 :group 'org-agenda
1817 :version "24.3"
1818 :type '(choice
1819 (const :tag "Show inherited tags when available" t)
1820 (const :tag "Always show inherited tags" always)
1821 (repeat :tag "Show inherited tags only in selected agenda types"
1822 (symbol :tag "Agenda type"))))
1824 (defcustom org-agenda-use-tag-inheritance '(todo search timeline agenda)
1825 "List of agenda view types where to use tag inheritance.
1827 In tags/tags-todo/tags-tree agenda views, tag inheritance is
1828 controlled by `org-use-tag-inheritance'. In other agenda types,
1829 `org-use-tag-inheritance' is not used for the selection of the
1830 agenda entries. Still, you may want the agenda to be aware of
1831 the inherited tags anyway, e.g. for later tag filtering.
1833 Allowed value are `todo', `search', `timeline' and `agenda'.
1835 This variable has no effect if `org-agenda-show-inherited-tags'
1836 is set to `always'. In that case, the agenda is aware of those
1837 tags.
1839 The default value sets tags in every agenda type. Setting this
1840 option to nil will speed up non-tags agenda view a lot."
1841 :group 'org-agenda
1842 :version "24.3"
1843 :type '(choice
1844 (const :tag "Use tag inheritance in all agenda types" t)
1845 (repeat :tag "Use tag inheritance in selected agenda types"
1846 (symbol :tag "Agenda type"))))
1848 (defcustom org-agenda-hide-tags-regexp nil
1849 "Regular expression used to filter away specific tags in agenda views.
1850 This means that these tags will be present, but not be shown in the agenda
1851 line. Secondary filtering will still work on the hidden tags.
1852 Nil means don't hide any tags."
1853 :group 'org-agenda-line-format
1854 :type '(choice
1855 (const :tag "Hide none" nil)
1856 (string :tag "Regexp ")))
1858 (defcustom org-agenda-remove-tags nil
1859 "Non-nil means remove the tags from the headline copy in the agenda.
1860 When this is the symbol `prefix', only remove tags when
1861 `org-agenda-prefix-format' contains a `%T' specifier."
1862 :group 'org-agenda-line-format
1863 :type '(choice
1864 (const :tag "Always" t)
1865 (const :tag "Never" nil)
1866 (const :tag "When prefix format contains %T" prefix)))
1868 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1869 'org-agenda-remove-tags)
1871 (defcustom org-agenda-tags-column -80
1872 "Shift tags in agenda items to this column.
1873 If this number is positive, it specifies the column. If it is negative,
1874 it means that the tags should be flushright to that column. For example,
1875 -80 works well for a normal 80 character screen."
1876 :group 'org-agenda-line-format
1877 :type 'integer)
1879 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column)
1881 (defcustom org-agenda-fontify-priorities 'cookies
1882 "Non-nil means highlight low and high priorities in agenda.
1883 When t, the highest priority entries are bold, lowest priority italic.
1884 However, settings in `org-priority-faces' will overrule these faces.
1885 When this variable is the symbol `cookies', only fontify the
1886 cookies, not the entire task.
1887 This may also be an association list of priority faces, whose
1888 keys are the character values of `org-highest-priority',
1889 `org-default-priority', and `org-lowest-priority' (the default values
1890 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1891 color as a string, or a list like `(:background \"Red\")'.
1892 If it is a color, the variable `org-faces-easy-properties'
1893 determines if it is a foreground or a background color."
1894 :group 'org-agenda-line-format
1895 :type '(choice
1896 (const :tag "Never" nil)
1897 (const :tag "Defaults" t)
1898 (const :tag "Cookies only" cookies)
1899 (repeat :tag "Specify"
1900 (list (character :tag "Priority" :value ?A)
1901 (choice :tag "Face "
1902 (string :tag "Color")
1903 (sexp :tag "Face"))))))
1905 (defcustom org-agenda-day-face-function nil
1906 "Function called to determine what face should be used to display a day.
1907 The only argument passed to that function is the day. It should
1908 returns a face, or nil if does not want to specify a face and let
1909 the normal rules apply."
1910 :group 'org-agenda-line-format
1911 :version "24.1"
1912 :type '(choice (const nil) (function)))
1914 (defcustom org-agenda-category-icon-alist nil
1915 "Alist of category icon to be displayed in agenda views.
1917 Each entry should have the following format:
1919 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1921 Where CATEGORY-REGEXP is a regexp matching the categories where
1922 the icon should be displayed.
1923 FILE-OR-DATA either a file path or a string containing image data.
1925 The other fields can be omitted safely if not needed:
1926 TYPE indicates the image type.
1927 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1928 image data.
1929 PROPS are additional image attributes to assign to the image,
1930 like, e.g. `:ascent center'.
1932 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1934 If you want to set the display properties yourself, just put a
1935 list as second element:
1937 (CATEGORY-REGEXP (MY PROPERTY LIST))
1939 For example, to display a 16px horizontal space for Emacs
1940 category, you can use:
1942 (\"Emacs\" \\='(space . (:width (16))))"
1943 :group 'org-agenda-line-format
1944 :version "24.1"
1945 :type '(alist :key-type (string :tag "Regexp matching category")
1946 :value-type (choice (list :tag "Icon"
1947 (string :tag "File or data")
1948 (symbol :tag "Type")
1949 (boolean :tag "Data?")
1950 (repeat :tag "Extra image properties" :inline t symbol))
1951 (list :tag "Display properties" sexp))))
1953 (defgroup org-agenda-column-view nil
1954 "Options concerning column view in the agenda."
1955 :tag "Org Agenda Column View"
1956 :group 'org-agenda)
1958 (defcustom org-agenda-view-columns-initially nil
1959 "When non-nil, switch to columns view right after creating the agenda."
1960 :group 'org-agenda-column-view
1961 :type 'boolean
1962 :version "26.1"
1963 :package-version '(Org . "9.0")
1964 :safe #'booleanp)
1966 (defcustom org-agenda-columns-show-summaries t
1967 "Non-nil means show summaries for columns displayed in the agenda view."
1968 :group 'org-agenda-column-view
1969 :type 'boolean)
1971 (defcustom org-agenda-columns-compute-summary-properties t
1972 "Non-nil means recompute all summary properties before column view.
1973 When column view in the agenda is listing properties that have a summary
1974 operator, it can go to all relevant buffers and recompute the summaries
1975 there. This can mean overhead for the agenda column view, but is necessary
1976 to have thing up to date.
1977 As a special case, a CLOCKSUM property also makes sure that the clock
1978 computations are current."
1979 :group 'org-agenda-column-view
1980 :type 'boolean)
1982 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1983 "Non-nil means the duration of an appointment will add to day effort.
1984 The property to which appointment durations will be added is the one given
1985 in the option `org-effort-property'. If an appointment does not have
1986 an end time, `org-agenda-default-appointment-duration' will be used. If that
1987 is not set, an appointment without end time will not contribute to the time
1988 estimate."
1989 :group 'org-agenda-column-view
1990 :type 'boolean)
1992 (defcustom org-agenda-auto-exclude-function nil
1993 "A function called with a tag to decide if it is filtered on \
1994 \\<org-agenda-mode-map>`\\[org-agenda-filter-by-tag] RET'.
1995 The sole argument to the function, which is called once for each
1996 possible tag, is a string giving the name of the tag. The
1997 function should return either nil if the tag should be included
1998 as normal, or \"-<TAG>\" to exclude the tag.
1999 Note that for the purpose of tag filtering, only the lower-case version of
2000 all tags will be considered, so that this function will only ever see
2001 the lower-case version of all tags."
2002 :group 'org-agenda
2003 :type '(choice (const nil) (function)))
2005 (defcustom org-agenda-bulk-custom-functions nil
2006 "Alist of characters and custom functions for bulk actions.
2007 For example, this value makes those two functions available:
2009 \\='((?R set-category)
2010 (?C bulk-cut))
2012 With selected entries in an agenda buffer, `B R' will call
2013 the custom function `set-category' on the selected entries.
2014 Note that functions in this alist don't need to be quoted."
2015 :type '(alist :key-type character :value-type (group function))
2016 :version "24.1"
2017 :group 'org-agenda)
2019 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
2020 "Execute BODY with point at location given by `org-hd-marker' property.
2021 If STRING is non-nil, the text property will be fetched from position 0
2022 in that string. If STRING is nil, it will be fetched from the beginning
2023 of the current line."
2024 (org-with-gensyms (marker)
2025 `(let ((,marker (get-text-property (if ,string 0 (point-at-bol))
2026 'org-hd-marker ,string)))
2027 (with-current-buffer (marker-buffer ,marker)
2028 (save-excursion
2029 (goto-char ,marker)
2030 ,@body)))))
2031 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
2033 (defun org-add-agenda-custom-command (entry)
2034 "Replace or add a command in `org-agenda-custom-commands'.
2035 This is mostly for hacking and trying a new command - once the command
2036 works you probably want to add it to `org-agenda-custom-commands' for good."
2037 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
2038 (if ass
2039 (setcdr ass (cdr entry))
2040 (push entry org-agenda-custom-commands))))
2042 ;;; Define the org-agenda-mode
2044 (defvar org-agenda-mode-map (make-sparse-keymap)
2045 "Keymap for `org-agenda-mode'.")
2046 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map)
2048 (defvar org-agenda-menu) ; defined later in this file.
2049 (defvar org-agenda-restrict nil) ; defined later in this file.
2050 (defvar org-agenda-follow-mode nil)
2051 (defvar org-agenda-entry-text-mode nil)
2052 (defvar org-agenda-clockreport-mode nil)
2053 (defvar org-agenda-show-log nil)
2054 (defvar org-agenda-redo-command nil)
2055 (defvar org-agenda-query-string nil)
2056 (defvar org-agenda-mode-hook nil
2057 "Hook run after `org-agenda-mode' is turned on.
2058 The buffer is still writable when this hook is called.")
2059 (defvar org-agenda-type nil)
2060 (defvar org-agenda-force-single-file nil)
2061 (defvar org-agenda-bulk-marked-entries nil
2062 "List of markers that refer to marked entries in the agenda.")
2063 (defvar org-agenda-current-date nil
2064 "Active date when building the agenda.")
2066 ;;; Multiple agenda buffers support
2068 (defcustom org-agenda-sticky nil
2069 "Non-nil means agenda q key will bury agenda buffers.
2070 Agenda commands will then show existing buffer instead of generating new ones.
2071 When nil, `q' will kill the single agenda buffer."
2072 :group 'org-agenda
2073 :version "24.3"
2074 :type 'boolean)
2077 ;;;###autoload
2078 (defun org-toggle-sticky-agenda (&optional arg)
2079 "Toggle `org-agenda-sticky'."
2080 (interactive "P")
2081 (let ((new-value (if arg
2082 (> (prefix-numeric-value arg) 0)
2083 (not org-agenda-sticky))))
2084 (if (equal new-value org-agenda-sticky)
2085 (and (called-interactively-p 'interactive)
2086 (message "Sticky agenda was already %s"
2087 (if org-agenda-sticky "enabled" "disabled")))
2088 (setq org-agenda-sticky new-value)
2089 (org-agenda-kill-all-agenda-buffers)
2090 (and (called-interactively-p 'interactive)
2091 (message "Sticky agenda %s"
2092 (if org-agenda-sticky "enabled" "disabled"))))))
2094 (defvar org-agenda-buffer nil
2095 "Agenda buffer currently being generated.")
2097 (defvar org-agenda-last-prefix-arg nil)
2098 (defvar org-agenda-this-buffer-name nil)
2099 (defvar org-agenda-doing-sticky-redo nil)
2100 (defvar org-agenda-this-buffer-is-sticky nil)
2101 (defvar org-agenda-last-indirect-buffer nil
2102 "Last buffer loaded by `org-agenda-tree-to-indirect-buffer'.")
2104 (defconst org-agenda-local-vars
2105 '(org-agenda-this-buffer-name
2106 org-agenda-undo-list
2107 org-agenda-pending-undo-list
2108 org-agenda-follow-mode
2109 org-agenda-entry-text-mode
2110 org-agenda-clockreport-mode
2111 org-agenda-show-log
2112 org-agenda-redo-command
2113 org-agenda-query-string
2114 org-agenda-type
2115 org-agenda-bulk-marked-entries
2116 org-agenda-undo-has-started-in
2117 org-agenda-info
2118 org-agenda-pre-window-conf
2119 org-agenda-columns-active
2120 org-agenda-tag-filter
2121 org-agenda-category-filter
2122 org-agenda-top-headline-filter
2123 org-agenda-regexp-filter
2124 org-agenda-effort-filter
2125 org-agenda-markers
2126 org-agenda-last-search-view-search-was-boolean
2127 org-agenda-last-indirect-buffer
2128 org-agenda-filtered-by-category
2129 org-agenda-filter-form
2130 org-agenda-cycle-counter
2131 org-agenda-last-prefix-arg)
2132 "Variables that must be local in agenda buffers to allow multiple buffers.")
2134 (defun org-agenda-mode ()
2135 "Mode for time-sorted view on action items in Org files.
2137 The following commands are available:
2139 \\{org-agenda-mode-map}"
2140 (interactive)
2141 (cond (org-agenda-doing-sticky-redo
2142 ;; Refreshing sticky agenda-buffer
2144 ;; Preserve the value of `org-agenda-local-vars' variables,
2145 ;; while letting `kill-all-local-variables' kill the rest
2146 (let ((save (buffer-local-variables)))
2147 (kill-all-local-variables)
2148 (mapc 'make-local-variable org-agenda-local-vars)
2149 (dolist (elem save)
2150 (let ((var (car elem))
2151 (val (cdr elem)))
2152 (when (and val
2153 (member var org-agenda-local-vars))
2154 (set var val)))))
2155 (setq-local org-agenda-this-buffer-is-sticky t))
2156 (org-agenda-sticky
2157 ;; Creating a sticky Agenda buffer for the first time
2158 (kill-all-local-variables)
2159 (mapc 'make-local-variable org-agenda-local-vars)
2160 (setq-local org-agenda-this-buffer-is-sticky t))
2162 ;; Creating a non-sticky agenda buffer
2163 (kill-all-local-variables)
2164 (setq-local org-agenda-this-buffer-is-sticky nil)))
2165 (setq org-agenda-undo-list nil
2166 org-agenda-pending-undo-list nil
2167 org-agenda-bulk-marked-entries nil)
2168 (setq major-mode 'org-agenda-mode)
2169 ;; Keep global-font-lock-mode from turning on font-lock-mode
2170 (setq-local font-lock-global-modes (list 'not major-mode))
2171 (setq mode-name "Org-Agenda")
2172 (setq indent-tabs-mode nil)
2173 (use-local-map org-agenda-mode-map)
2174 (easy-menu-add org-agenda-menu)
2175 (if org-startup-truncated (setq truncate-lines t))
2176 (setq-local line-move-visual nil)
2177 (add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
2178 (add-hook 'pre-command-hook 'org-unhighlight nil 'local)
2179 ;; Make sure properties are removed when copying text
2180 (add-hook 'filter-buffer-substring-functions
2181 (lambda (fun start end delete)
2182 (substring-no-properties (funcall fun start end delete)))
2183 nil t)
2184 (unless org-agenda-keep-modes
2185 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
2186 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode))
2187 (setq org-agenda-show-log org-agenda-start-with-log-mode)
2188 (setq org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode)
2189 (add-to-invisibility-spec '(org-filtered))
2190 (add-to-invisibility-spec '(org-link))
2191 (easy-menu-change
2192 '("Agenda") "Agenda Files"
2193 (append
2194 (list
2195 (vector
2196 (if (get 'org-agenda-files 'org-restrict)
2197 "Restricted to single file"
2198 "Edit File List")
2199 '(org-edit-agenda-file-list)
2200 (not (get 'org-agenda-files 'org-restrict)))
2201 "--")
2202 (mapcar 'org-file-menu-entry (org-agenda-files))))
2203 (org-agenda-set-mode-name)
2204 (apply
2205 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2206 (list 'org-agenda-mode-hook)))
2208 (substitute-key-definition 'undo 'org-agenda-undo
2209 org-agenda-mode-map global-map)
2210 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2211 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2212 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2213 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2214 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2215 (org-defkey org-agenda-mode-map [(meta down)] 'org-agenda-drag-line-forward)
2216 (org-defkey org-agenda-mode-map [(meta up)] 'org-agenda-drag-line-backward)
2217 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2218 (org-defkey org-agenda-mode-map "\M-m" 'org-agenda-bulk-toggle)
2219 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2220 (org-defkey org-agenda-mode-map "\M-*" 'org-agenda-bulk-toggle-all)
2221 (org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
2222 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2223 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2224 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2225 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2226 (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2227 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2228 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2229 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2230 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2231 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2232 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2233 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2234 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2235 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2236 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2237 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2238 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2239 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2240 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2241 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2242 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2243 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2244 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2245 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2246 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2247 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2248 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2249 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2250 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2251 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2252 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2253 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2254 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2255 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2256 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2257 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2258 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2259 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2261 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2262 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2263 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2264 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2265 (while l (org-defkey org-agenda-mode-map
2266 (int-to-string (pop l)) 'digit-argument)))
2268 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2269 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2270 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2271 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2272 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2273 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2274 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2275 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2276 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2277 (org-defkey org-agenda-mode-map "g" (lambda () (interactive) (org-agenda-redo t)))
2278 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2279 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2280 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2281 'org-clock-modify-effort-estimate)
2282 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2283 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2284 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2285 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2286 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2287 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2288 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2289 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2290 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2291 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2292 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2293 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2294 (substitute-key-definition 'next-line 'org-agenda-next-line
2295 org-agenda-mode-map global-map)
2296 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2297 org-agenda-mode-map global-map)
2298 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2299 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2300 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2301 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2302 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2303 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2304 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2305 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2306 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2307 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2308 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2309 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2310 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2311 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2312 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2313 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2314 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2315 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2316 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2317 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2318 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2319 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2320 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2321 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2322 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2323 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2324 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2325 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2326 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2327 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2328 (org-defkey org-agenda-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock-from-agenda)
2330 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2331 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2332 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2333 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2334 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2335 (org-defkey org-agenda-mode-map "_" 'org-agenda-filter-by-effort)
2336 (org-defkey org-agenda-mode-map "=" 'org-agenda-filter-by-regexp)
2337 (org-defkey org-agenda-mode-map "|" 'org-agenda-filter-remove-all)
2338 (org-defkey org-agenda-mode-map "~" 'org-agenda-limit-interactively)
2339 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2340 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-headline)
2341 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2342 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2343 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2344 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2346 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2347 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2349 (define-key org-agenda-mode-map [remap forward-paragraph] 'org-agenda-forward-block)
2350 (define-key org-agenda-mode-map [remap backward-paragraph] 'org-agenda-backward-block)
2352 (when org-agenda-mouse-1-follows-link
2353 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2354 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2355 '("Agenda"
2356 ("Agenda Files")
2357 "--"
2358 ("Agenda Dates"
2359 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2360 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2361 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2362 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2363 "--"
2364 ("View"
2365 ["Day View" org-agenda-day-view
2366 :active (org-agenda-check-type nil 'agenda)
2367 :style radio :selected (eq org-agenda-current-span 'day)
2368 :keys "v d (or just d)"]
2369 ["Week View" org-agenda-week-view
2370 :active (org-agenda-check-type nil 'agenda)
2371 :style radio :selected (eq org-agenda-current-span 'week)
2372 :keys "v w"]
2373 ["Fortnight View" org-agenda-fortnight-view
2374 :active (org-agenda-check-type nil 'agenda)
2375 :style radio :selected (eq org-agenda-current-span 'fortnight)
2376 :keys "v f"]
2377 ["Month View" org-agenda-month-view
2378 :active (org-agenda-check-type nil 'agenda)
2379 :style radio :selected (eq org-agenda-current-span 'month)
2380 :keys "v m"]
2381 ["Year View" org-agenda-year-view
2382 :active (org-agenda-check-type nil 'agenda)
2383 :style radio :selected (eq org-agenda-current-span 'year)
2384 :keys "v y"]
2385 "--"
2386 ["Include Diary" org-agenda-toggle-diary
2387 :style toggle :selected org-agenda-include-diary
2388 :active (org-agenda-check-type nil 'agenda)]
2389 ["Include Deadlines" org-agenda-toggle-deadlines
2390 :style toggle :selected org-agenda-include-deadlines
2391 :active (org-agenda-check-type nil 'agenda)]
2392 ["Use Time Grid" org-agenda-toggle-time-grid
2393 :style toggle :selected org-agenda-use-time-grid
2394 :active (org-agenda-check-type nil 'agenda)]
2395 "--"
2396 ["Show clock report" org-agenda-clockreport-mode
2397 :style toggle :selected org-agenda-clockreport-mode
2398 :active (org-agenda-check-type nil 'agenda)]
2399 ["Show some entry text" org-agenda-entry-text-mode
2400 :style toggle :selected org-agenda-entry-text-mode
2401 :active t]
2402 "--"
2403 ["Show Logbook entries" org-agenda-log-mode
2404 :style toggle :selected org-agenda-show-log
2405 :active (org-agenda-check-type nil 'agenda 'timeline)
2406 :keys "v l (or just l)"]
2407 ["Include archived trees" org-agenda-archives-mode
2408 :style toggle :selected org-agenda-archives-mode :active t
2409 :keys "v a"]
2410 ["Include archive files" (org-agenda-archives-mode t)
2411 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2412 :keys "v A"]
2413 "--"
2414 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2415 ["Write view to file" org-agenda-write t]
2416 ["Rebuild buffer" org-agenda-redo t]
2417 ["Save all Org buffers" org-save-all-org-buffers t]
2418 "--"
2419 ["Show original entry" org-agenda-show t]
2420 ["Go To (other window)" org-agenda-goto t]
2421 ["Go To (this window)" org-agenda-switch-to t]
2422 ["Capture with cursor date" org-agenda-capture t]
2423 ["Follow Mode" org-agenda-follow-mode
2424 :style toggle :selected org-agenda-follow-mode :active t]
2425 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2426 "--"
2427 ("TODO"
2428 ["Cycle TODO" org-agenda-todo t]
2429 ["Next TODO set" org-agenda-todo-nextset t]
2430 ["Previous TODO set" org-agenda-todo-previousset t]
2431 ["Add note" org-agenda-add-note t])
2432 ("Archive/Refile/Delete"
2433 ["Archive default" org-agenda-archive-default t]
2434 ["Archive default" org-agenda-archive-default-with-confirmation t]
2435 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2436 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2437 ["Archive subtree" org-agenda-archive t]
2438 "--"
2439 ["Refile" org-agenda-refile t]
2440 "--"
2441 ["Delete subtree" org-agenda-kill t])
2442 ("Bulk action"
2443 ["Mark entry" org-agenda-bulk-mark t]
2444 ["Mark all" org-agenda-bulk-mark-all t]
2445 ["Unmark entry" org-agenda-bulk-unmark t]
2446 ["Unmark all" org-agenda-bulk-unmark-all :active t :keys "U"]
2447 ["Toggle mark" org-agenda-bulk-toggle t]
2448 ["Toggle all" org-agenda-bulk-toggle-all t]
2449 ["Mark regexp" org-agenda-bulk-mark-regexp t])
2450 ["Act on all marked" org-agenda-bulk-action t]
2451 "--"
2452 ("Tags and Properties"
2453 ["Show all Tags" org-agenda-show-tags t]
2454 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2455 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2456 "--"
2457 ["Column View" org-columns t])
2458 ("Deadline/Schedule"
2459 ["Schedule" org-agenda-schedule t]
2460 ["Set Deadline" org-agenda-deadline t]
2461 "--"
2462 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2463 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2464 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2465 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2466 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2467 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2468 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2469 ("Clock and Effort"
2470 ["Clock in" org-agenda-clock-in t]
2471 ["Clock out" org-agenda-clock-out t]
2472 ["Clock cancel" org-agenda-clock-cancel t]
2473 ["Goto running clock" org-clock-goto t]
2474 "--"
2475 ["Set Effort" org-agenda-set-effort t]
2476 ["Change clocked effort" org-clock-modify-effort-estimate
2477 (org-clock-is-active)])
2478 ("Priority"
2479 ["Set Priority" org-agenda-priority t]
2480 ["Increase Priority" org-agenda-priority-up t]
2481 ["Decrease Priority" org-agenda-priority-down t]
2482 ["Show Priority" org-show-priority t])
2483 ("Calendar/Diary"
2484 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2485 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2486 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2487 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2488 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2489 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2490 "--"
2491 ["Create iCalendar File" org-icalendar-combine-agenda-files t])
2492 "--"
2493 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2494 "--"
2495 ("MobileOrg"
2496 ["Push Files and Views" org-mobile-push t]
2497 ["Get Captured and Flagged" org-mobile-pull t]
2498 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2499 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2500 "--"
2501 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2502 "--"
2503 ["Quit" org-agenda-quit t]
2504 ["Exit and Release Buffers" org-agenda-exit t]
2507 ;;; Agenda undo
2509 (defvar org-agenda-allow-remote-undo t
2510 "Non-nil means allow remote undo from the agenda buffer.")
2511 (defvar org-agenda-undo-has-started-in nil
2512 "Buffers that have already seen `undo-start' in the current undo sequence.")
2514 (defun org-agenda-undo ()
2515 "Undo a remote editing step in the agenda.
2516 This undoes changes both in the agenda buffer and in the remote buffer
2517 that have been changed along."
2518 (interactive)
2519 (or org-agenda-allow-remote-undo
2520 (user-error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2521 (if (not (eq this-command last-command))
2522 (setq org-agenda-undo-has-started-in nil
2523 org-agenda-pending-undo-list org-agenda-undo-list))
2524 (if (not org-agenda-pending-undo-list)
2525 (user-error "No further undo information"))
2526 (let* ((entry (pop org-agenda-pending-undo-list))
2527 buf line cmd rembuf)
2528 (setq cmd (pop entry) line (pop entry))
2529 (setq rembuf (nth 2 entry))
2530 (org-with-remote-undo rembuf
2531 (while (bufferp (setq buf (pop entry)))
2532 (if (pop entry)
2533 (with-current-buffer buf
2534 (let ((last-undo-buffer buf)
2535 (inhibit-read-only t))
2536 (unless (memq buf org-agenda-undo-has-started-in)
2537 (push buf org-agenda-undo-has-started-in)
2538 (make-local-variable 'pending-undo-list)
2539 (undo-start))
2540 (while (and pending-undo-list
2541 (listp pending-undo-list)
2542 (not (car pending-undo-list)))
2543 (pop pending-undo-list))
2544 (undo-more 1))))))
2545 (org-goto-line line)
2546 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2548 (defun org-verify-change-for-undo (l1 l2)
2549 "Verify that a real change occurred between the undo lists L1 and L2."
2550 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2551 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2552 (not (eq l1 l2)))
2554 ;;; Agenda dispatch
2556 (defvar org-agenda-restrict-begin (make-marker))
2557 (defvar org-agenda-restrict-end (make-marker))
2558 (defvar org-agenda-last-dispatch-buffer nil)
2559 (defvar org-agenda-overriding-restriction nil)
2561 (defcustom org-agenda-custom-commands-contexts nil
2562 "Alist of custom agenda keys and contextual rules.
2564 For example, if you have a custom agenda command \"p\" and you
2565 want this command to be accessible only from plain text files,
2566 use this:
2568 \\='((\"p\" ((in-file . \"\\\\.txt\\\\'\"))))
2570 Here are the available contexts definitions:
2572 in-file: command displayed only in matching files
2573 in-mode: command displayed only in matching modes
2574 not-in-file: command not displayed in matching files
2575 not-in-mode: command not displayed in matching modes
2576 in-buffer: command displayed only in matching buffers
2577 not-in-buffer: command not displayed in matching buffers
2578 [function]: a custom function taking no argument
2580 If you define several checks, the agenda command will be
2581 accessible if there is at least one valid check.
2583 You can also bind a key to another agenda custom command
2584 depending on contextual rules.
2586 \\='((\"p\" \"q\" ((in-file . \"\\\\.txt\\\\'\"))))
2588 Here it means: in .txt files, use \"p\" as the key for the
2589 agenda command otherwise associated with \"q\". (The command
2590 originally associated with \"q\" is not displayed to avoid
2591 duplicates.)"
2592 :version "24.3"
2593 :group 'org-agenda-custom-commands
2594 :type '(repeat (list :tag "Rule"
2595 (string :tag " Agenda key")
2596 (string :tag "Replace by command")
2597 (repeat :tag "Available when"
2598 (choice
2599 (cons :tag "Condition"
2600 (choice
2601 (const :tag "In file" in-file)
2602 (const :tag "Not in file" not-in-file)
2603 (const :tag "In buffer" in-buffer)
2604 (const :tag "Not in buffer" not-in-buffer)
2605 (const :tag "In mode" in-mode)
2606 (const :tag "Not in mode" not-in-mode))
2607 (regexp))
2608 (function :tag "Custom function"))))))
2610 (defcustom org-agenda-max-entries nil
2611 "Maximum number of entries to display in an agenda.
2612 This can be nil (no limit) or an integer or an alist of agenda
2613 types with an associated number of entries to display in this
2614 type."
2615 :version "24.4"
2616 :package-version '(Org . "8.0")
2617 :group 'org-agenda-custom-commands
2618 :type '(choice (symbol :tag "No limit" nil)
2619 (integer :tag "Max number of entries")
2620 (repeat
2621 (cons (choice :tag "Agenda type"
2622 (const agenda)
2623 (const todo)
2624 (const tags)
2625 (const search)
2626 (const timeline))
2627 (integer :tag "Max number of entries")))))
2629 (defcustom org-agenda-max-todos nil
2630 "Maximum number of TODOs to display in an agenda.
2631 This can be nil (no limit) or an integer or an alist of agenda
2632 types with an associated number of entries to display in this
2633 type."
2634 :version "24.4"
2635 :package-version '(Org . "8.0")
2636 :group 'org-agenda-custom-commands
2637 :type '(choice (symbol :tag "No limit" nil)
2638 (integer :tag "Max number of TODOs")
2639 (repeat
2640 (cons (choice :tag "Agenda type"
2641 (const agenda)
2642 (const todo)
2643 (const tags)
2644 (const search)
2645 (const timeline))
2646 (integer :tag "Max number of TODOs")))))
2648 (defcustom org-agenda-max-tags nil
2649 "Maximum number of tagged entries to display in an agenda.
2650 This can be nil (no limit) or an integer or an alist of agenda
2651 types with an associated number of entries to display in this
2652 type."
2653 :version "24.4"
2654 :package-version '(Org . "8.0")
2655 :group 'org-agenda-custom-commands
2656 :type '(choice (symbol :tag "No limit" nil)
2657 (integer :tag "Max number of tagged entries")
2658 (repeat
2659 (cons (choice :tag "Agenda type"
2660 (const agenda)
2661 (const todo)
2662 (const tags)
2663 (const search)
2664 (const timeline))
2665 (integer :tag "Max number of tagged entries")))))
2667 (defcustom org-agenda-max-effort nil
2668 "Maximum cumulated effort duration for the agenda.
2669 This can be nil (no limit) or a number of minutes (as an integer)
2670 or an alist of agenda types with an associated number of minutes
2671 to limit entries to in this type."
2672 :version "24.4"
2673 :package-version '(Org . "8.0")
2674 :group 'org-agenda-custom-commands
2675 :type '(choice (symbol :tag "No limit" nil)
2676 (integer :tag "Max number of minutes")
2677 (repeat
2678 (cons (choice :tag "Agenda type"
2679 (const agenda)
2680 (const todo)
2681 (const tags)
2682 (const search)
2683 (const timeline))
2684 (integer :tag "Max number of minutes")))))
2686 (defvar org-agenda-keep-restricted-file-list nil)
2687 (defvar org-keys nil)
2688 (defvar org-match nil)
2689 ;;;###autoload
2690 (defun org-agenda (&optional arg org-keys restriction)
2691 "Dispatch agenda commands to collect entries to the agenda buffer.
2692 Prompts for a command to execute. Any prefix arg will be passed
2693 on to the selected command. The default selections are:
2695 a Call `org-agenda-list' to display the agenda for current day or week.
2696 t Call `org-todo-list' to display the global todo list.
2697 T Call `org-todo-list' to display the global todo list, select only
2698 entries with a specific TODO keyword (the user gets a prompt).
2699 m Call `org-tags-view' to display headlines with tags matching
2700 a condition (the user is prompted for the condition).
2701 M Like `m', but select only TODO entries, no ordinary headlines.
2702 e Export views to associated files.
2703 s Search entries for keywords.
2704 S Search entries for keywords, only with TODO keywords.
2705 / Multi occur across all agenda files and also files listed
2706 in `org-agenda-text-search-extra-files'.
2707 < Restrict agenda commands to buffer, subtree, or region.
2708 Press several times to get the desired effect.
2709 > Remove a previous restriction.
2710 # List \"stuck\" projects.
2711 ! Configure what \"stuck\" means.
2712 C Configure custom agenda commands.
2714 More commands can be added by configuring the variable
2715 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2716 searches can be pre-defined in this way.
2718 If the current buffer is in Org mode and visiting a file, you can also
2719 first press `<' once to indicate that the agenda should be temporarily
2720 \(until the next use of `\\[org-agenda]') restricted to the current file.
2721 Pressing `<' twice means to restrict to the current subtree or region
2722 \(if active)."
2723 (interactive "P")
2724 (catch 'exit
2725 (let* ((prefix-descriptions nil)
2726 (org-agenda-buffer-name org-agenda-buffer-name)
2727 (org-agenda-window-setup (if (equal (buffer-name)
2728 org-agenda-buffer-name)
2729 'current-window
2730 org-agenda-window-setup))
2731 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2732 (org-agenda-custom-commands
2733 ;; normalize different versions
2734 (delq nil
2735 (mapcar
2736 (lambda (x)
2737 (cond ((stringp (cdr x))
2738 (push x prefix-descriptions)
2739 nil)
2740 ((stringp (nth 1 x)) x)
2741 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2742 (t (cons (car x) (cons "" (cdr x))))))
2743 org-agenda-custom-commands)))
2744 (org-agenda-custom-commands
2745 (org-contextualize-keys
2746 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2747 (buf (current-buffer))
2748 (bfn (buffer-file-name (buffer-base-buffer)))
2749 entry key type org-match lprops ans)
2750 ;; Turn off restriction unless there is an overriding one,
2751 (unless org-agenda-overriding-restriction
2752 (unless org-agenda-keep-restricted-file-list
2753 ;; There is a request to keep the file list in place
2754 (put 'org-agenda-files 'org-restrict nil))
2755 (setq org-agenda-restrict nil)
2756 (move-marker org-agenda-restrict-begin nil)
2757 (move-marker org-agenda-restrict-end nil))
2758 ;; Delete old local properties
2759 (put 'org-agenda-redo-command 'org-lprops nil)
2760 ;; Delete previously set last-arguments
2761 (put 'org-agenda-redo-command 'last-args nil)
2762 ;; Remember where this call originated
2763 (setq org-agenda-last-dispatch-buffer (current-buffer))
2764 (unless org-keys
2765 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2766 org-keys (car ans)
2767 restriction (cdr ans)))
2768 ;; If we have sticky agenda buffers, set a name for the buffer,
2769 ;; depending on the invoking keys. The user may still set this
2770 ;; as a command option, which will overwrite what we do here.
2771 (if org-agenda-sticky
2772 (setq org-agenda-buffer-name
2773 (format "*Org Agenda(%s)*" org-keys)))
2774 ;; Establish the restriction, if any
2775 (when (and (not org-agenda-overriding-restriction) restriction)
2776 (put 'org-agenda-files 'org-restrict (list bfn))
2777 (cond
2778 ((eq restriction 'region)
2779 (setq org-agenda-restrict (current-buffer))
2780 (move-marker org-agenda-restrict-begin (region-beginning))
2781 (move-marker org-agenda-restrict-end (region-end)))
2782 ((eq restriction 'subtree)
2783 (save-excursion
2784 (setq org-agenda-restrict (current-buffer))
2785 (org-back-to-heading t)
2786 (move-marker org-agenda-restrict-begin (point))
2787 (move-marker org-agenda-restrict-end
2788 (progn (org-end-of-subtree t)))))))
2790 ;; For example the todo list should not need it (but does...)
2791 (cond
2792 ((setq entry (assoc org-keys org-agenda-custom-commands))
2793 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2794 (progn
2795 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
2796 lprops (nth 4 entry))
2797 (if org-agenda-sticky
2798 (setq org-agenda-buffer-name
2799 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2800 (format "*Org Agenda(%s)*" org-keys))))
2801 (put 'org-agenda-redo-command 'org-lprops lprops)
2802 (cond
2803 ((eq type 'agenda)
2804 (org-let lprops '(org-agenda-list current-prefix-arg)))
2805 ((eq type 'agenda*)
2806 (org-let lprops '(org-agenda-list current-prefix-arg nil nil t)))
2807 ((eq type 'alltodo)
2808 (org-let lprops '(org-todo-list current-prefix-arg)))
2809 ((eq type 'search)
2810 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
2811 ((eq type 'stuck)
2812 (org-let lprops '(org-agenda-list-stuck-projects
2813 current-prefix-arg)))
2814 ((eq type 'tags)
2815 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2816 ((eq type 'tags-todo)
2817 (org-let lprops '(org-tags-view '(4) org-match)))
2818 ((eq type 'todo)
2819 (org-let lprops '(org-todo-list org-match)))
2820 ((eq type 'tags-tree)
2821 (org-check-for-org-mode)
2822 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
2823 ((eq type 'todo-tree)
2824 (org-check-for-org-mode)
2825 (org-let lprops
2826 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2827 (regexp-quote org-match) "\\>"))))
2828 ((eq type 'occur-tree)
2829 (org-check-for-org-mode)
2830 (org-let lprops '(org-occur org-match)))
2831 ((functionp type)
2832 (org-let lprops '(funcall type org-match)))
2833 ((fboundp type)
2834 (org-let lprops '(funcall type org-match)))
2835 (t (user-error "Invalid custom agenda command type %s" type))))
2836 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2837 ((equal org-keys "C")
2838 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2839 (customize-variable 'org-agenda-custom-commands))
2840 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2841 ((equal org-keys "s") (call-interactively 'org-search-view))
2842 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2843 ((equal org-keys "t") (call-interactively 'org-todo-list))
2844 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2845 ((equal org-keys "m") (call-interactively 'org-tags-view))
2846 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2847 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2848 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
2849 (add-hook
2850 'post-command-hook
2851 (lambda ()
2852 (unless (current-message)
2853 (let* ((m (org-agenda-get-any-marker))
2854 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2855 (when note
2856 (message (concat
2857 "FLAGGING-NOTE ([?] for more info): "
2858 (org-add-props
2859 (replace-regexp-in-string
2860 "\\\\n" "//"
2861 (copy-sequence note))
2862 nil 'face 'org-warning)))))))
2863 t t))
2864 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2865 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2866 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
2867 (t (user-error "Invalid agenda key"))))))
2869 (defvar org-agenda-multi)
2871 (defun org-agenda-append-agenda ()
2872 "Append another agenda view to the current one.
2873 This function allows interactive building of block agendas.
2874 Agenda views are separated by `org-agenda-block-separator'."
2875 (interactive)
2876 (unless (derived-mode-p 'org-agenda-mode)
2877 (user-error "Can only append from within agenda buffer"))
2878 (let ((org-agenda-multi t))
2879 (org-agenda)
2880 (widen)
2881 (org-agenda-finalize)
2882 (setq buffer-read-only t)
2883 (org-agenda-fit-window-to-buffer)))
2885 (defun org-agenda-normalize-custom-commands (cmds)
2886 "Normalize custom commands CMDS."
2887 (delq nil
2888 (mapcar
2889 (lambda (x)
2890 (cond ((stringp (cdr x)) nil)
2891 ((stringp (nth 1 x)) x)
2892 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2893 (t (cons (car x) (cons "" (cdr x))))))
2894 cmds)))
2896 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2897 "The user interface for selecting an agenda command."
2898 (catch 'exit
2899 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2900 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2901 (region-p (org-region-active-p))
2902 (custom org-agenda-custom-commands)
2903 (selstring "")
2904 restriction second-time
2905 c entry key type match prefixes rmheader header-end custom1 desc
2906 line lines left right n n1)
2907 (save-window-excursion
2908 (delete-other-windows)
2909 (org-switch-to-buffer-other-window " *Agenda Commands*")
2910 (erase-buffer)
2911 (insert (eval-when-compile
2912 (let ((header
2913 "Press key for an agenda command:
2914 -------------------------------- < Buffer, subtree/region restriction
2915 a Agenda for current week or day > Remove restriction
2916 t List of all TODO entries e Export agenda views
2917 m Match a TAGS/PROP/TODO query T Entries with special TODO kwd
2918 s Search for keywords M Like m, but only TODO entries
2919 / Multi-occur S Like s, but only TODO entries
2920 ? Find :FLAGGED: entries C Configure custom agenda commands
2921 * Toggle sticky agenda views # List stuck projects (!=configure)
2923 (start 0))
2924 (while (string-match
2925 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2926 header start)
2927 (setq start (match-end 0))
2928 (add-text-properties (match-beginning 2) (match-end 2)
2929 '(face bold) header))
2930 header)))
2931 (setq header-end (point-marker))
2932 (while t
2933 (setq custom1 custom)
2934 (when (eq rmheader t)
2935 (org-goto-line 1)
2936 (re-search-forward ":" nil t)
2937 (delete-region (match-end 0) (point-at-eol))
2938 (forward-char 1)
2939 (looking-at "-+")
2940 (delete-region (match-end 0) (point-at-eol))
2941 (move-marker header-end (match-end 0)))
2942 (goto-char header-end)
2943 (delete-region (point) (point-max))
2945 ;; Produce all the lines that describe custom commands and prefixes
2946 (setq lines nil)
2947 (while (setq entry (pop custom1))
2948 (setq key (car entry) desc (nth 1 entry)
2949 type (nth 2 entry)
2950 match (nth 3 entry))
2951 (if (> (length key) 1)
2952 (cl-pushnew (string-to-char key) prefixes :test #'equal)
2953 (setq line
2954 (format
2955 "%-4s%-14s"
2956 (org-add-props (copy-sequence key)
2957 '(face bold))
2958 (cond
2959 ((string-match "\\S-" desc) desc)
2960 ((eq type 'agenda) "Agenda for current week or day")
2961 ((eq type 'agenda*) "Appointments for current week or day")
2962 ((eq type 'alltodo) "List of all TODO entries")
2963 ((eq type 'search) "Word search")
2964 ((eq type 'stuck) "List of stuck projects")
2965 ((eq type 'todo) "TODO keyword")
2966 ((eq type 'tags) "Tags query")
2967 ((eq type 'tags-todo) "Tags (TODO)")
2968 ((eq type 'tags-tree) "Tags tree")
2969 ((eq type 'todo-tree) "TODO kwd tree")
2970 ((eq type 'occur-tree) "Occur tree")
2971 ((functionp type) (if (symbolp type)
2972 (symbol-name type)
2973 "Lambda expression"))
2974 (t "???"))))
2975 (if org-agenda-menu-show-matcher
2976 (setq line
2977 (concat line ": "
2978 (cond
2979 ((stringp match)
2980 (setq match (copy-sequence match))
2981 (org-add-props match nil 'face 'org-warning))
2982 ((listp type)
2983 (format "set of %d commands" (length type))))))
2984 (if (org-string-nw-p match)
2985 (add-text-properties
2986 0 (length line) (list 'help-echo
2987 (concat "Matcher: " match)) line)))
2988 (push line lines)))
2989 (setq lines (nreverse lines))
2990 (when prefixes
2991 (mapc (lambda (x)
2992 (push
2993 (format "%s %s"
2994 (org-add-props (char-to-string x)
2995 nil 'face 'bold)
2996 (or (cdr (assoc (concat selstring
2997 (char-to-string x))
2998 prefix-descriptions))
2999 "Prefix key"))
3000 lines))
3001 prefixes))
3003 ;; Check if we should display in two columns
3004 (if org-agenda-menu-two-columns
3005 (progn
3006 (setq n (length lines)
3007 n1 (+ (/ n 2) (mod n 2))
3008 right (nthcdr n1 lines)
3009 left (copy-sequence lines))
3010 (setcdr (nthcdr (1- n1) left) nil))
3011 (setq left lines right nil))
3012 (while left
3013 (insert "\n" (pop left))
3014 (when right
3015 (if (< (current-column) 40)
3016 (move-to-column 40 t)
3017 (insert " "))
3018 (insert (pop right))))
3020 ;; Make the window the right size
3021 (goto-char (point-min))
3022 (if second-time
3023 (if (not (pos-visible-in-window-p (point-max)))
3024 (org-fit-window-to-buffer))
3025 (setq second-time t)
3026 (org-fit-window-to-buffer))
3028 ;; Ask for selection
3029 (message "Press key for agenda command%s:"
3030 (if (or restrict-ok org-agenda-overriding-restriction)
3031 (if org-agenda-overriding-restriction
3032 " (restriction lock active)"
3033 (if restriction
3034 (format " (restricted to %s)" restriction)
3035 " (unrestricted)"))
3036 ""))
3037 (setq c (read-char-exclusive))
3038 (message "")
3039 (cond
3040 ((assoc (char-to-string c) custom)
3041 (setq selstring (concat selstring (char-to-string c)))
3042 (throw 'exit (cons selstring restriction)))
3043 ((memq c prefixes)
3044 (setq selstring (concat selstring (char-to-string c))
3045 prefixes nil
3046 rmheader (or rmheader t)
3047 custom (delq nil (mapcar
3048 (lambda (x)
3049 (if (or (= (length (car x)) 1)
3050 (/= (string-to-char (car x)) c))
3052 (cons (substring (car x) 1) (cdr x))))
3053 custom))))
3054 ((eq c ?*)
3055 (call-interactively 'org-toggle-sticky-agenda)
3056 (sit-for 2))
3057 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
3058 (message "Restriction is only possible in Org buffers")
3059 (ding) (sit-for 1))
3060 ((eq c ?1)
3061 (org-agenda-remove-restriction-lock 'noupdate)
3062 (setq restriction 'buffer))
3063 ((eq c ?0)
3064 (org-agenda-remove-restriction-lock 'noupdate)
3065 (setq restriction (if region-p 'region 'subtree)))
3066 ((eq c ?<)
3067 (org-agenda-remove-restriction-lock 'noupdate)
3068 (setq restriction
3069 (cond
3070 ((eq restriction 'buffer)
3071 (if region-p 'region 'subtree))
3072 ((memq restriction '(subtree region))
3073 nil)
3074 (t 'buffer))))
3075 ((eq c ?>)
3076 (org-agenda-remove-restriction-lock 'noupdate)
3077 (setq restriction nil))
3078 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
3079 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
3080 ((and (> (length selstring) 0) (eq c ?\d))
3081 (delete-window)
3082 (org-agenda-get-restriction-and-command prefix-descriptions))
3084 ((equal c ?q) (error "Abort"))
3085 (t (user-error "Invalid key %c" c))))))))
3087 (defun org-agenda-fit-window-to-buffer ()
3088 "Fit the window to the buffer size."
3089 (and (memq org-agenda-window-setup '(reorganize-frame))
3090 (fboundp 'fit-window-to-buffer)
3091 (if (and (= (cdr org-agenda-window-frame-fractions) 1.0)
3092 (= (car org-agenda-window-frame-fractions) 1.0))
3093 (delete-other-windows)
3094 (org-fit-window-to-buffer
3096 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3097 (floor (* (frame-height) (car org-agenda-window-frame-fractions)))))))
3099 (defvar org-cmd nil)
3100 (defvar org-agenda-overriding-cmd nil)
3101 (defvar org-agenda-overriding-arguments nil)
3102 (defvar org-agenda-overriding-cmd-arguments nil)
3103 (defun org-agenda-run-series (name series)
3104 "Run agenda NAME as a SERIES of agenda commands."
3105 (org-let (nth 1 series) '(org-agenda-prepare name))
3106 ;; We need to reset agenda markers here, because when constructing a
3107 ;; block agenda, the individual blocks do not do that.
3108 (org-agenda-reset-markers)
3109 (let* ((org-agenda-multi t)
3110 (redo (list 'org-agenda-run-series name (list 'quote series)))
3111 (cmds (car series))
3112 (gprops (nth 1 series))
3113 match ;; The byte compiler incorrectly complains about this. Keep it!
3114 org-cmd type lprops)
3115 (while (setq org-cmd (pop cmds))
3116 (setq type (car org-cmd))
3117 (setq match (eval (nth 1 org-cmd)))
3118 (setq lprops (nth 2 org-cmd))
3119 (let ((org-agenda-overriding-arguments
3120 (if (eq org-agenda-overriding-cmd org-cmd)
3121 (or org-agenda-overriding-arguments
3122 org-agenda-overriding-cmd-arguments))))
3123 (cond
3124 ((eq type 'agenda)
3125 (org-let2 gprops lprops
3126 '(call-interactively 'org-agenda-list)))
3127 ((eq type 'agenda*)
3128 (org-let2 gprops lprops
3129 '(funcall 'org-agenda-list nil nil t)))
3130 ((eq type 'alltodo)
3131 (org-let2 gprops lprops
3132 '(call-interactively 'org-todo-list)))
3133 ((eq type 'search)
3134 (org-let2 gprops lprops
3135 '(org-search-view current-prefix-arg match nil)))
3136 ((eq type 'stuck)
3137 (org-let2 gprops lprops
3138 '(call-interactively 'org-agenda-list-stuck-projects)))
3139 ((eq type 'tags)
3140 (org-let2 gprops lprops
3141 '(org-tags-view current-prefix-arg match)))
3142 ((eq type 'tags-todo)
3143 (org-let2 gprops lprops
3144 '(org-tags-view '(4) match)))
3145 ((eq type 'todo)
3146 (org-let2 gprops lprops
3147 '(org-todo-list match)))
3148 ((fboundp type)
3149 (org-let2 gprops lprops
3150 '(funcall type match)))
3151 (t (error "Invalid type in command series")))))
3152 (widen)
3153 (let ((inhibit-read-only t))
3154 (add-text-properties (point-min) (point-max)
3155 `(org-series t org-series-redo-cmd ,redo)))
3156 (setq org-agenda-redo-command redo)
3157 (goto-char (point-min)))
3158 (org-agenda-fit-window-to-buffer)
3159 (org-let (nth 1 series) '(org-agenda-finalize)))
3161 ;;;###autoload
3162 (defmacro org-batch-agenda (cmd-key &rest parameters)
3163 "Run an agenda command in batch mode and send the result to STDOUT.
3164 If CMD-KEY is a string of length 1, it is used as a key in
3165 `org-agenda-custom-commands' and triggers this command. If it is a
3166 longer string it is used as a tags/todo match string.
3167 Parameters are alternating variable names and values that will be bound
3168 before running the agenda command."
3169 (org-eval-in-environment (org-make-parameter-alist parameters)
3170 (let (org-agenda-sticky)
3171 (if (> (length cmd-key) 1)
3172 (org-tags-view nil cmd-key)
3173 (org-agenda nil cmd-key))))
3174 (set-buffer org-agenda-buffer-name)
3175 (princ (buffer-string)))
3177 (defvar org-agenda-info nil)
3179 ;;;###autoload
3180 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
3181 "Run an agenda command in batch mode and send the result to STDOUT.
3182 If CMD-KEY is a string of length 1, it is used as a key in
3183 `org-agenda-custom-commands' and triggers this command. If it is a
3184 longer string it is used as a tags/todo match string.
3185 Parameters are alternating variable names and values that will be bound
3186 before running the agenda command.
3188 The output gives a line for each selected agenda item. Each
3189 item is a list of comma-separated values, like this:
3191 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3193 category The category of the item
3194 head The headline, without TODO kwd, TAGS and PRIORITY
3195 type The type of the agenda entry, can be
3196 todo selected in TODO match
3197 tagsmatch selected in tags match
3198 diary imported from diary
3199 deadline a deadline on given date
3200 scheduled scheduled on given date
3201 timestamp entry has timestamp on given date
3202 closed entry was closed on given date
3203 upcoming-deadline warning about deadline
3204 past-scheduled forwarded scheduled item
3205 block entry has date block including g. date
3206 todo The todo keyword, if any
3207 tags All tags including inherited ones, separated by colons
3208 date The relevant date, like 2007-2-14
3209 time The time, like 15:00-16:50
3210 extra Sting with extra planning info
3211 priority-l The priority letter if any was given
3212 priority-n The computed numerical priority
3213 agenda-day The day in the agenda where this is listed"
3214 (org-eval-in-environment (append '((org-agenda-remove-tags t))
3215 (org-make-parameter-alist parameters))
3216 (if (> (length cmd-key) 2)
3217 (org-tags-view nil cmd-key)
3218 (org-agenda nil cmd-key)))
3219 (set-buffer org-agenda-buffer-name)
3220 (let* ((lines (org-split-string (buffer-string) "\n"))
3221 line)
3222 (while (setq line (pop lines))
3223 (catch 'next
3224 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
3225 (setq org-agenda-info
3226 (org-fix-agenda-info (text-properties-at 0 line)))
3227 (princ
3228 (mapconcat 'org-agenda-export-csv-mapper
3229 '(org-category txt type todo tags date time extra
3230 priority-letter priority agenda-day)
3231 ","))
3232 (princ "\n")))))
3234 (defun org-fix-agenda-info (props)
3235 "Make sure all properties on an agenda item have a canonical form.
3236 This ensures the export commands can easily use it."
3237 (let (tmp re)
3238 (when (setq tmp (plist-get props 'tags))
3239 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3240 (when (setq tmp (plist-get props 'date))
3241 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3242 (let ((calendar-date-display-form '(year "-" month "-" day)))
3243 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3245 (setq tmp (calendar-date-string tmp)))
3246 (setq props (plist-put props 'date tmp)))
3247 (when (setq tmp (plist-get props 'day))
3248 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3249 (let ((calendar-date-display-form '(year "-" month "-" day)))
3250 (setq tmp (calendar-date-string tmp)))
3251 (setq props (plist-put props 'day tmp))
3252 (setq props (plist-put props 'agenda-day tmp)))
3253 (when (setq tmp (plist-get props 'txt))
3254 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3255 (plist-put props 'priority-letter (match-string 1 tmp))
3256 (setq tmp (replace-match "" t t tmp)))
3257 (when (and (setq re (plist-get props 'org-todo-regexp))
3258 (setq re (concat "\\`\\.*" re " ?"))
3259 (let ((case-fold-search nil)) (string-match re tmp)))
3260 (plist-put props 'todo (match-string 1 tmp))
3261 (setq tmp (replace-match "" t t tmp)))
3262 (plist-put props 'txt tmp)))
3263 props)
3265 (defun org-agenda-export-csv-mapper (prop)
3266 (let ((res (plist-get org-agenda-info prop)))
3267 (setq res
3268 (cond
3269 ((not res) "")
3270 ((stringp res) res)
3271 (t (prin1-to-string res))))
3272 (org-trim (replace-regexp-in-string "," ";" res nil t))))
3274 ;;;###autoload
3275 (defun org-store-agenda-views (&rest parameters)
3276 "Store agenda views."
3277 (interactive)
3278 (eval (list 'org-batch-store-agenda-views)))
3280 ;;;###autoload
3281 (defmacro org-batch-store-agenda-views (&rest parameters)
3282 "Run all custom agenda commands that have a file argument."
3283 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3284 (pop-up-frames nil)
3285 (dir default-directory)
3286 (pars (org-make-parameter-alist parameters))
3287 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
3288 (save-window-excursion
3289 (while cmds
3290 (setq cmd (pop cmds)
3291 thiscmdkey (car cmd)
3292 thiscmdcmd (cdr cmd)
3293 match (nth 2 thiscmdcmd)
3294 bufname (if org-agenda-sticky
3295 (or (and (stringp match)
3296 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3297 (format "*Org Agenda(%s)*" thiscmdkey))
3298 org-agenda-buffer-name)
3299 cmd-or-set (nth 2 cmd)
3300 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3301 files (nth (if (listp cmd-or-set) 4 5) cmd))
3302 (if (stringp files) (setq files (list files)))
3303 (when files
3304 (org-eval-in-environment (append org-agenda-exporter-settings
3305 opts pars)
3306 (org-agenda nil thiscmdkey))
3307 (set-buffer bufname)
3308 (while files
3309 (org-eval-in-environment (append org-agenda-exporter-settings
3310 opts pars)
3311 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3312 (and (get-buffer bufname)
3313 (kill-buffer bufname)))))))
3315 (defvar org-agenda-current-span nil
3316 "The current span used in the agenda view.") ; local variable in the agenda buffer
3317 (defun org-agenda-mark-header-line (pos)
3318 "Mark the line at POS as an agenda structure header."
3319 (save-excursion
3320 (goto-char pos)
3321 (put-text-property (point-at-bol) (point-at-eol)
3322 'org-agenda-structural-header t)
3323 (when org-agenda-title-append
3324 (put-text-property (point-at-bol) (point-at-eol)
3325 'org-agenda-title-append org-agenda-title-append))))
3327 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3328 (defvar org-agenda-write-buffer-name "Agenda View")
3329 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3330 "Write the current buffer (an agenda view) as a file.
3332 Depending on the extension of the file name, plain text (.txt),
3333 HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
3334 If the extension is .ics, translate visible agenda into iCalendar
3335 format. If the extension is .org, collect all subtrees
3336 corresponding to the agenda entries and add them in an .org file.
3338 With prefix argument OPEN, open the new file immediately. If
3339 NOSETTINGS is given, do not scope the settings of
3340 `org-agenda-exporter-settings' into the export commands. This is
3341 used when the settings have already been scoped and we do not
3342 wish to overrule other, higher priority settings. If
3343 AGENDA-BUFFER-NAME is provided, use this as the buffer name for
3344 the agenda to write."
3345 (interactive "FWrite agenda to file: \nP")
3346 (if (or (not (file-writable-p file))
3347 (and (file-exists-p file)
3348 (if (called-interactively-p 'any)
3349 (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
3350 (user-error "Cannot write agenda to file %s" file))
3351 (org-let (if nosettings nil org-agenda-exporter-settings)
3352 '(save-excursion
3353 (save-window-excursion
3354 (let ((bs (copy-sequence (buffer-string)))
3355 (extension (file-name-extension file))
3356 beg content)
3357 (with-temp-buffer
3358 (rename-buffer org-agenda-write-buffer-name t)
3359 (set-buffer-modified-p nil)
3360 (insert bs)
3361 (org-agenda-remove-marked-text 'invisible 'org-filtered)
3362 (run-hooks 'org-agenda-before-write-hook)
3363 (cond
3364 ((bound-and-true-p org-mobile-creating-agendas)
3365 (org-mobile-write-agenda-for-mobile file))
3366 ((string= "org" extension)
3367 (let (content p m message-log-max)
3368 (goto-char (point-min))
3369 (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
3370 (goto-char p)
3371 (setq m (get-text-property (point) 'org-hd-marker))
3372 (when m
3373 (push (save-excursion
3374 (set-buffer (marker-buffer m))
3375 (goto-char m)
3376 (org-copy-subtree 1 nil t t)
3377 org-subtree-clip)
3378 content)))
3379 (find-file file)
3380 (erase-buffer)
3381 (dolist (s content) (org-paste-subtree 1 s))
3382 (write-file file)
3383 (kill-buffer (current-buffer))
3384 (message "Org file written to %s" file)))
3385 ((member extension '("html" "htm"))
3386 (require 'htmlize)
3387 (set-buffer (htmlize-buffer (current-buffer)))
3388 (when org-agenda-export-html-style
3389 ;; replace <style> section with org-agenda-export-html-style
3390 (goto-char (point-min))
3391 (kill-region (- (search-forward "<style") 6)
3392 (search-forward "</style>"))
3393 (insert org-agenda-export-html-style))
3394 (write-file file)
3395 (kill-buffer (current-buffer))
3396 (message "HTML written to %s" file))
3397 ((string= "ps" extension)
3398 (require 'ps-print)
3399 (ps-print-buffer-with-faces file)
3400 (message "Postscript written to %s" file))
3401 ((string= "pdf" extension)
3402 (require 'ps-print)
3403 (ps-print-buffer-with-faces
3404 (concat (file-name-sans-extension file) ".ps"))
3405 (call-process "ps2pdf" nil nil nil
3406 (expand-file-name
3407 (concat (file-name-sans-extension file) ".ps"))
3408 (expand-file-name file))
3409 (delete-file (concat (file-name-sans-extension file) ".ps"))
3410 (message "PDF written to %s" file))
3411 ((string= "ics" extension)
3412 (require 'ox-icalendar)
3413 (org-icalendar-export-current-agenda (expand-file-name file)))
3415 (let ((bs (buffer-string)))
3416 (find-file file)
3417 (erase-buffer)
3418 (insert bs)
3419 (save-buffer 0)
3420 (kill-buffer (current-buffer))
3421 (message "Plain text written to %s" file))))))))
3422 (set-buffer (or agenda-bufname
3423 (and (called-interactively-p 'any) (buffer-name))
3424 org-agenda-buffer-name)))
3425 (when open (org-open-file file)))
3427 (defun org-agenda-remove-marked-text (property &optional value)
3428 "Delete all text marked with VALUE of PROPERTY.
3429 VALUE defaults to t."
3430 (let (beg)
3431 (setq value (or value t))
3432 (while (setq beg (text-property-any (point-min) (point-max)
3433 property value))
3434 (delete-region
3435 beg (or (next-single-property-change beg property)
3436 (point-max))))))
3438 (defun org-agenda-add-entry-text ()
3439 "Add entry text to agenda lines.
3440 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3441 entry text following headings shown in the agenda.
3442 Drawers will be excluded, also the line with scheduling/deadline info."
3443 (when (and (> org-agenda-add-entry-text-maxlines 0)
3444 (not (bound-and-true-p org-mobile-creating-agendas)))
3445 (let (m txt)
3446 (goto-char (point-min))
3447 (while (not (eobp))
3448 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3449 (beginning-of-line 2)
3450 (setq txt (org-agenda-get-some-entry-text
3451 m org-agenda-add-entry-text-maxlines " > "))
3452 (end-of-line 1)
3453 (if (string-match "\\S-" txt)
3454 (insert "\n" txt)
3455 (or (eobp) (forward-char 1))))))))
3457 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3458 &rest keep)
3459 "Extract entry text from MARKER, at most N-LINES lines.
3460 This will ignore drawers etc, just get the text.
3461 If INDENT is given, prefix every line with this string. If KEEP is
3462 given, it is a list of symbols, defining stuff that should not be
3463 removed from the entry content. Currently only `planning' is allowed here."
3464 (let (txt drawer-re kwd-time-re ind)
3465 (save-excursion
3466 (with-current-buffer (marker-buffer marker)
3467 (if (not (derived-mode-p 'org-mode))
3468 (setq txt "")
3469 (org-with-wide-buffer
3470 (goto-char marker)
3471 (end-of-line 1)
3472 (setq txt (buffer-substring
3473 (min (1+ (point)) (point-max))
3474 (progn (outline-next-heading) (point)))
3475 drawer-re org-drawer-regexp
3476 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3477 ".*\n?"))
3478 (with-temp-buffer
3479 (insert txt)
3480 (when org-agenda-add-entry-text-descriptive-links
3481 (goto-char (point-min))
3482 (while (org-activate-bracket-links (point-max))
3483 (add-text-properties (match-beginning 0) (match-end 0)
3484 '(face org-link))))
3485 (goto-char (point-min))
3486 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3487 (set-text-properties (match-beginning 0) (match-end 0)
3488 nil))
3489 (goto-char (point-min))
3490 (while (re-search-forward drawer-re nil t)
3491 (delete-region
3492 (match-beginning 0)
3493 (progn (re-search-forward
3494 "^[ \t]*:END:.*\n?" nil 'move)
3495 (point))))
3496 (unless (member 'planning keep)
3497 (goto-char (point-min))
3498 (while (re-search-forward kwd-time-re nil t)
3499 (replace-match "")))
3500 (goto-char (point-min))
3501 (when org-agenda-entry-text-exclude-regexps
3502 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3503 (while (setq re (pop re-list))
3504 (goto-char (point-min))
3505 (while (re-search-forward re nil t)
3506 (replace-match "")))))
3507 (goto-char (point-max))
3508 (skip-chars-backward " \t\n")
3509 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3511 ;; find and remove min common indentation
3512 (goto-char (point-min))
3513 (untabify (point-min) (point-max))
3514 (setq ind (org-get-indentation))
3515 (while (not (eobp))
3516 (unless (looking-at "[ \t]*$")
3517 (setq ind (min ind (org-get-indentation))))
3518 (beginning-of-line 2))
3519 (goto-char (point-min))
3520 (while (not (eobp))
3521 (unless (looking-at "[ \t]*$")
3522 (move-to-column ind)
3523 (delete-region (point-at-bol) (point)))
3524 (beginning-of-line 2))
3526 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3528 (goto-char (point-min))
3529 (when indent
3530 (while (and (not (eobp)) (re-search-forward "^" nil t))
3531 (replace-match indent t t)))
3532 (goto-char (point-min))
3533 (while (looking-at "[ \t]*\n") (replace-match ""))
3534 (goto-char (point-max))
3535 (when (> (org-current-line)
3536 n-lines)
3537 (org-goto-line (1+ n-lines))
3538 (backward-char 1))
3539 (setq txt (buffer-substring (point-min) (point))))))))
3540 txt))
3542 (defun org-check-for-org-mode ()
3543 "Make sure current buffer is in org-mode. Error if not."
3544 (or (derived-mode-p 'org-mode)
3545 (error "Cannot execute Org agenda command on buffer in %s"
3546 major-mode)))
3548 ;;; Agenda prepare and finalize
3550 (defvar org-agenda-multi nil) ; dynamically scoped
3551 (defvar org-agenda-pre-window-conf nil)
3552 (defvar org-agenda-columns-active nil)
3553 (defvar org-agenda-name nil)
3554 (defvar org-agenda-tag-filter nil)
3555 (defvar org-agenda-category-filter nil)
3556 (defvar org-agenda-regexp-filter nil)
3557 (defvar org-agenda-effort-filter nil)
3558 (defvar org-agenda-top-headline-filter nil)
3559 (defvar org-agenda-tag-filter-preset nil
3560 "A preset of the tags filter used for secondary agenda filtering.
3561 This must be a list of strings, each string must be a single tag preceded
3562 by \"+\" or \"-\".
3563 This variable should not be set directly, but agenda custom commands can
3564 bind it in the options section. The preset filter is a global property of
3565 the entire agenda view. In a block agenda, it will not work reliably to
3566 define a filter for one of the individual blocks. You need to set it in
3567 the global options and expect it to be applied to the entire view.")
3569 (defvar org-agenda-category-filter-preset nil
3570 "A preset of the category filter used for secondary agenda filtering.
3571 This must be a list of strings, each string must be a single category
3572 preceded by \"+\" or \"-\".
3573 This variable should not be set directly, but agenda custom commands can
3574 bind it in the options section. The preset filter is a global property of
3575 the entire agenda view. In a block agenda, it will not work reliably to
3576 define a filter for one of the individual blocks. You need to set it in
3577 the global options and expect it to be applied to the entire view.")
3579 (defvar org-agenda-regexp-filter-preset nil
3580 "A preset of the regexp filter used for secondary agenda filtering.
3581 This must be a list of strings, each string must be a single regexp
3582 preceded by \"+\" or \"-\".
3583 This variable should not be set directly, but agenda custom commands can
3584 bind it in the options section. The preset filter is a global property of
3585 the entire agenda view. In a block agenda, it will not work reliably to
3586 define a filter for one of the individual blocks. You need to set it in
3587 the global options and expect it to be applied to the entire view.")
3589 (defvar org-agenda-effort-filter-preset nil
3590 "A preset of the effort condition used for secondary agenda filtering.
3591 This must be a list of strings, each string must be a single regexp
3592 preceded by \"+\" or \"-\".
3593 This variable should not be set directly, but agenda custom commands can
3594 bind it in the options section. The preset filter is a global property of
3595 the entire agenda view. In a block agenda, it will not work reliably to
3596 define a filter for one of the individual blocks. You need to set it in
3597 the global options and expect it to be applied to the entire view.")
3599 (defun org-agenda-use-sticky-p ()
3600 "Return non-nil if an agenda buffer named
3601 `org-agenda-buffer-name' exists and should be shown instead of
3602 generating a new one."
3603 (and
3604 ;; turned off by user
3605 org-agenda-sticky
3606 ;; For multi-agenda buffer already exists
3607 (not org-agenda-multi)
3608 ;; buffer found
3609 (get-buffer org-agenda-buffer-name)
3610 ;; C-u parameter is same as last call
3611 (with-current-buffer (get-buffer org-agenda-buffer-name)
3612 (and
3613 (equal current-prefix-arg
3614 org-agenda-last-prefix-arg)
3615 ;; In case user turned stickiness on, while having existing
3616 ;; Agenda buffer active, don't reuse that buffer, because it
3617 ;; does not have org variables local
3618 org-agenda-this-buffer-is-sticky))))
3620 (defun org-agenda-prepare-window (abuf filter-alist)
3621 "Setup agenda buffer in the window.
3622 ABUF is the buffer for the agenda window.
3623 FILTER-ALIST is an alist of filters we need to apply when
3624 `org-agenda-persistent-filter' is non-nil."
3625 (let* ((awin (get-buffer-window abuf)) wconf)
3626 (cond
3627 ((equal (current-buffer) abuf) nil)
3628 (awin (select-window awin))
3629 ((not (setq wconf (current-window-configuration))))
3630 ((eq org-agenda-window-setup 'current-window)
3631 (pop-to-buffer-same-window abuf))
3632 ((eq org-agenda-window-setup 'other-window)
3633 (org-switch-to-buffer-other-window abuf))
3634 ((eq org-agenda-window-setup 'other-frame)
3635 (switch-to-buffer-other-frame abuf))
3636 ((eq org-agenda-window-setup 'only-window)
3637 (delete-other-windows)
3638 (pop-to-buffer-same-window abuf))
3639 ((eq org-agenda-window-setup 'reorganize-frame)
3640 (delete-other-windows)
3641 (org-switch-to-buffer-other-window abuf)))
3642 (setq org-agenda-tag-filter (cdr (assq 'tag filter-alist)))
3643 (setq org-agenda-category-filter (cdr (assq 'cat filter-alist)))
3644 (setq org-agenda-effort-filter (cdr (assq 'effort filter-alist)))
3645 (setq org-agenda-regexp-filter (cdr (assq 're filter-alist)))
3646 ;; Additional test in case agenda is invoked from within agenda
3647 ;; buffer via elisp link.
3648 (unless (equal (current-buffer) abuf)
3649 (pop-to-buffer-same-window abuf))
3650 (setq org-agenda-pre-window-conf
3651 (or wconf org-agenda-pre-window-conf))))
3653 (defun org-agenda-prepare (&optional name)
3654 (let ((filter-alist (if org-agenda-persistent-filter
3655 (with-current-buffer
3656 (get-buffer-create org-agenda-buffer-name)
3657 (list `(tag . ,org-agenda-tag-filter)
3658 `(re . ,org-agenda-regexp-filter)
3659 `(effort . ,org-agenda-effort-filter)
3660 `(cat . ,org-agenda-category-filter))))))
3661 (if (org-agenda-use-sticky-p)
3662 (progn
3663 (put 'org-agenda-tag-filter :preset-filter nil)
3664 (put 'org-agenda-category-filter :preset-filter nil)
3665 (put 'org-agenda-regexp-filter :preset-filter nil)
3666 ;; Popup existing buffer
3667 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name)
3668 filter-alist)
3669 (message "Sticky Agenda buffer, use `r' to refresh")
3670 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3671 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3672 (setq org-todo-keywords-for-agenda nil)
3673 (put 'org-agenda-tag-filter :preset-filter
3674 org-agenda-tag-filter-preset)
3675 (put 'org-agenda-category-filter :preset-filter
3676 org-agenda-category-filter-preset)
3677 (put 'org-agenda-regexp-filter :preset-filter
3678 org-agenda-regexp-filter-preset)
3679 (put 'org-agenda-effort-filter :preset-filter
3680 org-agenda-effort-filter-preset)
3681 (if org-agenda-multi
3682 (progn
3683 (setq buffer-read-only nil)
3684 (goto-char (point-max))
3685 (unless (or (bobp) org-agenda-compact-blocks
3686 (not org-agenda-block-separator))
3687 (insert "\n"
3688 (if (stringp org-agenda-block-separator)
3689 org-agenda-block-separator
3690 (make-string (window-width) org-agenda-block-separator))
3691 "\n"))
3692 (narrow-to-region (point) (point-max)))
3693 (setq org-done-keywords-for-agenda nil)
3694 ;; Setting any org variables that are in org-agenda-local-vars
3695 ;; list need to be done after the prepare call
3696 (org-agenda-prepare-window
3697 (get-buffer-create org-agenda-buffer-name) filter-alist)
3698 (setq buffer-read-only nil)
3699 (org-agenda-reset-markers)
3700 (let ((inhibit-read-only t)) (erase-buffer))
3701 (org-agenda-mode)
3702 (setq org-agenda-buffer (current-buffer))
3703 (setq org-agenda-contributing-files nil)
3704 (setq org-agenda-columns-active nil)
3705 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3706 (setq org-todo-keywords-for-agenda
3707 (org-uniquify org-todo-keywords-for-agenda))
3708 (setq org-done-keywords-for-agenda
3709 (org-uniquify org-done-keywords-for-agenda))
3710 (setq org-agenda-last-prefix-arg current-prefix-arg)
3711 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3712 (and name (not org-agenda-name)
3713 (setq-local org-agenda-name name)))
3714 (setq buffer-read-only nil))))
3716 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3717 (defun org-agenda-finalize ()
3718 "Finishing touch for the agenda buffer, called just before displaying it."
3719 (unless org-agenda-multi
3720 (save-excursion
3721 (let ((inhibit-read-only t))
3722 (goto-char (point-min))
3723 (save-excursion
3724 (while (org-activate-bracket-links (point-max))
3725 (add-text-properties (match-beginning 0) (match-end 0)
3726 '(face org-link))))
3727 (save-excursion
3728 (while (org-activate-plain-links (point-max))
3729 (add-text-properties (match-beginning 0) (match-end 0)
3730 '(face org-link))))
3731 (unless (eq org-agenda-remove-tags t)
3732 (org-agenda-align-tags))
3733 (unless org-agenda-with-colors
3734 (remove-text-properties (point-min) (point-max) '(face nil)))
3735 (if (and (boundp 'org-agenda-overriding-columns-format)
3736 org-agenda-overriding-columns-format)
3737 (setq-local org-agenda-overriding-columns-format
3738 org-agenda-overriding-columns-format))
3739 (if (and (boundp 'org-agenda-view-columns-initially)
3740 org-agenda-view-columns-initially)
3741 (org-agenda-columns))
3742 (when org-agenda-fontify-priorities
3743 (org-agenda-fontify-priorities))
3744 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3745 (org-agenda-dim-blocked-tasks))
3746 (org-agenda-mark-clocking-task)
3747 (when org-agenda-entry-text-mode
3748 (org-agenda-entry-text-hide)
3749 (org-agenda-entry-text-show))
3750 (if (and (functionp 'org-habit-insert-consistency-graphs)
3751 (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
3752 (org-habit-insert-consistency-graphs))
3753 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3754 (unless (or (eq org-agenda-show-inherited-tags 'always)
3755 (and (listp org-agenda-show-inherited-tags)
3756 (memq org-agenda-type org-agenda-show-inherited-tags))
3757 (and (eq org-agenda-show-inherited-tags t)
3758 (or (eq org-agenda-use-tag-inheritance t)
3759 (and (listp org-agenda-use-tag-inheritance)
3760 (not (memq org-agenda-type
3761 org-agenda-use-tag-inheritance))))))
3762 (let (mrk)
3763 (save-excursion
3764 (goto-char (point-min))
3765 (while (equal (forward-line) 0)
3766 (when (setq mrk (get-text-property (point) 'org-hd-marker))
3767 (put-text-property (point-at-bol) (point-at-eol)
3768 'tags (org-with-point-at mrk
3769 (delete-dups
3770 (mapcar 'downcase (org-get-tags-at))))))))))
3771 (run-hooks 'org-agenda-finalize-hook)
3772 (when org-agenda-top-headline-filter
3773 (org-agenda-filter-top-headline-apply
3774 org-agenda-top-headline-filter))
3775 (when org-agenda-tag-filter
3776 (org-agenda-filter-apply org-agenda-tag-filter 'tag t))
3777 (when (get 'org-agenda-tag-filter :preset-filter)
3778 (org-agenda-filter-apply
3779 (get 'org-agenda-tag-filter :preset-filter) 'tag t))
3780 (when org-agenda-category-filter
3781 (org-agenda-filter-apply org-agenda-category-filter 'category))
3782 (when (get 'org-agenda-category-filter :preset-filter)
3783 (org-agenda-filter-apply
3784 (get 'org-agenda-category-filter :preset-filter) 'category))
3785 (when org-agenda-regexp-filter
3786 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
3787 (when (get 'org-agenda-regexp-filter :preset-filter)
3788 (org-agenda-filter-apply
3789 (get 'org-agenda-regexp-filter :preset-filter) 'regexp))
3790 (when org-agenda-effort-filter
3791 (org-agenda-filter-apply org-agenda-effort-filter 'effort))
3792 (when (get 'org-agenda-effort-filter :preset-filter)
3793 (org-agenda-filter-apply
3794 (get 'org-agenda-effort-filter :preset-filter) 'effort))
3795 (add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)))))
3797 (defun org-agenda-mark-clocking-task ()
3798 "Mark the current clock entry in the agenda if it is present."
3799 ;; We need to widen when `org-agenda-finalize' is called from
3800 ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
3801 (when (bound-and-true-p org-clock-current-task)
3802 (save-restriction
3803 (widen)
3804 (org-agenda-unmark-clocking-task)
3805 (when (marker-buffer org-clock-hd-marker)
3806 (save-excursion
3807 (goto-char (point-min))
3808 (let (s ov)
3809 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3810 (goto-char s)
3811 (when (equal (org-get-at-bol 'org-hd-marker)
3812 org-clock-hd-marker)
3813 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3814 (overlay-put ov 'type 'org-agenda-clocking)
3815 (overlay-put ov 'face 'org-agenda-clocking)
3816 (overlay-put ov 'help-echo
3817 "The clock is running in this item")))))))))
3819 (defun org-agenda-unmark-clocking-task ()
3820 "Unmark the current clocking task."
3821 (mapc (lambda (o)
3822 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3823 (delete-overlay o)))
3824 (overlays-in (point-min) (point-max))))
3826 (defun org-agenda-fontify-priorities ()
3827 "Make highest priority lines bold, and lowest italic."
3828 (interactive)
3829 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3830 (delete-overlay o)))
3831 (overlays-in (point-min) (point-max)))
3832 (save-excursion
3833 (let (b e p ov h l)
3834 (goto-char (point-min))
3835 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3836 (setq h (or (get-char-property (point) 'org-highest-priority)
3837 org-highest-priority)
3838 l (or (get-char-property (point) 'org-lowest-priority)
3839 org-lowest-priority)
3840 p (string-to-char (match-string 1))
3841 b (match-beginning 0)
3842 e (if (eq org-agenda-fontify-priorities 'cookies)
3843 (match-end 0)
3844 (point-at-eol))
3845 ov (make-overlay b e))
3846 (overlay-put
3847 ov 'face
3848 (let ((special-face
3849 (cond ((org-face-from-face-or-color
3850 'priority 'org-priority
3851 (cdr (assoc p org-priority-faces))))
3852 ((and (listp org-agenda-fontify-priorities)
3853 (org-face-from-face-or-color
3854 'priority 'org-priority
3855 (cdr (assoc p org-agenda-fontify-priorities)))))
3856 ((equal p l) 'italic)
3857 ((equal p h) 'bold))))
3858 (if special-face (list special-face 'org-priority) 'org-priority)))
3859 (overlay-put ov 'org-type 'org-priority)))))
3861 (defvar org-depend-tag-blocked)
3863 (defun org-agenda-dim-blocked-tasks (&optional invisible)
3864 "Dim currently blocked TODOs in the agenda display.
3865 When INVISIBLE is non-nil, hide currently blocked TODO instead of
3866 dimming them."
3867 (interactive "P")
3868 (when (called-interactively-p 'interactive)
3869 (message "Dim or hide blocked tasks..."))
3870 (dolist (o (overlays-in (point-min) (point-max)))
3871 (when (eq (overlay-get o 'org-type) 'org-blocked-todo)
3872 (delete-overlay o)))
3873 (save-excursion
3874 (let ((inhibit-read-only t)
3875 (org-depend-tag-blocked nil)
3876 org-blocked-by-checkboxes)
3877 (goto-char (point-min))
3878 (while (let ((pos (text-property-not-all
3879 (point) (point-max) 'todo-state nil)))
3880 (when pos (goto-char pos)))
3881 (setq org-blocked-by-checkboxes nil)
3882 (let ((marker (org-get-at-bol 'org-hd-marker)))
3883 (when (and (markerp marker)
3884 (with-current-buffer (marker-buffer marker)
3885 (save-excursion (goto-char marker)
3886 (org-entry-blocked-p))))
3887 ;; Entries blocked by checkboxes cannot be made invisible.
3888 ;; See `org-agenda-dim-blocked-tasks' for details.
3889 (let* ((really-invisible
3890 (and (not org-blocked-by-checkboxes)
3891 (or invisible (eq org-agenda-dim-blocked-tasks
3892 'invisible))))
3893 (ov (make-overlay (if really-invisible (line-end-position 0)
3894 (line-beginning-position))
3895 (line-end-position))))
3896 (if really-invisible (overlay-put ov 'invisible t)
3897 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3898 (overlay-put ov 'org-type 'org-blocked-todo))))
3899 (forward-line))))
3900 (when (called-interactively-p 'interactive)
3901 (message "Dim or hide blocked tasks...done")))
3903 (defvar org-agenda-skip-function nil
3904 "Function to be called at each match during agenda construction.
3905 If this function returns nil, the current match should not be skipped.
3906 Otherwise, the function must return a position from where the search
3907 should be continued.
3908 This may also be a Lisp form, it will be evaluated.
3909 Never set this variable using `setq' or so, because then it will apply
3910 to all future agenda commands. If you do want a global skipping condition,
3911 use the option `org-agenda-skip-function-global' instead.
3912 The correct usage for `org-agenda-skip-function' is to bind it with
3913 `let' to scope it dynamically into the agenda-constructing command.
3914 A good way to set it is through options in `org-agenda-custom-commands'.")
3916 (defun org-agenda-skip ()
3917 "Throw to `:skip' in places that should be skipped.
3918 Also moves point to the end of the skipped region, so that search can
3919 continue from there."
3920 (let ((p (point-at-bol)) to)
3921 (when (or
3922 (save-excursion (goto-char p) (looking-at comment-start-skip))
3923 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3924 (get-text-property p :org-archived)
3925 (org-end-of-subtree t))
3926 (and org-agenda-skip-comment-trees
3927 (get-text-property p :org-comment)
3928 (org-end-of-subtree t))
3929 (and (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3930 (org-agenda-skip-eval org-agenda-skip-function)))
3931 (goto-char to))
3932 (org-in-src-block-p t))
3933 (throw :skip t))))
3935 (defun org-agenda-skip-eval (form)
3936 "If FORM is a function or a list, call (or eval) it and return the result.
3937 `save-excursion' and `save-match-data' are wrapped around the call, so point
3938 and match data are returned to the previous state no matter what these
3939 functions do."
3940 (let (fp)
3941 (and form
3942 (or (setq fp (functionp form))
3943 (consp form))
3944 (save-excursion
3945 (save-match-data
3946 (if fp
3947 (funcall form)
3948 (eval form)))))))
3950 (defvar org-agenda-markers nil
3951 "List of all currently active markers created by `org-agenda'.")
3952 (defvar org-agenda-last-marker-time (float-time)
3953 "Creation time of the last agenda marker.")
3955 (defun org-agenda-new-marker (&optional pos)
3956 "Return a new agenda marker.
3957 Maker is at point, or at POS if non-nil. Org mode keeps a list of
3958 these markers and resets them when they are no longer in use."
3959 (let ((m (copy-marker (or pos (point)) t)))
3960 (setq org-agenda-last-marker-time (float-time))
3961 (if org-agenda-buffer
3962 (with-current-buffer org-agenda-buffer
3963 (push m org-agenda-markers))
3964 (push m org-agenda-markers))
3967 (defun org-agenda-reset-markers ()
3968 "Reset markers created by `org-agenda'."
3969 (while org-agenda-markers
3970 (move-marker (pop org-agenda-markers) nil)))
3972 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3973 "Save relative positions of markers in region.
3974 This check for agenda markers in all agenda buffers currently active."
3975 (dolist (buf (buffer-list))
3976 (with-current-buffer buf
3977 (when (eq major-mode 'org-agenda-mode)
3978 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3979 org-agenda-markers)))))
3981 ;;; Entry text mode
3983 (defun org-agenda-entry-text-show-here ()
3984 "Add some text from the entry as context to the current line."
3985 (let (m txt o)
3986 (setq m (org-get-at-bol 'org-hd-marker))
3987 (unless (marker-buffer m)
3988 (error "No marker points to an entry here"))
3989 (setq txt (concat "\n" (org-no-properties
3990 (org-agenda-get-some-entry-text
3991 m org-agenda-entry-text-maxlines
3992 org-agenda-entry-text-leaders))))
3993 (when (string-match "\\S-" txt)
3994 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3995 (overlay-put o 'evaporate t)
3996 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3997 (overlay-put o 'after-string txt))))
3999 (defun org-agenda-entry-text-show ()
4000 "Add entry context for all agenda lines."
4001 (interactive)
4002 (save-excursion
4003 (goto-char (point-max))
4004 (beginning-of-line 1)
4005 (while (not (bobp))
4006 (when (org-get-at-bol 'org-hd-marker)
4007 (org-agenda-entry-text-show-here))
4008 (beginning-of-line 0))))
4010 (defun org-agenda-entry-text-hide ()
4011 "Remove any shown entry context."
4012 (delq nil
4013 (mapcar (lambda (o)
4014 (if (eq (overlay-get o 'org-overlay-type)
4015 'agenda-entry-content)
4016 (progn (delete-overlay o) t)))
4017 (overlays-in (point-min) (point-max)))))
4019 (defun org-agenda-get-day-face (date)
4020 "Return the face DATE should be displayed with."
4021 (cond ((and (functionp org-agenda-day-face-function)
4022 (funcall org-agenda-day-face-function date)))
4023 ((org-agenda-today-p date) 'org-agenda-date-today)
4024 ((memq (calendar-day-of-week date) org-agenda-weekend-days)
4025 'org-agenda-date-weekend)
4026 (t 'org-agenda-date)))
4028 (defvar org-agenda-show-log-scoped)
4030 ;;; Agenda Daily/Weekly
4032 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4033 "Start day for the agenda view.
4034 Custom commands can set this variable in the options section.
4035 This is usually a string like \"2007-11-01\", \"+2d\" or any other
4036 input allowed when reading a date through the Org calendar.
4037 See the docstring of `org-read-date' for details.")
4038 (defvar org-starting-day nil) ; local variable in the agenda buffer
4039 (defvar org-arg-loc nil) ; local variable
4041 (defvar org-agenda-buffer-tmp-name nil)
4042 ;;;###autoload
4043 (defun org-agenda-list (&optional arg start-day span with-hour)
4044 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4045 The view will be for the current day or week, but from the overview buffer
4046 you will be able to go to other days/weeks.
4048 With a numeric prefix argument in an interactive call, the agenda will
4049 span ARG days. Lisp programs should instead specify SPAN to change
4050 the number of days. SPAN defaults to `org-agenda-span'.
4052 START-DAY defaults to TODAY, or to the most recent match for the weekday
4053 given in `org-agenda-start-on-weekday'.
4055 When WITH-HOUR is non-nil, only include scheduled and deadline
4056 items if they have an hour specification like [h]h:mm."
4057 (interactive "P")
4058 (if org-agenda-overriding-arguments
4059 (setq arg (car org-agenda-overriding-arguments)
4060 start-day (nth 1 org-agenda-overriding-arguments)
4061 span (nth 2 org-agenda-overriding-arguments)))
4062 (if (and (integerp arg) (> arg 0))
4063 (setq span arg arg nil))
4064 (catch 'exit
4065 (setq org-agenda-buffer-name
4066 (or org-agenda-buffer-tmp-name
4067 (if org-agenda-sticky
4068 (cond ((and org-keys (stringp org-match))
4069 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4070 (org-keys
4071 (format "*Org Agenda(%s)*" org-keys))
4072 (t "*Org Agenda(a)*")))
4073 org-agenda-buffer-name))
4074 (org-agenda-prepare "Day/Week")
4075 (setq start-day (or start-day org-agenda-start-day))
4076 (if (stringp start-day)
4077 ;; Convert to an absolute day number
4078 (setq start-day (time-to-days (org-read-date nil t start-day))))
4079 (org-compile-prefix-format 'agenda)
4080 (org-set-sorting-strategy 'agenda)
4081 (let* ((span (org-agenda-ndays-to-span (or span org-agenda-span)))
4082 (today (org-today))
4083 (sd (or start-day today))
4084 (ndays (org-agenda-span-to-ndays span sd))
4085 (org-agenda-start-on-weekday
4086 (if (or (eq ndays 7) (eq ndays 14))
4087 org-agenda-start-on-weekday))
4088 (thefiles (org-agenda-files nil 'ifmode))
4089 (files thefiles)
4090 (start (if (or (null org-agenda-start-on-weekday)
4091 (< ndays 7))
4093 (let* ((nt (calendar-day-of-week
4094 (calendar-gregorian-from-absolute sd)))
4095 (n1 org-agenda-start-on-weekday)
4096 (d (- nt n1)))
4097 (- sd (+ (if (< d 0) 7 0) d)))))
4098 (day-numbers (list start))
4099 (day-cnt 0)
4100 (inhibit-redisplay (not debug-on-error))
4101 (org-agenda-show-log-scoped org-agenda-show-log)
4102 s e rtn rtnall file date d start-pos end-pos todayp
4103 clocktable-start clocktable-end filter)
4104 (setq org-agenda-redo-command
4105 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4106 (dotimes (n (1- ndays))
4107 (push (1+ (car day-numbers)) day-numbers))
4108 (setq day-numbers (nreverse day-numbers))
4109 (setq clocktable-start (car day-numbers)
4110 clocktable-end (1+ (or (org-last day-numbers) 0)))
4111 (setq-local org-starting-day (car day-numbers))
4112 (setq-local org-arg-loc arg)
4113 (setq-local org-agenda-current-span (org-agenda-ndays-to-span span))
4114 (unless org-agenda-compact-blocks
4115 (let* ((d1 (car day-numbers))
4116 (d2 (org-last day-numbers))
4117 (w1 (org-days-to-iso-week d1))
4118 (w2 (org-days-to-iso-week d2)))
4119 (setq s (point))
4120 (if org-agenda-overriding-header
4121 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4122 nil 'face 'org-agenda-structure) "\n")
4123 (insert (org-agenda-span-name span)
4124 "-agenda"
4125 (if (< (- d2 d1) 350)
4126 (if (= w1 w2)
4127 (format " (W%02d)" w1)
4128 (format " (W%02d-W%02d)" w1 w2))
4130 ":\n")))
4131 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4132 'org-date-line t))
4133 (org-agenda-mark-header-line s))
4134 (while (setq d (pop day-numbers))
4135 (setq date (calendar-gregorian-from-absolute d)
4136 s (point))
4137 (if (or (setq todayp (= d today))
4138 (and (not start-pos) (= d sd)))
4139 (setq start-pos (point))
4140 (if (and start-pos (not end-pos))
4141 (setq end-pos (point))))
4142 (setq files thefiles
4143 rtnall nil)
4144 (while (setq file (pop files))
4145 (catch 'nextfile
4146 (org-check-agenda-file file)
4147 (let ((org-agenda-entry-types org-agenda-entry-types))
4148 ;; Starred types override non-starred equivalents
4149 (when (member :deadline* org-agenda-entry-types)
4150 (setq org-agenda-entry-types
4151 (delq :deadline org-agenda-entry-types)))
4152 (when (member :scheduled* org-agenda-entry-types)
4153 (setq org-agenda-entry-types
4154 (delq :scheduled org-agenda-entry-types)))
4155 ;; Honor with-hour
4156 (when with-hour
4157 (when (member :deadline org-agenda-entry-types)
4158 (setq org-agenda-entry-types
4159 (delq :deadline org-agenda-entry-types))
4160 (push :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 (push :scheduled* org-agenda-entry-types)))
4165 (unless org-agenda-include-deadlines
4166 (setq org-agenda-entry-types
4167 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4168 (cond
4169 ((memq org-agenda-show-log-scoped '(only clockcheck))
4170 (setq rtn (org-agenda-get-day-entries
4171 file date :closed)))
4172 (org-agenda-show-log-scoped
4173 (setq rtn (apply 'org-agenda-get-day-entries
4174 file date
4175 (append '(:closed) org-agenda-entry-types))))
4177 (setq rtn (apply 'org-agenda-get-day-entries
4178 file date
4179 org-agenda-entry-types)))))
4180 (setq rtnall (append rtnall rtn)))) ;; all entries
4181 (if org-agenda-include-diary
4182 (let ((org-agenda-search-headline-for-time t))
4183 (require 'diary-lib)
4184 (setq rtn (org-get-entries-from-diary date))
4185 (setq rtnall (append rtnall rtn))))
4186 (if (or rtnall org-agenda-show-all-dates)
4187 (progn
4188 (setq day-cnt (1+ day-cnt))
4189 (insert
4190 (if (stringp org-agenda-format-date)
4191 (format-time-string org-agenda-format-date
4192 (org-time-from-absolute date))
4193 (funcall org-agenda-format-date date))
4194 "\n")
4195 (put-text-property s (1- (point)) 'face
4196 (org-agenda-get-day-face date))
4197 (put-text-property s (1- (point)) 'org-date-line t)
4198 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4199 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4200 (when todayp
4201 (put-text-property s (1- (point)) 'org-today t))
4202 (setq rtnall
4203 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4204 (if rtnall (insert ;; all entries
4205 (org-agenda-finalize-entries rtnall 'agenda)
4206 "\n"))
4207 (put-text-property s (1- (point)) 'day d)
4208 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4209 (when (and org-agenda-clockreport-mode clocktable-start)
4210 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4211 ;; the above line is to ensure the restricted range!
4212 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4213 tbl)
4214 (setq p (org-plist-delete p :block))
4215 (setq p (plist-put p :tstart clocktable-start))
4216 (setq p (plist-put p :tend clocktable-end))
4217 (setq p (plist-put p :scope 'agenda))
4218 (setq tbl (apply 'org-clock-get-clocktable p))
4219 (insert tbl)))
4220 (goto-char (point-min))
4221 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4222 (unless (and (pos-visible-in-window-p (point-min))
4223 (pos-visible-in-window-p (point-max)))
4224 (goto-char (1- (point-max)))
4225 (recenter -1)
4226 (if (not (pos-visible-in-window-p (or start-pos 1)))
4227 (progn
4228 (goto-char (or start-pos 1))
4229 (recenter 1))))
4230 (goto-char (or start-pos 1))
4231 (add-text-properties (point-min) (point-max)
4232 `(org-agenda-type agenda
4233 org-last-args (,arg ,start-day ,span)
4234 org-redo-cmd ,org-agenda-redo-command
4235 org-series-cmd ,org-cmd))
4236 (if (eq org-agenda-show-log-scoped 'clockcheck)
4237 (org-agenda-show-clocking-issues))
4238 (org-agenda-finalize)
4239 (setq buffer-read-only t)
4240 (message ""))))
4242 (defun org-agenda-ndays-to-span (n)
4243 "Return a span symbol for a span of N days, or N if none matches."
4244 (cond ((symbolp n) n)
4245 ((= n 1) 'day)
4246 ((= n 7) 'week)
4247 ((= n 14) 'fortnight)
4248 (t n)))
4250 (defun org-agenda-span-to-ndays (span &optional start-day)
4251 "Return ndays from SPAN, possibly starting at START-DAY.
4252 START-DAY is an absolute time value."
4253 (cond ((numberp span) span)
4254 ((eq span 'day) 1)
4255 ((eq span 'week) 7)
4256 ((eq span 'fortnight) 14)
4257 ((eq span 'month)
4258 (let ((date (calendar-gregorian-from-absolute start-day)))
4259 (calendar-last-day-of-month (car date) (cl-caddr date))))
4260 ((eq span 'year)
4261 (let ((date (calendar-gregorian-from-absolute start-day)))
4262 (if (calendar-leap-year-p (cl-caddr date)) 366 365)))))
4264 (defun org-agenda-span-name (span)
4265 "Return a SPAN name."
4266 (if (null span)
4268 (if (symbolp span)
4269 (capitalize (symbol-name span))
4270 (format "%d days" span))))
4272 ;;; Agenda word search
4274 (defvar org-agenda-search-history nil)
4276 (defvar org-search-syntax-table nil
4277 "Special syntax table for Org search.
4278 In this table, we have single quotes not as word constituents, to
4279 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4281 (defvar org-mode-syntax-table) ; From org.el
4282 (defun org-search-syntax-table ()
4283 (unless org-search-syntax-table
4284 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4285 (modify-syntax-entry ?' "." org-search-syntax-table)
4286 (modify-syntax-entry ?` "." org-search-syntax-table))
4287 org-search-syntax-table)
4289 (defvar org-agenda-last-search-view-search-was-boolean nil)
4291 ;;;###autoload
4292 (defun org-search-view (&optional todo-only string edit-at)
4293 "Show all entries that contain a phrase or words or regular expressions.
4295 With optional prefix argument TODO-ONLY, only consider entries that are
4296 TODO entries. The argument STRING can be used to pass a default search
4297 string into this function. If EDIT-AT is non-nil, it means that the
4298 user should get a chance to edit this string, with cursor at position
4299 EDIT-AT.
4301 The search string can be viewed either as a phrase that should be found as
4302 is, or it can be broken into a number of snippets, each of which must match
4303 in a Boolean way to select an entry. The default depends on the variable
4304 `org-agenda-search-view-always-boolean'.
4305 Even if this is turned off (the default) you can always switch to
4306 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4308 The default is a direct search of the whole phrase, where each space in
4309 the search string can expand to an arbitrary amount of whitespace,
4310 including newlines.
4312 If using a Boolean search, the search string is split on whitespace and
4313 each snippet is searched separately, with logical AND to select an entry.
4314 Words prefixed with a minus must *not* occur in the entry. Words without
4315 a prefix or prefixed with a plus must occur in the entry. Matching is
4316 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4317 match whole words, not parts of a word) if
4318 `org-agenda-search-view-force-full-words' is set (default is nil).
4320 Boolean search snippets enclosed by curly braces are interpreted as
4321 regular expressions that must or (when preceded with \"-\") must not
4322 match in the entry. Snippets enclosed into double quotes will be taken
4323 as a whole, to include whitespace.
4325 - If the search string starts with an asterisk, search only in headlines.
4326 - If (possibly after the leading star) the search string starts with an
4327 exclamation mark, this also means to look at TODO entries only, an effect
4328 that can also be achieved with a prefix argument.
4329 - If (possibly after star and exclamation mark) the search string starts
4330 with a colon, this will mean that the (non-regexp) snippets of the
4331 Boolean search must match as full words.
4333 This command searches the agenda files, and in addition the files listed
4334 in `org-agenda-text-search-extra-files'."
4335 (interactive "P")
4336 (if org-agenda-overriding-arguments
4337 (setq todo-only (car org-agenda-overriding-arguments)
4338 string (nth 1 org-agenda-overriding-arguments)
4339 edit-at (nth 2 org-agenda-overriding-arguments)))
4340 (let* ((props (list 'face nil
4341 'done-face 'org-agenda-done
4342 'org-not-done-regexp org-not-done-regexp
4343 'org-todo-regexp org-todo-regexp
4344 'org-complex-heading-regexp org-complex-heading-regexp
4345 'mouse-face 'highlight
4346 'help-echo (format "mouse-2 or RET jump to location")))
4347 (full-words org-agenda-search-view-force-full-words)
4348 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4349 regexp rtn rtnall files file pos inherited-tags
4350 marker category level tags c neg re boolean
4351 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4352 (unless (and (not edit-at)
4353 (stringp string)
4354 (string-match "\\S-" string))
4355 (setq string (read-string
4356 (if org-agenda-search-view-always-boolean
4357 "[+-]Word/{Regexp} ...: "
4358 "Phrase or [+-]Word/{Regexp} ...: ")
4359 (cond
4360 ((integerp edit-at) (cons string edit-at))
4361 (edit-at string))
4362 'org-agenda-search-history)))
4363 (catch 'exit
4364 (if org-agenda-sticky
4365 (setq org-agenda-buffer-name
4366 (if (stringp string)
4367 (format "*Org Agenda(%s:%s)*"
4368 (or org-keys (or (and todo-only "S") "s")) string)
4369 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4370 (org-agenda-prepare "SEARCH")
4371 (org-compile-prefix-format 'search)
4372 (org-set-sorting-strategy 'search)
4373 (setq org-agenda-redo-command
4374 (list 'org-search-view (if todo-only t nil)
4375 (list 'if 'current-prefix-arg nil string)))
4376 (setq org-agenda-query-string string)
4377 (if (equal (string-to-char string) ?*)
4378 (setq hdl-only t
4379 words (substring string 1))
4380 (setq words string))
4381 (when (equal (string-to-char words) ?!)
4382 (setq todo-only t
4383 words (substring words 1)))
4384 (when (equal (string-to-char words) ?:)
4385 (setq full-words t
4386 words (substring words 1)))
4387 (if (or org-agenda-search-view-always-boolean
4388 (member (string-to-char words) '(?- ?+ ?\{)))
4389 (setq boolean t))
4390 (setq words (org-split-string words))
4391 (let (www w)
4392 (while (setq w (pop words))
4393 (while (and (string-match "\\\\\\'" w) words)
4394 (setq w (concat (substring w 0 -1) " " (pop words))))
4395 (push w www))
4396 (setq words (nreverse www) www nil)
4397 (while (setq w (pop words))
4398 (when (and (string-match "\\`[-+]?{" w)
4399 (not (string-match "}\\'" w)))
4400 (while (and words (not (string-match "}\\'" (car words))))
4401 (setq w (concat w " " (pop words))))
4402 (setq w (concat w " " (pop words))))
4403 (push w www))
4404 (setq words (nreverse www)))
4405 (setq org-agenda-last-search-view-search-was-boolean boolean)
4406 (when boolean
4407 (let (wds w)
4408 (while (setq w (pop words))
4409 (if (or (equal (substring w 0 1) "\"")
4410 (and (> (length w) 1)
4411 (member (substring w 0 1) '("+" "-"))
4412 (equal (substring w 1 2) "\"")))
4413 (while (and words (not (equal (substring w -1) "\"")))
4414 (setq w (concat w " " (pop words)))))
4415 (and (string-match "\\`\\([-+]?\\)\"" w)
4416 (setq w (replace-match "\\1" nil nil w)))
4417 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4418 (push w wds))
4419 (setq words (nreverse wds))))
4420 (if boolean
4421 (mapc (lambda (w)
4422 (setq c (string-to-char w))
4423 (if (equal c ?-)
4424 (setq neg t w (substring w 1))
4425 (if (equal c ?+)
4426 (setq neg nil w (substring w 1))
4427 (setq neg nil)))
4428 (if (string-match "\\`{.*}\\'" w)
4429 (setq re (substring w 1 -1))
4430 (if full-words
4431 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4432 (setq re (regexp-quote (downcase w)))))
4433 (if neg (push re regexps-) (push re regexps+)))
4434 words)
4435 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4436 regexps+))
4437 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4438 (if (not regexps+)
4439 (setq regexp org-outline-regexp-bol)
4440 (setq regexp (pop regexps+))
4441 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4442 regexp))))
4443 (setq files (org-agenda-files nil 'ifmode))
4444 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4445 (pop org-agenda-text-search-extra-files)
4446 (setq files (org-add-archive-files files)))
4447 (setq files (append files org-agenda-text-search-extra-files)
4448 rtnall nil)
4449 (while (setq file (pop files))
4450 (setq ee nil)
4451 (catch 'nextfile
4452 (org-check-agenda-file file)
4453 (setq buffer (if (file-exists-p file)
4454 (org-get-agenda-file-buffer file)
4455 (error "No such file %s" file)))
4456 (if (not buffer)
4457 ;; If file does not exist, make sure an error message is sent
4458 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4459 file))))
4460 (with-current-buffer buffer
4461 (with-syntax-table (org-search-syntax-table)
4462 (unless (derived-mode-p 'org-mode)
4463 (error "Agenda file %s is not in `org-mode'" file))
4464 (let ((case-fold-search t))
4465 (save-excursion
4466 (save-restriction
4467 (if (eq buffer org-agenda-restrict)
4468 (narrow-to-region org-agenda-restrict-begin
4469 org-agenda-restrict-end)
4470 (widen))
4471 (goto-char (point-min))
4472 (unless (or (org-at-heading-p)
4473 (outline-next-heading))
4474 (throw 'nextfile t))
4475 (goto-char (max (point-min) (1- (point))))
4476 (while (re-search-forward regexp nil t)
4477 (org-back-to-heading t)
4478 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4479 (> (org-reduced-level (org-outline-level))
4480 org-agenda-search-view-max-outline-level)
4481 (forward-line -1)
4482 (org-back-to-heading t)))
4483 (skip-chars-forward "* ")
4484 (setq beg (point-at-bol)
4485 beg1 (point)
4486 end (progn
4487 (outline-next-heading)
4488 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4489 (> (org-reduced-level (org-outline-level))
4490 org-agenda-search-view-max-outline-level)
4491 (forward-line 1)
4492 (outline-next-heading)))
4493 (point)))
4495 (catch :skip
4496 (goto-char beg)
4497 (org-agenda-skip)
4498 (setq str (buffer-substring-no-properties
4499 (point-at-bol)
4500 (if hdl-only (point-at-eol) end)))
4501 (mapc (lambda (wr) (when (string-match wr str)
4502 (goto-char (1- end))
4503 (throw :skip t)))
4504 regexps-)
4505 (mapc (lambda (wr) (unless (string-match wr str)
4506 (goto-char (1- end))
4507 (throw :skip t)))
4508 (if todo-only
4509 (cons (concat "^\\*+[ \t]+"
4510 org-not-done-regexp)
4511 regexps+)
4512 regexps+))
4513 (goto-char beg)
4514 (setq marker (org-agenda-new-marker (point))
4515 category (org-get-category)
4516 level (make-string (org-reduced-level (org-outline-level)) ? )
4517 inherited-tags
4518 (or (eq org-agenda-show-inherited-tags 'always)
4519 (and (listp org-agenda-show-inherited-tags)
4520 (memq 'todo org-agenda-show-inherited-tags))
4521 (and (eq org-agenda-show-inherited-tags t)
4522 (or (eq org-agenda-use-tag-inheritance t)
4523 (memq 'todo org-agenda-use-tag-inheritance))))
4524 tags (org-get-tags-at nil (not inherited-tags))
4525 txt (org-agenda-format-item
4527 (buffer-substring-no-properties
4528 beg1 (point-at-eol))
4529 level category tags t))
4530 (org-add-props txt props
4531 'org-marker marker 'org-hd-marker marker
4532 'org-todo-regexp org-todo-regexp
4533 'level level
4534 'org-complex-heading-regexp org-complex-heading-regexp
4535 'priority 1000
4536 'type "search")
4537 (push txt ee)
4538 (goto-char (1- end))))))))))
4539 (setq rtn (nreverse ee))
4540 (setq rtnall (append rtnall rtn)))
4541 (if org-agenda-overriding-header
4542 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4543 nil 'face 'org-agenda-structure) "\n")
4544 (insert "Search words: ")
4545 (add-text-properties (point-min) (1- (point))
4546 (list 'face 'org-agenda-structure))
4547 (setq pos (point))
4548 (insert string "\n")
4549 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4550 (setq pos (point))
4551 (unless org-agenda-multi
4552 (insert (substitute-command-keys "\
4553 Press `\\[org-agenda-manipulate-query-add]', \
4554 `\\[org-agenda-manipulate-query-subtract]' to add/sub word, \
4555 `\\[org-agenda-manipulate-query-add-re]', \
4556 `\\[org-agenda-manipulate-query-subtract-re]' to add/sub regexp, \
4557 `\\[universal-argument] \\[org-agenda-redo]' to edit\n"))
4558 (add-text-properties pos (1- (point))
4559 (list 'face 'org-agenda-structure))))
4560 (org-agenda-mark-header-line (point-min))
4561 (when rtnall
4562 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4563 (goto-char (point-min))
4564 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4565 (add-text-properties (point-min) (point-max)
4566 `(org-agenda-type search
4567 org-last-args (,todo-only ,string ,edit-at)
4568 org-redo-cmd ,org-agenda-redo-command
4569 org-series-cmd ,org-cmd))
4570 (org-agenda-finalize)
4571 (setq buffer-read-only t))))
4573 ;;; Agenda TODO list
4575 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4576 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4577 (concat
4578 (if (or (equal keywords "ALL") (not keywords))
4579 (propertize "ALL" 'face 'warning)
4580 (mapconcat
4581 (lambda (kw)
4582 (propertize kw 'face (org-get-todo-face kw)))
4583 (org-split-string keywords "|")
4584 "|"))
4585 "\n"))
4587 (defvar org-select-this-todo-keyword nil)
4588 (defvar org-last-arg nil)
4590 ;;;###autoload
4591 (defun org-todo-list (&optional arg)
4592 "Show all (not done) TODO entries from all agenda file in a single list.
4593 The prefix arg can be used to select a specific TODO keyword and limit
4594 the list to these. When using `\\[universal-argument]', you will be prompted
4595 for a keyword. A numeric prefix directly selects the Nth keyword in
4596 `org-todo-keywords-1'."
4597 (interactive "P")
4598 (if org-agenda-overriding-arguments
4599 (setq arg org-agenda-overriding-arguments))
4600 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4601 (let* ((today (org-today))
4602 (date (calendar-gregorian-from-absolute today))
4603 (kwds org-todo-keywords-for-agenda)
4604 (completion-ignore-case t)
4605 (org-select-this-todo-keyword
4606 (if (stringp arg) arg
4607 (and arg (integerp arg) (> arg 0)
4608 (nth (1- arg) kwds))))
4609 rtn rtnall files file pos)
4610 (when (equal arg '(4))
4611 (setq org-select-this-todo-keyword
4612 (completing-read "Keyword (or KWD1|K2D2|...): "
4613 (mapcar #'list kwds) nil nil)))
4614 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4615 (catch 'exit
4616 (if org-agenda-sticky
4617 (setq org-agenda-buffer-name
4618 (if (stringp org-select-this-todo-keyword)
4619 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4620 org-select-this-todo-keyword)
4621 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4622 (org-agenda-prepare "TODO")
4623 (org-compile-prefix-format 'todo)
4624 (org-set-sorting-strategy 'todo)
4625 (setq org-agenda-redo-command
4626 `(org-todo-list (or (and (numberp current-prefix-arg)
4627 current-prefix-arg)
4628 ,org-select-this-todo-keyword
4629 current-prefix-arg ,arg)))
4630 (setq files (org-agenda-files nil 'ifmode)
4631 rtnall nil)
4632 (while (setq file (pop files))
4633 (catch 'nextfile
4634 (org-check-agenda-file file)
4635 (setq rtn (org-agenda-get-day-entries file date :todo))
4636 (setq rtnall (append rtnall rtn))))
4637 (if org-agenda-overriding-header
4638 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4639 nil 'face 'org-agenda-structure) "\n")
4640 (insert "Global list of TODO items of type: ")
4641 (add-text-properties (point-min) (1- (point))
4642 (list 'face 'org-agenda-structure
4643 'short-heading
4644 (concat "ToDo: "
4645 (or org-select-this-todo-keyword "ALL"))))
4646 (org-agenda-mark-header-line (point-min))
4647 (insert (org-agenda-propertize-selected-todo-keywords
4648 org-select-this-todo-keyword))
4649 (setq pos (point))
4650 (unless org-agenda-multi
4651 (insert (substitute-command-keys "Available with \
4652 `N \\[org-agenda-redo]': (0)[ALL]"))
4653 (let ((n 0) s)
4654 (mapc (lambda (x)
4655 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4656 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4657 (insert "\n "))
4658 (insert " " s))
4659 kwds))
4660 (insert "\n"))
4661 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4662 (org-agenda-mark-header-line (point-min))
4663 (when rtnall
4664 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4665 (goto-char (point-min))
4666 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4667 (add-text-properties (point-min) (point-max)
4668 `(org-agenda-type todo
4669 org-last-args ,arg
4670 org-redo-cmd ,org-agenda-redo-command
4671 org-series-cmd ,org-cmd))
4672 (org-agenda-finalize)
4673 (setq buffer-read-only t))))
4675 ;;; Agenda tags match
4677 ;;;###autoload
4678 (defun org-tags-view (&optional todo-only match)
4679 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4680 The prefix arg TODO-ONLY limits the search to TODO entries."
4681 (interactive "P")
4682 (if org-agenda-overriding-arguments
4683 (setq todo-only (car org-agenda-overriding-arguments)
4684 match (nth 1 org-agenda-overriding-arguments)))
4685 (let* ((org-tags-match-list-sublevels
4686 org-tags-match-list-sublevels)
4687 (completion-ignore-case t)
4688 rtn rtnall files file pos matcher
4689 buffer)
4690 (when (and (stringp match) (not (string-match "\\S-" match)))
4691 (setq match nil))
4692 (catch 'exit
4693 (if org-agenda-sticky
4694 (setq org-agenda-buffer-name
4695 (if (stringp match)
4696 (format "*Org Agenda(%s:%s)*"
4697 (or org-keys (or (and todo-only "M") "m")) match)
4698 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4699 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4700 ;; expanding tags within `org-make-tags-matcher'
4701 (org-agenda-prepare (concat "TAGS " match))
4702 (setq org--matcher-tags-todo-only todo-only
4703 matcher (org-make-tags-matcher match)
4704 match (car matcher)
4705 matcher (cdr matcher))
4706 (org-compile-prefix-format 'tags)
4707 (org-set-sorting-strategy 'tags)
4708 (setq org-agenda-query-string match)
4709 (setq org-agenda-redo-command
4710 (list 'org-tags-view
4711 `(quote ,org--matcher-tags-todo-only)
4712 `(if current-prefix-arg nil ,org-agenda-query-string)))
4713 (setq files (org-agenda-files nil 'ifmode)
4714 rtnall nil)
4715 (while (setq file (pop files))
4716 (catch 'nextfile
4717 (org-check-agenda-file file)
4718 (setq buffer (if (file-exists-p file)
4719 (org-get-agenda-file-buffer file)
4720 (error "No such file %s" file)))
4721 (if (not buffer)
4722 ;; If file does not exist, error message to agenda
4723 (setq rtn (list
4724 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4725 rtnall (append rtnall rtn))
4726 (with-current-buffer buffer
4727 (unless (derived-mode-p 'org-mode)
4728 (error "Agenda file %s is not in `org-mode'" file))
4729 (save-excursion
4730 (save-restriction
4731 (if (eq buffer org-agenda-restrict)
4732 (narrow-to-region org-agenda-restrict-begin
4733 org-agenda-restrict-end)
4734 (widen))
4735 (setq rtn (org-scan-tags 'agenda
4736 matcher
4737 org--matcher-tags-todo-only))
4738 (setq rtnall (append rtnall rtn))))))))
4739 (if org-agenda-overriding-header
4740 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4741 nil 'face 'org-agenda-structure) "\n")
4742 (insert "Headlines with TAGS match: ")
4743 (add-text-properties (point-min) (1- (point))
4744 (list 'face 'org-agenda-structure
4745 'short-heading
4746 (concat "Match: " match)))
4747 (setq pos (point))
4748 (insert match "\n")
4749 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4750 (setq pos (point))
4751 (unless org-agenda-multi
4752 (insert (substitute-command-keys
4753 "Press `\\[universal-argument] \\[org-agenda-redo]' \
4754 to search again with new search string\n")))
4755 (add-text-properties pos (1- (point))
4756 (list 'face 'org-agenda-structure)))
4757 (org-agenda-mark-header-line (point-min))
4758 (when rtnall
4759 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4760 (goto-char (point-min))
4761 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4762 (add-text-properties
4763 (point-min) (point-max)
4764 `(org-agenda-type tags
4765 org-last-args (,org--matcher-tags-todo-only ,match)
4766 org-redo-cmd ,org-agenda-redo-command
4767 org-series-cmd ,org-cmd))
4768 (org-agenda-finalize)
4769 (setq buffer-read-only t))))
4771 ;;; Agenda Finding stuck projects
4773 (defvar org-agenda-skip-regexp nil
4774 "Regular expression used in skipping subtrees for the agenda.
4775 This is basically a temporary global variable that can be set and then
4776 used by user-defined selections using `org-agenda-skip-function'.")
4778 (defvar org-agenda-overriding-header nil
4779 "When set during agenda, todo and tags searches it replaces the header.
4780 This variable should not be set directly, but custom commands can bind it
4781 in the options section.")
4783 (defun org-agenda-skip-entry-if (&rest conditions)
4784 "Skip entry if any of CONDITIONS is true.
4785 See `org-agenda-skip-if' for details."
4786 (org-agenda-skip-if nil conditions))
4788 (defun org-agenda-skip-subtree-if (&rest conditions)
4789 "Skip subtree if any of CONDITIONS is true.
4790 See `org-agenda-skip-if' for details."
4791 (org-agenda-skip-if t conditions))
4793 (defun org-agenda-skip-if (subtree conditions)
4794 "Checks current entity for CONDITIONS.
4795 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4796 the entry (i.e. the text before the next heading) is checked.
4798 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4799 from different tests. Valid conditions are:
4801 scheduled Check if there is a scheduled cookie
4802 notscheduled Check if there is no scheduled cookie
4803 deadline Check if there is a deadline
4804 notdeadline Check if there is no deadline
4805 timestamp Check if there is a timestamp (also deadline or scheduled)
4806 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4807 regexp Check if regexp matches
4808 notregexp Check if regexp does not match.
4809 todo Check if TODO keyword matches
4810 nottodo Check if TODO keyword does not match
4812 The regexp is taken from the conditions list, it must come right after
4813 the `regexp' or `notregexp' element.
4815 `todo' and `nottodo' accept as an argument a list of todo
4816 keywords, which may include \"*\" to match any todo keyword.
4818 (org-agenda-skip-entry-if \\='todo \\='(\"TODO\" \"WAITING\"))
4820 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4822 Instead of a list, a keyword class may be given. For example:
4824 (org-agenda-skip-entry-if \\='nottodo \\='done)
4826 would skip entries that haven't been marked with any of \"DONE\"
4827 keywords. Possible classes are: `todo', `done', `any'.
4829 If any of these conditions is met, this function returns the end point of
4830 the entity, causing the search to continue from there. This is a function
4831 that can be put into `org-agenda-skip-function' for the duration of a command."
4832 (let (beg end m)
4833 (org-back-to-heading t)
4834 (setq beg (point)
4835 end (if subtree
4836 (progn (org-end-of-subtree t) (point))
4837 (progn (outline-next-heading) (1- (point)))))
4838 (goto-char beg)
4839 (and
4841 (and (memq 'scheduled conditions)
4842 (re-search-forward org-scheduled-time-regexp end t))
4843 (and (memq 'notscheduled conditions)
4844 (not (re-search-forward org-scheduled-time-regexp end t)))
4845 (and (memq 'deadline conditions)
4846 (re-search-forward org-deadline-time-regexp end t))
4847 (and (memq 'notdeadline conditions)
4848 (not (re-search-forward org-deadline-time-regexp end t)))
4849 (and (memq 'timestamp conditions)
4850 (re-search-forward org-ts-regexp end t))
4851 (and (memq 'nottimestamp conditions)
4852 (not (re-search-forward org-ts-regexp end t)))
4853 (and (setq m (memq 'regexp conditions))
4854 (stringp (nth 1 m))
4855 (re-search-forward (nth 1 m) end t))
4856 (and (setq m (memq 'notregexp conditions))
4857 (stringp (nth 1 m))
4858 (not (re-search-forward (nth 1 m) end t)))
4859 (and (or
4860 (setq m (memq 'nottodo conditions))
4861 (setq m (memq 'todo-unblocked conditions))
4862 (setq m (memq 'nottodo-unblocked conditions))
4863 (setq m (memq 'todo conditions)))
4864 (org-agenda-skip-if-todo m end)))
4865 end)))
4867 (defun org-agenda-skip-if-todo (args end)
4868 "Helper function for `org-agenda-skip-if', do not use it directly.
4869 ARGS is a list with first element either `todo', `nottodo',
4870 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
4871 a list of TODO keywords, or a state symbol `todo' or `done' or
4872 `any'."
4873 (let ((kw (car args))
4874 (arg (cadr args))
4875 todo-wds todo-re)
4876 (setq todo-wds
4877 (org-uniquify
4878 (cond
4879 ((listp arg) ;; list of keywords
4880 (if (member "*" arg)
4881 (mapcar 'substring-no-properties org-todo-keywords-1)
4882 arg))
4883 ((symbolp arg) ;; keyword class name
4884 (cond
4885 ((eq arg 'todo)
4886 (org-delete-all org-done-keywords
4887 (mapcar 'substring-no-properties
4888 org-todo-keywords-1)))
4889 ((eq arg 'done) org-done-keywords)
4890 ((eq arg 'any)
4891 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4892 (setq todo-re
4893 (concat "^\\*+[ \t]+\\<\\("
4894 (mapconcat 'identity todo-wds "\\|")
4895 "\\)\\>"))
4896 (cond
4897 ((eq kw 'todo) (re-search-forward todo-re end t))
4898 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
4899 ((eq kw 'todo-unblocked)
4900 (catch 'unblocked
4901 (while (re-search-forward todo-re end t)
4902 (or (org-entry-blocked-p) (throw 'unblocked t)))
4903 nil))
4904 ((eq kw 'nottodo-unblocked)
4905 (catch 'unblocked
4906 (while (re-search-forward todo-re end t)
4907 (or (org-entry-blocked-p) (throw 'unblocked nil)))
4911 ;;;###autoload
4912 (defun org-agenda-list-stuck-projects (&rest ignore)
4913 "Create agenda view for projects that are stuck.
4914 Stuck projects are project that have no next actions. For the definitions
4915 of what a project is and how to check if it stuck, customize the variable
4916 `org-stuck-projects'."
4917 (interactive)
4918 (let* ((org-agenda-overriding-header
4919 (or org-agenda-overriding-header "List of stuck projects: "))
4920 (matcher (nth 0 org-stuck-projects))
4921 (todo (nth 1 org-stuck-projects))
4922 (tags (nth 2 org-stuck-projects))
4923 (gen-re (org-string-nw-p (nth 3 org-stuck-projects)))
4924 (todo-wds
4925 (if (not (member "*" todo)) todo
4926 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
4927 (org-delete-all org-done-keywords-for-agenda
4928 (copy-sequence org-todo-keywords-for-agenda))))
4929 (todo-re (and todo
4930 (format "^\\*+[ \t]+\\(%s\\)\\>"
4931 (mapconcat #'identity todo-wds "\\|"))))
4932 (tags-re (cond ((null tags) nil)
4933 ((member "*" tags)
4934 (eval-when-compile
4935 (concat org-outline-regexp-bol
4936 ".*:[[:alnum:]_@#%]+:[ \t]*$")))
4937 (tags (concat org-outline-regexp-bol
4938 ".*:\\("
4939 (mapconcat #'identity tags "\\|")
4940 "\\):[[:alnum:]_@#%:]*[ \t]*$"))
4941 (t nil)))
4942 (re-list (delq nil (list todo-re tags-re gen-re)))
4943 (skip-re
4944 (if (null re-list)
4945 (error "Missing information to identify unstuck projects")
4946 (mapconcat #'identity re-list "\\|")))
4947 (org-agenda-skip-function
4948 ;; Skip entry if `org-agenda-skip-regexp' matches anywhere
4949 ;; in the subtree.
4950 `(lambda ()
4951 (and (save-excursion
4952 (let ((case-fold-search nil))
4953 (re-search-forward
4954 ,skip-re (save-excursion (org-end-of-subtree t)) t)))
4955 (progn (outline-next-heading) (point))))))
4956 (org-tags-view nil matcher)
4957 (setq org-agenda-buffer-name (buffer-name))
4958 (with-current-buffer org-agenda-buffer-name
4959 (setq org-agenda-redo-command
4960 `(org-agenda-list-stuck-projects ,current-prefix-arg)))))
4962 ;;; Diary integration
4964 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
4965 (defvar diary-list-entries-hook)
4966 (defvar diary-time-regexp)
4967 (defun org-get-entries-from-diary (date)
4968 "Get the (Emacs Calendar) diary entries for DATE."
4969 (require 'diary-lib)
4970 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
4971 (diary-display-function 'diary-fancy-display)
4972 (pop-up-frames nil)
4973 (diary-list-entries-hook
4974 (cons 'org-diary-default-entry diary-list-entries-hook))
4975 (diary-file-name-prefix nil) ; turn this feature off
4976 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
4977 entries
4978 (org-disable-agenda-to-diary t))
4979 (save-excursion
4980 (save-window-excursion
4981 (funcall (if (fboundp 'diary-list-entries)
4982 'diary-list-entries 'list-diary-entries)
4983 date 1)))
4984 (if (not (get-buffer diary-fancy-buffer))
4985 (setq entries nil)
4986 (with-current-buffer diary-fancy-buffer
4987 (setq buffer-read-only nil)
4988 (if (zerop (buffer-size))
4989 ;; No entries
4990 (setq entries nil)
4991 ;; Omit the date and other unnecessary stuff
4992 (org-agenda-cleanup-fancy-diary)
4993 ;; Add prefix to each line and extend the text properties
4994 (if (zerop (buffer-size))
4995 (setq entries nil)
4996 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
4997 (setq entries
4998 (with-temp-buffer
4999 (insert entries) (goto-char (point-min))
5000 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5001 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5002 (replace-match (concat "; " (match-string 1)))))
5003 (buffer-string)))))
5004 (set-buffer-modified-p nil)
5005 (kill-buffer diary-fancy-buffer)))
5006 (when entries
5007 (setq entries (org-split-string entries "\n"))
5008 (setq entries
5009 (mapcar
5010 (lambda (x)
5011 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5012 ;; Extend the text properties to the beginning of the line
5013 (org-add-props x (text-properties-at (1- (length x)) x)
5014 'type "diary" 'date date 'face 'org-agenda-diary))
5015 entries)))))
5017 (defvar org-agenda-cleanup-fancy-diary-hook nil
5018 "Hook run when the fancy diary buffer is cleaned up.")
5020 (defun org-agenda-cleanup-fancy-diary ()
5021 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5022 This gets rid of the date, the underline under the date, and
5023 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5024 date. It also removes lines that contain only whitespace."
5025 (goto-char (point-min))
5026 (if (looking-at ".*?:[ \t]*")
5027 (progn
5028 (replace-match "")
5029 (re-search-forward "\n=+$" nil t)
5030 (replace-match "")
5031 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5032 (re-search-forward "\n=+$" nil t)
5033 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5034 (goto-char (point-min))
5035 (while (re-search-forward "^ +\n" nil t)
5036 (replace-match ""))
5037 (goto-char (point-min))
5038 (if (re-search-forward "^Org mode dummy\n?" nil t)
5039 (replace-match ""))
5040 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5042 ;; Make sure entries from the diary have the right text properties.
5043 (eval-after-load "diary-lib"
5044 '(if (boundp 'diary-modify-entry-list-string-function)
5045 ;; We can rely on the hook, nothing to do
5047 ;; Hook not available, must use advice to make this work
5048 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5049 "Make the position visible."
5050 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5051 (stringp string)
5052 buffer-file-name)
5053 (setq string (org-modify-diary-entry-string string))))))
5055 (defun org-modify-diary-entry-string (string)
5056 "Add text properties to string, allowing Org to act on it."
5057 (org-add-props string nil
5058 'mouse-face 'highlight
5059 'help-echo (if buffer-file-name
5060 (format "mouse-2 or RET jump to diary file %s"
5061 (abbreviate-file-name buffer-file-name))
5063 'org-agenda-diary-link t
5064 'org-marker (org-agenda-new-marker (point-at-bol))))
5066 (defun org-diary-default-entry ()
5067 "Add a dummy entry to the diary.
5068 Needed to avoid empty dates which mess up holiday display."
5069 ;; Catch the error if dealing with the new add-to-diary-alist
5070 (when org-disable-agenda-to-diary
5071 (condition-case nil
5072 (org-add-to-diary-list original-date "Org mode dummy" "")
5073 (error
5074 (org-add-to-diary-list original-date "Org mode dummy" "" nil)))))
5076 (defun org-add-to-diary-list (&rest args)
5077 (if (fboundp 'diary-add-to-list)
5078 (apply 'diary-add-to-list args)
5079 (apply 'add-to-diary-list args)))
5081 (defvar org-diary-last-run-time nil)
5083 ;;;###autoload
5084 (defun org-diary (&rest args)
5085 "Return diary information from org files.
5086 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5087 It accesses org files and extracts information from those files to be
5088 listed in the diary. The function accepts arguments specifying what
5089 items should be listed. For a list of arguments allowed here, see the
5090 variable `org-agenda-entry-types'.
5092 The call in the diary file should look like this:
5094 &%%(org-diary) ~/path/to/some/orgfile.org
5096 Use a separate line for each org file to check. Or, if you omit the file name,
5097 all files listed in `org-agenda-files' will be checked automatically:
5099 &%%(org-diary)
5101 If you don't give any arguments (as in the example above), the default value
5102 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5103 So the example above may also be written as
5105 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5107 The function expects the lisp variables `entry' and `date' to be provided
5108 by the caller, because this is how the calendar works. Don't use this
5109 function from a program - use `org-agenda-get-day-entries' instead."
5110 (when (> (- (float-time)
5111 org-agenda-last-marker-time)
5113 ;; I am not sure if this works with sticky agendas, because the marker
5114 ;; list is then no longer a global variable.
5115 (org-agenda-reset-markers))
5116 (org-compile-prefix-format 'agenda)
5117 (org-set-sorting-strategy 'agenda)
5118 (setq args (or args org-agenda-entry-types))
5119 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5120 (list entry)
5121 (org-agenda-files t)))
5122 (time (float-time))
5123 file rtn results)
5124 (when (or (not org-diary-last-run-time)
5125 (> (- time
5126 org-diary-last-run-time)
5128 (org-agenda-prepare-buffers files))
5129 (setq org-diary-last-run-time time)
5130 ;; If this is called during org-agenda, don't return any entries to
5131 ;; the calendar. Org Agenda will list these entries itself.
5132 (if org-disable-agenda-to-diary (setq files nil))
5133 (while (setq file (pop files))
5134 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5135 (setq results (append results rtn)))
5136 (when results
5137 (setq results
5138 (mapcar (lambda (i) (replace-regexp-in-string
5139 org-bracket-link-regexp "\\3" i)) results))
5140 (concat (org-agenda-finalize-entries results) "\n"))))
5142 ;;; Agenda entry finders
5144 (defun org-agenda--timestamp-to-absolute (&rest args)
5145 "Call `org-time-string-to-absolute' with ARGS.
5146 However, throw `:skip' whenever an error is raised."
5147 (condition-case e
5148 (apply #'org-time-string-to-absolute args)
5149 (org-diary-sexp-no-match (throw :skip nil))
5150 (error
5151 (message "%s; Skipping entry" (error-message-string e))
5152 (throw :skip nil))))
5154 (defun org-agenda-get-day-entries (file date &rest args)
5155 "Does the work for `org-diary' and `org-agenda'.
5156 FILE is the path to a file to be checked for entries. DATE is date like
5157 the one returned by `calendar-current-date'. ARGS are symbols indicating
5158 which kind of entries should be extracted. For details about these, see
5159 the documentation of `org-diary'."
5160 (let* ((org-startup-folded nil)
5161 (org-startup-align-all-tables nil)
5162 (buffer (if (file-exists-p file) (org-get-agenda-file-buffer file)
5163 (error "No such file %s" file))))
5164 (if (not buffer)
5165 ;; If file does not exist, signal it in diary nonetheless.
5166 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5167 (with-current-buffer buffer
5168 (unless (derived-mode-p 'org-mode)
5169 (error "Agenda file %s is not in `org-mode'" file))
5170 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5171 (setf org-agenda-current-date date)
5172 (save-excursion
5173 (save-restriction
5174 (if (eq buffer org-agenda-restrict)
5175 (narrow-to-region org-agenda-restrict-begin
5176 org-agenda-restrict-end)
5177 (widen))
5178 ;; Rationalize ARGS. Also make sure `:deadline' comes
5179 ;; first in order to populate DEADLINES before passing it.
5181 ;; We use `delq' since `org-uniquify' duplicates ARGS,
5182 ;; guarding us from modifying `org-agenda-entry-types'.
5183 (setf args (org-uniquify (or args org-agenda-entry-types)))
5184 (when (and (memq :scheduled args) (memq :scheduled* args))
5185 (setf args (delq :scheduled* args)))
5186 (cond
5187 ((memq :deadline args)
5188 (setf args (cons :deadline
5189 (delq :deadline (delq :deadline* args)))))
5190 ((memq :deadline* args)
5191 (setf args (cons :deadline* (delq :deadline* args)))))
5192 ;; Collect list of headlines. Return them flattened.
5193 (let ((case-fold-search nil) results deadlines)
5194 (dolist (arg args (apply #'nconc (nreverse results)))
5195 (pcase arg
5196 ((and :todo (guard (org-agenda-today-p date)))
5197 (push (org-agenda-get-todos) results))
5198 (:timestamp
5199 (push (org-agenda-get-blocks) results)
5200 (push (org-agenda-get-timestamps deadlines) results))
5201 (:sexp
5202 (push (org-agenda-get-sexps) results))
5203 (:scheduled
5204 (push (org-agenda-get-scheduled deadlines) results))
5205 (:scheduled*
5206 (push (org-agenda-get-scheduled deadlines t) results))
5207 (:closed
5208 (push (org-agenda-get-progress) results))
5209 (:deadline
5210 (setf deadlines (org-agenda-get-deadlines))
5211 (push deadlines results))
5212 (:deadline*
5213 (setf deadlines (org-agenda-get-deadlines t))
5214 (push deadlines results)))))))))))
5216 (defsubst org-em (x y list)
5217 "Is X or Y a member of LIST?"
5218 (or (memq x list) (memq y list)))
5220 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5221 (defvar org-agenda-sorting-strategy-selected nil)
5223 (defun org-agenda-entry-get-agenda-timestamp (pom)
5224 "Retrieve timestamp information for sorting agenda views.
5225 Given a point or marker POM, returns a cons cell of the timestamp
5226 and the timestamp type relevant for the sorting strategy in
5227 `org-agenda-sorting-strategy-selected'."
5228 (let (ts ts-date-type)
5229 (save-match-data
5230 (cond ((org-em 'scheduled-up 'scheduled-down
5231 org-agenda-sorting-strategy-selected)
5232 (setq ts (org-entry-get pom "SCHEDULED")
5233 ts-date-type " scheduled"))
5234 ((org-em 'deadline-up 'deadline-down
5235 org-agenda-sorting-strategy-selected)
5236 (setq ts (org-entry-get pom "DEADLINE")
5237 ts-date-type " deadline"))
5238 ((org-em 'ts-up 'ts-down
5239 org-agenda-sorting-strategy-selected)
5240 (setq ts (org-entry-get pom "TIMESTAMP")
5241 ts-date-type " timestamp"))
5242 ((org-em 'tsia-up 'tsia-down
5243 org-agenda-sorting-strategy-selected)
5244 (setq ts (org-entry-get pom "TIMESTAMP_IA")
5245 ts-date-type " timestamp_ia"))
5246 ((org-em 'timestamp-up 'timestamp-down
5247 org-agenda-sorting-strategy-selected)
5248 (setq ts (or (org-entry-get pom "SCHEDULED")
5249 (org-entry-get pom "DEADLINE")
5250 (org-entry-get pom "TIMESTAMP")
5251 (org-entry-get pom "TIMESTAMP_IA"))
5252 ts-date-type ""))
5253 (t (setq ts-date-type "")))
5254 (cons (when ts (ignore-errors (org-time-string-to-absolute ts)))
5255 ts-date-type))))
5257 (defun org-agenda-get-todos ()
5258 "Return the TODO information for agenda display."
5259 (let* ((props (list 'face nil
5260 'done-face 'org-agenda-done
5261 'org-not-done-regexp org-not-done-regexp
5262 'org-todo-regexp org-todo-regexp
5263 'org-complex-heading-regexp org-complex-heading-regexp
5264 'mouse-face 'highlight
5265 'help-echo
5266 (format "mouse-2 or RET jump to org file %s"
5267 (abbreviate-file-name buffer-file-name))))
5268 (case-fold-search nil)
5269 (regexp (format org-heading-keyword-regexp-format
5270 (cond
5271 ((and org-select-this-todo-keyword
5272 (equal org-select-this-todo-keyword "*"))
5273 org-todo-regexp)
5274 (org-select-this-todo-keyword
5275 (concat "\\("
5276 (mapconcat 'identity
5277 (org-split-string
5278 org-select-this-todo-keyword
5279 "|")
5280 "\\|") "\\)"))
5281 (t org-not-done-regexp))))
5282 marker priority category level tags todo-state
5283 ts-date ts-date-type ts-date-pair
5284 ee txt beg end inherited-tags todo-state-end-pos)
5285 (goto-char (point-min))
5286 (while (re-search-forward regexp nil t)
5287 (catch :skip
5288 (save-match-data
5289 (beginning-of-line)
5290 (org-agenda-skip)
5291 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5292 (unless (and (setq todo-state (org-get-todo-state))
5293 (setq todo-state-end-pos (match-end 2)))
5294 (goto-char end)
5295 (throw :skip nil))
5296 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5297 (goto-char (1+ beg))
5298 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5299 (throw :skip nil)))
5300 (goto-char (match-beginning 2))
5301 (setq marker (org-agenda-new-marker (match-beginning 0))
5302 category (org-get-category)
5303 ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
5304 ts-date (car ts-date-pair)
5305 ts-date-type (cdr ts-date-pair)
5306 txt (org-trim (buffer-substring (match-beginning 2) (match-end 0)))
5307 inherited-tags
5308 (or (eq org-agenda-show-inherited-tags 'always)
5309 (and (listp org-agenda-show-inherited-tags)
5310 (memq 'todo org-agenda-show-inherited-tags))
5311 (and (eq org-agenda-show-inherited-tags t)
5312 (or (eq org-agenda-use-tag-inheritance t)
5313 (memq 'todo org-agenda-use-tag-inheritance))))
5314 tags (org-get-tags-at nil (not inherited-tags))
5315 level (make-string (org-reduced-level (org-outline-level)) ? )
5316 txt (org-agenda-format-item "" txt level category tags t)
5317 priority (1+ (org-get-priority txt)))
5318 (org-add-props txt props
5319 'org-marker marker 'org-hd-marker marker
5320 'priority priority
5321 'level level
5322 'ts-date ts-date
5323 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5324 (push txt ee)
5325 (if org-agenda-todo-list-sublevels
5326 (goto-char todo-state-end-pos)
5327 (org-end-of-subtree 'invisible))))
5328 (nreverse ee)))
5330 (defun org-agenda-todo-custom-ignore-p (time n)
5331 "Check whether timestamp is farther away than n number of days.
5332 This function is invoked if `org-agenda-todo-ignore-deadlines',
5333 `org-agenda-todo-ignore-scheduled' or
5334 `org-agenda-todo-ignore-timestamp' is set to an integer."
5335 (let ((days (org-time-stamp-to-now
5336 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5337 (if (>= n 0)
5338 (>= days n)
5339 (<= days n))))
5341 ;;;###autoload
5342 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5343 (&optional end)
5344 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5345 (when (or org-agenda-todo-ignore-with-date
5346 org-agenda-todo-ignore-scheduled
5347 org-agenda-todo-ignore-deadlines
5348 org-agenda-todo-ignore-timestamp)
5349 (setq end (or end (save-excursion (outline-next-heading) (point))))
5350 (save-excursion
5351 (or (and org-agenda-todo-ignore-with-date
5352 (re-search-forward org-ts-regexp end t))
5353 (and org-agenda-todo-ignore-scheduled
5354 (re-search-forward org-scheduled-time-regexp end t)
5355 (cond
5356 ((eq org-agenda-todo-ignore-scheduled 'future)
5357 (> (org-time-stamp-to-now
5358 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5359 ((eq org-agenda-todo-ignore-scheduled 'past)
5360 (<= (org-time-stamp-to-now
5361 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5362 ((numberp org-agenda-todo-ignore-scheduled)
5363 (org-agenda-todo-custom-ignore-p
5364 (match-string 1) org-agenda-todo-ignore-scheduled))
5365 (t)))
5366 (and org-agenda-todo-ignore-deadlines
5367 (re-search-forward org-deadline-time-regexp end t)
5368 (cond
5369 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5370 ((eq org-agenda-todo-ignore-deadlines 'far)
5371 (not (org-deadline-close-p (match-string 1))))
5372 ((eq org-agenda-todo-ignore-deadlines 'future)
5373 (> (org-time-stamp-to-now
5374 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5375 ((eq org-agenda-todo-ignore-deadlines 'past)
5376 (<= (org-time-stamp-to-now
5377 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5378 ((numberp org-agenda-todo-ignore-deadlines)
5379 (org-agenda-todo-custom-ignore-p
5380 (match-string 1) org-agenda-todo-ignore-deadlines))
5381 (t (org-deadline-close-p (match-string 1)))))
5382 (and org-agenda-todo-ignore-timestamp
5383 (let ((buffer (current-buffer))
5384 (regexp
5385 (concat
5386 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5387 (start (point)))
5388 ;; Copy current buffer into a temporary one
5389 (with-temp-buffer
5390 (insert-buffer-substring buffer start end)
5391 (goto-char (point-min))
5392 ;; Delete SCHEDULED and DEADLINE items
5393 (while (re-search-forward regexp end t)
5394 (delete-region (match-beginning 0) (match-end 0)))
5395 (goto-char (point-min))
5396 ;; No search for timestamp left
5397 (when (re-search-forward org-ts-regexp nil t)
5398 (cond
5399 ((eq org-agenda-todo-ignore-timestamp 'future)
5400 (> (org-time-stamp-to-now
5401 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5402 ((eq org-agenda-todo-ignore-timestamp 'past)
5403 (<= (org-time-stamp-to-now
5404 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5405 ((numberp org-agenda-todo-ignore-timestamp)
5406 (org-agenda-todo-custom-ignore-p
5407 (match-string 1) org-agenda-todo-ignore-timestamp))
5408 (t))))))))))
5410 (defun org-agenda-get-timestamps (&optional deadlines)
5411 "Return the date stamp information for agenda display.
5412 Optional argument DEADLINES is a list of deadline items to be
5413 displayed in agenda view."
5414 (let* ((props (list 'face 'org-agenda-calendar-event
5415 'org-not-done-regexp org-not-done-regexp
5416 'org-todo-regexp org-todo-regexp
5417 'org-complex-heading-regexp org-complex-heading-regexp
5418 'mouse-face 'highlight
5419 'help-echo
5420 (format "mouse-2 or RET jump to Org file %s"
5421 (abbreviate-file-name buffer-file-name))))
5422 (current (calendar-absolute-from-gregorian date))
5423 (today (org-today))
5424 (deadline-position-alist
5425 (mapcar (lambda (d)
5426 (let ((m (get-text-property 0 'org-hd-marker d)))
5427 (and m (marker-position m))))
5428 deadlines))
5429 ;; Match time-stamps set to current date, time-stamps with
5430 ;; a repeater, and S-exp time-stamps.
5431 (regexp
5432 (concat
5433 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5434 (regexp-quote
5435 (substring
5436 (format-time-string
5437 (car org-time-stamp-formats)
5438 (apply #'encode-time ; DATE bound by calendar
5439 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5440 1 11))
5441 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5442 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5443 timestamp-items)
5444 (goto-char (point-min))
5445 (while (re-search-forward regexp nil t)
5446 ;; Skip date ranges, scheduled and deadlines, which are handled
5447 ;; specially. Also skip time-stamps before first headline as
5448 ;; there would be no entry to add to the agenda. Eventually,
5449 ;; ignore clock entries.
5450 (catch :skip
5451 (save-match-data
5452 (when (or (org-at-date-range-p)
5453 (org-at-planning-p)
5454 (org-before-first-heading-p)
5455 (and org-agenda-include-inactive-timestamps
5456 (org-at-clock-log-p)))
5457 (throw :skip nil))
5458 (org-agenda-skip))
5459 (let* ((pos (match-beginning 0))
5460 (repeat (match-string 1))
5461 (sexp-entry (match-string 3))
5462 (time-stamp (if (or repeat sexp-entry) (match-string 0)
5463 (save-excursion
5464 (goto-char pos)
5465 (looking-at org-ts-regexp-both)
5466 (match-string 0))))
5467 (todo-state (org-get-todo-state))
5468 (warntime (get-text-property (point) 'org-appt-warntime))
5469 (done? (member todo-state org-done-keywords)))
5470 ;; Possibly skip done tasks.
5471 (when (and done? org-agenda-skip-timestamp-if-done)
5472 (throw :skip t))
5473 ;; S-exp entry doesn't match current day: skip it.
5474 (when (and sexp-entry (not (org-diary-sexp-entry sexp-entry "" date)))
5475 (throw :skip nil))
5476 (when repeat
5477 (let* ((past
5478 ;; A repeating time stamp is shown at its base
5479 ;; date and every repeated date up to TODAY. If
5480 ;; `org-agenda-prefer-last-repeat' is non-nil,
5481 ;; however, only the last repeat before today
5482 ;; (inclusive) is shown.
5483 (org-agenda--timestamp-to-absolute
5484 repeat
5485 (if (or (> current today)
5486 (eq org-agenda-prefer-last-repeat t)
5487 (member todo-state org-agenda-prefer-last-repeat))
5488 today
5489 current)
5490 'past (current-buffer) pos))
5491 (future
5492 ;; Display every repeated date past TODAY
5493 ;; (exclusive) unless
5494 ;; `org-agenda-show-future-repeats' is nil. If
5495 ;; this variable is set to `next', only display
5496 ;; the first repeated date after TODAY
5497 ;; (exclusive).
5498 (cond
5499 ((<= current today) past)
5500 ((not org-agenda-show-future-repeats) past)
5502 (let ((base (if (eq org-agenda-show-future-repeats 'next)
5503 (1+ today)
5504 current)))
5505 (org-agenda--timestamp-to-absolute
5506 repeat base 'future (current-buffer) pos))))))
5507 (when (and (/= current past) (/= current future))
5508 (throw :skip nil))))
5509 (save-excursion
5510 (re-search-backward org-outline-regexp-bol nil t)
5511 ;; Possibly skip time-stamp when a deadline is set.
5512 (when (and org-agenda-skip-timestamp-if-deadline-is-shown
5513 (assq (point) deadline-position-alist))
5514 (throw :skip nil))
5515 (let* ((category (org-get-category pos))
5516 (inherited-tags
5517 (or (eq org-agenda-show-inherited-tags 'always)
5518 (and (consp org-agenda-show-inherited-tags)
5519 (memq 'agenda org-agenda-show-inherited-tags))
5520 (and (eq org-agenda-show-inherited-tags t)
5521 (or (eq org-agenda-use-tag-inheritance t)
5522 (memq 'agenda
5523 org-agenda-use-tag-inheritance)))))
5524 (tags (org-get-tags-at nil (not inherited-tags)))
5525 (level (make-string (org-reduced-level (org-outline-level))
5526 ?\s))
5527 (head (and (looking-at "\\*+[ \t]+\\(.*\\)")
5528 (match-string 1)))
5529 (inactive? (= (char-after pos) ?\[))
5530 (habit? (and (fboundp 'org-is-habit-p) (org-is-habit-p)))
5531 (item
5532 (org-agenda-format-item
5533 (and inactive? org-agenda-inactive-leader)
5534 head level category tags time-stamp org-ts-regexp habit?)))
5535 (org-add-props item props
5536 'priority (if habit?
5537 (org-habit-get-priority (org-habit-parse-todo))
5538 (org-get-priority item))
5539 'org-marker (org-agenda-new-marker pos)
5540 'org-hd-marker (org-agenda-new-marker)
5541 'date date
5542 'level level
5543 'ts-date (if repeat (org-agenda--timestamp-to-absolute repeat)
5544 current)
5545 'todo-state todo-state
5546 'warntime warntime
5547 'type "timestamp")
5548 (push item timestamp-items))))
5549 (when org-agenda-skip-additional-timestamps-same-entry
5550 (outline-next-heading))))
5551 (nreverse timestamp-items)))
5553 (defun org-agenda-get-sexps ()
5554 "Return the sexp information for agenda display."
5555 (require 'diary-lib)
5556 (let* ((props (list 'face 'org-agenda-calendar-sexp
5557 'mouse-face 'highlight
5558 'help-echo
5559 (format "mouse-2 or RET jump to org file %s"
5560 (abbreviate-file-name buffer-file-name))))
5561 (regexp "^&?%%(")
5562 marker category extra level ee txt tags entry
5563 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5564 (goto-char (point-min))
5565 (while (re-search-forward regexp nil t)
5566 (catch :skip
5567 (org-agenda-skip)
5568 (setq beg (match-beginning 0))
5569 (goto-char (1- (match-end 0)))
5570 (setq b (point))
5571 (forward-sexp 1)
5572 (setq sexp (buffer-substring b (point)))
5573 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5574 (org-trim (match-string 1))
5575 ""))
5576 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5577 (when result
5578 (setq marker (org-agenda-new-marker beg)
5579 level (make-string (org-reduced-level (org-outline-level)) ? )
5580 category (org-get-category beg)
5581 inherited-tags
5582 (or (eq org-agenda-show-inherited-tags 'always)
5583 (and (listp org-agenda-show-inherited-tags)
5584 (memq 'agenda org-agenda-show-inherited-tags))
5585 (and (eq org-agenda-show-inherited-tags t)
5586 (or (eq org-agenda-use-tag-inheritance t)
5587 (memq 'agenda org-agenda-use-tag-inheritance))))
5588 tags (org-get-tags-at nil (not inherited-tags))
5589 todo-state (org-get-todo-state)
5590 warntime (get-text-property (point) 'org-appt-warntime)
5591 extra nil)
5593 (dolist (r (if (stringp result)
5594 (list result)
5595 result)) ;; we expect a list here
5596 (when (and org-agenda-diary-sexp-prefix
5597 (string-match org-agenda-diary-sexp-prefix r))
5598 (setq extra (match-string 0 r)
5599 r (replace-match "" nil nil r)))
5600 (if (string-match "\\S-" r)
5601 (setq txt r)
5602 (setq txt "SEXP entry returned empty string"))
5603 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5604 (org-add-props txt props 'org-marker marker
5605 'date date 'todo-state todo-state
5606 'level level 'type "sexp" 'warntime warntime)
5607 (push txt ee)))))
5608 (nreverse ee)))
5610 ;; Calendar sanity: define some functions that are independent of
5611 ;; `calendar-date-style'.
5612 (defun org-anniversary (year month day &optional mark)
5613 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5614 (with-no-warnings
5615 (let ((calendar-date-style 'iso))
5616 (diary-anniversary year month day mark))))
5617 (defun org-cyclic (N year month day &optional mark)
5618 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5619 (with-no-warnings
5620 (let ((calendar-date-style 'iso))
5621 (diary-cyclic N year month day mark))))
5622 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5623 "Like `diary-block', but with fixed (ISO) order of arguments."
5624 (with-no-warnings
5625 (let ((calendar-date-style 'iso))
5626 (diary-block Y1 M1 D1 Y2 M2 D2 mark))))
5627 (defun org-date (year month day &optional mark)
5628 "Like `diary-date', but with fixed (ISO) order of arguments."
5629 (with-no-warnings
5630 (let ((calendar-date-style 'iso))
5631 (diary-date year month day mark))))
5633 ;; Define the `org-class' function
5634 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5635 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5636 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5637 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5638 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5639 `holidays', then any date that is known by the Emacs calendar to be a
5640 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5641 then those holidays will be skipped."
5642 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5643 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5644 (d (calendar-absolute-from-gregorian date))
5645 (h (when skip-weeks (calendar-check-holidays date))))
5646 (and
5647 (<= date1 d)
5648 (<= d date2)
5649 (= (calendar-day-of-week date) dayname)
5650 (or (not skip-weeks)
5651 (progn
5652 (require 'cal-iso)
5653 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5654 (not (or (and h (memq 'holidays skip-weeks))
5655 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5656 entry)))
5658 (defalias 'org-get-closed 'org-agenda-get-progress)
5659 (defun org-agenda-get-progress ()
5660 "Return the logged TODO entries for agenda display."
5661 (let* ((props (list 'mouse-face 'highlight
5662 'org-not-done-regexp org-not-done-regexp
5663 'org-todo-regexp org-todo-regexp
5664 'org-complex-heading-regexp org-complex-heading-regexp
5665 'help-echo
5666 (format "mouse-2 or RET jump to org file %s"
5667 (abbreviate-file-name buffer-file-name))))
5668 (items (if (consp org-agenda-show-log-scoped)
5669 org-agenda-show-log-scoped
5670 (if (eq org-agenda-show-log-scoped 'clockcheck)
5671 '(clock)
5672 org-agenda-log-mode-items)))
5673 (parts
5674 (delq nil
5675 (list
5676 (if (memq 'closed items) (concat "\\<" org-closed-string))
5677 (if (memq 'clock items) (concat "\\<" org-clock-string))
5678 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5679 (parts-re (if parts (mapconcat 'identity parts "\\|")
5680 (error "`org-agenda-log-mode-items' is empty")))
5681 (regexp (concat
5682 "\\(" parts-re "\\)"
5683 " *\\["
5684 (regexp-quote
5685 (substring
5686 (format-time-string
5687 (car org-time-stamp-formats)
5688 (apply 'encode-time ; DATE bound by calendar
5689 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5690 1 11))))
5691 (org-agenda-search-headline-for-time nil)
5692 marker hdmarker priority category level tags closedp
5693 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5694 (goto-char (point-min))
5695 (while (re-search-forward regexp nil t)
5696 (catch :skip
5697 (org-agenda-skip)
5698 (setq marker (org-agenda-new-marker (match-beginning 0))
5699 closedp (equal (match-string 1) org-closed-string)
5700 statep (equal (string-to-char (match-string 1)) ?-)
5701 clockp (not (or closedp statep))
5702 state (and statep (match-string 2))
5703 category (org-get-category (match-beginning 0))
5704 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5705 (when (string-match "\\]" timestr)
5706 ;; substring should only run to end of time stamp
5707 (setq rest (substring timestr (match-end 0))
5708 timestr (substring timestr 0 (match-end 0)))
5709 (if (and (not closedp) (not statep)
5710 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5711 rest))
5712 (progn (setq timestr (concat (substring timestr 0 -1)
5713 "-" (match-string 1 rest) "]"))
5714 (setq clocked (match-string 2 rest)))
5715 (setq clocked "-")))
5716 (save-excursion
5717 (setq extra
5718 (cond
5719 ((not org-agenda-log-mode-add-notes) nil)
5720 (statep
5721 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5722 (match-string 1)))
5723 (clockp
5724 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5725 (match-string 1)))))
5726 (if (not (re-search-backward org-outline-regexp-bol nil t))
5727 (throw :skip nil)
5728 (goto-char (match-beginning 0))
5729 (setq hdmarker (org-agenda-new-marker)
5730 inherited-tags
5731 (or (eq org-agenda-show-inherited-tags 'always)
5732 (and (listp org-agenda-show-inherited-tags)
5733 (memq 'todo org-agenda-show-inherited-tags))
5734 (and (eq org-agenda-show-inherited-tags t)
5735 (or (eq org-agenda-use-tag-inheritance t)
5736 (memq 'todo org-agenda-use-tag-inheritance))))
5737 tags (org-get-tags-at nil (not inherited-tags))
5738 level (make-string (org-reduced-level (org-outline-level)) ? ))
5739 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5740 (setq txt (match-string 1))
5741 (when extra
5742 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5743 (setq txt (concat (substring txt 0 (match-beginning 1))
5744 " - " extra " " (match-string 2 txt)))
5745 (setq txt (concat txt " - " extra))))
5746 (setq txt (org-agenda-format-item
5747 (cond
5748 (closedp "Closed: ")
5749 (statep (concat "State: (" state ")"))
5750 (t (concat "Clocked: (" clocked ")")))
5751 txt level category tags timestr)))
5752 (setq priority 100000)
5753 (org-add-props txt props
5754 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5755 'priority priority 'level level
5756 'type "closed" 'date date
5757 'undone-face 'org-warning 'done-face 'org-agenda-done)
5758 (push txt ee))
5759 (goto-char (point-at-eol))))
5760 (nreverse ee)))
5762 (defun org-agenda-show-clocking-issues ()
5763 "Add overlays, showing issues with clocking.
5764 See also the user option `org-agenda-clock-consistency-checks'."
5765 (interactive)
5766 (let* ((pl org-agenda-clock-consistency-checks)
5767 (re (concat "^[ \t]*"
5768 org-clock-string
5769 "[ \t]+"
5770 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5771 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5772 (tlstart 0.)
5773 (tlend 0.)
5774 (maxtime (org-duration-to-minutes
5775 (or (plist-get pl :max-duration) "24:00")))
5776 (mintime (org-duration-to-minutes
5777 (or (plist-get pl :min-duration) 0)))
5778 (maxgap (org-duration-to-minutes
5779 ;; default 30:00 means never complain
5780 (or (plist-get pl :max-gap) "30:00")))
5781 (gapok (mapcar #'org-duration-to-minutes
5782 (plist-get pl :gap-ok-around)))
5783 (def-face (or (plist-get pl :default-face)
5784 '((:background "DarkRed") (:foreground "white"))))
5785 issue face m te ts dt ov)
5786 (goto-char (point-min))
5787 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5788 (setq issue nil face def-face)
5789 (catch 'next
5790 (setq m (org-get-at-bol 'org-marker)
5791 te nil ts nil)
5792 (unless (and m (markerp m))
5793 (setq issue "No valid clock line") (throw 'next t))
5794 (org-with-point-at m
5795 (save-excursion
5796 (goto-char (point-at-bol))
5797 (unless (looking-at re)
5798 (error "No valid Clock line")
5799 (throw 'next t))
5800 (unless (match-end 3)
5801 (setq issue "No end time"
5802 face (or (plist-get pl :no-end-time-face) face))
5803 (throw 'next t))
5804 (setq ts (match-string 1)
5805 te (match-string 3)
5806 ts (float-time
5807 (apply #'encode-time (org-parse-time-string ts)))
5808 te (float-time
5809 (apply #'encode-time (org-parse-time-string te)))
5810 dt (- te ts))))
5811 (cond
5812 ((> dt (* 60 maxtime))
5813 ;; a very long clocking chunk
5814 (setq issue (format "Clocking interval is very long: %s"
5815 (org-duration-from-minutes (floor (/ dt 60.))))
5816 face (or (plist-get pl :long-face) face)))
5817 ((< dt (* 60 mintime))
5818 ;; a very short clocking chunk
5819 (setq issue (format "Clocking interval is very short: %s"
5820 (org-duration-from-minutes (floor (/ dt 60.))))
5821 face (or (plist-get pl :short-face) face)))
5822 ((and (> tlend 0) (< ts tlend))
5823 ;; Two clock entries are overlapping
5824 (setq issue (format "Clocking overlap: %d minutes"
5825 (/ (- tlend ts) 60))
5826 face (or (plist-get pl :overlap-face) face)))
5827 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5828 ;; There is a gap, lets see if we need to report it
5829 (unless (org-agenda-check-clock-gap tlend ts gapok)
5830 (setq issue (format "Clocking gap: %d minutes"
5831 (/ (- ts tlend) 60))
5832 face (or (plist-get pl :gap-face) face))))
5833 (t nil)))
5834 (setq tlend (or te tlend) tlstart (or ts tlstart))
5835 (when issue
5836 ;; OK, there was some issue, add an overlay to show the issue
5837 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5838 (overlay-put ov 'before-string
5839 (concat
5840 (org-add-props
5841 (format "%-43s" (concat " " issue))
5843 'face face)
5844 "\n"))
5845 (overlay-put ov 'evaporate t)))))
5847 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5848 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5849 (catch 'exit
5850 (unless ok-list
5851 ;; there are no OK times for gaps...
5852 (throw 'exit nil))
5853 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5854 ;; This is more than 24 hours, so it is OK.
5855 ;; because we have at least one OK time, that must be in the
5856 ;; 24 hour interval.
5857 (throw 'exit t))
5858 ;; We have a shorter gap.
5859 ;; Now we have to get the minute of the day when these times are
5860 (let* ((t1dec (decode-time (seconds-to-time t1)))
5861 (t2dec (decode-time (seconds-to-time t2)))
5862 ;; compute the minute on the day
5863 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5864 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5865 (when (< min2 min1)
5866 ;; if min2 is smaller than min1, this means it is on the next day.
5867 ;; Wrap it to after midnight.
5868 (setq min2 (+ min2 1440)))
5869 ;; Now check if any of the OK times is in the gap
5870 (mapc (lambda (x)
5871 ;; Wrap the time to after midnight if necessary
5872 (if (< x min1) (setq x (+ x 1440)))
5873 ;; Check if in interval
5874 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5875 ok-list)
5876 ;; Nope, this gap is not OK
5877 nil)))
5879 (defun org-agenda-get-deadlines (&optional with-hour)
5880 "Return the deadline information for agenda display.
5881 When WITH-HOUR is non-nil, only return deadlines with an hour
5882 specification like [h]h:mm."
5883 (let* ((props (list 'mouse-face 'highlight
5884 'org-not-done-regexp org-not-done-regexp
5885 'org-todo-regexp org-todo-regexp
5886 'org-complex-heading-regexp org-complex-heading-regexp
5887 'help-echo
5888 (format "mouse-2 or RET jump to org file %s"
5889 (abbreviate-file-name buffer-file-name))))
5890 (regexp (if with-hour
5891 org-deadline-time-hour-regexp
5892 org-deadline-time-regexp))
5893 (today (org-today))
5894 (today? (org-agenda-today-p date)) ; DATE bound by calendar.
5895 (current (calendar-absolute-from-gregorian date))
5896 deadline-items)
5897 (goto-char (point-min))
5898 (while (re-search-forward regexp nil t)
5899 (catch :skip
5900 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
5901 (org-agenda-skip)
5902 (let* ((s (match-string 1))
5903 (pos (1- (match-beginning 1)))
5904 (todo-state (save-match-data (org-get-todo-state)))
5905 (done? (member todo-state org-done-keywords))
5906 (sexp? (string-prefix-p "%%" s))
5907 ;; DEADLINE is the deadline date for the entry. It is
5908 ;; either the base date or the last repeat, according
5909 ;; to `org-agenda-prefer-last-repeat'.
5910 (deadline
5911 (cond
5912 (sexp? (org-agenda--timestamp-to-absolute s current))
5913 ((or (eq org-agenda-prefer-last-repeat t)
5914 (member todo-state org-agenda-prefer-last-repeat))
5915 (org-agenda--timestamp-to-absolute
5916 s today 'past (current-buffer) pos))
5917 (t (org-agenda--timestamp-to-absolute s))))
5918 ;; REPEAT is the future repeat closest from CURRENT,
5919 ;; according to `org-agenda-show-future-repeats'. If
5920 ;; the latter is nil, or if the time stamp has no
5921 ;; repeat part, default to DEADLINE.
5922 (repeat
5923 (cond
5924 (sexp? deadline)
5925 ((<= current today) deadline)
5926 ((not org-agenda-show-future-repeats) deadline)
5928 (let ((base (if (eq org-agenda-show-future-repeats 'next)
5929 (1+ today)
5930 current)))
5931 (org-agenda--timestamp-to-absolute
5932 s base 'future (current-buffer) pos)))))
5933 (diff (- deadline current))
5934 (suppress-prewarning
5935 (let ((scheduled
5936 (and org-agenda-skip-deadline-prewarning-if-scheduled
5937 (org-entry-get nil "SCHEDULED"))))
5938 (cond
5939 ((not scheduled) nil)
5940 ;; The current item has a scheduled date, so
5941 ;; evaluate its prewarning lead time.
5942 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
5943 ;; Use global prewarning-restart lead time.
5944 org-agenda-skip-deadline-prewarning-if-scheduled)
5945 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
5946 'pre-scheduled)
5947 ;; Set pre-warning to no earlier than SCHEDULED.
5948 (min (- deadline
5949 (org-agenda--timestamp-to-absolute scheduled))
5950 org-deadline-warning-days))
5951 ;; Set pre-warning to deadline.
5952 (t 0))))
5953 (wdays (if suppress-prewarning
5954 (let ((org-deadline-warning-days suppress-prewarning))
5955 (org-get-wdays s))
5956 (org-get-wdays s))))
5957 ;; Display deadlines items at base date (DEADLINE), today,
5958 ;; if deadline is overdue or if the expiration of the
5959 ;; upcoming deadline is within WDAYS warning time. Also,
5960 ;; show any repeat past today.
5961 (when (or (and (/= current deadline)
5962 (/= current today)
5963 (/= current repeat))
5964 (and today?
5965 (> deadline current)
5966 (> diff wdays)))
5967 (throw :skip nil))
5968 ;; Possibly skip done tasks.
5969 (when (and done?
5970 (or org-agenda-skip-deadline-if-done
5971 (/= deadline current)))
5972 (throw :skip nil))
5973 (save-excursion
5974 (re-search-backward "^\\*+[ \t]+" nil t)
5975 (goto-char (match-end 0))
5976 (let* ((category (org-get-category))
5977 (level (make-string (org-reduced-level (org-outline-level))
5978 ?\s))
5979 (head (buffer-substring (point) (line-end-position)))
5980 (inherited-tags
5981 (or (eq org-agenda-show-inherited-tags 'always)
5982 (and (listp org-agenda-show-inherited-tags)
5983 (memq 'agenda org-agenda-show-inherited-tags))
5984 (and (eq org-agenda-show-inherited-tags t)
5985 (or (eq org-agenda-use-tag-inheritance t)
5986 (memq 'agenda
5987 org-agenda-use-tag-inheritance)))))
5988 (tags (org-get-tags-at nil (not inherited-tags)))
5989 (time
5990 (cond
5991 ;; No time of day designation if it is only
5992 ;; a reminder.
5993 ((and (/= current deadline) (/= current repeat)) nil)
5994 ((string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5995 (concat (substring s (match-beginning 1)) " "))
5996 (t 'time)))
5997 (item
5998 (org-agenda-format-item
5999 ;; Insert appropriate suffixes before deadlines.
6000 ;; Those only apply to today agenda.
6001 (pcase-let ((`(,now ,future ,past)
6002 org-agenda-deadline-leaders))
6003 (cond
6004 ((and today? (< deadline today)) (format past (- diff)))
6005 ((and today? (> deadline today)) (format future diff))
6006 (t now)))
6007 head level category tags time))
6008 (face (org-agenda-deadline-face
6009 (- 1 (/ (float diff) (max wdays 1)))))
6010 (upcoming? (and today? (> deadline today)))
6011 (warntime (get-text-property (point) 'org-appt-warntime)))
6012 (org-add-props item props
6013 'org-marker (org-agenda-new-marker pos)
6014 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
6015 'warntime warntime
6016 'level level
6017 'ts-date deadline
6018 'priority
6019 ;; Adjust priority to today reminders about deadlines.
6020 ;; Overdue deadlines get the highest priority
6021 ;; increase, then imminent deadlines and eventually
6022 ;; more distant deadlines.
6023 (let ((adjust (if today? (- diff) 0)))
6024 (+ adjust (org-get-priority item)))
6025 'todo-state todo-state
6026 'type (if upcoming? "upcoming-deadline" "deadline")
6027 'date (if upcoming? date deadline)
6028 'face (if done? 'org-agenda-done face)
6029 'undone-face face
6030 'done-face 'org-agenda-done)
6031 (push item deadline-items))))))
6032 (nreverse deadline-items)))
6034 (defun org-agenda-deadline-face (fraction)
6035 "Return the face to displaying a deadline item.
6036 FRACTION is what fraction of the head-warning time has passed."
6037 (assoc-default fraction org-agenda-deadline-faces #'<=))
6039 (defun org-agenda-get-scheduled (&optional deadlines with-hour)
6040 "Return the scheduled information for agenda display.
6041 Optional argument DEADLINES is a list of deadline items to be
6042 displayed in agenda view. When WITH-HOUR is non-nil, only return
6043 scheduled items with an hour specification like [h]h:mm."
6044 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
6045 'org-todo-regexp org-todo-regexp
6046 'org-complex-heading-regexp org-complex-heading-regexp
6047 'done-face 'org-agenda-done
6048 'mouse-face 'highlight
6049 'help-echo
6050 (format "mouse-2 or RET jump to Org file %s"
6051 (abbreviate-file-name buffer-file-name))))
6052 (regexp (if with-hour
6053 org-scheduled-time-hour-regexp
6054 org-scheduled-time-regexp))
6055 (today (org-today))
6056 (todayp (org-agenda-today-p date)) ; DATE bound by calendar.
6057 (current (calendar-absolute-from-gregorian date))
6058 (deadline-pos
6059 (mapcar (lambda (d)
6060 (let ((m (get-text-property 0 'org-hd-marker d)))
6061 (and m (marker-position m))))
6062 deadlines))
6063 scheduled-items)
6064 (goto-char (point-min))
6065 (while (re-search-forward regexp nil t)
6066 (catch :skip
6067 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
6068 (org-agenda-skip)
6069 (let* ((s (match-string 1))
6070 (pos (1- (match-beginning 1)))
6071 (todo-state (save-match-data (org-get-todo-state)))
6072 (donep (member todo-state org-done-keywords))
6073 (sexp? (string-prefix-p "%%" s))
6074 ;; SCHEDULE is the scheduled date for the entry. It is
6075 ;; either the bare date or the last repeat, according
6076 ;; to `org-agenda-prefer-last-repeat'.
6077 (schedule
6078 (cond
6079 (sexp? (org-agenda--timestamp-to-absolute s current))
6080 ((or (eq org-agenda-prefer-last-repeat t)
6081 (member todo-state org-agenda-prefer-last-repeat))
6082 (org-agenda--timestamp-to-absolute
6083 s today 'past (current-buffer) pos))
6084 (t (org-agenda--timestamp-to-absolute s))))
6085 ;; REPEAT is the future repeat closest from CURRENT,
6086 ;; according to `org-agenda-show-future-repeats'. If
6087 ;; the latter is nil, or if the time stamp has no
6088 ;; repeat part, default to SCHEDULE.
6089 (repeat
6090 (cond
6091 (sexp? schedule)
6092 ((<= current today) schedule)
6093 ((not org-agenda-show-future-repeats) schedule)
6095 (let ((base (if (eq org-agenda-show-future-repeats 'next)
6096 (1+ today)
6097 current)))
6098 (org-agenda--timestamp-to-absolute
6099 s base 'future (current-buffer) pos)))))
6100 (diff (- current schedule))
6101 (warntime (get-text-property (point) 'org-appt-warntime))
6102 (pastschedp (< schedule today))
6103 (habitp (and (fboundp 'org-is-habit-p) (org-is-habit-p)))
6104 (suppress-delay
6105 (let ((deadline (and org-agenda-skip-scheduled-delay-if-deadline
6106 (org-entry-get nil "DEADLINE"))))
6107 (cond
6108 ((not deadline) nil)
6109 ;; The current item has a deadline date, so
6110 ;; evaluate its delay time.
6111 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6112 ;; Use global delay time.
6113 (- org-agenda-skip-scheduled-delay-if-deadline))
6114 ((eq org-agenda-skip-scheduled-delay-if-deadline
6115 'post-deadline)
6116 ;; Set delay to no later than DEADLINE.
6117 (min (- schedule
6118 (org-agenda--timestamp-to-absolute deadline))
6119 org-scheduled-delay-days))
6120 (t 0))))
6121 (ddays
6122 (cond
6123 ;; Nullify delay when a repeater triggered already
6124 ;; and the delay is of the form --Xd.
6125 ((and (string-match-p "--[0-9]+[hdwmy]" s)
6126 (> current schedule))
6128 (suppress-delay
6129 (let ((org-scheduled-delay-days suppress-delay))
6130 (org-get-wdays s t t)))
6131 (t (org-get-wdays s t)))))
6132 ;; Display scheduled items at base date (SCHEDULE), today if
6133 ;; scheduled before the current date, and at any repeat past
6134 ;; today. However, skip delayed items and items that have
6135 ;; been displayed for more than `org-scheduled-past-days'.
6136 (unless (and todayp
6137 habitp
6138 (bound-and-true-p org-habit-show-all-today))
6139 (when (or (and (> ddays 0) (< diff ddays))
6140 (> diff org-scheduled-past-days)
6141 (> schedule current)
6142 (and (/= current schedule)
6143 (/= current today)
6144 (/= current repeat)))
6145 (throw :skip nil)))
6146 ;; Possibly skip done tasks.
6147 (when (and donep
6148 (or org-agenda-skip-scheduled-if-done
6149 (/= schedule current)))
6150 (throw :skip nil))
6151 ;; Skip entry if it already appears as a deadline, per
6152 ;; `org-agenda-skip-scheduled-if-deadline-is-shown'. This
6153 ;; doesn't apply to habits.
6154 (when (pcase org-agenda-skip-scheduled-if-deadline-is-shown
6155 ((guard
6156 (or (not (memq (line-beginning-position 0) deadline-pos))
6157 habitp))
6158 nil)
6159 (`repeated-after-deadline
6160 (let ((deadline (time-to-days
6161 (org-get-deadline-time (point)))))
6162 (and (<= schedule deadline) (> current deadline))))
6163 (`not-today pastschedp)
6164 (`t t)
6165 (_ nil))
6166 (throw :skip nil))
6167 ;; Skip habits if `org-habit-show-habits' is nil, or if we
6168 ;; only show them for today. Also skip done habits.
6169 (when (and habitp
6170 (or donep
6171 (not (bound-and-true-p org-habit-show-habits))
6172 (and (not todayp)
6173 (bound-and-true-p
6174 org-habit-show-habits-only-for-today))))
6175 (throw :skip nil))
6176 (save-excursion
6177 (re-search-backward "^\\*+[ \t]+" nil t)
6178 (goto-char (match-end 0))
6179 (let* ((category (org-get-category))
6180 (inherited-tags
6181 (or (eq org-agenda-show-inherited-tags 'always)
6182 (and (listp org-agenda-show-inherited-tags)
6183 (memq 'agenda org-agenda-show-inherited-tags))
6184 (and (eq org-agenda-show-inherited-tags t)
6185 (or (eq org-agenda-use-tag-inheritance t)
6186 (memq 'agenda
6187 org-agenda-use-tag-inheritance)))))
6188 (tags (org-get-tags-at nil (not inherited-tags)))
6189 (level (make-string (org-reduced-level (org-outline-level))
6190 ?\s))
6191 (head (buffer-substring (point) (line-end-position)))
6192 (time
6193 (cond
6194 ;; No time of day designation if it is only
6195 ;; a reminder.
6196 ((and (/= current schedule) (/= current repeat)) nil)
6197 ((string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6198 (concat (substring s (match-beginning 1)) " "))
6199 (t 'time)))
6200 (item
6201 (org-agenda-format-item
6202 (pcase-let ((`(,first ,past) org-agenda-scheduled-leaders))
6203 ;; Show a reminder of a past scheduled today.
6204 (if (and todayp pastschedp)
6205 (format past (1+ diff))
6206 first))
6207 head level category tags time nil habitp))
6208 (face (cond ((and (not habitp) pastschedp)
6209 'org-scheduled-previously)
6210 (todayp 'org-scheduled-today)
6211 (t 'org-scheduled)))
6212 (habitp (and habitp (org-habit-parse-todo))))
6213 (org-add-props item props
6214 'undone-face face
6215 'face (if donep 'org-agenda-done face)
6216 'org-marker (org-agenda-new-marker pos)
6217 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
6218 'type (if pastschedp "past-scheduled" "scheduled")
6219 'date (if pastschedp schedule date)
6220 'ts-date schedule
6221 'warntime warntime
6222 'level level
6223 'priority (if habitp (org-habit-get-priority habitp)
6224 (+ 99 diff (org-get-priority item)))
6225 'org-habit-p habitp
6226 'todo-state todo-state)
6227 (push item scheduled-items))))))
6228 (nreverse scheduled-items)))
6230 (defun org-agenda-get-blocks ()
6231 "Return the date-range information for agenda display."
6232 (let* ((props (list 'face nil
6233 'org-not-done-regexp org-not-done-regexp
6234 'org-todo-regexp org-todo-regexp
6235 'org-complex-heading-regexp org-complex-heading-regexp
6236 'mouse-face 'highlight
6237 'help-echo
6238 (format "mouse-2 or RET jump to org file %s"
6239 (abbreviate-file-name buffer-file-name))))
6240 (regexp org-tr-regexp)
6241 (d0 (calendar-absolute-from-gregorian date))
6242 marker hdmarker ee txt d1 d2 s1 s2 category
6243 level todo-state tags pos head donep inherited-tags)
6244 (goto-char (point-min))
6245 (while (re-search-forward regexp nil t)
6246 (catch :skip
6247 (org-agenda-skip)
6248 (setq pos (point))
6249 (let ((start-time (match-string 1))
6250 (end-time (match-string 2)))
6251 (setq s1 (match-string 1)
6252 s2 (match-string 2)
6253 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
6254 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
6255 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6256 ;; Only allow days between the limits, because the normal
6257 ;; date stamps will catch the limits.
6258 (save-excursion
6259 (setq todo-state (org-get-todo-state))
6260 (setq donep (member todo-state org-done-keywords))
6261 (if (and donep org-agenda-skip-timestamp-if-done)
6262 (throw :skip t))
6263 (setq marker (org-agenda-new-marker (point))
6264 category (org-get-category))
6265 (if (not (re-search-backward org-outline-regexp-bol nil t))
6266 (throw :skip nil)
6267 (goto-char (match-beginning 0))
6268 (setq hdmarker (org-agenda-new-marker (point))
6269 inherited-tags
6270 (or (eq org-agenda-show-inherited-tags 'always)
6271 (and (listp org-agenda-show-inherited-tags)
6272 (memq 'agenda org-agenda-show-inherited-tags))
6273 (and (eq org-agenda-show-inherited-tags t)
6274 (or (eq org-agenda-use-tag-inheritance t)
6275 (memq 'agenda org-agenda-use-tag-inheritance))))
6277 tags (org-get-tags-at nil (not inherited-tags)))
6278 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6279 (looking-at "\\*+[ \t]+\\(.*\\)")
6280 (setq head (match-string 1))
6281 (let ((remove-re
6282 (if org-agenda-remove-timeranges-from-blocks
6283 (concat
6284 "<" (regexp-quote s1) ".*?>"
6285 "--"
6286 "<" (regexp-quote s2) ".*?>")
6287 nil)))
6288 (setq txt (org-agenda-format-item
6289 (format
6290 (nth (if (= d1 d2) 0 1)
6291 org-agenda-timerange-leaders)
6292 (1+ (- d0 d1)) (1+ (- d2 d1)))
6293 head level category tags
6294 (cond ((and (= d1 d0) (= d2 d0))
6295 (concat "<" start-time ">--<" end-time ">"))
6296 ((= d1 d0)
6297 (concat "<" start-time ">"))
6298 ((= d2 d0)
6299 (concat "<" end-time ">")))
6300 remove-re))))
6301 (org-add-props txt props
6302 'org-marker marker 'org-hd-marker hdmarker
6303 'type "block" 'date date
6304 'level level
6305 'todo-state todo-state
6306 'priority (org-get-priority txt))
6307 (push txt ee))))
6308 (goto-char pos)))
6309 ;; Sort the entries by expiration date.
6310 (nreverse ee)))
6312 ;;; Agenda presentation and sorting
6314 (defvar org-prefix-has-time nil
6315 "A flag, set by `org-compile-prefix-format'.
6316 The flag is set if the currently compiled format contains a `%t'.")
6317 (defvar org-prefix-has-tag nil
6318 "A flag, set by `org-compile-prefix-format'.
6319 The flag is set if the currently compiled format contains a `%T'.")
6320 (defvar org-prefix-has-effort nil
6321 "A flag, set by `org-compile-prefix-format'.
6322 The flag is set if the currently compiled format contains a `%e'.")
6323 (defvar org-prefix-has-breadcrumbs nil
6324 "A flag, set by `org-compile-prefix-format'.
6325 The flag is set if the currently compiled format contains a `%b'.")
6326 (defvar org-prefix-category-length nil
6327 "Used by `org-compile-prefix-format' to remember the category field width.")
6328 (defvar org-prefix-category-max-length nil
6329 "Used by `org-compile-prefix-format' to remember the category field width.")
6331 (defun org-agenda-get-category-icon (category)
6332 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6333 (cl-dolist (entry org-agenda-category-icon-alist)
6334 (when (string-match-p (car entry) category)
6335 (if (listp (cadr entry))
6336 (cl-return (cadr entry))
6337 (cl-return (apply #'create-image (cdr entry)))))))
6339 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6340 remove-re habitp)
6341 "Format TXT to be inserted into the agenda buffer.
6342 In particular, add the prefix and corresponding text properties.
6344 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6345 LEVEL may be a string to replace the `%l' specifier.
6346 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6347 category taken from local variable or file name. It will replace the `%c'
6348 specifier in the format.
6349 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6350 TXT for sorting of this entry, and for the `%t' specifier in the format.
6351 When DOTIME is a string, this string is searched for a time before TXT is.
6352 TAGS can be the tags of the headline.
6353 Any match of REMOVE-RE will be removed from TXT."
6354 ;; We keep the org-prefix-* variable values along with a compiled
6355 ;; formatter, so that multiple agendas existing at the same time do
6356 ;; not step on each other toes.
6358 ;; It was inconvenient to make these variables buffer local in
6359 ;; Agenda buffers, because this function expects to be called with
6360 ;; the buffer where item comes from being current, and not agenda
6361 ;; buffer
6362 (let* ((bindings (car org-prefix-format-compiled))
6363 (formatter (cadr org-prefix-format-compiled)))
6364 (cl-loop for (var value) in bindings
6365 do (set var value))
6366 (save-match-data
6367 ;; Diary entries sometimes have extra whitespace at the beginning
6368 (setq txt (org-trim txt))
6370 ;; Fix the tags part in txt
6371 (setq txt (org-agenda-fix-displayed-tags
6372 txt tags
6373 org-agenda-show-inherited-tags
6374 org-agenda-hide-tags-regexp))
6376 (let* ((category (or category
6377 (if buffer-file-name
6378 (file-name-sans-extension
6379 (file-name-nondirectory buffer-file-name))
6380 "")))
6381 (category-icon (org-agenda-get-category-icon category))
6382 (category-icon (if category-icon
6383 (propertize " " 'display category-icon)
6384 ""))
6385 (effort (and (not (string= txt ""))
6386 (get-text-property 1 'effort txt)))
6387 ;; time, tag, effort are needed for the eval of the prefix format
6388 (tag (if tags (nth (1- (length tags)) tags) ""))
6389 time
6390 (ts (if dotime (concat
6391 (if (stringp dotime) dotime "")
6392 (and org-agenda-search-headline-for-time txt))))
6393 (time-of-day (and dotime (org-get-time-of-day ts)))
6394 stamp plain s0 s1 s2 rtn srp l
6395 duration breadcrumbs)
6396 (and (derived-mode-p 'org-mode) buffer-file-name
6397 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6398 (when (and dotime time-of-day)
6399 ;; Extract starting and ending time and move them to prefix
6400 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6401 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6402 (setq s0 (match-string 0 ts)
6403 srp (and stamp (match-end 3))
6404 s1 (match-string (if plain 1 2) ts)
6405 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6407 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6408 ;; them, we might want to remove them there to avoid duplication.
6409 ;; The user can turn this off with a variable.
6410 (if (and org-prefix-has-time
6411 org-agenda-remove-times-when-in-prefix (or stamp plain)
6412 (string-match (concat (regexp-quote s0) " *") txt)
6413 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6414 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6415 (= (match-beginning 0) 0)
6417 (setq txt (replace-match "" nil nil txt))))
6418 ;; Normalize the time(s) to 24 hour
6419 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6420 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6422 ;; Try to set s2 if s1 and
6423 ;; `org-agenda-default-appointment-duration' are set
6424 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6425 (setq s2
6426 (org-duration-from-minutes
6427 (+ (org-duration-to-minutes s1 t)
6428 org-agenda-default-appointment-duration)
6429 nil t)))
6431 ;; Compute the duration
6432 (when s2
6433 (setq duration (- (org-duration-to-minutes s2)
6434 (org-duration-to-minutes s1)))))
6436 (when (string-match "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" txt)
6437 ;; Tags are in the string
6438 (if (or (eq org-agenda-remove-tags t)
6439 (and org-agenda-remove-tags
6440 org-prefix-has-tag))
6441 (setq txt (replace-match "" t t txt))
6442 (setq txt (replace-match
6443 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6444 (match-string 2 txt))
6445 t t txt))))
6447 (when remove-re
6448 (while (string-match remove-re txt)
6449 (setq txt (replace-match "" t t txt))))
6451 ;; Set org-heading property on `txt' to mark the start of the
6452 ;; heading.
6453 (add-text-properties 0 (length txt) '(org-heading t) txt)
6455 ;; Prepare the variables needed in the eval of the compiled format
6456 (if org-prefix-has-breadcrumbs
6457 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6458 (let ((s (org-display-outline-path nil nil "->" t)))
6459 (if (eq "" s) "" (concat s "->"))))))
6460 (setq time (cond (s2 (concat
6461 (org-agenda-time-of-day-to-ampm-maybe s1)
6462 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6463 (if org-agenda-timegrid-use-ampm " ")))
6464 (s1 (concat
6465 (org-agenda-time-of-day-to-ampm-maybe s1)
6466 (if org-agenda-timegrid-use-ampm
6467 "........ "
6468 "......")))
6469 (t ""))
6470 extra (or (and (not habitp) extra) "")
6471 category (if (symbolp category) (symbol-name category) category)
6472 level (or level ""))
6473 (if (string-match org-bracket-link-regexp category)
6474 (progn
6475 (setq l (if (match-end 3)
6476 (- (match-end 3) (match-beginning 3))
6477 (- (match-end 1) (match-beginning 1))))
6478 (when (< l (or org-prefix-category-length 0))
6479 (setq category (copy-sequence category))
6480 (org-add-props category nil
6481 'extra-space (make-string
6482 (- org-prefix-category-length l 1) ?\ ))))
6483 (if (and org-prefix-category-max-length
6484 (>= (length category) org-prefix-category-max-length))
6485 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6486 ;; Evaluate the compiled format
6487 (setq rtn (concat (eval formatter) txt))
6489 ;; And finally add the text properties
6490 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6491 (org-add-props rtn nil
6492 'org-category category
6493 'tags (mapcar 'org-downcase-keep-props tags)
6494 'org-highest-priority org-highest-priority
6495 'org-lowest-priority org-lowest-priority
6496 'time-of-day time-of-day
6497 'duration duration
6498 'breadcrumbs breadcrumbs
6499 'txt txt
6500 'level level
6501 'time time
6502 'extra extra
6503 'format org-prefix-format-compiled
6504 'dotime dotime)))))
6506 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6507 "Remove tags string from TXT, and add a modified list of tags.
6508 The modified list may contain inherited tags, and tags matched by
6509 `org-agenda-hide-tags-regexp' will be removed."
6510 (when (or add-inherited hide-re)
6511 (if (string-match "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" txt)
6512 (setq txt (substring txt 0 (match-beginning 0))))
6513 (setq tags
6514 (delq nil
6515 (mapcar (lambda (tg)
6516 (if (or (and hide-re (string-match hide-re tg))
6517 (and (not add-inherited)
6518 (get-text-property 0 'inherited tg)))
6520 tg))
6521 tags)))
6522 (when tags
6523 (let ((have-i (get-text-property 0 'inherited (car tags)))
6525 (setq txt (concat txt " :"
6526 (mapconcat
6527 (lambda (x)
6528 (setq i (get-text-property 0 'inherited x))
6529 (if (and have-i (not i))
6530 (progn
6531 (setq have-i nil)
6532 (concat ":" x))
6534 tags ":")
6535 (if have-i "::" ":"))))))
6536 txt)
6538 (defun org-downcase-keep-props (s)
6539 (let ((props (text-properties-at 0 s)))
6540 (setq s (downcase s))
6541 (add-text-properties 0 (length s) props s)
6544 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6546 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6547 "Add a time-grid for agenda items which need it.
6549 LIST is the list of agenda items formatted by `org-agenda-list'.
6550 NDAYS is the span of the current agenda view.
6551 TODAYP is t when the current agenda view is on today."
6552 (catch 'exit
6553 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6554 ((and todayp (member 'today (car org-agenda-time-grid))))
6555 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6556 ((member 'weekly (car org-agenda-time-grid)))
6557 (t (throw 'exit list)))
6558 (let* ((have (delq nil (mapcar
6559 (lambda (x) (get-text-property 1 'time-of-day x))
6560 list)))
6561 (string (nth 1 org-agenda-time-grid))
6562 (gridtimes (nth 2 org-agenda-time-grid))
6563 (req (car org-agenda-time-grid))
6564 (remove (member 'remove-match req))
6565 new time)
6566 (if (and (member 'require-timed req) (not have))
6567 ;; don't show empty grid
6568 (throw 'exit list))
6569 (while (setq time (pop gridtimes))
6570 (unless (and remove (member time have))
6571 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6572 (push (org-agenda-format-item
6573 nil string nil "" nil
6574 (concat (substring time 0 -2) ":" (substring time -2)))
6575 new)
6576 (put-text-property
6577 2 (length (car new)) 'face 'org-time-grid (car new))))
6578 (when (and todayp org-agenda-show-current-time-in-grid)
6579 (push (org-agenda-format-item
6580 nil org-agenda-current-time-string nil "" nil
6581 (format-time-string "%H:%M "))
6582 new)
6583 (put-text-property
6584 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6586 (if (member 'time-up org-agenda-sorting-strategy-selected)
6587 (append new list)
6588 (append list new)))))
6590 (defun org-compile-prefix-format (key)
6591 "Compile the prefix format into a Lisp form that can be evaluated.
6592 The resulting form and associated variable bindings is returned
6593 and stored in the variable `org-prefix-format-compiled'."
6594 (setq org-prefix-has-time nil
6595 org-prefix-has-tag nil
6596 org-prefix-category-length nil
6597 org-prefix-has-effort nil
6598 org-prefix-has-breadcrumbs nil)
6599 (let ((s (cond
6600 ((stringp org-agenda-prefix-format)
6601 org-agenda-prefix-format)
6602 ((assq key org-agenda-prefix-format)
6603 (cdr (assq key org-agenda-prefix-format)))
6604 (t " %-12:c%?-12t% s")))
6605 (start 0)
6606 varform vars var e c f opt)
6607 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6608 s start)
6609 (setq var (or (cdr (assoc (match-string 4 s)
6610 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6611 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6612 'eval)
6613 c (or (match-string 3 s) "")
6614 opt (match-beginning 1)
6615 start (1+ (match-beginning 0)))
6616 (if (eq var 'time) (setq org-prefix-has-time t))
6617 (if (eq var 'tag) (setq org-prefix-has-tag t))
6618 (if (eq var 'effort) (setq org-prefix-has-effort t))
6619 (if (eq var 'breadcrumbs) (setq org-prefix-has-breadcrumbs t))
6620 (setq f (concat "%" (match-string 2 s) "s"))
6621 (when (eq var 'category)
6622 (setq org-prefix-category-length
6623 (floor (abs (string-to-number (match-string 2 s)))))
6624 (setq org-prefix-category-max-length
6625 (let ((x (match-string 2 s)))
6626 (save-match-data
6627 (if (string-match "\\.[0-9]+" x)
6628 (string-to-number (substring (match-string 0 x) 1)))))))
6629 (if (eq var 'eval)
6630 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6631 (if opt
6632 (setq varform
6633 `(if (or (equal "" ,var) (equal nil ,var))
6635 (format ,f (concat ,var ,c))))
6636 (setq varform
6637 `(format ,f (if (or (equal ,var "")
6638 (equal ,var nil)) ""
6639 (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6640 (setq s (replace-match "%s" t nil s))
6641 (push varform vars))
6642 (setq vars (nreverse vars))
6643 (with-current-buffer (or org-agenda-buffer (current-buffer))
6644 (setq org-prefix-format-compiled
6645 (list
6646 `((org-prefix-has-time ,org-prefix-has-time)
6647 (org-prefix-has-tag ,org-prefix-has-tag)
6648 (org-prefix-category-length ,org-prefix-category-length)
6649 (org-prefix-has-effort ,org-prefix-has-effort)
6650 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6651 `(format ,s ,@vars))))))
6653 (defun org-set-sorting-strategy (key)
6654 (if (symbolp (car org-agenda-sorting-strategy))
6655 ;; the old format
6656 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6657 (setq org-agenda-sorting-strategy-selected
6658 (or (cdr (assq key org-agenda-sorting-strategy))
6659 (cdr (assq 'agenda org-agenda-sorting-strategy))
6660 '(time-up category-keep priority-down)))))
6662 (defun org-get-time-of-day (s &optional string mod24)
6663 "Check string S for a time of day.
6664 If found, return it as a military time number between 0 and 2400.
6665 If not found, return nil.
6666 The optional STRING argument forces conversion into a 5 character wide string
6667 HH:MM."
6668 (save-match-data
6669 (when
6670 (and
6671 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6672 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6673 (not (eq (get-text-property 1 'face s) 'org-link)))
6674 (let* ((h (string-to-number (match-string 1 s)))
6675 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6676 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6677 (am-p (equal ampm "am"))
6678 (h1 (cond ((not ampm) h)
6679 ((= h 12) (if am-p 0 12))
6680 (t (+ h (if am-p 0 12)))))
6681 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6682 (mod h1 24) h1))
6683 (t0 (+ (* 100 h2) m))
6684 (t1 (concat (if (>= h1 24) "+" " ")
6685 (if (and org-agenda-time-leading-zero
6686 (< t0 1000)) "0" "")
6687 (if (< t0 100) "0" "")
6688 (if (< t0 10) "0" "")
6689 (int-to-string t0))))
6690 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6692 (defvar org-agenda-before-sorting-filter-function nil
6693 "Function to be applied to agenda items prior to sorting.
6694 Prior to sorting also means just before they are inserted into the agenda.
6696 To aid sorting, you may revisit the original entries and add more text
6697 properties which will later be used by the sorting functions.
6699 The function should take a string argument, an agenda line.
6700 It has access to the text properties in that line, which contain among
6701 other things, the property `org-hd-marker' that points to the entry
6702 where the line comes from. Note that not all lines going into the agenda
6703 have this property, only most.
6705 The function should return the modified string. It is probably best
6706 to ONLY change text properties.
6708 You can also use this function as a filter, by returning nil for lines
6709 you don't want to have in the agenda at all. For this application, you
6710 could bind the variable in the options section of a custom command.")
6712 (defun org-agenda-finalize-entries (list &optional type)
6713 "Sort, limit and concatenate the LIST of agenda items.
6714 The optional argument TYPE tells the agenda type."
6715 (let ((max-effort (cond ((listp org-agenda-max-effort)
6716 (cdr (assoc type org-agenda-max-effort)))
6717 (t org-agenda-max-effort)))
6718 (max-todo (cond ((listp org-agenda-max-todos)
6719 (cdr (assoc type org-agenda-max-todos)))
6720 (t org-agenda-max-todos)))
6721 (max-tags (cond ((listp org-agenda-max-tags)
6722 (cdr (assoc type org-agenda-max-tags)))
6723 (t org-agenda-max-tags)))
6724 (max-entries (cond ((listp org-agenda-max-entries)
6725 (cdr (assoc type org-agenda-max-entries)))
6726 (t org-agenda-max-entries))))
6727 (when org-agenda-before-sorting-filter-function
6728 (setq list
6729 (delq nil
6730 (mapcar
6731 org-agenda-before-sorting-filter-function list))))
6732 (setq list (mapcar 'org-agenda-highlight-todo list)
6733 list (mapcar 'identity (sort list 'org-entries-lessp)))
6734 (when max-effort
6735 (setq list (org-agenda-limit-entries
6736 list 'effort-minutes max-effort
6737 (lambda (e) (or e (if org-sort-agenda-noeffort-is-high
6738 32767 -1))))))
6739 (when max-todo
6740 (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
6741 (when max-tags
6742 (setq list (org-agenda-limit-entries list 'tags max-tags)))
6743 (when max-entries
6744 (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
6745 (mapconcat 'identity list "\n")))
6747 (defun org-agenda-limit-entries (list prop limit &optional fn)
6748 "Limit the number of agenda entries."
6749 (let ((include (and limit (< limit 0))))
6750 (if limit
6751 (let ((fun (or fn (lambda (p) (if p 1))))
6752 (lim 0))
6753 (delq nil
6754 (mapcar
6755 (lambda (e)
6756 (let ((pval (funcall
6757 fun (get-text-property (1- (length e))
6758 prop e))))
6759 (if pval (setq lim (+ lim pval)))
6760 (cond ((and pval (<= lim (abs limit))) e)
6761 ((and include (not pval)) e))))
6762 list)))
6763 list)))
6765 (defun org-agenda-limit-interactively (remove)
6766 "In agenda, interactively limit entries to various maximums."
6767 (interactive "P")
6768 (if remove
6769 (progn (setq org-agenda-max-entries nil
6770 org-agenda-max-todos nil
6771 org-agenda-max-tags nil
6772 org-agenda-max-effort nil)
6773 (org-agenda-redo))
6774 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6775 (msg (cond ((= max ?E) "How many minutes? ")
6776 ((= max ?e) "How many entries? ")
6777 ((= max ?t) "How many TODO entries? ")
6778 ((= max ?T) "How many tagged entries? ")
6779 (t (user-error "Wrong input"))))
6780 (num (string-to-number (read-from-minibuffer msg))))
6781 (cond ((equal max ?e)
6782 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6783 ((equal max ?t)
6784 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6785 ((equal max ?T)
6786 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6787 ((equal max ?E)
6788 (let ((org-agenda-max-effort num)) (org-agenda-redo))))))
6789 (org-agenda-fit-window-to-buffer))
6791 (defun org-agenda-highlight-todo (x)
6792 (let ((org-done-keywords org-done-keywords-for-agenda)
6793 (case-fold-search nil)
6795 (if (eq x 'line)
6796 (save-excursion
6797 (beginning-of-line 1)
6798 (setq re (org-get-at-bol 'org-todo-regexp))
6799 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6800 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6801 (add-text-properties (match-beginning 0) (match-end 1)
6802 (list 'face (org-get-todo-face 1)))
6803 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6804 (delete-region (match-beginning 1) (1- (match-end 0)))
6805 (goto-char (match-beginning 1))
6806 (insert (format org-agenda-todo-keyword-format s)))))
6807 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6808 (setq re (get-text-property 0 'org-todo-regexp x))
6809 (when (and re
6810 ;; Test `pl' because if there's no heading content,
6811 ;; there's no point matching to highlight. Note
6812 ;; that if we didn't test `pl' first, and there
6813 ;; happened to be no keyword from `org-todo-regexp'
6814 ;; on this heading line, then the `equal' comparison
6815 ;; afterwards would spuriously succeed in the case
6816 ;; where `pl' is nil -- causing an args-out-of-range
6817 ;; error when we try to add text properties to text
6818 ;; that isn't there.
6820 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6821 x pl) pl))
6822 (add-text-properties
6823 (or (match-end 1) (match-end 0)) (match-end 0)
6824 (list 'face (org-get-todo-face (match-string 2 x)))
6826 (when (match-end 1)
6827 (setq x (concat (substring x 0 (match-end 1))
6828 (format org-agenda-todo-keyword-format
6829 (match-string 2 x))
6830 (org-add-props " " (text-properties-at 0 x))
6831 (substring x (match-end 3)))))))
6832 x)))
6834 (defsubst org-cmp-values (a b property)
6835 "Compare the numeric value of text PROPERTY for string A and B."
6836 (let ((pa (or (get-text-property (1- (length a)) property a) 0))
6837 (pb (or (get-text-property (1- (length b)) property b) 0)))
6838 (cond ((> pa pb) +1)
6839 ((< pa pb) -1))))
6841 (defsubst org-cmp-effort (a b)
6842 "Compare the effort values of string A and B."
6843 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6844 ;; `effort-minutes' property is not directly accessible from
6845 ;; the strings, but is stored as a property in `txt'.
6846 (ea (or (get-text-property
6847 0 'effort-minutes (get-text-property 0 'txt a))
6848 def))
6849 (eb (or (get-text-property
6850 0 'effort-minutes (get-text-property 0 'txt b))
6851 def)))
6852 (cond ((> ea eb) +1)
6853 ((< ea eb) -1))))
6855 (defsubst org-cmp-category (a b)
6856 "Compare the string values of categories of strings A and B."
6857 (let ((ca (or (get-text-property (1- (length a)) 'org-category a) ""))
6858 (cb (or (get-text-property (1- (length b)) 'org-category b) "")))
6859 (cond ((string-lessp ca cb) -1)
6860 ((string-lessp cb ca) +1))))
6862 (defsubst org-cmp-todo-state (a b)
6863 "Compare the todo states of strings A and B."
6864 (let* ((ma (or (get-text-property 1 'org-marker a)
6865 (get-text-property 1 'org-hd-marker a)))
6866 (mb (or (get-text-property 1 'org-marker b)
6867 (get-text-property 1 'org-hd-marker b)))
6868 (fa (and ma (marker-buffer ma)))
6869 (fb (and mb (marker-buffer mb)))
6870 (todo-kwds
6871 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6872 (and fb (with-current-buffer fb org-todo-keywords-1))))
6873 (ta (or (get-text-property 1 'todo-state a) ""))
6874 (tb (or (get-text-property 1 'todo-state b) ""))
6875 (la (- (length (member ta todo-kwds))))
6876 (lb (- (length (member tb todo-kwds))))
6877 (donepa (member ta org-done-keywords-for-agenda))
6878 (donepb (member tb org-done-keywords-for-agenda)))
6879 (cond ((and donepa (not donepb)) -1)
6880 ((and (not donepa) donepb) +1)
6881 ((< la lb) -1)
6882 ((< lb la) +1))))
6884 (defsubst org-cmp-alpha (a b)
6885 "Compare the headlines, alphabetically."
6886 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6887 (plb (text-property-any 0 (length b) 'org-heading t b))
6888 (ta (and pla (substring a pla)))
6889 (tb (and plb (substring b plb)))
6890 (case-fold-search nil))
6891 (when pla
6892 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6893 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6894 (setq ta (substring ta (match-end 0))))
6895 (setq ta (downcase ta)))
6896 (when plb
6897 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6898 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6899 (setq tb (substring tb (match-end 0))))
6900 (setq tb (downcase tb)))
6901 (cond ((not ta) +1)
6902 ((not tb) -1)
6903 ((string-lessp ta tb) -1)
6904 ((string-lessp tb ta) +1))))
6906 (defsubst org-cmp-tag (a b)
6907 "Compare the string values of the first tags of A and B."
6908 (let ((ta (car (last (get-text-property 1 'tags a))))
6909 (tb (car (last (get-text-property 1 'tags b)))))
6910 (cond ((not ta) +1)
6911 ((not tb) -1)
6912 ((string-lessp ta tb) -1)
6913 ((string-lessp tb ta) +1))))
6915 (defsubst org-cmp-time (a b)
6916 "Compare the time-of-day values of strings A and B."
6917 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6918 (ta (or (get-text-property 1 'time-of-day a) def))
6919 (tb (or (get-text-property 1 'time-of-day b) def)))
6920 (cond ((< ta tb) -1)
6921 ((< tb ta) +1))))
6923 (defsubst org-cmp-ts (a b type)
6924 "Compare the timestamps values of entries A and B.
6925 When TYPE is \"scheduled\", \"deadline\", \"timestamp\" or
6926 \"timestamp_ia\", compare within each of these type. When TYPE
6927 is the empty string, compare all timestamps without respect of
6928 their type."
6929 (let* ((def (if org-sort-agenda-notime-is-late most-positive-fixnum -1))
6930 (ta (or (and (string-match type (or (get-text-property 1 'type a) ""))
6931 (get-text-property 1 'ts-date a))
6932 def))
6933 (tb (or (and (string-match type (or (get-text-property 1 'type b) ""))
6934 (get-text-property 1 'ts-date b))
6935 def)))
6936 (cond ((< ta tb) -1)
6937 ((< tb ta) +1))))
6939 (defsubst org-cmp-habit-p (a b)
6940 "Compare the todo states of strings A and B."
6941 (let ((ha (get-text-property 1 'org-habit-p a))
6942 (hb (get-text-property 1 'org-habit-p b)))
6943 (cond ((and ha (not hb)) -1)
6944 ((and (not ha) hb) +1))))
6946 (defun org-entries-lessp (a b)
6947 "Predicate for sorting agenda entries."
6948 ;; The following variables will be used when the form is evaluated.
6949 ;; So even though the compiler complains, keep them.
6950 (let* ((ss org-agenda-sorting-strategy-selected)
6951 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
6952 (org-cmp-ts a b "")))
6953 (timestamp-down (if timestamp-up (- timestamp-up) nil))
6954 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
6955 (org-cmp-ts a b "scheduled")))
6956 (scheduled-down (if scheduled-up (- scheduled-up) nil))
6957 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
6958 (org-cmp-ts a b "deadline")))
6959 (deadline-down (if deadline-up (- deadline-up) nil))
6960 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
6961 (org-cmp-ts a b "timestamp_ia")))
6962 (tsia-down (if tsia-up (- tsia-up) nil))
6963 (ts-up (and (org-em 'ts-up 'ts-down ss)
6964 (org-cmp-ts a b "timestamp")))
6965 (ts-down (if ts-up (- ts-up) nil))
6966 (time-up (and (org-em 'time-up 'time-down ss)
6967 (org-cmp-time a b)))
6968 (time-down (if time-up (- time-up) nil))
6969 (stats-up (and (org-em 'stats-up 'stats-down ss)
6970 (org-cmp-values a b 'org-stats)))
6971 (stats-down (if stats-up (- stats-up) nil))
6972 (priority-up (and (org-em 'priority-up 'priority-down ss)
6973 (org-cmp-values a b 'priority)))
6974 (priority-down (if priority-up (- priority-up) nil))
6975 (effort-up (and (org-em 'effort-up 'effort-down ss)
6976 (org-cmp-effort a b)))
6977 (effort-down (if effort-up (- effort-up) nil))
6978 (category-up (and (or (org-em 'category-up 'category-down ss)
6979 (memq 'category-keep ss))
6980 (org-cmp-category a b)))
6981 (category-down (if category-up (- category-up) nil))
6982 (category-keep (if category-up +1 nil))
6983 (tag-up (and (org-em 'tag-up 'tag-down ss)
6984 (org-cmp-tag a b)))
6985 (tag-down (if tag-up (- tag-up) nil))
6986 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
6987 (org-cmp-todo-state a b)))
6988 (todo-state-down (if todo-state-up (- todo-state-up) nil))
6989 (habit-up (and (org-em 'habit-up 'habit-down ss)
6990 (org-cmp-habit-p a b)))
6991 (habit-down (if habit-up (- habit-up) nil))
6992 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
6993 (org-cmp-alpha a b)))
6994 (alpha-down (if alpha-up (- alpha-up) nil))
6995 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
6996 user-defined-up user-defined-down)
6997 (if (and need-user-cmp org-agenda-cmp-user-defined
6998 (functionp org-agenda-cmp-user-defined))
6999 (setq user-defined-up
7000 (funcall org-agenda-cmp-user-defined a b)
7001 user-defined-down (if user-defined-up (- user-defined-up) nil)))
7002 (cdr (assoc
7003 (eval (cons 'or org-agenda-sorting-strategy-selected))
7004 '((-1 . t) (1 . nil) (nil . nil))))))
7006 ;;; Agenda restriction lock
7008 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
7009 "Overlay to mark the headline to which agenda commands are restricted.")
7010 (overlay-put org-agenda-restriction-lock-overlay
7011 'face 'org-agenda-restriction-lock)
7012 (overlay-put org-agenda-restriction-lock-overlay
7013 'help-echo "Agendas are currently limited to this subtree.")
7014 (delete-overlay org-agenda-restriction-lock-overlay)
7016 (defun org-agenda-set-restriction-lock-from-agenda (arg)
7017 "Set the restriction lock to the agenda item at point from within the agenda.
7018 When called with a `\\[universal-argument]' prefix, restrict to
7019 the file which contains the item.
7020 Argument ARG is the prefix argument."
7021 (interactive "P")
7022 (unless (derived-mode-p 'org-agenda-mode)
7023 (user-error "Not in an Org agenda buffer"))
7024 (let* ((marker (or (org-get-at-bol 'org-marker)
7025 (org-agenda-error)))
7026 (buffer (marker-buffer marker))
7027 (pos (marker-position marker)))
7028 (with-current-buffer buffer
7029 (goto-char pos)
7030 (org-agenda-set-restriction-lock arg))))
7032 ;;;###autoload
7033 (defun org-agenda-set-restriction-lock (&optional type)
7034 "Set restriction lock for agenda, to current subtree or file.
7035 Restriction will be the file if TYPE is `file', or if type is the
7036 universal prefix \\='(4), or if the cursor is before the first headline
7037 in the file. Otherwise, restriction will be to the current subtree."
7038 (interactive "P")
7039 (org-agenda-remove-restriction-lock 'noupdate)
7040 (and (equal type '(4)) (setq type 'file))
7041 (setq type (cond
7042 (type type)
7043 ((org-at-heading-p) 'subtree)
7044 ((condition-case nil (org-back-to-heading t) (error nil))
7045 'subtree)
7046 (t 'file)))
7047 (if (eq type 'subtree)
7048 (progn
7049 (setq org-agenda-restrict (current-buffer))
7050 (setq org-agenda-overriding-restriction 'subtree)
7051 (put 'org-agenda-files 'org-restrict
7052 (list (buffer-file-name (buffer-base-buffer))))
7053 (org-back-to-heading t)
7054 (move-overlay org-agenda-restriction-lock-overlay
7055 (point)
7056 (if org-agenda-restriction-lock-highlight-subtree
7057 (save-excursion (org-end-of-subtree t t) (point))
7058 (point-at-eol)))
7059 (move-marker org-agenda-restrict-begin (point))
7060 (move-marker org-agenda-restrict-end
7061 (save-excursion (org-end-of-subtree t t)))
7062 (message "Locking agenda restriction to subtree"))
7063 (put 'org-agenda-files 'org-restrict
7064 (list (buffer-file-name (buffer-base-buffer))))
7065 (setq org-agenda-restrict nil)
7066 (setq org-agenda-overriding-restriction 'file)
7067 (move-marker org-agenda-restrict-begin nil)
7068 (move-marker org-agenda-restrict-end nil)
7069 (message "Locking agenda restriction to file"))
7070 (setq current-prefix-arg nil)
7071 (org-agenda-maybe-redo))
7073 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7074 "Remove the agenda restriction lock."
7075 (interactive "P")
7076 (delete-overlay org-agenda-restriction-lock-overlay)
7077 (delete-overlay org-speedbar-restriction-lock-overlay)
7078 (setq org-agenda-overriding-restriction nil)
7079 (setq org-agenda-restrict nil)
7080 (put 'org-agenda-files 'org-restrict nil)
7081 (move-marker org-agenda-restrict-begin nil)
7082 (move-marker org-agenda-restrict-end nil)
7083 (setq current-prefix-arg nil)
7084 (message "Agenda restriction lock removed")
7085 (or noupdate (org-agenda-maybe-redo)))
7087 (defun org-agenda-maybe-redo ()
7088 "If there is any window showing the agenda view, update it."
7089 (let ((w (get-buffer-window (or org-agenda-this-buffer-name
7090 org-agenda-buffer-name)
7092 (w0 (selected-window)))
7093 (when w
7094 (select-window w)
7095 (org-agenda-redo)
7096 (select-window w0)
7097 (if org-agenda-overriding-restriction
7098 (message "Agenda view shifted to new %s restriction"
7099 org-agenda-overriding-restriction)
7100 (message "Agenda restriction lock removed")))))
7102 ;;; Agenda commands
7104 (defun org-agenda-check-type (error &rest types)
7105 "Check if agenda buffer is of allowed type.
7106 If ERROR is non-nil, throw an error, otherwise just return nil.
7107 Allowed types are `agenda' `timeline' `todo' `tags' `search'."
7108 (if (not org-agenda-type)
7109 (error "No Org agenda currently displayed")
7110 (if (memq org-agenda-type types)
7112 (if error
7113 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
7114 nil))))
7116 (defun org-agenda-Quit ()
7117 "Exit the agenda, killing the agenda buffer.
7118 Like `org-agenda-quit', but kill the buffer even when
7119 `org-agenda-sticky' is non-nil."
7120 (interactive)
7121 (org-agenda--quit))
7123 (defun org-agenda-quit ()
7124 "Exit the agenda.
7126 When `org-agenda-sticky' is non-nil, bury the agenda buffer
7127 instead of killing it.
7129 When `org-agenda-restore-windows-after-quit' is non-nil, restore
7130 the pre-agenda window configuration.
7132 When column view is active, exit column view instead of the
7133 agenda."
7134 (interactive)
7135 (org-agenda--quit org-agenda-sticky))
7137 (defun org-agenda--quit (&optional bury)
7138 (if org-agenda-columns-active
7139 (org-columns-quit)
7140 (let ((wconf org-agenda-pre-window-conf)
7141 (buf (current-buffer))
7142 (org-agenda-last-indirect-window
7143 (and (eq org-indirect-buffer-display 'other-window)
7144 org-agenda-last-indirect-buffer
7145 (get-buffer-window org-agenda-last-indirect-buffer))))
7146 (cond
7147 ((eq org-agenda-window-setup 'other-frame)
7148 (delete-frame))
7149 ((and org-agenda-restore-windows-after-quit
7150 wconf)
7151 ;; Maybe restore the pre-agenda window configuration. Reset
7152 ;; `org-agenda-pre-window-conf' before running
7153 ;; `set-window-configuration', which loses the current buffer.
7154 (setq org-agenda-pre-window-conf nil)
7155 (set-window-configuration wconf))
7157 (when org-agenda-last-indirect-window
7158 (delete-window org-agenda-last-indirect-window))
7159 (and (not (eq org-agenda-window-setup 'current-window))
7160 (not (one-window-p))
7161 (delete-window))))
7162 (if bury
7163 ;; Set the agenda buffer as the current buffer instead of
7164 ;; passing it as an argument to `bury-buffer' so that
7165 ;; `bury-buffer' removes it from the window.
7166 (with-current-buffer buf
7167 (bury-buffer))
7168 (kill-buffer buf)
7169 (setq org-agenda-archives-mode nil
7170 org-agenda-buffer nil)))))
7172 (defun org-agenda-exit ()
7173 "Exit the agenda, killing Org buffers loaded by the agenda.
7174 Like `org-agenda-Quit', but kill any buffers that were created by
7175 the agenda. Org buffers visited directly by the user will not be
7176 touched. Also, exit the agenda even if it is in column view."
7177 (interactive)
7178 (when org-agenda-columns-active
7179 (org-columns-quit))
7180 (org-release-buffers org-agenda-new-buffers)
7181 (setq org-agenda-new-buffers nil)
7182 (org-agenda-Quit))
7184 (defun org-agenda-kill-all-agenda-buffers ()
7185 "Kill all buffers in `org-agenda-mode'.
7186 This is used when toggling sticky agendas."
7187 (interactive)
7188 (let (blist)
7189 (dolist (buf (buffer-list))
7190 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7191 (push buf blist)))
7192 (mapc 'kill-buffer blist)))
7194 (defun org-agenda-execute (arg)
7195 "Execute another agenda command, keeping same window.
7196 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7197 in the agenda."
7198 (interactive "P")
7199 (let ((org-agenda-window-setup 'current-window))
7200 (org-agenda arg)))
7202 (defun org-agenda-redo (&optional all)
7203 "Rebuild possibly ALL agenda view(s) in the current buffer."
7204 (interactive "P")
7205 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7206 (cpa (unless (eq all t) current-prefix-arg))
7207 (org-agenda-doing-sticky-redo org-agenda-sticky)
7208 (org-agenda-sticky nil)
7209 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7210 org-agenda-buffer-name))
7211 (org-agenda-keep-modes t)
7212 (tag-filter org-agenda-tag-filter)
7213 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7214 (top-hl-filter org-agenda-top-headline-filter)
7215 (cat-filter org-agenda-category-filter)
7216 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7217 (re-filter org-agenda-regexp-filter)
7218 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7219 (effort-filter org-agenda-effort-filter)
7220 (effort-preset (get 'org-agenda-effort-filter :preset-filter))
7221 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7222 (cols org-agenda-columns-active)
7223 (line (org-current-line))
7224 (window-line (- line (org-current-line (window-start))))
7225 (lprops (get 'org-agenda-redo-command 'org-lprops))
7226 (redo-cmd (get-text-property p 'org-redo-cmd))
7227 (last-args (get-text-property p 'org-last-args))
7228 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7229 (org-agenda-overriding-cmd-arguments
7230 (unless (eq all t)
7231 (cond ((listp last-args)
7232 (cons (or cpa (car last-args)) (cdr last-args)))
7233 ((stringp last-args)
7234 last-args))))
7235 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7236 (put 'org-agenda-tag-filter :preset-filter nil)
7237 (put 'org-agenda-category-filter :preset-filter nil)
7238 (put 'org-agenda-regexp-filter :preset-filter nil)
7239 (put 'org-agenda-effort-filter :preset-filter nil)
7240 (and cols (org-columns-quit))
7241 (message "Rebuilding agenda buffer...")
7242 (if series-redo-cmd
7243 (eval series-redo-cmd)
7244 (org-let lprops redo-cmd))
7245 (setq org-agenda-undo-list nil
7246 org-agenda-pending-undo-list nil
7247 org-agenda-tag-filter tag-filter
7248 org-agenda-category-filter cat-filter
7249 org-agenda-regexp-filter re-filter
7250 org-agenda-effort-filter effort-filter
7251 org-agenda-top-headline-filter top-hl-filter)
7252 (message "Rebuilding agenda buffer...done")
7253 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7254 (put 'org-agenda-category-filter :preset-filter cat-preset)
7255 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7256 (put 'org-agenda-effort-filter :preset-filter effort-preset)
7257 (let ((tag (or tag-filter tag-preset))
7258 (cat (or cat-filter cat-preset))
7259 (effort (or effort-filter effort-preset))
7260 (re (or re-filter re-preset)))
7261 (when tag (org-agenda-filter-apply tag 'tag t))
7262 (when cat (org-agenda-filter-apply cat 'category))
7263 (when effort (org-agenda-filter-apply effort 'effort))
7264 (when re (org-agenda-filter-apply re 'regexp)))
7265 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7266 (and cols (called-interactively-p 'any) (org-agenda-columns))
7267 (org-goto-line line)
7268 (recenter window-line)))
7270 (defvar org-global-tags-completion-table nil)
7271 (defvar org-agenda-filter-form nil)
7272 (defvar org-agenda-filtered-by-category nil)
7274 (defun org-agenda-filter-by-category (strip)
7275 "Filter lines in the agenda buffer that have a specific category.
7276 The category is that of the current line.
7277 Without prefix argument, keep only the lines of that category.
7278 With a prefix argument, exclude the lines of that category.
7280 (interactive "P")
7281 (if (and org-agenda-filtered-by-category
7282 org-agenda-category-filter)
7283 (org-agenda-filter-show-all-cat)
7284 (let ((cat (org-no-properties (org-get-at-eol 'org-category 1))))
7285 (cond
7286 ((and cat strip)
7287 (org-agenda-filter-apply
7288 (push (concat "-" cat) org-agenda-category-filter) 'category))
7289 (cat
7290 (org-agenda-filter-apply
7291 (setq org-agenda-category-filter
7292 (list (concat "+" cat))) 'category))
7293 (t (error "No category at point"))))))
7295 (defun org-find-top-headline (&optional pos)
7296 "Find the topmost parent headline and return it."
7297 (save-excursion
7298 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
7299 (if pos (goto-char pos))
7300 ;; Skip up to the topmost parent
7301 (while (ignore-errors (outline-up-heading 1) t))
7302 (ignore-errors
7303 (nth 4 (org-heading-components))))))
7305 (defvar org-agenda-filtered-by-top-headline nil)
7306 (defun org-agenda-filter-by-top-headline (strip)
7307 "Keep only those lines that are descendants from the same top headline.
7308 The top headline is that of the current line."
7309 (interactive "P")
7310 (if org-agenda-filtered-by-top-headline
7311 (progn
7312 (setq org-agenda-filtered-by-top-headline nil
7313 org-agenda-top-headline-filter nil)
7314 (org-agenda-filter-show-all-top-filter))
7315 (let ((toph (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7316 (if toph (org-agenda-filter-top-headline-apply toph strip)
7317 (error "No top-level headline at point")))))
7319 (defvar org-agenda-regexp-filter nil)
7320 (defun org-agenda-filter-by-regexp (strip)
7321 "Filter agenda entries by a regular expression.
7322 Regexp filters are cumulative.
7323 With no prefix argument, keep entries matching the regexp.
7324 With one prefix argument, filter out entries matching the regexp.
7325 With two prefix arguments, remove the regexp filters."
7326 (interactive "P")
7327 (if (not (equal strip '(16)))
7328 (let ((flt (concat (if (equal strip '(4)) "-" "+")
7329 (read-from-minibuffer
7330 (if (equal strip '(4))
7331 "Filter out entries matching regexp: "
7332 "Narrow to entries matching regexp: ")))))
7333 (push flt org-agenda-regexp-filter)
7334 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
7335 (org-agenda-filter-show-all-re)
7336 (message "Regexp filter removed")))
7338 (defvar org-agenda-effort-filter nil)
7339 (defun org-agenda-filter-by-effort (strip)
7340 "Filter agenda entries by effort.
7341 With no prefix argument, keep entries matching the effort condition.
7342 With one prefix argument, filter out entries matching the condition.
7343 With two prefix arguments, remove the effort filters."
7344 (interactive "P")
7345 (cond
7346 ((member strip '(nil 4))
7347 (let* ((efforts (split-string
7348 (or (cdr (assoc (concat org-effort-property "_ALL")
7349 org-global-properties))
7350 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00")))
7351 ;; XXX: the following handles only up to 10 different
7352 ;; effort values.
7353 (allowed-keys (if (null efforts) nil
7354 (mapcar (lambda (n) (mod n 10)) ;turn 10 into 0
7355 (number-sequence 1 (length efforts)))))
7356 (op nil))
7357 (while (not (memq op '(?< ?> ?=)))
7358 (setq op (read-char-exclusive "Effort operator? (> = or <)")))
7359 ;; Select appropriate duration. Ignore non-digit characters.
7360 (let ((prompt
7361 (apply #'format
7362 (concat "Effort %c "
7363 (mapconcat (lambda (s) (concat "[%d]" s))
7364 efforts
7365 " "))
7366 op allowed-keys))
7367 (eff -1))
7368 (while (not (memq eff allowed-keys))
7369 (message prompt)
7370 (setq eff (- (read-char-exclusive) 48)))
7371 (setq org-agenda-effort-filter
7372 (list (concat (if strip "-" "+")
7373 (char-to-string op)
7374 ;; Numbering is 1 2 3 ... 9 0, but we want
7375 ;; 0 1 2 ... 8 9.
7376 (nth (mod (1- eff) 10) efforts)))))
7377 (org-agenda-filter-apply org-agenda-effort-filter 'effort)))
7378 (t (org-agenda-filter-show-all-effort)
7379 (message "Effort filter removed"))))
7381 (defun org-agenda-filter-remove-all ()
7382 "Remove all filters from the current agenda buffer."
7383 (interactive)
7384 (when org-agenda-tag-filter
7385 (org-agenda-filter-show-all-tag))
7386 (when org-agenda-category-filter
7387 (org-agenda-filter-show-all-cat))
7388 (when org-agenda-regexp-filter
7389 (org-agenda-filter-show-all-re))
7390 (when org-agenda-top-headline-filter
7391 (org-agenda-filter-show-all-top-filter))
7392 (when org-agenda-effort-filter
7393 (org-agenda-filter-show-all-effort))
7394 (org-agenda-finalize))
7396 (defun org-agenda-filter-by-tag (arg &optional char exclude)
7397 "Keep only those lines in the agenda buffer that have a specific tag.
7399 The tag is selected with its fast selection letter, as configured.
7401 With a `\\[universal-argument]' prefix, exclude the agenda search.
7403 With a `\\[universal-argument] \\[universal-argument]' prefix, filter the literal tag, \
7404 i.e. don't
7405 filter on all its group members.
7407 A lisp caller can specify CHAR. EXCLUDE means that the new tag
7408 should be used to exclude the search - the interactive user can
7409 also press `-' or `+' to switch between filtering and excluding."
7410 (interactive "P")
7411 (let* ((alist org-tag-alist-for-agenda)
7412 (tag-chars (mapconcat
7413 (lambda (x) (if (and (not (symbolp (car x)))
7414 (cdr x))
7415 (char-to-string (cdr x))
7416 ""))
7417 org-tag-alist-for-agenda ""))
7418 (valid-char-list (append '(?\t ?\r ?/ ?. ?\s ?q)
7419 (string-to-list tag-chars)))
7420 (exclude (or exclude (equal arg '(4))))
7421 (expand (not (equal arg '(16))))
7422 (inhibit-read-only t)
7423 (current org-agenda-tag-filter)
7424 a n tag)
7425 (unless char
7426 (while (not (memq char valid-char-list))
7427 (message
7428 "%s by tag [%s ], [TAB], %s[/]:off, [+/-]:filter/exclude%s, [q]:quit"
7429 (if exclude "Exclude" "Filter") tag-chars
7430 (if org-agenda-auto-exclude-function "[RET], " "")
7431 (if expand "" ", no grouptag expand"))
7432 (setq char (read-char-exclusive))
7433 ;; Excluding or filtering down
7434 (cond ((eq char ?-) (setq exclude t))
7435 ((eq char ?+) (setq exclude nil)))))
7436 (when (eq char ?\t)
7437 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7438 (setq-local org-global-tags-completion-table
7439 (org-global-tags-completion-table)))
7440 (let ((completion-ignore-case t))
7441 (setq tag (completing-read
7442 "Tag: " org-global-tags-completion-table))))
7443 (cond
7444 ((eq char ?\r)
7445 (org-agenda-filter-show-all-tag)
7446 (when org-agenda-auto-exclude-function
7447 (setq org-agenda-tag-filter nil)
7448 (dolist (tag (org-agenda-get-represented-tags))
7449 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7450 (if modifier
7451 (push modifier org-agenda-tag-filter))))
7452 (if (not (null org-agenda-tag-filter))
7453 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))))
7454 ((eq char ?/)
7455 (org-agenda-filter-show-all-tag)
7456 (when (get 'org-agenda-tag-filter :preset-filter)
7457 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand)))
7458 ((eq char ?.)
7459 (setq org-agenda-tag-filter
7460 (mapcar (lambda(tag) (concat "+" tag))
7461 (org-get-at-bol 'tags)))
7462 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7463 ((eq char ?q)) ;If q, abort (even if there is a q-key for a tag...)
7464 ((or (eq char ?\s)
7465 (setq a (rassoc char alist))
7466 (and tag (setq a (cons tag nil))))
7467 (org-agenda-filter-show-all-tag)
7468 (setq tag (car a))
7469 (setq org-agenda-tag-filter
7470 (cons (concat (if exclude "-" "+") tag)
7471 current))
7472 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7473 (t (error "Invalid tag selection character %c" char)))))
7475 (defun org-agenda-get-represented-tags ()
7476 "Get a list of all tags currently represented in the agenda."
7477 (let (p tags)
7478 (save-excursion
7479 (goto-char (point-min))
7480 (while (setq p (next-single-property-change (point) 'tags))
7481 (goto-char p)
7482 (mapc (lambda (x) (add-to-list 'tags x))
7483 (get-text-property (point) 'tags))))
7484 tags))
7487 (defun org-agenda-filter-make-matcher (filter type &optional expand)
7488 "Create the form that tests a line for agenda filter. Optional
7489 argument EXPAND can be used for the TYPE tag and will expand the
7490 tags in the FILTER if any of the tags in FILTER are grouptags."
7491 (let (f f1)
7492 (cond
7493 ;; Tag filter
7494 ((eq type 'tag)
7495 (setq filter
7496 (delete-dups
7497 (append (get 'org-agenda-tag-filter :preset-filter)
7498 filter)))
7499 (dolist (x filter)
7500 (let ((op (string-to-char x)))
7501 (if expand (setq x (org-agenda-filter-expand-tags (list x) t))
7502 (setq x (list x)))
7503 (setq f1 (org-agenda-filter-make-matcher-tag-exp x op))
7504 (push f1 f))))
7505 ;; Category filter
7506 ((eq type 'category)
7507 (setq filter
7508 (delete-dups
7509 (append (get 'org-agenda-category-filter :preset-filter)
7510 filter)))
7511 (dolist (x filter)
7512 (if (equal "-" (substring x 0 1))
7513 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7514 (setq f1 (list 'equal (substring x 1) 'cat)))
7515 (push f1 f)))
7516 ;; Regexp filter
7517 ((eq type 'regexp)
7518 (setq filter
7519 (delete-dups
7520 (append (get 'org-agenda-regexp-filter :preset-filter)
7521 filter)))
7522 (dolist (x filter)
7523 (if (equal "-" (substring x 0 1))
7524 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7525 (setq f1 (list 'string-match (substring x 1) 'txt)))
7526 (push f1 f)))
7527 ;; Effort filter
7528 ((eq type 'effort)
7529 (setq filter
7530 (delete-dups
7531 (append (get 'org-agenda-effort-filter :preset-filter)
7532 filter)))
7533 (dolist (x filter)
7534 (push (org-agenda-filter-effort-form x) f))))
7535 (cons 'and (nreverse f))))
7537 (defun org-agenda-filter-make-matcher-tag-exp (tags op)
7538 "Return a form associated to tag-expression TAGS.
7539 Build a form testing a line for agenda filter for
7540 tag-expressions. OP is an operator of type CHAR that allows the
7541 function to set the right switches in the returned form."
7542 (let (form)
7543 ;; Any of the expressions can match if OP is +, all must match if
7544 ;; the operator is -.
7545 (dolist (x tags (cons (if (eq op ?-) 'and 'or) form))
7546 (let* ((tag (substring x 1))
7547 (f (cond
7548 ((string= "" tag) '(not tags))
7549 ((and (string-match-p "\\`{" tag) (string-match-p "}\\'" tag))
7550 ;; TAG is a regexp.
7551 (list 'org-match-any-p (substring tag 1 -1) 'tags))
7552 (t (list 'member (downcase tag) 'tags)))))
7553 (push (if (eq op ?-) (list 'not f) f) form)))))
7555 (defun org-agenda-filter-effort-form (e)
7556 "Return the form to compare the effort of the current line with what E says.
7557 E looks like \"+<2:25\"."
7558 (let (op)
7559 (setq e (substring e 1))
7560 (setq op (string-to-char e) e (substring e 1))
7561 (setq op (cond ((equal op ?<) '<=)
7562 ((equal op ?>) '>=)
7563 ((equal op ??) op)
7564 (t '=)))
7565 (list 'org-agenda-compare-effort (list 'quote op)
7566 (org-duration-to-minutes e))))
7568 (defun org-agenda-compare-effort (op value)
7569 "Compare the effort of the current line with VALUE, using OP.
7570 If the line does not have an effort defined, return nil."
7571 ;; `effort-minutes' property cannot be extracted directly from
7572 ;; current line but is stored as a property in `txt'.
7573 (let ((effort (get-text-property 0 'effort-minutes (org-get-at-bol 'txt))))
7574 (funcall op
7575 (or effort (if org-sort-agenda-noeffort-is-high 32767 -1))
7576 value)))
7578 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7579 "Expand group tags in FILTER for the agenda.
7580 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7581 (if org-group-tags
7582 (let ((case-fold-search t) rtn)
7583 (mapc
7584 (lambda (f)
7585 (let (f0 dir)
7586 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7587 (setq dir (match-string 1 f) f0 (match-string 2 f))
7588 (setq dir (if no-operator "" "+") f0 f))
7589 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7590 (org-tags-expand f0 t t))
7591 rtn))))
7592 filter)
7593 (reverse rtn))
7594 filter))
7596 (defun org-agenda-filter-apply (filter type &optional expand)
7597 "Set FILTER as the new agenda filter and apply it. Optional
7598 argument EXPAND can be used for the TYPE tag and will expand the
7599 tags in the FILTER if any of the tags in FILTER are grouptags."
7600 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7601 (if org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7602 (let (tags cat txt)
7603 (setq org-agenda-filter-form (org-agenda-filter-make-matcher filter type expand))
7604 ;; Only set `org-agenda-filtered-by-category' to t when a unique
7605 ;; category is used as the filter:
7606 (setq org-agenda-filtered-by-category
7607 (and (eq type 'category)
7608 (not (equal (substring (car filter) 0 1) "-"))))
7609 (org-agenda-set-mode-name)
7610 (save-excursion
7611 (goto-char (point-min))
7612 (while (not (eobp))
7613 (if (org-get-at-bol 'org-marker)
7614 (progn
7615 (setq tags (org-get-at-bol 'tags)
7616 cat (org-get-at-eol 'org-category 1)
7617 txt (org-get-at-bol 'txt))
7618 (if (not (eval org-agenda-filter-form))
7619 (org-agenda-filter-hide-line type))
7620 (beginning-of-line 2))
7621 (beginning-of-line 2))))
7622 (if (get-char-property (point) 'invisible)
7623 (ignore-errors (org-agenda-previous-line)))))
7625 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7626 "Filter by top headline HL."
7627 (org-agenda-set-mode-name)
7628 (save-excursion
7629 (goto-char (point-min))
7630 (while (not (eobp))
7631 (let* ((pos (org-get-at-bol 'org-hd-marker))
7632 (tophl (and pos (org-find-top-headline pos))))
7633 (if (and tophl (funcall (if negative 'identity 'not)
7634 (string= hl tophl)))
7635 (org-agenda-filter-hide-line 'top-headline)))
7636 (beginning-of-line 2)))
7637 (if (get-char-property (point) 'invisible)
7638 (org-agenda-previous-line))
7639 (setq org-agenda-top-headline-filter hl
7640 org-agenda-filtered-by-top-headline t))
7642 (defun org-agenda-filter-hide-line (type)
7643 "Hide lines with TYPE in the agenda buffer."
7644 (let* ((b (max (point-min) (1- (point-at-bol))))
7645 (e (point-at-eol)))
7646 (let ((inhibit-read-only t))
7647 (add-text-properties
7648 b e `(invisible org-filtered org-filter-type ,type)))))
7650 (defun org-agenda-remove-filter (type)
7651 (interactive)
7652 "Remove filter of type TYPE from the agenda buffer."
7653 (save-excursion
7654 (goto-char (point-min))
7655 (let ((inhibit-read-only t) pos)
7656 (while (setq pos (text-property-any (point) (point-max) 'org-filter-type type))
7657 (goto-char pos)
7658 (remove-text-properties
7659 (point) (next-single-property-change (point) 'org-filter-type)
7660 `(invisible org-filtered org-filter-type ,type))))
7661 (set (intern (format "org-agenda-%s-filter" (intern-soft type))) nil)
7662 (setq org-agenda-filter-form nil)
7663 (org-agenda-set-mode-name)
7664 (org-agenda-finalize)))
7666 (defun org-agenda-filter-show-all-tag nil
7667 (org-agenda-remove-filter 'tag))
7668 (defun org-agenda-filter-show-all-re nil
7669 (org-agenda-remove-filter 'regexp))
7670 (defun org-agenda-filter-show-all-effort nil
7671 (org-agenda-remove-filter 'effort))
7672 (defun org-agenda-filter-show-all-cat nil
7673 (org-agenda-remove-filter 'category))
7674 (defun org-agenda-filter-show-all-top-filter nil
7675 (org-agenda-remove-filter 'top-headline))
7677 (defun org-agenda-manipulate-query-add ()
7678 "Manipulate the query by adding a search term with positive selection.
7679 Positive selection means the term must be matched for selection of an entry."
7680 (interactive)
7681 (org-agenda-manipulate-query ?\[))
7682 (defun org-agenda-manipulate-query-subtract ()
7683 "Manipulate the query by adding a search term with negative selection.
7684 Negative selection means term must not be matched for selection of an entry."
7685 (interactive)
7686 (org-agenda-manipulate-query ?\]))
7687 (defun org-agenda-manipulate-query-add-re ()
7688 "Manipulate the query by adding a search regexp with positive selection.
7689 Positive selection means the regexp must match for selection of an entry."
7690 (interactive)
7691 (org-agenda-manipulate-query ?\{))
7692 (defun org-agenda-manipulate-query-subtract-re ()
7693 "Manipulate the query by adding a search regexp with negative selection.
7694 Negative selection means regexp must not match for selection of an entry."
7695 (interactive)
7696 (org-agenda-manipulate-query ?\}))
7697 (defun org-agenda-manipulate-query (char)
7698 (cond
7699 ((memq org-agenda-type '(timeline agenda))
7700 (let ((org-agenda-include-inactive-timestamps t))
7701 (org-agenda-redo))
7702 (message "Display now includes inactive timestamps as well"))
7703 ((eq org-agenda-type 'search)
7704 (org-add-to-string
7705 'org-agenda-query-string
7706 (if org-agenda-last-search-view-search-was-boolean
7707 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7708 (?\{ . " +{}") (?\} . " -{}"))))
7709 " "))
7710 (setq org-agenda-redo-command
7711 (list 'org-search-view
7712 (car (get-text-property (min (1- (point-max)) (point))
7713 'org-last-args))
7714 org-agenda-query-string
7715 (+ (length org-agenda-query-string)
7716 (if (member char '(?\{ ?\})) 0 1))))
7717 (set-register org-agenda-query-register org-agenda-query-string)
7718 (let ((org-agenda-overriding-arguments
7719 (cdr org-agenda-redo-command)))
7720 (org-agenda-redo)))
7721 (t (error "Cannot manipulate query for %s-type agenda buffers"
7722 org-agenda-type))))
7724 (defun org-add-to-string (var string)
7725 (set var (concat (symbol-value var) string)))
7727 (defun org-agenda-goto-date (span)
7728 "Jump to DATE in agenda."
7729 (interactive "P")
7730 (let* ((org-read-date-prefer-future
7731 (eval org-agenda-jump-prefer-future))
7732 (date (org-read-date))
7733 (day (time-to-days (org-time-string-to-time date)))
7734 (org-agenda-sticky-orig org-agenda-sticky)
7735 (org-agenda-buffer-tmp-name (buffer-name))
7736 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7737 (0-arg (or current-prefix-arg (car args)))
7738 (2-arg (nth 2 args))
7739 (with-hour-p (nth 4 org-agenda-redo-command))
7740 (newcmd (list 'org-agenda-list 0-arg date
7741 (org-agenda-span-to-ndays
7742 2-arg (org-time-string-to-absolute date))
7743 with-hour-p))
7744 (newargs (cdr newcmd))
7745 (inhibit-read-only t)
7746 org-agenda-sticky)
7747 (if (not (org-agenda-check-type t 'agenda))
7748 (error "Not available in non-agenda views")
7749 (add-text-properties (point-min) (point-max)
7750 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7751 (org-agenda-redo)
7752 (goto-char (point-min))
7753 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7754 (save-excursion (move-beginning-of-line 2) (eobp))))
7755 (move-beginning-of-line 2))
7756 (setq org-agenda-sticky org-agenda-sticky-orig
7757 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7759 (defun org-agenda-goto-today ()
7760 "Go to today."
7761 (interactive)
7762 (org-agenda-check-type t 'timeline 'agenda)
7763 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7764 (curspan (nth 2 args))
7765 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7766 (cond
7767 (tdpos (goto-char tdpos))
7768 ((eq org-agenda-type 'agenda)
7769 (let* ((sd (org-agenda-compute-starting-span
7770 (org-today) (or curspan org-agenda-span)))
7771 (org-agenda-overriding-arguments args))
7772 (setf (nth 1 org-agenda-overriding-arguments) sd)
7773 (org-agenda-redo)
7774 (org-agenda-find-same-or-today-or-agenda)))
7775 (t (error "Cannot find today")))))
7777 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7778 (goto-char
7779 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7780 (text-property-any (point-min) (point-max) 'org-today t)
7781 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7782 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7783 (org-agenda-backward-block))
7784 (point-min))))
7786 (defun org-agenda-backward-block ()
7787 "Move backward by one agenda block."
7788 (interactive)
7789 (org-agenda-forward-block 'backward))
7791 (defun org-agenda-forward-block (&optional backward)
7792 "Move forward by one agenda block.
7793 When optional argument BACKWARD is set, go backward"
7794 (interactive)
7795 (cond ((not (derived-mode-p 'org-agenda-mode))
7796 (user-error
7797 "Cannot execute this command outside of org-agenda-mode buffers"))
7798 ((looking-at (if backward "\\`" "\\'"))
7799 (message "Already at the %s block" (if backward "first" "last")))
7800 (t (let ((pos (prog1 (point)
7801 (ignore-errors (if backward (backward-char 1)
7802 (move-end-of-line 1)))))
7803 (f (if backward
7804 'previous-single-property-change
7805 'next-single-property-change))
7806 moved dest)
7807 (while (and (setq dest (funcall
7808 f (point) 'org-agenda-structural-header))
7809 (not (get-text-property
7810 (point) 'org-agenda-structural-header)))
7811 (setq moved t)
7812 (goto-char dest))
7813 (if moved (move-beginning-of-line 1)
7814 (goto-char (if backward (point-min) (point-max)))
7815 (move-beginning-of-line 1)
7816 (message "No %s block" (if backward "previous" "further")))))))
7818 (defun org-agenda-later (arg)
7819 "Go forward in time by the current span.
7820 With prefix ARG, go forward that many times the current span."
7821 (interactive "p")
7822 (org-agenda-check-type t 'agenda)
7823 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7824 (span (or (nth 2 args) org-agenda-current-span))
7825 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7826 (greg (calendar-gregorian-from-absolute sd))
7827 (cnt (org-get-at-bol 'org-day-cnt))
7828 greg2)
7829 (cond
7830 ((numberp span)
7831 (setq sd (+ (* span arg) sd)))
7832 ((eq span 'day)
7833 (setq sd (+ arg sd)))
7834 ((eq span 'week)
7835 (setq sd (+ (* 7 arg) sd)))
7836 ((eq span 'fortnight)
7837 (setq sd (+ (* 14 arg) sd)))
7838 ((eq span 'month)
7839 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7840 sd (calendar-absolute-from-gregorian greg2))
7841 (setcar greg2 (1+ (car greg2))))
7842 ((eq span 'year)
7843 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7844 sd (calendar-absolute-from-gregorian greg2))
7845 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7847 (setq sd (+ (* span arg) sd))))
7848 (let ((org-agenda-overriding-cmd
7849 ;; `cmd' may have been set by `org-agenda-run-series' which
7850 ;; uses `org-agenda-overriding-cmd' to decide whether
7851 ;; overriding is allowed for `cmd'
7852 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7853 (org-agenda-overriding-arguments
7854 (list (car args) sd span)))
7855 (org-agenda-redo)
7856 (org-agenda-find-same-or-today-or-agenda cnt))))
7858 (defun org-agenda-earlier (arg)
7859 "Go backward in time by the current span.
7860 With prefix ARG, go backward that many times the current span."
7861 (interactive "p")
7862 (org-agenda-later (- arg)))
7864 (defun org-agenda-view-mode-dispatch ()
7865 "Call one of the view mode commands."
7866 (interactive)
7867 (message "View: [d]ay [w]eek for[t]night [m]onth [y]ear [SPC]reset [q]uit/abort
7868 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7869 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7870 (pcase (read-char-exclusive)
7871 (?\ (call-interactively 'org-agenda-reset-view))
7872 (?d (call-interactively 'org-agenda-day-view))
7873 (?w (call-interactively 'org-agenda-week-view))
7874 (?t (call-interactively 'org-agenda-fortnight-view))
7875 (?m (call-interactively 'org-agenda-month-view))
7876 (?y (call-interactively 'org-agenda-year-view))
7877 (?l (call-interactively 'org-agenda-log-mode))
7878 (?L (org-agenda-log-mode '(4)))
7879 (?c (org-agenda-log-mode 'clockcheck))
7880 ((or ?F ?f) (call-interactively 'org-agenda-follow-mode))
7881 (?a (call-interactively 'org-agenda-archives-mode))
7882 (?A (org-agenda-archives-mode 'files))
7883 ((or ?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7884 ((or ?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7885 (?G (call-interactively 'org-agenda-toggle-time-grid))
7886 (?D (call-interactively 'org-agenda-toggle-diary))
7887 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7888 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7889 (org-agenda-check-type t 'timeline 'agenda)
7890 (org-agenda-redo))
7891 (message "Display now includes inactive timestamps as well"))
7892 (?q (message "Abort"))
7893 (key (user-error "Invalid key: %s" key))))
7895 (defun org-agenda-reset-view ()
7896 "Switch to default view for agenda."
7897 (interactive)
7898 (org-agenda-change-time-span org-agenda-span))
7900 (defun org-agenda-day-view (&optional day-of-month)
7901 "Switch to daily view for agenda.
7902 With argument DAY-OF-MONTH, switch to that day of the month."
7903 (interactive "P")
7904 (org-agenda-change-time-span 'day day-of-month))
7906 (defun org-agenda-week-view (&optional iso-week)
7907 "Switch to weekly view for agenda.
7908 With argument ISO-WEEK, switch to the corresponding ISO week.
7909 If ISO-WEEK has more then 2 digits, only the last two encode
7910 the week. Any digits before this encode a year. So 200712
7911 means week 12 of year 2007. Years ranging from 70 years ago
7912 to 30 years in the future can also be written as 2-digit years."
7913 (interactive "P")
7914 (org-agenda-change-time-span 'week iso-week))
7916 (defun org-agenda-fortnight-view (&optional iso-week)
7917 "Switch to fortnightly view for agenda.
7918 With argument ISO-WEEK, switch to the corresponding ISO week.
7919 If ISO-WEEK has more then 2 digits, only the last two encode
7920 the week. Any digits before this encode a year. So 200712
7921 means week 12 of year 2007. Years ranging from 70 years ago
7922 to 30 years in the future can also be written as 2-digit years."
7923 (interactive "P")
7924 (org-agenda-change-time-span 'fortnight iso-week))
7926 (defun org-agenda-month-view (&optional month)
7927 "Switch to monthly view for agenda.
7928 With argument MONTH, switch to that month. If MONTH has more
7929 then 2 digits, only the last two encode the month. Any digits
7930 before this encode a year. So 200712 means December year 2007.
7931 Years ranging from 70 years ago to 30 years in the future can
7932 also be written as 2-digit years."
7933 (interactive "P")
7934 (org-agenda-change-time-span 'month month))
7936 (defun org-agenda-year-view (&optional year)
7937 "Switch to yearly view for agenda.
7938 With argument YEAR, switch to that year. Years ranging from 70
7939 years ago to 30 years in the future can also be written as
7940 2-digit years."
7941 (interactive "P")
7942 (when year
7943 (setq year (org-small-year-to-year year)))
7944 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
7945 (org-agenda-change-time-span 'year year)
7946 (error "Abort")))
7948 (defun org-agenda-change-time-span (span &optional n)
7949 "Change the agenda view to SPAN.
7950 SPAN may be `day', `week', `fortnight', `month', `year'."
7951 (org-agenda-check-type t 'agenda)
7952 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7953 (curspan (nth 2 args)))
7954 (if (and (not n) (equal curspan span))
7955 (error "Viewing span is already \"%s\"" span))
7956 (let* ((sd (or (org-get-at-bol 'day)
7957 (nth 1 args)
7958 org-starting-day))
7959 (sd (org-agenda-compute-starting-span sd span n))
7960 (org-agenda-overriding-cmd
7961 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7962 (org-agenda-overriding-arguments
7963 (list (car args) sd span)))
7964 (org-agenda-redo)
7965 (org-agenda-find-same-or-today-or-agenda))
7966 (org-agenda-set-mode-name)
7967 (message "Switched to %s view" span)))
7969 (defun org-agenda-compute-starting-span (sd span &optional n)
7970 "Compute starting date for agenda.
7971 SPAN may be `day', `week', `fortnight', `month', `year'. The return value
7972 is a cons cell with the starting date and the number of days,
7973 so that the date SD will be in that range."
7974 (let* ((greg (calendar-gregorian-from-absolute sd))
7975 (dg (nth 1 greg))
7976 (mg (car greg))
7977 (yg (nth 2 greg)))
7978 (cond
7979 ((eq span 'day)
7980 (when n
7981 (setq sd (+ (calendar-absolute-from-gregorian
7982 (list mg 1 yg))
7983 n -1))))
7984 ((or (eq span 'week) (eq span 'fortnight))
7985 (let* ((nt (calendar-day-of-week
7986 (calendar-gregorian-from-absolute sd)))
7987 (d (if org-agenda-start-on-weekday
7988 (- nt org-agenda-start-on-weekday)
7991 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
7992 (when n
7993 (require 'cal-iso)
7994 (when (> n 99)
7995 (setq y1 (org-small-year-to-year (/ n 100))
7996 n (mod n 100)))
7997 (setq sd
7998 (calendar-iso-to-absolute
7999 (list n 1
8000 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
8001 ((eq span 'month)
8002 (let (y1)
8003 (when (and n (> n 99))
8004 (setq y1 (org-small-year-to-year (/ n 100))
8005 n (mod n 100)))
8006 (setq sd (calendar-absolute-from-gregorian
8007 (list (or n mg) 1 (or y1 yg))))))
8008 ((eq span 'year)
8009 (setq sd (calendar-absolute-from-gregorian
8010 (list 1 1 (or n yg))))))
8011 sd))
8013 (defun org-agenda-next-date-line (&optional arg)
8014 "Jump to the next line indicating a date in agenda buffer."
8015 (interactive "p")
8016 (org-agenda-check-type t 'agenda 'timeline)
8017 (beginning-of-line 1)
8018 ;; This does not work if user makes date format that starts with a blank
8019 (if (looking-at "^\\S-") (forward-char 1))
8020 (if (not (re-search-forward "^\\S-" nil t arg))
8021 (progn
8022 (backward-char 1)
8023 (error "No next date after this line in this buffer")))
8024 (goto-char (match-beginning 0)))
8026 (defun org-agenda-previous-date-line (&optional arg)
8027 "Jump to the previous line indicating a date in agenda buffer."
8028 (interactive "p")
8029 (org-agenda-check-type t 'agenda 'timeline)
8030 (beginning-of-line 1)
8031 (if (not (re-search-backward "^\\S-" nil t arg))
8032 (error "No previous date before this line in this buffer")))
8034 ;; Initialize the highlight
8035 (defvar org-hl (make-overlay 1 1))
8036 (overlay-put org-hl 'face 'highlight)
8038 (defun org-highlight (begin end &optional buffer)
8039 "Highlight a region with overlay."
8040 (move-overlay org-hl begin end (or buffer (current-buffer))))
8042 (defun org-unhighlight ()
8043 "Detach overlay INDEX."
8044 (delete-overlay org-hl))
8046 (defun org-unhighlight-once ()
8047 "Remove the highlight from its position, and this function from the hook."
8048 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8049 (org-unhighlight))
8051 (defvar org-agenda-pre-follow-window-conf nil)
8052 (defun org-agenda-follow-mode ()
8053 "Toggle follow mode in an agenda buffer."
8054 (interactive)
8055 (unless org-agenda-follow-mode
8056 (setq org-agenda-pre-follow-window-conf
8057 (current-window-configuration)))
8058 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8059 (unless org-agenda-follow-mode
8060 (set-window-configuration org-agenda-pre-follow-window-conf))
8061 (org-agenda-set-mode-name)
8062 (org-agenda-do-context-action)
8063 (message "Follow mode is %s"
8064 (if org-agenda-follow-mode "on" "off")))
8066 (defun org-agenda-entry-text-mode (&optional arg)
8067 "Toggle entry text mode in an agenda buffer."
8068 (interactive "P")
8069 (if (or org-agenda-tag-filter
8070 org-agenda-category-filter
8071 org-agenda-regexp-filter
8072 org-agenda-top-headline-filter)
8073 (user-error "Can't show entry text in filtered views")
8074 (setq org-agenda-entry-text-mode (or (integerp arg)
8075 (not org-agenda-entry-text-mode)))
8076 (org-agenda-entry-text-hide)
8077 (and org-agenda-entry-text-mode
8078 (let ((org-agenda-entry-text-maxlines
8079 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8080 (org-agenda-entry-text-show)))
8081 (org-agenda-set-mode-name)
8082 (message "Entry text mode is %s%s"
8083 (if org-agenda-entry-text-mode "on" "off")
8084 (if (not org-agenda-entry-text-mode) ""
8085 (format " (maximum number of lines is %d)"
8086 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8088 (defun org-agenda-clockreport-mode ()
8089 "Toggle clocktable mode in an agenda buffer."
8090 (interactive)
8091 (org-agenda-check-type t 'agenda)
8092 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
8093 (setq org-agenda-start-with-clockreport-mode org-agenda-clockreport-mode)
8094 (org-agenda-set-mode-name)
8095 (org-agenda-redo)
8096 (message "Clocktable mode is %s"
8097 (if org-agenda-clockreport-mode "on" "off")))
8099 (defun org-agenda-log-mode (&optional special)
8100 "Toggle log mode in an agenda buffer.
8102 With argument SPECIAL, show all possible log items, not only the ones
8103 configured in `org-agenda-log-mode-items'.
8105 With a `\\[universal-argument] \\[universal-argument]' prefix, show *only* \
8106 log items, nothing else."
8107 (interactive "P")
8108 (org-agenda-check-type t 'agenda 'timeline)
8109 (setq org-agenda-show-log
8110 (cond
8111 ((equal special '(16)) 'only)
8112 ((eq special 'clockcheck)
8113 (if (eq org-agenda-show-log 'clockcheck)
8114 nil 'clockcheck))
8115 (special '(closed clock state))
8116 (t (not org-agenda-show-log))))
8117 (setq org-agenda-start-with-log-mode org-agenda-show-log)
8118 (org-agenda-set-mode-name)
8119 (org-agenda-redo)
8120 (message "Log mode is %s" (if org-agenda-show-log "on" "off")))
8122 (defun org-agenda-archives-mode (&optional with-files)
8123 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8124 When called with a prefix argument, include all archive files as well."
8125 (interactive "P")
8126 (setq org-agenda-archives-mode
8127 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8128 (org-agenda-set-mode-name)
8129 (org-agenda-redo)
8130 (message
8131 "%s"
8132 (cond
8133 ((eq org-agenda-archives-mode nil)
8134 "No archives are included")
8135 ((eq org-agenda-archives-mode 'trees)
8136 (format "Trees with :%s: tag are included" org-archive-tag))
8137 ((eq org-agenda-archives-mode t)
8138 (format "Trees with :%s: tag and all active archive files are included"
8139 org-archive-tag)))))
8141 (defun org-agenda-toggle-diary ()
8142 "Toggle diary inclusion in an agenda buffer."
8143 (interactive)
8144 (org-agenda-check-type t 'agenda)
8145 (setq org-agenda-include-diary (not org-agenda-include-diary))
8146 (org-agenda-redo)
8147 (org-agenda-set-mode-name)
8148 (message "Diary inclusion turned %s"
8149 (if org-agenda-include-diary "on" "off")))
8151 (defun org-agenda-toggle-deadlines ()
8152 "Toggle inclusion of entries with a deadline in an agenda buffer."
8153 (interactive)
8154 (org-agenda-check-type t 'agenda)
8155 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8156 (org-agenda-redo)
8157 (org-agenda-set-mode-name)
8158 (message "Deadlines inclusion turned %s"
8159 (if org-agenda-include-deadlines "on" "off")))
8161 (defun org-agenda-toggle-time-grid ()
8162 "Toggle time grid in an agenda buffer."
8163 (interactive)
8164 (org-agenda-check-type t 'agenda)
8165 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8166 (org-agenda-redo)
8167 (org-agenda-set-mode-name)
8168 (message "Time-grid turned %s"
8169 (if org-agenda-use-time-grid "on" "off")))
8171 (defun org-agenda-set-mode-name ()
8172 "Set the mode name to indicate all the small mode settings."
8173 (setq mode-name
8174 (list "Org-Agenda"
8175 (if (get 'org-agenda-files 'org-restrict) " []" "")
8177 '(:eval (org-agenda-span-name org-agenda-current-span))
8178 (if org-agenda-follow-mode " Follow" "")
8179 (if org-agenda-entry-text-mode " ETxt" "")
8180 (if org-agenda-include-diary " Diary" "")
8181 (if org-agenda-include-deadlines " Ddl" "")
8182 (if org-agenda-use-time-grid " Grid" "")
8183 (if (and (boundp 'org-habit-show-habits)
8184 org-habit-show-habits) " Habit" "")
8185 (cond
8186 ((consp org-agenda-show-log) " LogAll")
8187 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8188 (org-agenda-show-log " Log")
8189 (t ""))
8190 (if (or org-agenda-category-filter
8191 (get 'org-agenda-category-filter :preset-filter))
8192 '(:eval (propertize
8193 (concat " <"
8194 (mapconcat
8195 'identity
8196 (append
8197 (get 'org-agenda-category-filter :preset-filter)
8198 org-agenda-category-filter)
8200 ">")
8201 'face 'org-agenda-filter-category
8202 'help-echo "Category used in filtering")) "")
8203 (if (or org-agenda-tag-filter
8204 (get 'org-agenda-tag-filter :preset-filter))
8205 '(:eval (propertize
8206 (concat " {"
8207 (mapconcat
8208 'identity
8209 (append
8210 (get 'org-agenda-tag-filter :preset-filter)
8211 org-agenda-tag-filter)
8213 "}")
8214 'face 'org-agenda-filter-tags
8215 'help-echo "Tags used in filtering")) "")
8216 (if (or org-agenda-effort-filter
8217 (get 'org-agenda-effort-filter :preset-filter))
8218 '(:eval (propertize
8219 (concat " {"
8220 (mapconcat
8221 'identity
8222 (append
8223 (get 'org-agenda-effort-filter :preset-filter)
8224 org-agenda-effort-filter)
8226 "}")
8227 'face 'org-agenda-filter-effort
8228 'help-echo "Effort conditions used in filtering")) "")
8229 (if (or org-agenda-regexp-filter
8230 (get 'org-agenda-regexp-filter :preset-filter))
8231 '(:eval (propertize
8232 (concat " ["
8233 (mapconcat
8234 'identity
8235 (append
8236 (get 'org-agenda-regexp-filter :preset-filter)
8237 org-agenda-regexp-filter)
8239 "]")
8240 'face 'org-agenda-filter-regexp
8241 'help-echo "Regexp used in filtering")) "")
8242 (if org-agenda-archives-mode
8243 (if (eq org-agenda-archives-mode t)
8244 " Archives"
8245 (format " :%s:" org-archive-tag))
8247 (if org-agenda-clockreport-mode " Clock" "")))
8248 (force-mode-line-update))
8250 (defun org-agenda-update-agenda-type ()
8251 "Update the agenda type after each command."
8252 (setq org-agenda-type
8253 (or (get-text-property (point) 'org-agenda-type)
8254 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8256 (defun org-agenda-next-line ()
8257 "Move cursor to the next line, and show if follow mode is active."
8258 (interactive)
8259 (call-interactively 'next-line)
8260 (org-agenda-do-context-action))
8262 (defun org-agenda-previous-line ()
8263 "Move cursor to the previous line, and show if follow-mode is active."
8264 (interactive)
8265 (call-interactively 'previous-line)
8266 (org-agenda-do-context-action))
8268 (defun org-agenda-next-item (n)
8269 "Move cursor to next agenda item."
8270 (interactive "p")
8271 (let ((col (current-column)))
8272 (dotimes (c n)
8273 (when (next-single-property-change (point-at-eol) 'org-marker)
8274 (move-end-of-line 1)
8275 (goto-char (next-single-property-change (point) 'org-marker))))
8276 (org-move-to-column col))
8277 (org-agenda-do-context-action))
8279 (defun org-agenda-previous-item (n)
8280 "Move cursor to next agenda item."
8281 (interactive "p")
8282 (dotimes (c n)
8283 (let ((col (current-column))
8284 (goto (save-excursion
8285 (move-end-of-line 0)
8286 (previous-single-property-change (point) 'org-marker))))
8287 (if goto (goto-char goto))
8288 (org-move-to-column col)))
8289 (org-agenda-do-context-action))
8291 (defun org-agenda-do-context-action ()
8292 "Show outline path and, maybe, follow mode window."
8293 (let ((m (org-get-at-bol 'org-marker)))
8294 (when (and (markerp m) (marker-buffer m))
8295 (and org-agenda-follow-mode
8296 (if org-agenda-follow-indirect
8297 (org-agenda-tree-to-indirect-buffer nil)
8298 (org-agenda-show)))
8299 (and org-agenda-show-outline-path
8300 (org-with-point-at m (org-display-outline-path t))))))
8302 (defun org-agenda-show-tags ()
8303 "Show the tags applicable to the current item."
8304 (interactive)
8305 (let* ((tags (org-get-at-bol 'tags)))
8306 (if tags
8307 (message "Tags are :%s:"
8308 (org-no-properties (mapconcat 'identity tags ":")))
8309 (message "No tags associated with this line"))))
8311 (defun org-agenda-goto (&optional highlight)
8312 "Go to the entry at point in the corresponding Org file."
8313 (interactive)
8314 (let* ((marker (or (org-get-at-bol 'org-marker)
8315 (org-agenda-error)))
8316 (buffer (marker-buffer marker))
8317 (pos (marker-position marker)))
8318 (switch-to-buffer-other-window buffer)
8319 (widen)
8320 (push-mark)
8321 (goto-char pos)
8322 (when (derived-mode-p 'org-mode)
8323 (org-show-context 'agenda)
8324 (recenter (/ (window-height) 2))
8325 (org-back-to-heading t)
8326 (let ((case-fold-search nil))
8327 (when (re-search-forward org-complex-heading-regexp nil t)
8328 (goto-char (match-beginning 4)))))
8329 (run-hooks 'org-agenda-after-show-hook)
8330 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8332 (defvar org-agenda-after-show-hook nil
8333 "Normal hook run after an item has been shown from the agenda.
8334 Point is in the buffer where the item originated.")
8336 (defun org-agenda-kill ()
8337 "Kill the entry or subtree belonging to the current agenda entry."
8338 (interactive)
8339 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8340 (let* ((bufname-orig (buffer-name))
8341 (marker (or (org-get-at-bol 'org-marker)
8342 (org-agenda-error)))
8343 (buffer (marker-buffer marker))
8344 (pos (marker-position marker))
8345 (type (org-get-at-bol 'type))
8346 dbeg dend (n 0) conf)
8347 (org-with-remote-undo buffer
8348 (with-current-buffer buffer
8349 (save-excursion
8350 (goto-char pos)
8351 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8352 (setq dbeg (progn (org-back-to-heading t) (point))
8353 dend (org-end-of-subtree t t))
8354 (setq dbeg (point-at-bol)
8355 dend (min (point-max) (1+ (point-at-eol)))))
8356 (goto-char dbeg)
8357 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8358 (setq conf (or (eq t org-agenda-confirm-kill)
8359 (and (numberp org-agenda-confirm-kill)
8360 (> n org-agenda-confirm-kill))))
8361 (and conf
8362 (not (y-or-n-p
8363 (format "Delete entry with %d lines in buffer \"%s\"? "
8364 n (buffer-name buffer))))
8365 (error "Abort"))
8366 (let ((org-agenda-buffer-name bufname-orig))
8367 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8368 (with-current-buffer buffer (delete-region dbeg dend))
8369 (message "Agenda item and source killed"))))
8371 (defvar org-archive-default-command) ; defined in org-archive.el
8372 (defun org-agenda-archive-default ()
8373 "Archive the entry or subtree belonging to the current agenda entry."
8374 (interactive)
8375 (require 'org-archive)
8376 (org-agenda-archive-with org-archive-default-command))
8378 (defun org-agenda-archive-default-with-confirmation ()
8379 "Archive the entry or subtree belonging to the current agenda entry."
8380 (interactive)
8381 (require 'org-archive)
8382 (org-agenda-archive-with org-archive-default-command 'confirm))
8384 (defun org-agenda-archive ()
8385 "Archive the entry or subtree belonging to the current agenda entry."
8386 (interactive)
8387 (org-agenda-archive-with 'org-archive-subtree))
8389 (defun org-agenda-archive-to-archive-sibling ()
8390 "Move the entry to the archive sibling."
8391 (interactive)
8392 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8394 (defun org-agenda-archive-with (cmd &optional confirm)
8395 "Move the entry to the archive sibling."
8396 (interactive)
8397 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8398 (let* ((bufname-orig (buffer-name))
8399 (marker (or (org-get-at-bol 'org-marker)
8400 (org-agenda-error)))
8401 (buffer (marker-buffer marker))
8402 (pos (marker-position marker)))
8403 (org-with-remote-undo buffer
8404 (with-current-buffer buffer
8405 (if (derived-mode-p 'org-mode)
8406 (if (and confirm
8407 (not (y-or-n-p "Archive this subtree or entry? ")))
8408 (error "Abort")
8409 (save-window-excursion
8410 (goto-char pos)
8411 (let ((org-agenda-buffer-name bufname-orig))
8412 (org-remove-subtree-entries-from-agenda))
8413 (org-back-to-heading t)
8414 (funcall cmd)))
8415 (error "Archiving works only in Org files"))))))
8417 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8418 "Remove all lines in the agenda that correspond to a given subtree.
8419 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8420 If this information is not given, the function uses the tree at point."
8421 (let ((buf (or buf (current-buffer))) m p)
8422 (save-excursion
8423 (unless (and beg end)
8424 (org-back-to-heading t)
8425 (setq beg (point))
8426 (org-end-of-subtree t)
8427 (setq end (point)))
8428 (set-buffer (get-buffer org-agenda-buffer-name))
8429 (save-excursion
8430 (goto-char (point-max))
8431 (beginning-of-line 1)
8432 (while (not (bobp))
8433 (when (and (setq m (org-get-at-bol 'org-marker))
8434 (equal buf (marker-buffer m))
8435 (setq p (marker-position m))
8436 (>= p beg)
8437 (< p end))
8438 (let ((inhibit-read-only t))
8439 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8440 (beginning-of-line 0))))))
8442 (defun org-agenda-refile (&optional goto rfloc no-update)
8443 "Refile the item at point.
8445 When called with `\\[universal-argument] \\[universal-argument]', \
8446 go to the location of the last
8447 refiled item.
8449 When called with `\\[universal-argument] \\[universal-argument] \
8450 \\[universal-argument]' prefix or when GOTO is 0, clear
8451 the refile cache.
8453 RFLOC can be a refile location obtained in a different way.
8455 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8456 (interactive "P")
8457 (cond
8458 ((member goto '(0 (64)))
8459 (org-refile-cache-clear))
8460 ((equal goto '(16))
8461 (org-refile-goto-last-stored))
8463 (let* ((buffer-orig (buffer-name))
8464 (marker (or (org-get-at-bol 'org-hd-marker)
8465 (org-agenda-error)))
8466 (buffer (marker-buffer marker))
8467 (pos (marker-position marker))
8468 (rfloc (or rfloc
8469 (org-refile-get-location
8470 (if goto "Goto" "Refile to") buffer
8471 org-refile-allow-creating-parent-nodes))))
8472 (with-current-buffer buffer
8473 (org-with-wide-buffer
8474 (goto-char marker)
8475 (let ((org-agenda-buffer-name buffer-orig))
8476 (org-remove-subtree-entries-from-agenda))
8477 (org-refile goto buffer rfloc))))
8478 (unless no-update (org-agenda-redo)))))
8480 (defun org-agenda-open-link (&optional arg)
8481 "Open the link(s) in the current entry, if any.
8482 This looks for a link in the displayed line in the agenda.
8483 It also looks at the text of the entry itself."
8484 (interactive "P")
8485 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8486 (org-get-at-bol 'org-marker)))
8487 (buffer (and marker (marker-buffer marker)))
8488 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8489 (lkall (and buffer (org-offer-links-in-entry
8490 buffer marker arg prefix)))
8491 (lk0 (car lkall))
8492 (lk (if (stringp lk0) (list lk0) lk0))
8493 (lkend (cdr lkall))
8494 trg)
8495 (cond
8496 ((and buffer lk)
8497 (mapcar (lambda(l)
8498 (with-current-buffer buffer
8499 (setq trg (and (string-match org-bracket-link-regexp l)
8500 (match-string 1 l)))
8501 (if (or (not trg) (string-match org-any-link-re trg))
8502 (org-with-wide-buffer
8503 (goto-char marker)
8504 (when (search-forward l nil lkend)
8505 (goto-char (match-beginning 0))
8506 (org-open-at-point)))
8507 ;; This is an internal link, widen the buffer
8508 (switch-to-buffer-other-window buffer)
8509 (widen)
8510 (goto-char marker)
8511 (when (search-forward l nil lkend)
8512 (goto-char (match-beginning 0))
8513 (org-open-at-point)))))
8514 lk))
8515 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8516 (save-excursion
8517 (beginning-of-line 1)
8518 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8519 (org-open-link-from-string (match-string 1)))
8520 (t (message "No link to open here")))))
8522 (defun org-agenda-copy-local-variable (var)
8523 "Get a variable from a referenced buffer and install it here."
8524 (let ((m (org-get-at-bol 'org-marker)))
8525 (when (and m (buffer-live-p (marker-buffer m)))
8526 (set (make-local-variable var)
8527 (with-current-buffer (marker-buffer m)
8528 (symbol-value var))))))
8530 (defun org-agenda-switch-to (&optional delete-other-windows)
8531 "Go to the Org mode file which contains the item at point.
8532 When optional argument DELETE-OTHER-WINDOWS is non-nil, the
8533 displayed Org file fills the frame."
8534 (interactive)
8535 (if (and org-return-follows-link
8536 (not (org-get-at-bol 'org-marker))
8537 (org-in-regexp org-bracket-link-regexp))
8538 (org-open-link-from-string (match-string 0))
8539 (let* ((marker (or (org-get-at-bol 'org-marker)
8540 (org-agenda-error)))
8541 (buffer (marker-buffer marker))
8542 (pos (marker-position marker)))
8543 (unless buffer (user-error "Trying to switch to non-existent buffer"))
8544 (pop-to-buffer-same-window buffer)
8545 (when delete-other-windows (delete-other-windows))
8546 (widen)
8547 (goto-char pos)
8548 (when (derived-mode-p 'org-mode)
8549 (org-show-context 'agenda)
8550 (run-hooks 'org-agenda-after-show-hook)))))
8552 (defun org-agenda-goto-mouse (ev)
8553 "Go to the Org file which contains the item at the mouse click."
8554 (interactive "e")
8555 (mouse-set-point ev)
8556 (org-agenda-goto))
8558 (defun org-agenda-show (&optional full-entry)
8559 "Display the Org file which contains the item at point.
8560 With prefix argument FULL-ENTRY, make the entire entry visible
8561 if it was hidden in the outline."
8562 (interactive "P")
8563 (let ((win (selected-window)))
8564 (org-agenda-goto t)
8565 (when full-entry (org-show-entry))
8566 (select-window win)))
8568 (defvar org-agenda-show-window nil)
8569 (defun org-agenda-show-and-scroll-up (&optional arg)
8570 "Display the Org file which contains the item at point.
8572 When called repeatedly, scroll the window that is displaying the buffer.
8574 With a `\\[universal-argument]' prefix, use `org-show-entry' instead of \
8575 `outline-show-subtree'
8576 to display the item, so that drawers and logbooks stay folded."
8577 (interactive "P")
8578 (let ((win (selected-window)))
8579 (if (and (window-live-p org-agenda-show-window)
8580 (eq this-command last-command))
8581 (progn
8582 (select-window org-agenda-show-window)
8583 (ignore-errors (scroll-up)))
8584 (org-agenda-goto t)
8585 (if arg (org-show-entry) (outline-show-subtree))
8586 (setq org-agenda-show-window (selected-window)))
8587 (select-window win)))
8589 (defun org-agenda-show-scroll-down ()
8590 "Scroll down the window showing the agenda."
8591 (interactive)
8592 (let ((win (selected-window)))
8593 (when (window-live-p org-agenda-show-window)
8594 (select-window org-agenda-show-window)
8595 (ignore-errors (scroll-down))
8596 (select-window win))))
8598 (defun org-agenda-show-1 (&optional more)
8599 "Display the Org file which contains the item at point.
8600 The prefix arg selects the amount of information to display:
8602 0 hide the subtree
8603 1 just show the entry according to defaults.
8604 2 show the children view
8605 3 show the subtree view
8606 4 show the entire subtree and any LOGBOOK drawers
8607 5 show the entire subtree and any drawers
8608 With prefix argument FULL-ENTRY, make the entire entry visible
8609 if it was hidden in the outline."
8610 (interactive "p")
8611 (let ((win (selected-window)))
8612 (org-agenda-goto t)
8613 (org-back-to-heading)
8614 (set-window-start (selected-window) (point-at-bol))
8615 (cond
8616 ((= more 0)
8617 (outline-hide-subtree)
8618 (save-excursion
8619 (org-back-to-heading)
8620 (run-hook-with-args 'org-cycle-hook 'folded))
8621 (message "Remote: FOLDED"))
8622 ((and (called-interactively-p 'any) (= more 1))
8623 (message "Remote: show with default settings"))
8624 ((= more 2)
8625 (outline-show-entry)
8626 (org-show-children)
8627 (save-excursion
8628 (org-back-to-heading)
8629 (run-hook-with-args 'org-cycle-hook 'children))
8630 (message "Remote: CHILDREN"))
8631 ((= more 3)
8632 (outline-show-subtree)
8633 (save-excursion
8634 (org-back-to-heading)
8635 (run-hook-with-args 'org-cycle-hook 'subtree))
8636 (message "Remote: SUBTREE"))
8637 ((= more 4)
8638 (outline-show-subtree)
8639 (save-excursion
8640 (org-back-to-heading)
8641 (org-cycle-hide-drawers 'subtree '("LOGBOOK")))
8642 (message "Remote: SUBTREE AND LOGBOOK"))
8643 ((> more 4)
8644 (outline-show-subtree)
8645 (message "Remote: SUBTREE AND ALL DRAWERS")))
8646 (select-window win)))
8648 (defvar org-agenda-cycle-counter nil)
8649 (defun org-agenda-cycle-show (&optional n)
8650 "Show the current entry in another window, with default settings.
8652 Default settings are taken from `org-show-context-detail'. When
8653 use repeatedly in immediate succession, the remote entry will
8654 cycle through visibility
8656 children -> subtree -> folded
8658 When called with a numeric prefix arg, that arg will be passed through to
8659 `org-agenda-show-1'. For the interpretation of that argument, see the
8660 docstring of `org-agenda-show-1'."
8661 (interactive "P")
8662 (if (integerp n)
8663 (setq org-agenda-cycle-counter n)
8664 (if (not (eq last-command this-command))
8665 (setq org-agenda-cycle-counter 1)
8666 (if (equal org-agenda-cycle-counter 0)
8667 (setq org-agenda-cycle-counter 2)
8668 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8669 (if (> org-agenda-cycle-counter 3)
8670 (setq org-agenda-cycle-counter 0)))))
8671 (org-agenda-show-1 org-agenda-cycle-counter))
8673 (defun org-agenda-recenter (arg)
8674 "Display the Org file which contains the item at point and recenter."
8675 (interactive "P")
8676 (let ((win (selected-window)))
8677 (org-agenda-goto t)
8678 (recenter arg)
8679 (select-window win)))
8681 (defun org-agenda-show-mouse (ev)
8682 "Display the Org file which contains the item at the mouse click."
8683 (interactive "e")
8684 (mouse-set-point ev)
8685 (org-agenda-show))
8687 (defun org-agenda-check-no-diary ()
8688 "Check if the entry is a diary link and abort if yes."
8689 (if (org-get-at-bol 'org-agenda-diary-link)
8690 (org-agenda-error)))
8692 (defun org-agenda-error ()
8693 "Throw an error when a command is not allowed in the agenda."
8694 (user-error "Command not allowed in this line"))
8696 (defun org-agenda-tree-to-indirect-buffer (arg)
8697 "Show the subtree corresponding to the current entry in an indirect buffer.
8698 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8700 With a numerical prefix ARG, go up to this level and then take that tree.
8701 With a negative numeric ARG, go up by this number of levels.
8703 With a `\\[universal-argument]' prefix, make a separate frame for this tree, \
8704 i.e. don't use
8705 the dedicated frame."
8706 (interactive "P")
8707 (if current-prefix-arg
8708 (org-agenda-do-tree-to-indirect-buffer arg)
8709 (let ((agenda-buffer (buffer-name))
8710 (agenda-window (selected-window))
8711 (indirect-window
8712 (and org-last-indirect-buffer
8713 (get-buffer-window org-last-indirect-buffer))))
8714 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8715 (unless (or (eq org-indirect-buffer-display 'new-frame)
8716 (eq org-indirect-buffer-display 'dedicated-frame))
8717 (unwind-protect
8718 (unless (and indirect-window (window-live-p indirect-window))
8719 (setq indirect-window (split-window agenda-window)))
8720 (and indirect-window (select-window indirect-window))
8721 (switch-to-buffer org-last-indirect-buffer :norecord)
8722 (fit-window-to-buffer indirect-window)))
8723 (select-window (get-buffer-window agenda-buffer))
8724 (setq org-agenda-last-indirect-buffer org-last-indirect-buffer))))
8726 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8727 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8728 (org-agenda-check-no-diary)
8729 (let* ((marker (or (org-get-at-bol 'org-marker)
8730 (org-agenda-error)))
8731 (buffer (marker-buffer marker))
8732 (pos (marker-position marker)))
8733 (with-current-buffer buffer
8734 (save-excursion
8735 (goto-char pos)
8736 (funcall 'org-tree-to-indirect-buffer arg)))))
8738 (defvar org-last-heading-marker (make-marker)
8739 "Marker pointing to the headline that last changed its TODO state
8740 by a remote command from the agenda.")
8742 (defun org-agenda-todo-nextset ()
8743 "Switch TODO entry to next sequence."
8744 (interactive)
8745 (org-agenda-todo 'nextset))
8747 (defun org-agenda-todo-previousset ()
8748 "Switch TODO entry to previous sequence."
8749 (interactive)
8750 (org-agenda-todo 'previousset))
8752 (defun org-agenda-todo (&optional arg)
8753 "Cycle TODO state of line at point, also in Org file.
8754 This changes the line at point, all other lines in the agenda referring to
8755 the same tree node, and the headline of the tree node in the Org file."
8756 (interactive "P")
8757 (org-agenda-check-no-diary)
8758 (let* ((col (current-column))
8759 (marker (or (org-get-at-bol 'org-marker)
8760 (org-agenda-error)))
8761 (buffer (marker-buffer marker))
8762 (pos (marker-position marker))
8763 (hdmarker (org-get-at-bol 'org-hd-marker))
8764 (todayp (org-agenda-today-p (org-get-at-bol 'day)))
8765 (inhibit-read-only t)
8766 org-agenda-headline-snapshot-before-repeat newhead just-one)
8767 (org-with-remote-undo buffer
8768 (with-current-buffer buffer
8769 (widen)
8770 (goto-char pos)
8771 (org-show-context 'agenda)
8772 (let ((current-prefix-arg arg))
8773 (call-interactively 'org-todo))
8774 (and (bolp) (forward-char 1))
8775 (setq newhead (org-get-heading))
8776 (when (and (bound-and-true-p
8777 org-agenda-headline-snapshot-before-repeat)
8778 (not (equal org-agenda-headline-snapshot-before-repeat
8779 newhead))
8780 todayp)
8781 (setq newhead org-agenda-headline-snapshot-before-repeat
8782 just-one t))
8783 (save-excursion
8784 (org-back-to-heading)
8785 (move-marker org-last-heading-marker (point))))
8786 (beginning-of-line 1)
8787 (save-window-excursion
8788 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8789 (when (bound-and-true-p org-clock-out-when-done)
8790 (string-match (concat "^" (regexp-opt org-done-keywords-for-agenda))
8791 newhead)
8792 (org-agenda-unmark-clocking-task))
8793 (org-move-to-column col)
8794 (org-agenda-mark-clocking-task))))
8796 (defun org-agenda-add-note (&optional arg)
8797 "Add a time-stamped note to the entry at point."
8798 (interactive "P")
8799 (org-agenda-check-no-diary)
8800 (let* ((marker (or (org-get-at-bol 'org-marker)
8801 (org-agenda-error)))
8802 (buffer (marker-buffer marker))
8803 (pos (marker-position marker))
8804 (hdmarker (org-get-at-bol 'org-hd-marker))
8805 (inhibit-read-only t))
8806 (with-current-buffer buffer
8807 (widen)
8808 (goto-char pos)
8809 (org-show-context 'agenda)
8810 (org-add-note))))
8812 (defun org-agenda-change-all-lines (newhead hdmarker
8813 &optional fixface just-this)
8814 "Change all lines in the agenda buffer which match HDMARKER.
8815 The new content of the line will be NEWHEAD (as modified by
8816 `org-agenda-format-item'). HDMARKER is checked with
8817 `equal' against all `org-hd-marker' text properties in the file.
8818 If FIXFACE is non-nil, the face of each item is modified according to
8819 the new TODO state.
8820 If JUST-THIS is non-nil, change just the current line, not all.
8821 If FORCE-TAGS is non nil, the car of it returns the new tags."
8822 (let* ((inhibit-read-only t)
8823 (line (org-current-line))
8824 (org-agenda-buffer (current-buffer))
8825 (thetags (with-current-buffer (marker-buffer hdmarker)
8826 (org-with-wide-buffer
8827 (goto-char hdmarker)
8828 (org-get-tags-at))))
8829 props m pl undone-face done-face finish new dotime level cat tags)
8830 (save-excursion
8831 (goto-char (point-max))
8832 (beginning-of-line 1)
8833 (while (not finish)
8834 (setq finish (bobp))
8835 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8836 (or (not just-this) (= (org-current-line) line))
8837 (equal m hdmarker))
8838 (setq props (text-properties-at (point))
8839 dotime (org-get-at-bol 'dotime)
8840 cat (org-get-at-eol 'org-category 1)
8841 level (org-get-at-bol 'level)
8842 tags thetags
8844 (let ((org-prefix-format-compiled
8845 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8846 org-prefix-format-compiled))
8847 (extra (org-get-at-bol 'extra)))
8848 (with-current-buffer (marker-buffer hdmarker)
8849 (org-with-wide-buffer
8850 (org-agenda-format-item extra newhead level cat tags dotime))))
8851 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8852 undone-face (org-get-at-bol 'undone-face)
8853 done-face (org-get-at-bol 'done-face))
8854 (beginning-of-line 1)
8855 (cond
8856 ((equal new "") (delete-region (point) (line-beginning-position 2)))
8857 ((looking-at ".*")
8858 ;; When replacing the whole line, preserve bulk mark
8859 ;; overlay, if any.
8860 (let ((mark (catch :overlay
8861 (dolist (o (overlays-in (point) (+ 2 (point))))
8862 (when (eq (overlay-get o 'type)
8863 'org-marked-entry-overlay)
8864 (throw :overlay o))))))
8865 (replace-match new t t)
8866 (beginning-of-line)
8867 (when mark (move-overlay mark (point) (+ 2 (point)))))
8868 (add-text-properties (point-at-bol) (point-at-eol) props)
8869 (when fixface
8870 (add-text-properties
8871 (point-at-bol) (point-at-eol)
8872 (list 'face
8873 (if org-last-todo-state-is-todo
8874 undone-face done-face))))
8875 (org-agenda-highlight-todo 'line)
8876 (beginning-of-line 1))
8877 (t (error "Line update did not work")))
8878 (save-restriction
8879 (narrow-to-region (point-at-bol) (point-at-eol))
8880 (org-agenda-finalize)))
8881 (beginning-of-line 0)))))
8883 (defun org-agenda-align-tags (&optional line)
8884 "Align all tags in agenda items to `org-agenda-tags-column'."
8885 (let ((inhibit-read-only t) l c)
8886 (save-excursion
8887 (goto-char (if line (point-at-bol) (point-min)))
8888 (while (re-search-forward "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"
8889 (if line (point-at-eol) nil) t)
8890 (add-text-properties
8891 (match-beginning 2) (match-end 2)
8892 (list 'face (delq nil (let ((prop (get-text-property
8893 (match-beginning 2) 'face)))
8894 (or (listp prop) (setq prop (list prop)))
8895 (if (memq 'org-tag prop)
8896 prop
8897 (cons 'org-tag prop))))))
8898 (setq l (- (match-end 2) (match-beginning 2))
8899 c (if (< org-agenda-tags-column 0)
8900 (- (abs org-agenda-tags-column) l)
8901 org-agenda-tags-column))
8902 (delete-region (match-beginning 1) (match-end 1))
8903 (goto-char (match-beginning 1))
8904 (insert (org-add-props
8905 (make-string (max 1 (- c (current-column))) ?\ )
8906 (plist-put (copy-sequence (text-properties-at (point)))
8907 'face nil))))
8908 (goto-char (point-min))
8909 (org-font-lock-add-tag-faces (point-max)))))
8911 (defun org-agenda-priority-up ()
8912 "Increase the priority of line at point, also in Org file."
8913 (interactive)
8914 (org-agenda-priority 'up))
8916 (defun org-agenda-priority-down ()
8917 "Decrease the priority of line at point, also in Org file."
8918 (interactive)
8919 (org-agenda-priority 'down))
8921 (defun org-agenda-priority (&optional force-direction)
8922 "Set the priority of line at point, also in Org file.
8923 This changes the line at point, all other lines in the agenda referring to
8924 the same tree node, and the headline of the tree node in the Org file.
8925 Called with a universal prefix arg, show the priority instead of setting it."
8926 (interactive "P")
8927 (if (equal force-direction '(4))
8928 (org-show-priority)
8929 (unless org-enable-priority-commands
8930 (error "Priority commands are disabled"))
8931 (org-agenda-check-no-diary)
8932 (let* ((col (current-column))
8933 (marker (or (org-get-at-bol 'org-marker)
8934 (org-agenda-error)))
8935 (hdmarker (org-get-at-bol 'org-hd-marker))
8936 (buffer (marker-buffer hdmarker))
8937 (pos (marker-position hdmarker))
8938 (inhibit-read-only t)
8939 newhead)
8940 (org-with-remote-undo buffer
8941 (with-current-buffer buffer
8942 (widen)
8943 (goto-char pos)
8944 (org-show-context 'agenda)
8945 (funcall 'org-priority force-direction)
8946 (end-of-line 1)
8947 (setq newhead (org-get-heading)))
8948 (org-agenda-change-all-lines newhead hdmarker)
8949 (org-move-to-column col)))))
8951 ;; FIXME: should fix the tags property of the agenda line.
8952 (defun org-agenda-set-tags (&optional tag onoff)
8953 "Set tags for the current headline."
8954 (interactive)
8955 (org-agenda-check-no-diary)
8956 (if (and (org-region-active-p) (called-interactively-p 'any))
8957 (call-interactively 'org-change-tag-in-region)
8958 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8959 (org-agenda-error)))
8960 (buffer (marker-buffer hdmarker))
8961 (pos (marker-position hdmarker))
8962 (inhibit-read-only t)
8963 newhead)
8964 (org-with-remote-undo buffer
8965 (with-current-buffer buffer
8966 (widen)
8967 (goto-char pos)
8968 (org-show-context 'agenda)
8969 (if tag
8970 (org-toggle-tag tag onoff)
8971 (call-interactively 'org-set-tags))
8972 (end-of-line 1)
8973 (setq newhead (org-get-heading)))
8974 (org-agenda-change-all-lines newhead hdmarker)
8975 (beginning-of-line 1)))))
8977 (defun org-agenda-set-property ()
8978 "Set a property for the current headline."
8979 (interactive)
8980 (org-agenda-check-no-diary)
8981 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8982 (org-agenda-error)))
8983 (buffer (marker-buffer hdmarker))
8984 (pos (marker-position hdmarker))
8985 (inhibit-read-only t)
8986 newhead)
8987 (org-with-remote-undo buffer
8988 (with-current-buffer buffer
8989 (widen)
8990 (goto-char pos)
8991 (org-show-context 'agenda)
8992 (call-interactively 'org-set-property)))))
8994 (defun org-agenda-set-effort ()
8995 "Set the effort property for the current headline."
8996 (interactive)
8997 (org-agenda-check-no-diary)
8998 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8999 (org-agenda-error)))
9000 (buffer (marker-buffer hdmarker))
9001 (pos (marker-position hdmarker))
9002 (inhibit-read-only t)
9003 newhead)
9004 (org-with-remote-undo buffer
9005 (with-current-buffer buffer
9006 (widen)
9007 (goto-char pos)
9008 (org-show-context 'agenda)
9009 (call-interactively 'org-set-effort)
9010 (end-of-line 1)
9011 (setq newhead (org-get-heading)))
9012 (org-agenda-change-all-lines newhead hdmarker))))
9014 (defun org-agenda-toggle-archive-tag ()
9015 "Toggle the archive tag for the current entry."
9016 (interactive)
9017 (org-agenda-check-no-diary)
9018 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9019 (org-agenda-error)))
9020 (buffer (marker-buffer hdmarker))
9021 (pos (marker-position hdmarker))
9022 (inhibit-read-only t)
9023 newhead)
9024 (org-with-remote-undo buffer
9025 (with-current-buffer buffer
9026 (widen)
9027 (goto-char pos)
9028 (org-show-context 'agenda)
9029 (call-interactively 'org-toggle-archive-tag)
9030 (end-of-line 1)
9031 (setq newhead (org-get-heading)))
9032 (org-agenda-change-all-lines newhead hdmarker)
9033 (beginning-of-line 1))))
9035 (defun org-agenda-do-date-later (arg)
9036 (interactive "P")
9037 (cond
9038 ((or (equal arg '(16))
9039 (memq last-command
9040 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9041 (setq this-command 'org-agenda-date-later-minutes)
9042 (org-agenda-date-later-minutes 1))
9043 ((or (equal arg '(4))
9044 (memq last-command
9045 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9046 (setq this-command 'org-agenda-date-later-hours)
9047 (org-agenda-date-later-hours 1))
9049 (org-agenda-date-later (prefix-numeric-value arg)))))
9051 (defun org-agenda-do-date-earlier (arg)
9052 (interactive "P")
9053 (cond
9054 ((or (equal arg '(16))
9055 (memq last-command
9056 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9057 (setq this-command 'org-agenda-date-earlier-minutes)
9058 (org-agenda-date-earlier-minutes 1))
9059 ((or (equal arg '(4))
9060 (memq last-command
9061 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9062 (setq this-command 'org-agenda-date-earlier-hours)
9063 (org-agenda-date-earlier-hours 1))
9065 (org-agenda-date-earlier (prefix-numeric-value arg)))))
9067 (defun org-agenda-date-later (arg &optional what)
9068 "Change the date of this item to ARG day(s) later."
9069 (interactive "p")
9070 (org-agenda-check-type t 'agenda 'timeline)
9071 (org-agenda-check-no-diary)
9072 (let* ((marker (or (org-get-at-bol 'org-marker)
9073 (org-agenda-error)))
9074 (buffer (marker-buffer marker))
9075 (pos (marker-position marker))
9076 cdate today)
9077 (org-with-remote-undo buffer
9078 (with-current-buffer buffer
9079 (widen)
9080 (goto-char pos)
9081 (if (not (org-at-timestamp-p))
9082 (error "Cannot find time stamp"))
9083 (when (and org-agenda-move-date-from-past-immediately-to-today
9084 (equal arg 1)
9085 (or (not what) (eq what 'day))
9086 (not (save-match-data (org-at-date-range-p))))
9087 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9088 cdate (calendar-absolute-from-gregorian
9089 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9090 today (org-today))
9091 (if (> today cdate)
9092 ;; immediately shift to today
9093 (setq arg (- today cdate))))
9094 (org-timestamp-change arg (or what 'day))
9095 (when (and (org-at-date-range-p)
9096 (re-search-backward org-tr-regexp-both (point-at-bol)))
9097 (let ((end org-last-changed-timestamp))
9098 (org-timestamp-change arg (or what 'day))
9099 (setq org-last-changed-timestamp
9100 (concat org-last-changed-timestamp "--" end)))))
9101 (org-agenda-show-new-time marker org-last-changed-timestamp))
9102 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9104 (defun org-agenda-date-earlier (arg &optional what)
9105 "Change the date of this item to ARG day(s) earlier."
9106 (interactive "p")
9107 (org-agenda-date-later (- arg) what))
9109 (defun org-agenda-date-later-minutes (arg)
9110 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9111 (interactive "p")
9112 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9113 (org-agenda-date-later arg 'minute))
9115 (defun org-agenda-date-earlier-minutes (arg)
9116 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9117 (interactive "p")
9118 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9119 (org-agenda-date-earlier arg 'minute))
9121 (defun org-agenda-date-later-hours (arg)
9122 "Change the time of this item, in hour steps."
9123 (interactive "p")
9124 (org-agenda-date-later arg 'hour))
9126 (defun org-agenda-date-earlier-hours (arg)
9127 "Change the time of this item, in hour steps."
9128 (interactive "p")
9129 (org-agenda-date-earlier arg 'hour))
9131 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9132 "Show new date stamp via text properties."
9133 ;; We use text properties to make this undoable
9134 (let ((inhibit-read-only t))
9135 (setq stamp (concat prefix " => " stamp " "))
9136 (save-excursion
9137 (goto-char (point-max))
9138 (while (not (bobp))
9139 (when (equal marker (org-get-at-bol 'org-marker))
9140 (remove-text-properties (point-at-bol) (point-at-eol) '(display))
9141 (org-move-to-column (- (window-width) (length stamp)) t)
9142 (add-text-properties
9143 (1- (point)) (point-at-eol)
9144 (list 'display (org-add-props stamp nil
9145 'face '(secondary-selection default))))
9146 (beginning-of-line 1))
9147 (beginning-of-line 0)))))
9149 (defun org-agenda-date-prompt (arg)
9150 "Change the date of this item. Date is prompted for, with default today.
9151 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9152 be used to request time specification in the time stamp."
9153 (interactive "P")
9154 (org-agenda-check-type t 'agenda 'timeline)
9155 (org-agenda-check-no-diary)
9156 (let* ((marker (or (org-get-at-bol 'org-marker)
9157 (org-agenda-error)))
9158 (buffer (marker-buffer marker))
9159 (pos (marker-position marker)))
9160 (org-with-remote-undo buffer
9161 (with-current-buffer buffer
9162 (widen)
9163 (goto-char pos)
9164 (if (not (org-at-timestamp-p t))
9165 (error "Cannot find time stamp"))
9166 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
9167 (org-agenda-show-new-time marker org-last-changed-timestamp))
9168 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9170 (defun org-agenda-schedule (arg &optional time)
9171 "Schedule the item at point.
9172 ARG is passed through to `org-schedule'."
9173 (interactive "P")
9174 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9175 (org-agenda-check-no-diary)
9176 (let* ((marker (or (org-get-at-bol 'org-marker)
9177 (org-agenda-error)))
9178 (type (marker-insertion-type marker))
9179 (buffer (marker-buffer marker))
9180 (pos (marker-position marker))
9182 (set-marker-insertion-type marker t)
9183 (org-with-remote-undo buffer
9184 (with-current-buffer buffer
9185 (widen)
9186 (goto-char pos)
9187 (setq ts (org-schedule arg time)))
9188 (org-agenda-show-new-time marker ts " S"))
9189 (message "%s" ts)))
9191 (defun org-agenda-deadline (arg &optional time)
9192 "Schedule the item at point.
9193 ARG is passed through to `org-deadline'."
9194 (interactive "P")
9195 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9196 (org-agenda-check-no-diary)
9197 (let* ((marker (or (org-get-at-bol 'org-marker)
9198 (org-agenda-error)))
9199 (buffer (marker-buffer marker))
9200 (pos (marker-position marker))
9202 (org-with-remote-undo buffer
9203 (with-current-buffer buffer
9204 (widen)
9205 (goto-char pos)
9206 (setq ts (org-deadline arg time)))
9207 (org-agenda-show-new-time marker ts " D"))
9208 (message "%s" ts)))
9210 (defun org-agenda-clock-in (&optional arg)
9211 "Start the clock on the currently selected item."
9212 (interactive "P")
9213 (org-agenda-check-no-diary)
9214 (if (equal arg '(4))
9215 (org-clock-in arg)
9216 (let* ((marker (or (org-get-at-bol 'org-marker)
9217 (org-agenda-error)))
9218 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9219 (pos (marker-position marker))
9220 (col (current-column))
9221 newhead)
9222 (org-with-remote-undo (marker-buffer marker)
9223 (with-current-buffer (marker-buffer marker)
9224 (widen)
9225 (goto-char pos)
9226 (org-show-context 'agenda)
9227 (org-cycle-hide-drawers 'children)
9228 (org-clock-in arg)
9229 (setq newhead (org-get-heading)))
9230 (org-agenda-change-all-lines newhead hdmarker))
9231 (org-move-to-column col))))
9233 (defun org-agenda-clock-out ()
9234 "Stop the currently running clock."
9235 (interactive)
9236 (unless (marker-buffer org-clock-marker)
9237 (error "No running clock"))
9238 (let ((marker (make-marker)) (col (current-column)) newhead)
9239 (org-with-remote-undo (marker-buffer org-clock-marker)
9240 (with-current-buffer (marker-buffer org-clock-marker)
9241 (org-with-wide-buffer
9242 (goto-char org-clock-marker)
9243 (org-back-to-heading t)
9244 (move-marker marker (point))
9245 (org-clock-out)
9246 (setq newhead (org-get-heading)))))
9247 (org-agenda-change-all-lines newhead marker)
9248 (move-marker marker nil)
9249 (org-move-to-column col)
9250 (org-agenda-unmark-clocking-task)))
9252 (defun org-agenda-clock-cancel (&optional arg)
9253 "Cancel the currently running clock."
9254 (interactive "P")
9255 (unless (marker-buffer org-clock-marker)
9256 (user-error "No running clock"))
9257 (org-with-remote-undo (marker-buffer org-clock-marker)
9258 (org-clock-cancel)))
9260 (defun org-agenda-clock-goto ()
9261 "Jump to the currently clocked in task within the agenda.
9262 If the currently clocked in task is not listed in the agenda
9263 buffer, display it in another window."
9264 (interactive)
9265 (let (pos)
9266 (mapc (lambda (o)
9267 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
9268 (setq pos (overlay-start o))))
9269 (overlays-in (point-min) (point-max)))
9270 (cond (pos (goto-char pos))
9271 ;; If the currently clocked entry is not in the agenda
9272 ;; buffer, we visit it in another window:
9273 ((bound-and-true-p org-clock-current-task)
9274 (org-switch-to-buffer-other-window (org-clock-goto)))
9275 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9277 (defun org-agenda-diary-entry-in-org-file ()
9278 "Make a diary entry in the file `org-agenda-diary-file'."
9279 (let (d1 d2 char (text "") dp1 dp2)
9280 (if (equal (buffer-name) "*Calendar*")
9281 (setq d1 (calendar-cursor-to-date t)
9282 d2 (car calendar-mark-ring))
9283 (setq dp1 (get-text-property (point-at-bol) 'day))
9284 (unless dp1 (user-error "No date defined in current line"))
9285 (setq d1 (calendar-gregorian-from-absolute dp1)
9286 d2 (and (ignore-errors (mark))
9287 (save-excursion
9288 (goto-char (mark))
9289 (setq dp2 (get-text-property (point-at-bol) 'day)))
9290 (calendar-gregorian-from-absolute dp2))))
9291 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9292 (setq char (read-char-exclusive))
9293 (cond
9294 ((equal char ?d)
9295 (setq text (read-string "Day entry: "))
9296 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9297 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9298 ((equal char ?a)
9299 (setq d1 (list (car d1) (nth 1 d1)
9300 (read-number (format "Reference year [%d]: " (nth 2 d1))
9301 (nth 2 d1))))
9302 (setq text (read-string "Anniversary (use %d to show years): "))
9303 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9304 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9305 ((equal char ?b)
9306 (setq text (read-string "Block entry: "))
9307 (unless (and d1 d2 (not (equal d1 d2)))
9308 (user-error "No block of days selected"))
9309 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9310 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9311 ((equal char ?j)
9312 (org-switch-to-buffer-other-window
9313 (find-file-noselect org-agenda-diary-file))
9314 (require 'org-datetree)
9315 (org-datetree-find-date-create d1)
9316 (org-reveal t))
9317 (t (user-error "Invalid selection character `%c'" char)))))
9319 (defcustom org-agenda-insert-diary-strategy 'date-tree
9320 "Where in `org-agenda-diary-file' should new entries be added?
9321 Valid values:
9323 date-tree in the date tree, as first child of the date
9324 date-tree-last in the date tree, as last child of the date
9325 top-level as top-level entries at the end of the file."
9326 :group 'org-agenda
9327 :type '(choice
9328 (const :tag "first in a date tree" date-tree)
9329 (const :tag "last in a date tree" date-tree-last)
9330 (const :tag "as top level at end of file" top-level)))
9332 (defcustom org-agenda-insert-diary-extract-time nil
9333 "Non-nil means extract any time specification from the diary entry."
9334 :group 'org-agenda
9335 :version "24.1"
9336 :type 'boolean)
9338 (defcustom org-agenda-bulk-mark-char ">"
9339 "A single-character string to be used as the bulk mark."
9340 :group 'org-agenda
9341 :version "24.1"
9342 :type 'string)
9344 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9345 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9346 If TEXT is not empty, it will become the headline of the new entry, and
9347 the resulting entry will not be shown. When TEXT is empty, switch to
9348 `org-agenda-diary-file' and let the user finish the entry there."
9349 (let ((cw (current-window-configuration)))
9350 (org-switch-to-buffer-other-window
9351 (find-file-noselect org-agenda-diary-file))
9352 (widen)
9353 (goto-char (point-min))
9354 (cond
9355 ((eq type 'anniversary)
9356 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
9357 (progn
9358 (or (org-at-heading-p t)
9359 (progn
9360 (outline-next-heading)
9361 (insert "* Anniversaries\n\n")
9362 (beginning-of-line -1)))))
9363 (outline-next-heading)
9364 (org-back-over-empty-lines)
9365 (backward-char 1)
9366 (insert "\n")
9367 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9368 (nth 2 d1) (car d1) (nth 1 d1) text)))
9369 ((eq type 'day)
9370 (let ((org-prefix-has-time t)
9371 (org-agenda-time-leading-zero t)
9372 fmt time time2)
9373 (if org-agenda-insert-diary-extract-time
9374 ;; Use org-agenda-format-item to parse text for a time-range and
9375 ;; remove it. FIXME: This is a hack, we should refactor
9376 ;; that function to make time extraction available separately
9377 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9378 time (get-text-property 0 'time fmt)
9379 time2 (if (> (length time) 0)
9380 ;; split-string removes trailing ...... if
9381 ;; no end time given. First space
9382 ;; separates time from date.
9383 (concat " " (car (split-string time "\\.")))
9384 nil)
9385 text (get-text-property 0 'txt fmt)))
9386 (if (eq org-agenda-insert-diary-strategy 'top-level)
9387 (org-agenda-insert-diary-as-top-level text)
9388 (require 'org-datetree)
9389 (org-datetree-find-date-create d1)
9390 (org-agenda-insert-diary-make-new-entry text))
9391 (org-insert-time-stamp (org-time-from-absolute
9392 (calendar-absolute-from-gregorian d1))
9393 nil nil nil nil time2))
9394 (end-of-line 0))
9395 ((eq type 'block)
9396 (if (> (calendar-absolute-from-gregorian d1)
9397 (calendar-absolute-from-gregorian d2))
9398 (setq d1 (prog1 d2 (setq d2 d1))))
9399 (if (eq org-agenda-insert-diary-strategy 'top-level)
9400 (org-agenda-insert-diary-as-top-level text)
9401 (require 'org-datetree)
9402 (org-datetree-find-date-create d1)
9403 (org-agenda-insert-diary-make-new-entry text))
9404 (org-insert-time-stamp (org-time-from-absolute
9405 (calendar-absolute-from-gregorian d1)))
9406 (insert "--")
9407 (org-insert-time-stamp (org-time-from-absolute
9408 (calendar-absolute-from-gregorian d2)))
9409 (end-of-line 0)))
9410 (if (string-match "\\S-" text)
9411 (progn
9412 (set-window-configuration cw)
9413 (message "%s entry added to %s"
9414 (capitalize (symbol-name type))
9415 (abbreviate-file-name org-agenda-diary-file)))
9416 (org-reveal t)
9417 (message "Please finish entry here"))))
9419 (defun org-agenda-insert-diary-as-top-level (text)
9420 "Make new entry as a top-level entry at the end of the file.
9421 Add TEXT as headline, and position the cursor in the second line so that
9422 a timestamp can be added there."
9423 (widen)
9424 (goto-char (point-max))
9425 (unless (bolp) (insert "\n"))
9426 (org-insert-heading nil t t)
9427 (insert text)
9428 (org-end-of-meta-data)
9429 (unless (bolp) (insert "\n"))
9430 (when org-adapt-indentation (indent-to-column 2)))
9432 (defun org-agenda-insert-diary-make-new-entry (text)
9433 "Make a new entry with TEXT as a child of the current subtree.
9434 Position the point in the heading's first body line so that
9435 a timestamp can be added there."
9436 (cond
9437 ((eq org-agenda-insert-diary-strategy 'date-tree-last)
9438 (end-of-line)
9439 (org-insert-heading '(4) t)
9440 (org-do-demote))
9442 (outline-next-heading)
9443 (org-back-over-empty-lines)
9444 (unless (looking-at "[ \t]*$") (save-excursion (insert "\n")))
9445 (org-insert-heading nil t)
9446 (org-do-demote)))
9447 (let ((col (current-column)))
9448 (insert text)
9449 (org-end-of-meta-data)
9450 ;; Ensure point is left on a blank line, at proper indentation.
9451 (unless (bolp) (insert "\n"))
9452 (unless (looking-at-p "^[ \t]*$") (save-excursion (insert "\n")))
9453 (when org-adapt-indentation (indent-to-column col)))
9454 (org-show-set-visibility 'lineage))
9456 (defun org-agenda-diary-entry ()
9457 "Make a diary entry, like the `i' command from the calendar.
9458 All the standard commands work: block, weekly etc.
9459 When `org-agenda-diary-file' points to a file,
9460 `org-agenda-diary-entry-in-org-file' is called instead to create
9461 entries in that Org file."
9462 (interactive)
9463 (if (not (eq org-agenda-diary-file 'diary-file))
9464 (org-agenda-diary-entry-in-org-file)
9465 (require 'diary-lib)
9466 (let* ((char (progn
9467 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
9468 (read-char-exclusive)))
9469 (cmd (cdr (assoc char
9470 '((?d . diary-insert-entry)
9471 (?w . diary-insert-weekly-entry)
9472 (?m . diary-insert-monthly-entry)
9473 (?y . diary-insert-yearly-entry)
9474 (?a . diary-insert-anniversary-entry)
9475 (?b . diary-insert-block-entry)
9476 (?c . diary-insert-cyclic-entry)))))
9477 (oldf (symbol-function 'calendar-cursor-to-date))
9478 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9479 (point (point))
9480 (mark (or (mark t) (point))))
9481 (unless cmd
9482 (user-error "No command associated with <%c>" char))
9483 (unless (and (get-text-property point 'day)
9484 (or (not (equal ?b char))
9485 (get-text-property mark 'day)))
9486 (user-error "Don't know which date to use for diary entry"))
9487 ;; We implement this by hacking the `calendar-cursor-to-date' function
9488 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9489 (let ((calendar-mark-ring
9490 (list (calendar-gregorian-from-absolute
9491 (or (get-text-property mark 'day)
9492 (get-text-property point 'day))))))
9493 (unwind-protect
9494 (progn
9495 (fset 'calendar-cursor-to-date
9496 (lambda (&optional error dummy)
9497 (calendar-gregorian-from-absolute
9498 (get-text-property point 'day))))
9499 (call-interactively cmd))
9500 (fset 'calendar-cursor-to-date oldf))))))
9502 (defun org-agenda-execute-calendar-command (cmd)
9503 "Execute a calendar command from the agenda with date from cursor."
9504 (org-agenda-check-type t 'agenda 'timeline)
9505 (require 'diary-lib)
9506 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9507 (user-error "Don't know which date to use for the calendar command"))
9508 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9509 (point (point))
9510 (date (calendar-gregorian-from-absolute
9511 (get-text-property point 'day)))
9512 ;; the following 2 vars are needed in the calendar
9513 (displayed-month (car date))
9514 (displayed-year (nth 2 date)))
9515 (unwind-protect
9516 (progn
9517 (fset 'calendar-cursor-to-date
9518 (lambda (&optional error dummy)
9519 (calendar-gregorian-from-absolute
9520 (get-text-property point 'day))))
9521 (call-interactively cmd))
9522 (fset 'calendar-cursor-to-date oldf))))
9524 (defun org-agenda-phases-of-moon ()
9525 "Display the phases of the moon for the 3 months around the cursor date."
9526 (interactive)
9527 (org-agenda-execute-calendar-command 'calendar-lunar-phases))
9529 (defun org-agenda-holidays ()
9530 "Display the holidays for the 3 months around the cursor date."
9531 (interactive)
9532 (org-agenda-execute-calendar-command 'calendar-list-holidays))
9534 (defvar calendar-longitude) ; defined in calendar.el
9535 (defvar calendar-latitude) ; defined in calendar.el
9536 (defvar calendar-location-name) ; defined in calendar.el
9538 (defun org-agenda-sunrise-sunset (arg)
9539 "Display sunrise and sunset for the cursor date.
9540 Latitude and longitude can be specified with the variables
9541 `calendar-latitude' and `calendar-longitude'. When called with prefix
9542 argument, latitude and longitude will be prompted for."
9543 (interactive "P")
9544 (require 'solar)
9545 (let ((calendar-longitude (if arg nil calendar-longitude))
9546 (calendar-latitude (if arg nil calendar-latitude))
9547 (calendar-location-name
9548 (if arg "the given coordinates" calendar-location-name)))
9549 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9551 (defun org-agenda-goto-calendar ()
9552 "Open the Emacs calendar with the date at the cursor."
9553 (interactive)
9554 (org-agenda-check-type t 'agenda 'timeline)
9555 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9556 (user-error "Don't know which date to open in calendar")))
9557 (date (calendar-gregorian-from-absolute day))
9558 (calendar-move-hook nil)
9559 (calendar-view-holidays-initially-flag nil)
9560 (calendar-view-diary-initially-flag nil))
9561 (calendar)
9562 (calendar-goto-date date)))
9564 ;;;###autoload
9565 (defun org-calendar-goto-agenda ()
9566 "Compute the Org agenda for the calendar date displayed at the cursor.
9567 This is a command that has to be installed in `calendar-mode-map'."
9568 (interactive)
9569 ;; Temporarily disable sticky agenda since user clearly wants to
9570 ;; refresh view anyway.
9571 (let ((org-agenda-buffer-tmp-name "*Org Agenda(a)*")
9572 (org-agenda-sticky nil))
9573 (org-agenda-list nil (calendar-absolute-from-gregorian
9574 (calendar-cursor-to-date))
9575 nil)))
9577 (defun org-agenda-convert-date ()
9578 (interactive)
9579 (org-agenda-check-type t 'agenda 'timeline)
9580 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9581 date s)
9582 (unless day
9583 (user-error "Don't know which date to convert"))
9584 (setq date (calendar-gregorian-from-absolute day))
9585 (setq s (concat
9586 "Gregorian: " (calendar-date-string date) "\n"
9587 "ISO: " (calendar-iso-date-string date) "\n"
9588 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9589 "Julian: " (calendar-julian-date-string date) "\n"
9590 "Astron. JD: " (calendar-astro-date-string date)
9591 " (Julian date number at noon UTC)\n"
9592 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9593 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9594 "French: " (calendar-french-date-string date) "\n"
9595 "Bahá’í: " (calendar-bahai-date-string date) " (until sunset)\n"
9596 "Mayan: " (calendar-mayan-date-string date) "\n"
9597 "Coptic: " (calendar-coptic-date-string date) "\n"
9598 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9599 "Persian: " (calendar-persian-date-string date) "\n"
9600 "Chinese: " (calendar-chinese-date-string date) "\n"))
9601 (with-output-to-temp-buffer "*Dates*"
9602 (princ s))
9603 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9605 ;;; Bulk commands
9607 (defun org-agenda-bulk-marked-p ()
9608 "Non-nil when current entry is marked for bulk action."
9609 (eq (get-char-property (point-at-bol) 'type)
9610 'org-marked-entry-overlay))
9612 (defun org-agenda-bulk-mark (&optional arg)
9613 "Mark the entry at point for future bulk action."
9614 (interactive "p")
9615 (dotimes (i (or arg 1))
9616 (unless (org-get-at-bol 'org-agenda-diary-link)
9617 (let* ((m (org-get-at-bol 'org-hd-marker))
9619 (unless (org-agenda-bulk-marked-p)
9620 (unless m (user-error "Nothing to mark at point"))
9621 (push m org-agenda-bulk-marked-entries)
9622 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9623 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9624 (org-get-todo-face "TODO")
9625 'evaporate)
9626 (overlay-put ov 'type 'org-marked-entry-overlay))
9627 (end-of-line 1)
9628 (or (ignore-errors
9629 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9630 (beginning-of-line 2))
9631 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9632 (beginning-of-line 2))
9633 (message "%d entries marked for bulk action"
9634 (length org-agenda-bulk-marked-entries))))))
9636 (defun org-agenda-bulk-mark-all ()
9637 "Mark all entries for future agenda bulk action."
9638 (interactive)
9639 (org-agenda-bulk-mark-regexp "."))
9641 (defun org-agenda-bulk-mark-regexp (regexp)
9642 "Mark entries matching REGEXP for future agenda bulk action."
9643 (interactive "sMark entries matching regexp: ")
9644 (let ((entries-marked 0) txt-at-point)
9645 (save-excursion
9646 (goto-char (point-min))
9647 (goto-char (next-single-property-change (point) 'org-hd-marker))
9648 (while (and (re-search-forward regexp nil t)
9649 (setq txt-at-point (get-text-property (point) 'txt)))
9650 (if (get-char-property (point) 'invisible)
9651 (beginning-of-line 2)
9652 (when (string-match regexp txt-at-point)
9653 (setq entries-marked (1+ entries-marked))
9654 (call-interactively 'org-agenda-bulk-mark)))))
9656 (if (not entries-marked)
9657 (message "No entry matching this regexp."))))
9659 (defun org-agenda-bulk-unmark (&optional arg)
9660 "Unmark the entry at point for future bulk action."
9661 (interactive "P")
9662 (if arg
9663 (org-agenda-bulk-unmark-all)
9664 (cond ((org-agenda-bulk-marked-p)
9665 (org-agenda-bulk-remove-overlays
9666 (point-at-bol) (+ 2 (point-at-bol)))
9667 (setq org-agenda-bulk-marked-entries
9668 (delete (org-get-at-bol 'org-hd-marker)
9669 org-agenda-bulk-marked-entries))
9670 (end-of-line 1)
9671 (or (ignore-errors
9672 (goto-char (next-single-property-change (point) 'txt)))
9673 (beginning-of-line 2))
9674 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9675 (beginning-of-line 2))
9676 (message "%d entries left marked for bulk action"
9677 (length org-agenda-bulk-marked-entries)))
9678 (t (message "No entry to unmark here")))))
9680 (defun org-agenda-bulk-toggle-all ()
9681 "Toggle all marks for bulk action."
9682 (interactive)
9683 (save-excursion
9684 (goto-char (point-min))
9685 (while (ignore-errors
9686 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9687 (org-agenda-bulk-toggle))))
9689 (defun org-agenda-bulk-toggle ()
9690 "Toggle the mark at point for bulk action."
9691 (interactive)
9692 (if (org-agenda-bulk-marked-p)
9693 (org-agenda-bulk-unmark)
9694 (org-agenda-bulk-mark)))
9696 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9697 "Remove the mark overlays between BEG and END in the agenda buffer.
9698 BEG and END default to the buffer limits.
9700 This only removes the overlays, it does not remove the markers
9701 from the list in `org-agenda-bulk-marked-entries'."
9702 (interactive)
9703 (mapc (lambda (ov)
9704 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9705 (delete-overlay ov)))
9706 (overlays-in (or beg (point-min)) (or end (point-max)))))
9708 (defun org-agenda-bulk-unmark-all ()
9709 "Remove all marks in the agenda buffer.
9710 This will remove the markers and the overlays."
9711 (interactive)
9712 (if (null org-agenda-bulk-marked-entries)
9713 (message "No entry to unmark")
9714 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
9715 (setq org-agenda-bulk-marked-entries nil)
9716 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9718 (defcustom org-agenda-persistent-marks nil
9719 "Non-nil means marked items will stay marked after a bulk action.
9720 You can toggle this interactively by typing `p' when prompted for a
9721 bulk action."
9722 :group 'org-agenda
9723 :version "24.1"
9724 :type 'boolean)
9726 (defun org-agenda-bulk-action (&optional arg)
9727 "Execute an remote-editing action on all marked entries.
9728 The prefix arg is passed through to the command if possible."
9729 (interactive "P")
9730 ;; Make sure we have markers, and only valid ones
9731 (unless org-agenda-bulk-marked-entries (user-error "No entries are marked"))
9732 (mapc
9733 (lambda (m)
9734 (unless (and (markerp m)
9735 (marker-buffer m)
9736 (buffer-live-p (marker-buffer m))
9737 (marker-position m))
9738 (user-error "Marker %s for bulk command is invalid" m)))
9739 org-agenda-bulk-marked-entries)
9741 ;; Prompt for the bulk command
9742 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
9743 (message (concat msg "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9744 "[S]catter [f]unction "
9745 (when org-agenda-bulk-custom-functions
9746 (concat " Custom: ["
9747 (mapconcat (lambda(f) (char-to-string (car f)))
9748 org-agenda-bulk-custom-functions "")
9749 "]"))))
9750 (catch 'exit
9751 (let* ((action (read-char-exclusive))
9752 (org-log-refile (if org-log-refile 'time nil))
9753 (entries (reverse org-agenda-bulk-marked-entries))
9754 (org-overriding-default-time
9755 (if (get-text-property (point) 'org-agenda-date-header)
9756 (org-get-cursor-date)))
9757 redo-at-end
9758 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
9759 (cond
9760 ((equal action ?p)
9761 (let ((org-agenda-persistent-marks
9762 (not org-agenda-persistent-marks)))
9763 (org-agenda-bulk-action)
9764 (throw 'exit nil)))
9766 ((equal action ?$)
9767 (setq cmd '(org-agenda-archive)))
9769 ((equal action ?A)
9770 (setq cmd '(org-agenda-archive-to-archive-sibling)))
9772 ((member action '(?r ?w))
9773 (setq rfloc (org-refile-get-location
9774 "Refile to"
9775 (marker-buffer (car entries))
9776 org-refile-allow-creating-parent-nodes))
9777 (if (nth 3 rfloc)
9778 (setcar (nthcdr 3 rfloc)
9779 (move-marker (make-marker) (nth 3 rfloc)
9780 (or (get-file-buffer (nth 1 rfloc))
9781 (find-buffer-visiting (nth 1 rfloc))
9782 (error "This should not happen")))))
9784 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
9785 redo-at-end t))
9787 ((equal action ?t)
9788 (setq state (completing-read
9789 "Todo state: "
9790 (with-current-buffer (marker-buffer (car entries))
9791 (mapcar #'list org-todo-keywords-1))))
9792 (setq cmd `(let ((org-inhibit-blocking t)
9793 (org-inhibit-logging 'note))
9794 (org-agenda-todo ,state))))
9796 ((memq action '(?- ?+))
9797 (setq tag (completing-read
9798 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9799 (with-current-buffer (marker-buffer (car entries))
9800 (delq nil
9801 (mapcar (lambda (x) (and (stringp (car x)) x))
9802 org-current-tag-alist)))))
9803 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
9805 ((memq action '(?s ?d))
9806 (let* ((time
9807 (unless arg
9808 (org-read-date
9809 nil nil nil
9810 (if (eq action ?s) "(Re)Schedule to" "(Re)Set Deadline to")
9811 org-overriding-default-time)))
9812 (c1 (if (eq action ?s) 'org-agenda-schedule
9813 'org-agenda-deadline)))
9814 ;; Make sure to not prompt for a note when bulk
9815 ;; rescheduling as Org cannot cope with simultaneous Org.
9816 ;; Besides, it could be annoying depending on the number
9817 ;; of items re-scheduled.
9818 (setq cmd `(eval '(let ((org-log-reschedule
9819 (and org-log-reschedule 'time)))
9820 (,c1 arg ,time))))))
9822 ((equal action ?S)
9823 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
9824 (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
9825 (let ((days (read-number
9826 (format "Scatter tasks across how many %sdays: "
9827 (if arg "week" "")) 7)))
9828 (setq cmd
9829 `(let ((distance (1+ (random ,days))))
9830 (if arg
9831 (let ((dist distance)
9832 (day-of-week
9833 (calendar-day-of-week
9834 (calendar-gregorian-from-absolute (org-today)))))
9835 (dotimes (i (1+ dist))
9836 (while (member day-of-week org-agenda-weekend-days)
9837 (cl-incf distance)
9838 (cl-incf day-of-week)
9839 (when (= day-of-week 7)
9840 (setq day-of-week 0)))
9841 (cl-incf day-of-week)
9842 (when (= day-of-week 7)
9843 (setq day-of-week 0)))))
9844 ;; silently fail when try to replan a sexp entry
9845 (condition-case nil
9846 (let* ((date (calendar-gregorian-from-absolute
9847 (+ (org-today) distance)))
9848 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9849 (nth 2 date))))
9850 (org-agenda-schedule nil time))
9851 (error nil)))))))
9853 ((assoc action org-agenda-bulk-custom-functions)
9854 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
9855 redo-at-end t))
9857 ((equal action ?f)
9858 (setq cmd (list (intern
9859 (completing-read "Function: "
9860 obarray 'fboundp t nil nil)))))
9862 (t (user-error "Invalid bulk action")))
9864 ;; Sort the markers, to make sure that parents are handled before children
9865 (setq entries (sort entries
9866 (lambda (a b)
9867 (cond
9868 ((equal (marker-buffer a) (marker-buffer b))
9869 (< (marker-position a) (marker-position b)))
9871 (string< (buffer-name (marker-buffer a))
9872 (buffer-name (marker-buffer b))))))))
9874 ;; Now loop over all markers and apply cmd
9875 (while (setq e (pop entries))
9876 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
9877 (if (not pos)
9878 (progn (message "Skipping removed entry at %s" e)
9879 (setq cntskip (1+ cntskip)))
9880 (goto-char pos)
9881 (let (org-loop-over-headlines-in-active-region)
9882 (eval cmd))
9883 ;; `post-command-hook' is not run yet. We make sure any
9884 ;; pending log note is processed.
9885 (when (or (memq 'org-add-log-note (default-value 'post-command-hook))
9886 (memq 'org-add-log-note post-command-hook))
9887 (org-add-log-note))
9888 (setq cnt (1+ cnt))))
9889 (when redo-at-end (org-agenda-redo))
9890 (unless org-agenda-persistent-marks
9891 (org-agenda-bulk-unmark-all))
9892 (message "Acted on %d entries%s%s"
9894 (if (= cntskip 0)
9896 (format ", skipped %d (disappeared before their turn)"
9897 cntskip))
9898 (if (not org-agenda-persistent-marks)
9899 "" " (kept marked)"))))))
9901 (defun org-agenda-capture (&optional with-time)
9902 "Call `org-capture' with the date at point.
9903 With a `C-1' prefix, use the HH:MM value at point (if any) or the
9904 current HH:MM time."
9905 (interactive "P")
9906 (if (not (eq major-mode 'org-agenda-mode))
9907 (user-error "You cannot do this outside of agenda buffers")
9908 (let ((org-overriding-default-time
9909 (org-get-cursor-date (equal with-time 1))))
9910 (call-interactively 'org-capture))))
9912 ;;; Dragging agenda lines forward/backward
9914 (defun org-agenda-reapply-filters ()
9915 "Re-apply all agenda filters."
9916 (mapcar
9917 (lambda(f) (when (car f) (org-agenda-filter-apply (car f) (cadr f) t)))
9918 `((,org-agenda-tag-filter tag)
9919 (,org-agenda-category-filter category)
9920 (,org-agenda-regexp-filter regexp)
9921 (,org-agenda-effort-filter effort)
9922 (,(get 'org-agenda-tag-filter :preset-filter) tag)
9923 (,(get 'org-agenda-category-filter :preset-filter) category)
9924 (,(get 'org-agenda-effort-filter :preset-filter) effort)
9925 (,(get 'org-agenda-regexp-filter :preset-filter) regexp))))
9927 (defun org-agenda-drag-line-forward (arg &optional backward)
9928 "Drag an agenda line forward by ARG lines.
9929 When the optional argument `backward' is non-nil, move backward."
9930 (interactive "p")
9931 (let ((inhibit-read-only t) lst line)
9932 (if (or (not (get-text-property (point) 'txt))
9933 (save-excursion
9934 (dotimes (n arg)
9935 (move-beginning-of-line (if backward 0 2))
9936 (push (not (get-text-property (point) 'txt)) lst))
9937 (delq nil lst)))
9938 (message "Cannot move line forward")
9939 (let ((end (save-excursion (move-beginning-of-line 2) (point))))
9940 (move-beginning-of-line 1)
9941 (setq line (buffer-substring (point) end))
9942 (delete-region (point) end)
9943 (move-beginning-of-line (funcall (if backward '1- '1+) arg))
9944 (insert line)
9945 (org-agenda-reapply-filters)
9946 (org-agenda-mark-clocking-task)
9947 (move-beginning-of-line 0)))))
9949 (defun org-agenda-drag-line-backward (arg)
9950 "Drag an agenda line backward by ARG lines."
9951 (interactive "p")
9952 (org-agenda-drag-line-forward arg t))
9954 ;;; Flagging notes
9956 (defun org-agenda-show-the-flagging-note ()
9957 "Display the flagging note in the other window.
9958 When called a second time in direct sequence, offer to remove the FLAGGING
9959 tag and (if present) the flagging note."
9960 (interactive)
9961 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
9962 (win (selected-window))
9963 note heading newhead)
9964 (unless hdmarker
9965 (user-error "No linked entry at point"))
9966 (if (and (eq this-command last-command)
9967 (y-or-n-p "Unflag and remove any flagging note? "))
9968 (progn
9969 (org-agenda-remove-flag hdmarker)
9970 (let ((win (get-buffer-window "*Flagging Note*")))
9971 (and win (delete-window win)))
9972 (message "Entry unflagged"))
9973 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
9974 (unless note
9975 (user-error "No flagging note"))
9976 (org-kill-new note)
9977 (org-switch-to-buffer-other-window "*Flagging Note*")
9978 (erase-buffer)
9979 (insert note)
9980 (goto-char (point-min))
9981 (while (re-search-forward "\\\\n" nil t)
9982 (replace-match "\n" t t))
9983 (goto-char (point-min))
9984 (select-window win)
9985 (message "%s" (substitute-command-keys "Flagging note pushed to \
9986 kill ring. Press `\\[org-agenda-show-the-flagging-note]' again to remove \
9987 tag and note")))))
9989 (defun org-agenda-remove-flag (marker)
9990 "Remove the FLAGGED tag and any flagging note in the entry."
9991 (let (newhead)
9992 (org-with-point-at marker
9993 (org-toggle-tag "FLAGGED" 'off)
9994 (org-entry-delete nil "THEFLAGGINGNOTE")
9995 (setq newhead (org-get-heading)))
9996 (org-agenda-change-all-lines newhead marker)
9997 (message "Entry unflagged")))
9999 (defun org-agenda-get-any-marker (&optional pos)
10000 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
10001 (get-text-property (or pos (point-at-bol)) 'org-marker)))
10003 ;;; Appointment reminders
10005 (defvar appt-time-msg-list) ; defined in appt.el
10007 ;;;###autoload
10008 (defun org-agenda-to-appt (&optional refresh filter &rest args)
10009 "Activate appointments found in `org-agenda-files'.
10011 With a `\\[universal-argument]' prefix, refresh the list of \
10012 appointments.
10014 If FILTER is t, interactively prompt the user for a regular
10015 expression, and filter out entries that don't match it.
10017 If FILTER is a string, use this string as a regular expression
10018 for filtering entries out.
10020 If FILTER is a function, filter out entries against which
10021 calling the function returns nil. This function takes one
10022 argument: an entry from `org-agenda-get-day-entries'.
10024 FILTER can also be an alist with the car of each cell being
10025 either `headline' or `category'. For example:
10027 \\='((headline \"IMPORTANT\")
10028 (category \"Work\"))
10030 will only add headlines containing IMPORTANT or headlines
10031 belonging to the \"Work\" category.
10033 ARGS are symbols indicating what kind of entries to consider.
10034 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
10035 \(i.e., deadlines and scheduled items with a hh:mm specification)
10036 and :timestamp entries. See the docstring of `org-diary' for
10037 details and examples.
10039 If an entry has a APPT_WARNTIME property, its value will be used
10040 to override `appt-message-warning-time'."
10041 (interactive "P")
10042 (if refresh (setq appt-time-msg-list nil))
10043 (if (eq filter t)
10044 (setq filter (read-from-minibuffer "Regexp filter: ")))
10045 (let* ((cnt 0) ; count added events
10046 (scope (or args '(:deadline* :scheduled* :timestamp)))
10047 (org-agenda-new-buffers nil)
10048 (org-deadline-warning-days 0)
10049 ;; Do not use `org-today' here because appt only takes
10050 ;; time and without date as argument, so it may pass wrong
10051 ;; information otherwise
10052 (today (org-date-to-gregorian
10053 (time-to-days (current-time))))
10054 (org-agenda-restrict nil)
10055 (files (org-agenda-files 'unrestricted)) entries file
10056 (org-agenda-buffer nil))
10057 ;; Get all entries which may contain an appt
10058 (org-agenda-prepare-buffers files)
10059 (while (setq file (pop files))
10060 (setq entries
10061 (delq nil
10062 (append entries
10063 (apply 'org-agenda-get-day-entries
10064 file today scope)))))
10065 ;; Map thru entries and find if we should filter them out
10066 (mapc
10067 (lambda (x)
10068 (let* ((evt (org-trim
10069 (replace-regexp-in-string
10070 org-bracket-link-regexp "\\3"
10071 (or (get-text-property 1 'txt x) ""))))
10072 (cat (get-text-property (1- (length x)) 'org-category x))
10073 (tod (get-text-property 1 'time-of-day x))
10074 (ok (or (null filter)
10075 (and (stringp filter) (string-match filter evt))
10076 (and (functionp filter) (funcall filter x))
10077 (and (listp filter)
10078 (let ((cat-filter (cadr (assq 'category filter)))
10079 (evt-filter (cadr (assq 'headline filter))))
10080 (or (and (stringp cat-filter)
10081 (string-match cat-filter cat))
10082 (and (stringp evt-filter)
10083 (string-match evt-filter evt)))))))
10084 (wrn (get-text-property 1 'warntime x)))
10085 ;; FIXME: Shall we remove text-properties for the appt text?
10086 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
10087 (when (and ok tod (not (string-match "\\`DONE\\|CANCELLED" evt)))
10088 (setq tod (concat "00" (number-to-string tod)))
10089 (setq tod (when (string-match
10090 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
10091 (concat (match-string 1 tod) ":"
10092 (match-string 2 tod))))
10093 (when (if (version< emacs-version "23.3")
10094 (appt-add tod evt)
10095 (appt-add tod evt wrn))
10096 (setq cnt (1+ cnt))))))
10097 entries)
10098 (org-release-buffers org-agenda-new-buffers)
10099 (if (eq cnt 0)
10100 (message "No event to add")
10101 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
10103 (defun org-agenda-today-p (date)
10104 "Non nil when DATE means today.
10105 DATE is either a list of the form (month day year) or a number of
10106 days as returned by `calendar-absolute-from-gregorian' or
10107 `org-today'. This function considers `org-extend-today-until'
10108 when defining today."
10109 (eq (org-today)
10110 (if (consp date) (calendar-absolute-from-gregorian date) date)))
10112 (defun org-agenda-todo-yesterday (&optional arg)
10113 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
10114 (interactive "P")
10115 (let* ((org-use-effective-time t)
10116 (hour (nth 2 (decode-time (org-current-time))))
10117 (org-extend-today-until (1+ hour)))
10118 (org-agenda-todo arg)))
10120 (provide 'org-agenda)
10122 ;;; org-agenda.el ends here