Merge branch 'maint'
[org-mode/org-tableheadings.git] / lisp / org-agenda.el
blobbf406f19386f2fbad328aac106eb823ab2fc4355
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004-2017 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file contains the code for creating and using the Agenda for Org.
29 ;; The functions `org-batch-agenda', `org-batch-agenda-csv', and
30 ;; `org-batch-store-agenda-views' are implemented as macros to provide
31 ;; a convenient way for extracting agenda information from the command
32 ;; line. The Lisp does not evaluate parameters of a macro call; thus
33 ;; it is not necessary to quote the parameters passed to one of those
34 ;; functions. E.g. you can write:
36 ;; emacs -batch -l ~/.emacs -eval '(org-batch-agenda "a" org-agenda-span 7)'
38 ;; To export an agenda spanning 7 days. If `org-batch-agenda' would
39 ;; have been implemented as a regular function you'd have to quote the
40 ;; symbol org-agenda-span. Moreover: To use a symbol as parameter
41 ;; value you would have to double quote the symbol.
43 ;; This is a hack, but it works even when running Org byte-compiled.
46 ;;; Code:
48 (require 'cl-lib)
49 (require 'org)
50 (require 'org-macs)
52 (declare-function diary-add-to-list "diary-lib"
53 (date string specifier &optional marker globcolor literal))
54 (declare-function calendar-iso-to-absolute "cal-iso" (date))
55 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
56 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
57 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
58 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
59 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
60 (declare-function calendar-french-date-string "cal-french" (&optional date))
61 (declare-function calendar-goto-date "cal-move" (date))
62 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
63 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
64 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
65 (declare-function calendar-iso-from-absolute "cal-iso" (date))
66 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
67 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
68 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
69 (declare-function calendar-check-holidays "holidays" (date))
71 (declare-function org-columns-remove-overlays "org-colview" ())
72 (declare-function org-datetree-find-date-create "org-datetree"
73 (date &optional keep-restriction))
74 (declare-function org-columns-quit "org-colview" ())
75 (declare-function diary-date-display-form "diary-lib" (&optional type))
76 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
77 (declare-function org-habit-insert-consistency-graphs
78 "org-habit" (&optional line))
79 (declare-function org-is-habit-p "org-habit" (&optional pom))
80 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
81 (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
82 (declare-function org-agenda-columns "org-colview" ())
83 (declare-function org-add-archive-files "org-archive" (files))
84 (declare-function org-capture "org-capture" (&optional goto keys))
86 (defvar calendar-mode-map)
87 (defvar org-clock-current-task)
88 (defvar org-current-tag-alist)
89 (defvar org-mobile-force-id-on-agenda-items)
90 (defvar org-habit-show-habits)
91 (defvar org-habit-show-habits-only-for-today)
92 (defvar org-habit-show-all-today)
94 ;; Defined somewhere in this file, but used before definition.
95 (defvar org-agenda-buffer-name "*Org Agenda*")
96 (defvar org-agenda-overriding-header nil)
97 (defvar org-agenda-title-append nil)
98 (with-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
99 (with-no-warnings (defvar date)) ;; unprefixed, from calendar.el
100 (defvar original-date) ; dynamically scoped, calendar.el does scope this
102 (defvar org-agenda-undo-list nil
103 "List of undoable operations in the agenda since last refresh.")
104 (defvar org-agenda-pending-undo-list nil
105 "In a series of undo commands, this is the list of remaining undo items.")
107 (defcustom org-agenda-confirm-kill 1
108 "When set, remote killing from the agenda buffer needs confirmation.
109 When t, a confirmation is always needed. When a number N, confirmation is
110 only needed when the text to be killed contains more than N non-white lines."
111 :group 'org-agenda
112 :type '(choice
113 (const :tag "Never" nil)
114 (const :tag "Always" t)
115 (integer :tag "When more than N lines")))
117 (defcustom org-agenda-compact-blocks nil
118 "Non-nil means make the block agenda more compact.
119 This is done globally by leaving out lines like the agenda span
120 name and week number or the separator lines."
121 :group 'org-agenda
122 :type 'boolean)
124 (defcustom org-agenda-block-separator ?=
125 "The separator between blocks in the agenda.
126 If this is a string, it will be used as the separator, with a newline added.
127 If it is a character, it will be repeated to fill the window width.
128 If nil the separator is disabled. In `org-agenda-custom-commands' this
129 addresses the separator between the current and the previous block."
130 :group 'org-agenda
131 :type '(choice
132 (const :tag "Disabled" nil)
133 (character)
134 (string)))
136 (defgroup org-agenda-export nil
137 "Options concerning exporting agenda views in Org mode."
138 :tag "Org Agenda Export"
139 :group 'org-agenda)
141 (defcustom org-agenda-with-colors t
142 "Non-nil means use colors in agenda views."
143 :group 'org-agenda-export
144 :type 'boolean)
146 (defcustom org-agenda-exporter-settings nil
147 "Alist of variable/value pairs that should be active during agenda export.
148 This is a good place to set options for ps-print and for htmlize.
149 Note that the way this is implemented, the values will be evaluated
150 before assigned to the variables. So make sure to quote values you do
151 *not* want evaluated, for example
153 (setq org-agenda-exporter-settings
154 \\='((ps-print-color-p \\='black-white)))"
155 :group 'org-agenda-export
156 :type '(repeat
157 (list
158 (variable)
159 (sexp :tag "Value"))))
161 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
162 "Hook run in a temporary buffer before writing the agenda to an export file.
163 A useful function for this hook is `org-agenda-add-entry-text'."
164 :group 'org-agenda-export
165 :type 'hook
166 :options '(org-agenda-add-entry-text))
168 (defcustom org-agenda-add-entry-text-maxlines 0
169 "Maximum number of entry text lines to be added to agenda.
170 This is only relevant when `org-agenda-add-entry-text' is part of
171 `org-agenda-before-write-hook', which is the default.
172 When this is 0, nothing will happen. When it is greater than 0, it
173 specifies the maximum number of lines that will be added for each entry
174 that is listed in the agenda view.
176 Note that this variable is not used during display, only when exporting
177 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
178 and `org-agenda-entry-text-maxlines'."
179 :group 'org-agenda
180 :type 'integer)
182 (defcustom org-agenda-add-entry-text-descriptive-links t
183 "Non-nil means export org-links as descriptive links in agenda added text.
184 This variable applies to the text added to the agenda when
185 `org-agenda-add-entry-text-maxlines' is larger than 0.
186 When this variable nil, the URL will (also) be shown."
187 :group 'org-agenda
188 :type 'boolean)
190 (defcustom org-agenda-export-html-style nil
191 "The style specification for exported HTML Agenda files.
192 If this variable contains a string, it will replace the default <style>
193 section as produced by `htmlize'.
194 Since there are different ways of setting style information, this variable
195 needs to contain the full HTML structure to provide a style, including the
196 surrounding HTML tags. The style specifications should include definitions
197 the fonts used by the agenda, here is an example:
199 <style type=\"text/css\">
200 p { font-weight: normal; color: gray; }
201 .org-agenda-structure {
202 font-size: 110%;
203 color: #003399;
204 font-weight: 600;
206 .org-todo {
207 color: #cc6666;
208 font-weight: bold;
210 .org-agenda-done {
211 color: #339933;
213 .org-done {
214 color: #339933;
216 .title { text-align: center; }
217 .todo, .deadline { color: red; }
218 .done { color: green; }
219 </style>
221 or, if you want to keep the style in a file,
223 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
225 As the value of this option simply gets inserted into the HTML <head> header,
226 you can \"misuse\" it to also add other text to the header."
227 :group 'org-agenda-export
228 :group 'org-export-html
229 :type '(choice
230 (const nil)
231 (string)))
233 (defcustom org-agenda-persistent-filter nil
234 "When set, keep filters from one agenda view to the next."
235 :group 'org-agenda
236 :type 'boolean)
238 (defgroup org-agenda-custom-commands nil
239 "Options concerning agenda views in Org mode."
240 :tag "Org Agenda Custom Commands"
241 :group 'org-agenda)
243 (defconst org-sorting-choice
244 '(choice
245 (const time-up) (const time-down)
246 (const timestamp-up) (const timestamp-down)
247 (const scheduled-up) (const scheduled-down)
248 (const deadline-up) (const deadline-down)
249 (const ts-up) (const ts-down)
250 (const tsia-up) (const tsia-down)
251 (const category-keep) (const category-up) (const category-down)
252 (const tag-down) (const tag-up)
253 (const priority-up) (const priority-down)
254 (const todo-state-up) (const todo-state-down)
255 (const effort-up) (const effort-down)
256 (const habit-up) (const habit-down)
257 (const alpha-up) (const alpha-down)
258 (const user-defined-up) (const user-defined-down))
259 "Sorting choices.")
261 ;; Keep custom values for `org-agenda-filter-preset' compatible with
262 ;; the new variable `org-agenda-tag-filter-preset'.
263 (defvaralias 'org-agenda-filter-preset 'org-agenda-tag-filter-preset)
264 (defvaralias 'org-agenda-filter 'org-agenda-tag-filter)
266 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
267 "List of types searched for when creating the daily/weekly agenda.
268 This variable is a list of symbols that controls the types of
269 items that appear in the daily/weekly agenda. Allowed symbols in this
270 list are are
272 :timestamp List items containing a date stamp or date range matching
273 the selected date. This includes sexp entries in angular
274 brackets.
276 :sexp List entries resulting from plain diary-like sexps.
278 :deadline List deadline due on that date. When the date is today,
279 also list any deadlines past due, or due within
280 `org-deadline-warning-days'.
282 :deadline* Same as above, but only include the deadline if it has an
283 hour specification as [h]h:mm.
285 :scheduled List all items which are scheduled for the given date.
286 The diary for *today* also contains items which were
287 scheduled earlier and are not yet marked DONE.
289 :scheduled* Same as above, but only include the scheduled item if it
290 has an hour specification as [h]h:mm.
292 By default, all four non-starred types are turned on.
294 When :scheduled* or :deadline* are included, :schedule or :deadline
295 will be ignored.
297 Never set this variable globally using `setq', because then it
298 will apply to all future agenda commands. Instead, bind it with
299 `let' to scope it dynamically into the agenda-constructing
300 command. A good way to set it is through options in
301 `org-agenda-custom-commands'. For a more flexible (though
302 somewhat less efficient) way of determining what is included in
303 the daily/weekly agenda, see `org-agenda-skip-function'.")
305 (defconst org-agenda-custom-commands-local-options
306 `(repeat :tag "Local settings for this command. Remember to quote values"
307 (choice :tag "Setting"
308 (list :tag "Heading for this block"
309 (const org-agenda-overriding-header)
310 (string :tag "Headline"))
311 (list :tag "Files to be searched"
312 (const org-agenda-files)
313 (list
314 (const :format "" quote)
315 (repeat (file))))
316 (list :tag "Sorting strategy"
317 (const org-agenda-sorting-strategy)
318 (list
319 (const :format "" quote)
320 (repeat
321 ,org-sorting-choice)))
322 (list :tag "Prefix format"
323 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
324 (string))
325 (list :tag "Number of days in agenda"
326 (const org-agenda-span)
327 (list
328 (const :format "" quote)
329 (choice (const :tag "Day" day)
330 (const :tag "Week" week)
331 (const :tag "Fortnight" fortnight)
332 (const :tag "Month" month)
333 (const :tag "Year" year)
334 (integer :tag "Custom"))))
335 (list :tag "Fixed starting date"
336 (const org-agenda-start-day)
337 (string :value "2007-11-01"))
338 (list :tag "Start on day of week"
339 (const org-agenda-start-on-weekday)
340 (choice :value 1
341 (const :tag "Today" nil)
342 (integer :tag "Weekday No.")))
343 (list :tag "Include data from diary"
344 (const org-agenda-include-diary)
345 (boolean))
346 (list :tag "Deadline Warning days"
347 (const org-deadline-warning-days)
348 (integer :value 1))
349 (list :tag "Category filter preset"
350 (const org-agenda-category-filter-preset)
351 (list
352 (const :format "" quote)
353 (repeat
354 (string :tag "+category or -category"))))
355 (list :tag "Tags filter preset"
356 (const org-agenda-tag-filter-preset)
357 (list
358 (const :format "" quote)
359 (repeat
360 (string :tag "+tag or -tag"))))
361 (list :tag "Effort filter preset"
362 (const org-agenda-effort-filter-preset)
363 (list
364 (const :format "" quote)
365 (repeat
366 (string :tag "+=10 or -=10 or +<10 or ->10"))))
367 (list :tag "Regexp filter preset"
368 (const org-agenda-regexp-filter-preset)
369 (list
370 (const :format "" quote)
371 (repeat
372 (string :tag "+regexp or -regexp"))))
373 (list :tag "Set daily/weekly entry types"
374 (const org-agenda-entry-types)
375 (list
376 (const :format "" quote)
377 (set :greedy t :value ,org-agenda-entry-types
378 (const :deadline)
379 (const :scheduled)
380 (const :deadline*)
381 (const :scheduled*)
382 (const :timestamp)
383 (const :sexp))))
384 (list :tag "Standard skipping condition"
385 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
386 (const org-agenda-skip-function)
387 (list
388 (const :format "" quote)
389 (list
390 (choice
391 :tag "Skipping range"
392 (const :tag "Skip entry" org-agenda-skip-entry-if)
393 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
394 (repeat :inline t :tag "Conditions for skipping"
395 (choice
396 :tag "Condition type"
397 (list :tag "Regexp matches" :inline t
398 (const :format "" regexp)
399 (regexp))
400 (list :tag "Regexp does not match" :inline t
401 (const :format "" notregexp)
402 (regexp))
403 (list :tag "TODO state is" :inline t
404 (const todo)
405 (choice
406 (const :tag "Any not-done state" todo)
407 (const :tag "Any done state" done)
408 (const :tag "Any state" any)
409 (list :tag "Keyword list"
410 (const :format "" quote)
411 (repeat (string :tag "Keyword")))))
412 (list :tag "TODO state is not" :inline t
413 (const nottodo)
414 (choice
415 (const :tag "Any not-done state" todo)
416 (const :tag "Any done state" done)
417 (const :tag "Any state" any)
418 (list :tag "Keyword list"
419 (const :format "" quote)
420 (repeat (string :tag "Keyword")))))
421 (const :tag "scheduled" scheduled)
422 (const :tag "not scheduled" notscheduled)
423 (const :tag "deadline" deadline)
424 (const :tag "no deadline" notdeadline)
425 (const :tag "timestamp" timestamp)
426 (const :tag "no timestamp" nottimestamp))))))
427 (list :tag "Non-standard skipping condition"
428 :value (org-agenda-skip-function)
429 (const org-agenda-skip-function)
430 (sexp :tag "Function or form (quoted!)"))
431 (list :tag "Any variable"
432 (variable :tag "Variable")
433 (sexp :tag "Value (sexp)"))))
434 "Selection of examples for agenda command settings.
435 This will be spliced into the custom type of
436 `org-agenda-custom-commands'.")
439 (defcustom org-agenda-custom-commands
440 '(("n" "Agenda and all TODOs" ((agenda "") (alltodo ""))))
441 "Custom commands for the agenda.
442 \\<org-mode-map>
443 These commands will be offered on the splash screen displayed by the
444 agenda dispatcher `\\[org-agenda]'. Each entry is a list like this:
446 (key desc type match settings files)
448 key The key (one or more characters as a string) to be associated
449 with the command.
450 desc A description of the command, when omitted or nil, a default
451 description is built using MATCH.
452 type The command type, any of the following symbols:
453 agenda The daily/weekly agenda.
454 todo Entries with a specific TODO keyword, in all agenda files.
455 search Entries containing search words entry or headline.
456 tags Tags/Property/TODO match in all agenda files.
457 tags-todo Tags/P/T match in all agenda files, TODO entries only.
458 todo-tree Sparse tree of specific TODO keyword in *current* file.
459 tags-tree Sparse tree with all tags matches in *current* file.
460 occur-tree Occur sparse tree for *current* file.
461 ... A user-defined function.
462 match What to search for:
463 - a single keyword for TODO keyword searches
464 - a tags match expression for tags searches
465 - a word search expression for text searches.
466 - a regular expression for occur searches
467 For all other commands, this should be the empty string.
468 settings A list of option settings, similar to that in a let form, so like
469 this: ((opt1 val1) (opt2 val2) ...). The values will be
470 evaluated at the moment of execution, so quote them when needed.
471 files A list of files to write the produced agenda buffer to with
472 the command `org-store-agenda-views'.
473 If a file name ends in \".html\", an HTML version of the buffer
474 is written out. If it ends in \".ps\", a postscript version is
475 produced. Otherwise, only the plain text is written to the file.
477 You can also define a set of commands, to create a composite agenda buffer.
478 In this case, an entry looks like this:
480 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
482 where
484 desc A description string to be displayed in the dispatcher menu.
485 cmd An agenda command, similar to the above. However, tree commands
486 are not allowed, but instead you can get agenda and global todo list.
487 So valid commands for a set are:
488 (agenda \"\" settings)
489 (alltodo \"\" settings)
490 (stuck \"\" settings)
491 (todo \"match\" settings files)
492 (search \"match\" settings files)
493 (tags \"match\" settings files)
494 (tags-todo \"match\" settings files)
496 Each command can carry a list of options, and another set of options can be
497 given for the whole set of commands. Individual command options take
498 precedence over the general options.
500 When using several characters as key to a command, the first characters
501 are prefix commands. For the dispatcher to display useful information, you
502 should provide a description for the prefix, like
504 (setq org-agenda-custom-commands
505 \\='((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
506 (\"hl\" tags \"+HOME+Lisa\")
507 (\"hp\" tags \"+HOME+Peter\")
508 (\"hk\" tags \"+HOME+Kim\")))"
509 :group 'org-agenda-custom-commands
510 :type `(repeat
511 (choice :value ("x" "Describe command here" tags "" nil)
512 (list :tag "Single command"
513 (string :tag "Access Key(s) ")
514 (option (string :tag "Description"))
515 (choice
516 (const :tag "Agenda" agenda)
517 (const :tag "TODO list" alltodo)
518 (const :tag "Search words" search)
519 (const :tag "Stuck projects" stuck)
520 (const :tag "Tags/Property match (all agenda files)" tags)
521 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
522 (const :tag "TODO keyword search (all agenda files)" todo)
523 (const :tag "Tags sparse tree (current buffer)" tags-tree)
524 (const :tag "TODO keyword tree (current buffer)" todo-tree)
525 (const :tag "Occur tree (current buffer)" occur-tree)
526 (sexp :tag "Other, user-defined function"))
527 (string :tag "Match (only for some commands)")
528 ,org-agenda-custom-commands-local-options
529 (option (repeat :tag "Export" (file :tag "Export to"))))
530 (list :tag "Command series, all agenda files"
531 (string :tag "Access Key(s)")
532 (string :tag "Description ")
533 (repeat :tag "Component"
534 (choice
535 (list :tag "Agenda"
536 (const :format "" agenda)
537 (const :tag "" :format "" "")
538 ,org-agenda-custom-commands-local-options)
539 (list :tag "TODO list (all keywords)"
540 (const :format "" alltodo)
541 (const :tag "" :format "" "")
542 ,org-agenda-custom-commands-local-options)
543 (list :tag "Search words"
544 (const :format "" search)
545 (string :tag "Match")
546 ,org-agenda-custom-commands-local-options)
547 (list :tag "Stuck projects"
548 (const :format "" stuck)
549 (const :tag "" :format "" "")
550 ,org-agenda-custom-commands-local-options)
551 (list :tag "Tags search"
552 (const :format "" tags)
553 (string :tag "Match")
554 ,org-agenda-custom-commands-local-options)
555 (list :tag "Tags search, TODO entries only"
556 (const :format "" tags-todo)
557 (string :tag "Match")
558 ,org-agenda-custom-commands-local-options)
559 (list :tag "TODO keyword search"
560 (const :format "" todo)
561 (string :tag "Match")
562 ,org-agenda-custom-commands-local-options)
563 (list :tag "Other, user-defined function"
564 (symbol :tag "function")
565 (string :tag "Match")
566 ,org-agenda-custom-commands-local-options)))
568 (repeat :tag "Settings for entire command set"
569 (list (variable :tag "Any variable")
570 (sexp :tag "Value")))
571 (option (repeat :tag "Export" (file :tag "Export to"))))
572 (cons :tag "Prefix key documentation"
573 (string :tag "Access Key(s)")
574 (string :tag "Description ")))))
576 (defcustom org-agenda-query-register ?o
577 "The register holding the current query string.
578 The purpose of this is that if you construct a query string interactively,
579 you can then use it to define a custom command."
580 :group 'org-agenda-custom-commands
581 :type 'character)
583 (defcustom org-stuck-projects
584 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
585 "How to identify stuck projects.
586 This is a list of four items:
587 1. A tags/todo/property matcher string that is used to identify a project.
588 See the manual for a description of tag and property searches.
589 The entire tree below a headline matched by this is considered one project.
590 2. A list of TODO keywords identifying non-stuck projects.
591 If the project subtree contains any headline with one of these todo
592 keywords, the project is considered to be not stuck. If you specify
593 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
594 3. A list of tags identifying non-stuck projects.
595 If the project subtree contains any headline with one of these tags,
596 the project is considered to be not stuck. If you specify \"*\" as
597 a tag, any tag will mark the project unstuck. Note that this is about
598 the explicit presence of a tag somewhere in the subtree, inherited
599 tags do not count here. If inherited tags make a project not stuck,
600 use \"-TAG\" in the tags part of the matcher under (1.) above.
601 4. An arbitrary regular expression matching non-stuck projects.
603 If the project turns out to be not stuck, search continues also in the
604 subtree to see if any of the subtasks have project status.
606 See also the variable `org-tags-match-list-sublevels' which applies
607 to projects matched by this search as well.
609 After defining this variable, you may use `org-agenda-list-stuck-projects'
610 \(bound to `\\[org-agenda] #') to produce the list."
611 :group 'org-agenda-custom-commands
612 :type '(list
613 (string :tag "Tags/TODO match to identify a project")
614 (repeat :tag "Projects are *not* stuck if they have an entry with \
615 TODO keyword any of" (string))
616 (repeat :tag "Projects are *not* stuck if they have an entry with \
617 TAG being any of" (string))
618 (regexp :tag "Projects are *not* stuck if this regexp matches inside \
619 the subtree")))
621 (defgroup org-agenda-skip nil
622 "Options concerning skipping parts of agenda files."
623 :tag "Org Agenda Skip"
624 :group 'org-agenda)
626 (defcustom org-agenda-skip-function-global nil
627 "Function to be called at each match during agenda construction.
628 If this function returns nil, the current match should not be skipped.
629 If the function decided to skip an agenda match, is must return the
630 buffer position from which the search should be continued.
631 This may also be a Lisp form, which will be evaluated.
633 This variable will be applied to every agenda match, including
634 tags/property searches and TODO lists. So try to make the test function
635 do its checking as efficiently as possible. To implement a skipping
636 condition just for specific agenda commands, use the variable
637 `org-agenda-skip-function' which can be set in the options section
638 of custom agenda commands."
639 :group 'org-agenda-skip
640 :type 'sexp)
642 (defgroup org-agenda-daily/weekly nil
643 "Options concerning the daily/weekly agenda."
644 :tag "Org Agenda Daily/Weekly"
645 :group 'org-agenda)
646 (defgroup org-agenda-todo-list nil
647 "Options concerning the global todo list agenda view."
648 :tag "Org Agenda Todo List"
649 :group 'org-agenda)
650 (defgroup org-agenda-match-view nil
651 "Options concerning the general tags/property/todo match agenda view."
652 :tag "Org Agenda Match View"
653 :group 'org-agenda)
654 (defgroup org-agenda-search-view nil
655 "Options concerning the search agenda view."
656 :tag "Org Agenda Search View"
657 :group 'org-agenda)
659 (defvar org-agenda-archives-mode nil
660 "Non-nil means the agenda will include archived items.
661 If this is the symbol `trees', trees in the selected agenda scope
662 that are marked with the ARCHIVE tag will be included anyway. When this is
663 t, also all archive files associated with the current selection of agenda
664 files will be included.")
666 (defcustom org-agenda-restriction-lock-highlight-subtree t
667 "Non-nil means highlight the whole subtree when restriction is active.
668 Otherwise only highlight the headline. Highlighting the whole subtree is
669 useful to ensure no edits happen beyond the restricted region."
670 :group 'org-agenda
671 :type 'boolean)
673 (defcustom org-agenda-skip-comment-trees t
674 "Non-nil means skip trees that start with the COMMENT keyword.
675 When nil, these trees are also scanned by agenda commands."
676 :group 'org-agenda-skip
677 :type 'boolean)
679 (defcustom org-agenda-todo-list-sublevels t
680 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
681 When nil, the sublevels of a TODO entry are not checked, resulting in
682 potentially much shorter TODO lists."
683 :group 'org-agenda-skip
684 :group 'org-agenda-todo-list
685 :type 'boolean)
687 (defcustom org-agenda-todo-ignore-with-date nil
688 "Non-nil means don't show entries with a date in the global todo list.
689 You can use this if you prefer to mark mere appointments with a TODO keyword,
690 but don't want them to show up in the TODO list.
691 When this is set, it also covers deadlines and scheduled items, the settings
692 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
693 will be ignored.
694 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
695 :group 'org-agenda-skip
696 :group 'org-agenda-todo-list
697 :type 'boolean)
699 (defcustom org-agenda-todo-ignore-timestamp nil
700 "Non-nil means don't show entries with a timestamp.
701 This applies when creating the global todo list.
702 Valid values are:
704 past Don't show entries for today or in the past.
706 future Don't show entries with a timestamp in the future.
707 The idea behind this is that if it has a future
708 timestamp, you don't want to think about it until the
709 date.
711 all Don't show any entries with a timestamp in the global todo list.
712 The idea behind this is that by setting a timestamp, you
713 have already \"taken care\" of this item.
715 This variable can also have an integer as a value. If positive (N),
716 todos with a timestamp N or more days in the future will be ignored. If
717 negative (-N), todos with a timestamp N or more days in the past will be
718 ignored. If 0, todos with a timestamp either today or in the future will
719 be ignored. For example, a value of -1 will exclude todos with a
720 timestamp in the past (yesterday or earlier), while a value of 7 will
721 exclude todos with a timestamp a week or more in the future.
723 See also `org-agenda-todo-ignore-with-date'.
724 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
725 to make his option also apply to the tags-todo list."
726 :group 'org-agenda-skip
727 :group 'org-agenda-todo-list
728 :version "24.1"
729 :type '(choice
730 (const :tag "Ignore future timestamp todos" future)
731 (const :tag "Ignore past or present timestamp todos" past)
732 (const :tag "Ignore all timestamp todos" all)
733 (const :tag "Show timestamp todos" nil)
734 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
736 (defcustom org-agenda-todo-ignore-scheduled nil
737 "Non-nil means, ignore some scheduled TODO items when making TODO list.
738 This applies when creating the global todo list.
739 Valid values are:
741 past Don't show entries scheduled today or in the past.
743 future Don't show entries scheduled in the future.
744 The idea behind this is that by scheduling it, you don't want to
745 think about it until the scheduled date.
747 all Don't show any scheduled entries in the global todo list.
748 The idea behind this is that by scheduling it, you have already
749 \"taken care\" of this item.
751 t Same as `all', for backward compatibility.
753 This variable can also have an integer as a value. See
754 `org-agenda-todo-ignore-timestamp' for more details.
756 See also `org-agenda-todo-ignore-with-date'.
757 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
758 to make his option also apply to the tags-todo list."
759 :group 'org-agenda-skip
760 :group 'org-agenda-todo-list
761 :type '(choice
762 (const :tag "Ignore future-scheduled todos" future)
763 (const :tag "Ignore past- or present-scheduled todos" past)
764 (const :tag "Ignore all scheduled todos" all)
765 (const :tag "Ignore all scheduled todos (compatibility)" t)
766 (const :tag "Show scheduled todos" nil)
767 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
769 (defcustom org-agenda-todo-ignore-deadlines nil
770 "Non-nil means ignore some deadline TODO items when making TODO list.
772 There are different motivations for using different values, please think
773 carefully when configuring this variable.
775 This applies when creating the global TODO list.
777 Valid values are:
779 near Don't show near deadline entries. A deadline is near when it is
780 closer than `org-deadline-warning-days' days. The idea behind this
781 is that such items will appear in the agenda anyway.
783 far Don't show TODO entries where a deadline has been defined, but
784 is not going to happen anytime soon. This is useful if you want to use
785 the TODO list to figure out what to do now.
787 past Don't show entries with a deadline timestamp for today or in the past.
789 future Don't show entries with a deadline timestamp in the future, not even
790 when they become `near' ones. Use it with caution.
792 all Ignore all TODO entries that do have a deadline.
794 t Same as `near', for backward compatibility.
796 This variable can also have an integer as a value. See
797 `org-agenda-todo-ignore-timestamp' for more details.
799 See also `org-agenda-todo-ignore-with-date'.
800 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
801 to make his option also apply to the tags-todo list."
802 :group 'org-agenda-skip
803 :group 'org-agenda-todo-list
804 :type '(choice
805 (const :tag "Ignore near deadlines" near)
806 (const :tag "Ignore near deadlines (compatibility)" t)
807 (const :tag "Ignore far deadlines" far)
808 (const :tag "Ignore all TODOs with a deadlines" all)
809 (const :tag "Show all TODOs, even if they have a deadline" nil)
810 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
812 (defcustom org-agenda-todo-ignore-time-comparison-use-seconds nil
813 "Time unit to use when possibly ignoring an agenda item.
815 See the docstring of various `org-agenda-todo-ignore-*' options.
816 The default is to compare time stamps using days. An item is thus
817 considered to be in the future if it is at least one day after today.
818 Non-nil means to compare time stamps using seconds. An item is then
819 considered future if it has a time value later than current time."
820 :group 'org-agenda-skip
821 :group 'org-agenda-todo-list
822 :version "24.4"
823 :package-version '(Org . "8.0")
824 :type '(choice
825 (const :tag "Compare time with days" nil)
826 (const :tag "Compare time with seconds" t)))
828 (defcustom org-agenda-tags-todo-honor-ignore-options nil
829 "Non-nil means honor todo-list ignores options also in tags-todo search.
830 The variables
831 `org-agenda-todo-ignore-with-date',
832 `org-agenda-todo-ignore-timestamp',
833 `org-agenda-todo-ignore-scheduled',
834 `org-agenda-todo-ignore-deadlines'
835 make the global TODO list skip entries that have time stamps of certain
836 kinds. If this option is set, the same options will also apply for the
837 tags-todo search, which is the general tags/property matcher
838 restricted to unfinished TODO entries only."
839 :group 'org-agenda-skip
840 :group 'org-agenda-todo-list
841 :group 'org-agenda-match-view
842 :type 'boolean)
844 (defcustom org-agenda-skip-scheduled-if-done nil
845 "Non-nil means don't show scheduled items in agenda when they are done.
846 This is relevant for the daily/weekly agenda, not for the TODO list. It
847 applies only to the actual date of the scheduling. Warnings about an item
848 with a past scheduling dates are always turned off when the item is DONE."
849 :group 'org-agenda-skip
850 :group 'org-agenda-daily/weekly
851 :type 'boolean)
853 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
854 "Non-nil means skip scheduling line if same entry shows because of deadline.
856 In the agenda of today, an entry can show up multiple times
857 because it is both scheduled and has a nearby deadline, and maybe
858 a plain time stamp as well.
860 When this variable is nil, the entry will be shown several times.
862 When set to t, then only the deadline is shown and the fact that
863 the entry is scheduled today or was scheduled previously is not
864 shown.
866 When set to the symbol `not-today', skip scheduled previously,
867 but not scheduled today.
869 When set to the symbol `repeated-after-deadline', skip scheduled
870 items if they are repeated beyond the current deadline."
871 :group 'org-agenda-skip
872 :group 'org-agenda-daily/weekly
873 :type '(choice
874 (const :tag "Never" nil)
875 (const :tag "Always" t)
876 (const :tag "Not when scheduled today" not-today)
877 (const :tag "When repeated past deadline" repeated-after-deadline)))
879 (defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
880 "Non-nil means skip timestamp line if same entry shows because of deadline.
881 In the agenda of today, an entry can show up multiple times
882 because it has both a plain timestamp and has a nearby deadline.
883 When this variable is t, then only the deadline is shown and the
884 fact that the entry has a timestamp for or including today is not
885 shown. When this variable is nil, the entry will be shown
886 several times."
887 :group 'org-agenda-skip
888 :group 'org-agenda-daily/weekly
889 :version "24.1"
890 :type '(choice
891 (const :tag "Never" nil)
892 (const :tag "Always" t)))
894 (defcustom org-agenda-skip-deadline-if-done nil
895 "Non-nil means don't show deadlines when the corresponding item is done.
896 When nil, the deadline is still shown and should give you a happy feeling.
897 This is relevant for the daily/weekly agenda. It applies only to the
898 actual date of the deadline. Warnings about approaching and past-due
899 deadlines are always turned off when the item is DONE."
900 :group 'org-agenda-skip
901 :group 'org-agenda-daily/weekly
902 :type 'boolean)
904 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
905 "Non-nil means skip deadline prewarning when entry is also scheduled.
906 This will apply on all days where a prewarning for the deadline would
907 be shown, but not at the day when the entry is actually due. On that day,
908 the deadline will be shown anyway.
909 This variable may be set to nil, t, the symbol `pre-scheduled',
910 or a number which will then give the number of days before the actual
911 deadline when the prewarnings should resume. The symbol `pre-scheduled'
912 eliminates the deadline prewarning only prior to the scheduled date.
913 This can be used in a workflow where the first showing of the deadline will
914 trigger you to schedule it, and then you don't want to be reminded of it
915 because you will take care of it on the day when scheduled."
916 :group 'org-agenda-skip
917 :group 'org-agenda-daily/weekly
918 :version "24.1"
919 :type '(choice
920 (const :tag "Always show prewarning" nil)
921 (const :tag "Remove prewarning prior to scheduled date" pre-scheduled)
922 (const :tag "Remove prewarning if entry is scheduled" t)
923 (integer :tag "Restart prewarning N days before deadline")))
925 (defcustom org-agenda-skip-scheduled-delay-if-deadline nil
926 "Non-nil means skip scheduled delay when entry also has a deadline.
927 This variable may be set to nil, t, the symbol `post-deadline',
928 or a number which will then give the number of days after the actual
929 scheduled date when the delay should expire. The symbol `post-deadline'
930 eliminates the schedule delay when the date is posterior to the deadline."
931 :group 'org-agenda-skip
932 :group 'org-agenda-daily/weekly
933 :version "24.4"
934 :package-version '(Org . "8.0")
935 :type '(choice
936 (const :tag "Always honor delay" nil)
937 (const :tag "Ignore delay if posterior to the deadline" post-deadline)
938 (const :tag "Ignore delay if entry has a deadline" t)
939 (integer :tag "Honor delay up until N days after the scheduled date")))
941 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
942 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
943 When non-nil, after the search for timestamps has matched once in an
944 entry, the rest of the entry will not be searched."
945 :group 'org-agenda-skip
946 :type 'boolean)
948 (defcustom org-agenda-skip-timestamp-if-done nil
949 "Non-nil means don't select item by timestamp or -range if it is DONE."
950 :group 'org-agenda-skip
951 :group 'org-agenda-daily/weekly
952 :type 'boolean)
954 (defcustom org-agenda-dim-blocked-tasks t
955 "Non-nil means dim blocked tasks in the agenda display.
956 This causes some overhead during agenda construction, but if you
957 have turned on `org-enforce-todo-dependencies',
958 `org-enforce-todo-checkbox-dependencies', or any other blocking
959 mechanism, this will create useful feedback in the agenda.
961 Instead of t, this variable can also have the value `invisible'.
962 Then blocked tasks will be invisible and only become visible when
963 they become unblocked. An exemption to this behavior is when a task is
964 blocked because of unchecked checkboxes below it. Since checkboxes do
965 not show up in the agenda views, making this task invisible you remove any
966 trace from agenda views that there is something to do. Therefore, a task
967 that is blocked because of checkboxes will never be made invisible, it
968 will only be dimmed."
969 :group 'org-agenda-daily/weekly
970 :group 'org-agenda-todo-list
971 :version "24.3"
972 :type '(choice
973 (const :tag "Do not dim" nil)
974 (const :tag "Dim to a gray face" t)
975 (const :tag "Make invisible" invisible)))
977 (defgroup org-agenda-startup nil
978 "Options concerning initial settings in the Agenda in Org Mode."
979 :tag "Org Agenda Startup"
980 :group 'org-agenda)
982 (defcustom org-agenda-menu-show-matcher t
983 "Non-nil means show the match string in the agenda dispatcher menu.
984 When nil, the matcher string is not shown, but is put into the help-echo
985 property so than moving the mouse over the command shows it.
986 Setting it to nil is good if matcher strings are very long and/or if
987 you want to use two-columns display (see `org-agenda-menu-two-columns')."
988 :group 'org-agenda
989 :version "24.1"
990 :type 'boolean)
992 (defcustom org-agenda-menu-two-columns nil
993 "Non-nil means, use two columns to show custom commands in the dispatcher.
994 If you use this, you probably want to set `org-agenda-menu-show-matcher'
995 to nil."
996 :group 'org-agenda
997 :version "24.1"
998 :type 'boolean)
1000 (defcustom org-agenda-finalize-hook nil
1001 "Hook run just before displaying an agenda buffer.
1002 The buffer is still writable when the hook is called.
1004 You can modify some of the buffer substrings but you should be
1005 extra careful not to modify the text properties of the agenda
1006 headlines as the agenda display heavily relies on them."
1007 :group 'org-agenda-startup
1008 :type 'hook)
1010 (defcustom org-agenda-mouse-1-follows-link nil
1011 "Non-nil means mouse-1 on a link will follow the link in the agenda.
1012 A longer mouse click will still set point. Needs to be set
1013 before org.el is loaded."
1014 :group 'org-agenda-startup
1015 :type 'boolean)
1017 (defcustom org-agenda-start-with-follow-mode nil
1018 "The initial value of follow mode in a newly created agenda window."
1019 :group 'org-agenda-startup
1020 :type 'boolean)
1022 (defcustom org-agenda-follow-indirect nil
1023 "Non-nil means `org-agenda-follow-mode' displays only the
1024 current item's tree, in an indirect buffer."
1025 :group 'org-agenda
1026 :version "24.1"
1027 :type 'boolean)
1029 (defcustom org-agenda-show-outline-path t
1030 "Non-nil means show outline path in echo area after line motion."
1031 :group 'org-agenda-startup
1032 :type 'boolean)
1034 (defcustom org-agenda-start-with-entry-text-mode nil
1035 "The initial value of entry-text-mode in a newly created agenda window."
1036 :group 'org-agenda-startup
1037 :type 'boolean)
1039 (defcustom org-agenda-entry-text-maxlines 5
1040 "Number of text lines to be added when `E' is pressed in the agenda.
1042 Note that this variable only used during agenda display. To add entry text
1043 when exporting the agenda, configure the variable
1044 `org-agenda-add-entry-text-maxlines'."
1045 :group 'org-agenda
1046 :type 'integer)
1048 (defcustom org-agenda-entry-text-exclude-regexps nil
1049 "List of regular expressions to clean up entry text.
1050 The complete matches of all regular expressions in this list will be
1051 removed from entry text before it is shown in the agenda."
1052 :group 'org-agenda
1053 :type '(repeat (regexp)))
1055 (defcustom org-agenda-entry-text-leaders " > "
1056 "Text prepended to the entry text in agenda buffers."
1057 :version "24.4"
1058 :package-version '(Org . "8.0")
1059 :group 'org-agenda
1060 :type 'string)
1062 (defvar org-agenda-entry-text-cleanup-hook nil
1063 "Hook that is run after basic cleanup of entry text to be shown in agenda.
1064 This cleanup is done in a temporary buffer, so the function may inspect and
1065 change the entire buffer.
1066 Some default stuff like drawers and scheduling/deadline dates will already
1067 have been removed when this is called, as will any matches for regular
1068 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
1070 (defvar org-agenda-include-inactive-timestamps nil
1071 "Non-nil means include inactive time stamps in agenda.
1072 Dynamically scoped.")
1074 (defgroup org-agenda-windows nil
1075 "Options concerning the windows used by the Agenda in Org Mode."
1076 :tag "Org Agenda Windows"
1077 :group 'org-agenda)
1079 (defcustom org-agenda-window-setup 'reorganize-frame
1080 "How the agenda buffer should be displayed.
1081 Possible values for this option are:
1083 current-window Show agenda in the current window, keeping all other windows.
1084 other-window Use `switch-to-buffer-other-window' to display agenda.
1085 only-window Show agenda, deleting all other windows.
1086 reorganize-frame Show only two windows on the current frame, the current
1087 window and the agenda.
1088 other-frame Use `switch-to-buffer-other-frame' to display agenda.
1089 Also, when exiting the agenda, kill that frame.
1090 See also the variable `org-agenda-restore-windows-after-quit'."
1091 :group 'org-agenda-windows
1092 :type '(choice
1093 (const current-window)
1094 (const other-frame)
1095 (const other-window)
1096 (const only-window)
1097 (const reorganize-frame)))
1099 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
1100 "The min and max height of the agenda window as a fraction of frame height.
1101 The value of the variable is a cons cell with two numbers between 0 and 1.
1102 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
1103 :group 'org-agenda-windows
1104 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
1106 (defcustom org-agenda-restore-windows-after-quit nil
1107 "Non-nil means restore window configuration upon exiting agenda.
1108 Before the window configuration is changed for displaying the agenda,
1109 the current status is recorded. When the agenda is exited with
1110 `q' or `x' and this option is set, the old state is restored. If
1111 `org-agenda-window-setup' is `other-frame', the value of this
1112 option will be ignored."
1113 :group 'org-agenda-windows
1114 :type 'boolean)
1116 (defcustom org-agenda-span 'week
1117 "Number of days to include in overview display.
1118 Can be day, week, month, year, or any number of days.
1119 Custom commands can set this variable in the options section."
1120 :group 'org-agenda-daily/weekly
1121 :type '(choice (const :tag "Day" day)
1122 (const :tag "Week" week)
1123 (const :tag "Fortnight" fortnight)
1124 (const :tag "Month" month)
1125 (const :tag "Year" year)
1126 (integer :tag "Custom")))
1128 (defcustom org-agenda-start-on-weekday 1
1129 "Non-nil means start the overview always on the specified weekday.
1130 0 denotes Sunday, 1 denotes Monday, etc.
1131 When nil, always start on the current day.
1132 Custom commands can set this variable in the options section."
1133 :group 'org-agenda-daily/weekly
1134 :type '(choice (const :tag "Today" nil)
1135 (integer :tag "Weekday No.")))
1137 (defcustom org-agenda-show-all-dates t
1138 "Non-nil means `org-agenda' shows every day in the selected range.
1139 When nil, only the days which actually have entries are shown."
1140 :group 'org-agenda-daily/weekly
1141 :type 'boolean)
1143 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1144 "Format string for displaying dates in the agenda.
1145 Used by the daily/weekly agenda. This should be a format string
1146 understood by `format-time-string', or a function returning the
1147 formatted date as a string. The function must take a single
1148 argument, a calendar-style date list like (month day year)."
1149 :group 'org-agenda-daily/weekly
1150 :type '(choice
1151 (string :tag "Format string")
1152 (function :tag "Function")))
1154 (defun org-agenda-format-date-aligned (date)
1155 "Format a DATE string for display in the daily/weekly agenda.
1156 This function makes sure that dates are aligned for easy reading."
1157 (require 'cal-iso)
1158 (let* ((dayname (calendar-day-name date))
1159 (day (cadr date))
1160 (day-of-week (calendar-day-of-week date))
1161 (month (car date))
1162 (monthname (calendar-month-name month))
1163 (year (nth 2 date))
1164 (iso-week (org-days-to-iso-week
1165 (calendar-absolute-from-gregorian date)))
1166 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1167 (1- year))
1168 ((and (= month 12) (<= iso-week 1))
1169 (1+ year))
1170 (t year)))
1171 (weekstring (if (= day-of-week 1)
1172 (format " W%02d" iso-week)
1173 "")))
1174 (format "%-10s %2d %s %4d%s"
1175 dayname day monthname year weekstring)))
1177 (defcustom org-agenda-time-leading-zero nil
1178 "Non-nil means use leading zero for military times in agenda.
1179 For example, 9:30am would become 09:30 rather than 9:30."
1180 :group 'org-agenda-daily/weekly
1181 :version "24.1"
1182 :type 'boolean)
1184 (defcustom org-agenda-timegrid-use-ampm nil
1185 "When set, show AM/PM style timestamps on the timegrid."
1186 :group 'org-agenda
1187 :version "24.1"
1188 :type 'boolean)
1190 (defun org-agenda-time-of-day-to-ampm (time)
1191 "Convert TIME of a string like \"13:45\" to an AM/PM style time string."
1192 (let* ((hour-number (string-to-number (substring time 0 -3)))
1193 (minute (substring time -2))
1194 (ampm "am"))
1195 (cond
1196 ((equal hour-number 12)
1197 (setq ampm "pm"))
1198 ((> hour-number 12)
1199 (setq ampm "pm")
1200 (setq hour-number (- hour-number 12))))
1201 (concat
1202 (if org-agenda-time-leading-zero
1203 (format "%02d" hour-number)
1204 (format "%02s" (number-to-string hour-number)))
1205 ":" minute ampm)))
1207 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1208 "Conditionally convert TIME to AM/PM format based on `org-agenda-timegrid-use-ampm'."
1209 (if org-agenda-timegrid-use-ampm
1210 (org-agenda-time-of-day-to-ampm time)
1211 time))
1213 (defcustom org-agenda-weekend-days '(6 0)
1214 "Which days are weekend?
1215 These days get the special face `org-agenda-date-weekend' in the agenda."
1216 :group 'org-agenda-daily/weekly
1217 :type '(set :greedy t
1218 (const :tag "Monday" 1)
1219 (const :tag "Tuesday" 2)
1220 (const :tag "Wednesday" 3)
1221 (const :tag "Thursday" 4)
1222 (const :tag "Friday" 5)
1223 (const :tag "Saturday" 6)
1224 (const :tag "Sunday" 0)))
1226 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1227 "Non-nil means jump to today when moving a past date forward in time.
1228 When using S-right in the agenda to move a a date forward, and the date
1229 stamp currently points to the past, the first key press will move it
1230 to today. WHen nil, just move one day forward even if the date stays
1231 in the past."
1232 :group 'org-agenda-daily/weekly
1233 :version "24.1"
1234 :type 'boolean)
1236 (defcustom org-agenda-include-diary nil
1237 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1238 Custom commands can set this variable in the options section."
1239 :group 'org-agenda-daily/weekly
1240 :type 'boolean)
1242 (defcustom org-agenda-include-deadlines t
1243 "If non-nil, include entries within their deadline warning period.
1244 Custom commands can set this variable in the options section."
1245 :group 'org-agenda-daily/weekly
1246 :version "24.1"
1247 :type 'boolean)
1249 (defcustom org-agenda-show-future-repeats t
1250 "Non-nil shows repeated entries in the future part of the agenda.
1251 When set to the symbol `next' only the first future repeat is shown."
1252 :group 'org-agenda-daily/weekly
1253 :type '(choice
1254 (const :tag "Show all repeated entries" t)
1255 (const :tag "Show next repeated entry" next)
1256 (const :tag "Do not show repeated entries" nil))
1257 :version "26.1"
1258 :package-version '(Org . "9.1")
1259 :safe #'symbolp)
1261 (defcustom org-agenda-prefer-last-repeat nil
1262 "Non-nil sets date for repeated entries to their last repeat.
1264 When nil, display SCHEDULED and DEADLINE dates at their base
1265 date, and in today's agenda, as a reminder. Display plain
1266 time-stamps, on the other hand, at every repeat date in the past
1267 in addition to the base date.
1269 When non-nil, show a repeated entry at its latest repeat date,
1270 possibly being today even if it wasn't marked as done. This
1271 setting is useful if you do not always mark repeated entries as
1272 done and, yet, consider that reaching repeat date starts the task
1273 anew.
1275 When set to a list of strings, prefer last repeats only for
1276 entries with these TODO keywords."
1277 :group 'org-agenda-daily/weekly
1278 :type '(choice
1279 (const :tag "Prefer last repeat" t)
1280 (const :tag "Prefer base date" nil)
1281 (repeat :tag "Prefer last repeat for entries with these TODO keywords"
1282 (string :tag "TODO keyword")))
1283 :version "26.1"
1284 :package-version '(Org . "9.1")
1285 :safe (lambda (x) (or (booleanp x) (consp x))))
1287 (defcustom org-scheduled-past-days 10000
1288 "Number of days to continue listing scheduled items not marked DONE.
1289 When an item is scheduled on a date, it shows up in the agenda on
1290 this day and will be listed until it is marked done or for the
1291 number of days given here."
1292 :group 'org-agenda-daily/weekly
1293 :type 'integer
1294 :safe 'integerp)
1296 (defcustom org-deadline-past-days 10000
1297 "Number of days to warn about missed deadlines.
1298 When an item has deadline on a date, it shows up in the agenda on
1299 this day and will appear as a reminder until it is marked DONE or
1300 for the number of days given here."
1301 :group 'org-agenda-daily/weekly
1302 :type 'integer
1303 :version "26.1"
1304 :package-version '(Org . "9.1")
1305 :safe 'integerp)
1307 (defcustom org-agenda-log-mode-items '(closed clock)
1308 "List of items that should be shown in agenda log mode.
1309 \\<org-agenda-mode-map>\
1310 This list may contain the following symbols:
1312 closed Show entries that have been closed on that day.
1313 clock Show entries that have received clocked time on that day.
1314 state Show all logged state changes.
1315 Note that instead of changing this variable, you can also press \
1316 `\\[universal-argument] \\[org-agenda-log-mode]' in
1317 the agenda to display all available LOG items temporarily."
1318 :group 'org-agenda-daily/weekly
1319 :type '(set :greedy t (const closed) (const clock) (const state)))
1321 (defcustom org-agenda-clock-consistency-checks
1322 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1323 :gap-ok-around ("4:00")
1324 :default-face ((:background "DarkRed") (:foreground "white"))
1325 :overlap-face nil :gap-face nil :no-end-time-face nil
1326 :long-face nil :short-face nil)
1327 "This is a property list, with the following keys:
1329 :max-duration Mark clocking chunks that are longer than this time.
1330 This is a time string like \"HH:MM\", or the number
1331 of minutes as an integer.
1333 :min-duration Mark clocking chunks that are shorter that this.
1334 This is a time string like \"HH:MM\", or the number
1335 of minutes as an integer.
1337 :max-gap Mark gaps between clocking chunks that are longer than
1338 this duration. A number of minutes, or a string
1339 like \"HH:MM\".
1341 :gap-ok-around List of times during the day which are usually not working
1342 times. When a gap is detected, but the gap contains any
1343 of these times, the gap is *not* reported. For example,
1344 if this is (\"4:00\" \"13:00\") then gaps that contain
1345 4:00 in the morning (i.e. the night) and 13:00
1346 (i.e. a typical lunch time) do not cause a warning.
1347 You should have at least one time during the night in this
1348 list, or otherwise the first task each morning will trigger
1349 a warning because it follows a long gap.
1351 Furthermore, the following properties can be used to define faces for
1352 issue display.
1354 :default-face the default face, if the specific face is undefined
1355 :overlap-face face for overlapping clocks
1356 :gap-face face for gaps between clocks
1357 :no-end-time-face face for incomplete clocks
1358 :long-face face for clock intervals that are too long
1359 :short-face face for clock intervals that are too short"
1360 :group 'org-agenda-daily/weekly
1361 :group 'org-clock
1362 :version "24.1"
1363 :type 'plist)
1365 (defcustom org-agenda-log-mode-add-notes t
1366 "Non-nil means add first line of notes to log entries in agenda views.
1367 If a log item like a state change or a clock entry is associated with
1368 notes, the first line of these notes will be added to the entry in the
1369 agenda display."
1370 :group 'org-agenda-daily/weekly
1371 :type 'boolean)
1373 (defcustom org-agenda-start-with-log-mode nil
1374 "The initial value of log-mode in a newly created agenda window.
1375 See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
1376 explanations on the possible values."
1377 :group 'org-agenda-startup
1378 :group 'org-agenda-daily/weekly
1379 :type '(choice (const :tag "Don't show log items" nil)
1380 (const :tag "Show only log items" only)
1381 (const :tag "Show all possible log items" clockcheck)
1382 (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
1383 (choice (const :tag "Show closed log items" closed)
1384 (const :tag "Show clocked log items" clock)
1385 (const :tag "Show all logged state changes" state)))))
1387 (defcustom org-agenda-start-with-clockreport-mode nil
1388 "The initial value of clockreport-mode in a newly created agenda window."
1389 :group 'org-agenda-startup
1390 :group 'org-agenda-daily/weekly
1391 :type 'boolean)
1393 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1394 "Property list with parameters for the clocktable in clockreport mode.
1395 This is the display mode that shows a clock table in the daily/weekly
1396 agenda, the properties for this dynamic block can be set here.
1397 The usual clocktable parameters are allowed here, but you cannot set
1398 the properties :name, :tstart, :tend, :block, and :scope - these will
1399 be overwritten to make sure the content accurately reflects the
1400 current display in the agenda."
1401 :group 'org-agenda-daily/weekly
1402 :type 'plist)
1404 (defcustom org-agenda-search-view-always-boolean nil
1405 "Non-nil means the search string is interpreted as individual parts.
1407 The search string for search view can either be interpreted as a phrase,
1408 or as a list of snippets that define a boolean search for a number of
1409 strings.
1411 When this is non-nil, the string will be split on whitespace, and each
1412 snippet will be searched individually, and all must match in order to
1413 select an entry. A snippet is then a single string of non-white
1414 characters, or a string in double quotes, or a regexp in {} braces.
1415 If a snippet is preceded by \"-\", the snippet must *not* match.
1416 \"+\" is syntactic sugar for positive selection. Each snippet may
1417 be found as a full word or a partial word, but see the variable
1418 `org-agenda-search-view-force-full-words'.
1420 When this is nil, search will look for the entire search phrase as one,
1421 with each space character matching any amount of whitespace, including
1422 line breaks.
1424 Even when this is nil, you can still switch to Boolean search dynamically
1425 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1426 is a regexp marked with braces like \"{abc}\", this will also switch to
1427 boolean search."
1428 :group 'org-agenda-search-view
1429 :version "24.1"
1430 :type 'boolean)
1432 (defvaralias 'org-agenda-search-view-search-words-only
1433 'org-agenda-search-view-always-boolean)
1435 (defcustom org-agenda-search-view-force-full-words nil
1436 "Non-nil means, search words must be matches as complete words.
1437 When nil, they may also match part of a word."
1438 :group 'org-agenda-search-view
1439 :version "24.1"
1440 :type 'boolean)
1442 (defcustom org-agenda-search-view-max-outline-level 0
1443 "Maximum outline level to display in search view.
1444 E.g. when this is set to 1, the search view will only
1445 show headlines of level 1. When set to 0, the default
1446 value, don't limit agenda view by outline level."
1447 :group 'org-agenda-search-view
1448 :version "26.1"
1449 :package-version '(Org . "8.3")
1450 :type 'integer)
1452 (defgroup org-agenda-time-grid nil
1453 "Options concerning the time grid in the Org Agenda."
1454 :tag "Org Agenda Time Grid"
1455 :group 'org-agenda)
1457 (defcustom org-agenda-search-headline-for-time t
1458 "Non-nil means search headline for a time-of-day.
1459 If the headline contains a time-of-day in one format or another, it will
1460 be used to sort the entry into the time sequence of items for a day.
1461 Some people have time stamps in the headline that refer to the creation
1462 time or so, and then this produces an unwanted side effect. If this is
1463 the case for your, use this variable to turn off searching the headline
1464 for a time."
1465 :group 'org-agenda-time-grid
1466 :type 'boolean)
1468 (defcustom org-agenda-use-time-grid t
1469 "Non-nil means show a time grid in the agenda schedule.
1470 A time grid is a set of lines for specific times (like every two hours between
1471 8:00 and 20:00). The items scheduled for a day at specific times are
1472 sorted in between these lines.
1473 For details about when the grid will be shown, and what it will look like, see
1474 the variable `org-agenda-time-grid'."
1475 :group 'org-agenda-time-grid
1476 :type 'boolean)
1478 (defcustom org-agenda-time-grid
1479 '((daily today require-timed)
1480 (800 1000 1200 1400 1600 1800 2000)
1481 "......"
1482 "----------------")
1484 "The settings for time grid for agenda display.
1485 This is a list of four items. The first item is again a list. It contains
1486 symbols specifying conditions when the grid should be displayed:
1488 daily if the agenda shows a single day
1489 weekly if the agenda shows an entire week
1490 today show grid on current date, independent of daily/weekly display
1491 require-timed show grid only if at least one item has a time specification
1492 remove-match skip grid times already present in an entry
1494 The second item is a list of integers, indicating the times that
1495 should have a grid line.
1497 The third item is a string which will be placed right after the
1498 times that have a grid line.
1500 The fourth item is a string placed after the grid times. This
1501 will align with agenda items"
1502 :group 'org-agenda-time-grid
1503 :type
1504 '(list
1505 (set :greedy t :tag "Grid Display Options"
1506 (const :tag "Show grid in single day agenda display" daily)
1507 (const :tag "Show grid in weekly agenda display" weekly)
1508 (const :tag "Always show grid for today" today)
1509 (const :tag "Show grid only if any timed entries are present"
1510 require-timed)
1511 (const :tag "Skip grid times already present in an entry"
1512 remove-match))
1513 (repeat :tag "Grid Times" (integer :tag "Time"))
1514 (string :tag "Grid String (after agenda times)")
1515 (string :tag "Grid String (aligns with agenda items)")))
1517 (defcustom org-agenda-show-current-time-in-grid t
1518 "Non-nil means show the current time in the time grid."
1519 :group 'org-agenda-time-grid
1520 :version "24.1"
1521 :type 'boolean)
1523 (defcustom org-agenda-current-time-string
1524 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1525 "The string for the current time marker in the agenda."
1526 :group 'org-agenda-time-grid
1527 :version "24.1"
1528 :type 'string)
1530 (defgroup org-agenda-sorting nil
1531 "Options concerning sorting in the Org Agenda."
1532 :tag "Org Agenda Sorting"
1533 :group 'org-agenda)
1535 (defcustom org-agenda-sorting-strategy
1536 '((agenda habit-down time-up priority-down category-keep)
1537 (todo priority-down category-keep)
1538 (tags priority-down category-keep)
1539 (search category-keep))
1540 "Sorting structure for the agenda items of a single day.
1541 This is a list of symbols which will be used in sequence to determine
1542 if an entry should be listed before another entry. The following
1543 symbols are recognized:
1545 time-up Put entries with time-of-day indications first, early first
1546 time-down Put entries with time-of-day indications first, late first
1547 timestamp-up Sort by any timestamp, early first
1548 timestamp-down Sort by any timestamp, late first
1549 scheduled-up Sort by scheduled timestamp, early first
1550 scheduled-down Sort by scheduled timestamp, late first
1551 deadline-up Sort by deadline timestamp, early first
1552 deadline-down Sort by deadline timestamp, late first
1553 ts-up Sort by active timestamp, early first
1554 ts-down Sort by active timestamp, late first
1555 tsia-up Sort by inactive timestamp, early first
1556 tsia-down Sort by inactive timestamp, late first
1557 category-keep Keep the default order of categories, corresponding to the
1558 sequence in `org-agenda-files'.
1559 category-up Sort alphabetically by category, A-Z.
1560 category-down Sort alphabetically by category, Z-A.
1561 tag-up Sort alphabetically by last tag, A-Z.
1562 tag-down Sort alphabetically by last tag, Z-A.
1563 priority-up Sort numerically by priority, high priority last.
1564 priority-down Sort numerically by priority, high priority first.
1565 todo-state-up Sort by todo state, tasks that are done last.
1566 todo-state-down Sort by todo state, tasks that are done first.
1567 effort-up Sort numerically by estimated effort, high effort last.
1568 effort-down Sort numerically by estimated effort, high effort first.
1569 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1570 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1571 habit-up Put entries that are habits first
1572 habit-down Put entries that are habits last
1573 alpha-up Sort headlines alphabetically
1574 alpha-down Sort headlines alphabetically, reversed
1576 The different possibilities will be tried in sequence, and testing stops
1577 if one comparison returns a \"not-equal\". For example, the default
1578 '(time-up category-keep priority-down)
1579 means: Pull out all entries having a specified time of day and sort them,
1580 in order to make a time schedule for the current day the first thing in the
1581 agenda listing for the day. Of the entries without a time indication, keep
1582 the grouped in categories, don't sort the categories, but keep them in
1583 the sequence given in `org-agenda-files'. Within each category sort by
1584 priority.
1586 Leaving out `category-keep' would mean that items will be sorted across
1587 categories by priority.
1589 Instead of a single list, this can also be a set of list for specific
1590 contents, with a context symbol in the car of the list, any of
1591 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1593 Custom commands can bind this variable in the options section."
1594 :group 'org-agenda-sorting
1595 :type `(choice
1596 (repeat :tag "General" ,org-sorting-choice)
1597 (list :tag "Individually"
1598 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1599 (repeat ,org-sorting-choice))
1600 (cons (const :tag "Strategy for TODO lists" todo)
1601 (repeat ,org-sorting-choice))
1602 (cons (const :tag "Strategy for Tags matches" tags)
1603 (repeat ,org-sorting-choice))
1604 (cons (const :tag "Strategy for search matches" search)
1605 (repeat ,org-sorting-choice)))))
1607 (defcustom org-agenda-cmp-user-defined nil
1608 "A function to define the comparison `user-defined'.
1609 This function must receive two arguments, agenda entry a and b.
1610 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1611 the user comparison, return nil.
1612 When this is defined, you can make `user-defined-up' and `user-defined-down'
1613 part of an agenda sorting strategy."
1614 :group 'org-agenda-sorting
1615 :type 'symbol)
1617 (defcustom org-sort-agenda-notime-is-late t
1618 "Non-nil means items without time are considered late.
1619 This is only relevant for sorting. When t, items which have no explicit
1620 time like 15:30 will be considered as 99:01, i.e. later than any items which
1621 do have a time. When nil, the default time is before 0:00. You can use this
1622 option to decide if the schedule for today should come before or after timeless
1623 agenda entries."
1624 :group 'org-agenda-sorting
1625 :type 'boolean)
1627 (defcustom org-sort-agenda-noeffort-is-high t
1628 "Non-nil means items without effort estimate are sorted as high effort.
1629 This also applies when filtering an agenda view with respect to the
1630 < or > effort operator. Then, tasks with no effort defined will be treated
1631 as tasks with high effort.
1632 When nil, such items are sorted as 0 minutes effort."
1633 :group 'org-agenda-sorting
1634 :type 'boolean)
1636 (defgroup org-agenda-line-format nil
1637 "Options concerning the entry prefix in the Org agenda display."
1638 :tag "Org Agenda Line Format"
1639 :group 'org-agenda)
1641 (defcustom org-agenda-prefix-format
1642 '((agenda . " %i %-12:c%?-12t% s")
1643 (todo . " %i %-12:c")
1644 (tags . " %i %-12:c")
1645 (search . " %i %-12:c"))
1646 "Format specifications for the prefix of items in the agenda views.
1647 An alist with five entries, each for the different agenda types. The
1648 keys of the sublists are `agenda', `todo', `search' and `tags'.
1649 The values are format strings.
1651 This format works similar to a printf format, with the following meaning:
1653 %c the category of the item, \"Diary\" for entries from the diary,
1654 or as given by the CATEGORY keyword or derived from the file name
1655 %e the effort required by the item
1656 %l the level of the item (insert X space(s) if item is of level X)
1657 %i the icon category of the item, see `org-agenda-category-icon-alist'
1658 %T the last tag of the item (ignore inherited tags, which come first)
1659 %t the HH:MM time-of-day specification if one applies to the entry
1660 %s Scheduling/Deadline information, a short string
1661 %b show breadcrumbs, i.e., the names of the higher levels
1662 %(expression) Eval EXPRESSION and replace the control string
1663 by the result
1665 All specifiers work basically like the standard `%s' of printf, but may
1666 contain two additional characters: a question mark just after the `%'
1667 and a whitespace/punctuation character just before the final letter.
1669 If the first character after `%' is a question mark, the entire field
1670 will only be included if the corresponding value applies to the current
1671 entry. This is useful for fields which should have fixed width when
1672 present, but zero width when absent. For example, \"%?-12t\" will
1673 result in a 12 character time field if a time of the day is specified,
1674 but will completely disappear in entries which do not contain a time.
1676 If there is punctuation or whitespace character just before the
1677 final format letter, this character will be appended to the field
1678 value if the value is not empty. For example, the format
1679 \"%-12:c\" leads to \"Diary: \" if the category is \"Diary\". If
1680 the category is empty, no additional colon is inserted.
1682 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1683 which means:
1685 - Indent the line with two space characters
1686 - Give the category a 12 chars wide field, padded with whitespace on
1687 the right (because of `-'). Append a colon if there is a category
1688 (because of `:').
1689 - If there is a time-of-day, put it into a 12 chars wide field. If no
1690 time, don't put in an empty field, just skip it (because of '?').
1691 - Finally, put the scheduling information.
1693 See also the variables `org-agenda-remove-times-when-in-prefix' and
1694 `org-agenda-remove-tags'.
1696 Custom commands can set this variable in the options section."
1697 :type '(choice
1698 (string :tag "General format")
1699 (list :greedy t :tag "View dependent"
1700 (cons (const agenda) (string :tag "Format"))
1701 (cons (const todo) (string :tag "Format"))
1702 (cons (const tags) (string :tag "Format"))
1703 (cons (const search) (string :tag "Format"))))
1704 :group 'org-agenda-line-format
1705 :version "26.1"
1706 :package-version '(Org . "9.1"))
1708 (defvar org-prefix-format-compiled nil
1709 "The compiled prefix format and associated variables.
1710 This is a list where first element is a list of variable bindings, and second
1711 element is the compiled format expression. See the variable
1712 `org-agenda-prefix-format'.")
1714 (defcustom org-agenda-todo-keyword-format "%-1s"
1715 "Format for the TODO keyword in agenda lines.
1716 Set this to something like \"%-12s\" if you want all TODO keywords
1717 to occupy a fixed space in the agenda display."
1718 :group 'org-agenda-line-format
1719 :type 'string)
1721 (defcustom org-agenda-diary-sexp-prefix nil
1722 "A regexp that matches part of a diary sexp entry
1723 which should be treated as scheduling/deadline information in
1724 `org-agenda'.
1726 For example, you can use this to extract the `diary-remind-message' from
1727 `diary-remind' entries."
1728 :group 'org-agenda-line-format
1729 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1731 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1732 "Text preceding timerange entries in the agenda view.
1733 This is a list with two strings. The first applies when the range
1734 is entirely on one day. The second applies if the range spans several days.
1735 The strings may have two \"%d\" format specifiers which will be filled
1736 with the sequence number of the days, and the total number of days in the
1737 range, respectively."
1738 :group 'org-agenda-line-format
1739 :type '(list
1740 (string :tag "Deadline today ")
1741 (choice :tag "Deadline relative"
1742 (string :tag "Format string")
1743 (function))))
1745 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1746 "Text preceding scheduled items in the agenda view.
1747 This is a list with two strings. The first applies when the item is
1748 scheduled on the current day. The second applies when it has been scheduled
1749 previously, it may contain a %d indicating that this is the nth time that
1750 this item is scheduled, due to automatic rescheduling of unfinished items
1751 for the following day. So this number is one larger than the number of days
1752 that passed since this item was scheduled first."
1753 :group 'org-agenda-line-format
1754 :version "24.4"
1755 :package-version '(Org . "8.0")
1756 :type '(list
1757 (string :tag "Scheduled today ")
1758 (string :tag "Scheduled previously")))
1760 (defcustom org-agenda-inactive-leader "["
1761 "Text preceding item pulled into the agenda by inactive time stamps.
1762 These entries are added to the agenda when pressing \"[\"."
1763 :group 'org-agenda-line-format
1764 :version "24.1"
1765 :type 'string)
1767 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: " "%2d d. ago: ")
1768 "Text preceding deadline items in the agenda view.
1769 This is a list with three strings. The first applies when the item has its
1770 deadline on the current day. The second applies when the deadline is in the
1771 future, the third one when it is in the past. The strings may contain %d
1772 to capture the number of days."
1773 :group 'org-agenda-line-format
1774 :version "24.4"
1775 :package-version '(Org . "8.0")
1776 :type '(list
1777 (string :tag "Deadline today ")
1778 (string :tag "Deadline in the future ")
1779 (string :tag "Deadline in the past ")))
1781 (defcustom org-agenda-remove-times-when-in-prefix t
1782 "Non-nil means remove duplicate time specifications in agenda items.
1783 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1784 time-of-day specification in a headline or diary entry is extracted and
1785 placed into the prefix. If this option is non-nil, the original specification
1786 \(a timestamp or -range, or just a plain time(range) specification like
1787 11:30-4pm) will be removed for agenda display. This makes the agenda less
1788 cluttered.
1789 The option can be t or nil. It may also be the symbol `beg', indicating
1790 that the time should only be removed when it is located at the beginning of
1791 the headline/diary entry."
1792 :group 'org-agenda-line-format
1793 :type '(choice
1794 (const :tag "Always" t)
1795 (const :tag "Never" nil)
1796 (const :tag "When at beginning of entry" beg)))
1798 (defcustom org-agenda-remove-timeranges-from-blocks nil
1799 "Non-nil means remove time ranges specifications in agenda
1800 items that span on several days."
1801 :group 'org-agenda-line-format
1802 :version "24.1"
1803 :type 'boolean)
1805 (defcustom org-agenda-default-appointment-duration nil
1806 "Default duration for appointments that only have a starting time.
1807 When nil, no duration is specified in such cases.
1808 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1809 :group 'org-agenda-line-format
1810 :type '(choice
1811 (integer :tag "Minutes")
1812 (const :tag "No default duration")))
1814 (defcustom org-agenda-show-inherited-tags t
1815 "Non-nil means show inherited tags in each agenda line.
1817 When this option is set to `always', it takes precedence over
1818 `org-agenda-use-tag-inheritance' and inherited tags are shown
1819 in every agenda.
1821 When this option is set to t (the default), inherited tags are
1822 shown when they are available, i.e. when the value of
1823 `org-agenda-use-tag-inheritance' enables tag inheritance for the
1824 given agenda type.
1826 This can be set to a list of agenda types in which the agenda
1827 must display the inherited tags. Available types are `todo',
1828 `agenda' and `search'.
1830 When set to nil, never show inherited tags in agenda lines."
1831 :group 'org-agenda-line-format
1832 :group 'org-agenda
1833 :version "24.3"
1834 :type '(choice
1835 (const :tag "Show inherited tags when available" t)
1836 (const :tag "Always show inherited tags" always)
1837 (repeat :tag "Show inherited tags only in selected agenda types"
1838 (symbol :tag "Agenda type"))))
1840 (defcustom org-agenda-use-tag-inheritance '(todo search agenda)
1841 "List of agenda view types where to use tag inheritance.
1843 In tags/tags-todo/tags-tree agenda views, tag inheritance is
1844 controlled by `org-use-tag-inheritance'. In other agenda types,
1845 `org-use-tag-inheritance' is not used for the selection of the
1846 agenda entries. Still, you may want the agenda to be aware of
1847 the inherited tags anyway, e.g. for later tag filtering.
1849 Allowed value are `todo', `search' and `agenda'.
1851 This variable has no effect if `org-agenda-show-inherited-tags'
1852 is set to `always'. In that case, the agenda is aware of those
1853 tags.
1855 The default value sets tags in every agenda type. Setting this
1856 option to nil will speed up non-tags agenda view a lot."
1857 :group 'org-agenda
1858 :version "26.1"
1859 :package-version '(Org . "9.1")
1860 :type '(choice
1861 (const :tag "Use tag inheritance in all agenda types" t)
1862 (repeat :tag "Use tag inheritance in selected agenda types"
1863 (symbol :tag "Agenda type"))))
1865 (defcustom org-agenda-hide-tags-regexp nil
1866 "Regular expression used to filter away specific tags in agenda views.
1867 This means that these tags will be present, but not be shown in the agenda
1868 line. Secondary filtering will still work on the hidden tags.
1869 Nil means don't hide any tags."
1870 :group 'org-agenda-line-format
1871 :type '(choice
1872 (const :tag "Hide none" nil)
1873 (string :tag "Regexp ")))
1875 (defcustom org-agenda-remove-tags nil
1876 "Non-nil means remove the tags from the headline copy in the agenda.
1877 When this is the symbol `prefix', only remove tags when
1878 `org-agenda-prefix-format' contains a `%T' specifier."
1879 :group 'org-agenda-line-format
1880 :type '(choice
1881 (const :tag "Always" t)
1882 (const :tag "Never" nil)
1883 (const :tag "When prefix format contains %T" prefix)))
1885 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1886 'org-agenda-remove-tags)
1888 (defcustom org-agenda-tags-column 'auto
1889 "Shift tags in agenda items to this column.
1890 If set to `auto', tags will be automatically aligned to the right
1891 edge of the window.
1893 If set to a positive number, tags will be left-aligned to that
1894 column. If set to a negative number, tags will be right-aligned
1895 to that column. For example, -80 works well for a normal 80
1896 character screen."
1897 :group 'org-agenda-line-format
1898 :type '(choice
1899 (const :tag "Automatically align to right edge of window" auto)
1900 (integer :tag "Specific column" -80))
1901 :package-version '(Org . "9.1")
1902 :version "26.1")
1904 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column)
1906 (defcustom org-agenda-fontify-priorities 'cookies
1907 "Non-nil means highlight low and high priorities in agenda.
1908 When t, the highest priority entries are bold, lowest priority italic.
1909 However, settings in `org-priority-faces' will overrule these faces.
1910 When this variable is the symbol `cookies', only fontify the
1911 cookies, not the entire task.
1912 This may also be an association list of priority faces, whose
1913 keys are the character values of `org-highest-priority',
1914 `org-default-priority', and `org-lowest-priority' (the default values
1915 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1916 color as a string, or a list like `(:background \"Red\")'.
1917 If it is a color, the variable `org-faces-easy-properties'
1918 determines if it is a foreground or a background color."
1919 :group 'org-agenda-line-format
1920 :type '(choice
1921 (const :tag "Never" nil)
1922 (const :tag "Defaults" t)
1923 (const :tag "Cookies only" cookies)
1924 (repeat :tag "Specify"
1925 (list (character :tag "Priority" :value ?A)
1926 (choice :tag "Face "
1927 (string :tag "Color")
1928 (sexp :tag "Face"))))))
1930 (defcustom org-agenda-day-face-function nil
1931 "Function called to determine what face should be used to display a day.
1932 The only argument passed to that function is the day. It should
1933 returns a face, or nil if does not want to specify a face and let
1934 the normal rules apply."
1935 :group 'org-agenda-line-format
1936 :version "24.1"
1937 :type '(choice (const nil) (function)))
1939 (defcustom org-agenda-category-icon-alist nil
1940 "Alist of category icon to be displayed in agenda views.
1942 Each entry should have the following format:
1944 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1946 Where CATEGORY-REGEXP is a regexp matching the categories where
1947 the icon should be displayed.
1948 FILE-OR-DATA either a file path or a string containing image data.
1950 The other fields can be omitted safely if not needed:
1951 TYPE indicates the image type.
1952 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1953 image data.
1954 PROPS are additional image attributes to assign to the image,
1955 like, e.g. `:ascent center'.
1957 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1959 If you want to set the display properties yourself, just put a
1960 list as second element:
1962 (CATEGORY-REGEXP (MY PROPERTY LIST))
1964 For example, to display a 16px horizontal space for Emacs
1965 category, you can use:
1967 (\"Emacs\" \\='(space . (:width (16))))"
1968 :group 'org-agenda-line-format
1969 :version "24.1"
1970 :type '(alist :key-type (string :tag "Regexp matching category")
1971 :value-type (choice (list :tag "Icon"
1972 (string :tag "File or data")
1973 (symbol :tag "Type")
1974 (boolean :tag "Data?")
1975 (repeat :tag "Extra image properties" :inline t symbol))
1976 (list :tag "Display properties" sexp))))
1978 (defgroup org-agenda-column-view nil
1979 "Options concerning column view in the agenda."
1980 :tag "Org Agenda Column View"
1981 :group 'org-agenda)
1983 (defcustom org-agenda-view-columns-initially nil
1984 "When non-nil, switch to columns view right after creating the agenda."
1985 :group 'org-agenda-column-view
1986 :type 'boolean
1987 :version "26.1"
1988 :package-version '(Org . "9.0")
1989 :safe #'booleanp)
1991 (defcustom org-agenda-columns-show-summaries t
1992 "Non-nil means show summaries for columns displayed in the agenda view."
1993 :group 'org-agenda-column-view
1994 :type 'boolean)
1996 (defcustom org-agenda-columns-compute-summary-properties t
1997 "Non-nil means recompute all summary properties before column view.
1998 When column view in the agenda is listing properties that have a summary
1999 operator, it can go to all relevant buffers and recompute the summaries
2000 there. This can mean overhead for the agenda column view, but is necessary
2001 to have thing up to date.
2002 As a special case, a CLOCKSUM property also makes sure that the clock
2003 computations are current."
2004 :group 'org-agenda-column-view
2005 :type 'boolean)
2007 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
2008 "Non-nil means the duration of an appointment will add to day effort.
2009 The property to which appointment durations will be added is the one given
2010 in the option `org-effort-property'. If an appointment does not have
2011 an end time, `org-agenda-default-appointment-duration' will be used. If that
2012 is not set, an appointment without end time will not contribute to the time
2013 estimate."
2014 :group 'org-agenda-column-view
2015 :type 'boolean)
2017 (defcustom org-agenda-auto-exclude-function nil
2018 "A function called with a tag to decide if it is filtered on \
2019 \\<org-agenda-mode-map>`\\[org-agenda-filter-by-tag] RET'.
2020 The sole argument to the function, which is called once for each
2021 possible tag, is a string giving the name of the tag. The
2022 function should return either nil if the tag should be included
2023 as normal, or \"-<TAG>\" to exclude the tag.
2024 Note that for the purpose of tag filtering, only the lower-case version of
2025 all tags will be considered, so that this function will only ever see
2026 the lower-case version of all tags."
2027 :group 'org-agenda
2028 :type '(choice (const nil) (function)))
2030 (defcustom org-agenda-bulk-custom-functions nil
2031 "Alist of characters and custom functions for bulk actions.
2032 For example, this value makes those two functions available:
2034 \\='((?R set-category)
2035 (?C bulk-cut))
2037 With selected entries in an agenda buffer, `B R' will call
2038 the custom function `set-category' on the selected entries.
2039 Note that functions in this alist don't need to be quoted."
2040 :type '(alist :key-type character :value-type (group function))
2041 :version "24.1"
2042 :group 'org-agenda)
2044 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
2045 "Execute BODY with point at location given by `org-hd-marker' property.
2046 If STRING is non-nil, the text property will be fetched from position 0
2047 in that string. If STRING is nil, it will be fetched from the beginning
2048 of the current line."
2049 (org-with-gensyms (marker)
2050 `(let ((,marker (get-text-property (if ,string 0 (point-at-bol))
2051 'org-hd-marker ,string)))
2052 (with-current-buffer (marker-buffer ,marker)
2053 (save-excursion
2054 (goto-char ,marker)
2055 ,@body)))))
2056 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
2058 (defun org-add-agenda-custom-command (entry)
2059 "Replace or add a command in `org-agenda-custom-commands'.
2060 This is mostly for hacking and trying a new command - once the command
2061 works you probably want to add it to `org-agenda-custom-commands' for good."
2062 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
2063 (if ass
2064 (setcdr ass (cdr entry))
2065 (push entry org-agenda-custom-commands))))
2067 (defmacro org-agenda--insert-overriding-header (default)
2068 "Insert header into agenda view.
2069 The inserted header depends on `org-agenda-overriding-header'.
2070 If the empty string, don't insert a header. If any other string,
2071 insert it as a header. If nil, insert DEFAULT, which should
2072 evaluate to a string."
2073 (declare (debug (form)) (indent defun))
2074 `(cond
2075 ((not org-agenda-overriding-header) (insert ,default))
2076 ((equal org-agenda-overriding-header "") nil)
2077 ((stringp org-agenda-overriding-header)
2078 (insert (propertize org-agenda-overriding-header
2079 'face 'org-agenda-structure)
2080 "\n"))
2081 (t (user-error "Invalid value for `org-agenda-overriding-header': %S"
2082 org-agenda-overriding-header))))
2084 ;;; Define the org-agenda-mode
2086 (defvar org-agenda-mode-map (make-sparse-keymap)
2087 "Keymap for `org-agenda-mode'.")
2088 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map)
2090 (defvar org-agenda-menu) ; defined later in this file.
2091 (defvar org-agenda-restrict nil) ; defined later in this file.
2092 (defvar org-agenda-follow-mode nil)
2093 (defvar org-agenda-entry-text-mode nil)
2094 (defvar org-agenda-clockreport-mode nil)
2095 (defvar org-agenda-show-log nil
2096 "When non-nil, show the log in the agenda.
2097 Do not set this directly; instead use
2098 `org-agenda-start-with-log-mode', which see.")
2099 (defvar org-agenda-redo-command nil)
2100 (defvar org-agenda-query-string nil)
2101 (defvar org-agenda-mode-hook nil
2102 "Hook run after `org-agenda-mode' is turned on.
2103 The buffer is still writable when this hook is called.")
2104 (defvar org-agenda-type nil)
2105 (defvar org-agenda-force-single-file nil)
2106 (defvar org-agenda-bulk-marked-entries nil
2107 "List of markers that refer to marked entries in the agenda.")
2108 (defvar org-agenda-current-date nil
2109 "Active date when building the agenda.")
2111 ;;; Multiple agenda buffers support
2113 (defcustom org-agenda-sticky nil
2114 "Non-nil means agenda q key will bury agenda buffers.
2115 Agenda commands will then show existing buffer instead of generating new ones.
2116 When nil, `q' will kill the single agenda buffer."
2117 :group 'org-agenda
2118 :version "24.3"
2119 :type 'boolean)
2122 ;;;###autoload
2123 (defun org-toggle-sticky-agenda (&optional arg)
2124 "Toggle `org-agenda-sticky'."
2125 (interactive "P")
2126 (let ((new-value (if arg
2127 (> (prefix-numeric-value arg) 0)
2128 (not org-agenda-sticky))))
2129 (if (equal new-value org-agenda-sticky)
2130 (and (called-interactively-p 'interactive)
2131 (message "Sticky agenda was already %s"
2132 (if org-agenda-sticky "enabled" "disabled")))
2133 (setq org-agenda-sticky new-value)
2134 (org-agenda-kill-all-agenda-buffers)
2135 (and (called-interactively-p 'interactive)
2136 (message "Sticky agenda %s"
2137 (if org-agenda-sticky "enabled" "disabled"))))))
2139 (defvar org-agenda-buffer nil
2140 "Agenda buffer currently being generated.")
2142 (defvar org-agenda-last-prefix-arg nil)
2143 (defvar org-agenda-this-buffer-name nil)
2144 (defvar org-agenda-doing-sticky-redo nil)
2145 (defvar org-agenda-this-buffer-is-sticky nil)
2146 (defvar org-agenda-last-indirect-buffer nil
2147 "Last buffer loaded by `org-agenda-tree-to-indirect-buffer'.")
2148 (defvar org-agenda-lprops nil)
2150 (defconst org-agenda-local-vars
2151 '(org-agenda-this-buffer-name
2152 org-agenda-undo-list
2153 org-agenda-pending-undo-list
2154 org-agenda-follow-mode
2155 org-agenda-entry-text-mode
2156 org-agenda-clockreport-mode
2157 org-agenda-show-log
2158 org-agenda-redo-command
2159 org-agenda-query-string
2160 org-agenda-type
2161 org-agenda-bulk-marked-entries
2162 org-agenda-undo-has-started-in
2163 org-agenda-info
2164 org-agenda-pre-window-conf
2165 org-agenda-columns-active
2166 org-agenda-tag-filter
2167 org-agenda-category-filter
2168 org-agenda-top-headline-filter
2169 org-agenda-regexp-filter
2170 org-agenda-effort-filter
2171 org-agenda-markers
2172 org-agenda-last-search-view-search-was-boolean
2173 org-agenda-last-indirect-buffer
2174 org-agenda-filtered-by-category
2175 org-agenda-filter-form
2176 org-agenda-cycle-counter
2177 org-agenda-lprops
2178 org-agenda-last-prefix-arg)
2179 "Variables that must be local in agenda buffers to allow multiple buffers.")
2181 (defun org-agenda-mode ()
2182 "Mode for time-sorted view on action items in Org files.
2184 The following commands are available:
2186 \\{org-agenda-mode-map}"
2187 (interactive)
2188 (cond (org-agenda-doing-sticky-redo
2189 ;; Refreshing sticky agenda-buffer
2191 ;; Preserve the value of `org-agenda-local-vars' variables,
2192 ;; while letting `kill-all-local-variables' kill the rest
2193 (let ((save (buffer-local-variables)))
2194 (kill-all-local-variables)
2195 (mapc #'make-local-variable org-agenda-local-vars)
2196 (dolist (elem save)
2197 (pcase elem
2198 (`(,var . ,val) ;ignore unbound variables
2199 (when (and val (memq var org-agenda-local-vars))
2200 (set var val))))))
2201 (setq-local org-agenda-this-buffer-is-sticky t))
2202 (org-agenda-sticky
2203 ;; Creating a sticky Agenda buffer for the first time
2204 (kill-all-local-variables)
2205 (mapc 'make-local-variable org-agenda-local-vars)
2206 (setq-local org-agenda-this-buffer-is-sticky t))
2208 ;; Creating a non-sticky agenda buffer
2209 (kill-all-local-variables)
2210 (setq-local org-agenda-this-buffer-is-sticky nil)))
2211 (setq org-agenda-undo-list nil
2212 org-agenda-pending-undo-list nil
2213 org-agenda-bulk-marked-entries nil)
2214 (setq major-mode 'org-agenda-mode)
2215 ;; Keep global-font-lock-mode from turning on font-lock-mode
2216 (setq-local font-lock-global-modes (list 'not major-mode))
2217 (setq mode-name "Org-Agenda")
2218 (setq indent-tabs-mode nil)
2219 (use-local-map org-agenda-mode-map)
2220 (easy-menu-add org-agenda-menu)
2221 (if org-startup-truncated (setq truncate-lines t))
2222 (setq-local line-move-visual nil)
2223 (add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
2224 (add-hook 'pre-command-hook 'org-unhighlight nil 'local)
2225 ;; Make sure properties are removed when copying text
2226 (add-hook 'filter-buffer-substring-functions
2227 (lambda (fun start end delete)
2228 (substring-no-properties (funcall fun start end delete)))
2229 nil t)
2230 (unless org-agenda-keep-modes
2231 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
2232 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode))
2233 (setq org-agenda-show-log org-agenda-start-with-log-mode)
2234 (setq org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode)
2235 (add-to-invisibility-spec '(org-filtered))
2236 (add-to-invisibility-spec '(org-link))
2237 (easy-menu-change
2238 '("Agenda") "Agenda Files"
2239 (append
2240 (list
2241 (vector
2242 (if (get 'org-agenda-files 'org-restrict)
2243 "Restricted to single file"
2244 "Edit File List")
2245 '(org-edit-agenda-file-list)
2246 (not (get 'org-agenda-files 'org-restrict)))
2247 "--")
2248 (mapcar 'org-file-menu-entry (org-agenda-files))))
2249 (org-agenda-set-mode-name)
2250 (apply
2251 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2252 (list 'org-agenda-mode-hook)))
2254 (substitute-key-definition 'undo 'org-agenda-undo
2255 org-agenda-mode-map global-map)
2256 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2257 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2258 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2259 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2260 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2261 (org-defkey org-agenda-mode-map [(meta down)] 'org-agenda-drag-line-forward)
2262 (org-defkey org-agenda-mode-map [(meta up)] 'org-agenda-drag-line-backward)
2263 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2264 (org-defkey org-agenda-mode-map "\M-m" 'org-agenda-bulk-toggle)
2265 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2266 (org-defkey org-agenda-mode-map "\M-*" 'org-agenda-bulk-toggle-all)
2267 (org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
2268 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2269 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2270 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2271 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2272 (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2273 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2274 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2275 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2276 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2277 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2278 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2279 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2280 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2281 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2282 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2283 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2284 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2285 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2286 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2287 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2288 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2289 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2290 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2291 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2292 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2293 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2294 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2295 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2296 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2297 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2298 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2299 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2300 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2301 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2302 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2303 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2304 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2305 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2307 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2308 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2309 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2310 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2311 (while l (org-defkey org-agenda-mode-map
2312 (int-to-string (pop l)) 'digit-argument)))
2314 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2315 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2316 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2317 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2318 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2319 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2320 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2321 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2322 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2323 (org-defkey org-agenda-mode-map "g" 'org-agenda-redo-all)
2324 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2325 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2326 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2327 'org-clock-modify-effort-estimate)
2328 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2329 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2330 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2331 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2332 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2333 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2334 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2335 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2336 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2337 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2338 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2339 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2340 (substitute-key-definition 'next-line 'org-agenda-next-line
2341 org-agenda-mode-map global-map)
2342 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2343 org-agenda-mode-map global-map)
2344 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2345 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2346 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2347 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2348 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2349 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2350 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2351 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2352 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2353 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2354 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2355 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2356 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2357 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2358 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2359 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2360 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2361 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2362 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2363 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2364 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2365 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2366 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2367 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2368 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2369 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2370 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2371 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2372 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2373 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2374 (org-defkey org-agenda-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock-from-agenda)
2376 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2377 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2378 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2379 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2380 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2381 (org-defkey org-agenda-mode-map "_" 'org-agenda-filter-by-effort)
2382 (org-defkey org-agenda-mode-map "=" 'org-agenda-filter-by-regexp)
2383 (org-defkey org-agenda-mode-map "|" 'org-agenda-filter-remove-all)
2384 (org-defkey org-agenda-mode-map "~" 'org-agenda-limit-interactively)
2385 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2386 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-headline)
2387 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2388 (org-defkey org-agenda-mode-map "\C-c\C-x_" 'org-timer-stop)
2389 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2390 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2391 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2393 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2394 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2396 (define-key org-agenda-mode-map [remap forward-paragraph] 'org-agenda-forward-block)
2397 (define-key org-agenda-mode-map [remap backward-paragraph] 'org-agenda-backward-block)
2399 (when org-agenda-mouse-1-follows-link
2400 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2401 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2402 '("Agenda"
2403 ("Agenda Files")
2404 "--"
2405 ("Agenda Dates"
2406 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda)]
2407 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2408 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2409 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2410 "--"
2411 ("View"
2412 ["Day View" org-agenda-day-view
2413 :active (org-agenda-check-type nil 'agenda)
2414 :style radio :selected (eq org-agenda-current-span 'day)
2415 :keys "v d (or just d)"]
2416 ["Week View" org-agenda-week-view
2417 :active (org-agenda-check-type nil 'agenda)
2418 :style radio :selected (eq org-agenda-current-span 'week)
2419 :keys "v w"]
2420 ["Fortnight View" org-agenda-fortnight-view
2421 :active (org-agenda-check-type nil 'agenda)
2422 :style radio :selected (eq org-agenda-current-span 'fortnight)
2423 :keys "v t"]
2424 ["Month View" org-agenda-month-view
2425 :active (org-agenda-check-type nil 'agenda)
2426 :style radio :selected (eq org-agenda-current-span 'month)
2427 :keys "v m"]
2428 ["Year View" org-agenda-year-view
2429 :active (org-agenda-check-type nil 'agenda)
2430 :style radio :selected (eq org-agenda-current-span 'year)
2431 :keys "v y"]
2432 "--"
2433 ["Include Diary" org-agenda-toggle-diary
2434 :style toggle :selected org-agenda-include-diary
2435 :active (org-agenda-check-type nil 'agenda)]
2436 ["Include Deadlines" org-agenda-toggle-deadlines
2437 :style toggle :selected org-agenda-include-deadlines
2438 :active (org-agenda-check-type nil 'agenda)]
2439 ["Use Time Grid" org-agenda-toggle-time-grid
2440 :style toggle :selected org-agenda-use-time-grid
2441 :active (org-agenda-check-type nil 'agenda)]
2442 "--"
2443 ["Show clock report" org-agenda-clockreport-mode
2444 :style toggle :selected org-agenda-clockreport-mode
2445 :active (org-agenda-check-type nil 'agenda)]
2446 ["Show some entry text" org-agenda-entry-text-mode
2447 :style toggle :selected org-agenda-entry-text-mode
2448 :active t]
2449 "--"
2450 ["Show Logbook entries" org-agenda-log-mode
2451 :style toggle :selected org-agenda-show-log
2452 :active (org-agenda-check-type nil 'agenda)
2453 :keys "v l (or just l)"]
2454 ["Include archived trees" org-agenda-archives-mode
2455 :style toggle :selected org-agenda-archives-mode :active t
2456 :keys "v a"]
2457 ["Include archive files" (org-agenda-archives-mode t)
2458 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2459 :keys "v A"]
2460 "--"
2461 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2462 ["Write view to file" org-agenda-write t]
2463 ["Rebuild buffer" org-agenda-redo t]
2464 ["Save all Org buffers" org-save-all-org-buffers t]
2465 "--"
2466 ["Show original entry" org-agenda-show t]
2467 ["Go To (other window)" org-agenda-goto t]
2468 ["Go To (this window)" org-agenda-switch-to t]
2469 ["Capture with cursor date" org-agenda-capture t]
2470 ["Follow Mode" org-agenda-follow-mode
2471 :style toggle :selected org-agenda-follow-mode :active t]
2472 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2473 "--"
2474 ("TODO"
2475 ["Cycle TODO" org-agenda-todo t]
2476 ["Next TODO set" org-agenda-todo-nextset t]
2477 ["Previous TODO set" org-agenda-todo-previousset t]
2478 ["Add note" org-agenda-add-note t])
2479 ("Archive/Refile/Delete"
2480 ["Archive default" org-agenda-archive-default t]
2481 ["Archive default" org-agenda-archive-default-with-confirmation t]
2482 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2483 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2484 ["Archive subtree" org-agenda-archive t]
2485 "--"
2486 ["Refile" org-agenda-refile t]
2487 "--"
2488 ["Delete subtree" org-agenda-kill t])
2489 ("Bulk action"
2490 ["Mark entry" org-agenda-bulk-mark t]
2491 ["Mark all" org-agenda-bulk-mark-all t]
2492 ["Unmark entry" org-agenda-bulk-unmark t]
2493 ["Unmark all" org-agenda-bulk-unmark-all :active t :keys "U"]
2494 ["Toggle mark" org-agenda-bulk-toggle t]
2495 ["Toggle all" org-agenda-bulk-toggle-all t]
2496 ["Mark regexp" org-agenda-bulk-mark-regexp t])
2497 ["Act on all marked" org-agenda-bulk-action t]
2498 "--"
2499 ("Tags and Properties"
2500 ["Show all Tags" org-agenda-show-tags t]
2501 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2502 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2503 "--"
2504 ["Column View" org-columns t])
2505 ("Deadline/Schedule"
2506 ["Schedule" org-agenda-schedule t]
2507 ["Set Deadline" org-agenda-deadline t]
2508 "--"
2509 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda)]
2510 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda)]
2511 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda) :keys "C-u S-right"]
2512 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda) :keys "C-u S-left"]
2513 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda) :keys "C-u C-u S-right"]
2514 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda) :keys "C-u C-u S-left"]
2515 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda)])
2516 ("Clock and Effort"
2517 ["Clock in" org-agenda-clock-in t]
2518 ["Clock out" org-agenda-clock-out t]
2519 ["Clock cancel" org-agenda-clock-cancel t]
2520 ["Goto running clock" org-clock-goto t]
2521 "--"
2522 ["Set Effort" org-agenda-set-effort t]
2523 ["Change clocked effort" org-clock-modify-effort-estimate
2524 (org-clock-is-active)])
2525 ("Priority"
2526 ["Set Priority" org-agenda-priority t]
2527 ["Increase Priority" org-agenda-priority-up t]
2528 ["Decrease Priority" org-agenda-priority-down t]
2529 ["Show Priority" org-show-priority t])
2530 ("Calendar/Diary"
2531 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda)]
2532 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda)]
2533 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda)]
2534 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda)]
2535 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda)]
2536 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda)]
2537 "--"
2538 ["Create iCalendar File" org-icalendar-combine-agenda-files t])
2539 "--"
2540 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2541 "--"
2542 ("MobileOrg"
2543 ["Push Files and Views" org-mobile-push t]
2544 ["Get Captured and Flagged" org-mobile-pull t]
2545 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "\\[org-agenda] ?"]
2546 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2547 "--"
2548 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2549 "--"
2550 ["Quit" org-agenda-quit t]
2551 ["Exit and Release Buffers" org-agenda-exit t]
2554 ;;; Agenda undo
2556 (defvar org-agenda-allow-remote-undo t
2557 "Non-nil means allow remote undo from the agenda buffer.")
2558 (defvar org-agenda-undo-has-started-in nil
2559 "Buffers that have already seen `undo-start' in the current undo sequence.")
2561 (defun org-agenda-undo ()
2562 "Undo a remote editing step in the agenda.
2563 This undoes changes both in the agenda buffer and in the remote buffer
2564 that have been changed along."
2565 (interactive)
2566 (or org-agenda-allow-remote-undo
2567 (user-error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2568 (if (not (eq this-command last-command))
2569 (setq org-agenda-undo-has-started-in nil
2570 org-agenda-pending-undo-list org-agenda-undo-list))
2571 (if (not org-agenda-pending-undo-list)
2572 (user-error "No further undo information"))
2573 (let* ((entry (pop org-agenda-pending-undo-list))
2574 buf line cmd rembuf)
2575 (setq cmd (pop entry) line (pop entry))
2576 (setq rembuf (nth 2 entry))
2577 (org-with-remote-undo rembuf
2578 (while (bufferp (setq buf (pop entry)))
2579 (if (pop entry)
2580 (with-current-buffer buf
2581 (let ((last-undo-buffer buf)
2582 (inhibit-read-only t))
2583 (unless (memq buf org-agenda-undo-has-started-in)
2584 (push buf org-agenda-undo-has-started-in)
2585 (make-local-variable 'pending-undo-list)
2586 (undo-start))
2587 (while (and pending-undo-list
2588 (listp pending-undo-list)
2589 (not (car pending-undo-list)))
2590 (pop pending-undo-list))
2591 (undo-more 1))))))
2592 (org-goto-line line)
2593 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2595 (defun org-verify-change-for-undo (l1 l2)
2596 "Verify that a real change occurred between the undo lists L1 and L2."
2597 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2598 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2599 (not (eq l1 l2)))
2601 ;;; Agenda dispatch
2603 (defvar org-agenda-restrict-begin (make-marker))
2604 (defvar org-agenda-restrict-end (make-marker))
2605 (defvar org-agenda-last-dispatch-buffer nil)
2606 (defvar org-agenda-overriding-restriction nil)
2608 (defcustom org-agenda-custom-commands-contexts nil
2609 "Alist of custom agenda keys and contextual rules.
2611 For example, if you have a custom agenda command \"p\" and you
2612 want this command to be accessible only from plain text files,
2613 use this:
2615 \\='((\"p\" ((in-file . \"\\\\.txt\\\\'\"))))
2617 Here are the available contexts definitions:
2619 in-file: command displayed only in matching files
2620 in-mode: command displayed only in matching modes
2621 not-in-file: command not displayed in matching files
2622 not-in-mode: command not displayed in matching modes
2623 in-buffer: command displayed only in matching buffers
2624 not-in-buffer: command not displayed in matching buffers
2625 [function]: a custom function taking no argument
2627 If you define several checks, the agenda command will be
2628 accessible if there is at least one valid check.
2630 You can also bind a key to another agenda custom command
2631 depending on contextual rules.
2633 \\='((\"p\" \"q\" ((in-file . \"\\\\.txt\\\\'\"))))
2635 Here it means: in .txt files, use \"p\" as the key for the
2636 agenda command otherwise associated with \"q\". (The command
2637 originally associated with \"q\" is not displayed to avoid
2638 duplicates.)"
2639 :version "24.3"
2640 :group 'org-agenda-custom-commands
2641 :type '(repeat (list :tag "Rule"
2642 (string :tag " Agenda key")
2643 (string :tag "Replace by command")
2644 (repeat :tag "Available when"
2645 (choice
2646 (cons :tag "Condition"
2647 (choice
2648 (const :tag "In file" in-file)
2649 (const :tag "Not in file" not-in-file)
2650 (const :tag "In buffer" in-buffer)
2651 (const :tag "Not in buffer" not-in-buffer)
2652 (const :tag "In mode" in-mode)
2653 (const :tag "Not in mode" not-in-mode))
2654 (regexp))
2655 (function :tag "Custom function"))))))
2657 (defcustom org-agenda-max-entries nil
2658 "Maximum number of entries to display in an agenda.
2659 This can be nil (no limit) or an integer or an alist of agenda
2660 types with an associated number of entries to display in this
2661 type."
2662 :version "24.4"
2663 :package-version '(Org . "8.0")
2664 :group 'org-agenda-custom-commands
2665 :type '(choice (symbol :tag "No limit" nil)
2666 (integer :tag "Max number of entries")
2667 (repeat
2668 (cons (choice :tag "Agenda type"
2669 (const agenda)
2670 (const todo)
2671 (const tags)
2672 (const search))
2673 (integer :tag "Max number of entries")))))
2675 (defcustom org-agenda-max-todos nil
2676 "Maximum number of TODOs to display in an agenda.
2677 This can be nil (no limit) or an integer or an alist of agenda
2678 types with an associated number of entries to display in this
2679 type."
2680 :version "24.4"
2681 :package-version '(Org . "8.0")
2682 :group 'org-agenda-custom-commands
2683 :type '(choice (symbol :tag "No limit" nil)
2684 (integer :tag "Max number of TODOs")
2685 (repeat
2686 (cons (choice :tag "Agenda type"
2687 (const agenda)
2688 (const todo)
2689 (const tags)
2690 (const search))
2691 (integer :tag "Max number of TODOs")))))
2693 (defcustom org-agenda-max-tags nil
2694 "Maximum number of tagged entries to display in an agenda.
2695 This can be nil (no limit) or an integer or an alist of agenda
2696 types with an associated number of entries to display in this
2697 type."
2698 :version "24.4"
2699 :package-version '(Org . "8.0")
2700 :group 'org-agenda-custom-commands
2701 :type '(choice (symbol :tag "No limit" nil)
2702 (integer :tag "Max number of tagged entries")
2703 (repeat
2704 (cons (choice :tag "Agenda type"
2705 (const agenda)
2706 (const todo)
2707 (const tags)
2708 (const search))
2709 (integer :tag "Max number of tagged entries")))))
2711 (defcustom org-agenda-max-effort nil
2712 "Maximum cumulated effort duration for the agenda.
2713 This can be nil (no limit) or a number of minutes (as an integer)
2714 or an alist of agenda types with an associated number of minutes
2715 to limit entries to in this type."
2716 :version "24.4"
2717 :package-version '(Org . "8.0")
2718 :group 'org-agenda-custom-commands
2719 :type '(choice (symbol :tag "No limit" nil)
2720 (integer :tag "Max number of minutes")
2721 (repeat
2722 (cons (choice :tag "Agenda type"
2723 (const agenda)
2724 (const todo)
2725 (const tags)
2726 (const search))
2727 (integer :tag "Max number of minutes")))))
2729 (defvar org-agenda-keep-restricted-file-list nil)
2730 (defvar org-keys nil)
2731 (defvar org-match nil)
2732 ;;;###autoload
2733 (defun org-agenda (&optional arg org-keys restriction)
2734 "Dispatch agenda commands to collect entries to the agenda buffer.
2735 Prompts for a command to execute. Any prefix arg will be passed
2736 on to the selected command. The default selections are:
2738 a Call `org-agenda-list' to display the agenda for current day or week.
2739 t Call `org-todo-list' to display the global todo list.
2740 T Call `org-todo-list' to display the global todo list, select only
2741 entries with a specific TODO keyword (the user gets a prompt).
2742 m Call `org-tags-view' to display headlines with tags matching
2743 a condition (the user is prompted for the condition).
2744 M Like `m', but select only TODO entries, no ordinary headlines.
2745 e Export views to associated files.
2746 s Search entries for keywords.
2747 S Search entries for keywords, only with TODO keywords.
2748 / Multi occur across all agenda files and also files listed
2749 in `org-agenda-text-search-extra-files'.
2750 < Restrict agenda commands to buffer, subtree, or region.
2751 Press several times to get the desired effect.
2752 > Remove a previous restriction.
2753 # List \"stuck\" projects.
2754 ! Configure what \"stuck\" means.
2755 C Configure custom agenda commands.
2757 More commands can be added by configuring the variable
2758 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2759 searches can be pre-defined in this way.
2761 If the current buffer is in Org mode and visiting a file, you can also
2762 first press `<' once to indicate that the agenda should be temporarily
2763 \(until the next use of `\\[org-agenda]') restricted to the current file.
2764 Pressing `<' twice means to restrict to the current subtree or region
2765 \(if active)."
2766 (interactive "P")
2767 (catch 'exit
2768 (let* ((prefix-descriptions nil)
2769 (org-agenda-buffer-name org-agenda-buffer-name)
2770 (org-agenda-window-setup (if (equal (buffer-name)
2771 org-agenda-buffer-name)
2772 'current-window
2773 org-agenda-window-setup))
2774 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2775 (org-agenda-custom-commands
2776 ;; normalize different versions
2777 (delq nil
2778 (mapcar
2779 (lambda (x)
2780 (cond ((stringp (cdr x))
2781 (push x prefix-descriptions)
2782 nil)
2783 ((stringp (nth 1 x)) x)
2784 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2785 (t (cons (car x) (cons "" (cdr x))))))
2786 org-agenda-custom-commands)))
2787 (org-agenda-custom-commands
2788 (org-contextualize-keys
2789 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2790 (buf (current-buffer))
2791 (bfn (buffer-file-name (buffer-base-buffer)))
2792 entry key type org-match lprops ans)
2793 ;; Turn off restriction unless there is an overriding one,
2794 (unless org-agenda-overriding-restriction
2795 (unless org-agenda-keep-restricted-file-list
2796 ;; There is a request to keep the file list in place
2797 (put 'org-agenda-files 'org-restrict nil))
2798 (setq org-agenda-restrict nil)
2799 (move-marker org-agenda-restrict-begin nil)
2800 (move-marker org-agenda-restrict-end nil))
2801 ;; Delete old local properties
2802 (put 'org-agenda-redo-command 'org-lprops nil)
2803 ;; Delete previously set last-arguments
2804 (put 'org-agenda-redo-command 'last-args nil)
2805 ;; Remember where this call originated
2806 (setq org-agenda-last-dispatch-buffer (current-buffer))
2807 (unless org-keys
2808 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2809 org-keys (car ans)
2810 restriction (cdr ans)))
2811 ;; If we have sticky agenda buffers, set a name for the buffer,
2812 ;; depending on the invoking keys. The user may still set this
2813 ;; as a command option, which will overwrite what we do here.
2814 (if org-agenda-sticky
2815 (setq org-agenda-buffer-name
2816 (format "*Org Agenda(%s)*" org-keys)))
2817 ;; Establish the restriction, if any
2818 (when (and (not org-agenda-overriding-restriction) restriction)
2819 (put 'org-agenda-files 'org-restrict (list bfn))
2820 (cond
2821 ((eq restriction 'region)
2822 (setq org-agenda-restrict (current-buffer))
2823 (move-marker org-agenda-restrict-begin (region-beginning))
2824 (move-marker org-agenda-restrict-end (region-end)))
2825 ((eq restriction 'subtree)
2826 (save-excursion
2827 (setq org-agenda-restrict (current-buffer))
2828 (org-back-to-heading t)
2829 (move-marker org-agenda-restrict-begin (point))
2830 (move-marker org-agenda-restrict-end
2831 (progn (org-end-of-subtree t)))))))
2833 ;; For example the todo list should not need it (but does...)
2834 (cond
2835 ((setq entry (assoc org-keys org-agenda-custom-commands))
2836 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2837 (progn
2838 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
2839 lprops (nth 4 entry))
2840 (if org-agenda-sticky
2841 (setq org-agenda-buffer-name
2842 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2843 (format "*Org Agenda(%s)*" org-keys))))
2844 (put 'org-agenda-redo-command 'org-lprops lprops)
2845 (cond
2846 ((eq type 'agenda)
2847 (org-let lprops '(org-agenda-list current-prefix-arg)))
2848 ((eq type 'agenda*)
2849 (org-let lprops '(org-agenda-list current-prefix-arg nil nil t)))
2850 ((eq type 'alltodo)
2851 (org-let lprops '(org-todo-list current-prefix-arg)))
2852 ((eq type 'search)
2853 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
2854 ((eq type 'stuck)
2855 (org-let lprops '(org-agenda-list-stuck-projects
2856 current-prefix-arg)))
2857 ((eq type 'tags)
2858 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2859 ((eq type 'tags-todo)
2860 (org-let lprops '(org-tags-view '(4) org-match)))
2861 ((eq type 'todo)
2862 (org-let lprops '(org-todo-list org-match)))
2863 ((eq type 'tags-tree)
2864 (org-check-for-org-mode)
2865 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
2866 ((eq type 'todo-tree)
2867 (org-check-for-org-mode)
2868 (org-let lprops
2869 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2870 (regexp-quote org-match) "\\>"))))
2871 ((eq type 'occur-tree)
2872 (org-check-for-org-mode)
2873 (org-let lprops '(org-occur org-match)))
2874 ((functionp type)
2875 (org-let lprops '(funcall type org-match)))
2876 ((fboundp type)
2877 (org-let lprops '(funcall type org-match)))
2878 (t (user-error "Invalid custom agenda command type %s" type))))
2879 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2880 ((equal org-keys "C")
2881 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2882 (customize-variable 'org-agenda-custom-commands))
2883 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2884 ((equal org-keys "s") (call-interactively 'org-search-view))
2885 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2886 ((equal org-keys "t") (call-interactively 'org-todo-list))
2887 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2888 ((equal org-keys "m") (call-interactively 'org-tags-view))
2889 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2890 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2891 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
2892 (add-hook
2893 'post-command-hook
2894 (lambda ()
2895 (unless (current-message)
2896 (let* ((m (org-agenda-get-any-marker))
2897 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2898 (when note
2899 (message (concat
2900 "FLAGGING-NOTE ([?] for more info): "
2901 (org-add-props
2902 (replace-regexp-in-string
2903 "\\\\n" "//"
2904 (copy-sequence note))
2905 nil 'face 'org-warning)))))))
2906 t t))
2907 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2908 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2909 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
2910 (t (user-error "Invalid agenda key"))))))
2912 (defvar org-agenda-multi)
2914 (defun org-agenda-append-agenda ()
2915 "Append another agenda view to the current one.
2916 This function allows interactive building of block agendas.
2917 Agenda views are separated by `org-agenda-block-separator'."
2918 (interactive)
2919 (unless (derived-mode-p 'org-agenda-mode)
2920 (user-error "Can only append from within agenda buffer"))
2921 (let ((org-agenda-multi t))
2922 (org-agenda)
2923 (widen)
2924 (org-agenda-finalize)
2925 (setq buffer-read-only t)
2926 (org-agenda-fit-window-to-buffer)))
2928 (defun org-agenda-normalize-custom-commands (cmds)
2929 "Normalize custom commands CMDS."
2930 (delq nil
2931 (mapcar
2932 (lambda (x)
2933 (cond ((stringp (cdr x)) nil)
2934 ((stringp (nth 1 x)) x)
2935 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2936 (t (cons (car x) (cons "" (cdr x))))))
2937 cmds)))
2939 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2940 "The user interface for selecting an agenda command."
2941 (catch 'exit
2942 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2943 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2944 (region-p (org-region-active-p))
2945 (custom org-agenda-custom-commands)
2946 (selstring "")
2947 restriction second-time
2948 c entry key type match prefixes rmheader header-end custom1 desc
2949 line lines left right n n1)
2950 (save-window-excursion
2951 (delete-other-windows)
2952 (org-switch-to-buffer-other-window " *Agenda Commands*")
2953 (erase-buffer)
2954 (insert (eval-when-compile
2955 (let ((header
2956 "Press key for an agenda command:
2957 -------------------------------- < Buffer, subtree/region restriction
2958 a Agenda for current week or day > Remove restriction
2959 t List of all TODO entries e Export agenda views
2960 m Match a TAGS/PROP/TODO query T Entries with special TODO kwd
2961 s Search for keywords M Like m, but only TODO entries
2962 / Multi-occur S Like s, but only TODO entries
2963 ? Find :FLAGGED: entries C Configure custom agenda commands
2964 * Toggle sticky agenda views # List stuck projects (!=configure)
2966 (start 0))
2967 (while (string-match
2968 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2969 header start)
2970 (setq start (match-end 0))
2971 (add-text-properties (match-beginning 2) (match-end 2)
2972 '(face bold) header))
2973 header)))
2974 (setq header-end (point-marker))
2975 (while t
2976 (setq custom1 custom)
2977 (when (eq rmheader t)
2978 (org-goto-line 1)
2979 (re-search-forward ":" nil t)
2980 (delete-region (match-end 0) (point-at-eol))
2981 (forward-char 1)
2982 (looking-at "-+")
2983 (delete-region (match-end 0) (point-at-eol))
2984 (move-marker header-end (match-end 0)))
2985 (goto-char header-end)
2986 (delete-region (point) (point-max))
2988 ;; Produce all the lines that describe custom commands and prefixes
2989 (setq lines nil)
2990 (while (setq entry (pop custom1))
2991 (setq key (car entry) desc (nth 1 entry)
2992 type (nth 2 entry)
2993 match (nth 3 entry))
2994 (if (> (length key) 1)
2995 (cl-pushnew (string-to-char key) prefixes :test #'equal)
2996 (setq line
2997 (format
2998 "%-4s%-14s"
2999 (org-add-props (copy-sequence key)
3000 '(face bold))
3001 (cond
3002 ((string-match "\\S-" desc) desc)
3003 ((eq type 'agenda) "Agenda for current week or day")
3004 ((eq type 'agenda*) "Appointments for current week or day")
3005 ((eq type 'alltodo) "List of all TODO entries")
3006 ((eq type 'search) "Word search")
3007 ((eq type 'stuck) "List of stuck projects")
3008 ((eq type 'todo) "TODO keyword")
3009 ((eq type 'tags) "Tags query")
3010 ((eq type 'tags-todo) "Tags (TODO)")
3011 ((eq type 'tags-tree) "Tags tree")
3012 ((eq type 'todo-tree) "TODO kwd tree")
3013 ((eq type 'occur-tree) "Occur tree")
3014 ((functionp type) (if (symbolp type)
3015 (symbol-name type)
3016 "Lambda expression"))
3017 (t "???"))))
3018 (if org-agenda-menu-show-matcher
3019 (setq line
3020 (concat line ": "
3021 (cond
3022 ((stringp match)
3023 (setq match (copy-sequence match))
3024 (org-add-props match nil 'face 'org-warning))
3025 ((listp type)
3026 (format "set of %d commands" (length type))))))
3027 (if (org-string-nw-p match)
3028 (add-text-properties
3029 0 (length line) (list 'help-echo
3030 (concat "Matcher: " match)) line)))
3031 (push line lines)))
3032 (setq lines (nreverse lines))
3033 (when prefixes
3034 (mapc (lambda (x)
3035 (push
3036 (format "%s %s"
3037 (org-add-props (char-to-string x)
3038 nil 'face 'bold)
3039 (or (cdr (assoc (concat selstring
3040 (char-to-string x))
3041 prefix-descriptions))
3042 "Prefix key"))
3043 lines))
3044 prefixes))
3046 ;; Check if we should display in two columns
3047 (if org-agenda-menu-two-columns
3048 (progn
3049 (setq n (length lines)
3050 n1 (+ (/ n 2) (mod n 2))
3051 right (nthcdr n1 lines)
3052 left (copy-sequence lines))
3053 (setcdr (nthcdr (1- n1) left) nil))
3054 (setq left lines right nil))
3055 (while left
3056 (insert "\n" (pop left))
3057 (when right
3058 (if (< (current-column) 40)
3059 (move-to-column 40 t)
3060 (insert " "))
3061 (insert (pop right))))
3063 ;; Make the window the right size
3064 (goto-char (point-min))
3065 (if second-time
3066 (if (not (pos-visible-in-window-p (point-max)))
3067 (org-fit-window-to-buffer))
3068 (setq second-time t)
3069 (org-fit-window-to-buffer))
3071 ;; Ask for selection
3072 (message "Press key for agenda command%s:"
3073 (if (or restrict-ok org-agenda-overriding-restriction)
3074 (if org-agenda-overriding-restriction
3075 " (restriction lock active)"
3076 (if restriction
3077 (format " (restricted to %s)" restriction)
3078 " (unrestricted)"))
3079 ""))
3080 (setq c (read-char-exclusive))
3081 (message "")
3082 (cond
3083 ((assoc (char-to-string c) custom)
3084 (setq selstring (concat selstring (char-to-string c)))
3085 (throw 'exit (cons selstring restriction)))
3086 ((memq c prefixes)
3087 (setq selstring (concat selstring (char-to-string c))
3088 prefixes nil
3089 rmheader (or rmheader t)
3090 custom (delq nil (mapcar
3091 (lambda (x)
3092 (if (or (= (length (car x)) 1)
3093 (/= (string-to-char (car x)) c))
3095 (cons (substring (car x) 1) (cdr x))))
3096 custom))))
3097 ((eq c ?*)
3098 (call-interactively 'org-toggle-sticky-agenda)
3099 (sit-for 2))
3100 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
3101 (message "Restriction is only possible in Org buffers")
3102 (ding) (sit-for 1))
3103 ((eq c ?1)
3104 (org-agenda-remove-restriction-lock 'noupdate)
3105 (setq restriction 'buffer))
3106 ((eq c ?0)
3107 (org-agenda-remove-restriction-lock 'noupdate)
3108 (setq restriction (if region-p 'region 'subtree)))
3109 ((eq c ?<)
3110 (org-agenda-remove-restriction-lock 'noupdate)
3111 (setq restriction
3112 (cond
3113 ((eq restriction 'buffer)
3114 (if region-p 'region 'subtree))
3115 ((memq restriction '(subtree region))
3116 nil)
3117 (t 'buffer))))
3118 ((eq c ?>)
3119 (org-agenda-remove-restriction-lock 'noupdate)
3120 (setq restriction nil))
3121 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
3122 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
3123 ((and (> (length selstring) 0) (eq c ?\d))
3124 (delete-window)
3125 (org-agenda-get-restriction-and-command prefix-descriptions))
3127 ((equal c ?q) (error "Abort"))
3128 (t (user-error "Invalid key %c" c))))))))
3130 (defun org-agenda-fit-window-to-buffer ()
3131 "Fit the window to the buffer size."
3132 (and (memq org-agenda-window-setup '(reorganize-frame))
3133 (fboundp 'fit-window-to-buffer)
3134 (if (and (= (cdr org-agenda-window-frame-fractions) 1.0)
3135 (= (car org-agenda-window-frame-fractions) 1.0))
3136 (delete-other-windows)
3137 (org-fit-window-to-buffer
3139 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3140 (floor (* (frame-height) (car org-agenda-window-frame-fractions)))))))
3142 (defvar org-cmd nil)
3143 (defvar org-agenda-overriding-cmd nil)
3144 (defvar org-agenda-overriding-arguments nil)
3145 (defvar org-agenda-overriding-cmd-arguments nil)
3146 (defun org-agenda-run-series (name series)
3147 "Run agenda NAME as a SERIES of agenda commands."
3148 (org-let (nth 1 series) '(org-agenda-prepare name))
3149 ;; We need to reset agenda markers here, because when constructing a
3150 ;; block agenda, the individual blocks do not do that.
3151 (org-agenda-reset-markers)
3152 (let* ((org-agenda-multi t)
3153 (redo (list 'org-agenda-run-series name (list 'quote series)))
3154 (cmds (car series))
3155 (gprops (nth 1 series))
3156 match ;; The byte compiler incorrectly complains about this. Keep it!
3157 org-cmd type lprops)
3158 (while (setq org-cmd (pop cmds))
3159 (setq type (car org-cmd))
3160 (setq match (eval (nth 1 org-cmd)))
3161 (setq lprops (nth 2 org-cmd))
3162 (let ((org-agenda-overriding-arguments
3163 (if (eq org-agenda-overriding-cmd org-cmd)
3164 (or org-agenda-overriding-arguments
3165 org-agenda-overriding-cmd-arguments))))
3166 (cond
3167 ((eq type 'agenda)
3168 (org-let2 gprops lprops
3169 '(call-interactively 'org-agenda-list)))
3170 ((eq type 'agenda*)
3171 (org-let2 gprops lprops
3172 '(funcall 'org-agenda-list nil nil t)))
3173 ((eq type 'alltodo)
3174 (org-let2 gprops lprops
3175 '(call-interactively 'org-todo-list)))
3176 ((eq type 'search)
3177 (org-let2 gprops lprops
3178 '(org-search-view current-prefix-arg match nil)))
3179 ((eq type 'stuck)
3180 (org-let2 gprops lprops
3181 '(call-interactively 'org-agenda-list-stuck-projects)))
3182 ((eq type 'tags)
3183 (org-let2 gprops lprops
3184 '(org-tags-view current-prefix-arg match)))
3185 ((eq type 'tags-todo)
3186 (org-let2 gprops lprops
3187 '(org-tags-view '(4) match)))
3188 ((eq type 'todo)
3189 (org-let2 gprops lprops
3190 '(org-todo-list match)))
3191 ((fboundp type)
3192 (org-let2 gprops lprops
3193 '(funcall type match)))
3194 (t (error "Invalid type in command series")))))
3195 (widen)
3196 (let ((inhibit-read-only t))
3197 (add-text-properties (point-min) (point-max)
3198 `(org-series t org-series-redo-cmd ,redo)))
3199 (setq org-agenda-redo-command redo)
3200 (goto-char (point-min)))
3201 (org-agenda-fit-window-to-buffer)
3202 (org-let (nth 1 series) '(org-agenda-finalize)))
3204 ;;;###autoload
3205 (defmacro org-batch-agenda (cmd-key &rest parameters)
3206 "Run an agenda command in batch mode and send the result to STDOUT.
3207 If CMD-KEY is a string of length 1, it is used as a key in
3208 `org-agenda-custom-commands' and triggers this command. If it is a
3209 longer string it is used as a tags/todo match string.
3210 Parameters are alternating variable names and values that will be bound
3211 before running the agenda command."
3212 (org-eval-in-environment (org-make-parameter-alist parameters)
3213 (let (org-agenda-sticky)
3214 (if (> (length cmd-key) 1)
3215 (org-tags-view nil cmd-key)
3216 (org-agenda nil cmd-key))))
3217 (set-buffer org-agenda-buffer-name)
3218 (princ (buffer-string)))
3220 (defvar org-agenda-info nil)
3222 ;;;###autoload
3223 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
3224 "Run an agenda command in batch mode and send the result to STDOUT.
3225 If CMD-KEY is a string of length 1, it is used as a key in
3226 `org-agenda-custom-commands' and triggers this command. If it is a
3227 longer string it is used as a tags/todo match string.
3228 Parameters are alternating variable names and values that will be bound
3229 before running the agenda command.
3231 The output gives a line for each selected agenda item. Each
3232 item is a list of comma-separated values, like this:
3234 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3236 category The category of the item
3237 head The headline, without TODO kwd, TAGS and PRIORITY
3238 type The type of the agenda entry, can be
3239 todo selected in TODO match
3240 tagsmatch selected in tags match
3241 diary imported from diary
3242 deadline a deadline on given date
3243 scheduled scheduled on given date
3244 timestamp entry has timestamp on given date
3245 closed entry was closed on given date
3246 upcoming-deadline warning about deadline
3247 past-scheduled forwarded scheduled item
3248 block entry has date block including g. date
3249 todo The todo keyword, if any
3250 tags All tags including inherited ones, separated by colons
3251 date The relevant date, like 2007-2-14
3252 time The time, like 15:00-16:50
3253 extra Sting with extra planning info
3254 priority-l The priority letter if any was given
3255 priority-n The computed numerical priority
3256 agenda-day The day in the agenda where this is listed"
3257 (org-eval-in-environment (append '((org-agenda-remove-tags t))
3258 (org-make-parameter-alist parameters))
3259 (if (> (length cmd-key) 2)
3260 (org-tags-view nil cmd-key)
3261 (org-agenda nil cmd-key)))
3262 (set-buffer org-agenda-buffer-name)
3263 (let* ((lines (org-split-string (buffer-string) "\n"))
3264 line)
3265 (while (setq line (pop lines))
3266 (catch 'next
3267 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
3268 (setq org-agenda-info
3269 (org-fix-agenda-info (text-properties-at 0 line)))
3270 (princ
3271 (mapconcat 'org-agenda-export-csv-mapper
3272 '(org-category txt type todo tags date time extra
3273 priority-letter priority agenda-day)
3274 ","))
3275 (princ "\n")))))
3277 (defun org-fix-agenda-info (props)
3278 "Make sure all properties on an agenda item have a canonical form.
3279 This ensures the export commands can easily use it."
3280 (let (tmp re)
3281 (when (setq tmp (plist-get props 'tags))
3282 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3283 (when (setq tmp (plist-get props 'date))
3284 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3285 (let ((calendar-date-display-form '(year "-" month "-" day)))
3286 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3288 (setq tmp (calendar-date-string tmp)))
3289 (setq props (plist-put props 'date tmp)))
3290 (when (setq tmp (plist-get props 'day))
3291 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3292 (let ((calendar-date-display-form '(year "-" month "-" day)))
3293 (setq tmp (calendar-date-string tmp)))
3294 (setq props (plist-put props 'day tmp))
3295 (setq props (plist-put props 'agenda-day tmp)))
3296 (when (setq tmp (plist-get props 'txt))
3297 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3298 (plist-put props 'priority-letter (match-string 1 tmp))
3299 (setq tmp (replace-match "" t t tmp)))
3300 (when (and (setq re (plist-get props 'org-todo-regexp))
3301 (setq re (concat "\\`\\.*" re " ?"))
3302 (let ((case-fold-search nil)) (string-match re tmp)))
3303 (plist-put props 'todo (match-string 1 tmp))
3304 (setq tmp (replace-match "" t t tmp)))
3305 (plist-put props 'txt tmp)))
3306 props)
3308 (defun org-agenda-export-csv-mapper (prop)
3309 (let ((res (plist-get org-agenda-info prop)))
3310 (setq res
3311 (cond
3312 ((not res) "")
3313 ((stringp res) res)
3314 (t (prin1-to-string res))))
3315 (org-trim (replace-regexp-in-string "," ";" res nil t))))
3317 ;;;###autoload
3318 (defun org-store-agenda-views (&rest parameters)
3319 "Store agenda views."
3320 (interactive)
3321 (eval (list 'org-batch-store-agenda-views)))
3323 ;;;###autoload
3324 (defmacro org-batch-store-agenda-views (&rest parameters)
3325 "Run all custom agenda commands that have a file argument."
3326 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3327 (pop-up-frames nil)
3328 (dir default-directory)
3329 (pars (org-make-parameter-alist parameters))
3330 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
3331 (save-window-excursion
3332 (while cmds
3333 (setq cmd (pop cmds)
3334 thiscmdkey (car cmd)
3335 thiscmdcmd (cdr cmd)
3336 match (nth 2 thiscmdcmd)
3337 bufname (if org-agenda-sticky
3338 (or (and (stringp match)
3339 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3340 (format "*Org Agenda(%s)*" thiscmdkey))
3341 org-agenda-buffer-name)
3342 cmd-or-set (nth 2 cmd)
3343 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3344 files (nth (if (listp cmd-or-set) 4 5) cmd))
3345 (if (stringp files) (setq files (list files)))
3346 (when files
3347 (org-eval-in-environment (append org-agenda-exporter-settings
3348 opts pars)
3349 (org-agenda nil thiscmdkey))
3350 (set-buffer bufname)
3351 (while files
3352 (org-eval-in-environment (append org-agenda-exporter-settings
3353 opts pars)
3354 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3355 (and (get-buffer bufname)
3356 (kill-buffer bufname)))))))
3358 (defvar org-agenda-current-span nil
3359 "The current span used in the agenda view.") ; local variable in the agenda buffer
3360 (defun org-agenda-mark-header-line (pos)
3361 "Mark the line at POS as an agenda structure header."
3362 (save-excursion
3363 (goto-char pos)
3364 (put-text-property (point-at-bol) (point-at-eol)
3365 'org-agenda-structural-header t)
3366 (when org-agenda-title-append
3367 (put-text-property (point-at-bol) (point-at-eol)
3368 'org-agenda-title-append org-agenda-title-append))))
3370 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3371 (defvar org-agenda-write-buffer-name "Agenda View")
3372 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3373 "Write the current buffer (an agenda view) as a file.
3375 Depending on the extension of the file name, plain text (.txt),
3376 HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
3377 If the extension is .ics, translate visible agenda into iCalendar
3378 format. If the extension is .org, collect all subtrees
3379 corresponding to the agenda entries and add them in an .org file.
3381 With prefix argument OPEN, open the new file immediately. If
3382 NOSETTINGS is given, do not scope the settings of
3383 `org-agenda-exporter-settings' into the export commands. This is
3384 used when the settings have already been scoped and we do not
3385 wish to overrule other, higher priority settings. If
3386 AGENDA-BUFFER-NAME is provided, use this as the buffer name for
3387 the agenda to write."
3388 (interactive "FWrite agenda to file: \nP")
3389 (if (or (not (file-writable-p file))
3390 (and (file-exists-p file)
3391 (if (called-interactively-p 'any)
3392 (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
3393 (user-error "Cannot write agenda to file %s" file))
3394 (org-let (if nosettings nil org-agenda-exporter-settings)
3395 '(save-excursion
3396 (save-window-excursion
3397 (let ((bs (copy-sequence (buffer-string)))
3398 (extension (file-name-extension file))
3399 (default-directory (file-name-directory file))
3400 beg content)
3401 (with-temp-buffer
3402 (rename-buffer org-agenda-write-buffer-name t)
3403 (set-buffer-modified-p nil)
3404 (insert bs)
3405 (org-agenda-remove-marked-text 'invisible 'org-filtered)
3406 (run-hooks 'org-agenda-before-write-hook)
3407 (cond
3408 ((bound-and-true-p org-mobile-creating-agendas)
3409 (org-mobile-write-agenda-for-mobile file))
3410 ((string= "org" extension)
3411 (let (content p m message-log-max)
3412 (goto-char (point-min))
3413 (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
3414 (goto-char p)
3415 (setq m (get-text-property (point) 'org-hd-marker))
3416 (when m
3417 (push (save-excursion
3418 (set-buffer (marker-buffer m))
3419 (goto-char m)
3420 (org-copy-subtree 1 nil t t)
3421 org-subtree-clip)
3422 content)))
3423 (find-file file)
3424 (erase-buffer)
3425 (dolist (s content) (org-paste-subtree 1 s))
3426 (write-file file)
3427 (kill-buffer (current-buffer))
3428 (message "Org file written to %s" file)))
3429 ((member extension '("html" "htm"))
3430 (or (require 'htmlize nil t)
3431 (error "Please install htmlize from https://github.com/hniksic/emacs-htmlize"))
3432 (set-buffer (htmlize-buffer (current-buffer)))
3433 (when org-agenda-export-html-style
3434 ;; replace <style> section with org-agenda-export-html-style
3435 (goto-char (point-min))
3436 (kill-region (- (search-forward "<style") 6)
3437 (search-forward "</style>"))
3438 (insert org-agenda-export-html-style))
3439 (write-file file)
3440 (kill-buffer (current-buffer))
3441 (message "HTML written to %s" file))
3442 ((string= "ps" extension)
3443 (require 'ps-print)
3444 (ps-print-buffer-with-faces file)
3445 (message "Postscript written to %s" file))
3446 ((string= "pdf" extension)
3447 (require 'ps-print)
3448 (ps-print-buffer-with-faces
3449 (concat (file-name-sans-extension file) ".ps"))
3450 (call-process "ps2pdf" nil nil nil
3451 (expand-file-name
3452 (concat (file-name-sans-extension file) ".ps"))
3453 (expand-file-name file))
3454 (delete-file (concat (file-name-sans-extension file) ".ps"))
3455 (message "PDF written to %s" file))
3456 ((string= "ics" extension)
3457 (require 'ox-icalendar)
3458 (org-icalendar-export-current-agenda (expand-file-name file)))
3460 (let ((bs (buffer-string)))
3461 (find-file file)
3462 (erase-buffer)
3463 (insert bs)
3464 (save-buffer 0)
3465 (kill-buffer (current-buffer))
3466 (message "Plain text written to %s" file))))))))
3467 (set-buffer (or agenda-bufname
3468 (and (called-interactively-p 'any) (buffer-name))
3469 org-agenda-buffer-name)))
3470 (when open (org-open-file file)))
3472 (defun org-agenda-remove-marked-text (property &optional value)
3473 "Delete all text marked with VALUE of PROPERTY.
3474 VALUE defaults to t."
3475 (let (beg)
3476 (setq value (or value t))
3477 (while (setq beg (text-property-any (point-min) (point-max)
3478 property value))
3479 (delete-region
3480 beg (or (next-single-property-change beg property)
3481 (point-max))))))
3483 (defun org-agenda-add-entry-text ()
3484 "Add entry text to agenda lines.
3485 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3486 entry text following headings shown in the agenda.
3487 Drawers will be excluded, also the line with scheduling/deadline info."
3488 (when (and (> org-agenda-add-entry-text-maxlines 0)
3489 (not (bound-and-true-p org-mobile-creating-agendas)))
3490 (let (m txt)
3491 (goto-char (point-min))
3492 (while (not (eobp))
3493 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3494 (beginning-of-line 2)
3495 (setq txt (org-agenda-get-some-entry-text
3496 m org-agenda-add-entry-text-maxlines " > "))
3497 (end-of-line 1)
3498 (if (string-match "\\S-" txt)
3499 (insert "\n" txt)
3500 (or (eobp) (forward-char 1))))))))
3502 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3503 &rest keep)
3504 "Extract entry text from MARKER, at most N-LINES lines.
3505 This will ignore drawers etc, just get the text.
3506 If INDENT is given, prefix every line with this string. If KEEP is
3507 given, it is a list of symbols, defining stuff that should not be
3508 removed from the entry content. Currently only `planning' is allowed here."
3509 (let (txt drawer-re kwd-time-re ind)
3510 (save-excursion
3511 (with-current-buffer (marker-buffer marker)
3512 (if (not (derived-mode-p 'org-mode))
3513 (setq txt "")
3514 (org-with-wide-buffer
3515 (goto-char marker)
3516 (end-of-line 1)
3517 (setq txt (buffer-substring
3518 (min (1+ (point)) (point-max))
3519 (progn (outline-next-heading) (point)))
3520 drawer-re org-drawer-regexp
3521 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3522 ".*\n?"))
3523 (with-temp-buffer
3524 (insert txt)
3525 (when org-agenda-add-entry-text-descriptive-links
3526 (goto-char (point-min))
3527 (while (org-activate-links (point-max))
3528 (add-text-properties (match-beginning 0) (match-end 0)
3529 '(face org-link))))
3530 (goto-char (point-min))
3531 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3532 (set-text-properties (match-beginning 0) (match-end 0)
3533 nil))
3534 (goto-char (point-min))
3535 (while (re-search-forward drawer-re nil t)
3536 (delete-region
3537 (match-beginning 0)
3538 (progn (re-search-forward
3539 "^[ \t]*:END:.*\n?" nil 'move)
3540 (point))))
3541 (unless (member 'planning keep)
3542 (goto-char (point-min))
3543 (while (re-search-forward kwd-time-re nil t)
3544 (replace-match "")))
3545 (goto-char (point-min))
3546 (when org-agenda-entry-text-exclude-regexps
3547 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3548 (while (setq re (pop re-list))
3549 (goto-char (point-min))
3550 (while (re-search-forward re nil t)
3551 (replace-match "")))))
3552 (goto-char (point-max))
3553 (skip-chars-backward " \t\n")
3554 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3556 ;; find and remove min common indentation
3557 (goto-char (point-min))
3558 (untabify (point-min) (point-max))
3559 (setq ind (org-get-indentation))
3560 (while (not (eobp))
3561 (unless (looking-at "[ \t]*$")
3562 (setq ind (min ind (org-get-indentation))))
3563 (beginning-of-line 2))
3564 (goto-char (point-min))
3565 (while (not (eobp))
3566 (unless (looking-at "[ \t]*$")
3567 (move-to-column ind)
3568 (delete-region (point-at-bol) (point)))
3569 (beginning-of-line 2))
3571 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3573 (goto-char (point-min))
3574 (when indent
3575 (while (and (not (eobp)) (re-search-forward "^" nil t))
3576 (replace-match indent t t)))
3577 (goto-char (point-min))
3578 (while (looking-at "[ \t]*\n") (replace-match ""))
3579 (goto-char (point-max))
3580 (when (> (org-current-line)
3581 n-lines)
3582 (org-goto-line (1+ n-lines))
3583 (backward-char 1))
3584 (setq txt (buffer-substring (point-min) (point))))))))
3585 txt))
3587 (defun org-check-for-org-mode ()
3588 "Make sure current buffer is in Org mode. Error if not."
3589 (or (derived-mode-p 'org-mode)
3590 (error "Cannot execute Org agenda command on buffer in %s"
3591 major-mode)))
3593 ;;; Agenda prepare and finalize
3595 (defvar org-agenda-multi nil) ; dynamically scoped
3596 (defvar org-agenda-pre-window-conf nil)
3597 (defvar org-agenda-columns-active nil)
3598 (defvar org-agenda-name nil)
3599 (defvar org-agenda-tag-filter nil)
3600 (defvar org-agenda-category-filter nil)
3601 (defvar org-agenda-regexp-filter nil)
3602 (defvar org-agenda-effort-filter nil)
3603 (defvar org-agenda-top-headline-filter nil)
3604 (defvar org-agenda-tag-filter-preset nil
3605 "A preset of the tags filter used for secondary agenda filtering.
3606 This must be a list of strings, each string must be a single tag preceded
3607 by \"+\" or \"-\".
3608 This variable should not be set directly, but agenda custom commands can
3609 bind it in the options section. The preset filter is a global property of
3610 the entire agenda view. In a block agenda, it will not work reliably to
3611 define a filter for one of the individual blocks. You need to set it in
3612 the global options and expect it to be applied to the entire view.")
3614 (defvar org-agenda-category-filter-preset nil
3615 "A preset of the category filter used for secondary agenda filtering.
3616 This must be a list of strings, each string must be a single category
3617 preceded by \"+\" or \"-\".
3618 This variable should not be set directly, but agenda custom commands can
3619 bind it in the options section. The preset filter is a global property of
3620 the entire agenda view. In a block agenda, it will not work reliably to
3621 define a filter for one of the individual blocks. You need to set it in
3622 the global options and expect it to be applied to the entire view.")
3624 (defvar org-agenda-regexp-filter-preset nil
3625 "A preset of the regexp filter used for secondary agenda filtering.
3626 This must be a list of strings, each string must be a single regexp
3627 preceded by \"+\" or \"-\".
3628 This variable should not be set directly, but agenda custom commands can
3629 bind it in the options section. The preset filter is a global property of
3630 the entire agenda view. In a block agenda, it will not work reliably to
3631 define a filter for one of the individual blocks. You need to set it in
3632 the global options and expect it to be applied to the entire view.")
3634 (defvar org-agenda-effort-filter-preset nil
3635 "A preset of the effort condition used for secondary agenda filtering.
3636 This must be a list of strings, each string must be a single regexp
3637 preceded by \"+\" or \"-\".
3638 This variable should not be set directly, but agenda custom commands can
3639 bind it in the options section. The preset filter is a global property of
3640 the entire agenda view. In a block agenda, it will not work reliably to
3641 define a filter for one of the individual blocks. You need to set it in
3642 the global options and expect it to be applied to the entire view.")
3644 (defun org-agenda-use-sticky-p ()
3645 "Return non-nil if an agenda buffer named
3646 `org-agenda-buffer-name' exists and should be shown instead of
3647 generating a new one."
3648 (and
3649 ;; turned off by user
3650 org-agenda-sticky
3651 ;; For multi-agenda buffer already exists
3652 (not org-agenda-multi)
3653 ;; buffer found
3654 (get-buffer org-agenda-buffer-name)
3655 ;; C-u parameter is same as last call
3656 (with-current-buffer (get-buffer org-agenda-buffer-name)
3657 (and
3658 (equal current-prefix-arg
3659 org-agenda-last-prefix-arg)
3660 ;; In case user turned stickiness on, while having existing
3661 ;; Agenda buffer active, don't reuse that buffer, because it
3662 ;; does not have org variables local
3663 org-agenda-this-buffer-is-sticky))))
3665 (defun org-agenda-prepare-window (abuf filter-alist)
3666 "Setup agenda buffer in the window.
3667 ABUF is the buffer for the agenda window.
3668 FILTER-ALIST is an alist of filters we need to apply when
3669 `org-agenda-persistent-filter' is non-nil."
3670 (let* ((awin (get-buffer-window abuf)) wconf)
3671 (cond
3672 ((equal (current-buffer) abuf) nil)
3673 (awin (select-window awin))
3674 ((not (setq wconf (current-window-configuration))))
3675 ((eq org-agenda-window-setup 'current-window)
3676 (pop-to-buffer-same-window abuf))
3677 ((eq org-agenda-window-setup 'other-window)
3678 (org-switch-to-buffer-other-window abuf))
3679 ((eq org-agenda-window-setup 'other-frame)
3680 (switch-to-buffer-other-frame abuf))
3681 ((eq org-agenda-window-setup 'only-window)
3682 (delete-other-windows)
3683 (pop-to-buffer-same-window abuf))
3684 ((eq org-agenda-window-setup 'reorganize-frame)
3685 (delete-other-windows)
3686 (org-switch-to-buffer-other-window abuf)))
3687 (setq org-agenda-tag-filter (cdr (assq 'tag filter-alist)))
3688 (setq org-agenda-category-filter (cdr (assq 'cat filter-alist)))
3689 (setq org-agenda-effort-filter (cdr (assq 'effort filter-alist)))
3690 (setq org-agenda-regexp-filter (cdr (assq 're filter-alist)))
3691 ;; Additional test in case agenda is invoked from within agenda
3692 ;; buffer via elisp link.
3693 (unless (equal (current-buffer) abuf)
3694 (pop-to-buffer-same-window abuf))
3695 (setq org-agenda-pre-window-conf
3696 (or wconf org-agenda-pre-window-conf))))
3698 (defun org-agenda-prepare (&optional name)
3699 (let ((filter-alist (if org-agenda-persistent-filter
3700 (with-current-buffer
3701 (get-buffer-create org-agenda-buffer-name)
3702 (list `(tag . ,org-agenda-tag-filter)
3703 `(re . ,org-agenda-regexp-filter)
3704 `(effort . ,org-agenda-effort-filter)
3705 `(cat . ,org-agenda-category-filter))))))
3706 (if (org-agenda-use-sticky-p)
3707 (progn
3708 (put 'org-agenda-tag-filter :preset-filter nil)
3709 (put 'org-agenda-category-filter :preset-filter nil)
3710 (put 'org-agenda-regexp-filter :preset-filter nil)
3711 ;; Popup existing buffer
3712 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name)
3713 filter-alist)
3714 (message "Sticky Agenda buffer, use `r' to refresh")
3715 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3716 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3717 (setq org-todo-keywords-for-agenda nil)
3718 (put 'org-agenda-tag-filter :preset-filter
3719 org-agenda-tag-filter-preset)
3720 (put 'org-agenda-category-filter :preset-filter
3721 org-agenda-category-filter-preset)
3722 (put 'org-agenda-regexp-filter :preset-filter
3723 org-agenda-regexp-filter-preset)
3724 (put 'org-agenda-effort-filter :preset-filter
3725 org-agenda-effort-filter-preset)
3726 (if org-agenda-multi
3727 (progn
3728 (setq buffer-read-only nil)
3729 (goto-char (point-max))
3730 (unless (or (bobp) org-agenda-compact-blocks
3731 (not org-agenda-block-separator))
3732 (insert "\n"
3733 (if (stringp org-agenda-block-separator)
3734 org-agenda-block-separator
3735 (make-string (window-width) org-agenda-block-separator))
3736 "\n"))
3737 (narrow-to-region (point) (point-max)))
3738 (setq org-done-keywords-for-agenda nil)
3739 ;; Setting any org variables that are in org-agenda-local-vars
3740 ;; list need to be done after the prepare call
3741 (org-agenda-prepare-window
3742 (get-buffer-create org-agenda-buffer-name) filter-alist)
3743 (setq buffer-read-only nil)
3744 (org-agenda-reset-markers)
3745 (let ((inhibit-read-only t)) (erase-buffer))
3746 (org-agenda-mode)
3747 (setq org-agenda-buffer (current-buffer))
3748 (setq org-agenda-contributing-files nil)
3749 (setq org-agenda-columns-active nil)
3750 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3751 (setq org-todo-keywords-for-agenda
3752 (org-uniquify org-todo-keywords-for-agenda))
3753 (setq org-done-keywords-for-agenda
3754 (org-uniquify org-done-keywords-for-agenda))
3755 (setq org-agenda-last-prefix-arg current-prefix-arg)
3756 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3757 ;; Don't set these until we know we're in the agenda buffer,
3758 ;; and we know they're valid.
3759 (setq org-agenda-lprops (or org-agenda-lprops
3760 (get 'org-agenda-redo-command 'org-lprops)))
3761 (and name (not org-agenda-name)
3762 (setq-local org-agenda-name name)))
3763 (setq buffer-read-only nil))))
3765 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3766 (defun org-agenda-finalize ()
3767 "Finishing touch for the agenda buffer, called just before displaying it."
3768 (unless org-agenda-multi
3769 (save-excursion
3770 (let ((inhibit-read-only t))
3771 (goto-char (point-min))
3772 (save-excursion
3773 (while (org-activate-links (point-max))
3774 (add-text-properties (match-beginning 0) (match-end 0)
3775 '(face org-link))))
3776 (unless (eq org-agenda-remove-tags t)
3777 (org-agenda-align-tags))
3778 (unless org-agenda-with-colors
3779 (remove-text-properties (point-min) (point-max) '(face nil)))
3780 (if (and (boundp 'org-agenda-overriding-columns-format)
3781 org-agenda-overriding-columns-format)
3782 (setq-local org-agenda-overriding-columns-format
3783 org-agenda-overriding-columns-format))
3784 (if (and (boundp 'org-agenda-view-columns-initially)
3785 org-agenda-view-columns-initially)
3786 (org-agenda-columns))
3787 (when org-agenda-fontify-priorities
3788 (org-agenda-fontify-priorities))
3789 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3790 (org-agenda-dim-blocked-tasks))
3791 (org-agenda-mark-clocking-task)
3792 (when org-agenda-entry-text-mode
3793 (org-agenda-entry-text-hide)
3794 (org-agenda-entry-text-show))
3795 (if (and (functionp 'org-habit-insert-consistency-graphs)
3796 (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
3797 (org-habit-insert-consistency-graphs))
3798 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3799 (unless (or (eq org-agenda-show-inherited-tags 'always)
3800 (and (listp org-agenda-show-inherited-tags)
3801 (memq org-agenda-type org-agenda-show-inherited-tags))
3802 (and (eq org-agenda-show-inherited-tags t)
3803 (or (eq org-agenda-use-tag-inheritance t)
3804 (and (listp org-agenda-use-tag-inheritance)
3805 (not (memq org-agenda-type
3806 org-agenda-use-tag-inheritance))))))
3807 (let (mrk)
3808 (save-excursion
3809 (goto-char (point-min))
3810 (while (equal (forward-line) 0)
3811 (when (setq mrk (get-text-property (point) 'org-hd-marker))
3812 (put-text-property (point-at-bol) (point-at-eol)
3813 'tags (org-with-point-at mrk
3814 (delete-dups
3815 (mapcar 'downcase (org-get-tags-at))))))))))
3816 (run-hooks 'org-agenda-finalize-hook)
3817 (when org-agenda-top-headline-filter
3818 (org-agenda-filter-top-headline-apply
3819 org-agenda-top-headline-filter))
3820 (when org-agenda-tag-filter
3821 (org-agenda-filter-apply org-agenda-tag-filter 'tag t))
3822 (when (get 'org-agenda-tag-filter :preset-filter)
3823 (org-agenda-filter-apply
3824 (get 'org-agenda-tag-filter :preset-filter) 'tag t))
3825 (when org-agenda-category-filter
3826 (org-agenda-filter-apply org-agenda-category-filter 'category))
3827 (when (get 'org-agenda-category-filter :preset-filter)
3828 (org-agenda-filter-apply
3829 (get 'org-agenda-category-filter :preset-filter) 'category))
3830 (when org-agenda-regexp-filter
3831 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
3832 (when (get 'org-agenda-regexp-filter :preset-filter)
3833 (org-agenda-filter-apply
3834 (get 'org-agenda-regexp-filter :preset-filter) 'regexp))
3835 (when org-agenda-effort-filter
3836 (org-agenda-filter-apply org-agenda-effort-filter 'effort))
3837 (when (get 'org-agenda-effort-filter :preset-filter)
3838 (org-agenda-filter-apply
3839 (get 'org-agenda-effort-filter :preset-filter) 'effort))
3840 (add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)))))
3842 (defun org-agenda-mark-clocking-task ()
3843 "Mark the current clock entry in the agenda if it is present."
3844 ;; We need to widen when `org-agenda-finalize' is called from
3845 ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
3846 (when (bound-and-true-p org-clock-current-task)
3847 (save-restriction
3848 (widen)
3849 (org-agenda-unmark-clocking-task)
3850 (when (marker-buffer org-clock-hd-marker)
3851 (save-excursion
3852 (goto-char (point-min))
3853 (let (s ov)
3854 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3855 (goto-char s)
3856 (when (equal (org-get-at-bol 'org-hd-marker)
3857 org-clock-hd-marker)
3858 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3859 (overlay-put ov 'type 'org-agenda-clocking)
3860 (overlay-put ov 'face 'org-agenda-clocking)
3861 (overlay-put ov 'help-echo
3862 "The clock is running in this item")))))))))
3864 (defun org-agenda-unmark-clocking-task ()
3865 "Unmark the current clocking task."
3866 (mapc (lambda (o)
3867 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3868 (delete-overlay o)))
3869 (overlays-in (point-min) (point-max))))
3871 (defun org-agenda-fontify-priorities ()
3872 "Make highest priority lines bold, and lowest italic."
3873 (interactive)
3874 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3875 (delete-overlay o)))
3876 (overlays-in (point-min) (point-max)))
3877 (save-excursion
3878 (let (b e p ov h l)
3879 (goto-char (point-min))
3880 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3881 (setq h (or (get-char-property (point) 'org-highest-priority)
3882 org-highest-priority)
3883 l (or (get-char-property (point) 'org-lowest-priority)
3884 org-lowest-priority)
3885 p (string-to-char (match-string 1))
3886 b (match-beginning 0)
3887 e (if (eq org-agenda-fontify-priorities 'cookies)
3888 (match-end 0)
3889 (point-at-eol))
3890 ov (make-overlay b e))
3891 (overlay-put
3892 ov 'face
3893 (let ((special-face
3894 (cond ((org-face-from-face-or-color
3895 'priority 'org-priority
3896 (cdr (assoc p org-priority-faces))))
3897 ((and (listp org-agenda-fontify-priorities)
3898 (org-face-from-face-or-color
3899 'priority 'org-priority
3900 (cdr (assoc p org-agenda-fontify-priorities)))))
3901 ((equal p l) 'italic)
3902 ((equal p h) 'bold))))
3903 (if special-face (list special-face 'org-priority) 'org-priority)))
3904 (overlay-put ov 'org-type 'org-priority)))))
3906 (defvar org-depend-tag-blocked)
3908 (defun org-agenda-dim-blocked-tasks (&optional invisible)
3909 "Dim currently blocked TODOs in the agenda display.
3910 When INVISIBLE is non-nil, hide currently blocked TODO instead of
3911 dimming them."
3912 (interactive "P")
3913 (when (called-interactively-p 'interactive)
3914 (message "Dim or hide blocked tasks..."))
3915 (dolist (o (overlays-in (point-min) (point-max)))
3916 (when (eq (overlay-get o 'org-type) 'org-blocked-todo)
3917 (delete-overlay o)))
3918 (save-excursion
3919 (let ((inhibit-read-only t))
3920 (goto-char (point-min))
3921 (while (let ((pos (text-property-not-all
3922 (point) (point-max) 'org-todo-blocked nil)))
3923 (when pos (goto-char pos)))
3924 (let* ((invisible (eq (org-get-at-bol 'org-todo-blocked) 'invisible))
3925 (ov (make-overlay (if invisible
3926 (line-end-position 0)
3927 (line-beginning-position))
3928 (line-end-position))))
3929 (if invisible
3930 (overlay-put ov 'invisible t)
3931 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3932 (overlay-put ov 'org-type 'org-blocked-todo))
3933 (forward-line))))
3934 (when (called-interactively-p 'interactive)
3935 (message "Dim or hide blocked tasks...done")))
3937 (defun org-agenda--mark-blocked-entry (entry)
3938 "For ENTRY a string with the text property `org-hd-marker', if
3939 the header at `org-hd-marker' is blocked according to
3940 `org-entry-blocked-p', then if `org-agenda-dim-blocked-tasks' is
3941 'invisible and the header is not blocked by checkboxes, set the
3942 text property `org-todo-blocked' to 'invisible, otherwise set it
3943 to t."
3944 (when (get-text-property 0 'todo-state entry)
3945 (let ((entry-marker (get-text-property 0 'org-hd-marker entry))
3946 (org-blocked-by-checkboxes nil)
3947 ;; Necessary so that `org-entry-blocked-p' does not change
3948 ;; the buffer.
3949 (org-depend-tag-blocked nil))
3950 (when entry-marker
3951 (let ((blocked
3952 (with-current-buffer (marker-buffer entry-marker)
3953 (save-excursion
3954 (goto-char entry-marker)
3955 (org-entry-blocked-p)))))
3956 (when blocked
3957 (let ((really-invisible
3958 (and (not org-blocked-by-checkboxes)
3959 (eq org-agenda-dim-blocked-tasks 'invisible))))
3960 (put-text-property
3961 0 (length entry) 'org-todo-blocked
3962 (if really-invisible 'invisible t)
3963 entry)))))))
3964 entry)
3966 (defvar org-agenda-skip-function nil
3967 "Function to be called at each match during agenda construction.
3968 If this function returns nil, the current match should not be skipped.
3969 Otherwise, the function must return a position from where the search
3970 should be continued.
3971 This may also be a Lisp form, it will be evaluated.
3972 Never set this variable using `setq' or so, because then it will apply
3973 to all future agenda commands. If you do want a global skipping condition,
3974 use the option `org-agenda-skip-function-global' instead.
3975 The correct usage for `org-agenda-skip-function' is to bind it with
3976 `let' to scope it dynamically into the agenda-constructing command.
3977 A good way to set it is through options in `org-agenda-custom-commands'.")
3979 (defun org-agenda-skip ()
3980 "Throw to `:skip' in places that should be skipped.
3981 Also moves point to the end of the skipped region, so that search can
3982 continue from there."
3983 (let ((p (point-at-bol)) to)
3984 (when (or
3985 (save-excursion (goto-char p) (looking-at comment-start-skip))
3986 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3987 (get-text-property p :org-archived)
3988 (org-end-of-subtree t))
3989 (and org-agenda-skip-comment-trees
3990 (get-text-property p :org-comment)
3991 (org-end-of-subtree t))
3992 (and (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3993 (org-agenda-skip-eval org-agenda-skip-function)))
3994 (goto-char to))
3995 (org-in-src-block-p t))
3996 (throw :skip t))))
3998 (defun org-agenda-skip-eval (form)
3999 "If FORM is a function or a list, call (or eval) it and return the result.
4000 `save-excursion' and `save-match-data' are wrapped around the call, so point
4001 and match data are returned to the previous state no matter what these
4002 functions do."
4003 (let (fp)
4004 (and form
4005 (or (setq fp (functionp form))
4006 (consp form))
4007 (save-excursion
4008 (save-match-data
4009 (if fp
4010 (funcall form)
4011 (eval form)))))))
4013 (defvar org-agenda-markers nil
4014 "List of all currently active markers created by `org-agenda'.")
4015 (defvar org-agenda-last-marker-time (float-time)
4016 "Creation time of the last agenda marker.")
4018 (defun org-agenda-new-marker (&optional pos)
4019 "Return a new agenda marker.
4020 Maker is at point, or at POS if non-nil. Org mode keeps a list of
4021 these markers and resets them when they are no longer in use."
4022 (let ((m (copy-marker (or pos (point)) t)))
4023 (setq org-agenda-last-marker-time (float-time))
4024 (if org-agenda-buffer
4025 (with-current-buffer org-agenda-buffer
4026 (push m org-agenda-markers))
4027 (push m org-agenda-markers))
4030 (defun org-agenda-reset-markers ()
4031 "Reset markers created by `org-agenda'."
4032 (while org-agenda-markers
4033 (move-marker (pop org-agenda-markers) nil)))
4035 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
4036 "Save relative positions of markers in region.
4037 This check for agenda markers in all agenda buffers currently active."
4038 (dolist (buf (buffer-list))
4039 (with-current-buffer buf
4040 (when (eq major-mode 'org-agenda-mode)
4041 (mapc (lambda (m) (org-check-and-save-marker m beg end))
4042 org-agenda-markers)))))
4044 ;;; Entry text mode
4046 (defun org-agenda-entry-text-show-here ()
4047 "Add some text from the entry as context to the current line."
4048 (let (m txt o)
4049 (setq m (org-get-at-bol 'org-hd-marker))
4050 (unless (marker-buffer m)
4051 (error "No marker points to an entry here"))
4052 (setq txt (concat "\n" (org-no-properties
4053 (org-agenda-get-some-entry-text
4054 m org-agenda-entry-text-maxlines
4055 org-agenda-entry-text-leaders))))
4056 (when (string-match "\\S-" txt)
4057 (setq o (make-overlay (point-at-bol) (point-at-eol)))
4058 (overlay-put o 'evaporate t)
4059 (overlay-put o 'org-overlay-type 'agenda-entry-content)
4060 (overlay-put o 'after-string txt))))
4062 (defun org-agenda-entry-text-show ()
4063 "Add entry context for all agenda lines."
4064 (interactive)
4065 (save-excursion
4066 (goto-char (point-max))
4067 (beginning-of-line 1)
4068 (while (not (bobp))
4069 (when (org-get-at-bol 'org-hd-marker)
4070 (org-agenda-entry-text-show-here))
4071 (beginning-of-line 0))))
4073 (defun org-agenda-entry-text-hide ()
4074 "Remove any shown entry context."
4075 (delq nil
4076 (mapcar (lambda (o)
4077 (if (eq (overlay-get o 'org-overlay-type)
4078 'agenda-entry-content)
4079 (progn (delete-overlay o) t)))
4080 (overlays-in (point-min) (point-max)))))
4082 (defun org-agenda-get-day-face (date)
4083 "Return the face DATE should be displayed with."
4084 (cond ((and (functionp org-agenda-day-face-function)
4085 (funcall org-agenda-day-face-function date)))
4086 ((org-agenda-today-p date) 'org-agenda-date-today)
4087 ((memq (calendar-day-of-week date) org-agenda-weekend-days)
4088 'org-agenda-date-weekend)
4089 (t 'org-agenda-date)))
4091 (defvar org-agenda-show-log-scoped)
4093 ;;; Agenda Daily/Weekly
4095 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4096 "Start day for the agenda view.
4097 Custom commands can set this variable in the options section.
4098 This is usually a string like \"2007-11-01\", \"+2d\" or any other
4099 input allowed when reading a date through the Org calendar.
4100 See the docstring of `org-read-date' for details.")
4101 (defvar org-starting-day nil) ; local variable in the agenda buffer
4102 (defvar org-arg-loc nil) ; local variable
4104 (defvar org-agenda-buffer-tmp-name nil)
4105 ;;;###autoload
4106 (defun org-agenda-list (&optional arg start-day span with-hour)
4107 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4108 The view will be for the current day or week, but from the overview buffer
4109 you will be able to go to other days/weeks.
4111 With a numeric prefix argument in an interactive call, the agenda will
4112 span ARG days. Lisp programs should instead specify SPAN to change
4113 the number of days. SPAN defaults to `org-agenda-span'.
4115 START-DAY defaults to TODAY, or to the most recent match for the weekday
4116 given in `org-agenda-start-on-weekday'.
4118 When WITH-HOUR is non-nil, only include scheduled and deadline
4119 items if they have an hour specification like [h]h:mm."
4120 (interactive "P")
4121 (if org-agenda-overriding-arguments
4122 (setq arg (car org-agenda-overriding-arguments)
4123 start-day (nth 1 org-agenda-overriding-arguments)
4124 span (nth 2 org-agenda-overriding-arguments)))
4125 (if (and (integerp arg) (> arg 0))
4126 (setq span arg arg nil))
4127 (catch 'exit
4128 (setq org-agenda-buffer-name
4129 (or org-agenda-buffer-tmp-name
4130 (and org-agenda-doing-sticky-redo org-agenda-buffer-name)
4131 (if org-agenda-sticky
4132 (cond ((and org-keys (stringp org-match))
4133 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4134 (org-keys
4135 (format "*Org Agenda(%s)*" org-keys))
4136 (t "*Org Agenda(a)*")))
4137 "*Org Agenda*"))
4138 (org-agenda-prepare "Day/Week")
4139 (setq start-day (or start-day org-agenda-start-day))
4140 (if (stringp start-day)
4141 ;; Convert to an absolute day number
4142 (setq start-day (time-to-days (org-read-date nil t start-day))))
4143 (org-compile-prefix-format 'agenda)
4144 (org-set-sorting-strategy 'agenda)
4145 (let* ((span (org-agenda-ndays-to-span (or span org-agenda-span)))
4146 (today (org-today))
4147 (sd (or start-day today))
4148 (ndays (org-agenda-span-to-ndays span sd))
4149 (org-agenda-start-on-weekday
4150 (if (or (eq ndays 7) (eq ndays 14))
4151 org-agenda-start-on-weekday))
4152 (thefiles (org-agenda-files nil 'ifmode))
4153 (files thefiles)
4154 (start (if (or (null org-agenda-start-on-weekday)
4155 (< ndays 7))
4157 (let* ((nt (calendar-day-of-week
4158 (calendar-gregorian-from-absolute sd)))
4159 (n1 org-agenda-start-on-weekday)
4160 (d (- nt n1)))
4161 (- sd (+ (if (< d 0) 7 0) d)))))
4162 (day-numbers (list start))
4163 (day-cnt 0)
4164 (inhibit-redisplay (not debug-on-error))
4165 (org-agenda-show-log-scoped org-agenda-show-log)
4166 s e rtn rtnall file date d start-pos end-pos todayp
4167 clocktable-start clocktable-end filter)
4168 (setq org-agenda-redo-command
4169 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4170 (dotimes (n (1- ndays))
4171 (push (1+ (car day-numbers)) day-numbers))
4172 (setq day-numbers (nreverse day-numbers))
4173 (setq clocktable-start (car day-numbers)
4174 clocktable-end (1+ (or (org-last day-numbers) 0)))
4175 (setq-local org-starting-day (car day-numbers))
4176 (setq-local org-arg-loc arg)
4177 (setq-local org-agenda-current-span (org-agenda-ndays-to-span span))
4178 (unless org-agenda-compact-blocks
4179 (let* ((d1 (car day-numbers))
4180 (d2 (org-last day-numbers))
4181 (w1 (org-days-to-iso-week d1))
4182 (w2 (org-days-to-iso-week d2)))
4183 (setq s (point))
4184 (org-agenda--insert-overriding-header
4185 (concat (org-agenda-span-name span)
4186 "-agenda"
4187 (cond ((<= 350 (- d2 d1)) "")
4188 ((= w1 w2) (format " (W%02d)" w1))
4189 (t (format " (W%02d-W%02d)" w1 w2)))
4190 ":\n")))
4191 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4192 'org-date-line t))
4193 (org-agenda-mark-header-line s))
4194 (while (setq d (pop day-numbers))
4195 (setq date (calendar-gregorian-from-absolute d)
4196 s (point))
4197 (if (or (setq todayp (= d today))
4198 (and (not start-pos) (= d sd)))
4199 (setq start-pos (point))
4200 (if (and start-pos (not end-pos))
4201 (setq end-pos (point))))
4202 (setq files thefiles
4203 rtnall nil)
4204 (while (setq file (pop files))
4205 (catch 'nextfile
4206 (org-check-agenda-file file)
4207 (let ((org-agenda-entry-types org-agenda-entry-types))
4208 ;; Starred types override non-starred equivalents
4209 (when (member :deadline* org-agenda-entry-types)
4210 (setq org-agenda-entry-types
4211 (delq :deadline org-agenda-entry-types)))
4212 (when (member :scheduled* org-agenda-entry-types)
4213 (setq org-agenda-entry-types
4214 (delq :scheduled org-agenda-entry-types)))
4215 ;; Honor with-hour
4216 (when with-hour
4217 (when (member :deadline org-agenda-entry-types)
4218 (setq org-agenda-entry-types
4219 (delq :deadline org-agenda-entry-types))
4220 (push :deadline* org-agenda-entry-types))
4221 (when (member :scheduled org-agenda-entry-types)
4222 (setq org-agenda-entry-types
4223 (delq :scheduled org-agenda-entry-types))
4224 (push :scheduled* org-agenda-entry-types)))
4225 (unless org-agenda-include-deadlines
4226 (setq org-agenda-entry-types
4227 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4228 (cond
4229 ((memq org-agenda-show-log-scoped '(only clockcheck))
4230 (setq rtn (org-agenda-get-day-entries
4231 file date :closed)))
4232 (org-agenda-show-log-scoped
4233 (setq rtn (apply 'org-agenda-get-day-entries
4234 file date
4235 (append '(:closed) org-agenda-entry-types))))
4237 (setq rtn (apply 'org-agenda-get-day-entries
4238 file date
4239 org-agenda-entry-types)))))
4240 (setq rtnall (append rtnall rtn)))) ;; all entries
4241 (if org-agenda-include-diary
4242 (let ((org-agenda-search-headline-for-time t))
4243 (require 'diary-lib)
4244 (setq rtn (org-get-entries-from-diary date))
4245 (setq rtnall (append rtnall rtn))))
4246 (if (or rtnall org-agenda-show-all-dates)
4247 (progn
4248 (setq day-cnt (1+ day-cnt))
4249 (insert
4250 (if (stringp org-agenda-format-date)
4251 (format-time-string org-agenda-format-date
4252 (org-time-from-absolute date))
4253 (funcall org-agenda-format-date date))
4254 "\n")
4255 (put-text-property s (1- (point)) 'face
4256 (org-agenda-get-day-face date))
4257 (put-text-property s (1- (point)) 'org-date-line t)
4258 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4259 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4260 (when todayp
4261 (put-text-property s (1- (point)) 'org-today t))
4262 (setq rtnall
4263 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4264 (if rtnall (insert ;; all entries
4265 (org-agenda-finalize-entries rtnall 'agenda)
4266 "\n"))
4267 (put-text-property s (1- (point)) 'day d)
4268 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4269 (when (and org-agenda-clockreport-mode clocktable-start)
4270 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4271 ;; the above line is to ensure the restricted range!
4272 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4273 tbl)
4274 (setq p (org-plist-delete p :block))
4275 (setq p (plist-put p :tstart clocktable-start))
4276 (setq p (plist-put p :tend clocktable-end))
4277 (setq p (plist-put p :scope 'agenda))
4278 (setq tbl (apply 'org-clock-get-clocktable p))
4279 (insert tbl)))
4280 (goto-char (point-min))
4281 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4282 (unless (and (pos-visible-in-window-p (point-min))
4283 (pos-visible-in-window-p (point-max)))
4284 (goto-char (1- (point-max)))
4285 (recenter -1)
4286 (if (not (pos-visible-in-window-p (or start-pos 1)))
4287 (progn
4288 (goto-char (or start-pos 1))
4289 (recenter 1))))
4290 (goto-char (or start-pos 1))
4291 (add-text-properties (point-min) (point-max)
4292 `(org-agenda-type agenda
4293 org-last-args (,arg ,start-day ,span)
4294 org-redo-cmd ,org-agenda-redo-command
4295 org-series-cmd ,org-cmd))
4296 (if (eq org-agenda-show-log-scoped 'clockcheck)
4297 (org-agenda-show-clocking-issues))
4298 (org-agenda-finalize)
4299 (setq buffer-read-only t)
4300 (message ""))))
4302 (defun org-agenda-ndays-to-span (n)
4303 "Return a span symbol for a span of N days, or N if none matches."
4304 (cond ((symbolp n) n)
4305 ((= n 1) 'day)
4306 ((= n 7) 'week)
4307 ((= n 14) 'fortnight)
4308 (t n)))
4310 (defun org-agenda-span-to-ndays (span &optional start-day)
4311 "Return ndays from SPAN, possibly starting at START-DAY.
4312 START-DAY is an absolute time value."
4313 (cond ((numberp span) span)
4314 ((eq span 'day) 1)
4315 ((eq span 'week) 7)
4316 ((eq span 'fortnight) 14)
4317 ((eq span 'month)
4318 (let ((date (calendar-gregorian-from-absolute start-day)))
4319 (calendar-last-day-of-month (car date) (cl-caddr date))))
4320 ((eq span 'year)
4321 (let ((date (calendar-gregorian-from-absolute start-day)))
4322 (if (calendar-leap-year-p (cl-caddr date)) 366 365)))))
4324 (defun org-agenda-span-name (span)
4325 "Return a SPAN name."
4326 (if (null span)
4328 (if (symbolp span)
4329 (capitalize (symbol-name span))
4330 (format "%d days" span))))
4332 ;;; Agenda word search
4334 (defvar org-agenda-search-history nil)
4336 (defvar org-search-syntax-table nil
4337 "Special syntax table for Org search.
4338 In this table, we have single quotes not as word constituents, to
4339 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4341 (defvar org-mode-syntax-table) ; From org.el
4342 (defun org-search-syntax-table ()
4343 (unless org-search-syntax-table
4344 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4345 (modify-syntax-entry ?' "." org-search-syntax-table)
4346 (modify-syntax-entry ?` "." org-search-syntax-table))
4347 org-search-syntax-table)
4349 (defvar org-agenda-last-search-view-search-was-boolean nil)
4351 ;;;###autoload
4352 (defun org-search-view (&optional todo-only string edit-at)
4353 "Show all entries that contain a phrase or words or regular expressions.
4355 With optional prefix argument TODO-ONLY, only consider entries that are
4356 TODO entries. The argument STRING can be used to pass a default search
4357 string into this function. If EDIT-AT is non-nil, it means that the
4358 user should get a chance to edit this string, with cursor at position
4359 EDIT-AT.
4361 The search string can be viewed either as a phrase that should be found as
4362 is, or it can be broken into a number of snippets, each of which must match
4363 in a Boolean way to select an entry. The default depends on the variable
4364 `org-agenda-search-view-always-boolean'.
4365 Even if this is turned off (the default) you can always switch to
4366 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4368 The default is a direct search of the whole phrase, where each space in
4369 the search string can expand to an arbitrary amount of whitespace,
4370 including newlines.
4372 If using a Boolean search, the search string is split on whitespace and
4373 each snippet is searched separately, with logical AND to select an entry.
4374 Words prefixed with a minus must *not* occur in the entry. Words without
4375 a prefix or prefixed with a plus must occur in the entry. Matching is
4376 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4377 match whole words, not parts of a word) if
4378 `org-agenda-search-view-force-full-words' is set (default is nil).
4380 Boolean search snippets enclosed by curly braces are interpreted as
4381 regular expressions that must or (when preceded with \"-\") must not
4382 match in the entry. Snippets enclosed into double quotes will be taken
4383 as a whole, to include whitespace.
4385 - If the search string starts with an asterisk, search only in headlines.
4386 - If (possibly after the leading star) the search string starts with an
4387 exclamation mark, this also means to look at TODO entries only, an effect
4388 that can also be achieved with a prefix argument.
4389 - If (possibly after star and exclamation mark) the search string starts
4390 with a colon, this will mean that the (non-regexp) snippets of the
4391 Boolean search must match as full words.
4393 This command searches the agenda files, and in addition the files
4394 listed in `org-agenda-text-search-extra-files' unless a restriction lock
4395 is active."
4396 (interactive "P")
4397 (if org-agenda-overriding-arguments
4398 (setq todo-only (car org-agenda-overriding-arguments)
4399 string (nth 1 org-agenda-overriding-arguments)
4400 edit-at (nth 2 org-agenda-overriding-arguments)))
4401 (let* ((props (list 'face nil
4402 'done-face 'org-agenda-done
4403 'org-not-done-regexp org-not-done-regexp
4404 'org-todo-regexp org-todo-regexp
4405 'org-complex-heading-regexp org-complex-heading-regexp
4406 'mouse-face 'highlight
4407 'help-echo (format "mouse-2 or RET jump to location")))
4408 (full-words org-agenda-search-view-force-full-words)
4409 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4410 regexp rtn rtnall files file pos inherited-tags
4411 marker category level tags c neg re boolean
4412 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4413 (unless (and (not edit-at)
4414 (stringp string)
4415 (string-match "\\S-" string))
4416 (setq string (read-string
4417 (if org-agenda-search-view-always-boolean
4418 "[+-]Word/{Regexp} ...: "
4419 "Phrase or [+-]Word/{Regexp} ...: ")
4420 (cond
4421 ((integerp edit-at) (cons string edit-at))
4422 (edit-at string))
4423 'org-agenda-search-history)))
4424 (catch 'exit
4425 (if org-agenda-sticky
4426 (setq org-agenda-buffer-name
4427 (if (stringp string)
4428 (format "*Org Agenda(%s:%s)*"
4429 (or org-keys (or (and todo-only "S") "s")) string)
4430 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4431 (org-agenda-prepare "SEARCH")
4432 (org-compile-prefix-format 'search)
4433 (org-set-sorting-strategy 'search)
4434 (setq org-agenda-redo-command
4435 (list 'org-search-view (if todo-only t nil)
4436 (list 'if 'current-prefix-arg nil string)))
4437 (setq org-agenda-query-string string)
4438 (if (equal (string-to-char string) ?*)
4439 (setq hdl-only t
4440 words (substring string 1))
4441 (setq words string))
4442 (when (equal (string-to-char words) ?!)
4443 (setq todo-only t
4444 words (substring words 1)))
4445 (when (equal (string-to-char words) ?:)
4446 (setq full-words t
4447 words (substring words 1)))
4448 (if (or org-agenda-search-view-always-boolean
4449 (member (string-to-char words) '(?- ?+ ?\{)))
4450 (setq boolean t))
4451 (setq words (split-string words))
4452 (let (www w)
4453 (while (setq w (pop words))
4454 (while (and (string-match "\\\\\\'" w) words)
4455 (setq w (concat (substring w 0 -1) " " (pop words))))
4456 (push w www))
4457 (setq words (nreverse www) www nil)
4458 (while (setq w (pop words))
4459 (when (and (string-match "\\`[-+]?{" w)
4460 (not (string-match "}\\'" w)))
4461 (while (and words (not (string-match "}\\'" (car words))))
4462 (setq w (concat w " " (pop words))))
4463 (setq w (concat w " " (pop words))))
4464 (push w www))
4465 (setq words (nreverse www)))
4466 (setq org-agenda-last-search-view-search-was-boolean boolean)
4467 (when boolean
4468 (let (wds w)
4469 (while (setq w (pop words))
4470 (if (or (equal (substring w 0 1) "\"")
4471 (and (> (length w) 1)
4472 (member (substring w 0 1) '("+" "-"))
4473 (equal (substring w 1 2) "\"")))
4474 (while (and words (not (equal (substring w -1) "\"")))
4475 (setq w (concat w " " (pop words)))))
4476 (and (string-match "\\`\\([-+]?\\)\"" w)
4477 (setq w (replace-match "\\1" nil nil w)))
4478 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4479 (push w wds))
4480 (setq words (nreverse wds))))
4481 (if boolean
4482 (mapc (lambda (w)
4483 (setq c (string-to-char w))
4484 (if (equal c ?-)
4485 (setq neg t w (substring w 1))
4486 (if (equal c ?+)
4487 (setq neg nil w (substring w 1))
4488 (setq neg nil)))
4489 (if (string-match "\\`{.*}\\'" w)
4490 (setq re (substring w 1 -1))
4491 (if full-words
4492 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4493 (setq re (regexp-quote (downcase w)))))
4494 (if neg (push re regexps-) (push re regexps+)))
4495 words)
4496 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4497 regexps+))
4498 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4499 (if (not regexps+)
4500 (setq regexp org-outline-regexp-bol)
4501 (setq regexp (pop regexps+))
4502 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4503 regexp))))
4504 (setq files (org-agenda-files nil 'ifmode))
4505 ;; Add `org-agenda-text-search-extra-files' unless there is some
4506 ;; restriction.
4507 (unless (get 'org-agenda-files 'org-restrict)
4508 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4509 (pop org-agenda-text-search-extra-files)
4510 (setq files (org-add-archive-files files))))
4511 ;; Uniquify files. However, let `org-check-agenda-file' handle
4512 ;; non-existent ones.
4513 (setq files (cl-remove-duplicates
4514 (append files org-agenda-text-search-extra-files)
4515 :test (lambda (a b)
4516 (and (file-exists-p a)
4517 (file-exists-p b)
4518 (file-equal-p a b))))
4519 rtnall nil)
4520 (while (setq file (pop files))
4521 (setq ee nil)
4522 (catch 'nextfile
4523 (org-check-agenda-file file)
4524 (setq buffer (if (file-exists-p file)
4525 (org-get-agenda-file-buffer file)
4526 (error "No such file %s" file)))
4527 (if (not buffer)
4528 ;; If file does not exist, make sure an error message is sent
4529 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4530 file))))
4531 (with-current-buffer buffer
4532 (with-syntax-table (org-search-syntax-table)
4533 (unless (derived-mode-p 'org-mode)
4534 (error "Agenda file %s is not in Org mode" file))
4535 (let ((case-fold-search t))
4536 (save-excursion
4537 (save-restriction
4538 (if (eq buffer org-agenda-restrict)
4539 (narrow-to-region org-agenda-restrict-begin
4540 org-agenda-restrict-end)
4541 (widen))
4542 (goto-char (point-min))
4543 (unless (or (org-at-heading-p)
4544 (outline-next-heading))
4545 (throw 'nextfile t))
4546 (goto-char (max (point-min) (1- (point))))
4547 (while (re-search-forward regexp nil t)
4548 (org-back-to-heading t)
4549 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4550 (> (org-reduced-level (org-outline-level))
4551 org-agenda-search-view-max-outline-level)
4552 (forward-line -1)
4553 (org-back-to-heading t)))
4554 (skip-chars-forward "* ")
4555 (setq beg (point-at-bol)
4556 beg1 (point)
4557 end (progn
4558 (outline-next-heading)
4559 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4560 (> (org-reduced-level (org-outline-level))
4561 org-agenda-search-view-max-outline-level)
4562 (forward-line 1)
4563 (outline-next-heading)))
4564 (point)))
4566 (catch :skip
4567 (goto-char beg)
4568 (org-agenda-skip)
4569 (setq str (buffer-substring-no-properties
4570 (point-at-bol)
4571 (if hdl-only (point-at-eol) end)))
4572 (mapc (lambda (wr) (when (string-match wr str)
4573 (goto-char (1- end))
4574 (throw :skip t)))
4575 regexps-)
4576 (mapc (lambda (wr) (unless (string-match wr str)
4577 (goto-char (1- end))
4578 (throw :skip t)))
4579 (if todo-only
4580 (cons (concat "^\\*+[ \t]+"
4581 org-not-done-regexp)
4582 regexps+)
4583 regexps+))
4584 (goto-char beg)
4585 (setq marker (org-agenda-new-marker (point))
4586 category (org-get-category)
4587 level (make-string (org-reduced-level (org-outline-level)) ? )
4588 inherited-tags
4589 (or (eq org-agenda-show-inherited-tags 'always)
4590 (and (listp org-agenda-show-inherited-tags)
4591 (memq 'todo org-agenda-show-inherited-tags))
4592 (and (eq org-agenda-show-inherited-tags t)
4593 (or (eq org-agenda-use-tag-inheritance t)
4594 (memq 'todo org-agenda-use-tag-inheritance))))
4595 tags (org-get-tags-at nil (not inherited-tags))
4596 txt (org-agenda-format-item
4598 (buffer-substring-no-properties
4599 beg1 (point-at-eol))
4600 level category tags t))
4601 (org-add-props txt props
4602 'org-marker marker 'org-hd-marker marker
4603 'org-todo-regexp org-todo-regexp
4604 'level level
4605 'org-complex-heading-regexp org-complex-heading-regexp
4606 'priority 1000
4607 'type "search")
4608 (push txt ee)
4609 (goto-char (1- end))))))))))
4610 (setq rtn (nreverse ee))
4611 (setq rtnall (append rtnall rtn)))
4612 (org-agenda--insert-overriding-header
4613 (with-temp-buffer
4614 (insert "Search words: ")
4615 (add-text-properties (point-min) (1- (point))
4616 (list 'face 'org-agenda-structure))
4617 (setq pos (point))
4618 (insert string "\n")
4619 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4620 (setq pos (point))
4621 (unless org-agenda-multi
4622 (insert (substitute-command-keys "\\<org-agenda-mode-map>\
4623 Press `\\[org-agenda-manipulate-query-add]', \
4624 `\\[org-agenda-manipulate-query-subtract]' to add/sub word, \
4625 `\\[org-agenda-manipulate-query-add-re]', \
4626 `\\[org-agenda-manipulate-query-subtract-re]' to add/sub regexp, \
4627 `\\[universal-argument] \\[org-agenda-redo]' to edit\n"))
4628 (add-text-properties pos (1- (point))
4629 (list 'face 'org-agenda-structure)))
4630 (buffer-string)))
4631 (org-agenda-mark-header-line (point-min))
4632 (when rtnall
4633 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4634 (goto-char (point-min))
4635 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4636 (add-text-properties (point-min) (point-max)
4637 `(org-agenda-type search
4638 org-last-args (,todo-only ,string ,edit-at)
4639 org-redo-cmd ,org-agenda-redo-command
4640 org-series-cmd ,org-cmd))
4641 (org-agenda-finalize)
4642 (setq buffer-read-only t))))
4644 ;;; Agenda TODO list
4646 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4647 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4648 (concat
4649 (if (or (equal keywords "ALL") (not keywords))
4650 (propertize "ALL" 'face 'warning)
4651 (mapconcat
4652 (lambda (kw)
4653 (propertize kw 'face (org-get-todo-face kw)))
4654 (org-split-string keywords "|")
4655 "|"))
4656 "\n"))
4658 (defvar org-select-this-todo-keyword nil)
4659 (defvar org-last-arg nil)
4661 ;;;###autoload
4662 (defun org-todo-list (&optional arg)
4663 "Show all (not done) TODO entries from all agenda file in a single list.
4664 The prefix arg can be used to select a specific TODO keyword and limit
4665 the list to these. When using `\\[universal-argument]', you will be prompted
4666 for a keyword. A numeric prefix directly selects the Nth keyword in
4667 `org-todo-keywords-1'."
4668 (interactive "P")
4669 (if org-agenda-overriding-arguments
4670 (setq arg org-agenda-overriding-arguments))
4671 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4672 (let* ((today (org-today))
4673 (date (calendar-gregorian-from-absolute today))
4674 (kwds org-todo-keywords-for-agenda)
4675 (completion-ignore-case t)
4676 (org-select-this-todo-keyword
4677 (if (stringp arg) arg
4678 (and arg (integerp arg) (> arg 0)
4679 (nth (1- arg) kwds))))
4680 rtn rtnall files file pos)
4681 (when (equal arg '(4))
4682 (setq org-select-this-todo-keyword
4683 (completing-read "Keyword (or KWD1|K2D2|...): "
4684 (mapcar #'list kwds) nil nil)))
4685 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4686 (catch 'exit
4687 (if org-agenda-sticky
4688 (setq org-agenda-buffer-name
4689 (if (stringp org-select-this-todo-keyword)
4690 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4691 org-select-this-todo-keyword)
4692 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4693 (org-agenda-prepare "TODO")
4694 (org-compile-prefix-format 'todo)
4695 (org-set-sorting-strategy 'todo)
4696 (setq org-agenda-redo-command
4697 `(org-todo-list (or (and (numberp current-prefix-arg)
4698 current-prefix-arg)
4699 ,org-select-this-todo-keyword
4700 current-prefix-arg ,arg)))
4701 (setq files (org-agenda-files nil 'ifmode)
4702 rtnall nil)
4703 (while (setq file (pop files))
4704 (catch 'nextfile
4705 (org-check-agenda-file file)
4706 (setq rtn (org-agenda-get-day-entries file date :todo))
4707 (setq rtnall (append rtnall rtn))))
4708 (org-agenda--insert-overriding-header
4709 (with-temp-buffer
4710 (insert "Global list of TODO items of type: ")
4711 (add-text-properties (point-min) (1- (point))
4712 (list 'face 'org-agenda-structure
4713 'short-heading
4714 (concat "ToDo: "
4715 (or org-select-this-todo-keyword "ALL"))))
4716 (org-agenda-mark-header-line (point-min))
4717 (insert (org-agenda-propertize-selected-todo-keywords
4718 org-select-this-todo-keyword))
4719 (setq pos (point))
4720 (unless org-agenda-multi
4721 (insert (substitute-command-keys "Available with \
4722 \\<org-agenda-mode-map>`N \\[org-agenda-redo]': (0)[ALL]"))
4723 (let ((n 0))
4724 (dolist (k kwds)
4725 (let ((s (format "(%d)%s" (cl-incf n) k)))
4726 (when (> (+ (current-column) (string-width s) 1) (window-width))
4727 (insert "\n "))
4728 (insert " " s))))
4729 (insert "\n"))
4730 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure))
4731 (buffer-string)))
4732 (org-agenda-mark-header-line (point-min))
4733 (when rtnall
4734 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4735 (goto-char (point-min))
4736 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4737 (add-text-properties (point-min) (point-max)
4738 `(org-agenda-type todo
4739 org-last-args ,arg
4740 org-redo-cmd ,org-agenda-redo-command
4741 org-series-cmd ,org-cmd))
4742 (org-agenda-finalize)
4743 (setq buffer-read-only t))))
4745 ;;; Agenda tags match
4747 ;;;###autoload
4748 (defun org-tags-view (&optional todo-only match)
4749 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4750 The prefix arg TODO-ONLY limits the search to TODO entries."
4751 (interactive "P")
4752 (if org-agenda-overriding-arguments
4753 (setq todo-only (car org-agenda-overriding-arguments)
4754 match (nth 1 org-agenda-overriding-arguments)))
4755 (let* ((org-tags-match-list-sublevels
4756 org-tags-match-list-sublevels)
4757 (completion-ignore-case t)
4758 (org--matcher-tags-todo-only todo-only)
4759 rtn rtnall files file pos matcher
4760 buffer)
4761 (when (and (stringp match) (not (string-match "\\S-" match)))
4762 (setq match nil))
4763 (catch 'exit
4764 (if org-agenda-sticky
4765 (setq org-agenda-buffer-name
4766 (if (stringp match)
4767 (format "*Org Agenda(%s:%s)*"
4768 (or org-keys (or (and todo-only "M") "m")) match)
4769 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4770 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4771 ;; expanding tags within `org-make-tags-matcher'
4772 (org-agenda-prepare (concat "TAGS " match))
4773 (setq matcher (org-make-tags-matcher match)
4774 match (car matcher)
4775 matcher (cdr matcher))
4776 (org-compile-prefix-format 'tags)
4777 (org-set-sorting-strategy 'tags)
4778 (setq org-agenda-query-string match)
4779 (setq org-agenda-redo-command
4780 (list 'org-tags-view
4781 `(quote ,org--matcher-tags-todo-only)
4782 `(if current-prefix-arg nil ,org-agenda-query-string)))
4783 (setq files (org-agenda-files nil 'ifmode)
4784 rtnall nil)
4785 (while (setq file (pop files))
4786 (catch 'nextfile
4787 (org-check-agenda-file file)
4788 (setq buffer (if (file-exists-p file)
4789 (org-get-agenda-file-buffer file)
4790 (error "No such file %s" file)))
4791 (if (not buffer)
4792 ;; If file does not exist, error message to agenda
4793 (setq rtn (list
4794 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4795 rtnall (append rtnall rtn))
4796 (with-current-buffer buffer
4797 (unless (derived-mode-p 'org-mode)
4798 (error "Agenda file %s is not in Org mode" file))
4799 (save-excursion
4800 (save-restriction
4801 (if (eq buffer org-agenda-restrict)
4802 (narrow-to-region org-agenda-restrict-begin
4803 org-agenda-restrict-end)
4804 (widen))
4805 (setq rtn (org-scan-tags 'agenda
4806 matcher
4807 org--matcher-tags-todo-only))
4808 (setq rtnall (append rtnall rtn))))))))
4809 (org-agenda--insert-overriding-header
4810 (with-temp-buffer
4811 (insert "Headlines with TAGS match: ")
4812 (add-text-properties (point-min) (1- (point))
4813 (list 'face 'org-agenda-structure
4814 'short-heading
4815 (concat "Match: " match)))
4816 (setq pos (point))
4817 (insert match "\n")
4818 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4819 (setq pos (point))
4820 (unless org-agenda-multi
4821 (insert (substitute-command-keys
4822 "Press \
4823 \\<org-agenda-mode-map>`\\[universal-argument] \\[org-agenda-redo]' \
4824 to search again with new search string\n")))
4825 (add-text-properties pos (1- (point))
4826 (list 'face 'org-agenda-structure))
4827 (buffer-string)))
4828 (org-agenda-mark-header-line (point-min))
4829 (when rtnall
4830 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4831 (goto-char (point-min))
4832 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4833 (add-text-properties
4834 (point-min) (point-max)
4835 `(org-agenda-type tags
4836 org-last-args (,org--matcher-tags-todo-only ,match)
4837 org-redo-cmd ,org-agenda-redo-command
4838 org-series-cmd ,org-cmd))
4839 (org-agenda-finalize)
4840 (setq buffer-read-only t))))
4842 ;;; Agenda Finding stuck projects
4844 (defvar org-agenda-skip-regexp nil
4845 "Regular expression used in skipping subtrees for the agenda.
4846 This is basically a temporary global variable that can be set and then
4847 used by user-defined selections using `org-agenda-skip-function'.")
4849 (defvar org-agenda-overriding-header nil
4850 "When set during agenda, todo and tags searches it replaces the header.
4851 If an empty string, no header will be inserted. If any other
4852 string, it will be inserted as a header. If nil, a header will
4853 be generated automatically according to the command. This
4854 variable should not be set directly, but custom commands can bind
4855 it in the options section.")
4857 (defun org-agenda-skip-entry-if (&rest conditions)
4858 "Skip entry if any of CONDITIONS is true.
4859 See `org-agenda-skip-if' for details."
4860 (org-agenda-skip-if nil conditions))
4862 (defun org-agenda-skip-subtree-if (&rest conditions)
4863 "Skip subtree if any of CONDITIONS is true.
4864 See `org-agenda-skip-if' for details."
4865 (org-agenda-skip-if t conditions))
4867 (defun org-agenda-skip-if (subtree conditions)
4868 "Checks current entity for CONDITIONS.
4869 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4870 the entry (i.e. the text before the next heading) is checked.
4872 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4873 from different tests. Valid conditions are:
4875 scheduled Check if there is a scheduled cookie
4876 notscheduled Check if there is no scheduled cookie
4877 deadline Check if there is a deadline
4878 notdeadline Check if there is no deadline
4879 timestamp Check if there is a timestamp (also deadline or scheduled)
4880 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4881 regexp Check if regexp matches
4882 notregexp Check if regexp does not match.
4883 todo Check if TODO keyword matches
4884 nottodo Check if TODO keyword does not match
4886 The regexp is taken from the conditions list, it must come right after
4887 the `regexp' or `notregexp' element.
4889 `todo' and `nottodo' accept as an argument a list of todo
4890 keywords, which may include \"*\" to match any todo keyword.
4892 (org-agenda-skip-entry-if \\='todo \\='(\"TODO\" \"WAITING\"))
4894 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4896 Instead of a list, a keyword class may be given. For example:
4898 (org-agenda-skip-entry-if \\='nottodo \\='done)
4900 would skip entries that haven't been marked with any of \"DONE\"
4901 keywords. Possible classes are: `todo', `done', `any'.
4903 If any of these conditions is met, this function returns the end point of
4904 the entity, causing the search to continue from there. This is a function
4905 that can be put into `org-agenda-skip-function' for the duration of a command."
4906 (org-back-to-heading t)
4907 (let* ((beg (point))
4908 (end (if subtree (save-excursion (org-end-of-subtree t) (point))
4909 (org-entry-end-position)))
4910 (planning-end (if subtree end (line-end-position 2)))
4912 (and
4913 (or (and (memq 'scheduled conditions)
4914 (re-search-forward org-scheduled-time-regexp planning-end t))
4915 (and (memq 'notscheduled conditions)
4916 (not
4917 (save-excursion
4918 (re-search-forward org-scheduled-time-regexp planning-end t))))
4919 (and (memq 'deadline conditions)
4920 (re-search-forward org-deadline-time-regexp planning-end t))
4921 (and (memq 'notdeadline conditions)
4922 (not
4923 (save-excursion
4924 (re-search-forward org-deadline-time-regexp planning-end t))))
4925 (and (memq 'timestamp conditions)
4926 (re-search-forward org-ts-regexp end t))
4927 (and (memq 'nottimestamp conditions)
4928 (not (save-excursion (re-search-forward org-ts-regexp end t))))
4929 (and (setq m (memq 'regexp conditions))
4930 (stringp (nth 1 m))
4931 (re-search-forward (nth 1 m) end t))
4932 (and (setq m (memq 'notregexp conditions))
4933 (stringp (nth 1 m))
4934 (not (save-excursion (re-search-forward (nth 1 m) end t))))
4935 (and (or
4936 (setq m (memq 'nottodo conditions))
4937 (setq m (memq 'todo-unblocked conditions))
4938 (setq m (memq 'nottodo-unblocked conditions))
4939 (setq m (memq 'todo conditions)))
4940 (org-agenda-skip-if-todo m end)))
4941 end)))
4943 (defun org-agenda-skip-if-todo (args end)
4944 "Helper function for `org-agenda-skip-if', do not use it directly.
4945 ARGS is a list with first element either `todo', `nottodo',
4946 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
4947 a list of TODO keywords, or a state symbol `todo' or `done' or
4948 `any'."
4949 (let ((todo-re
4950 (concat "^\\*+[ \t]+"
4951 (regexp-opt
4952 (pcase args
4953 (`(,_ todo)
4954 (org-delete-all org-done-keywords
4955 (copy-sequence org-todo-keywords-1)))
4956 (`(,_ done) org-done-keywords)
4957 (`(,_ any) org-todo-keywords-1)
4958 (`(,_ ,(pred atom))
4959 (error "Invalid TODO class or type: %S" args))
4960 (`(,_ ,(pred (member "*"))) org-todo-keywords-1)
4961 (`(,_ ,todo-list) todo-list))
4962 'words))))
4963 (pcase args
4964 (`(todo . ,_)
4965 (let (case-fold-search) (re-search-forward todo-re end t)))
4966 (`(nottodo . ,_)
4967 (not (let (case-fold-search) (re-search-forward todo-re end t))))
4968 (`(todo-unblocked . ,_)
4969 (catch :unblocked
4970 (while (let (case-fold-search) (re-search-forward todo-re end t))
4971 (when (org-entry-blocked-p) (throw :unblocked t)))
4972 nil))
4973 (`(nottodo-unblocked . ,_)
4974 (catch :unblocked
4975 (while (let (case-fold-search) (re-search-forward todo-re end t))
4976 (when (org-entry-blocked-p) (throw :unblocked nil)))
4978 (`(,type . ,_) (error "Unknown TODO skip type: %S" type)))))
4980 ;;;###autoload
4981 (defun org-agenda-list-stuck-projects (&rest ignore)
4982 "Create agenda view for projects that are stuck.
4983 Stuck projects are project that have no next actions. For the definitions
4984 of what a project is and how to check if it stuck, customize the variable
4985 `org-stuck-projects'."
4986 (interactive)
4987 (let* ((org-agenda-overriding-header
4988 (or org-agenda-overriding-header "List of stuck projects: "))
4989 (matcher (nth 0 org-stuck-projects))
4990 (todo (nth 1 org-stuck-projects))
4991 (tags (nth 2 org-stuck-projects))
4992 (gen-re (org-string-nw-p (nth 3 org-stuck-projects)))
4993 (todo-wds
4994 (if (not (member "*" todo)) todo
4995 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
4996 (org-delete-all org-done-keywords-for-agenda
4997 (copy-sequence org-todo-keywords-for-agenda))))
4998 (todo-re (and todo
4999 (format "^\\*+[ \t]+\\(%s\\)\\>"
5000 (mapconcat #'identity todo-wds "\\|"))))
5001 (tags-re (cond ((null tags) nil)
5002 ((member "*" tags)
5003 (eval-when-compile
5004 (concat org-outline-regexp-bol
5005 ".*:[[:alnum:]_@#%]+:[ \t]*$")))
5006 (tags (concat org-outline-regexp-bol
5007 ".*:\\("
5008 (mapconcat #'identity tags "\\|")
5009 "\\):[[:alnum:]_@#%:]*[ \t]*$"))
5010 (t nil)))
5011 (re-list (delq nil (list todo-re tags-re gen-re)))
5012 (skip-re
5013 (if (null re-list)
5014 (error "Missing information to identify unstuck projects")
5015 (mapconcat #'identity re-list "\\|")))
5016 (org-agenda-skip-function
5017 ;; Skip entry if `org-agenda-skip-regexp' matches anywhere
5018 ;; in the subtree.
5019 `(lambda ()
5020 (and (save-excursion
5021 (let ((case-fold-search nil))
5022 (re-search-forward
5023 ,skip-re (save-excursion (org-end-of-subtree t)) t)))
5024 (progn (outline-next-heading) (point))))))
5025 (org-tags-view nil matcher)
5026 (setq org-agenda-buffer-name (buffer-name))
5027 (with-current-buffer org-agenda-buffer-name
5028 (setq org-agenda-redo-command
5029 `(org-agenda-list-stuck-projects ,current-prefix-arg))
5030 (let ((inhibit-read-only t))
5031 (add-text-properties
5032 (point-min) (point-max)
5033 `(org-redo-cmd ,org-agenda-redo-command))))))
5035 ;;; Diary integration
5037 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
5038 (defvar diary-list-entries-hook)
5039 (defvar diary-time-regexp)
5040 (defun org-get-entries-from-diary (date)
5041 "Get the (Emacs Calendar) diary entries for DATE."
5042 (require 'diary-lib)
5043 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
5044 (diary-display-function 'diary-fancy-display)
5045 (pop-up-frames nil)
5046 (diary-list-entries-hook
5047 (cons 'org-diary-default-entry diary-list-entries-hook))
5048 (diary-file-name-prefix nil) ; turn this feature off
5049 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
5050 entries
5051 (org-disable-agenda-to-diary t))
5052 (save-excursion
5053 (save-window-excursion
5054 (funcall (if (fboundp 'diary-list-entries)
5055 'diary-list-entries 'list-diary-entries)
5056 date 1)))
5057 (if (not (get-buffer diary-fancy-buffer))
5058 (setq entries nil)
5059 (with-current-buffer diary-fancy-buffer
5060 (setq buffer-read-only nil)
5061 (if (zerop (buffer-size))
5062 ;; No entries
5063 (setq entries nil)
5064 ;; Omit the date and other unnecessary stuff
5065 (org-agenda-cleanup-fancy-diary)
5066 ;; Add prefix to each line and extend the text properties
5067 (if (zerop (buffer-size))
5068 (setq entries nil)
5069 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
5070 (setq entries
5071 (with-temp-buffer
5072 (insert entries) (goto-char (point-min))
5073 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5074 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5075 (replace-match (concat "; " (match-string 1)))))
5076 (buffer-string)))))
5077 (set-buffer-modified-p nil)
5078 (kill-buffer diary-fancy-buffer)))
5079 (when entries
5080 (setq entries (org-split-string entries "\n"))
5081 (setq entries
5082 (mapcar
5083 (lambda (x)
5084 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5085 ;; Extend the text properties to the beginning of the line
5086 (org-add-props x (text-properties-at (1- (length x)) x)
5087 'type "diary" 'date date 'face 'org-agenda-diary))
5088 entries)))))
5090 (defvar org-agenda-cleanup-fancy-diary-hook nil
5091 "Hook run when the fancy diary buffer is cleaned up.")
5093 (defun org-agenda-cleanup-fancy-diary ()
5094 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5095 This gets rid of the date, the underline under the date, and the
5096 dummy entry installed by Org mode to ensure non-empty diary for
5097 each date. It also removes lines that contain only whitespace."
5098 (goto-char (point-min))
5099 (if (looking-at ".*?:[ \t]*")
5100 (progn
5101 (replace-match "")
5102 (re-search-forward "\n=+$" nil t)
5103 (replace-match "")
5104 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5105 (re-search-forward "\n=+$" nil t)
5106 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5107 (goto-char (point-min))
5108 (while (re-search-forward "^ +\n" nil t)
5109 (replace-match ""))
5110 (goto-char (point-min))
5111 (if (re-search-forward "^Org mode dummy\n?" nil t)
5112 (replace-match ""))
5113 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5115 ;; Make sure entries from the diary have the right text properties.
5116 (eval-after-load "diary-lib"
5117 '(if (boundp 'diary-modify-entry-list-string-function)
5118 ;; We can rely on the hook, nothing to do
5120 ;; Hook not available, must use advice to make this work
5121 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5122 "Make the position visible."
5123 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5124 (stringp string)
5125 buffer-file-name)
5126 (setq string (org-modify-diary-entry-string string))))))
5128 (defun org-modify-diary-entry-string (string)
5129 "Add text properties to string, allowing Org to act on it."
5130 (org-add-props string nil
5131 'mouse-face 'highlight
5132 'help-echo (if buffer-file-name
5133 (format "mouse-2 or RET jump to diary file %s"
5134 (abbreviate-file-name buffer-file-name))
5136 'org-agenda-diary-link t
5137 'org-marker (org-agenda-new-marker (point-at-bol))))
5139 (defun org-diary-default-entry ()
5140 "Add a dummy entry to the diary.
5141 Needed to avoid empty dates which mess up holiday display."
5142 ;; Catch the error if dealing with the new add-to-diary-alist
5143 (when org-disable-agenda-to-diary
5144 (condition-case nil
5145 (org-add-to-diary-list original-date "Org mode dummy" "")
5146 (error
5147 (org-add-to-diary-list original-date "Org mode dummy" "" nil)))))
5149 (defun org-add-to-diary-list (&rest args)
5150 (if (fboundp 'diary-add-to-list)
5151 (apply 'diary-add-to-list args)
5152 (apply 'add-to-diary-list args)))
5154 (defvar org-diary-last-run-time nil)
5156 ;;;###autoload
5157 (defun org-diary (&rest args)
5158 "Return diary information from org files.
5159 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5160 It accesses org files and extracts information from those files to be
5161 listed in the diary. The function accepts arguments specifying what
5162 items should be listed. For a list of arguments allowed here, see the
5163 variable `org-agenda-entry-types'.
5165 The call in the diary file should look like this:
5167 &%%(org-diary) ~/path/to/some/orgfile.org
5169 Use a separate line for each org file to check. Or, if you omit the file name,
5170 all files listed in `org-agenda-files' will be checked automatically:
5172 &%%(org-diary)
5174 If you don't give any arguments (as in the example above), the default value
5175 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5176 So the example above may also be written as
5178 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5180 The function expects the lisp variables `entry' and `date' to be provided
5181 by the caller, because this is how the calendar works. Don't use this
5182 function from a program - use `org-agenda-get-day-entries' instead."
5183 (when (> (- (float-time)
5184 org-agenda-last-marker-time)
5186 ;; I am not sure if this works with sticky agendas, because the marker
5187 ;; list is then no longer a global variable.
5188 (org-agenda-reset-markers))
5189 (org-compile-prefix-format 'agenda)
5190 (org-set-sorting-strategy 'agenda)
5191 (setq args (or args org-agenda-entry-types))
5192 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5193 (list entry)
5194 (org-agenda-files t)))
5195 (time (float-time))
5196 file rtn results)
5197 (when (or (not org-diary-last-run-time)
5198 (> (- time
5199 org-diary-last-run-time)
5201 (org-agenda-prepare-buffers files))
5202 (setq org-diary-last-run-time time)
5203 ;; If this is called during org-agenda, don't return any entries to
5204 ;; the calendar. Org Agenda will list these entries itself.
5205 (if org-disable-agenda-to-diary (setq files nil))
5206 (while (setq file (pop files))
5207 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5208 (setq results (append results rtn)))
5209 (when results
5210 (setq results
5211 (mapcar (lambda (i) (replace-regexp-in-string
5212 org-bracket-link-regexp "\\3" i)) results))
5213 (concat (org-agenda-finalize-entries results) "\n"))))
5215 ;;; Agenda entry finders
5217 (defun org-agenda--timestamp-to-absolute (&rest args)
5218 "Call `org-time-string-to-absolute' with ARGS.
5219 However, throw `:skip' whenever an error is raised."
5220 (condition-case e
5221 (apply #'org-time-string-to-absolute args)
5222 (org-diary-sexp-no-match (throw :skip nil))
5223 (error
5224 (message "%s; Skipping entry" (error-message-string e))
5225 (throw :skip nil))))
5227 (defun org-agenda-get-day-entries (file date &rest args)
5228 "Does the work for `org-diary' and `org-agenda'.
5229 FILE is the path to a file to be checked for entries. DATE is date like
5230 the one returned by `calendar-current-date'. ARGS are symbols indicating
5231 which kind of entries should be extracted. For details about these, see
5232 the documentation of `org-diary'."
5233 (let* ((org-startup-folded nil)
5234 (org-startup-align-all-tables nil)
5235 (buffer (if (file-exists-p file) (org-get-agenda-file-buffer file)
5236 (error "No such file %s" file))))
5237 (if (not buffer)
5238 ;; If file does not exist, signal it in diary nonetheless.
5239 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5240 (with-current-buffer buffer
5241 (unless (derived-mode-p 'org-mode)
5242 (error "Agenda file %s is not in Org mode" file))
5243 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5244 (setf org-agenda-current-date date)
5245 (save-excursion
5246 (save-restriction
5247 (if (eq buffer org-agenda-restrict)
5248 (narrow-to-region org-agenda-restrict-begin
5249 org-agenda-restrict-end)
5250 (widen))
5251 ;; Rationalize ARGS. Also make sure `:deadline' comes
5252 ;; first in order to populate DEADLINES before passing it.
5254 ;; We use `delq' since `org-uniquify' duplicates ARGS,
5255 ;; guarding us from modifying `org-agenda-entry-types'.
5256 (setf args (org-uniquify (or args org-agenda-entry-types)))
5257 (when (and (memq :scheduled args) (memq :scheduled* args))
5258 (setf args (delq :scheduled* args)))
5259 (cond
5260 ((memq :deadline args)
5261 (setf args (cons :deadline
5262 (delq :deadline (delq :deadline* args)))))
5263 ((memq :deadline* args)
5264 (setf args (cons :deadline* (delq :deadline* args)))))
5265 ;; Collect list of headlines. Return them flattened.
5266 (let ((case-fold-search nil) results deadlines)
5267 (dolist (arg args (apply #'nconc (nreverse results)))
5268 (pcase arg
5269 ((and :todo (guard (org-agenda-today-p date)))
5270 (push (org-agenda-get-todos) results))
5271 (:timestamp
5272 (push (org-agenda-get-blocks) results)
5273 (push (org-agenda-get-timestamps deadlines) results))
5274 (:sexp
5275 (push (org-agenda-get-sexps) results))
5276 (:scheduled
5277 (push (org-agenda-get-scheduled deadlines) results))
5278 (:scheduled*
5279 (push (org-agenda-get-scheduled deadlines t) results))
5280 (:closed
5281 (push (org-agenda-get-progress) results))
5282 (:deadline
5283 (setf deadlines (org-agenda-get-deadlines))
5284 (push deadlines results))
5285 (:deadline*
5286 (setf deadlines (org-agenda-get-deadlines t))
5287 (push deadlines results)))))))))))
5289 (defsubst org-em (x y list)
5290 "Is X or Y a member of LIST?"
5291 (or (memq x list) (memq y list)))
5293 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5294 (defvar org-agenda-sorting-strategy-selected nil)
5296 (defun org-agenda-entry-get-agenda-timestamp (pom)
5297 "Retrieve timestamp information for sorting agenda views.
5298 Given a point or marker POM, returns a cons cell of the timestamp
5299 and the timestamp type relevant for the sorting strategy in
5300 `org-agenda-sorting-strategy-selected'."
5301 (let (ts ts-date-type)
5302 (save-match-data
5303 (cond ((org-em 'scheduled-up 'scheduled-down
5304 org-agenda-sorting-strategy-selected)
5305 (setq ts (org-entry-get pom "SCHEDULED")
5306 ts-date-type " scheduled"))
5307 ((org-em 'deadline-up 'deadline-down
5308 org-agenda-sorting-strategy-selected)
5309 (setq ts (org-entry-get pom "DEADLINE")
5310 ts-date-type " deadline"))
5311 ((org-em 'ts-up 'ts-down
5312 org-agenda-sorting-strategy-selected)
5313 (setq ts (org-entry-get pom "TIMESTAMP")
5314 ts-date-type " timestamp"))
5315 ((org-em 'tsia-up 'tsia-down
5316 org-agenda-sorting-strategy-selected)
5317 (setq ts (org-entry-get pom "TIMESTAMP_IA")
5318 ts-date-type " timestamp_ia"))
5319 ((org-em 'timestamp-up 'timestamp-down
5320 org-agenda-sorting-strategy-selected)
5321 (setq ts (or (org-entry-get pom "SCHEDULED")
5322 (org-entry-get pom "DEADLINE")
5323 (org-entry-get pom "TIMESTAMP")
5324 (org-entry-get pom "TIMESTAMP_IA"))
5325 ts-date-type ""))
5326 (t (setq ts-date-type "")))
5327 (cons (when ts (ignore-errors (org-time-string-to-absolute ts)))
5328 ts-date-type))))
5330 (defun org-agenda-get-todos ()
5331 "Return the TODO information for agenda display."
5332 (let* ((props (list 'face nil
5333 'done-face 'org-agenda-done
5334 'org-not-done-regexp org-not-done-regexp
5335 'org-todo-regexp org-todo-regexp
5336 'org-complex-heading-regexp org-complex-heading-regexp
5337 'mouse-face 'highlight
5338 'help-echo
5339 (format "mouse-2 or RET jump to org file %s"
5340 (abbreviate-file-name buffer-file-name))))
5341 (case-fold-search nil)
5342 (regexp (format org-heading-keyword-regexp-format
5343 (cond
5344 ((and org-select-this-todo-keyword
5345 (equal org-select-this-todo-keyword "*"))
5346 org-todo-regexp)
5347 (org-select-this-todo-keyword
5348 (concat "\\("
5349 (mapconcat 'identity
5350 (org-split-string
5351 org-select-this-todo-keyword
5352 "|")
5353 "\\|") "\\)"))
5354 (t org-not-done-regexp))))
5355 marker priority category level tags todo-state
5356 ts-date ts-date-type ts-date-pair
5357 ee txt beg end inherited-tags todo-state-end-pos)
5358 (goto-char (point-min))
5359 (while (re-search-forward regexp nil t)
5360 (catch :skip
5361 (save-match-data
5362 (beginning-of-line)
5363 (org-agenda-skip)
5364 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5365 (unless (and (setq todo-state (org-get-todo-state))
5366 (setq todo-state-end-pos (match-end 2)))
5367 (goto-char end)
5368 (throw :skip nil))
5369 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5370 (goto-char (1+ beg))
5371 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5372 (throw :skip nil)))
5373 (goto-char (match-beginning 2))
5374 (setq marker (org-agenda-new-marker (match-beginning 0))
5375 category (org-get-category)
5376 ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
5377 ts-date (car ts-date-pair)
5378 ts-date-type (cdr ts-date-pair)
5379 txt (org-trim (buffer-substring (match-beginning 2) (match-end 0)))
5380 inherited-tags
5381 (or (eq org-agenda-show-inherited-tags 'always)
5382 (and (listp org-agenda-show-inherited-tags)
5383 (memq 'todo org-agenda-show-inherited-tags))
5384 (and (eq org-agenda-show-inherited-tags t)
5385 (or (eq org-agenda-use-tag-inheritance t)
5386 (memq 'todo org-agenda-use-tag-inheritance))))
5387 tags (org-get-tags-at nil (not inherited-tags))
5388 level (make-string (org-reduced-level (org-outline-level)) ? )
5389 txt (org-agenda-format-item "" txt level category tags t)
5390 priority (1+ (org-get-priority txt)))
5391 (org-add-props txt props
5392 'org-marker marker 'org-hd-marker marker
5393 'priority priority
5394 'level level
5395 'ts-date ts-date
5396 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5397 (push txt ee)
5398 (if org-agenda-todo-list-sublevels
5399 (goto-char todo-state-end-pos)
5400 (org-end-of-subtree 'invisible))))
5401 (nreverse ee)))
5403 (defun org-agenda-todo-custom-ignore-p (time n)
5404 "Check whether timestamp is farther away than n number of days.
5405 This function is invoked if `org-agenda-todo-ignore-deadlines',
5406 `org-agenda-todo-ignore-scheduled' or
5407 `org-agenda-todo-ignore-timestamp' is set to an integer."
5408 (let ((days (org-time-stamp-to-now
5409 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5410 (if (>= n 0)
5411 (>= days n)
5412 (<= days n))))
5414 ;;;###autoload
5415 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5416 (&optional end)
5417 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5418 (when (or org-agenda-todo-ignore-with-date
5419 org-agenda-todo-ignore-scheduled
5420 org-agenda-todo-ignore-deadlines
5421 org-agenda-todo-ignore-timestamp)
5422 (setq end (or end (save-excursion (outline-next-heading) (point))))
5423 (save-excursion
5424 (or (and org-agenda-todo-ignore-with-date
5425 (re-search-forward org-ts-regexp end t))
5426 (and org-agenda-todo-ignore-scheduled
5427 (re-search-forward org-scheduled-time-regexp end t)
5428 (cond
5429 ((eq org-agenda-todo-ignore-scheduled 'future)
5430 (> (org-time-stamp-to-now
5431 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5432 ((eq org-agenda-todo-ignore-scheduled 'past)
5433 (<= (org-time-stamp-to-now
5434 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5435 ((numberp org-agenda-todo-ignore-scheduled)
5436 (org-agenda-todo-custom-ignore-p
5437 (match-string 1) org-agenda-todo-ignore-scheduled))
5438 (t)))
5439 (and org-agenda-todo-ignore-deadlines
5440 (re-search-forward org-deadline-time-regexp end t)
5441 (cond
5442 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5443 ((eq org-agenda-todo-ignore-deadlines 'far)
5444 (not (org-deadline-close-p (match-string 1))))
5445 ((eq org-agenda-todo-ignore-deadlines 'future)
5446 (> (org-time-stamp-to-now
5447 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5448 ((eq org-agenda-todo-ignore-deadlines 'past)
5449 (<= (org-time-stamp-to-now
5450 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5451 ((numberp org-agenda-todo-ignore-deadlines)
5452 (org-agenda-todo-custom-ignore-p
5453 (match-string 1) org-agenda-todo-ignore-deadlines))
5454 (t (org-deadline-close-p (match-string 1)))))
5455 (and org-agenda-todo-ignore-timestamp
5456 (let ((buffer (current-buffer))
5457 (regexp
5458 (concat
5459 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5460 (start (point)))
5461 ;; Copy current buffer into a temporary one
5462 (with-temp-buffer
5463 (insert-buffer-substring buffer start end)
5464 (goto-char (point-min))
5465 ;; Delete SCHEDULED and DEADLINE items
5466 (while (re-search-forward regexp end t)
5467 (delete-region (match-beginning 0) (match-end 0)))
5468 (goto-char (point-min))
5469 ;; No search for timestamp left
5470 (when (re-search-forward org-ts-regexp nil t)
5471 (cond
5472 ((eq org-agenda-todo-ignore-timestamp 'future)
5473 (> (org-time-stamp-to-now
5474 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5475 ((eq org-agenda-todo-ignore-timestamp 'past)
5476 (<= (org-time-stamp-to-now
5477 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5478 ((numberp org-agenda-todo-ignore-timestamp)
5479 (org-agenda-todo-custom-ignore-p
5480 (match-string 1) org-agenda-todo-ignore-timestamp))
5481 (t))))))))))
5483 (defun org-agenda-get-timestamps (&optional deadlines)
5484 "Return the date stamp information for agenda display.
5485 Optional argument DEADLINES is a list of deadline items to be
5486 displayed in agenda view."
5487 (let* ((props (list 'face 'org-agenda-calendar-event
5488 'org-not-done-regexp org-not-done-regexp
5489 'org-todo-regexp org-todo-regexp
5490 'org-complex-heading-regexp org-complex-heading-regexp
5491 'mouse-face 'highlight
5492 'help-echo
5493 (format "mouse-2 or RET jump to Org file %s"
5494 (abbreviate-file-name buffer-file-name))))
5495 (current (calendar-absolute-from-gregorian date))
5496 (today (org-today))
5497 (deadline-position-alist
5498 (mapcar (lambda (d)
5499 (let ((m (get-text-property 0 'org-hd-marker d)))
5500 (and m (marker-position m))))
5501 deadlines))
5502 ;; Match time-stamps set to current date, time-stamps with
5503 ;; a repeater, and S-exp time-stamps.
5504 (regexp
5505 (concat
5506 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5507 (regexp-quote
5508 (substring
5509 (format-time-string
5510 (car org-time-stamp-formats)
5511 (apply #'encode-time ; DATE bound by calendar
5512 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5513 1 11))
5514 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5515 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5516 timestamp-items)
5517 (goto-char (point-min))
5518 (while (re-search-forward regexp nil t)
5519 ;; Skip date ranges, scheduled and deadlines, which are handled
5520 ;; specially. Also skip time-stamps before first headline as
5521 ;; there would be no entry to add to the agenda. Eventually,
5522 ;; ignore clock entries.
5523 (catch :skip
5524 (save-match-data
5525 (when (or (org-at-date-range-p)
5526 (org-at-planning-p)
5527 (org-before-first-heading-p)
5528 (and org-agenda-include-inactive-timestamps
5529 (org-at-clock-log-p)))
5530 (throw :skip nil))
5531 (org-agenda-skip))
5532 (let* ((pos (match-beginning 0))
5533 (repeat (match-string 1))
5534 (sexp-entry (match-string 3))
5535 (time-stamp (if (or repeat sexp-entry) (match-string 0)
5536 (save-excursion
5537 (goto-char pos)
5538 (looking-at org-ts-regexp-both)
5539 (match-string 0))))
5540 (todo-state (org-get-todo-state))
5541 (warntime (get-text-property (point) 'org-appt-warntime))
5542 (done? (member todo-state org-done-keywords)))
5543 ;; Possibly skip done tasks.
5544 (when (and done? org-agenda-skip-timestamp-if-done)
5545 (throw :skip t))
5546 ;; S-exp entry doesn't match current day: skip it.
5547 (when (and sexp-entry (not (org-diary-sexp-entry sexp-entry "" date)))
5548 (throw :skip nil))
5549 (when repeat
5550 (let* ((past
5551 ;; A repeating time stamp is shown at its base
5552 ;; date and every repeated date up to TODAY. If
5553 ;; `org-agenda-prefer-last-repeat' is non-nil,
5554 ;; however, only the last repeat before today
5555 ;; (inclusive) is shown.
5556 (org-agenda--timestamp-to-absolute
5557 repeat
5558 (if (or (> current today)
5559 (eq org-agenda-prefer-last-repeat t)
5560 (member todo-state org-agenda-prefer-last-repeat))
5561 today
5562 current)
5563 'past (current-buffer) pos))
5564 (future
5565 ;; Display every repeated date past TODAY
5566 ;; (exclusive) unless
5567 ;; `org-agenda-show-future-repeats' is nil. If
5568 ;; this variable is set to `next', only display
5569 ;; the first repeated date after TODAY
5570 ;; (exclusive).
5571 (cond
5572 ((<= current today) past)
5573 ((not org-agenda-show-future-repeats) past)
5575 (let ((base (if (eq org-agenda-show-future-repeats 'next)
5576 (1+ today)
5577 current)))
5578 (org-agenda--timestamp-to-absolute
5579 repeat base 'future (current-buffer) pos))))))
5580 (when (and (/= current past) (/= current future))
5581 (throw :skip nil))))
5582 (save-excursion
5583 (re-search-backward org-outline-regexp-bol nil t)
5584 ;; Possibly skip time-stamp when a deadline is set.
5585 (when (and org-agenda-skip-timestamp-if-deadline-is-shown
5586 (assq (point) deadline-position-alist))
5587 (throw :skip nil))
5588 (let* ((category (org-get-category pos))
5589 (inherited-tags
5590 (or (eq org-agenda-show-inherited-tags 'always)
5591 (and (consp org-agenda-show-inherited-tags)
5592 (memq 'agenda org-agenda-show-inherited-tags))
5593 (and (eq org-agenda-show-inherited-tags t)
5594 (or (eq org-agenda-use-tag-inheritance t)
5595 (memq 'agenda
5596 org-agenda-use-tag-inheritance)))))
5597 (tags (org-get-tags-at nil (not inherited-tags)))
5598 (level (make-string (org-reduced-level (org-outline-level))
5599 ?\s))
5600 (head (and (looking-at "\\*+[ \t]+\\(.*\\)")
5601 (match-string 1)))
5602 (inactive? (= (char-after pos) ?\[))
5603 (habit? (and (fboundp 'org-is-habit-p) (org-is-habit-p)))
5604 (item
5605 (org-agenda-format-item
5606 (and inactive? org-agenda-inactive-leader)
5607 head level category tags time-stamp org-ts-regexp habit?)))
5608 (org-add-props item props
5609 'priority (if habit?
5610 (org-habit-get-priority (org-habit-parse-todo))
5611 (org-get-priority item))
5612 'org-marker (org-agenda-new-marker pos)
5613 'org-hd-marker (org-agenda-new-marker)
5614 'date date
5615 'level level
5616 'ts-date (if repeat (org-agenda--timestamp-to-absolute repeat)
5617 current)
5618 'todo-state todo-state
5619 'warntime warntime
5620 'type "timestamp")
5621 (push item timestamp-items))))
5622 (when org-agenda-skip-additional-timestamps-same-entry
5623 (outline-next-heading))))
5624 (nreverse timestamp-items)))
5626 (defun org-agenda-get-sexps ()
5627 "Return the sexp information for agenda display."
5628 (require 'diary-lib)
5629 (let* ((props (list 'face 'org-agenda-calendar-sexp
5630 'mouse-face 'highlight
5631 'help-echo
5632 (format "mouse-2 or RET jump to org file %s"
5633 (abbreviate-file-name buffer-file-name))))
5634 (regexp "^&?%%(")
5635 marker category extra level ee txt tags entry
5636 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5637 (goto-char (point-min))
5638 (while (re-search-forward regexp nil t)
5639 (catch :skip
5640 (org-agenda-skip)
5641 (setq beg (match-beginning 0))
5642 (goto-char (1- (match-end 0)))
5643 (setq b (point))
5644 (forward-sexp 1)
5645 (setq sexp (buffer-substring b (point)))
5646 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5647 (org-trim (match-string 1))
5648 ""))
5649 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5650 (when result
5651 (setq marker (org-agenda-new-marker beg)
5652 level (make-string (org-reduced-level (org-outline-level)) ? )
5653 category (org-get-category beg)
5654 inherited-tags
5655 (or (eq org-agenda-show-inherited-tags 'always)
5656 (and (listp org-agenda-show-inherited-tags)
5657 (memq 'agenda org-agenda-show-inherited-tags))
5658 (and (eq org-agenda-show-inherited-tags t)
5659 (or (eq org-agenda-use-tag-inheritance t)
5660 (memq 'agenda org-agenda-use-tag-inheritance))))
5661 tags (org-get-tags-at nil (not inherited-tags))
5662 todo-state (org-get-todo-state)
5663 warntime (get-text-property (point) 'org-appt-warntime)
5664 extra nil)
5666 (dolist (r (if (stringp result)
5667 (list result)
5668 result)) ;; we expect a list here
5669 (when (and org-agenda-diary-sexp-prefix
5670 (string-match org-agenda-diary-sexp-prefix r))
5671 (setq extra (match-string 0 r)
5672 r (replace-match "" nil nil r)))
5673 (if (string-match "\\S-" r)
5674 (setq txt r)
5675 (setq txt "SEXP entry returned empty string"))
5676 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5677 (org-add-props txt props 'org-marker marker
5678 'date date 'todo-state todo-state
5679 'level level 'type "sexp" 'warntime warntime)
5680 (push txt ee)))))
5681 (nreverse ee)))
5683 ;; Calendar sanity: define some functions that are independent of
5684 ;; `calendar-date-style'.
5685 (defun org-anniversary (year month day &optional mark)
5686 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5687 (with-no-warnings
5688 (let ((calendar-date-style 'iso))
5689 (diary-anniversary year month day mark))))
5690 (defun org-cyclic (N year month day &optional mark)
5691 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5692 (with-no-warnings
5693 (let ((calendar-date-style 'iso))
5694 (diary-cyclic N year month day mark))))
5695 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5696 "Like `diary-block', but with fixed (ISO) order of arguments."
5697 (with-no-warnings
5698 (let ((calendar-date-style 'iso))
5699 (diary-block Y1 M1 D1 Y2 M2 D2 mark))))
5700 (defun org-date (year month day &optional mark)
5701 "Like `diary-date', but with fixed (ISO) order of arguments."
5702 (with-no-warnings
5703 (let ((calendar-date-style 'iso))
5704 (diary-date year month day mark))))
5706 ;; Define the `org-class' function
5707 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5708 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5709 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5710 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5711 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5712 `holidays', then any date that is known by the Emacs calendar to be a
5713 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5714 then those holidays will be skipped."
5715 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5716 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5717 (d (calendar-absolute-from-gregorian date))
5718 (h (when skip-weeks (calendar-check-holidays date))))
5719 (and
5720 (<= date1 d)
5721 (<= d date2)
5722 (= (calendar-day-of-week date) dayname)
5723 (or (not skip-weeks)
5724 (progn
5725 (require 'cal-iso)
5726 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5727 (not (or (and h (memq 'holidays skip-weeks))
5728 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5729 entry)))
5731 (defalias 'org-get-closed 'org-agenda-get-progress)
5732 (defun org-agenda-get-progress ()
5733 "Return the logged TODO entries for agenda display."
5734 (let* ((props (list 'mouse-face 'highlight
5735 'org-not-done-regexp org-not-done-regexp
5736 'org-todo-regexp org-todo-regexp
5737 'org-complex-heading-regexp org-complex-heading-regexp
5738 'help-echo
5739 (format "mouse-2 or RET jump to org file %s"
5740 (abbreviate-file-name buffer-file-name))))
5741 (items (if (consp org-agenda-show-log-scoped)
5742 org-agenda-show-log-scoped
5743 (if (eq org-agenda-show-log-scoped 'clockcheck)
5744 '(clock)
5745 org-agenda-log-mode-items)))
5746 (parts
5747 (delq nil
5748 (list
5749 (if (memq 'closed items) (concat "\\<" org-closed-string))
5750 (if (memq 'clock items) (concat "\\<" org-clock-string))
5751 (if (memq 'state items)
5752 (format "- State \"%s\".*?" org-todo-regexp)))))
5753 (parts-re (if parts (mapconcat 'identity parts "\\|")
5754 (error "`org-agenda-log-mode-items' is empty")))
5755 (regexp (concat
5756 "\\(" parts-re "\\)"
5757 " *\\["
5758 (regexp-quote
5759 (substring
5760 (format-time-string
5761 (car org-time-stamp-formats)
5762 (apply 'encode-time ; DATE bound by calendar
5763 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5764 1 11))))
5765 (org-agenda-search-headline-for-time nil)
5766 marker hdmarker priority category level tags closedp
5767 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5768 (goto-char (point-min))
5769 (while (re-search-forward regexp nil t)
5770 (catch :skip
5771 (org-agenda-skip)
5772 (setq marker (org-agenda-new-marker (match-beginning 0))
5773 closedp (equal (match-string 1) org-closed-string)
5774 statep (equal (string-to-char (match-string 1)) ?-)
5775 clockp (not (or closedp statep))
5776 state (and statep (match-string 2))
5777 category (org-get-category (match-beginning 0))
5778 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5779 (when (string-match "\\]" timestr)
5780 ;; substring should only run to end of time stamp
5781 (setq rest (substring timestr (match-end 0))
5782 timestr (substring timestr 0 (match-end 0)))
5783 (if (and (not closedp) (not statep)
5784 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5785 rest))
5786 (progn (setq timestr (concat (substring timestr 0 -1)
5787 "-" (match-string 1 rest) "]"))
5788 (setq clocked (match-string 2 rest)))
5789 (setq clocked "-")))
5790 (save-excursion
5791 (setq extra
5792 (cond
5793 ((not org-agenda-log-mode-add-notes) nil)
5794 (statep
5795 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5796 (match-string 1)))
5797 (clockp
5798 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5799 (match-string 1)))))
5800 (if (not (re-search-backward org-outline-regexp-bol nil t))
5801 (throw :skip nil)
5802 (goto-char (match-beginning 0))
5803 (setq hdmarker (org-agenda-new-marker)
5804 inherited-tags
5805 (or (eq org-agenda-show-inherited-tags 'always)
5806 (and (listp org-agenda-show-inherited-tags)
5807 (memq 'todo org-agenda-show-inherited-tags))
5808 (and (eq org-agenda-show-inherited-tags t)
5809 (or (eq org-agenda-use-tag-inheritance t)
5810 (memq 'todo org-agenda-use-tag-inheritance))))
5811 tags (org-get-tags-at nil (not inherited-tags))
5812 level (make-string (org-reduced-level (org-outline-level)) ? ))
5813 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5814 (setq txt (match-string 1))
5815 (when extra
5816 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5817 (setq txt (concat (substring txt 0 (match-beginning 1))
5818 " - " extra " " (match-string 2 txt)))
5819 (setq txt (concat txt " - " extra))))
5820 (setq txt (org-agenda-format-item
5821 (cond
5822 (closedp "Closed: ")
5823 (statep (concat "State: (" state ")"))
5824 (t (concat "Clocked: (" clocked ")")))
5825 txt level category tags timestr)))
5826 (setq priority 100000)
5827 (org-add-props txt props
5828 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5829 'priority priority 'level level
5830 'type "closed" 'date date
5831 'undone-face 'org-warning 'done-face 'org-agenda-done)
5832 (push txt ee))
5833 (goto-char (point-at-eol))))
5834 (nreverse ee)))
5836 (defun org-agenda-show-clocking-issues ()
5837 "Add overlays, showing issues with clocking.
5838 See also the user option `org-agenda-clock-consistency-checks'."
5839 (interactive)
5840 (let* ((pl org-agenda-clock-consistency-checks)
5841 (re (concat "^[ \t]*"
5842 org-clock-string
5843 "[ \t]+"
5844 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5845 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5846 (tlstart 0.)
5847 (tlend 0.)
5848 (maxtime (org-duration-to-minutes
5849 (or (plist-get pl :max-duration) "24:00")))
5850 (mintime (org-duration-to-minutes
5851 (or (plist-get pl :min-duration) 0)))
5852 (maxgap (org-duration-to-minutes
5853 ;; default 30:00 means never complain
5854 (or (plist-get pl :max-gap) "30:00")))
5855 (gapok (mapcar #'org-duration-to-minutes
5856 (plist-get pl :gap-ok-around)))
5857 (def-face (or (plist-get pl :default-face)
5858 '((:background "DarkRed") (:foreground "white"))))
5859 issue face m te ts dt ov)
5860 (goto-char (point-min))
5861 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5862 (setq issue nil face def-face)
5863 (catch 'next
5864 (setq m (org-get-at-bol 'org-marker)
5865 te nil ts nil)
5866 (unless (and m (markerp m))
5867 (setq issue "No valid clock line") (throw 'next t))
5868 (org-with-point-at m
5869 (save-excursion
5870 (goto-char (point-at-bol))
5871 (unless (looking-at re)
5872 (error "No valid Clock line")
5873 (throw 'next t))
5874 (unless (match-end 3)
5875 (setq issue "No end time"
5876 face (or (plist-get pl :no-end-time-face) face))
5877 (throw 'next t))
5878 (setq ts (match-string 1)
5879 te (match-string 3)
5880 ts (float-time
5881 (apply #'encode-time (org-parse-time-string ts)))
5882 te (float-time
5883 (apply #'encode-time (org-parse-time-string te)))
5884 dt (- te ts))))
5885 (cond
5886 ((> dt (* 60 maxtime))
5887 ;; a very long clocking chunk
5888 (setq issue (format "Clocking interval is very long: %s"
5889 (org-duration-from-minutes (floor (/ dt 60.))))
5890 face (or (plist-get pl :long-face) face)))
5891 ((< dt (* 60 mintime))
5892 ;; a very short clocking chunk
5893 (setq issue (format "Clocking interval is very short: %s"
5894 (org-duration-from-minutes (floor (/ dt 60.))))
5895 face (or (plist-get pl :short-face) face)))
5896 ((and (> tlend 0) (< ts tlend))
5897 ;; Two clock entries are overlapping
5898 (setq issue (format "Clocking overlap: %d minutes"
5899 (/ (- tlend ts) 60))
5900 face (or (plist-get pl :overlap-face) face)))
5901 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5902 ;; There is a gap, lets see if we need to report it
5903 (unless (org-agenda-check-clock-gap tlend ts gapok)
5904 (setq issue (format "Clocking gap: %d minutes"
5905 (/ (- ts tlend) 60))
5906 face (or (plist-get pl :gap-face) face))))
5907 (t nil)))
5908 (setq tlend (or te tlend) tlstart (or ts tlstart))
5909 (when issue
5910 ;; OK, there was some issue, add an overlay to show the issue
5911 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5912 (overlay-put ov 'before-string
5913 (concat
5914 (org-add-props
5915 (format "%-43s" (concat " " issue))
5917 'face face)
5918 "\n"))
5919 (overlay-put ov 'evaporate t)))))
5921 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5922 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5923 (catch 'exit
5924 (unless ok-list
5925 ;; there are no OK times for gaps...
5926 (throw 'exit nil))
5927 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5928 ;; This is more than 24 hours, so it is OK.
5929 ;; because we have at least one OK time, that must be in the
5930 ;; 24 hour interval.
5931 (throw 'exit t))
5932 ;; We have a shorter gap.
5933 ;; Now we have to get the minute of the day when these times are
5934 (let* ((t1dec (decode-time (seconds-to-time t1)))
5935 (t2dec (decode-time (seconds-to-time t2)))
5936 ;; compute the minute on the day
5937 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5938 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5939 (when (< min2 min1)
5940 ;; if min2 is smaller than min1, this means it is on the next day.
5941 ;; Wrap it to after midnight.
5942 (setq min2 (+ min2 1440)))
5943 ;; Now check if any of the OK times is in the gap
5944 (mapc (lambda (x)
5945 ;; Wrap the time to after midnight if necessary
5946 (if (< x min1) (setq x (+ x 1440)))
5947 ;; Check if in interval
5948 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5949 ok-list)
5950 ;; Nope, this gap is not OK
5951 nil)))
5953 (defun org-agenda-get-deadlines (&optional with-hour)
5954 "Return the deadline information for agenda display.
5955 When WITH-HOUR is non-nil, only return deadlines with an hour
5956 specification like [h]h:mm."
5957 (let* ((props (list 'mouse-face 'highlight
5958 'org-not-done-regexp org-not-done-regexp
5959 'org-todo-regexp org-todo-regexp
5960 'org-complex-heading-regexp org-complex-heading-regexp
5961 'help-echo
5962 (format "mouse-2 or RET jump to org file %s"
5963 (abbreviate-file-name buffer-file-name))))
5964 (regexp (if with-hour
5965 org-deadline-time-hour-regexp
5966 org-deadline-time-regexp))
5967 (today (org-today))
5968 (today? (org-agenda-today-p date)) ; DATE bound by calendar.
5969 (current (calendar-absolute-from-gregorian date))
5970 deadline-items)
5971 (goto-char (point-min))
5972 (while (re-search-forward regexp nil t)
5973 (catch :skip
5974 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
5975 (org-agenda-skip)
5976 (let* ((s (match-string 1))
5977 (pos (1- (match-beginning 1)))
5978 (todo-state (save-match-data (org-get-todo-state)))
5979 (done? (member todo-state org-done-keywords))
5980 (sexp? (string-prefix-p "%%" s))
5981 ;; DEADLINE is the deadline date for the entry. It is
5982 ;; either the base date or the last repeat, according
5983 ;; to `org-agenda-prefer-last-repeat'.
5984 (deadline
5985 (cond
5986 (sexp? (org-agenda--timestamp-to-absolute s current))
5987 ((or (eq org-agenda-prefer-last-repeat t)
5988 (member todo-state org-agenda-prefer-last-repeat))
5989 (org-agenda--timestamp-to-absolute
5990 s today 'past (current-buffer) pos))
5991 (t (org-agenda--timestamp-to-absolute s))))
5992 ;; REPEAT is the future repeat closest from CURRENT,
5993 ;; according to `org-agenda-show-future-repeats'. If
5994 ;; the latter is nil, or if the time stamp has no
5995 ;; repeat part, default to DEADLINE.
5996 (repeat
5997 (cond
5998 (sexp? deadline)
5999 ((<= current today) deadline)
6000 ((not org-agenda-show-future-repeats) deadline)
6002 (let ((base (if (eq org-agenda-show-future-repeats 'next)
6003 (1+ today)
6004 current)))
6005 (org-agenda--timestamp-to-absolute
6006 s base 'future (current-buffer) pos)))))
6007 (diff (- deadline current))
6008 (suppress-prewarning
6009 (let ((scheduled
6010 (and org-agenda-skip-deadline-prewarning-if-scheduled
6011 (org-entry-get nil "SCHEDULED"))))
6012 (cond
6013 ((not scheduled) nil)
6014 ;; The current item has a scheduled date, so
6015 ;; evaluate its prewarning lead time.
6016 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
6017 ;; Use global prewarning-restart lead time.
6018 org-agenda-skip-deadline-prewarning-if-scheduled)
6019 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
6020 'pre-scheduled)
6021 ;; Set pre-warning to no earlier than SCHEDULED.
6022 (min (- deadline
6023 (org-agenda--timestamp-to-absolute scheduled))
6024 org-deadline-warning-days))
6025 ;; Set pre-warning to deadline.
6026 (t 0))))
6027 (wdays (if suppress-prewarning
6028 (let ((org-deadline-warning-days suppress-prewarning))
6029 (org-get-wdays s))
6030 (org-get-wdays s))))
6031 (cond
6032 ;; Only display deadlines at their base date, at future
6033 ;; repeat occurrences or in today agenda.
6034 ((= current deadline) nil)
6035 ((= current repeat) nil)
6036 ((not today?) (throw :skip nil))
6037 ;; Upcoming deadline: display within warning period WDAYS.
6038 ((> deadline current) (when (> diff wdays) (throw :skip nil)))
6039 ;; Overdue deadline: warn about it for
6040 ;; `org-deadline-past-days' duration.
6041 (t (when (< org-deadline-past-days (- diff)) (throw :skip nil))))
6042 ;; Possibly skip done tasks.
6043 (when (and done?
6044 (or org-agenda-skip-deadline-if-done
6045 (/= deadline current)))
6046 (throw :skip nil))
6047 (save-excursion
6048 (re-search-backward "^\\*+[ \t]+" nil t)
6049 (goto-char (match-end 0))
6050 (let* ((category (org-get-category))
6051 (level (make-string (org-reduced-level (org-outline-level))
6052 ?\s))
6053 (head (buffer-substring (point) (line-end-position)))
6054 (inherited-tags
6055 (or (eq org-agenda-show-inherited-tags 'always)
6056 (and (listp org-agenda-show-inherited-tags)
6057 (memq 'agenda org-agenda-show-inherited-tags))
6058 (and (eq org-agenda-show-inherited-tags t)
6059 (or (eq org-agenda-use-tag-inheritance t)
6060 (memq 'agenda
6061 org-agenda-use-tag-inheritance)))))
6062 (tags (org-get-tags-at nil (not inherited-tags)))
6063 (time
6064 (cond
6065 ;; No time of day designation if it is only
6066 ;; a reminder.
6067 ((and (/= current deadline) (/= current repeat)) nil)
6068 ((string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6069 (concat (substring s (match-beginning 1)) " "))
6070 (t 'time)))
6071 (item
6072 (org-agenda-format-item
6073 ;; Insert appropriate suffixes before deadlines.
6074 ;; Those only apply to today agenda.
6075 (pcase-let ((`(,now ,future ,past)
6076 org-agenda-deadline-leaders))
6077 (cond
6078 ((and today? (< deadline today)) (format past (- diff)))
6079 ((and today? (> deadline today)) (format future diff))
6080 (t now)))
6081 head level category tags time))
6082 (face (org-agenda-deadline-face
6083 (- 1 (/ (float diff) (max wdays 1)))))
6084 (upcoming? (and today? (> deadline today)))
6085 (warntime (get-text-property (point) 'org-appt-warntime)))
6086 (org-add-props item props
6087 'org-marker (org-agenda-new-marker pos)
6088 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
6089 'warntime warntime
6090 'level level
6091 'ts-date deadline
6092 'priority
6093 ;; Adjust priority to today reminders about deadlines.
6094 ;; Overdue deadlines get the highest priority
6095 ;; increase, then imminent deadlines and eventually
6096 ;; more distant deadlines.
6097 (let ((adjust (if today? (- diff) 0)))
6098 (+ adjust (org-get-priority item)))
6099 'todo-state todo-state
6100 'type (if upcoming? "upcoming-deadline" "deadline")
6101 'date (if upcoming? date deadline)
6102 'face (if done? 'org-agenda-done face)
6103 'undone-face face
6104 'done-face 'org-agenda-done)
6105 (push item deadline-items))))))
6106 (nreverse deadline-items)))
6108 (defun org-agenda-deadline-face (fraction)
6109 "Return the face to displaying a deadline item.
6110 FRACTION is what fraction of the head-warning time has passed."
6111 (assoc-default fraction org-agenda-deadline-faces #'<=))
6113 (defun org-agenda-get-scheduled (&optional deadlines with-hour)
6114 "Return the scheduled information for agenda display.
6115 Optional argument DEADLINES is a list of deadline items to be
6116 displayed in agenda view. When WITH-HOUR is non-nil, only return
6117 scheduled items with an hour specification like [h]h:mm."
6118 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
6119 'org-todo-regexp org-todo-regexp
6120 'org-complex-heading-regexp org-complex-heading-regexp
6121 'done-face 'org-agenda-done
6122 'mouse-face 'highlight
6123 'help-echo
6124 (format "mouse-2 or RET jump to Org file %s"
6125 (abbreviate-file-name buffer-file-name))))
6126 (regexp (if with-hour
6127 org-scheduled-time-hour-regexp
6128 org-scheduled-time-regexp))
6129 (today (org-today))
6130 (todayp (org-agenda-today-p date)) ; DATE bound by calendar.
6131 (current (calendar-absolute-from-gregorian date))
6132 (deadline-pos
6133 (mapcar (lambda (d)
6134 (let ((m (get-text-property 0 'org-hd-marker d)))
6135 (and m (marker-position m))))
6136 deadlines))
6137 scheduled-items)
6138 (goto-char (point-min))
6139 (while (re-search-forward regexp nil t)
6140 (catch :skip
6141 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
6142 (org-agenda-skip)
6143 (let* ((s (match-string 1))
6144 (pos (1- (match-beginning 1)))
6145 (todo-state (save-match-data (org-get-todo-state)))
6146 (donep (member todo-state org-done-keywords))
6147 (sexp? (string-prefix-p "%%" s))
6148 ;; SCHEDULE is the scheduled date for the entry. It is
6149 ;; either the bare date or the last repeat, according
6150 ;; to `org-agenda-prefer-last-repeat'.
6151 (schedule
6152 (cond
6153 (sexp? (org-agenda--timestamp-to-absolute s current))
6154 ((or (eq org-agenda-prefer-last-repeat t)
6155 (member todo-state org-agenda-prefer-last-repeat))
6156 (org-agenda--timestamp-to-absolute
6157 s today 'past (current-buffer) pos))
6158 (t (org-agenda--timestamp-to-absolute s))))
6159 ;; REPEAT is the future repeat closest from CURRENT,
6160 ;; according to `org-agenda-show-future-repeats'. If
6161 ;; the latter is nil, or if the time stamp has no
6162 ;; repeat part, default to SCHEDULE.
6163 (repeat
6164 (cond
6165 (sexp? schedule)
6166 ((<= current today) schedule)
6167 ((not org-agenda-show-future-repeats) schedule)
6169 (let ((base (if (eq org-agenda-show-future-repeats 'next)
6170 (1+ today)
6171 current)))
6172 (org-agenda--timestamp-to-absolute
6173 s base 'future (current-buffer) pos)))))
6174 (diff (- current schedule))
6175 (warntime (get-text-property (point) 'org-appt-warntime))
6176 (pastschedp (< schedule today))
6177 (habitp (and (fboundp 'org-is-habit-p) (org-is-habit-p)))
6178 (suppress-delay
6179 (let ((deadline (and org-agenda-skip-scheduled-delay-if-deadline
6180 (org-entry-get nil "DEADLINE"))))
6181 (cond
6182 ((not deadline) nil)
6183 ;; The current item has a deadline date, so
6184 ;; evaluate its delay time.
6185 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6186 ;; Use global delay time.
6187 (- org-agenda-skip-scheduled-delay-if-deadline))
6188 ((eq org-agenda-skip-scheduled-delay-if-deadline
6189 'post-deadline)
6190 ;; Set delay to no later than DEADLINE.
6191 (min (- schedule
6192 (org-agenda--timestamp-to-absolute deadline))
6193 org-scheduled-delay-days))
6194 (t 0))))
6195 (ddays
6196 (cond
6197 ;; Nullify delay when a repeater triggered already
6198 ;; and the delay is of the form --Xd.
6199 ((and (string-match-p "--[0-9]+[hdwmy]" s)
6200 (> current schedule))
6202 (suppress-delay
6203 (let ((org-scheduled-delay-days suppress-delay))
6204 (org-get-wdays s t t)))
6205 (t (org-get-wdays s t)))))
6206 ;; Display scheduled items at base date (SCHEDULE), today if
6207 ;; scheduled before the current date, and at any repeat past
6208 ;; today. However, skip delayed items and items that have
6209 ;; been displayed for more than `org-scheduled-past-days'.
6210 (unless (and todayp
6211 habitp
6212 (bound-and-true-p org-habit-show-all-today))
6213 (when (or (and (> ddays 0) (< diff ddays))
6214 (> diff org-scheduled-past-days)
6215 (> schedule current)
6216 (and (/= current schedule)
6217 (/= current today)
6218 (/= current repeat)))
6219 (throw :skip nil)))
6220 ;; Possibly skip done tasks.
6221 (when (and donep
6222 (or org-agenda-skip-scheduled-if-done
6223 (/= schedule current)))
6224 (throw :skip nil))
6225 ;; Skip entry if it already appears as a deadline, per
6226 ;; `org-agenda-skip-scheduled-if-deadline-is-shown'. This
6227 ;; doesn't apply to habits.
6228 (when (pcase org-agenda-skip-scheduled-if-deadline-is-shown
6229 ((guard
6230 (or (not (memq (line-beginning-position 0) deadline-pos))
6231 habitp))
6232 nil)
6233 (`repeated-after-deadline
6234 (let ((deadline (time-to-days
6235 (org-get-deadline-time (point)))))
6236 (and (<= schedule deadline) (> current deadline))))
6237 (`not-today pastschedp)
6238 (`t t)
6239 (_ nil))
6240 (throw :skip nil))
6241 ;; Skip habits if `org-habit-show-habits' is nil, or if we
6242 ;; only show them for today. Also skip done habits.
6243 (when (and habitp
6244 (or donep
6245 (not (bound-and-true-p org-habit-show-habits))
6246 (and (not todayp)
6247 (bound-and-true-p
6248 org-habit-show-habits-only-for-today))))
6249 (throw :skip nil))
6250 (save-excursion
6251 (re-search-backward "^\\*+[ \t]+" nil t)
6252 (goto-char (match-end 0))
6253 (let* ((category (org-get-category))
6254 (inherited-tags
6255 (or (eq org-agenda-show-inherited-tags 'always)
6256 (and (listp org-agenda-show-inherited-tags)
6257 (memq 'agenda org-agenda-show-inherited-tags))
6258 (and (eq org-agenda-show-inherited-tags t)
6259 (or (eq org-agenda-use-tag-inheritance t)
6260 (memq 'agenda
6261 org-agenda-use-tag-inheritance)))))
6262 (tags (org-get-tags-at nil (not inherited-tags)))
6263 (level (make-string (org-reduced-level (org-outline-level))
6264 ?\s))
6265 (head (buffer-substring (point) (line-end-position)))
6266 (time
6267 (cond
6268 ;; No time of day designation if it is only
6269 ;; a reminder.
6270 ((and (/= current schedule) (/= current repeat)) nil)
6271 ((string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6272 (concat (substring s (match-beginning 1)) " "))
6273 (t 'time)))
6274 (item
6275 (org-agenda-format-item
6276 (pcase-let ((`(,first ,past) org-agenda-scheduled-leaders))
6277 ;; Show a reminder of a past scheduled today.
6278 (if (and todayp pastschedp)
6279 (format past diff)
6280 first))
6281 head level category tags time nil habitp))
6282 (face (cond ((and (not habitp) pastschedp)
6283 'org-scheduled-previously)
6284 (todayp 'org-scheduled-today)
6285 (t 'org-scheduled)))
6286 (habitp (and habitp (org-habit-parse-todo))))
6287 (org-add-props item props
6288 'undone-face face
6289 'face (if donep 'org-agenda-done face)
6290 'org-marker (org-agenda-new-marker pos)
6291 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
6292 'type (if pastschedp "past-scheduled" "scheduled")
6293 'date (if pastschedp schedule date)
6294 'ts-date schedule
6295 'warntime warntime
6296 'level level
6297 'priority (if habitp (org-habit-get-priority habitp)
6298 (+ 99 diff (org-get-priority item)))
6299 'org-habit-p habitp
6300 'todo-state todo-state)
6301 (push item scheduled-items))))))
6302 (nreverse scheduled-items)))
6304 (defun org-agenda-get-blocks ()
6305 "Return the date-range information for agenda display."
6306 (let* ((props (list 'face nil
6307 'org-not-done-regexp org-not-done-regexp
6308 'org-todo-regexp org-todo-regexp
6309 'org-complex-heading-regexp org-complex-heading-regexp
6310 'mouse-face 'highlight
6311 'help-echo
6312 (format "mouse-2 or RET jump to org file %s"
6313 (abbreviate-file-name buffer-file-name))))
6314 (regexp org-tr-regexp)
6315 (d0 (calendar-absolute-from-gregorian date))
6316 marker hdmarker ee txt d1 d2 s1 s2 category
6317 level todo-state tags pos head donep inherited-tags)
6318 (goto-char (point-min))
6319 (while (re-search-forward regexp nil t)
6320 (catch :skip
6321 (org-agenda-skip)
6322 (setq pos (point))
6323 (let ((start-time (match-string 1))
6324 (end-time (match-string 2)))
6325 (setq s1 (match-string 1)
6326 s2 (match-string 2)
6327 d1 (time-to-days
6328 (condition-case err
6329 (org-time-string-to-time s1)
6330 (error
6331 (error
6332 "Bad timestamp %S at %d in buffer %S\nError was: %s"
6335 (current-buffer)
6336 (error-message-string err)))))
6337 d2 (time-to-days
6338 (condition-case err
6339 (org-time-string-to-time s2)
6340 (error
6341 (error
6342 "Bad timestamp %S at %d in buffer %S\nError was: %s"
6345 (current-buffer)
6346 (error-message-string err))))))
6347 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6348 ;; Only allow days between the limits, because the normal
6349 ;; date stamps will catch the limits.
6350 (save-excursion
6351 (setq todo-state (org-get-todo-state))
6352 (setq donep (member todo-state org-done-keywords))
6353 (if (and donep org-agenda-skip-timestamp-if-done)
6354 (throw :skip t))
6355 (setq marker (org-agenda-new-marker (point))
6356 category (org-get-category))
6357 (if (not (re-search-backward org-outline-regexp-bol nil t))
6358 (throw :skip nil)
6359 (goto-char (match-beginning 0))
6360 (setq hdmarker (org-agenda-new-marker (point))
6361 inherited-tags
6362 (or (eq org-agenda-show-inherited-tags 'always)
6363 (and (listp org-agenda-show-inherited-tags)
6364 (memq 'agenda org-agenda-show-inherited-tags))
6365 (and (eq org-agenda-show-inherited-tags t)
6366 (or (eq org-agenda-use-tag-inheritance t)
6367 (memq 'agenda org-agenda-use-tag-inheritance))))
6369 tags (org-get-tags-at nil (not inherited-tags)))
6370 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6371 (looking-at "\\*+[ \t]+\\(.*\\)")
6372 (setq head (match-string 1))
6373 (let ((remove-re
6374 (if org-agenda-remove-timeranges-from-blocks
6375 (concat
6376 "<" (regexp-quote s1) ".*?>"
6377 "--"
6378 "<" (regexp-quote s2) ".*?>")
6379 nil)))
6380 (setq txt (org-agenda-format-item
6381 (format
6382 (nth (if (= d1 d2) 0 1)
6383 org-agenda-timerange-leaders)
6384 (1+ (- d0 d1)) (1+ (- d2 d1)))
6385 head level category tags
6386 (cond ((and (= d1 d0) (= d2 d0))
6387 (concat "<" start-time ">--<" end-time ">"))
6388 ((= d1 d0)
6389 (concat "<" start-time ">"))
6390 ((= d2 d0)
6391 (concat "<" end-time ">")))
6392 remove-re))))
6393 (org-add-props txt props
6394 'org-marker marker 'org-hd-marker hdmarker
6395 'type "block" 'date date
6396 'level level
6397 'todo-state todo-state
6398 'priority (org-get-priority txt))
6399 (push txt ee))))
6400 (goto-char pos)))
6401 ;; Sort the entries by expiration date.
6402 (nreverse ee)))
6404 ;;; Agenda presentation and sorting
6406 (defvar org-prefix-has-time nil
6407 "A flag, set by `org-compile-prefix-format'.
6408 The flag is set if the currently compiled format contains a `%t'.")
6409 (defvar org-prefix-has-tag nil
6410 "A flag, set by `org-compile-prefix-format'.
6411 The flag is set if the currently compiled format contains a `%T'.")
6412 (defvar org-prefix-has-effort nil
6413 "A flag, set by `org-compile-prefix-format'.
6414 The flag is set if the currently compiled format contains a `%e'.")
6415 (defvar org-prefix-has-breadcrumbs nil
6416 "A flag, set by `org-compile-prefix-format'.
6417 The flag is set if the currently compiled format contains a `%b'.")
6418 (defvar org-prefix-category-length nil
6419 "Used by `org-compile-prefix-format' to remember the category field width.")
6420 (defvar org-prefix-category-max-length nil
6421 "Used by `org-compile-prefix-format' to remember the category field width.")
6423 (defun org-agenda-get-category-icon (category)
6424 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6425 (cl-dolist (entry org-agenda-category-icon-alist)
6426 (when (string-match-p (car entry) category)
6427 (if (listp (cadr entry))
6428 (cl-return (cadr entry))
6429 (cl-return (apply #'create-image (cdr entry)))))))
6431 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6432 remove-re habitp)
6433 "Format TXT to be inserted into the agenda buffer.
6434 In particular, add the prefix and corresponding text properties.
6436 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6437 LEVEL may be a string to replace the `%l' specifier.
6438 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6439 category taken from local variable or file name. It will replace the `%c'
6440 specifier in the format.
6441 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6442 TXT for sorting of this entry, and for the `%t' specifier in the format.
6443 When DOTIME is a string, this string is searched for a time before TXT is.
6444 TAGS can be the tags of the headline.
6445 Any match of REMOVE-RE will be removed from TXT."
6446 ;; We keep the org-prefix-* variable values along with a compiled
6447 ;; formatter, so that multiple agendas existing at the same time do
6448 ;; not step on each other toes.
6450 ;; It was inconvenient to make these variables buffer local in
6451 ;; Agenda buffers, because this function expects to be called with
6452 ;; the buffer where item comes from being current, and not agenda
6453 ;; buffer
6454 (let* ((bindings (car org-prefix-format-compiled))
6455 (formatter (cadr org-prefix-format-compiled)))
6456 (cl-loop for (var value) in bindings
6457 do (set var value))
6458 (save-match-data
6459 ;; Diary entries sometimes have extra whitespace at the beginning
6460 (setq txt (org-trim txt))
6462 ;; Fix the tags part in txt
6463 (setq txt (org-agenda-fix-displayed-tags
6464 txt tags
6465 org-agenda-show-inherited-tags
6466 org-agenda-hide-tags-regexp))
6468 (let* ((category (or category
6469 (if buffer-file-name
6470 (file-name-sans-extension
6471 (file-name-nondirectory buffer-file-name))
6472 "")))
6473 (category-icon (org-agenda-get-category-icon category))
6474 (category-icon (if category-icon
6475 (propertize " " 'display category-icon)
6476 ""))
6477 (effort (and (not (string= txt ""))
6478 (get-text-property 1 'effort txt)))
6479 ;; time, tag, effort are needed for the eval of the prefix format
6480 (tag (if tags (nth (1- (length tags)) tags) ""))
6481 (time-grid-trailing-characters (nth 2 org-agenda-time-grid))
6482 time
6483 (ts (if dotime (concat
6484 (if (stringp dotime) dotime "")
6485 (and org-agenda-search-headline-for-time txt))))
6486 (time-of-day (and dotime (org-get-time-of-day ts)))
6487 stamp plain s0 s1 s2 rtn srp l
6488 duration breadcrumbs)
6489 (and (derived-mode-p 'org-mode) buffer-file-name
6490 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6491 (when (and dotime time-of-day)
6492 ;; Extract starting and ending time and move them to prefix
6493 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6494 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6495 (setq s0 (match-string 0 ts)
6496 srp (and stamp (match-end 3))
6497 s1 (match-string (if plain 1 2) ts)
6498 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6500 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6501 ;; them, we might want to remove them there to avoid duplication.
6502 ;; The user can turn this off with a variable.
6503 (if (and org-prefix-has-time
6504 org-agenda-remove-times-when-in-prefix (or stamp plain)
6505 (string-match (concat (regexp-quote s0) " *") txt)
6506 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6507 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6508 (= (match-beginning 0) 0)
6510 (setq txt (replace-match "" nil nil txt))))
6511 ;; Normalize the time(s) to 24 hour
6512 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6513 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6515 ;; Try to set s2 if s1 and
6516 ;; `org-agenda-default-appointment-duration' are set
6517 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6518 (setq s2
6519 (org-duration-from-minutes
6520 (+ (org-duration-to-minutes s1 t)
6521 org-agenda-default-appointment-duration)
6522 nil t)))
6524 ;; Compute the duration
6525 (when s2
6526 (setq duration (- (org-duration-to-minutes s2)
6527 (org-duration-to-minutes s1)))))
6529 (when (string-match "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" txt)
6530 ;; Tags are in the string
6531 (if (or (eq org-agenda-remove-tags t)
6532 (and org-agenda-remove-tags
6533 org-prefix-has-tag))
6534 (setq txt (replace-match "" t t txt))
6535 (setq txt (replace-match
6536 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6537 (match-string 2 txt))
6538 t t txt))))
6540 (when remove-re
6541 (while (string-match remove-re txt)
6542 (setq txt (replace-match "" t t txt))))
6544 ;; Set org-heading property on `txt' to mark the start of the
6545 ;; heading.
6546 (add-text-properties 0 (length txt) '(org-heading t) txt)
6548 ;; Prepare the variables needed in the eval of the compiled format
6549 (if org-prefix-has-breadcrumbs
6550 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6551 (let ((s (org-display-outline-path nil nil "->" t)))
6552 (if (eq "" s) "" (concat s "->"))))))
6553 (setq time (cond (s2 (concat
6554 (org-agenda-time-of-day-to-ampm-maybe s1)
6555 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6556 (if org-agenda-timegrid-use-ampm " ")))
6557 (s1 (concat
6558 (org-agenda-time-of-day-to-ampm-maybe s1)
6559 (if org-agenda-timegrid-use-ampm
6560 (concat time-grid-trailing-characters " ")
6561 time-grid-trailing-characters)))
6562 (t ""))
6563 extra (or (and (not habitp) extra) "")
6564 category (if (symbolp category) (symbol-name category) category)
6565 level (or level ""))
6566 (if (string-match org-bracket-link-regexp category)
6567 (progn
6568 (setq l (if (match-end 3)
6569 (- (match-end 3) (match-beginning 3))
6570 (- (match-end 1) (match-beginning 1))))
6571 (when (< l (or org-prefix-category-length 0))
6572 (setq category (copy-sequence category))
6573 (org-add-props category nil
6574 'extra-space (make-string
6575 (- org-prefix-category-length l 1) ?\ ))))
6576 (if (and org-prefix-category-max-length
6577 (>= (length category) org-prefix-category-max-length))
6578 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6579 ;; Evaluate the compiled format
6580 (setq rtn (concat (eval formatter) txt))
6582 ;; And finally add the text properties
6583 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6584 (org-add-props rtn nil
6585 'org-category category
6586 'tags (mapcar 'org-downcase-keep-props tags)
6587 'org-highest-priority org-highest-priority
6588 'org-lowest-priority org-lowest-priority
6589 'time-of-day time-of-day
6590 'duration duration
6591 'breadcrumbs breadcrumbs
6592 'txt txt
6593 'level level
6594 'time time
6595 'extra extra
6596 'format org-prefix-format-compiled
6597 'dotime dotime)))))
6599 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6600 "Remove tags string from TXT, and add a modified list of tags.
6601 The modified list may contain inherited tags, and tags matched by
6602 `org-agenda-hide-tags-regexp' will be removed."
6603 (when (or add-inherited hide-re)
6604 (if (string-match "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" txt)
6605 (setq txt (substring txt 0 (match-beginning 0))))
6606 (setq tags
6607 (delq nil
6608 (mapcar (lambda (tg)
6609 (if (or (and hide-re (string-match hide-re tg))
6610 (and (not add-inherited)
6611 (get-text-property 0 'inherited tg)))
6613 tg))
6614 tags)))
6615 (when tags
6616 (let ((have-i (get-text-property 0 'inherited (car tags)))
6618 (setq txt (concat txt " :"
6619 (mapconcat
6620 (lambda (x)
6621 (setq i (get-text-property 0 'inherited x))
6622 (if (and have-i (not i))
6623 (progn
6624 (setq have-i nil)
6625 (concat ":" x))
6627 tags ":")
6628 (if have-i "::" ":"))))))
6629 txt)
6631 (defun org-downcase-keep-props (s)
6632 (let ((props (text-properties-at 0 s)))
6633 (setq s (downcase s))
6634 (add-text-properties 0 (length s) props s)
6637 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6639 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6640 "Add a time-grid for agenda items which need it.
6642 LIST is the list of agenda items formatted by `org-agenda-list'.
6643 NDAYS is the span of the current agenda view.
6644 TODAYP is t when the current agenda view is on today."
6645 (catch 'exit
6646 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6647 ((and todayp (member 'today (car org-agenda-time-grid))))
6648 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6649 ((member 'weekly (car org-agenda-time-grid)))
6650 (t (throw 'exit list)))
6651 (let* ((have (delq nil (mapcar
6652 (lambda (x) (get-text-property 1 'time-of-day x))
6653 list)))
6654 (string (nth 3 org-agenda-time-grid))
6655 (gridtimes (nth 1 org-agenda-time-grid))
6656 (req (car org-agenda-time-grid))
6657 (remove (member 'remove-match req))
6658 new time)
6659 (if (and (member 'require-timed req) (not have))
6660 ;; don't show empty grid
6661 (throw 'exit list))
6662 (while (setq time (pop gridtimes))
6663 (unless (and remove (member time have))
6664 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6665 (push (org-agenda-format-item
6666 nil string nil "" nil
6667 (concat (substring time 0 -2) ":" (substring time -2)))
6668 new)
6669 (put-text-property
6670 2 (length (car new)) 'face 'org-time-grid (car new))))
6671 (when (and todayp org-agenda-show-current-time-in-grid)
6672 (push (org-agenda-format-item
6673 nil org-agenda-current-time-string nil "" nil
6674 (format-time-string "%H:%M "))
6675 new)
6676 (put-text-property
6677 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6679 (if (member 'time-up org-agenda-sorting-strategy-selected)
6680 (append new list)
6681 (append list new)))))
6683 (defun org-compile-prefix-format (key)
6684 "Compile the prefix format into a Lisp form that can be evaluated.
6685 The resulting form and associated variable bindings is returned
6686 and stored in the variable `org-prefix-format-compiled'."
6687 (setq org-prefix-has-time nil
6688 org-prefix-has-tag nil
6689 org-prefix-category-length nil
6690 org-prefix-has-effort nil
6691 org-prefix-has-breadcrumbs nil)
6692 (let ((s (cond
6693 ((stringp org-agenda-prefix-format)
6694 org-agenda-prefix-format)
6695 ((assq key org-agenda-prefix-format)
6696 (cdr (assq key org-agenda-prefix-format)))
6697 (t " %-12:c%?-12t% s")))
6698 (start 0)
6699 varform vars var e c f opt)
6700 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6701 s start)
6702 (setq var (or (cdr (assoc (match-string 4 s)
6703 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6704 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6705 'eval)
6706 c (or (match-string 3 s) "")
6707 opt (match-beginning 1)
6708 start (1+ (match-beginning 0)))
6709 (if (eq var 'time) (setq org-prefix-has-time t))
6710 (if (eq var 'tag) (setq org-prefix-has-tag t))
6711 (if (eq var 'effort) (setq org-prefix-has-effort t))
6712 (if (eq var 'breadcrumbs) (setq org-prefix-has-breadcrumbs t))
6713 (setq f (concat "%" (match-string 2 s) "s"))
6714 (when (eq var 'category)
6715 (setq org-prefix-category-length
6716 (floor (abs (string-to-number (match-string 2 s)))))
6717 (setq org-prefix-category-max-length
6718 (let ((x (match-string 2 s)))
6719 (save-match-data
6720 (if (string-match "\\.[0-9]+" x)
6721 (string-to-number (substring (match-string 0 x) 1)))))))
6722 (if (eq var 'eval)
6723 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6724 (if opt
6725 (setq varform
6726 `(if (or (equal "" ,var) (equal nil ,var))
6728 (format ,f (concat ,var ,c))))
6729 (setq varform
6730 `(format ,f (if (or (equal ,var "")
6731 (equal ,var nil)) ""
6732 (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6733 (setq s (replace-match "%s" t nil s))
6734 (push varform vars))
6735 (setq vars (nreverse vars))
6736 (with-current-buffer (or org-agenda-buffer (current-buffer))
6737 (setq org-prefix-format-compiled
6738 (list
6739 `((org-prefix-has-time ,org-prefix-has-time)
6740 (org-prefix-has-tag ,org-prefix-has-tag)
6741 (org-prefix-category-length ,org-prefix-category-length)
6742 (org-prefix-has-effort ,org-prefix-has-effort)
6743 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6744 `(format ,s ,@vars))))))
6746 (defun org-set-sorting-strategy (key)
6747 (if (symbolp (car org-agenda-sorting-strategy))
6748 ;; the old format
6749 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6750 (setq org-agenda-sorting-strategy-selected
6751 (or (cdr (assq key org-agenda-sorting-strategy))
6752 (cdr (assq 'agenda org-agenda-sorting-strategy))
6753 '(time-up category-keep priority-down)))))
6755 (defun org-get-time-of-day (s &optional string mod24)
6756 "Check string S for a time of day.
6757 If found, return it as a military time number between 0 and 2400.
6758 If not found, return nil.
6759 The optional STRING argument forces conversion into a 5 character wide string
6760 HH:MM."
6761 (save-match-data
6762 (when
6763 (and
6764 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6765 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6766 (not (eq (get-text-property 1 'face s) 'org-link)))
6767 (let* ((h (string-to-number (match-string 1 s)))
6768 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6769 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6770 (am-p (equal ampm "am"))
6771 (h1 (cond ((not ampm) h)
6772 ((= h 12) (if am-p 0 12))
6773 (t (+ h (if am-p 0 12)))))
6774 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6775 (mod h1 24) h1))
6776 (t0 (+ (* 100 h2) m))
6777 (t1 (concat (if (>= h1 24) "+" " ")
6778 (if (and org-agenda-time-leading-zero
6779 (< t0 1000)) "0" "")
6780 (if (< t0 100) "0" "")
6781 (if (< t0 10) "0" "")
6782 (int-to-string t0))))
6783 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6785 (defvar org-agenda-before-sorting-filter-function nil
6786 "Function to be applied to agenda items prior to sorting.
6787 Prior to sorting also means just before they are inserted into the agenda.
6789 To aid sorting, you may revisit the original entries and add more text
6790 properties which will later be used by the sorting functions.
6792 The function should take a string argument, an agenda line.
6793 It has access to the text properties in that line, which contain among
6794 other things, the property `org-hd-marker' that points to the entry
6795 where the line comes from. Note that not all lines going into the agenda
6796 have this property, only most.
6798 The function should return the modified string. It is probably best
6799 to ONLY change text properties.
6801 You can also use this function as a filter, by returning nil for lines
6802 you don't want to have in the agenda at all. For this application, you
6803 could bind the variable in the options section of a custom command.")
6805 (defun org-agenda-finalize-entries (list &optional type)
6806 "Sort, limit and concatenate the LIST of agenda items.
6807 The optional argument TYPE tells the agenda type."
6808 (let ((max-effort (cond ((listp org-agenda-max-effort)
6809 (cdr (assoc type org-agenda-max-effort)))
6810 (t org-agenda-max-effort)))
6811 (max-todo (cond ((listp org-agenda-max-todos)
6812 (cdr (assoc type org-agenda-max-todos)))
6813 (t org-agenda-max-todos)))
6814 (max-tags (cond ((listp org-agenda-max-tags)
6815 (cdr (assoc type org-agenda-max-tags)))
6816 (t org-agenda-max-tags)))
6817 (max-entries (cond ((listp org-agenda-max-entries)
6818 (cdr (assoc type org-agenda-max-entries)))
6819 (t org-agenda-max-entries))))
6820 (when org-agenda-before-sorting-filter-function
6821 (setq list
6822 (delq nil
6823 (mapcar
6824 org-agenda-before-sorting-filter-function list))))
6825 (setq list (mapcar 'org-agenda-highlight-todo list)
6826 list (mapcar 'identity (sort list 'org-entries-lessp)))
6827 (when max-effort
6828 (setq list (org-agenda-limit-entries
6829 list 'effort-minutes max-effort
6830 (lambda (e) (or e (if org-sort-agenda-noeffort-is-high
6831 32767 -1))))))
6832 (when max-todo
6833 (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
6834 (when max-tags
6835 (setq list (org-agenda-limit-entries list 'tags max-tags)))
6836 (when max-entries
6837 (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
6838 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
6839 (setq list (mapcar #'org-agenda--mark-blocked-entry list)))
6840 (mapconcat 'identity list "\n")))
6842 (defun org-agenda-limit-entries (list prop limit &optional fn)
6843 "Limit the number of agenda entries."
6844 (let ((include (and limit (< limit 0))))
6845 (if limit
6846 (let ((fun (or fn (lambda (p) (if p 1))))
6847 (lim 0))
6848 (delq nil
6849 (mapcar
6850 (lambda (e)
6851 (let ((pval (funcall
6852 fun (get-text-property (1- (length e))
6853 prop e))))
6854 (if pval (setq lim (+ lim pval)))
6855 (cond ((and pval (<= lim (abs limit))) e)
6856 ((and include (not pval)) e))))
6857 list)))
6858 list)))
6860 (defun org-agenda-limit-interactively (remove)
6861 "In agenda, interactively limit entries to various maximums."
6862 (interactive "P")
6863 (if remove
6864 (progn (setq org-agenda-max-entries nil
6865 org-agenda-max-todos nil
6866 org-agenda-max-tags nil
6867 org-agenda-max-effort nil)
6868 (org-agenda-redo))
6869 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6870 (msg (cond ((= max ?E) "How many minutes? ")
6871 ((= max ?e) "How many entries? ")
6872 ((= max ?t) "How many TODO entries? ")
6873 ((= max ?T) "How many tagged entries? ")
6874 (t (user-error "Wrong input"))))
6875 (num (string-to-number (read-from-minibuffer msg))))
6876 (cond ((equal max ?e)
6877 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6878 ((equal max ?t)
6879 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6880 ((equal max ?T)
6881 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6882 ((equal max ?E)
6883 (let ((org-agenda-max-effort num)) (org-agenda-redo))))))
6884 (org-agenda-fit-window-to-buffer))
6886 (defun org-agenda-highlight-todo (x)
6887 (let ((org-done-keywords org-done-keywords-for-agenda)
6888 (case-fold-search nil)
6890 (if (eq x 'line)
6891 (save-excursion
6892 (beginning-of-line 1)
6893 (setq re (org-get-at-bol 'org-todo-regexp))
6894 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6895 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6896 (add-text-properties (match-beginning 0) (match-end 1)
6897 (list 'face (org-get-todo-face 1)))
6898 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6899 (delete-region (match-beginning 1) (1- (match-end 0)))
6900 (goto-char (match-beginning 1))
6901 (insert (format org-agenda-todo-keyword-format s)))))
6902 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6903 (setq re (get-text-property 0 'org-todo-regexp x))
6904 (when (and re
6905 ;; Test `pl' because if there's no heading content,
6906 ;; there's no point matching to highlight. Note
6907 ;; that if we didn't test `pl' first, and there
6908 ;; happened to be no keyword from `org-todo-regexp'
6909 ;; on this heading line, then the `equal' comparison
6910 ;; afterwards would spuriously succeed in the case
6911 ;; where `pl' is nil -- causing an args-out-of-range
6912 ;; error when we try to add text properties to text
6913 ;; that isn't there.
6915 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6916 x pl) pl))
6917 (add-text-properties
6918 (or (match-end 1) (match-end 0)) (match-end 0)
6919 (list 'face (org-get-todo-face (match-string 2 x)))
6921 (when (match-end 1)
6922 (setq x
6923 (concat
6924 (substring x 0 (match-end 1))
6925 (format org-agenda-todo-keyword-format
6926 (match-string 2 x))
6927 ;; Remove `display' property as the icon could leak
6928 ;; on the white space.
6929 (org-add-props " " (org-plist-delete (text-properties-at 0 x)
6930 'display))
6931 (substring x (match-end 3)))))))
6932 x)))
6934 (defsubst org-cmp-values (a b property)
6935 "Compare the numeric value of text PROPERTY for string A and B."
6936 (let ((pa (or (get-text-property (1- (length a)) property a) 0))
6937 (pb (or (get-text-property (1- (length b)) property b) 0)))
6938 (cond ((> pa pb) +1)
6939 ((< pa pb) -1))))
6941 (defsubst org-cmp-effort (a b)
6942 "Compare the effort values of string A and B."
6943 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6944 ;; `effort-minutes' property is not directly accessible from
6945 ;; the strings, but is stored as a property in `txt'.
6946 (ea (or (get-text-property
6947 0 'effort-minutes (get-text-property 0 'txt a))
6948 def))
6949 (eb (or (get-text-property
6950 0 'effort-minutes (get-text-property 0 'txt b))
6951 def)))
6952 (cond ((> ea eb) +1)
6953 ((< ea eb) -1))))
6955 (defsubst org-cmp-category (a b)
6956 "Compare the string values of categories of strings A and B."
6957 (let ((ca (or (get-text-property (1- (length a)) 'org-category a) ""))
6958 (cb (or (get-text-property (1- (length b)) 'org-category b) "")))
6959 (cond ((string-lessp ca cb) -1)
6960 ((string-lessp cb ca) +1))))
6962 (defsubst org-cmp-todo-state (a b)
6963 "Compare the todo states of strings A and B."
6964 (let* ((ma (or (get-text-property 1 'org-marker a)
6965 (get-text-property 1 'org-hd-marker a)))
6966 (mb (or (get-text-property 1 'org-marker b)
6967 (get-text-property 1 'org-hd-marker b)))
6968 (fa (and ma (marker-buffer ma)))
6969 (fb (and mb (marker-buffer mb)))
6970 (todo-kwds
6971 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6972 (and fb (with-current-buffer fb org-todo-keywords-1))))
6973 (ta (or (get-text-property 1 'todo-state a) ""))
6974 (tb (or (get-text-property 1 'todo-state b) ""))
6975 (la (- (length (member ta todo-kwds))))
6976 (lb (- (length (member tb todo-kwds))))
6977 (donepa (member ta org-done-keywords-for-agenda))
6978 (donepb (member tb org-done-keywords-for-agenda)))
6979 (cond ((and donepa (not donepb)) -1)
6980 ((and (not donepa) donepb) +1)
6981 ((< la lb) -1)
6982 ((< lb la) +1))))
6984 (defsubst org-cmp-alpha (a b)
6985 "Compare the headlines, alphabetically."
6986 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6987 (plb (text-property-any 0 (length b) 'org-heading t b))
6988 (ta (and pla (substring a pla)))
6989 (tb (and plb (substring b plb)))
6990 (case-fold-search nil))
6991 (when pla
6992 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6993 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6994 (setq ta (substring ta (match-end 0))))
6995 (setq ta (downcase ta)))
6996 (when plb
6997 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6998 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6999 (setq tb (substring tb (match-end 0))))
7000 (setq tb (downcase tb)))
7001 (cond ((not ta) +1)
7002 ((not tb) -1)
7003 ((string-lessp ta tb) -1)
7004 ((string-lessp tb ta) +1))))
7006 (defsubst org-cmp-tag (a b)
7007 "Compare the string values of the first tags of A and B."
7008 (let ((ta (car (last (get-text-property 1 'tags a))))
7009 (tb (car (last (get-text-property 1 'tags b)))))
7010 (cond ((not ta) +1)
7011 ((not tb) -1)
7012 ((string-lessp ta tb) -1)
7013 ((string-lessp tb ta) +1))))
7015 (defsubst org-cmp-time (a b)
7016 "Compare the time-of-day values of strings A and B."
7017 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
7018 (ta (or (get-text-property 1 'time-of-day a) def))
7019 (tb (or (get-text-property 1 'time-of-day b) def)))
7020 (cond ((< ta tb) -1)
7021 ((< tb ta) +1))))
7023 (defsubst org-cmp-ts (a b type)
7024 "Compare the timestamps values of entries A and B.
7025 When TYPE is \"scheduled\", \"deadline\", \"timestamp\" or
7026 \"timestamp_ia\", compare within each of these type. When TYPE
7027 is the empty string, compare all timestamps without respect of
7028 their type."
7029 (let* ((def (if org-sort-agenda-notime-is-late most-positive-fixnum -1))
7030 (ta (or (and (string-match type (or (get-text-property 1 'type a) ""))
7031 (get-text-property 1 'ts-date a))
7032 def))
7033 (tb (or (and (string-match type (or (get-text-property 1 'type b) ""))
7034 (get-text-property 1 'ts-date b))
7035 def)))
7036 (cond ((< ta tb) -1)
7037 ((< tb ta) +1))))
7039 (defsubst org-cmp-habit-p (a b)
7040 "Compare the todo states of strings A and B."
7041 (let ((ha (get-text-property 1 'org-habit-p a))
7042 (hb (get-text-property 1 'org-habit-p b)))
7043 (cond ((and ha (not hb)) -1)
7044 ((and (not ha) hb) +1))))
7046 (defun org-entries-lessp (a b)
7047 "Predicate for sorting agenda entries."
7048 ;; The following variables will be used when the form is evaluated.
7049 ;; So even though the compiler complains, keep them.
7050 (let* ((ss org-agenda-sorting-strategy-selected)
7051 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
7052 (org-cmp-ts a b "")))
7053 (timestamp-down (if timestamp-up (- timestamp-up) nil))
7054 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
7055 (org-cmp-ts a b "scheduled")))
7056 (scheduled-down (if scheduled-up (- scheduled-up) nil))
7057 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
7058 (org-cmp-ts a b "deadline")))
7059 (deadline-down (if deadline-up (- deadline-up) nil))
7060 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
7061 (org-cmp-ts a b "timestamp_ia")))
7062 (tsia-down (if tsia-up (- tsia-up) nil))
7063 (ts-up (and (org-em 'ts-up 'ts-down ss)
7064 (org-cmp-ts a b "timestamp")))
7065 (ts-down (if ts-up (- ts-up) nil))
7066 (time-up (and (org-em 'time-up 'time-down ss)
7067 (org-cmp-time a b)))
7068 (time-down (if time-up (- time-up) nil))
7069 (stats-up (and (org-em 'stats-up 'stats-down ss)
7070 (org-cmp-values a b 'org-stats)))
7071 (stats-down (if stats-up (- stats-up) nil))
7072 (priority-up (and (org-em 'priority-up 'priority-down ss)
7073 (org-cmp-values a b 'priority)))
7074 (priority-down (if priority-up (- priority-up) nil))
7075 (effort-up (and (org-em 'effort-up 'effort-down ss)
7076 (org-cmp-effort a b)))
7077 (effort-down (if effort-up (- effort-up) nil))
7078 (category-up (and (or (org-em 'category-up 'category-down ss)
7079 (memq 'category-keep ss))
7080 (org-cmp-category a b)))
7081 (category-down (if category-up (- category-up) nil))
7082 (category-keep (if category-up +1 nil))
7083 (tag-up (and (org-em 'tag-up 'tag-down ss)
7084 (org-cmp-tag a b)))
7085 (tag-down (if tag-up (- tag-up) nil))
7086 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
7087 (org-cmp-todo-state a b)))
7088 (todo-state-down (if todo-state-up (- todo-state-up) nil))
7089 (habit-up (and (org-em 'habit-up 'habit-down ss)
7090 (org-cmp-habit-p a b)))
7091 (habit-down (if habit-up (- habit-up) nil))
7092 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
7093 (org-cmp-alpha a b)))
7094 (alpha-down (if alpha-up (- alpha-up) nil))
7095 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
7096 user-defined-up user-defined-down)
7097 (if (and need-user-cmp org-agenda-cmp-user-defined
7098 (functionp org-agenda-cmp-user-defined))
7099 (setq user-defined-up
7100 (funcall org-agenda-cmp-user-defined a b)
7101 user-defined-down (if user-defined-up (- user-defined-up) nil)))
7102 (cdr (assoc
7103 (eval (cons 'or org-agenda-sorting-strategy-selected))
7104 '((-1 . t) (1 . nil) (nil . nil))))))
7106 ;;; Agenda restriction lock
7108 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
7109 "Overlay to mark the headline to which agenda commands are restricted.")
7110 (overlay-put org-agenda-restriction-lock-overlay
7111 'face 'org-agenda-restriction-lock)
7112 (overlay-put org-agenda-restriction-lock-overlay
7113 'help-echo "Agendas are currently limited to this subtree.")
7114 (delete-overlay org-agenda-restriction-lock-overlay)
7116 (defun org-agenda-set-restriction-lock-from-agenda (arg)
7117 "Set the restriction lock to the agenda item at point from within the agenda.
7118 When called with a `\\[universal-argument]' prefix, restrict to
7119 the file which contains the item.
7120 Argument ARG is the prefix argument."
7121 (interactive "P")
7122 (unless (derived-mode-p 'org-agenda-mode)
7123 (user-error "Not in an Org agenda buffer"))
7124 (let* ((marker (or (org-get-at-bol 'org-marker)
7125 (org-agenda-error)))
7126 (buffer (marker-buffer marker))
7127 (pos (marker-position marker)))
7128 (with-current-buffer buffer
7129 (goto-char pos)
7130 (org-agenda-set-restriction-lock arg))))
7132 ;;;###autoload
7133 (defun org-agenda-set-restriction-lock (&optional type)
7134 "Set restriction lock for agenda, to current subtree or file.
7135 Restriction will be the file if TYPE is `file', or if type is the
7136 universal prefix \\='(4), or if the cursor is before the first headline
7137 in the file. Otherwise, restriction will be to the current subtree."
7138 (interactive "P")
7139 (org-agenda-remove-restriction-lock 'noupdate)
7140 (and (equal type '(4)) (setq type 'file))
7141 (setq type (cond
7142 (type type)
7143 ((org-at-heading-p) 'subtree)
7144 ((condition-case nil (org-back-to-heading t) (error nil))
7145 'subtree)
7146 (t 'file)))
7147 (if (eq type 'subtree)
7148 (progn
7149 (setq org-agenda-restrict (current-buffer))
7150 (setq org-agenda-overriding-restriction 'subtree)
7151 (put 'org-agenda-files 'org-restrict
7152 (list (buffer-file-name (buffer-base-buffer))))
7153 (org-back-to-heading t)
7154 (move-overlay org-agenda-restriction-lock-overlay
7155 (point)
7156 (if org-agenda-restriction-lock-highlight-subtree
7157 (save-excursion (org-end-of-subtree t t) (point))
7158 (point-at-eol)))
7159 (move-marker org-agenda-restrict-begin (point))
7160 (move-marker org-agenda-restrict-end
7161 (save-excursion (org-end-of-subtree t t)))
7162 (message "Locking agenda restriction to subtree"))
7163 (put 'org-agenda-files 'org-restrict
7164 (list (buffer-file-name (buffer-base-buffer))))
7165 (setq org-agenda-restrict nil)
7166 (setq org-agenda-overriding-restriction 'file)
7167 (move-marker org-agenda-restrict-begin nil)
7168 (move-marker org-agenda-restrict-end nil)
7169 (message "Locking agenda restriction to file"))
7170 (setq current-prefix-arg nil)
7171 (org-agenda-maybe-redo))
7173 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7174 "Remove the agenda restriction lock."
7175 (interactive "P")
7176 (delete-overlay org-agenda-restriction-lock-overlay)
7177 (delete-overlay org-speedbar-restriction-lock-overlay)
7178 (setq org-agenda-overriding-restriction nil)
7179 (setq org-agenda-restrict nil)
7180 (put 'org-agenda-files 'org-restrict nil)
7181 (move-marker org-agenda-restrict-begin nil)
7182 (move-marker org-agenda-restrict-end nil)
7183 (setq current-prefix-arg nil)
7184 (message "Agenda restriction lock removed")
7185 (or noupdate (org-agenda-maybe-redo)))
7187 (defun org-agenda-maybe-redo ()
7188 "If there is any window showing the agenda view, update it."
7189 (let ((w (get-buffer-window (or org-agenda-this-buffer-name
7190 org-agenda-buffer-name)
7192 (w0 (selected-window)))
7193 (when w
7194 (select-window w)
7195 (org-agenda-redo)
7196 (select-window w0)
7197 (if org-agenda-overriding-restriction
7198 (message "Agenda view shifted to new %s restriction"
7199 org-agenda-overriding-restriction)
7200 (message "Agenda restriction lock removed")))))
7202 ;;; Agenda commands
7204 (defun org-agenda-check-type (error &rest types)
7205 "Check if agenda buffer is of allowed type.
7206 If ERROR is non-nil, throw an error, otherwise just return nil.
7207 Allowed types are `agenda' `todo' `tags' `search'."
7208 (cond ((not org-agenda-type)
7209 (error "No Org agenda currently displayed"))
7210 ((memq org-agenda-type types) t)
7211 (error
7212 (error "Not allowed in %s-type agenda buffers" org-agenda-type))
7213 (t nil)))
7215 (defun org-agenda-Quit ()
7216 "Exit the agenda, killing the agenda buffer.
7217 Like `org-agenda-quit', but kill the buffer even when
7218 `org-agenda-sticky' is non-nil."
7219 (interactive)
7220 (org-agenda--quit))
7222 (defun org-agenda-quit ()
7223 "Exit the agenda.
7225 When `org-agenda-sticky' is non-nil, bury the agenda buffer
7226 instead of killing it.
7228 When `org-agenda-restore-windows-after-quit' is non-nil, restore
7229 the pre-agenda window configuration.
7231 When column view is active, exit column view instead of the
7232 agenda."
7233 (interactive)
7234 (org-agenda--quit org-agenda-sticky))
7236 (defun org-agenda--quit (&optional bury)
7237 (if org-agenda-columns-active
7238 (org-columns-quit)
7239 (let ((wconf org-agenda-pre-window-conf)
7240 (buf (current-buffer))
7241 (org-agenda-last-indirect-window
7242 (and (eq org-indirect-buffer-display 'other-window)
7243 org-agenda-last-indirect-buffer
7244 (get-buffer-window org-agenda-last-indirect-buffer))))
7245 (cond
7246 ((eq org-agenda-window-setup 'other-frame)
7247 (delete-frame))
7248 ((and org-agenda-restore-windows-after-quit
7249 wconf)
7250 ;; Maybe restore the pre-agenda window configuration. Reset
7251 ;; `org-agenda-pre-window-conf' before running
7252 ;; `set-window-configuration', which loses the current buffer.
7253 (setq org-agenda-pre-window-conf nil)
7254 (set-window-configuration wconf))
7256 (when org-agenda-last-indirect-window
7257 (delete-window org-agenda-last-indirect-window))
7258 (and (not (eq org-agenda-window-setup 'current-window))
7259 (not (one-window-p))
7260 (delete-window))))
7261 (if bury
7262 ;; Set the agenda buffer as the current buffer instead of
7263 ;; passing it as an argument to `bury-buffer' so that
7264 ;; `bury-buffer' removes it from the window.
7265 (with-current-buffer buf
7266 (bury-buffer))
7267 (kill-buffer buf)
7268 (setq org-agenda-archives-mode nil
7269 org-agenda-buffer nil)))))
7271 (defun org-agenda-exit ()
7272 "Exit the agenda, killing Org buffers loaded by the agenda.
7273 Like `org-agenda-Quit', but kill any buffers that were created by
7274 the agenda. Org buffers visited directly by the user will not be
7275 touched. Also, exit the agenda even if it is in column view."
7276 (interactive)
7277 (when org-agenda-columns-active
7278 (org-columns-quit))
7279 (org-release-buffers org-agenda-new-buffers)
7280 (setq org-agenda-new-buffers nil)
7281 (org-agenda-Quit))
7283 (defun org-agenda-kill-all-agenda-buffers ()
7284 "Kill all buffers in `org-agenda-mode'.
7285 This is used when toggling sticky agendas."
7286 (interactive)
7287 (let (blist)
7288 (dolist (buf (buffer-list))
7289 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7290 (push buf blist)))
7291 (mapc 'kill-buffer blist)))
7293 (defun org-agenda-execute (arg)
7294 "Execute another agenda command, keeping same window.
7295 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7296 in the agenda."
7297 (interactive "P")
7298 (let ((org-agenda-window-setup 'current-window))
7299 (org-agenda arg)))
7301 (defun org-agenda-redo (&optional all)
7302 "Rebuild possibly ALL agenda view(s) in the current buffer."
7303 (interactive "P")
7304 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7305 (cpa (unless (eq all t) current-prefix-arg))
7306 (org-agenda-doing-sticky-redo org-agenda-sticky)
7307 (org-agenda-sticky nil)
7308 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7309 org-agenda-buffer-name))
7310 (org-agenda-keep-modes t)
7311 (tag-filter org-agenda-tag-filter)
7312 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7313 (top-hl-filter org-agenda-top-headline-filter)
7314 (cat-filter org-agenda-category-filter)
7315 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7316 (re-filter org-agenda-regexp-filter)
7317 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7318 (effort-filter org-agenda-effort-filter)
7319 (effort-preset (get 'org-agenda-effort-filter :preset-filter))
7320 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7321 (cols org-agenda-columns-active)
7322 (line (org-current-line))
7323 (window-line (- line (org-current-line (window-start))))
7324 (lprops org-agenda-lprops)
7325 (redo-cmd (get-text-property p 'org-redo-cmd))
7326 (last-args (get-text-property p 'org-last-args))
7327 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7328 (org-agenda-overriding-cmd-arguments
7329 (unless (eq all t)
7330 (cond ((listp last-args)
7331 (cons (or cpa (car last-args)) (cdr last-args)))
7332 ((stringp last-args)
7333 last-args))))
7334 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7335 (put 'org-agenda-tag-filter :preset-filter nil)
7336 (put 'org-agenda-category-filter :preset-filter nil)
7337 (put 'org-agenda-regexp-filter :preset-filter nil)
7338 (put 'org-agenda-effort-filter :preset-filter nil)
7339 (and cols (org-columns-quit))
7340 (message "Rebuilding agenda buffer...")
7341 (if series-redo-cmd
7342 (eval series-redo-cmd)
7343 (org-let lprops redo-cmd))
7344 (setq org-agenda-undo-list nil
7345 org-agenda-pending-undo-list nil
7346 org-agenda-tag-filter tag-filter
7347 org-agenda-category-filter cat-filter
7348 org-agenda-regexp-filter re-filter
7349 org-agenda-effort-filter effort-filter
7350 org-agenda-top-headline-filter top-hl-filter)
7351 (message "Rebuilding agenda buffer...done")
7352 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7353 (put 'org-agenda-category-filter :preset-filter cat-preset)
7354 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7355 (put 'org-agenda-effort-filter :preset-filter effort-preset)
7356 (let ((tag (or tag-filter tag-preset))
7357 (cat (or cat-filter cat-preset))
7358 (effort (or effort-filter effort-preset))
7359 (re (or re-filter re-preset)))
7360 (when tag (org-agenda-filter-apply tag 'tag t))
7361 (when cat (org-agenda-filter-apply cat 'category))
7362 (when effort (org-agenda-filter-apply effort 'effort))
7363 (when re (org-agenda-filter-apply re 'regexp)))
7364 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7365 (and cols (called-interactively-p 'any) (org-agenda-columns))
7366 (org-goto-line line)
7367 (recenter window-line)))
7369 (defun org-agenda-redo-all (&optional exhaustive)
7370 "Rebuild all agenda views in the current buffer.
7371 With a prefix argument, do so in all agenda buffers."
7372 (interactive "P")
7373 (if exhaustive
7374 (dolist (buffer (buffer-list))
7375 (with-current-buffer buffer
7376 (when (derived-mode-p 'org-agenda-mode)
7377 (org-agenda-redo t))))
7378 (org-agenda-redo t)))
7380 (defvar org-global-tags-completion-table nil)
7381 (defvar org-agenda-filter-form nil)
7382 (defvar org-agenda-filtered-by-category nil)
7384 (defun org-agenda-filter-by-category (strip)
7385 "Filter lines in the agenda buffer that have a specific category.
7386 The category is that of the current line.
7387 Without prefix argument, keep only the lines of that category.
7388 With a prefix argument, exclude the lines of that category.
7390 (interactive "P")
7391 (if (and org-agenda-filtered-by-category
7392 org-agenda-category-filter)
7393 (org-agenda-filter-show-all-cat)
7394 (let ((cat (org-no-properties (org-get-at-eol 'org-category 1))))
7395 (cond
7396 ((and cat strip)
7397 (org-agenda-filter-apply
7398 (push (concat "-" cat) org-agenda-category-filter) 'category))
7399 (cat
7400 (org-agenda-filter-apply
7401 (setq org-agenda-category-filter
7402 (list (concat "+" cat))) 'category))
7403 (t (error "No category at point"))))))
7405 (defun org-find-top-headline (&optional pos)
7406 "Find the topmost parent headline and return it.
7407 POS when non-nil is the marker or buffer position to start the
7408 search from."
7409 (save-excursion
7410 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
7411 (when pos (goto-char pos))
7412 ;; Skip up to the topmost parent.
7413 (while (org-up-heading-safe))
7414 (ignore-errors (nth 4 (org-heading-components))))))
7416 (defvar org-agenda-filtered-by-top-headline nil)
7417 (defun org-agenda-filter-by-top-headline (strip)
7418 "Keep only those lines that are descendants from the same top headline.
7419 The top headline is that of the current line."
7420 (interactive "P")
7421 (if org-agenda-filtered-by-top-headline
7422 (progn
7423 (setq org-agenda-filtered-by-top-headline nil
7424 org-agenda-top-headline-filter nil)
7425 (org-agenda-filter-show-all-top-filter))
7426 (let ((toph (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7427 (if toph (org-agenda-filter-top-headline-apply toph strip)
7428 (error "No top-level headline at point")))))
7430 (defvar org-agenda-regexp-filter nil)
7431 (defun org-agenda-filter-by-regexp (strip)
7432 "Filter agenda entries by a regular expression.
7433 Regexp filters are cumulative.
7434 With no prefix argument, keep entries matching the regexp.
7435 With one prefix argument, filter out entries matching the regexp.
7436 With two prefix arguments, remove the regexp filters."
7437 (interactive "P")
7438 (if (not (equal strip '(16)))
7439 (let ((flt (concat (if (equal strip '(4)) "-" "+")
7440 (read-from-minibuffer
7441 (if (equal strip '(4))
7442 "Filter out entries matching regexp: "
7443 "Narrow to entries matching regexp: ")))))
7444 (push flt org-agenda-regexp-filter)
7445 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
7446 (org-agenda-filter-show-all-re)
7447 (message "Regexp filter removed")))
7449 (defvar org-agenda-effort-filter nil)
7450 (defun org-agenda-filter-by-effort (strip)
7451 "Filter agenda entries by effort.
7452 With no prefix argument, keep entries matching the effort condition.
7453 With one prefix argument, filter out entries matching the condition.
7454 With two prefix arguments, remove the effort filters."
7455 (interactive "P")
7456 (cond
7457 ((member strip '(nil 4))
7458 (let* ((efforts (split-string
7459 (or (cdr (assoc (concat org-effort-property "_ALL")
7460 org-global-properties))
7461 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00")))
7462 ;; XXX: the following handles only up to 10 different
7463 ;; effort values.
7464 (allowed-keys (if (null efforts) nil
7465 (mapcar (lambda (n) (mod n 10)) ;turn 10 into 0
7466 (number-sequence 1 (length efforts)))))
7467 (op nil))
7468 (while (not (memq op '(?< ?> ?=)))
7469 (setq op (read-char-exclusive "Effort operator? (> = or <)")))
7470 ;; Select appropriate duration. Ignore non-digit characters.
7471 (let ((prompt
7472 (apply #'format
7473 (concat "Effort %c "
7474 (mapconcat (lambda (s) (concat "[%d]" s))
7475 efforts
7476 " "))
7477 op allowed-keys))
7478 (eff -1))
7479 (while (not (memq eff allowed-keys))
7480 (message prompt)
7481 (setq eff (- (read-char-exclusive) 48)))
7482 (setq org-agenda-effort-filter
7483 (list (concat (if strip "-" "+")
7484 (char-to-string op)
7485 ;; Numbering is 1 2 3 ... 9 0, but we want
7486 ;; 0 1 2 ... 8 9.
7487 (nth (mod (1- eff) 10) efforts)))))
7488 (org-agenda-filter-apply org-agenda-effort-filter 'effort)))
7489 (t (org-agenda-filter-show-all-effort)
7490 (message "Effort filter removed"))))
7492 (defun org-agenda-filter-remove-all ()
7493 "Remove all filters from the current agenda buffer."
7494 (interactive)
7495 (when org-agenda-tag-filter
7496 (org-agenda-filter-show-all-tag))
7497 (when org-agenda-category-filter
7498 (org-agenda-filter-show-all-cat))
7499 (when org-agenda-regexp-filter
7500 (org-agenda-filter-show-all-re))
7501 (when org-agenda-top-headline-filter
7502 (org-agenda-filter-show-all-top-filter))
7503 (when org-agenda-effort-filter
7504 (org-agenda-filter-show-all-effort))
7505 (org-agenda-finalize))
7507 (defun org-agenda-filter-by-tag (arg &optional char exclude)
7508 "Keep only those lines in the agenda buffer that have a specific tag.
7510 The tag is selected with its fast selection letter, as configured.
7512 With a `\\[universal-argument]' prefix, exclude the agenda search.
7514 With a `\\[universal-argument] \\[universal-argument]' prefix, filter the literal tag, \
7515 i.e. don't
7516 filter on all its group members.
7518 A lisp caller can specify CHAR. EXCLUDE means that the new tag
7519 should be used to exclude the search - the interactive user can
7520 also press `-' or `+' to switch between filtering and excluding."
7521 (interactive "P")
7522 (let* ((alist org-tag-alist-for-agenda)
7523 (tag-chars (mapconcat
7524 (lambda (x) (if (and (not (symbolp (car x)))
7525 (cdr x))
7526 (char-to-string (cdr x))
7527 ""))
7528 org-tag-alist-for-agenda ""))
7529 (valid-char-list (append '(?\t ?\r ?/ ?. ?\s ?q)
7530 (string-to-list tag-chars)))
7531 (exclude (or exclude (equal arg '(4))))
7532 (expand (not (equal arg '(16))))
7533 (inhibit-read-only t)
7534 (current org-agenda-tag-filter)
7535 a n tag)
7536 (unless char
7537 (while (not (memq char valid-char-list))
7538 (message
7539 "%s by tag [%s ]:tag-char, [TAB]:tag, %s[/]:off, [+/-]:filter/exclude%s, [q]:quit"
7540 (if exclude "Exclude" "Filter")
7541 tag-chars
7542 (if org-agenda-auto-exclude-function "[RET], " "")
7543 (if expand "" ", no grouptag expand"))
7544 (setq char (read-char-exclusive))
7545 ;; Excluding or filtering down
7546 (cond ((eq char ?-) (setq exclude t))
7547 ((eq char ?+) (setq exclude nil)))))
7548 (when (eq char ?\t)
7549 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7550 (setq-local org-global-tags-completion-table
7551 (org-global-tags-completion-table)))
7552 (let ((completion-ignore-case t))
7553 (setq tag (completing-read
7554 "Tag: " org-global-tags-completion-table nil t))))
7555 (cond
7556 ((eq char ?\r)
7557 (org-agenda-filter-show-all-tag)
7558 (when org-agenda-auto-exclude-function
7559 (setq org-agenda-tag-filter nil)
7560 (dolist (tag (org-agenda-get-represented-tags))
7561 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7562 (if modifier
7563 (push modifier org-agenda-tag-filter))))
7564 (if (not (null org-agenda-tag-filter))
7565 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))))
7566 ((eq char ?/)
7567 (org-agenda-filter-show-all-tag)
7568 (when (get 'org-agenda-tag-filter :preset-filter)
7569 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand)))
7570 ((eq char ?.)
7571 (setq org-agenda-tag-filter
7572 (mapcar (lambda(tag) (concat "+" tag))
7573 (org-get-at-bol 'tags)))
7574 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7575 ((eq char ?q)) ;If q, abort (even if there is a q-key for a tag...)
7576 ((or (eq char ?\s)
7577 (setq a (rassoc char alist))
7578 (and tag (setq a (cons tag nil))))
7579 (org-agenda-filter-show-all-tag)
7580 (setq tag (car a))
7581 (setq org-agenda-tag-filter
7582 (cons (concat (if exclude "-" "+") tag)
7583 current))
7584 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7585 (t (error "Invalid tag selection character %c" char)))))
7587 (defun org-agenda-get-represented-tags ()
7588 "Get a list of all tags currently represented in the agenda."
7589 (let (p tags)
7590 (save-excursion
7591 (goto-char (point-min))
7592 (while (setq p (next-single-property-change (point) 'tags))
7593 (goto-char p)
7594 (mapc (lambda (x) (add-to-list 'tags x))
7595 (get-text-property (point) 'tags))))
7596 tags))
7599 (defun org-agenda-filter-make-matcher (filter type &optional expand)
7600 "Create the form that tests a line for agenda filter. Optional
7601 argument EXPAND can be used for the TYPE tag and will expand the
7602 tags in the FILTER if any of the tags in FILTER are grouptags."
7603 (let (f f1)
7604 (cond
7605 ;; Tag filter
7606 ((eq type 'tag)
7607 (setq filter
7608 (delete-dups
7609 (append (get 'org-agenda-tag-filter :preset-filter)
7610 filter)))
7611 (dolist (x filter)
7612 (let ((op (string-to-char x)))
7613 (if expand (setq x (org-agenda-filter-expand-tags (list x) t))
7614 (setq x (list x)))
7615 (setq f1 (org-agenda-filter-make-matcher-tag-exp x op))
7616 (push f1 f))))
7617 ;; Category filter
7618 ((eq type 'category)
7619 (setq filter
7620 (delete-dups
7621 (append (get 'org-agenda-category-filter :preset-filter)
7622 filter)))
7623 (dolist (x filter)
7624 (if (equal "-" (substring x 0 1))
7625 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7626 (setq f1 (list 'equal (substring x 1) 'cat)))
7627 (push f1 f)))
7628 ;; Regexp filter
7629 ((eq type 'regexp)
7630 (setq filter
7631 (delete-dups
7632 (append (get 'org-agenda-regexp-filter :preset-filter)
7633 filter)))
7634 (dolist (x filter)
7635 (if (equal "-" (substring x 0 1))
7636 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7637 (setq f1 (list 'string-match (substring x 1) 'txt)))
7638 (push f1 f)))
7639 ;; Effort filter
7640 ((eq type 'effort)
7641 (setq filter
7642 (delete-dups
7643 (append (get 'org-agenda-effort-filter :preset-filter)
7644 filter)))
7645 (dolist (x filter)
7646 (push (org-agenda-filter-effort-form x) f))))
7647 (cons 'and (nreverse f))))
7649 (defun org-agenda-filter-make-matcher-tag-exp (tags op)
7650 "Return a form associated to tag-expression TAGS.
7651 Build a form testing a line for agenda filter for
7652 tag-expressions. OP is an operator of type CHAR that allows the
7653 function to set the right switches in the returned form."
7654 (let (form)
7655 ;; Any of the expressions can match if OP is +, all must match if
7656 ;; the operator is -.
7657 (dolist (x tags (cons (if (eq op ?-) 'and 'or) form))
7658 (let* ((tag (substring x 1))
7659 (f (cond
7660 ((string= "" tag) '(not tags))
7661 ((and (string-match-p "\\`{" tag) (string-match-p "}\\'" tag))
7662 ;; TAG is a regexp.
7663 (list 'org-match-any-p (substring tag 1 -1) 'tags))
7664 (t (list 'member (downcase tag) 'tags)))))
7665 (push (if (eq op ?-) (list 'not f) f) form)))))
7667 (defun org-agenda-filter-effort-form (e)
7668 "Return the form to compare the effort of the current line with what E says.
7669 E looks like \"+<2:25\"."
7670 (let (op)
7671 (setq e (substring e 1))
7672 (setq op (string-to-char e) e (substring e 1))
7673 (setq op (cond ((equal op ?<) '<=)
7674 ((equal op ?>) '>=)
7675 ((equal op ??) op)
7676 (t '=)))
7677 (list 'org-agenda-compare-effort (list 'quote op)
7678 (org-duration-to-minutes e))))
7680 (defun org-agenda-compare-effort (op value)
7681 "Compare the effort of the current line with VALUE, using OP.
7682 If the line does not have an effort defined, return nil."
7683 ;; `effort-minutes' property cannot be extracted directly from
7684 ;; current line but is stored as a property in `txt'.
7685 (let ((effort (get-text-property 0 'effort-minutes (org-get-at-bol 'txt))))
7686 (funcall op
7687 (or effort (if org-sort-agenda-noeffort-is-high 32767 -1))
7688 value)))
7690 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7691 "Expand group tags in FILTER for the agenda.
7692 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7693 (if org-group-tags
7694 (let ((case-fold-search t) rtn)
7695 (mapc
7696 (lambda (f)
7697 (let (f0 dir)
7698 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7699 (setq dir (match-string 1 f) f0 (match-string 2 f))
7700 (setq dir (if no-operator "" "+") f0 f))
7701 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7702 (org-tags-expand f0 t t))
7703 rtn))))
7704 filter)
7705 (reverse rtn))
7706 filter))
7708 (defun org-agenda-filter-apply (filter type &optional expand)
7709 "Set FILTER as the new agenda filter and apply it. Optional
7710 argument EXPAND can be used for the TYPE tag and will expand the
7711 tags in the FILTER if any of the tags in FILTER are grouptags."
7712 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7713 (if org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7714 (let (tags cat txt)
7715 (setq org-agenda-filter-form (org-agenda-filter-make-matcher filter type expand))
7716 ;; Only set `org-agenda-filtered-by-category' to t when a unique
7717 ;; category is used as the filter:
7718 (setq org-agenda-filtered-by-category
7719 (and (eq type 'category)
7720 (not (equal (substring (car filter) 0 1) "-"))))
7721 (org-agenda-set-mode-name)
7722 (save-excursion
7723 (goto-char (point-min))
7724 (while (not (eobp))
7725 (if (org-get-at-bol 'org-marker)
7726 (progn
7727 (setq tags (org-get-at-bol 'tags)
7728 cat (org-get-at-eol 'org-category 1)
7729 txt (org-get-at-bol 'txt))
7730 (if (not (eval org-agenda-filter-form))
7731 (org-agenda-filter-hide-line type))
7732 (beginning-of-line 2))
7733 (beginning-of-line 2))))
7734 (if (get-char-property (point) 'invisible)
7735 (ignore-errors (org-agenda-previous-line)))))
7737 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7738 "Filter by top headline HL."
7739 (org-agenda-set-mode-name)
7740 (save-excursion
7741 (goto-char (point-min))
7742 (while (not (eobp))
7743 (let* ((pos (org-get-at-bol 'org-hd-marker))
7744 (tophl (and pos (org-find-top-headline pos))))
7745 (if (and tophl (funcall (if negative 'identity 'not)
7746 (string= hl tophl)))
7747 (org-agenda-filter-hide-line 'top-headline)))
7748 (beginning-of-line 2)))
7749 (if (get-char-property (point) 'invisible)
7750 (org-agenda-previous-line))
7751 (setq org-agenda-top-headline-filter hl
7752 org-agenda-filtered-by-top-headline t))
7754 (defun org-agenda-filter-hide-line (type)
7755 "Hide lines with TYPE in the agenda buffer."
7756 (let* ((b (max (point-min) (1- (point-at-bol))))
7757 (e (point-at-eol)))
7758 (let ((inhibit-read-only t))
7759 (add-text-properties
7760 b e `(invisible org-filtered org-filter-type ,type)))))
7762 (defun org-agenda-remove-filter (type)
7763 (interactive)
7764 "Remove filter of type TYPE from the agenda buffer."
7765 (save-excursion
7766 (goto-char (point-min))
7767 (let ((inhibit-read-only t) pos)
7768 (while (setq pos (text-property-any (point) (point-max) 'org-filter-type type))
7769 (goto-char pos)
7770 (remove-text-properties
7771 (point) (next-single-property-change (point) 'org-filter-type)
7772 `(invisible org-filtered org-filter-type ,type))))
7773 (set (intern (format "org-agenda-%s-filter" (intern-soft type))) nil)
7774 (setq org-agenda-filter-form nil)
7775 (org-agenda-set-mode-name)
7776 (org-agenda-finalize)))
7778 (defun org-agenda-filter-show-all-tag nil
7779 (org-agenda-remove-filter 'tag))
7780 (defun org-agenda-filter-show-all-re nil
7781 (org-agenda-remove-filter 'regexp))
7782 (defun org-agenda-filter-show-all-effort nil
7783 (org-agenda-remove-filter 'effort))
7784 (defun org-agenda-filter-show-all-cat nil
7785 (org-agenda-remove-filter 'category))
7786 (defun org-agenda-filter-show-all-top-filter nil
7787 (org-agenda-remove-filter 'top-headline))
7789 (defun org-agenda-manipulate-query-add ()
7790 "Manipulate the query by adding a search term with positive selection.
7791 Positive selection means the term must be matched for selection of an entry."
7792 (interactive)
7793 (org-agenda-manipulate-query ?\[))
7794 (defun org-agenda-manipulate-query-subtract ()
7795 "Manipulate the query by adding a search term with negative selection.
7796 Negative selection means term must not be matched for selection of an entry."
7797 (interactive)
7798 (org-agenda-manipulate-query ?\]))
7799 (defun org-agenda-manipulate-query-add-re ()
7800 "Manipulate the query by adding a search regexp with positive selection.
7801 Positive selection means the regexp must match for selection of an entry."
7802 (interactive)
7803 (org-agenda-manipulate-query ?\{))
7804 (defun org-agenda-manipulate-query-subtract-re ()
7805 "Manipulate the query by adding a search regexp with negative selection.
7806 Negative selection means regexp must not match for selection of an entry."
7807 (interactive)
7808 (org-agenda-manipulate-query ?\}))
7809 (defun org-agenda-manipulate-query (char)
7810 (cond
7811 ((eq org-agenda-type 'agenda)
7812 (let ((org-agenda-include-inactive-timestamps t))
7813 (org-agenda-redo))
7814 (message "Display now includes inactive timestamps as well"))
7815 ((eq org-agenda-type 'search)
7816 (org-add-to-string
7817 'org-agenda-query-string
7818 (if org-agenda-last-search-view-search-was-boolean
7819 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7820 (?\{ . " +{}") (?\} . " -{}"))))
7821 " "))
7822 (setq org-agenda-redo-command
7823 (list 'org-search-view
7824 (car (get-text-property (min (1- (point-max)) (point))
7825 'org-last-args))
7826 org-agenda-query-string
7827 (+ (length org-agenda-query-string)
7828 (if (member char '(?\{ ?\})) 0 1))))
7829 (set-register org-agenda-query-register org-agenda-query-string)
7830 (let ((org-agenda-overriding-arguments
7831 (cdr org-agenda-redo-command)))
7832 (org-agenda-redo)))
7833 (t (error "Cannot manipulate query for %s-type agenda buffers"
7834 org-agenda-type))))
7836 (defun org-add-to-string (var string)
7837 (set var (concat (symbol-value var) string)))
7839 (defun org-agenda-goto-date (span)
7840 "Jump to DATE in agenda."
7841 (interactive "P")
7842 (let* ((org-read-date-prefer-future
7843 (eval org-agenda-jump-prefer-future))
7844 (date (org-read-date))
7845 (day (time-to-days (org-time-string-to-time date)))
7846 (org-agenda-sticky-orig org-agenda-sticky)
7847 (org-agenda-buffer-tmp-name (buffer-name))
7848 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7849 (0-arg (or current-prefix-arg (car args)))
7850 (2-arg (nth 2 args))
7851 (with-hour-p (nth 4 org-agenda-redo-command))
7852 (newcmd (list 'org-agenda-list 0-arg date
7853 (org-agenda-span-to-ndays
7854 2-arg (org-time-string-to-absolute date))
7855 with-hour-p))
7856 (newargs (cdr newcmd))
7857 (inhibit-read-only t)
7858 org-agenda-sticky)
7859 (if (not (org-agenda-check-type t 'agenda))
7860 (error "Not available in non-agenda views")
7861 (add-text-properties (point-min) (point-max)
7862 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7863 (org-agenda-redo)
7864 (goto-char (point-min))
7865 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7866 (save-excursion (move-beginning-of-line 2) (eobp))))
7867 (move-beginning-of-line 2))
7868 (setq org-agenda-sticky org-agenda-sticky-orig
7869 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7871 (defun org-agenda-goto-today ()
7872 "Go to today."
7873 (interactive)
7874 (org-agenda-check-type t 'agenda)
7875 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7876 (curspan (nth 2 args))
7877 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7878 (cond
7879 (tdpos (goto-char tdpos))
7880 ((eq org-agenda-type 'agenda)
7881 (let* ((sd (org-agenda-compute-starting-span
7882 (org-today) (or curspan org-agenda-span)))
7883 (org-agenda-overriding-arguments args))
7884 (setf (nth 1 org-agenda-overriding-arguments) sd)
7885 (org-agenda-redo)
7886 (org-agenda-find-same-or-today-or-agenda)))
7887 (t (error "Cannot find today")))))
7889 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7890 (goto-char
7891 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7892 (text-property-any (point-min) (point-max) 'org-today t)
7893 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7894 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7895 (org-agenda-backward-block))
7896 (point-min))))
7898 (defun org-agenda-backward-block ()
7899 "Move backward by one agenda block."
7900 (interactive)
7901 (org-agenda-forward-block 'backward))
7903 (defun org-agenda-forward-block (&optional backward)
7904 "Move forward by one agenda block.
7905 When optional argument BACKWARD is set, go backward"
7906 (interactive)
7907 (cond ((not (derived-mode-p 'org-agenda-mode))
7908 (user-error
7909 "Cannot execute this command outside of org-agenda-mode buffers"))
7910 ((looking-at (if backward "\\`" "\\'"))
7911 (message "Already at the %s block" (if backward "first" "last")))
7912 (t (let ((pos (prog1 (point)
7913 (ignore-errors (if backward (backward-char 1)
7914 (move-end-of-line 1)))))
7915 (f (if backward
7916 'previous-single-property-change
7917 'next-single-property-change))
7918 moved dest)
7919 (while (and (setq dest (funcall
7920 f (point) 'org-agenda-structural-header))
7921 (not (get-text-property
7922 (point) 'org-agenda-structural-header)))
7923 (setq moved t)
7924 (goto-char dest))
7925 (if moved (move-beginning-of-line 1)
7926 (goto-char (if backward (point-min) (point-max)))
7927 (move-beginning-of-line 1)
7928 (message "No %s block" (if backward "previous" "further")))))))
7930 (defun org-agenda-later (arg)
7931 "Go forward in time by the current span.
7932 With prefix ARG, go forward that many times the current span."
7933 (interactive "p")
7934 (org-agenda-check-type t 'agenda)
7935 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7936 (span (or (nth 2 args) org-agenda-current-span))
7937 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7938 (greg (calendar-gregorian-from-absolute sd))
7939 (cnt (org-get-at-bol 'org-day-cnt))
7940 greg2)
7941 (cond
7942 ((numberp span)
7943 (setq sd (+ (* span arg) sd)))
7944 ((eq span 'day)
7945 (setq sd (+ arg sd)))
7946 ((eq span 'week)
7947 (setq sd (+ (* 7 arg) sd)))
7948 ((eq span 'fortnight)
7949 (setq sd (+ (* 14 arg) sd)))
7950 ((eq span 'month)
7951 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7952 sd (calendar-absolute-from-gregorian greg2))
7953 (setcar greg2 (1+ (car greg2))))
7954 ((eq span 'year)
7955 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7956 sd (calendar-absolute-from-gregorian greg2))
7957 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7959 (setq sd (+ (* span arg) sd))))
7960 (let ((org-agenda-overriding-cmd
7961 ;; `cmd' may have been set by `org-agenda-run-series' which
7962 ;; uses `org-agenda-overriding-cmd' to decide whether
7963 ;; overriding is allowed for `cmd'
7964 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7965 (org-agenda-overriding-arguments
7966 (list (car args) sd span)))
7967 (org-agenda-redo)
7968 (org-agenda-find-same-or-today-or-agenda cnt))))
7970 (defun org-agenda-earlier (arg)
7971 "Go backward in time by the current span.
7972 With prefix ARG, go backward that many times the current span."
7973 (interactive "p")
7974 (org-agenda-later (- arg)))
7976 (defun org-agenda-view-mode-dispatch ()
7977 "Call one of the view mode commands."
7978 (interactive)
7979 (message "View: [d]ay [w]eek for[t]night [m]onth [y]ear [SPC]reset [q]uit/abort
7980 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7981 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7982 (pcase (read-char-exclusive)
7983 (?\ (call-interactively 'org-agenda-reset-view))
7984 (?d (call-interactively 'org-agenda-day-view))
7985 (?w (call-interactively 'org-agenda-week-view))
7986 (?t (call-interactively 'org-agenda-fortnight-view))
7987 (?m (call-interactively 'org-agenda-month-view))
7988 (?y (call-interactively 'org-agenda-year-view))
7989 (?l (call-interactively 'org-agenda-log-mode))
7990 (?L (org-agenda-log-mode '(4)))
7991 (?c (org-agenda-log-mode 'clockcheck))
7992 ((or ?F ?f) (call-interactively 'org-agenda-follow-mode))
7993 (?a (call-interactively 'org-agenda-archives-mode))
7994 (?A (org-agenda-archives-mode 'files))
7995 ((or ?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7996 ((or ?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7997 (?G (call-interactively 'org-agenda-toggle-time-grid))
7998 (?D (call-interactively 'org-agenda-toggle-diary))
7999 (?\! (call-interactively 'org-agenda-toggle-deadlines))
8000 (?\[ (let ((org-agenda-include-inactive-timestamps t))
8001 (org-agenda-check-type t 'agenda)
8002 (org-agenda-redo))
8003 (message "Display now includes inactive timestamps as well"))
8004 (?q (message "Abort"))
8005 (key (user-error "Invalid key: %s" key))))
8007 (defun org-agenda-reset-view ()
8008 "Switch to default view for agenda."
8009 (interactive)
8010 (org-agenda-change-time-span org-agenda-span))
8012 (defun org-agenda-day-view (&optional day-of-month)
8013 "Switch to daily view for agenda.
8014 With argument DAY-OF-MONTH, switch to that day of the month."
8015 (interactive "P")
8016 (org-agenda-change-time-span 'day day-of-month))
8018 (defun org-agenda-week-view (&optional iso-week)
8019 "Switch to weekly view for agenda.
8020 With argument ISO-WEEK, switch to the corresponding ISO week.
8021 If ISO-WEEK has more then 2 digits, only the last two encode
8022 the week. Any digits before this encode a year. So 200712
8023 means week 12 of year 2007. Years ranging from 70 years ago
8024 to 30 years in the future can also be written as 2-digit years."
8025 (interactive "P")
8026 (org-agenda-change-time-span 'week iso-week))
8028 (defun org-agenda-fortnight-view (&optional iso-week)
8029 "Switch to fortnightly view for agenda.
8030 With argument ISO-WEEK, switch to the corresponding ISO week.
8031 If ISO-WEEK has more then 2 digits, only the last two encode
8032 the week. Any digits before this encode a year. So 200712
8033 means week 12 of year 2007. Years ranging from 70 years ago
8034 to 30 years in the future can also be written as 2-digit years."
8035 (interactive "P")
8036 (org-agenda-change-time-span 'fortnight iso-week))
8038 (defun org-agenda-month-view (&optional month)
8039 "Switch to monthly view for agenda.
8040 With argument MONTH, switch to that month. If MONTH has more
8041 then 2 digits, only the last two encode the month. Any digits
8042 before this encode a year. So 200712 means December year 2007.
8043 Years ranging from 70 years ago to 30 years in the future can
8044 also be written as 2-digit years."
8045 (interactive "P")
8046 (org-agenda-change-time-span 'month month))
8048 (defun org-agenda-year-view (&optional year)
8049 "Switch to yearly view for agenda.
8050 With argument YEAR, switch to that year. Years ranging from 70
8051 years ago to 30 years in the future can also be written as
8052 2-digit years."
8053 (interactive "P")
8054 (when year
8055 (setq year (org-small-year-to-year year)))
8056 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
8057 (org-agenda-change-time-span 'year year)
8058 (error "Abort")))
8060 (defun org-agenda-change-time-span (span &optional n)
8061 "Change the agenda view to SPAN.
8062 SPAN may be `day', `week', `fortnight', `month', `year'."
8063 (org-agenda-check-type t 'agenda)
8064 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
8065 (curspan (nth 2 args)))
8066 (if (and (not n) (equal curspan span))
8067 (error "Viewing span is already \"%s\"" span))
8068 (let* ((sd (or (org-get-at-bol 'day)
8069 (nth 1 args)
8070 org-starting-day))
8071 (sd (org-agenda-compute-starting-span sd span n))
8072 (org-agenda-overriding-cmd
8073 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
8074 (org-agenda-overriding-arguments
8075 (list (car args) sd span)))
8076 (org-agenda-redo)
8077 (org-agenda-find-same-or-today-or-agenda))
8078 (org-agenda-set-mode-name)
8079 (message "Switched to %s view" span)))
8081 (defun org-agenda-compute-starting-span (sd span &optional n)
8082 "Compute starting date for agenda.
8083 SPAN may be `day', `week', `fortnight', `month', `year'. The return value
8084 is a cons cell with the starting date and the number of days,
8085 so that the date SD will be in that range."
8086 (let* ((greg (calendar-gregorian-from-absolute sd))
8087 (dg (nth 1 greg))
8088 (mg (car greg))
8089 (yg (nth 2 greg)))
8090 (cond
8091 ((eq span 'day)
8092 (when n
8093 (setq sd (+ (calendar-absolute-from-gregorian
8094 (list mg 1 yg))
8095 n -1))))
8096 ((or (eq span 'week) (eq span 'fortnight))
8097 (let* ((nt (calendar-day-of-week
8098 (calendar-gregorian-from-absolute sd)))
8099 (d (if org-agenda-start-on-weekday
8100 (- nt org-agenda-start-on-weekday)
8103 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
8104 (when n
8105 (require 'cal-iso)
8106 (when (> n 99)
8107 (setq y1 (org-small-year-to-year (/ n 100))
8108 n (mod n 100)))
8109 (setq sd
8110 (calendar-iso-to-absolute
8111 (list n 1
8112 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
8113 ((eq span 'month)
8114 (let (y1)
8115 (when (and n (> n 99))
8116 (setq y1 (org-small-year-to-year (/ n 100))
8117 n (mod n 100)))
8118 (setq sd (calendar-absolute-from-gregorian
8119 (list (or n mg) 1 (or y1 yg))))))
8120 ((eq span 'year)
8121 (setq sd (calendar-absolute-from-gregorian
8122 (list 1 1 (or n yg))))))
8123 sd))
8125 (defun org-agenda-next-date-line (&optional arg)
8126 "Jump to the next line indicating a date in agenda buffer."
8127 (interactive "p")
8128 (org-agenda-check-type t 'agenda)
8129 (beginning-of-line 1)
8130 ;; This does not work if user makes date format that starts with a blank
8131 (if (looking-at "^\\S-") (forward-char 1))
8132 (if (not (re-search-forward "^\\S-" nil t arg))
8133 (progn
8134 (backward-char 1)
8135 (error "No next date after this line in this buffer")))
8136 (goto-char (match-beginning 0)))
8138 (defun org-agenda-previous-date-line (&optional arg)
8139 "Jump to the previous line indicating a date in agenda buffer."
8140 (interactive "p")
8141 (org-agenda-check-type t 'agenda)
8142 (beginning-of-line 1)
8143 (if (not (re-search-backward "^\\S-" nil t arg))
8144 (error "No previous date before this line in this buffer")))
8146 ;; Initialize the highlight
8147 (defvar org-hl (make-overlay 1 1))
8148 (overlay-put org-hl 'face 'highlight)
8150 (defun org-highlight (begin end &optional buffer)
8151 "Highlight a region with overlay."
8152 (move-overlay org-hl begin end (or buffer (current-buffer))))
8154 (defun org-unhighlight ()
8155 "Detach overlay INDEX."
8156 (delete-overlay org-hl))
8158 (defun org-unhighlight-once ()
8159 "Remove the highlight from its position, and this function from the hook."
8160 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8161 (org-unhighlight))
8163 (defvar org-agenda-pre-follow-window-conf nil)
8164 (defun org-agenda-follow-mode ()
8165 "Toggle follow mode in an agenda buffer."
8166 (interactive)
8167 (unless org-agenda-follow-mode
8168 (setq org-agenda-pre-follow-window-conf
8169 (current-window-configuration)))
8170 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8171 (unless org-agenda-follow-mode
8172 (set-window-configuration org-agenda-pre-follow-window-conf))
8173 (org-agenda-set-mode-name)
8174 (org-agenda-do-context-action)
8175 (message "Follow mode is %s"
8176 (if org-agenda-follow-mode "on" "off")))
8178 (defun org-agenda-entry-text-mode (&optional arg)
8179 "Toggle entry text mode in an agenda buffer."
8180 (interactive "P")
8181 (if (or org-agenda-tag-filter
8182 org-agenda-category-filter
8183 org-agenda-regexp-filter
8184 org-agenda-top-headline-filter)
8185 (user-error "Can't show entry text in filtered views")
8186 (setq org-agenda-entry-text-mode (or (integerp arg)
8187 (not org-agenda-entry-text-mode)))
8188 (org-agenda-entry-text-hide)
8189 (and org-agenda-entry-text-mode
8190 (let ((org-agenda-entry-text-maxlines
8191 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8192 (org-agenda-entry-text-show)))
8193 (org-agenda-set-mode-name)
8194 (message "Entry text mode is %s%s"
8195 (if org-agenda-entry-text-mode "on" "off")
8196 (if (not org-agenda-entry-text-mode) ""
8197 (format " (maximum number of lines is %d)"
8198 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8200 (defun org-agenda-clockreport-mode ()
8201 "Toggle clocktable mode in an agenda buffer."
8202 (interactive)
8203 (org-agenda-check-type t 'agenda)
8204 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
8205 (setq org-agenda-start-with-clockreport-mode org-agenda-clockreport-mode)
8206 (org-agenda-set-mode-name)
8207 (org-agenda-redo)
8208 (message "Clocktable mode is %s"
8209 (if org-agenda-clockreport-mode "on" "off")))
8211 (defun org-agenda-log-mode (&optional special)
8212 "Toggle log mode in an agenda buffer.
8214 With argument SPECIAL, show all possible log items, not only the ones
8215 configured in `org-agenda-log-mode-items'.
8217 With a `\\[universal-argument] \\[universal-argument]' prefix, show *only* \
8218 log items, nothing else."
8219 (interactive "P")
8220 (org-agenda-check-type t 'agenda)
8221 (setq org-agenda-show-log
8222 (cond
8223 ((equal special '(16)) 'only)
8224 ((eq special 'clockcheck)
8225 (if (eq org-agenda-show-log 'clockcheck)
8226 nil 'clockcheck))
8227 (special '(closed clock state))
8228 (t (not org-agenda-show-log))))
8229 (setq org-agenda-start-with-log-mode org-agenda-show-log)
8230 (org-agenda-set-mode-name)
8231 (org-agenda-redo)
8232 (message "Log mode is %s" (if org-agenda-show-log "on" "off")))
8234 (defun org-agenda-archives-mode (&optional with-files)
8235 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8236 When called with a prefix argument, include all archive files as well."
8237 (interactive "P")
8238 (setq org-agenda-archives-mode
8239 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8240 (org-agenda-set-mode-name)
8241 (org-agenda-redo)
8242 (message
8243 "%s"
8244 (cond
8245 ((eq org-agenda-archives-mode nil)
8246 "No archives are included")
8247 ((eq org-agenda-archives-mode 'trees)
8248 (format "Trees with :%s: tag are included" org-archive-tag))
8249 ((eq org-agenda-archives-mode t)
8250 (format "Trees with :%s: tag and all active archive files are included"
8251 org-archive-tag)))))
8253 (defun org-agenda-toggle-diary ()
8254 "Toggle diary inclusion in an agenda buffer."
8255 (interactive)
8256 (org-agenda-check-type t 'agenda)
8257 (setq org-agenda-include-diary (not org-agenda-include-diary))
8258 (org-agenda-redo)
8259 (org-agenda-set-mode-name)
8260 (message "Diary inclusion turned %s"
8261 (if org-agenda-include-diary "on" "off")))
8263 (defun org-agenda-toggle-deadlines ()
8264 "Toggle inclusion of entries with a deadline in an agenda buffer."
8265 (interactive)
8266 (org-agenda-check-type t 'agenda)
8267 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8268 (org-agenda-redo)
8269 (org-agenda-set-mode-name)
8270 (message "Deadlines inclusion turned %s"
8271 (if org-agenda-include-deadlines "on" "off")))
8273 (defun org-agenda-toggle-time-grid ()
8274 "Toggle time grid in an agenda buffer."
8275 (interactive)
8276 (org-agenda-check-type t 'agenda)
8277 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8278 (org-agenda-redo)
8279 (org-agenda-set-mode-name)
8280 (message "Time-grid turned %s"
8281 (if org-agenda-use-time-grid "on" "off")))
8283 (defun org-agenda-set-mode-name ()
8284 "Set the mode name to indicate all the small mode settings."
8285 (setq mode-name
8286 (list "Org-Agenda"
8287 (if (get 'org-agenda-files 'org-restrict) " []" "")
8289 '(:eval (org-agenda-span-name org-agenda-current-span))
8290 (if org-agenda-follow-mode " Follow" "")
8291 (if org-agenda-entry-text-mode " ETxt" "")
8292 (if org-agenda-include-diary " Diary" "")
8293 (if org-agenda-include-deadlines " Ddl" "")
8294 (if org-agenda-use-time-grid " Grid" "")
8295 (if (and (boundp 'org-habit-show-habits)
8296 org-habit-show-habits) " Habit" "")
8297 (cond
8298 ((consp org-agenda-show-log) " LogAll")
8299 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8300 (org-agenda-show-log " Log")
8301 (t ""))
8302 (if (or org-agenda-category-filter
8303 (get 'org-agenda-category-filter :preset-filter))
8304 '(:eval (propertize
8305 (concat " <"
8306 (mapconcat
8307 'identity
8308 (append
8309 (get 'org-agenda-category-filter :preset-filter)
8310 org-agenda-category-filter)
8312 ">")
8313 'face 'org-agenda-filter-category
8314 'help-echo "Category used in filtering")) "")
8315 (if (or org-agenda-tag-filter
8316 (get 'org-agenda-tag-filter :preset-filter))
8317 '(:eval (propertize
8318 (concat " {"
8319 (mapconcat
8320 'identity
8321 (append
8322 (get 'org-agenda-tag-filter :preset-filter)
8323 org-agenda-tag-filter)
8325 "}")
8326 'face 'org-agenda-filter-tags
8327 'help-echo "Tags used in filtering")) "")
8328 (if (or org-agenda-effort-filter
8329 (get 'org-agenda-effort-filter :preset-filter))
8330 '(:eval (propertize
8331 (concat " {"
8332 (mapconcat
8333 'identity
8334 (append
8335 (get 'org-agenda-effort-filter :preset-filter)
8336 org-agenda-effort-filter)
8338 "}")
8339 'face 'org-agenda-filter-effort
8340 'help-echo "Effort conditions used in filtering")) "")
8341 (if (or org-agenda-regexp-filter
8342 (get 'org-agenda-regexp-filter :preset-filter))
8343 '(:eval (propertize
8344 (concat " ["
8345 (mapconcat
8346 'identity
8347 (append
8348 (get 'org-agenda-regexp-filter :preset-filter)
8349 org-agenda-regexp-filter)
8351 "]")
8352 'face 'org-agenda-filter-regexp
8353 'help-echo "Regexp used in filtering")) "")
8354 (if org-agenda-archives-mode
8355 (if (eq org-agenda-archives-mode t)
8356 " Archives"
8357 (format " :%s:" org-archive-tag))
8359 (if org-agenda-clockreport-mode " Clock" "")))
8360 (force-mode-line-update))
8362 (defun org-agenda-update-agenda-type ()
8363 "Update the agenda type after each command."
8364 (setq org-agenda-type
8365 (or (get-text-property (point) 'org-agenda-type)
8366 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8368 (defun org-agenda-next-line ()
8369 "Move cursor to the next line, and show if follow mode is active."
8370 (interactive)
8371 (call-interactively 'next-line)
8372 (org-agenda-do-context-action))
8374 (defun org-agenda-previous-line ()
8375 "Move cursor to the previous line, and show if follow-mode is active."
8376 (interactive)
8377 (call-interactively 'previous-line)
8378 (org-agenda-do-context-action))
8380 (defun org-agenda-next-item (n)
8381 "Move cursor to next agenda item."
8382 (interactive "p")
8383 (let ((col (current-column)))
8384 (dotimes (c n)
8385 (when (next-single-property-change (point-at-eol) 'org-marker)
8386 (move-end-of-line 1)
8387 (goto-char (next-single-property-change (point) 'org-marker))))
8388 (org-move-to-column col))
8389 (org-agenda-do-context-action))
8391 (defun org-agenda-previous-item (n)
8392 "Move cursor to next agenda item."
8393 (interactive "p")
8394 (dotimes (c n)
8395 (let ((col (current-column))
8396 (goto (save-excursion
8397 (move-end-of-line 0)
8398 (previous-single-property-change (point) 'org-marker))))
8399 (if goto (goto-char goto))
8400 (org-move-to-column col)))
8401 (org-agenda-do-context-action))
8403 (defun org-agenda-do-context-action ()
8404 "Show outline path and, maybe, follow mode window."
8405 (let ((m (org-get-at-bol 'org-marker)))
8406 (when (and (markerp m) (marker-buffer m))
8407 (and org-agenda-follow-mode
8408 (if org-agenda-follow-indirect
8409 (org-agenda-tree-to-indirect-buffer nil)
8410 (org-agenda-show)))
8411 (and org-agenda-show-outline-path
8412 (org-with-point-at m (org-display-outline-path t))))))
8414 (defun org-agenda-show-tags ()
8415 "Show the tags applicable to the current item."
8416 (interactive)
8417 (let* ((tags (org-get-at-bol 'tags)))
8418 (if tags
8419 (message "Tags are :%s:"
8420 (org-no-properties (mapconcat 'identity tags ":")))
8421 (message "No tags associated with this line"))))
8423 (defun org-agenda-goto (&optional highlight)
8424 "Go to the entry at point in the corresponding Org file."
8425 (interactive)
8426 (let* ((marker (or (org-get-at-bol 'org-marker)
8427 (org-agenda-error)))
8428 (buffer (marker-buffer marker))
8429 (pos (marker-position marker)))
8430 (switch-to-buffer-other-window buffer)
8431 (widen)
8432 (push-mark)
8433 (goto-char pos)
8434 (when (derived-mode-p 'org-mode)
8435 (org-show-context 'agenda)
8436 (recenter (/ (window-height) 2))
8437 (org-back-to-heading t)
8438 (let ((case-fold-search nil))
8439 (when (re-search-forward org-complex-heading-regexp nil t)
8440 (goto-char (match-beginning 4)))))
8441 (run-hooks 'org-agenda-after-show-hook)
8442 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8444 (defvar org-agenda-after-show-hook nil
8445 "Normal hook run after an item has been shown from the agenda.
8446 Point is in the buffer where the item originated.")
8448 (defun org-agenda-kill ()
8449 "Kill the entry or subtree belonging to the current agenda entry."
8450 (interactive)
8451 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8452 (let* ((bufname-orig (buffer-name))
8453 (marker (or (org-get-at-bol 'org-marker)
8454 (org-agenda-error)))
8455 (buffer (marker-buffer marker))
8456 (pos (marker-position marker))
8457 (type (org-get-at-bol 'type))
8458 dbeg dend (n 0) conf)
8459 (org-with-remote-undo buffer
8460 (with-current-buffer buffer
8461 (save-excursion
8462 (goto-char pos)
8463 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8464 (setq dbeg (progn (org-back-to-heading t) (point))
8465 dend (org-end-of-subtree t t))
8466 (setq dbeg (point-at-bol)
8467 dend (min (point-max) (1+ (point-at-eol)))))
8468 (goto-char dbeg)
8469 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8470 (setq conf (or (eq t org-agenda-confirm-kill)
8471 (and (numberp org-agenda-confirm-kill)
8472 (> n org-agenda-confirm-kill))))
8473 (and conf
8474 (not (y-or-n-p
8475 (format "Delete entry with %d lines in buffer \"%s\"? "
8476 n (buffer-name buffer))))
8477 (error "Abort"))
8478 (let ((org-agenda-buffer-name bufname-orig))
8479 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8480 (with-current-buffer buffer (delete-region dbeg dend))
8481 (message "Agenda item and source killed"))))
8483 (defvar org-archive-default-command) ; defined in org-archive.el
8484 (defun org-agenda-archive-default ()
8485 "Archive the entry or subtree belonging to the current agenda entry."
8486 (interactive)
8487 (require 'org-archive)
8488 (org-agenda-archive-with org-archive-default-command))
8490 (defun org-agenda-archive-default-with-confirmation ()
8491 "Archive the entry or subtree belonging to the current agenda entry."
8492 (interactive)
8493 (require 'org-archive)
8494 (org-agenda-archive-with org-archive-default-command 'confirm))
8496 (defun org-agenda-archive ()
8497 "Archive the entry or subtree belonging to the current agenda entry."
8498 (interactive)
8499 (org-agenda-archive-with 'org-archive-subtree))
8501 (defun org-agenda-archive-to-archive-sibling ()
8502 "Move the entry to the archive sibling."
8503 (interactive)
8504 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8506 (defun org-agenda-archive-with (cmd &optional confirm)
8507 "Move the entry to the archive sibling."
8508 (interactive)
8509 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8510 (let* ((bufname-orig (buffer-name))
8511 (marker (or (org-get-at-bol 'org-marker)
8512 (org-agenda-error)))
8513 (buffer (marker-buffer marker))
8514 (pos (marker-position marker)))
8515 (org-with-remote-undo buffer
8516 (with-current-buffer buffer
8517 (if (derived-mode-p 'org-mode)
8518 (if (and confirm
8519 (not (y-or-n-p "Archive this subtree or entry? ")))
8520 (error "Abort")
8521 (save-window-excursion
8522 (goto-char pos)
8523 (let ((org-agenda-buffer-name bufname-orig))
8524 (org-remove-subtree-entries-from-agenda))
8525 (org-back-to-heading t)
8526 (funcall cmd)))
8527 (error "Archiving works only in Org files"))))))
8529 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8530 "Remove all lines in the agenda that correspond to a given subtree.
8531 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8532 If this information is not given, the function uses the tree at point."
8533 (let ((buf (or buf (current-buffer))) m p)
8534 (save-excursion
8535 (unless (and beg end)
8536 (org-back-to-heading t)
8537 (setq beg (point))
8538 (org-end-of-subtree t)
8539 (setq end (point)))
8540 (set-buffer (get-buffer org-agenda-buffer-name))
8541 (save-excursion
8542 (goto-char (point-max))
8543 (beginning-of-line 1)
8544 (while (not (bobp))
8545 (when (and (setq m (org-get-at-bol 'org-marker))
8546 (equal buf (marker-buffer m))
8547 (setq p (marker-position m))
8548 (>= p beg)
8549 (< p end))
8550 (let ((inhibit-read-only t))
8551 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8552 (beginning-of-line 0))))))
8554 (defun org-agenda-refile (&optional goto rfloc no-update)
8555 "Refile the item at point.
8557 When called with `\\[universal-argument] \\[universal-argument]', \
8558 go to the location of the last
8559 refiled item.
8561 When called with `\\[universal-argument] \\[universal-argument] \
8562 \\[universal-argument]' prefix or when GOTO is 0, clear
8563 the refile cache.
8565 RFLOC can be a refile location obtained in a different way.
8567 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8568 (interactive "P")
8569 (cond
8570 ((member goto '(0 (64)))
8571 (org-refile-cache-clear))
8572 ((equal goto '(16))
8573 (org-refile-goto-last-stored))
8575 (let* ((buffer-orig (buffer-name))
8576 (marker (or (org-get-at-bol 'org-hd-marker)
8577 (org-agenda-error)))
8578 (buffer (marker-buffer marker))
8579 (pos (marker-position marker))
8580 (rfloc (or rfloc
8581 (org-refile-get-location
8582 (if goto "Goto" "Refile to") buffer
8583 org-refile-allow-creating-parent-nodes))))
8584 (with-current-buffer buffer
8585 (org-with-wide-buffer
8586 (goto-char marker)
8587 (let ((org-agenda-buffer-name buffer-orig))
8588 (org-remove-subtree-entries-from-agenda))
8589 (org-refile goto buffer rfloc))))
8590 (unless no-update (org-agenda-redo)))))
8592 (defun org-agenda-open-link (&optional arg)
8593 "Open the link(s) in the current entry, if any.
8594 This looks for a link in the displayed line in the agenda.
8595 It also looks at the text of the entry itself."
8596 (interactive "P")
8597 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8598 (org-get-at-bol 'org-marker)))
8599 (buffer (and marker (marker-buffer marker)))
8600 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8601 (lkall (and buffer (org-offer-links-in-entry
8602 buffer marker arg prefix)))
8603 (lk0 (car lkall))
8604 (lk (if (stringp lk0) (list lk0) lk0))
8605 (lkend (cdr lkall))
8606 trg)
8607 (cond
8608 ((and buffer lk)
8609 (mapcar (lambda(l)
8610 (with-current-buffer buffer
8611 (setq trg (and (string-match org-bracket-link-regexp l)
8612 (match-string 1 l)))
8613 (if (or (not trg) (string-match org-any-link-re trg))
8614 (org-with-wide-buffer
8615 (goto-char marker)
8616 (when (search-forward l nil lkend)
8617 (goto-char (match-beginning 0))
8618 (org-open-at-point)))
8619 ;; This is an internal link, widen the buffer
8620 (switch-to-buffer-other-window buffer)
8621 (widen)
8622 (goto-char marker)
8623 (when (search-forward l nil lkend)
8624 (goto-char (match-beginning 0))
8625 (org-open-at-point)))))
8626 lk))
8627 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8628 (save-excursion
8629 (beginning-of-line 1)
8630 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8631 (org-open-link-from-string (match-string 1)))
8632 (t (message "No link to open here")))))
8634 (defun org-agenda-copy-local-variable (var)
8635 "Get a variable from a referenced buffer and install it here."
8636 (let ((m (org-get-at-bol 'org-marker)))
8637 (when (and m (buffer-live-p (marker-buffer m)))
8638 (set (make-local-variable var)
8639 (with-current-buffer (marker-buffer m)
8640 (symbol-value var))))))
8642 (defun org-agenda-switch-to (&optional delete-other-windows)
8643 "Go to the Org mode file which contains the item at point.
8644 When optional argument DELETE-OTHER-WINDOWS is non-nil, the
8645 displayed Org file fills the frame."
8646 (interactive)
8647 (if (and org-return-follows-link
8648 (not (org-get-at-bol 'org-marker))
8649 (org-in-regexp org-bracket-link-regexp))
8650 (org-open-link-from-string (match-string 0))
8651 (let* ((marker (or (org-get-at-bol 'org-marker)
8652 (org-agenda-error)))
8653 (buffer (marker-buffer marker))
8654 (pos (marker-position marker)))
8655 (unless buffer (user-error "Trying to switch to non-existent buffer"))
8656 (pop-to-buffer-same-window buffer)
8657 (when delete-other-windows (delete-other-windows))
8658 (widen)
8659 (goto-char pos)
8660 (when (derived-mode-p 'org-mode)
8661 (org-show-context 'agenda)
8662 (run-hooks 'org-agenda-after-show-hook)))))
8664 (defun org-agenda-goto-mouse (ev)
8665 "Go to the Org file which contains the item at the mouse click."
8666 (interactive "e")
8667 (mouse-set-point ev)
8668 (org-agenda-goto))
8670 (defun org-agenda-show (&optional full-entry)
8671 "Display the Org file which contains the item at point.
8672 With prefix argument FULL-ENTRY, make the entire entry visible
8673 if it was hidden in the outline."
8674 (interactive "P")
8675 (let ((win (selected-window)))
8676 (org-agenda-goto t)
8677 (when full-entry (org-show-entry))
8678 (select-window win)))
8680 (defvar org-agenda-show-window nil)
8681 (defun org-agenda-show-and-scroll-up (&optional arg)
8682 "Display the Org file which contains the item at point.
8684 When called repeatedly, scroll the window that is displaying the buffer.
8686 With a `\\[universal-argument]' prefix, use `org-show-entry' instead of \
8687 `outline-show-subtree'
8688 to display the item, so that drawers and logbooks stay folded."
8689 (interactive "P")
8690 (let ((win (selected-window)))
8691 (if (and (window-live-p org-agenda-show-window)
8692 (eq this-command last-command))
8693 (progn
8694 (select-window org-agenda-show-window)
8695 (ignore-errors (scroll-up)))
8696 (org-agenda-goto t)
8697 (if arg (org-show-entry) (outline-show-subtree))
8698 (setq org-agenda-show-window (selected-window)))
8699 (select-window win)))
8701 (defun org-agenda-show-scroll-down ()
8702 "Scroll down the window showing the agenda."
8703 (interactive)
8704 (let ((win (selected-window)))
8705 (when (window-live-p org-agenda-show-window)
8706 (select-window org-agenda-show-window)
8707 (ignore-errors (scroll-down))
8708 (select-window win))))
8710 (defun org-agenda-show-1 (&optional more)
8711 "Display the Org file which contains the item at point.
8712 The prefix arg selects the amount of information to display:
8714 0 hide the subtree
8715 1 just show the entry according to defaults.
8716 2 show the children view
8717 3 show the subtree view
8718 4 show the entire subtree and any LOGBOOK drawers
8719 5 show the entire subtree and any drawers
8720 With prefix argument FULL-ENTRY, make the entire entry visible
8721 if it was hidden in the outline."
8722 (interactive "p")
8723 (let ((win (selected-window)))
8724 (org-agenda-goto t)
8725 (org-back-to-heading)
8726 (set-window-start (selected-window) (point-at-bol))
8727 (cond
8728 ((= more 0)
8729 (outline-hide-subtree)
8730 (save-excursion
8731 (org-back-to-heading)
8732 (run-hook-with-args 'org-cycle-hook 'folded))
8733 (message "Remote: FOLDED"))
8734 ((and (called-interactively-p 'any) (= more 1))
8735 (message "Remote: show with default settings"))
8736 ((= more 2)
8737 (outline-show-entry)
8738 (org-show-children)
8739 (save-excursion
8740 (org-back-to-heading)
8741 (run-hook-with-args 'org-cycle-hook 'children))
8742 (message "Remote: CHILDREN"))
8743 ((= more 3)
8744 (outline-show-subtree)
8745 (save-excursion
8746 (org-back-to-heading)
8747 (run-hook-with-args 'org-cycle-hook 'subtree))
8748 (message "Remote: SUBTREE"))
8749 ((= more 4)
8750 (outline-show-subtree)
8751 (save-excursion
8752 (org-back-to-heading)
8753 (org-cycle-hide-drawers 'subtree '("LOGBOOK")))
8754 (message "Remote: SUBTREE AND LOGBOOK"))
8755 ((> more 4)
8756 (outline-show-subtree)
8757 (message "Remote: SUBTREE AND ALL DRAWERS")))
8758 (select-window win)))
8760 (defvar org-agenda-cycle-counter nil)
8761 (defun org-agenda-cycle-show (&optional n)
8762 "Show the current entry in another window, with default settings.
8764 Default settings are taken from `org-show-context-detail'. When
8765 use repeatedly in immediate succession, the remote entry will
8766 cycle through visibility
8768 children -> subtree -> folded
8770 When called with a numeric prefix arg, that arg will be passed through to
8771 `org-agenda-show-1'. For the interpretation of that argument, see the
8772 docstring of `org-agenda-show-1'."
8773 (interactive "P")
8774 (if (integerp n)
8775 (setq org-agenda-cycle-counter n)
8776 (if (not (eq last-command this-command))
8777 (setq org-agenda-cycle-counter 1)
8778 (if (equal org-agenda-cycle-counter 0)
8779 (setq org-agenda-cycle-counter 2)
8780 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8781 (if (> org-agenda-cycle-counter 3)
8782 (setq org-agenda-cycle-counter 0)))))
8783 (org-agenda-show-1 org-agenda-cycle-counter))
8785 (defun org-agenda-recenter (arg)
8786 "Display the Org file which contains the item at point and recenter."
8787 (interactive "P")
8788 (let ((win (selected-window)))
8789 (org-agenda-goto t)
8790 (recenter arg)
8791 (select-window win)))
8793 (defun org-agenda-show-mouse (ev)
8794 "Display the Org file which contains the item at the mouse click."
8795 (interactive "e")
8796 (mouse-set-point ev)
8797 (org-agenda-show))
8799 (defun org-agenda-check-no-diary ()
8800 "Check if the entry is a diary link and abort if yes."
8801 (if (org-get-at-bol 'org-agenda-diary-link)
8802 (org-agenda-error)))
8804 (defun org-agenda-error ()
8805 "Throw an error when a command is not allowed in the agenda."
8806 (user-error "Command not allowed in this line"))
8808 (defun org-agenda-tree-to-indirect-buffer (arg)
8809 "Show the subtree corresponding to the current entry in an indirect buffer.
8810 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8812 With a numerical prefix ARG, go up to this level and then take that tree.
8813 With a negative numeric ARG, go up by this number of levels.
8815 With a `\\[universal-argument]' prefix, make a separate frame for this tree, \
8816 i.e. don't use
8817 the dedicated frame."
8818 (interactive "P")
8819 (if current-prefix-arg
8820 (org-agenda-do-tree-to-indirect-buffer arg)
8821 (let ((agenda-buffer (buffer-name))
8822 (agenda-window (selected-window))
8823 (indirect-window
8824 (and org-last-indirect-buffer
8825 (get-buffer-window org-last-indirect-buffer))))
8826 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8827 (unless (or (eq org-indirect-buffer-display 'new-frame)
8828 (eq org-indirect-buffer-display 'dedicated-frame))
8829 (unwind-protect
8830 (unless (and indirect-window (window-live-p indirect-window))
8831 (setq indirect-window (split-window agenda-window)))
8832 (and indirect-window (select-window indirect-window))
8833 (switch-to-buffer org-last-indirect-buffer :norecord)
8834 (fit-window-to-buffer indirect-window)))
8835 (select-window (get-buffer-window agenda-buffer))
8836 (setq org-agenda-last-indirect-buffer org-last-indirect-buffer))))
8838 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8839 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8840 (org-agenda-check-no-diary)
8841 (let* ((marker (or (org-get-at-bol 'org-marker)
8842 (org-agenda-error)))
8843 (buffer (marker-buffer marker))
8844 (pos (marker-position marker)))
8845 (with-current-buffer buffer
8846 (save-excursion
8847 (goto-char pos)
8848 (funcall 'org-tree-to-indirect-buffer arg)))))
8850 (defvar org-last-heading-marker (make-marker)
8851 "Marker pointing to the headline that last changed its TODO state
8852 by a remote command from the agenda.")
8854 (defun org-agenda-todo-nextset ()
8855 "Switch TODO entry to next sequence."
8856 (interactive)
8857 (org-agenda-todo 'nextset))
8859 (defun org-agenda-todo-previousset ()
8860 "Switch TODO entry to previous sequence."
8861 (interactive)
8862 (org-agenda-todo 'previousset))
8864 (defun org-agenda-todo (&optional arg)
8865 "Cycle TODO state of line at point, also in Org file.
8866 This changes the line at point, all other lines in the agenda referring to
8867 the same tree node, and the headline of the tree node in the Org file."
8868 (interactive "P")
8869 (org-agenda-check-no-diary)
8870 (let* ((col (current-column))
8871 (marker (or (org-get-at-bol 'org-marker)
8872 (org-agenda-error)))
8873 (buffer (marker-buffer marker))
8874 (pos (marker-position marker))
8875 (hdmarker (org-get-at-bol 'org-hd-marker))
8876 (todayp (org-agenda-today-p (org-get-at-bol 'day)))
8877 (inhibit-read-only t)
8878 org-agenda-headline-snapshot-before-repeat newhead just-one)
8879 (org-with-remote-undo buffer
8880 (with-current-buffer buffer
8881 (widen)
8882 (goto-char pos)
8883 (org-show-context 'agenda)
8884 (let ((current-prefix-arg arg))
8885 (call-interactively 'org-todo))
8886 (and (bolp) (forward-char 1))
8887 (setq newhead (org-get-heading))
8888 (when (and (bound-and-true-p
8889 org-agenda-headline-snapshot-before-repeat)
8890 (not (equal org-agenda-headline-snapshot-before-repeat
8891 newhead))
8892 todayp)
8893 (setq newhead org-agenda-headline-snapshot-before-repeat
8894 just-one t))
8895 (save-excursion
8896 (org-back-to-heading)
8897 (move-marker org-last-heading-marker (point))))
8898 (beginning-of-line 1)
8899 (save-window-excursion
8900 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8901 (when (bound-and-true-p org-clock-out-when-done)
8902 (string-match (concat "^" (regexp-opt org-done-keywords-for-agenda))
8903 newhead)
8904 (org-agenda-unmark-clocking-task))
8905 (org-move-to-column col)
8906 (org-agenda-mark-clocking-task))))
8908 (defun org-agenda-add-note (&optional arg)
8909 "Add a time-stamped note to the entry at point."
8910 (interactive "P")
8911 (org-agenda-check-no-diary)
8912 (let* ((marker (or (org-get-at-bol 'org-marker)
8913 (org-agenda-error)))
8914 (buffer (marker-buffer marker))
8915 (pos (marker-position marker))
8916 (hdmarker (org-get-at-bol 'org-hd-marker))
8917 (inhibit-read-only t))
8918 (with-current-buffer buffer
8919 (widen)
8920 (goto-char pos)
8921 (org-show-context 'agenda)
8922 (org-add-note))))
8924 (defun org-agenda-change-all-lines (newhead hdmarker
8925 &optional fixface just-this)
8926 "Change all lines in the agenda buffer which match HDMARKER.
8927 The new content of the line will be NEWHEAD (as modified by
8928 `org-agenda-format-item'). HDMARKER is checked with
8929 `equal' against all `org-hd-marker' text properties in the file.
8930 If FIXFACE is non-nil, the face of each item is modified according to
8931 the new TODO state.
8932 If JUST-THIS is non-nil, change just the current line, not all.
8933 If FORCE-TAGS is non nil, the car of it returns the new tags."
8934 (let* ((inhibit-read-only t)
8935 (line (org-current-line))
8936 (org-agenda-buffer (current-buffer))
8937 (thetags (with-current-buffer (marker-buffer hdmarker)
8938 (org-with-wide-buffer
8939 (goto-char hdmarker)
8940 (org-get-tags-at))))
8941 props m pl undone-face done-face finish new dotime level cat tags)
8942 (save-excursion
8943 (goto-char (point-max))
8944 (beginning-of-line 1)
8945 (while (not finish)
8946 (setq finish (bobp))
8947 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8948 (or (not just-this) (= (org-current-line) line))
8949 (equal m hdmarker))
8950 (setq props (text-properties-at (point))
8951 dotime (org-get-at-bol 'dotime)
8952 cat (org-get-at-eol 'org-category 1)
8953 level (org-get-at-bol 'level)
8954 tags thetags
8956 (let ((org-prefix-format-compiled
8957 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8958 org-prefix-format-compiled))
8959 (extra (org-get-at-bol 'extra)))
8960 (with-current-buffer (marker-buffer hdmarker)
8961 (org-with-wide-buffer
8962 (org-agenda-format-item extra newhead level cat tags dotime))))
8963 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8964 undone-face (org-get-at-bol 'undone-face)
8965 done-face (org-get-at-bol 'done-face))
8966 (beginning-of-line 1)
8967 (cond
8968 ((equal new "") (delete-region (point) (line-beginning-position 2)))
8969 ((looking-at ".*")
8970 ;; When replacing the whole line, preserve bulk mark
8971 ;; overlay, if any.
8972 (let ((mark (catch :overlay
8973 (dolist (o (overlays-in (point) (+ 2 (point))))
8974 (when (eq (overlay-get o 'type)
8975 'org-marked-entry-overlay)
8976 (throw :overlay o))))))
8977 (replace-match new t t)
8978 (beginning-of-line)
8979 (when mark (move-overlay mark (point) (+ 2 (point)))))
8980 (add-text-properties (point-at-bol) (point-at-eol) props)
8981 (when fixface
8982 (add-text-properties
8983 (point-at-bol) (point-at-eol)
8984 (list 'face
8985 (if org-last-todo-state-is-todo
8986 undone-face done-face))))
8987 (org-agenda-highlight-todo 'line)
8988 (beginning-of-line 1))
8989 (t (error "Line update did not work")))
8990 (save-restriction
8991 (narrow-to-region (point-at-bol) (point-at-eol))
8992 (org-agenda-finalize)))
8993 (beginning-of-line 0)))))
8995 (defun org-agenda-align-tags (&optional line)
8996 "Align all tags in agenda items to `org-agenda-tags-column'."
8997 (let ((inhibit-read-only t)
8998 (org-agenda-tags-column (if (eq 'auto org-agenda-tags-column)
8999 (- (window-text-width))
9000 org-agenda-tags-column))
9001 l c)
9002 (save-excursion
9003 (goto-char (if line (point-at-bol) (point-min)))
9004 (while (re-search-forward "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"
9005 (if line (point-at-eol) nil) t)
9006 (add-text-properties
9007 (match-beginning 2) (match-end 2)
9008 (list 'face (delq nil (let ((prop (get-text-property
9009 (match-beginning 2) 'face)))
9010 (or (listp prop) (setq prop (list prop)))
9011 (if (memq 'org-tag prop)
9012 prop
9013 (cons 'org-tag prop))))))
9014 (setq l (- (match-end 2) (match-beginning 2))
9015 c (if (< org-agenda-tags-column 0)
9016 (- (abs org-agenda-tags-column) l)
9017 org-agenda-tags-column))
9018 (delete-region (match-beginning 1) (match-end 1))
9019 (goto-char (match-beginning 1))
9020 (insert (org-add-props
9021 (make-string (max 1 (- c (current-column))) ?\ )
9022 (plist-put (copy-sequence (text-properties-at (point)))
9023 'face nil))))
9024 (goto-char (point-min))
9025 (org-font-lock-add-tag-faces (point-max)))))
9027 (defun org-agenda-priority-up ()
9028 "Increase the priority of line at point, also in Org file."
9029 (interactive)
9030 (org-agenda-priority 'up))
9032 (defun org-agenda-priority-down ()
9033 "Decrease the priority of line at point, also in Org file."
9034 (interactive)
9035 (org-agenda-priority 'down))
9037 (defun org-agenda-priority (&optional force-direction)
9038 "Set the priority of line at point, also in Org file.
9039 This changes the line at point, all other lines in the agenda referring to
9040 the same tree node, and the headline of the tree node in the Org file.
9041 Called with a universal prefix arg, show the priority instead of setting it."
9042 (interactive "P")
9043 (if (equal force-direction '(4))
9044 (org-show-priority)
9045 (unless org-enable-priority-commands
9046 (error "Priority commands are disabled"))
9047 (org-agenda-check-no-diary)
9048 (let* ((col (current-column))
9049 (marker (or (org-get-at-bol 'org-marker)
9050 (org-agenda-error)))
9051 (hdmarker (org-get-at-bol 'org-hd-marker))
9052 (buffer (marker-buffer hdmarker))
9053 (pos (marker-position hdmarker))
9054 (inhibit-read-only t)
9055 newhead)
9056 (org-with-remote-undo buffer
9057 (with-current-buffer buffer
9058 (widen)
9059 (goto-char pos)
9060 (org-show-context 'agenda)
9061 (funcall 'org-priority force-direction)
9062 (end-of-line 1)
9063 (setq newhead (org-get-heading)))
9064 (org-agenda-change-all-lines newhead hdmarker)
9065 (org-move-to-column col)))))
9067 ;; FIXME: should fix the tags property of the agenda line.
9068 (defun org-agenda-set-tags (&optional tag onoff)
9069 "Set tags for the current headline."
9070 (interactive)
9071 (org-agenda-check-no-diary)
9072 (if (and (org-region-active-p) (called-interactively-p 'any))
9073 (call-interactively 'org-change-tag-in-region)
9074 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9075 (org-agenda-error)))
9076 (buffer (marker-buffer hdmarker))
9077 (pos (marker-position hdmarker))
9078 (inhibit-read-only t)
9079 newhead)
9080 (org-with-remote-undo buffer
9081 (with-current-buffer buffer
9082 (widen)
9083 (goto-char pos)
9084 (org-show-context 'agenda)
9085 (if tag
9086 (org-toggle-tag tag onoff)
9087 (call-interactively 'org-set-tags))
9088 (end-of-line 1)
9089 (setq newhead (org-get-heading)))
9090 (org-agenda-change-all-lines newhead hdmarker)
9091 (beginning-of-line 1)))))
9093 (defun org-agenda-set-property ()
9094 "Set a property for the current headline."
9095 (interactive)
9096 (org-agenda-check-no-diary)
9097 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9098 (org-agenda-error)))
9099 (buffer (marker-buffer hdmarker))
9100 (pos (marker-position hdmarker))
9101 (inhibit-read-only t)
9102 newhead)
9103 (org-with-remote-undo buffer
9104 (with-current-buffer buffer
9105 (widen)
9106 (goto-char pos)
9107 (org-show-context 'agenda)
9108 (call-interactively 'org-set-property)))))
9110 (defun org-agenda-set-effort ()
9111 "Set the effort property for the current headline."
9112 (interactive)
9113 (org-agenda-check-no-diary)
9114 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9115 (org-agenda-error)))
9116 (buffer (marker-buffer hdmarker))
9117 (pos (marker-position hdmarker))
9118 (inhibit-read-only t)
9119 newhead)
9120 (org-with-remote-undo buffer
9121 (with-current-buffer buffer
9122 (widen)
9123 (goto-char pos)
9124 (org-show-context 'agenda)
9125 (call-interactively 'org-set-effort)
9126 (end-of-line 1)
9127 (setq newhead (org-get-heading)))
9128 (org-agenda-change-all-lines newhead hdmarker))))
9130 (defun org-agenda-toggle-archive-tag ()
9131 "Toggle the archive tag for the current entry."
9132 (interactive)
9133 (org-agenda-check-no-diary)
9134 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9135 (org-agenda-error)))
9136 (buffer (marker-buffer hdmarker))
9137 (pos (marker-position hdmarker))
9138 (inhibit-read-only t)
9139 newhead)
9140 (org-with-remote-undo buffer
9141 (with-current-buffer buffer
9142 (widen)
9143 (goto-char pos)
9144 (org-show-context 'agenda)
9145 (call-interactively 'org-toggle-archive-tag)
9146 (end-of-line 1)
9147 (setq newhead (org-get-heading)))
9148 (org-agenda-change-all-lines newhead hdmarker)
9149 (beginning-of-line 1))))
9151 (defun org-agenda-do-date-later (arg)
9152 (interactive "P")
9153 (cond
9154 ((or (equal arg '(16))
9155 (memq last-command
9156 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9157 (setq this-command 'org-agenda-date-later-minutes)
9158 (org-agenda-date-later-minutes 1))
9159 ((or (equal arg '(4))
9160 (memq last-command
9161 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9162 (setq this-command 'org-agenda-date-later-hours)
9163 (org-agenda-date-later-hours 1))
9165 (org-agenda-date-later (prefix-numeric-value arg)))))
9167 (defun org-agenda-do-date-earlier (arg)
9168 (interactive "P")
9169 (cond
9170 ((or (equal arg '(16))
9171 (memq last-command
9172 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9173 (setq this-command 'org-agenda-date-earlier-minutes)
9174 (org-agenda-date-earlier-minutes 1))
9175 ((or (equal arg '(4))
9176 (memq last-command
9177 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9178 (setq this-command 'org-agenda-date-earlier-hours)
9179 (org-agenda-date-earlier-hours 1))
9181 (org-agenda-date-earlier (prefix-numeric-value arg)))))
9183 (defun org-agenda-date-later (arg &optional what)
9184 "Change the date of this item to ARG day(s) later."
9185 (interactive "p")
9186 (org-agenda-check-type t 'agenda)
9187 (org-agenda-check-no-diary)
9188 (let* ((marker (or (org-get-at-bol 'org-marker)
9189 (org-agenda-error)))
9190 (buffer (marker-buffer marker))
9191 (pos (marker-position marker))
9192 cdate today)
9193 (org-with-remote-undo buffer
9194 (with-current-buffer buffer
9195 (widen)
9196 (goto-char pos)
9197 (unless (org-at-timestamp-p 'lax) (error "Cannot find time stamp"))
9198 (when (and org-agenda-move-date-from-past-immediately-to-today
9199 (equal arg 1)
9200 (or (not what) (eq what 'day))
9201 (not (save-match-data (org-at-date-range-p))))
9202 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9203 cdate (calendar-absolute-from-gregorian
9204 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9205 today (org-today))
9206 (if (> today cdate)
9207 ;; immediately shift to today
9208 (setq arg (- today cdate))))
9209 (org-timestamp-change arg (or what 'day))
9210 (when (and (org-at-date-range-p)
9211 (re-search-backward org-tr-regexp-both (point-at-bol)))
9212 (let ((end org-last-changed-timestamp))
9213 (org-timestamp-change arg (or what 'day))
9214 (setq org-last-changed-timestamp
9215 (concat org-last-changed-timestamp "--" end)))))
9216 (org-agenda-show-new-time marker org-last-changed-timestamp))
9217 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9219 (defun org-agenda-date-earlier (arg &optional what)
9220 "Change the date of this item to ARG day(s) earlier."
9221 (interactive "p")
9222 (org-agenda-date-later (- arg) what))
9224 (defun org-agenda-date-later-minutes (arg)
9225 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9226 (interactive "p")
9227 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9228 (org-agenda-date-later arg 'minute))
9230 (defun org-agenda-date-earlier-minutes (arg)
9231 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9232 (interactive "p")
9233 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9234 (org-agenda-date-earlier arg 'minute))
9236 (defun org-agenda-date-later-hours (arg)
9237 "Change the time of this item, in hour steps."
9238 (interactive "p")
9239 (org-agenda-date-later arg 'hour))
9241 (defun org-agenda-date-earlier-hours (arg)
9242 "Change the time of this item, in hour steps."
9243 (interactive "p")
9244 (org-agenda-date-earlier arg 'hour))
9246 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9247 "Show new date stamp via text properties."
9248 ;; We use text properties to make this undoable
9249 (let ((inhibit-read-only t))
9250 (setq stamp (concat prefix " => " stamp " "))
9251 (save-excursion
9252 (goto-char (point-max))
9253 (while (not (bobp))
9254 (when (equal marker (org-get-at-bol 'org-marker))
9255 (remove-text-properties (point-at-bol) (point-at-eol) '(display))
9256 (org-move-to-column (- (window-width) (length stamp)) t)
9257 (add-text-properties
9258 (1- (point)) (point-at-eol)
9259 (list 'display (org-add-props stamp nil
9260 'face '(secondary-selection default))))
9261 (beginning-of-line 1))
9262 (beginning-of-line 0)))))
9264 (defun org-agenda-date-prompt (arg)
9265 "Change the date of this item. Date is prompted for, with default today.
9266 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9267 be used to request time specification in the time stamp."
9268 (interactive "P")
9269 (org-agenda-check-type t 'agenda)
9270 (org-agenda-check-no-diary)
9271 (let* ((marker (or (org-get-at-bol 'org-marker)
9272 (org-agenda-error)))
9273 (buffer (marker-buffer marker))
9274 (pos (marker-position marker)))
9275 (org-with-remote-undo buffer
9276 (with-current-buffer buffer
9277 (widen)
9278 (goto-char pos)
9279 (unless (org-at-timestamp-p 'lax) (error "Cannot find time stamp"))
9280 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
9281 (org-agenda-show-new-time marker org-last-changed-timestamp))
9282 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9284 (defun org-agenda-schedule (arg &optional time)
9285 "Schedule the item at point.
9286 ARG is passed through to `org-schedule'."
9287 (interactive "P")
9288 (org-agenda-check-type t 'agenda 'todo 'tags 'search)
9289 (org-agenda-check-no-diary)
9290 (let* ((marker (or (org-get-at-bol 'org-marker)
9291 (org-agenda-error)))
9292 (type (marker-insertion-type marker))
9293 (buffer (marker-buffer marker))
9294 (pos (marker-position marker))
9296 (set-marker-insertion-type marker t)
9297 (org-with-remote-undo buffer
9298 (with-current-buffer buffer
9299 (widen)
9300 (goto-char pos)
9301 (setq ts (org-schedule arg time)))
9302 (org-agenda-show-new-time marker ts " S"))
9303 (message "%s" ts)))
9305 (defun org-agenda-deadline (arg &optional time)
9306 "Schedule the item at point.
9307 ARG is passed through to `org-deadline'."
9308 (interactive "P")
9309 (org-agenda-check-type t 'agenda 'todo 'tags 'search)
9310 (org-agenda-check-no-diary)
9311 (let* ((marker (or (org-get-at-bol 'org-marker)
9312 (org-agenda-error)))
9313 (buffer (marker-buffer marker))
9314 (pos (marker-position marker))
9316 (org-with-remote-undo buffer
9317 (with-current-buffer buffer
9318 (widen)
9319 (goto-char pos)
9320 (setq ts (org-deadline arg time)))
9321 (org-agenda-show-new-time marker ts " D"))
9322 (message "%s" ts)))
9324 (defun org-agenda-clock-in (&optional arg)
9325 "Start the clock on the currently selected item."
9326 (interactive "P")
9327 (org-agenda-check-no-diary)
9328 (if (equal arg '(4))
9329 (org-clock-in arg)
9330 (let* ((marker (or (org-get-at-bol 'org-marker)
9331 (org-agenda-error)))
9332 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9333 (pos (marker-position marker))
9334 (col (current-column))
9335 newhead)
9336 (org-with-remote-undo (marker-buffer marker)
9337 (with-current-buffer (marker-buffer marker)
9338 (widen)
9339 (goto-char pos)
9340 (org-show-context 'agenda)
9341 (org-cycle-hide-drawers 'children)
9342 (org-clock-in arg)
9343 (setq newhead (org-get-heading)))
9344 (org-agenda-change-all-lines newhead hdmarker))
9345 (org-move-to-column col))))
9347 (defun org-agenda-clock-out ()
9348 "Stop the currently running clock."
9349 (interactive)
9350 (unless (marker-buffer org-clock-marker)
9351 (error "No running clock"))
9352 (let ((marker (make-marker)) (col (current-column)) newhead)
9353 (org-with-remote-undo (marker-buffer org-clock-marker)
9354 (with-current-buffer (marker-buffer org-clock-marker)
9355 (org-with-wide-buffer
9356 (goto-char org-clock-marker)
9357 (org-back-to-heading t)
9358 (move-marker marker (point))
9359 (org-clock-out)
9360 (setq newhead (org-get-heading)))))
9361 (org-agenda-change-all-lines newhead marker)
9362 (move-marker marker nil)
9363 (org-move-to-column col)
9364 (org-agenda-unmark-clocking-task)))
9366 (defun org-agenda-clock-cancel (&optional arg)
9367 "Cancel the currently running clock."
9368 (interactive "P")
9369 (unless (marker-buffer org-clock-marker)
9370 (user-error "No running clock"))
9371 (org-with-remote-undo (marker-buffer org-clock-marker)
9372 (org-clock-cancel)))
9374 (defun org-agenda-clock-goto ()
9375 "Jump to the currently clocked in task within the agenda.
9376 If the currently clocked in task is not listed in the agenda
9377 buffer, display it in another window."
9378 (interactive)
9379 (let (pos)
9380 (mapc (lambda (o)
9381 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
9382 (setq pos (overlay-start o))))
9383 (overlays-in (point-min) (point-max)))
9384 (cond (pos (goto-char pos))
9385 ;; If the currently clocked entry is not in the agenda
9386 ;; buffer, we visit it in another window:
9387 ((bound-and-true-p org-clock-current-task)
9388 (org-switch-to-buffer-other-window (org-clock-goto)))
9389 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9391 (defun org-agenda-diary-entry-in-org-file ()
9392 "Make a diary entry in the file `org-agenda-diary-file'."
9393 (let (d1 d2 char (text "") dp1 dp2)
9394 (if (equal (buffer-name) "*Calendar*")
9395 (setq d1 (calendar-cursor-to-date t)
9396 d2 (car calendar-mark-ring))
9397 (setq dp1 (get-text-property (point-at-bol) 'day))
9398 (unless dp1 (user-error "No date defined in current line"))
9399 (setq d1 (calendar-gregorian-from-absolute dp1)
9400 d2 (and (ignore-errors (mark))
9401 (save-excursion
9402 (goto-char (mark))
9403 (setq dp2 (get-text-property (point-at-bol) 'day)))
9404 (calendar-gregorian-from-absolute dp2))))
9405 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9406 (setq char (read-char-exclusive))
9407 (cond
9408 ((equal char ?d)
9409 (setq text (read-string "Day entry: "))
9410 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9411 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9412 ((equal char ?a)
9413 (setq d1 (list (car d1) (nth 1 d1)
9414 (read-number (format "Reference year [%d]: " (nth 2 d1))
9415 (nth 2 d1))))
9416 (setq text (read-string "Anniversary (use %d to show years): "))
9417 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9418 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9419 ((equal char ?b)
9420 (setq text (read-string "Block entry: "))
9421 (unless (and d1 d2 (not (equal d1 d2)))
9422 (user-error "No block of days selected"))
9423 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9424 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9425 ((equal char ?j)
9426 (org-switch-to-buffer-other-window
9427 (find-file-noselect org-agenda-diary-file))
9428 (require 'org-datetree)
9429 (org-datetree-find-date-create d1)
9430 (org-reveal t))
9431 (t (user-error "Invalid selection character `%c'" char)))))
9433 (defcustom org-agenda-insert-diary-strategy 'date-tree
9434 "Where in `org-agenda-diary-file' should new entries be added?
9435 Valid values:
9437 date-tree in the date tree, as first child of the date
9438 date-tree-last in the date tree, as last child of the date
9439 top-level as top-level entries at the end of the file."
9440 :group 'org-agenda
9441 :type '(choice
9442 (const :tag "first in a date tree" date-tree)
9443 (const :tag "last in a date tree" date-tree-last)
9444 (const :tag "as top level at end of file" top-level)))
9446 (defcustom org-agenda-insert-diary-extract-time nil
9447 "Non-nil means extract any time specification from the diary entry."
9448 :group 'org-agenda
9449 :version "24.1"
9450 :type 'boolean)
9452 (defcustom org-agenda-bulk-mark-char ">"
9453 "A single-character string to be used as the bulk mark."
9454 :group 'org-agenda
9455 :version "24.1"
9456 :type 'string)
9458 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9459 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9460 If TEXT is not empty, it will become the headline of the new entry, and
9461 the resulting entry will not be shown. When TEXT is empty, switch to
9462 `org-agenda-diary-file' and let the user finish the entry there."
9463 (let ((cw (current-window-configuration)))
9464 (org-switch-to-buffer-other-window
9465 (find-file-noselect org-agenda-diary-file))
9466 (widen)
9467 (goto-char (point-min))
9468 (cond
9469 ((eq type 'anniversary)
9470 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
9471 (progn
9472 (or (org-at-heading-p t)
9473 (progn
9474 (outline-next-heading)
9475 (insert "* Anniversaries\n\n")
9476 (beginning-of-line -1)))))
9477 (outline-next-heading)
9478 (org-back-over-empty-lines)
9479 (backward-char 1)
9480 (insert "\n")
9481 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9482 (nth 2 d1) (car d1) (nth 1 d1) text)))
9483 ((eq type 'day)
9484 (let ((org-prefix-has-time t)
9485 (org-agenda-time-leading-zero t)
9486 fmt time time2)
9487 (if org-agenda-insert-diary-extract-time
9488 ;; Use org-agenda-format-item to parse text for a time-range and
9489 ;; remove it. FIXME: This is a hack, we should refactor
9490 ;; that function to make time extraction available separately
9491 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9492 time (get-text-property 0 'time fmt)
9493 time2 (if (> (length time) 0)
9494 ;; split-string removes trailing ...... if
9495 ;; no end time given. First space
9496 ;; separates time from date.
9497 (concat " " (car (split-string time "\\.")))
9498 nil)
9499 text (get-text-property 0 'txt fmt)))
9500 (if (eq org-agenda-insert-diary-strategy 'top-level)
9501 (org-agenda-insert-diary-as-top-level text)
9502 (require 'org-datetree)
9503 (org-datetree-find-date-create d1)
9504 (org-agenda-insert-diary-make-new-entry text))
9505 (org-insert-time-stamp (org-time-from-absolute
9506 (calendar-absolute-from-gregorian d1))
9507 nil nil nil nil time2))
9508 (end-of-line 0))
9509 ((eq type 'block)
9510 (if (> (calendar-absolute-from-gregorian d1)
9511 (calendar-absolute-from-gregorian d2))
9512 (setq d1 (prog1 d2 (setq d2 d1))))
9513 (if (eq org-agenda-insert-diary-strategy 'top-level)
9514 (org-agenda-insert-diary-as-top-level text)
9515 (require 'org-datetree)
9516 (org-datetree-find-date-create d1)
9517 (org-agenda-insert-diary-make-new-entry text))
9518 (org-insert-time-stamp (org-time-from-absolute
9519 (calendar-absolute-from-gregorian d1)))
9520 (insert "--")
9521 (org-insert-time-stamp (org-time-from-absolute
9522 (calendar-absolute-from-gregorian d2)))
9523 (end-of-line 0)))
9524 (if (string-match "\\S-" text)
9525 (progn
9526 (set-window-configuration cw)
9527 (message "%s entry added to %s"
9528 (capitalize (symbol-name type))
9529 (abbreviate-file-name org-agenda-diary-file)))
9530 (org-reveal t)
9531 (message "Please finish entry here"))))
9533 (defun org-agenda-insert-diary-as-top-level (text)
9534 "Make new entry as a top-level entry at the end of the file.
9535 Add TEXT as headline, and position the cursor in the second line so that
9536 a timestamp can be added there."
9537 (widen)
9538 (goto-char (point-max))
9539 (unless (bolp) (insert "\n"))
9540 (org-insert-heading nil t t)
9541 (insert text)
9542 (org-end-of-meta-data)
9543 (unless (bolp) (insert "\n"))
9544 (when org-adapt-indentation (indent-to-column 2)))
9546 (defun org-agenda-insert-diary-make-new-entry (text)
9547 "Make a new entry with TEXT as a child of the current subtree.
9548 Position the point in the heading's first body line so that
9549 a timestamp can be added there."
9550 (cond
9551 ((eq org-agenda-insert-diary-strategy 'date-tree-last)
9552 (end-of-line)
9553 (org-insert-heading '(4) t)
9554 (org-do-demote))
9556 (outline-next-heading)
9557 (org-back-over-empty-lines)
9558 (unless (looking-at "[ \t]*$") (save-excursion (insert "\n")))
9559 (org-insert-heading nil t)
9560 (org-do-demote)))
9561 (let ((col (current-column)))
9562 (insert text)
9563 (org-end-of-meta-data)
9564 ;; Ensure point is left on a blank line, at proper indentation.
9565 (unless (bolp) (insert "\n"))
9566 (unless (looking-at-p "^[ \t]*$") (save-excursion (insert "\n")))
9567 (when org-adapt-indentation (indent-to-column col)))
9568 (org-show-set-visibility 'lineage))
9570 (defun org-agenda-diary-entry ()
9571 "Make a diary entry, like the `i' command from the calendar.
9572 All the standard commands work: block, weekly etc.
9573 When `org-agenda-diary-file' points to a file,
9574 `org-agenda-diary-entry-in-org-file' is called instead to create
9575 entries in that Org file."
9576 (interactive)
9577 (if (not (eq org-agenda-diary-file 'diary-file))
9578 (org-agenda-diary-entry-in-org-file)
9579 (require 'diary-lib)
9580 (let* ((char (progn
9581 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
9582 (read-char-exclusive)))
9583 (cmd (cdr (assoc char
9584 '((?d . diary-insert-entry)
9585 (?w . diary-insert-weekly-entry)
9586 (?m . diary-insert-monthly-entry)
9587 (?y . diary-insert-yearly-entry)
9588 (?a . diary-insert-anniversary-entry)
9589 (?b . diary-insert-block-entry)
9590 (?c . diary-insert-cyclic-entry)))))
9591 (oldf (symbol-function 'calendar-cursor-to-date))
9592 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9593 (point (point))
9594 (mark (or (mark t) (point))))
9595 (unless cmd
9596 (user-error "No command associated with <%c>" char))
9597 (unless (and (get-text-property point 'day)
9598 (or (not (equal ?b char))
9599 (get-text-property mark 'day)))
9600 (user-error "Don't know which date to use for diary entry"))
9601 ;; We implement this by hacking the `calendar-cursor-to-date' function
9602 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9603 (let ((calendar-mark-ring
9604 (list (calendar-gregorian-from-absolute
9605 (or (get-text-property mark 'day)
9606 (get-text-property point 'day))))))
9607 (unwind-protect
9608 (progn
9609 (fset 'calendar-cursor-to-date
9610 (lambda (&optional error dummy)
9611 (calendar-gregorian-from-absolute
9612 (get-text-property point 'day))))
9613 (call-interactively cmd))
9614 (fset 'calendar-cursor-to-date oldf))))))
9616 (defun org-agenda-execute-calendar-command (cmd)
9617 "Execute a calendar command from the agenda with date from cursor."
9618 (org-agenda-check-type t 'agenda)
9619 (require 'diary-lib)
9620 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9621 (user-error "Don't know which date to use for the calendar command"))
9622 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9623 (point (point))
9624 (date (calendar-gregorian-from-absolute
9625 (get-text-property point 'day)))
9626 ;; the following 2 vars are needed in the calendar
9627 (displayed-month (car date))
9628 (displayed-year (nth 2 date)))
9629 (unwind-protect
9630 (progn
9631 (fset 'calendar-cursor-to-date
9632 (lambda (&optional error dummy)
9633 (calendar-gregorian-from-absolute
9634 (get-text-property point 'day))))
9635 (call-interactively cmd))
9636 (fset 'calendar-cursor-to-date oldf))))
9638 (defun org-agenda-phases-of-moon ()
9639 "Display the phases of the moon for the 3 months around the cursor date."
9640 (interactive)
9641 (org-agenda-execute-calendar-command 'calendar-lunar-phases))
9643 (defun org-agenda-holidays ()
9644 "Display the holidays for the 3 months around the cursor date."
9645 (interactive)
9646 (org-agenda-execute-calendar-command 'calendar-list-holidays))
9648 (defvar calendar-longitude) ; defined in calendar.el
9649 (defvar calendar-latitude) ; defined in calendar.el
9650 (defvar calendar-location-name) ; defined in calendar.el
9652 (defun org-agenda-sunrise-sunset (arg)
9653 "Display sunrise and sunset for the cursor date.
9654 Latitude and longitude can be specified with the variables
9655 `calendar-latitude' and `calendar-longitude'. When called with prefix
9656 argument, latitude and longitude will be prompted for."
9657 (interactive "P")
9658 (require 'solar)
9659 (let ((calendar-longitude (if arg nil calendar-longitude))
9660 (calendar-latitude (if arg nil calendar-latitude))
9661 (calendar-location-name
9662 (if arg "the given coordinates" calendar-location-name)))
9663 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9665 (defun org-agenda-goto-calendar ()
9666 "Open the Emacs calendar with the date at the cursor."
9667 (interactive)
9668 (org-agenda-check-type t 'agenda)
9669 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9670 (user-error "Don't know which date to open in calendar")))
9671 (date (calendar-gregorian-from-absolute day))
9672 (calendar-move-hook nil)
9673 (calendar-view-holidays-initially-flag nil)
9674 (calendar-view-diary-initially-flag nil))
9675 (calendar)
9676 (calendar-goto-date date)))
9678 ;;;###autoload
9679 (defun org-calendar-goto-agenda ()
9680 "Compute the Org agenda for the calendar date displayed at the cursor.
9681 This is a command that has to be installed in `calendar-mode-map'."
9682 (interactive)
9683 ;; Temporarily disable sticky agenda since user clearly wants to
9684 ;; refresh view anyway.
9685 (let ((org-agenda-buffer-tmp-name "*Org Agenda(a)*")
9686 (org-agenda-sticky nil))
9687 (org-agenda-list nil (calendar-absolute-from-gregorian
9688 (calendar-cursor-to-date))
9689 nil)))
9691 (defun org-agenda-convert-date ()
9692 (interactive)
9693 (org-agenda-check-type t 'agenda)
9694 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9695 date s)
9696 (unless day
9697 (user-error "Don't know which date to convert"))
9698 (setq date (calendar-gregorian-from-absolute day))
9699 (setq s (concat
9700 "Gregorian: " (calendar-date-string date) "\n"
9701 "ISO: " (calendar-iso-date-string date) "\n"
9702 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9703 "Julian: " (calendar-julian-date-string date) "\n"
9704 "Astron. JD: " (calendar-astro-date-string date)
9705 " (Julian date number at noon UTC)\n"
9706 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9707 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9708 "French: " (calendar-french-date-string date) "\n"
9709 "Bahá’í: " (calendar-bahai-date-string date) " (until sunset)\n"
9710 "Mayan: " (calendar-mayan-date-string date) "\n"
9711 "Coptic: " (calendar-coptic-date-string date) "\n"
9712 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9713 "Persian: " (calendar-persian-date-string date) "\n"
9714 "Chinese: " (calendar-chinese-date-string date) "\n"))
9715 (with-output-to-temp-buffer "*Dates*"
9716 (princ s))
9717 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9719 ;;; Bulk commands
9721 (defun org-agenda-bulk-marked-p ()
9722 "Non-nil when current entry is marked for bulk action."
9723 (eq (get-char-property (point-at-bol) 'type)
9724 'org-marked-entry-overlay))
9726 (defun org-agenda-bulk-mark (&optional arg)
9727 "Mark entries for future bulk action.
9729 When ARG is nil or one and region is not active then mark the
9730 entry at point.
9732 When ARG is nil or one and region is active then mark the entries
9733 in the region.
9735 When ARG is greater than one mark ARG lines."
9736 (interactive "p")
9737 (when (and (or (not arg) (= arg 1)) (use-region-p))
9738 (setq arg (count-lines (region-beginning) (region-end)))
9739 (goto-char (region-beginning))
9740 (deactivate-mark))
9741 (dotimes (i (or arg 1))
9742 (unless (org-get-at-bol 'org-agenda-diary-link)
9743 (let* ((m (org-get-at-bol 'org-hd-marker))
9745 (unless (org-agenda-bulk-marked-p)
9746 (unless m (user-error "Nothing to mark at point"))
9747 (push m org-agenda-bulk-marked-entries)
9748 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9749 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9750 (org-get-todo-face "TODO")
9751 'evaporate)
9752 (overlay-put ov 'type 'org-marked-entry-overlay))
9753 (end-of-line 1)
9754 (or (ignore-errors
9755 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9756 (beginning-of-line 2))
9757 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9758 (beginning-of-line 2))
9759 (message "%d entries marked for bulk action"
9760 (length org-agenda-bulk-marked-entries))))))
9762 (defun org-agenda-bulk-mark-all ()
9763 "Mark all entries for future agenda bulk action."
9764 (interactive)
9765 (org-agenda-bulk-mark-regexp "."))
9767 (defun org-agenda-bulk-mark-regexp (regexp)
9768 "Mark entries matching REGEXP for future agenda bulk action."
9769 (interactive "sMark entries matching regexp: ")
9770 (let ((entries-marked 0) txt-at-point)
9771 (save-excursion
9772 (goto-char (point-min))
9773 (goto-char (next-single-property-change (point) 'org-hd-marker))
9774 (while (and (re-search-forward regexp nil t)
9775 (setq txt-at-point (get-text-property (point) 'txt)))
9776 (if (get-char-property (point) 'invisible)
9777 (beginning-of-line 2)
9778 (when (string-match regexp txt-at-point)
9779 (setq entries-marked (1+ entries-marked))
9780 (call-interactively 'org-agenda-bulk-mark)))))
9782 (if (not entries-marked)
9783 (message "No entry matching this regexp."))))
9785 (defun org-agenda-bulk-unmark (&optional arg)
9786 "Unmark the entry at point for future bulk action."
9787 (interactive "P")
9788 (if arg
9789 (org-agenda-bulk-unmark-all)
9790 (cond ((org-agenda-bulk-marked-p)
9791 (org-agenda-bulk-remove-overlays
9792 (point-at-bol) (+ 2 (point-at-bol)))
9793 (setq org-agenda-bulk-marked-entries
9794 (delete (org-get-at-bol 'org-hd-marker)
9795 org-agenda-bulk-marked-entries))
9796 (end-of-line 1)
9797 (or (ignore-errors
9798 (goto-char (next-single-property-change (point) 'txt)))
9799 (beginning-of-line 2))
9800 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9801 (beginning-of-line 2))
9802 (message "%d entries left marked for bulk action"
9803 (length org-agenda-bulk-marked-entries)))
9804 (t (message "No entry to unmark here")))))
9806 (defun org-agenda-bulk-toggle-all ()
9807 "Toggle all marks for bulk action."
9808 (interactive)
9809 (save-excursion
9810 (goto-char (point-min))
9811 (while (ignore-errors
9812 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9813 (org-agenda-bulk-toggle))))
9815 (defun org-agenda-bulk-toggle ()
9816 "Toggle the mark at point for bulk action."
9817 (interactive)
9818 (if (org-agenda-bulk-marked-p)
9819 (org-agenda-bulk-unmark)
9820 (org-agenda-bulk-mark)))
9822 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9823 "Remove the mark overlays between BEG and END in the agenda buffer.
9824 BEG and END default to the buffer limits.
9826 This only removes the overlays, it does not remove the markers
9827 from the list in `org-agenda-bulk-marked-entries'."
9828 (interactive)
9829 (mapc (lambda (ov)
9830 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9831 (delete-overlay ov)))
9832 (overlays-in (or beg (point-min)) (or end (point-max)))))
9834 (defun org-agenda-bulk-unmark-all ()
9835 "Remove all marks in the agenda buffer.
9836 This will remove the markers and the overlays."
9837 (interactive)
9838 (if (null org-agenda-bulk-marked-entries)
9839 (message "No entry to unmark")
9840 (setq org-agenda-bulk-marked-entries nil)
9841 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9843 (defcustom org-agenda-persistent-marks nil
9844 "Non-nil means marked items will stay marked after a bulk action.
9845 You can toggle this interactively by typing `p' when prompted for a
9846 bulk action."
9847 :group 'org-agenda
9848 :version "24.1"
9849 :type 'boolean)
9851 (defun org-agenda-bulk-action (&optional arg)
9852 "Execute an remote-editing action on all marked entries.
9853 The prefix arg is passed through to the command if possible."
9854 (interactive "P")
9855 ;; Make sure we have markers, and only valid ones.
9856 (unless org-agenda-bulk-marked-entries (user-error "No entries are marked"))
9857 (dolist (m org-agenda-bulk-marked-entries)
9858 (unless (and (markerp m)
9859 (marker-buffer m)
9860 (buffer-live-p (marker-buffer m))
9861 (marker-position m))
9862 (user-error "Marker %s for bulk command is invalid" m)))
9864 ;; Prompt for the bulk command.
9865 (message
9866 (concat (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")
9867 "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9868 "[S]catter [f]unction "
9869 (and org-agenda-bulk-custom-functions
9870 (format " Custom: [%s]"
9871 (mapconcat (lambda (f) (char-to-string (car f)))
9872 org-agenda-bulk-custom-functions
9873 "")))))
9874 (catch 'exit
9875 (let* ((org-log-refile (if org-log-refile 'time nil))
9876 (entries (reverse org-agenda-bulk-marked-entries))
9877 (org-overriding-default-time
9878 (and (get-text-property (point) 'org-agenda-date-header)
9879 (org-get-cursor-date)))
9880 redo-at-end
9881 cmd)
9882 (pcase (read-char-exclusive)
9884 (let ((org-agenda-persistent-marks
9885 (not org-agenda-persistent-marks)))
9886 (org-agenda-bulk-action)
9887 (throw 'exit nil)))
9890 (setq cmd #'org-agenda-archive))
9893 (setq cmd #'org-agenda-archive-to-archive-sibling))
9895 ((or ?r ?w)
9896 (let ((refile-location
9897 (org-refile-get-location
9898 "Refile to"
9899 (marker-buffer (car entries))
9900 org-refile-allow-creating-parent-nodes)))
9901 (when (nth 3 refile-location)
9902 (setcar (nthcdr 3 refile-location)
9903 (move-marker
9904 (make-marker)
9905 (nth 3 refile-location)
9906 (or (get-file-buffer (nth 1 refile-location))
9907 (find-buffer-visiting (nth 1 refile-location))
9908 (error "This should not happen")))))
9910 (setq cmd `(lambda () (org-agenda-refile nil ',refile-location t)))
9911 (setq redo-at-end t)))
9914 (let ((state (completing-read
9915 "Todo state: "
9916 (with-current-buffer (marker-buffer (car entries))
9917 (mapcar #'list org-todo-keywords-1)))))
9918 (setq cmd `(lambda ()
9919 (let ((org-inhibit-blocking t)
9920 (org-inhibit-logging 'note))
9921 (org-agenda-todo ,state))))))
9923 ((and (or ?- ?+) action)
9924 (let ((tag (completing-read
9925 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9926 (with-current-buffer (marker-buffer (car entries))
9927 (delq nil
9928 (mapcar (lambda (x) (and (stringp (car x)) x))
9929 org-current-tag-alist))))))
9930 (setq cmd
9931 `(lambda ()
9932 (org-agenda-set-tags ,tag
9933 ,(if (eq action ?+) ''on ''off))))))
9936 (let ((time
9937 (and (not arg)
9938 (org-read-date nil nil nil "(Re)Schedule to"
9939 org-overriding-default-time))))
9940 ;; Make sure to not prompt for a note when bulk
9941 ;; rescheduling as Org cannot cope with simultaneous notes.
9942 ;; Besides, it could be annoying depending on the number of
9943 ;; items re-scheduled.
9944 (setq cmd
9945 `(lambda ()
9946 (let ((org-log-reschedule (and org-log-reschedule 'time)))
9947 (org-agenda-schedule arg ,time))))))
9949 (let ((time
9950 (and (not arg)
9951 (org-read-date nil nil nil "(Re)Set Deadline to"
9952 org-overriding-default-time))))
9953 ;; Make sure to not prompt for a note when bulk
9954 ;; rescheduling as Org cannot cope with simultaneous
9955 ;; notes. Besides, it could be annoying depending on the
9956 ;; number of items re-scheduled.
9957 (setq cmd
9958 `(lambda ()
9959 (let ((org-log-redeadline (and org-log-redeadline 'time)))
9960 (org-agenda-deadline arg ,time))))))
9963 (unless (org-agenda-check-type nil 'agenda 'todo)
9964 (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type))
9965 (let ((days (read-number
9966 (format "Scatter tasks across how many %sdays: "
9967 (if arg "week" ""))
9968 7)))
9969 (setq cmd
9970 `(lambda ()
9971 (let ((distance (1+ (random ,days))))
9972 (when arg
9973 (let ((dist distance)
9974 (day-of-week
9975 (calendar-day-of-week
9976 (calendar-gregorian-from-absolute (org-today)))))
9977 (dotimes (i (1+ dist))
9978 (while (member day-of-week org-agenda-weekend-days)
9979 (cl-incf distance)
9980 (cl-incf day-of-week)
9981 (when (= day-of-week 7)
9982 (setq day-of-week 0)))
9983 (cl-incf day-of-week)
9984 (when (= day-of-week 7)
9985 (setq day-of-week 0)))))
9986 ;; Silently fail when try to replan a sexp entry.
9987 (ignore-errors
9988 (let* ((date (calendar-gregorian-from-absolute
9989 (+ (org-today) distance)))
9990 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9991 (nth 2 date))))
9992 (org-agenda-schedule nil time))))))))
9995 (setq cmd
9996 (intern
9997 (completing-read "Function: " obarray #'fboundp t nil nil))))
9999 (action
10000 (pcase (assoc action org-agenda-bulk-custom-functions)
10001 (`(,_ ,f) (setq cmd f) (setq redo-at-end t))
10002 (_ (user-error "Invalid bulk action: %c" action)))))
10004 ;; Sort the markers, to make sure that parents are handled
10005 ;; before children.
10006 (setq entries (sort entries
10007 (lambda (a b)
10008 (cond
10009 ((eq (marker-buffer a) (marker-buffer b))
10010 (< (marker-position a) (marker-position b)))
10012 (string< (buffer-name (marker-buffer a))
10013 (buffer-name (marker-buffer b))))))))
10015 ;; Now loop over all markers and apply CMD.
10016 (let ((processed 0)
10017 (skipped 0))
10018 (dolist (e entries)
10019 (let ((pos (text-property-any (point-min) (point-max) 'org-hd-marker e)))
10020 (if (not pos)
10021 (progn (message "Skipping removed entry at %s" e)
10022 (cl-incf skipped))
10023 (goto-char pos)
10024 (let (org-loop-over-headlines-in-active-region) (funcall cmd))
10025 ;; `post-command-hook' is not run yet. We make sure any
10026 ;; pending log note is processed.
10027 (when (or (memq 'org-add-log-note (default-value 'post-command-hook))
10028 (memq 'org-add-log-note post-command-hook))
10029 (org-add-log-note))
10030 (cl-incf processed))))
10031 (when redo-at-end (org-agenda-redo))
10032 (unless org-agenda-persistent-marks (org-agenda-bulk-unmark-all))
10033 (message "Acted on %d entries%s%s"
10034 processed
10035 (if (= skipped 0)
10037 (format ", skipped %d (disappeared before their turn)"
10038 skipped))
10039 (if (not org-agenda-persistent-marks) "" " (kept marked)"))))))
10041 (defun org-agenda-capture (&optional with-time)
10042 "Call `org-capture' with the date at point.
10043 With a `C-1' prefix, use the HH:MM value at point (if any) or the
10044 current HH:MM time."
10045 (interactive "P")
10046 (if (not (eq major-mode 'org-agenda-mode))
10047 (user-error "You cannot do this outside of agenda buffers")
10048 (let ((org-overriding-default-time
10049 (org-get-cursor-date (equal with-time 1))))
10050 (call-interactively 'org-capture))))
10052 ;;; Dragging agenda lines forward/backward
10054 (defun org-agenda-reapply-filters ()
10055 "Re-apply all agenda filters."
10056 (mapcar
10057 (lambda(f) (when (car f) (org-agenda-filter-apply (car f) (cadr f) t)))
10058 `((,org-agenda-tag-filter tag)
10059 (,org-agenda-category-filter category)
10060 (,org-agenda-regexp-filter regexp)
10061 (,org-agenda-effort-filter effort)
10062 (,(get 'org-agenda-tag-filter :preset-filter) tag)
10063 (,(get 'org-agenda-category-filter :preset-filter) category)
10064 (,(get 'org-agenda-effort-filter :preset-filter) effort)
10065 (,(get 'org-agenda-regexp-filter :preset-filter) regexp))))
10067 (defun org-agenda-drag-line-forward (arg &optional backward)
10068 "Drag an agenda line forward by ARG lines.
10069 When the optional argument `backward' is non-nil, move backward."
10070 (interactive "p")
10071 (let ((inhibit-read-only t) lst line)
10072 (if (or (not (get-text-property (point) 'txt))
10073 (save-excursion
10074 (dotimes (n arg)
10075 (move-beginning-of-line (if backward 0 2))
10076 (push (not (get-text-property (point) 'txt)) lst))
10077 (delq nil lst)))
10078 (message "Cannot move line forward")
10079 (let ((end (save-excursion (move-beginning-of-line 2) (point))))
10080 (move-beginning-of-line 1)
10081 (setq line (buffer-substring (point) end))
10082 (delete-region (point) end)
10083 (move-beginning-of-line (funcall (if backward '1- '1+) arg))
10084 (insert line)
10085 (org-agenda-reapply-filters)
10086 (org-agenda-mark-clocking-task)
10087 (move-beginning-of-line 0)))))
10089 (defun org-agenda-drag-line-backward (arg)
10090 "Drag an agenda line backward by ARG lines."
10091 (interactive "p")
10092 (org-agenda-drag-line-forward arg t))
10094 ;;; Flagging notes
10096 (defun org-agenda-show-the-flagging-note ()
10097 "Display the flagging note in the other window.
10098 When called a second time in direct sequence, offer to remove the FLAGGING
10099 tag and (if present) the flagging note."
10100 (interactive)
10101 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
10102 (win (selected-window))
10103 note heading newhead)
10104 (unless hdmarker
10105 (user-error "No linked entry at point"))
10106 (if (and (eq this-command last-command)
10107 (y-or-n-p "Unflag and remove any flagging note? "))
10108 (progn
10109 (org-agenda-remove-flag hdmarker)
10110 (let ((win (get-buffer-window "*Flagging Note*")))
10111 (and win (delete-window win)))
10112 (message "Entry unflagged"))
10113 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
10114 (unless note
10115 (user-error "No flagging note"))
10116 (org-kill-new note)
10117 (org-switch-to-buffer-other-window "*Flagging Note*")
10118 (erase-buffer)
10119 (insert note)
10120 (goto-char (point-min))
10121 (while (re-search-forward "\\\\n" nil t)
10122 (replace-match "\n" t t))
10123 (goto-char (point-min))
10124 (select-window win)
10125 (message "%s" (substitute-command-keys "Flagging note pushed to \
10126 kill ring. Press `\\[org-agenda-show-the-flagging-note]' again to remove \
10127 tag and note")))))
10129 (defun org-agenda-remove-flag (marker)
10130 "Remove the FLAGGED tag and any flagging note in the entry."
10131 (let (newhead)
10132 (org-with-point-at marker
10133 (org-toggle-tag "FLAGGED" 'off)
10134 (org-entry-delete nil "THEFLAGGINGNOTE")
10135 (setq newhead (org-get-heading)))
10136 (org-agenda-change-all-lines newhead marker)
10137 (message "Entry unflagged")))
10139 (defun org-agenda-get-any-marker (&optional pos)
10140 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
10141 (get-text-property (or pos (point-at-bol)) 'org-marker)))
10143 ;;; Appointment reminders
10145 (defvar appt-time-msg-list) ; defined in appt.el
10147 ;;;###autoload
10148 (defun org-agenda-to-appt (&optional refresh filter &rest args)
10149 "Activate appointments found in `org-agenda-files'.
10151 With a `\\[universal-argument]' prefix, refresh the list of \
10152 appointments.
10154 If FILTER is t, interactively prompt the user for a regular
10155 expression, and filter out entries that don't match it.
10157 If FILTER is a string, use this string as a regular expression
10158 for filtering entries out.
10160 If FILTER is a function, filter out entries against which
10161 calling the function returns nil. This function takes one
10162 argument: an entry from `org-agenda-get-day-entries'.
10164 FILTER can also be an alist with the car of each cell being
10165 either `headline' or `category'. For example:
10167 \\='((headline \"IMPORTANT\")
10168 (category \"Work\"))
10170 will only add headlines containing IMPORTANT or headlines
10171 belonging to the \"Work\" category.
10173 ARGS are symbols indicating what kind of entries to consider.
10174 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
10175 \(i.e., deadlines and scheduled items with a hh:mm specification)
10176 and :timestamp entries. See the docstring of `org-diary' for
10177 details and examples.
10179 If an entry has a APPT_WARNTIME property, its value will be used
10180 to override `appt-message-warning-time'."
10181 (interactive "P")
10182 (if refresh (setq appt-time-msg-list nil))
10183 (if (eq filter t)
10184 (setq filter (read-from-minibuffer "Regexp filter: ")))
10185 (let* ((cnt 0) ; count added events
10186 (scope (or args '(:deadline* :scheduled* :timestamp)))
10187 (org-agenda-new-buffers nil)
10188 (org-deadline-warning-days 0)
10189 ;; Do not use `org-today' here because appt only takes
10190 ;; time and without date as argument, so it may pass wrong
10191 ;; information otherwise
10192 (today (org-date-to-gregorian
10193 (time-to-days (current-time))))
10194 (org-agenda-restrict nil)
10195 (files (org-agenda-files 'unrestricted)) entries file
10196 (org-agenda-buffer nil))
10197 ;; Get all entries which may contain an appt
10198 (org-agenda-prepare-buffers files)
10199 (while (setq file (pop files))
10200 (setq entries
10201 (delq nil
10202 (append entries
10203 (apply 'org-agenda-get-day-entries
10204 file today scope)))))
10205 ;; Map thru entries and find if we should filter them out
10206 (mapc
10207 (lambda (x)
10208 (let* ((evt (org-trim
10209 (replace-regexp-in-string
10210 org-bracket-link-regexp "\\3"
10211 (or (get-text-property 1 'txt x) ""))))
10212 (cat (get-text-property (1- (length x)) 'org-category x))
10213 (tod (get-text-property 1 'time-of-day x))
10214 (ok (or (null filter)
10215 (and (stringp filter) (string-match filter evt))
10216 (and (functionp filter) (funcall filter x))
10217 (and (listp filter)
10218 (let ((cat-filter (cadr (assq 'category filter)))
10219 (evt-filter (cadr (assq 'headline filter))))
10220 (or (and (stringp cat-filter)
10221 (string-match cat-filter cat))
10222 (and (stringp evt-filter)
10223 (string-match evt-filter evt)))))))
10224 (wrn (get-text-property 1 'warntime x)))
10225 ;; FIXME: Shall we remove text-properties for the appt text?
10226 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
10227 (when (and ok tod (not (string-match "\\`DONE\\|CANCELLED" evt)))
10228 (setq tod (concat "00" (number-to-string tod)))
10229 (setq tod (when (string-match
10230 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
10231 (concat (match-string 1 tod) ":"
10232 (match-string 2 tod))))
10233 (when (appt-add tod evt wrn)
10234 (setq cnt (1+ cnt))))))
10235 entries)
10236 (org-release-buffers org-agenda-new-buffers)
10237 (if (eq cnt 0)
10238 (message "No event to add")
10239 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
10241 (defun org-agenda-today-p (date)
10242 "Non nil when DATE means today.
10243 DATE is either a list of the form (month day year) or a number of
10244 days as returned by `calendar-absolute-from-gregorian' or
10245 `org-today'. This function considers `org-extend-today-until'
10246 when defining today."
10247 (eq (org-today)
10248 (if (consp date) (calendar-absolute-from-gregorian date) date)))
10250 (defun org-agenda-todo-yesterday (&optional arg)
10251 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
10252 (interactive "P")
10253 (let* ((org-use-effective-time t)
10254 (hour (nth 2 (decode-time (org-current-time))))
10255 (org-extend-today-until (1+ hour)))
10256 (org-agenda-todo arg)))
10258 (provide 'org-agenda)
10260 ;;; org-agenda.el ends here