Merge branch 'maint'
[org-mode/org-tableheadings.git] / lisp / org-agenda.el
blob78c1a37f48b3cc08ab5649d263a38802d7c172fa
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004-2017 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file contains the code for creating and using the Agenda for Org.
29 ;; The functions `org-batch-agenda', `org-batch-agenda-csv', and
30 ;; `org-batch-store-agenda-views' are implemented as macros to provide
31 ;; a convenient way for extracting agenda information from the command
32 ;; line. The Lisp does not evaluate parameters of a macro call; thus
33 ;; it is not necessary to quote the parameters passed to one of those
34 ;; functions. E.g. you can write:
36 ;; emacs -batch -l ~/.emacs -eval '(org-batch-agenda "a" org-agenda-span 7)'
38 ;; To export an agenda spanning 7 days. If `org-batch-agenda' would
39 ;; have been implemented as a regular function you'd have to quote the
40 ;; symbol org-agenda-span. Moreover: To use a symbol as parameter
41 ;; value you would have to double quote the symbol.
43 ;; This is a hack, but it works even when running Org byte-compiled.
46 ;;; Code:
48 (require 'cl-lib)
49 (require 'org)
50 (require 'org-macs)
52 (declare-function diary-add-to-list "diary-lib"
53 (date string specifier &optional marker globcolor literal))
54 (declare-function calendar-iso-to-absolute "cal-iso" (date))
55 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
56 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
57 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
58 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
59 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
60 (declare-function calendar-french-date-string "cal-french" (&optional date))
61 (declare-function calendar-goto-date "cal-move" (date))
62 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
63 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
64 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
65 (declare-function calendar-iso-from-absolute "cal-iso" (date))
66 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
67 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
68 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
69 (declare-function calendar-check-holidays "holidays" (date))
71 (declare-function org-columns-remove-overlays "org-colview" ())
72 (declare-function org-datetree-find-date-create "org-datetree"
73 (date &optional keep-restriction))
74 (declare-function org-columns-quit "org-colview" ())
75 (declare-function diary-date-display-form "diary-lib" (&optional type))
76 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
77 (declare-function org-habit-insert-consistency-graphs
78 "org-habit" (&optional line))
79 (declare-function org-is-habit-p "org-habit" (&optional pom))
80 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
81 (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
82 (declare-function org-agenda-columns "org-colview" ())
83 (declare-function org-add-archive-files "org-archive" (files))
84 (declare-function org-capture "org-capture" (&optional goto keys))
86 (defvar calendar-mode-map)
87 (defvar org-clock-current-task)
88 (defvar org-current-tag-alist)
89 (defvar org-mobile-force-id-on-agenda-items)
90 (defvar org-habit-show-habits)
91 (defvar org-habit-show-habits-only-for-today)
92 (defvar org-habit-show-all-today)
94 ;; Defined somewhere in this file, but used before definition.
95 (defvar org-agenda-buffer-name "*Org Agenda*")
96 (defvar org-agenda-overriding-header nil)
97 (defvar org-agenda-title-append nil)
98 (with-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
99 (with-no-warnings (defvar date)) ;; unprefixed, from calendar.el
100 (defvar original-date) ; dynamically scoped, calendar.el does scope this
102 (defvar org-agenda-undo-list nil
103 "List of undoable operations in the agenda since last refresh.")
104 (defvar org-agenda-pending-undo-list nil
105 "In a series of undo commands, this is the list of remaining undo items.")
107 (defcustom org-agenda-confirm-kill 1
108 "When set, remote killing from the agenda buffer needs confirmation.
109 When t, a confirmation is always needed. When a number N, confirmation is
110 only needed when the text to be killed contains more than N non-white lines."
111 :group 'org-agenda
112 :type '(choice
113 (const :tag "Never" nil)
114 (const :tag "Always" t)
115 (integer :tag "When more than N lines")))
117 (defcustom org-agenda-compact-blocks nil
118 "Non-nil means make the block agenda more compact.
119 This is done globally by leaving out lines like the agenda span
120 name and week number or the separator lines."
121 :group 'org-agenda
122 :type 'boolean)
124 (defcustom org-agenda-block-separator ?=
125 "The separator between blocks in the agenda.
126 If this is a string, it will be used as the separator, with a newline added.
127 If it is a character, it will be repeated to fill the window width.
128 If nil the separator is disabled. In `org-agenda-custom-commands' this
129 addresses the separator between the current and the previous block."
130 :group 'org-agenda
131 :type '(choice
132 (const :tag "Disabled" nil)
133 (character)
134 (string)))
136 (defgroup org-agenda-export nil
137 "Options concerning exporting agenda views in Org mode."
138 :tag "Org Agenda Export"
139 :group 'org-agenda)
141 (defcustom org-agenda-with-colors t
142 "Non-nil means use colors in agenda views."
143 :group 'org-agenda-export
144 :type 'boolean)
146 (defcustom org-agenda-exporter-settings nil
147 "Alist of variable/value pairs that should be active during agenda export.
148 This is a good place to set options for ps-print and for htmlize.
149 Note that the way this is implemented, the values will be evaluated
150 before assigned to the variables. So make sure to quote values you do
151 *not* want evaluated, for example
153 (setq org-agenda-exporter-settings
154 \\='((ps-print-color-p \\='black-white)))"
155 :group 'org-agenda-export
156 :type '(repeat
157 (list
158 (variable)
159 (sexp :tag "Value"))))
161 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
162 "Hook run in a temporary buffer before writing the agenda to an export file.
163 A useful function for this hook is `org-agenda-add-entry-text'."
164 :group 'org-agenda-export
165 :type 'hook
166 :options '(org-agenda-add-entry-text))
168 (defcustom org-agenda-add-entry-text-maxlines 0
169 "Maximum number of entry text lines to be added to agenda.
170 This is only relevant when `org-agenda-add-entry-text' is part of
171 `org-agenda-before-write-hook', which is the default.
172 When this is 0, nothing will happen. When it is greater than 0, it
173 specifies the maximum number of lines that will be added for each entry
174 that is listed in the agenda view.
176 Note that this variable is not used during display, only when exporting
177 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
178 and `org-agenda-entry-text-maxlines'."
179 :group 'org-agenda
180 :type 'integer)
182 (defcustom org-agenda-add-entry-text-descriptive-links t
183 "Non-nil means export org-links as descriptive links in agenda added text.
184 This variable applies to the text added to the agenda when
185 `org-agenda-add-entry-text-maxlines' is larger than 0.
186 When this variable nil, the URL will (also) be shown."
187 :group 'org-agenda
188 :type 'boolean)
190 (defcustom org-agenda-export-html-style nil
191 "The style specification for exported HTML Agenda files.
192 If this variable contains a string, it will replace the default <style>
193 section as produced by `htmlize'.
194 Since there are different ways of setting style information, this variable
195 needs to contain the full HTML structure to provide a style, including the
196 surrounding HTML tags. The style specifications should include definitions
197 the fonts used by the agenda, here is an example:
199 <style type=\"text/css\">
200 p { font-weight: normal; color: gray; }
201 .org-agenda-structure {
202 font-size: 110%;
203 color: #003399;
204 font-weight: 600;
206 .org-todo {
207 color: #cc6666;
208 font-weight: bold;
210 .org-agenda-done {
211 color: #339933;
213 .org-done {
214 color: #339933;
216 .title { text-align: center; }
217 .todo, .deadline { color: red; }
218 .done { color: green; }
219 </style>
221 or, if you want to keep the style in a file,
223 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
225 As the value of this option simply gets inserted into the HTML <head> header,
226 you can \"misuse\" it to also add other text to the header."
227 :group 'org-agenda-export
228 :group 'org-export-html
229 :type '(choice
230 (const nil)
231 (string)))
233 (defcustom org-agenda-persistent-filter nil
234 "When set, keep filters from one agenda view to the next."
235 :group 'org-agenda
236 :type 'boolean)
238 (defgroup org-agenda-custom-commands nil
239 "Options concerning agenda views in Org mode."
240 :tag "Org Agenda Custom Commands"
241 :group 'org-agenda)
243 (defconst org-sorting-choice
244 '(choice
245 (const time-up) (const time-down)
246 (const timestamp-up) (const timestamp-down)
247 (const scheduled-up) (const scheduled-down)
248 (const deadline-up) (const deadline-down)
249 (const ts-up) (const ts-down)
250 (const tsia-up) (const tsia-down)
251 (const category-keep) (const category-up) (const category-down)
252 (const tag-down) (const tag-up)
253 (const priority-up) (const priority-down)
254 (const todo-state-up) (const todo-state-down)
255 (const effort-up) (const effort-down)
256 (const habit-up) (const habit-down)
257 (const alpha-up) (const alpha-down)
258 (const user-defined-up) (const user-defined-down))
259 "Sorting choices.")
261 ;; Keep custom values for `org-agenda-filter-preset' compatible with
262 ;; the new variable `org-agenda-tag-filter-preset'.
263 (defvaralias 'org-agenda-filter-preset 'org-agenda-tag-filter-preset)
264 (defvaralias 'org-agenda-filter 'org-agenda-tag-filter)
266 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
267 "List of types searched for when creating the daily/weekly agenda.
268 This variable is a list of symbols that controls the types of
269 items that appear in the daily/weekly agenda. Allowed symbols in this
270 list are are
272 :timestamp List items containing a date stamp or date range matching
273 the selected date. This includes sexp entries in angular
274 brackets.
276 :sexp List entries resulting from plain diary-like sexps.
278 :deadline List deadline due on that date. When the date is today,
279 also list any deadlines past due, or due within
280 `org-deadline-warning-days'. `:deadline' must appear before
281 `:scheduled' if the setting of
282 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
283 any effect.
285 :deadline* Same as above, but only include the deadline if it has an
286 hour specification as [h]h:mm.
288 :scheduled List all items which are scheduled for the given date.
289 The diary for *today* also contains items which were
290 scheduled earlier and are not yet marked DONE.
292 :scheduled* Same as above, but only include the scheduled item if it
293 has an hour specification as [h]h:mm.
295 By default, all four non-starred types are turned on.
297 When :scheduled* or :deadline* are included, :schedule or :deadline
298 will be ignored.
300 Never set this variable globally using `setq', because then it
301 will apply to all future agenda commands. Instead, bind it with
302 `let' to scope it dynamically into the agenda-constructing
303 command. A good way to set it is through options in
304 `org-agenda-custom-commands'. For a more flexible (though
305 somewhat less efficient) way of determining what is included in
306 the daily/weekly agenda, see `org-agenda-skip-function'.")
308 (defconst org-agenda-custom-commands-local-options
309 `(repeat :tag "Local settings for this command. Remember to quote values"
310 (choice :tag "Setting"
311 (list :tag "Heading for this block"
312 (const org-agenda-overriding-header)
313 (string :tag "Headline"))
314 (list :tag "Files to be searched"
315 (const org-agenda-files)
316 (list
317 (const :format "" quote)
318 (repeat (file))))
319 (list :tag "Sorting strategy"
320 (const org-agenda-sorting-strategy)
321 (list
322 (const :format "" quote)
323 (repeat
324 ,org-sorting-choice)))
325 (list :tag "Prefix format"
326 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
327 (string))
328 (list :tag "Number of days in agenda"
329 (const org-agenda-span)
330 (choice (const :tag "Day" day)
331 (const :tag "Week" week)
332 (const :tag "Fortnight" fortnight)
333 (const :tag "Month" month)
334 (const :tag "Year" year)
335 (integer :tag "Custom")))
336 (list :tag "Fixed starting date"
337 (const org-agenda-start-day)
338 (string :value "2007-11-01"))
339 (list :tag "Start on day of week"
340 (const org-agenda-start-on-weekday)
341 (choice :value 1
342 (const :tag "Today" nil)
343 (integer :tag "Weekday No.")))
344 (list :tag "Include data from diary"
345 (const org-agenda-include-diary)
346 (boolean))
347 (list :tag "Deadline Warning days"
348 (const org-deadline-warning-days)
349 (integer :value 1))
350 (list :tag "Category filter preset"
351 (const org-agenda-category-filter-preset)
352 (list
353 (const :format "" quote)
354 (repeat
355 (string :tag "+category or -category"))))
356 (list :tag "Tags filter preset"
357 (const org-agenda-tag-filter-preset)
358 (list
359 (const :format "" quote)
360 (repeat
361 (string :tag "+tag or -tag"))))
362 (list :tag "Effort filter preset"
363 (const org-agenda-effort-filter-preset)
364 (list
365 (const :format "" quote)
366 (repeat
367 (string :tag "+=10 or -=10 or +<10 or ->10"))))
368 (list :tag "Regexp filter preset"
369 (const org-agenda-regexp-filter-preset)
370 (list
371 (const :format "" quote)
372 (repeat
373 (string :tag "+regexp or -regexp"))))
374 (list :tag "Set daily/weekly entry types"
375 (const org-agenda-entry-types)
376 (list
377 (const :format "" quote)
378 (set :greedy t :value ,org-agenda-entry-types
379 (const :deadline)
380 (const :scheduled)
381 (const :deadline*)
382 (const :scheduled*)
383 (const :timestamp)
384 (const :sexp))))
385 (list :tag "Standard skipping condition"
386 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
387 (const org-agenda-skip-function)
388 (list
389 (const :format "" quote)
390 (list
391 (choice
392 :tag "Skipping range"
393 (const :tag "Skip entry" org-agenda-skip-entry-if)
394 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
395 (repeat :inline t :tag "Conditions for skipping"
396 (choice
397 :tag "Condition type"
398 (list :tag "Regexp matches" :inline t
399 (const :format "" 'regexp)
400 (regexp))
401 (list :tag "Regexp does not match" :inline t
402 (const :format "" 'notregexp)
403 (regexp))
404 (list :tag "TODO state is" :inline t
405 (const 'todo)
406 (choice
407 (const :tag "Any not-done state" 'todo)
408 (const :tag "Any done state" 'done)
409 (const :tag "Any state" 'any)
410 (list :tag "Keyword list"
411 (const :format "" quote)
412 (repeat (string :tag "Keyword")))))
413 (list :tag "TODO state is not" :inline t
414 (const 'nottodo)
415 (choice
416 (const :tag "Any not-done state" 'todo)
417 (const :tag "Any done state" 'done)
418 (const :tag "Any state" 'any)
419 (list :tag "Keyword list"
420 (const :format "" quote)
421 (repeat (string :tag "Keyword")))))
422 (const :tag "scheduled" 'scheduled)
423 (const :tag "not scheduled" 'notscheduled)
424 (const :tag "deadline" 'deadline)
425 (const :tag "no deadline" 'notdeadline)
426 (const :tag "timestamp" 'timestamp)
427 (const :tag "no timestamp" 'nottimestamp))))))
428 (list :tag "Non-standard skipping condition"
429 :value (org-agenda-skip-function)
430 (const org-agenda-skip-function)
431 (sexp :tag "Function or form (quoted!)"))
432 (list :tag "Any variable"
433 (variable :tag "Variable")
434 (sexp :tag "Value (sexp)"))))
435 "Selection of examples for agenda command settings.
436 This will be spliced into the custom type of
437 `org-agenda-custom-commands'.")
440 (defcustom org-agenda-custom-commands
441 '(("n" "Agenda and all TODOs" ((agenda "") (alltodo ""))))
442 "Custom commands for the agenda.
443 \\<org-mode-map>
444 These commands will be offered on the splash screen displayed by the
445 agenda dispatcher `\\[org-agenda]'. Each entry is a list like this:
447 (key desc type match settings files)
449 key The key (one or more characters as a string) to be associated
450 with the command.
451 desc A description of the command, when omitted or nil, a default
452 description is built using MATCH.
453 type The command type, any of the following symbols:
454 agenda The daily/weekly agenda.
455 todo Entries with a specific TODO keyword, in all agenda files.
456 search Entries containing search words entry or headline.
457 tags Tags/Property/TODO match in all agenda files.
458 tags-todo Tags/P/T match in all agenda files, TODO entries only.
459 todo-tree Sparse tree of specific TODO keyword in *current* file.
460 tags-tree Sparse tree with all tags matches in *current* file.
461 occur-tree Occur sparse tree for *current* file.
462 ... A user-defined function.
463 match What to search for:
464 - a single keyword for TODO keyword searches
465 - a tags match expression for tags searches
466 - a word search expression for text searches.
467 - a regular expression for occur searches
468 For all other commands, this should be the empty string.
469 settings A list of option settings, similar to that in a let form, so like
470 this: ((opt1 val1) (opt2 val2) ...). The values will be
471 evaluated at the moment of execution, so quote them when needed.
472 files A list of files to write the produced agenda buffer to with
473 the command `org-store-agenda-views'.
474 If a file name ends in \".html\", an HTML version of the buffer
475 is written out. If it ends in \".ps\", a postscript version is
476 produced. Otherwise, only the plain text is written to the file.
478 You can also define a set of commands, to create a composite agenda buffer.
479 In this case, an entry looks like this:
481 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
483 where
485 desc A description string to be displayed in the dispatcher menu.
486 cmd An agenda command, similar to the above. However, tree commands
487 are not allowed, but instead you can get agenda and global todo list.
488 So valid commands for a set are:
489 (agenda \"\" settings)
490 (alltodo \"\" settings)
491 (stuck \"\" settings)
492 (todo \"match\" settings files)
493 (search \"match\" settings files)
494 (tags \"match\" settings files)
495 (tags-todo \"match\" settings files)
497 Each command can carry a list of options, and another set of options can be
498 given for the whole set of commands. Individual command options take
499 precedence over the general options.
501 When using several characters as key to a command, the first characters
502 are prefix commands. For the dispatcher to display useful information, you
503 should provide a description for the prefix, like
505 (setq org-agenda-custom-commands
506 \\='((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
507 (\"hl\" tags \"+HOME+Lisa\")
508 (\"hp\" tags \"+HOME+Peter\")
509 (\"hk\" tags \"+HOME+Kim\")))"
510 :group 'org-agenda-custom-commands
511 :type `(repeat
512 (choice :value ("x" "Describe command here" tags "" nil)
513 (list :tag "Single command"
514 (string :tag "Access Key(s) ")
515 (option (string :tag "Description"))
516 (choice
517 (const :tag "Agenda" agenda)
518 (const :tag "TODO list" alltodo)
519 (const :tag "Search words" search)
520 (const :tag "Stuck projects" stuck)
521 (const :tag "Tags/Property match (all agenda files)" tags)
522 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
523 (const :tag "TODO keyword search (all agenda files)" todo)
524 (const :tag "Tags sparse tree (current buffer)" tags-tree)
525 (const :tag "TODO keyword tree (current buffer)" todo-tree)
526 (const :tag "Occur tree (current buffer)" occur-tree)
527 (sexp :tag "Other, user-defined function"))
528 (string :tag "Match (only for some commands)")
529 ,org-agenda-custom-commands-local-options
530 (option (repeat :tag "Export" (file :tag "Export to"))))
531 (list :tag "Command series, all agenda files"
532 (string :tag "Access Key(s)")
533 (string :tag "Description ")
534 (repeat :tag "Component"
535 (choice
536 (list :tag "Agenda"
537 (const :format "" agenda)
538 (const :tag "" :format "" "")
539 ,org-agenda-custom-commands-local-options)
540 (list :tag "TODO list (all keywords)"
541 (const :format "" alltodo)
542 (const :tag "" :format "" "")
543 ,org-agenda-custom-commands-local-options)
544 (list :tag "Search words"
545 (const :format "" search)
546 (string :tag "Match")
547 ,org-agenda-custom-commands-local-options)
548 (list :tag "Stuck projects"
549 (const :format "" stuck)
550 (const :tag "" :format "" "")
551 ,org-agenda-custom-commands-local-options)
552 (list :tag "Tags search"
553 (const :format "" tags)
554 (string :tag "Match")
555 ,org-agenda-custom-commands-local-options)
556 (list :tag "Tags search, TODO entries only"
557 (const :format "" tags-todo)
558 (string :tag "Match")
559 ,org-agenda-custom-commands-local-options)
560 (list :tag "TODO keyword search"
561 (const :format "" todo)
562 (string :tag "Match")
563 ,org-agenda-custom-commands-local-options)
564 (list :tag "Other, user-defined function"
565 (symbol :tag "function")
566 (string :tag "Match")
567 ,org-agenda-custom-commands-local-options)))
569 (repeat :tag "Settings for entire command set"
570 (list (variable :tag "Any variable")
571 (sexp :tag "Value")))
572 (option (repeat :tag "Export" (file :tag "Export to"))))
573 (cons :tag "Prefix key documentation"
574 (string :tag "Access Key(s)")
575 (string :tag "Description ")))))
577 (defcustom org-agenda-query-register ?o
578 "The register holding the current query string.
579 The purpose of this is that if you construct a query string interactively,
580 you can then use it to define a custom command."
581 :group 'org-agenda-custom-commands
582 :type 'character)
584 (defcustom org-stuck-projects
585 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
586 "How to identify stuck projects.
587 This is a list of four items:
588 1. A tags/todo/property matcher string that is used to identify a project.
589 See the manual for a description of tag and property searches.
590 The entire tree below a headline matched by this is considered one project.
591 2. A list of TODO keywords identifying non-stuck projects.
592 If the project subtree contains any headline with one of these todo
593 keywords, the project is considered to be not stuck. If you specify
594 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
595 3. A list of tags identifying non-stuck projects.
596 If the project subtree contains any headline with one of these tags,
597 the project is considered to be not stuck. If you specify \"*\" as
598 a tag, any tag will mark the project unstuck. Note that this is about
599 the explicit presence of a tag somewhere in the subtree, inherited
600 tags do not count here. If inherited tags make a project not stuck,
601 use \"-TAG\" in the tags part of the matcher under (1.) above.
602 4. An arbitrary regular expression matching non-stuck projects.
604 If the project turns out to be not stuck, search continues also in the
605 subtree to see if any of the subtasks have project status.
607 See also the variable `org-tags-match-list-sublevels' which applies
608 to projects matched by this search as well.
610 After defining this variable, you may use `\\[org-agenda-list-stuck-projects]'
611 \(bound to `C-c a #') to produce the list."
612 :group 'org-agenda-custom-commands
613 :type '(list
614 (string :tag "Tags/TODO match to identify a project")
615 (repeat :tag "Projects are *not* stuck if they have an entry with \
616 TODO keyword any of" (string))
617 (repeat :tag "Projects are *not* stuck if they have an entry with \
618 TAG being any of" (string))
619 (regexp :tag "Projects are *not* stuck if this regexp matches inside \
620 the subtree")))
622 (defgroup org-agenda-skip nil
623 "Options concerning skipping parts of agenda files."
624 :tag "Org Agenda Skip"
625 :group 'org-agenda)
627 (defcustom org-agenda-skip-function-global nil
628 "Function to be called at each match during agenda construction.
629 If this function returns nil, the current match should not be skipped.
630 If the function decided to skip an agenda match, is must return the
631 buffer position from which the search should be continued.
632 This may also be a Lisp form, which will be evaluated.
634 This variable will be applied to every agenda match, including
635 tags/property searches and TODO lists. So try to make the test function
636 do its checking as efficiently as possible. To implement a skipping
637 condition just for specific agenda commands, use the variable
638 `org-agenda-skip-function' which can be set in the options section
639 of custom agenda commands."
640 :group 'org-agenda-skip
641 :type 'sexp)
643 (defgroup org-agenda-daily/weekly nil
644 "Options concerning the daily/weekly agenda."
645 :tag "Org Agenda Daily/Weekly"
646 :group 'org-agenda)
647 (defgroup org-agenda-todo-list nil
648 "Options concerning the global todo list agenda view."
649 :tag "Org Agenda Todo List"
650 :group 'org-agenda)
651 (defgroup org-agenda-match-view nil
652 "Options concerning the general tags/property/todo match agenda view."
653 :tag "Org Agenda Match View"
654 :group 'org-agenda)
655 (defgroup org-agenda-search-view nil
656 "Options concerning the search agenda view."
657 :tag "Org Agenda Search View"
658 :group 'org-agenda)
660 (defvar org-agenda-archives-mode nil
661 "Non-nil means the agenda will include archived items.
662 If this is the symbol `trees', trees in the selected agenda scope
663 that are marked with the ARCHIVE tag will be included anyway. When this is
664 t, also all archive files associated with the current selection of agenda
665 files will be included.")
667 (defcustom org-agenda-restriction-lock-highlight-subtree t
668 "Non-nil means highlight the whole subtree when restriction is active.
669 Otherwise only highlight the headline. Highlighting the whole subtree is
670 useful to ensure no edits happen beyond the restricted region."
671 :group 'org-agenda
672 :type 'boolean)
674 (defcustom org-agenda-skip-comment-trees t
675 "Non-nil means skip trees that start with the COMMENT keyword.
676 When nil, these trees are also scanned by agenda commands."
677 :group 'org-agenda-skip
678 :type 'boolean)
680 (defcustom org-agenda-todo-list-sublevels t
681 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
682 When nil, the sublevels of a TODO entry are not checked, resulting in
683 potentially much shorter TODO lists."
684 :group 'org-agenda-skip
685 :group 'org-agenda-todo-list
686 :type 'boolean)
688 (defcustom org-agenda-todo-ignore-with-date nil
689 "Non-nil means don't show entries with a date in the global todo list.
690 You can use this if you prefer to mark mere appointments with a TODO keyword,
691 but don't want them to show up in the TODO list.
692 When this is set, it also covers deadlines and scheduled items, the settings
693 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
694 will be ignored.
695 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
696 :group 'org-agenda-skip
697 :group 'org-agenda-todo-list
698 :type 'boolean)
700 (defcustom org-agenda-todo-ignore-timestamp nil
701 "Non-nil means don't show entries with a timestamp.
702 This applies when creating the global todo list.
703 Valid values are:
705 past Don't show entries for today or in the past.
707 future Don't show entries with a timestamp in the future.
708 The idea behind this is that if it has a future
709 timestamp, you don't want to think about it until the
710 date.
712 all Don't show any entries with a timestamp in the global todo list.
713 The idea behind this is that by setting a timestamp, you
714 have already \"taken care\" of this item.
716 This variable can also have an integer as a value. If positive (N),
717 todos with a timestamp N or more days in the future will be ignored. If
718 negative (-N), todos with a timestamp N or more days in the past will be
719 ignored. If 0, todos with a timestamp either today or in the future will
720 be ignored. For example, a value of -1 will exclude todos with a
721 timestamp in the past (yesterday or earlier), while a value of 7 will
722 exclude todos with a timestamp a week or more in the future.
724 See also `org-agenda-todo-ignore-with-date'.
725 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
726 to make his option also apply to the tags-todo list."
727 :group 'org-agenda-skip
728 :group 'org-agenda-todo-list
729 :version "24.1"
730 :type '(choice
731 (const :tag "Ignore future timestamp todos" future)
732 (const :tag "Ignore past or present timestamp todos" past)
733 (const :tag "Ignore all timestamp todos" all)
734 (const :tag "Show timestamp todos" nil)
735 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
737 (defcustom org-agenda-todo-ignore-scheduled nil
738 "Non-nil means, ignore some scheduled TODO items when making TODO list.
739 This applies when creating the global todo list.
740 Valid values are:
742 past Don't show entries scheduled today or in the past.
744 future Don't show entries scheduled in the future.
745 The idea behind this is that by scheduling it, you don't want to
746 think about it until the scheduled date.
748 all Don't show any scheduled entries in the global todo list.
749 The idea behind this is that by scheduling it, you have already
750 \"taken care\" of this item.
752 t Same as `all', for backward compatibility.
754 This variable can also have an integer as a value. See
755 `org-agenda-todo-ignore-timestamp' for more details.
757 See also `org-agenda-todo-ignore-with-date'.
758 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
759 to make his option also apply to the tags-todo list."
760 :group 'org-agenda-skip
761 :group 'org-agenda-todo-list
762 :type '(choice
763 (const :tag "Ignore future-scheduled todos" future)
764 (const :tag "Ignore past- or present-scheduled todos" past)
765 (const :tag "Ignore all scheduled todos" all)
766 (const :tag "Ignore all scheduled todos (compatibility)" t)
767 (const :tag "Show scheduled todos" nil)
768 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
770 (defcustom org-agenda-todo-ignore-deadlines nil
771 "Non-nil means ignore some deadline TODO items when making TODO list.
773 There are different motivations for using different values, please think
774 carefully when configuring this variable.
776 This applies when creating the global TODO list.
778 Valid values are:
780 near Don't show near deadline entries. A deadline is near when it is
781 closer than `org-deadline-warning-days' days. The idea behind this
782 is that such items will appear in the agenda anyway.
784 far Don't show TODO entries where a deadline has been defined, but
785 is not going to happen anytime soon. This is useful if you want to use
786 the TODO list to figure out what to do now.
788 past Don't show entries with a deadline timestamp for today or in the past.
790 future Don't show entries with a deadline timestamp in the future, not even
791 when they become `near' ones. Use it with caution.
793 all Ignore all TODO entries that do have a deadline.
795 t Same as `near', for backward compatibility.
797 This variable can also have an integer as a value. See
798 `org-agenda-todo-ignore-timestamp' for more details.
800 See also `org-agenda-todo-ignore-with-date'.
801 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
802 to make his option also apply to the tags-todo list."
803 :group 'org-agenda-skip
804 :group 'org-agenda-todo-list
805 :type '(choice
806 (const :tag "Ignore near deadlines" near)
807 (const :tag "Ignore near deadlines (compatibility)" t)
808 (const :tag "Ignore far deadlines" far)
809 (const :tag "Ignore all TODOs with a deadlines" all)
810 (const :tag "Show all TODOs, even if they have a deadline" nil)
811 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
813 (defcustom org-agenda-todo-ignore-time-comparison-use-seconds nil
814 "Time unit to use when possibly ignoring an agenda item.
816 See the docstring of various `org-agenda-todo-ignore-*' options.
817 The default is to compare time stamps using days. An item is thus
818 considered to be in the future if it is at least one day after today.
819 Non-nil means to compare time stamps using seconds. An item is then
820 considered future if it has a time value later than current time."
821 :group 'org-agenda-skip
822 :group 'org-agenda-todo-list
823 :version "24.4"
824 :package-version '(Org . "8.0")
825 :type '(choice
826 (const :tag "Compare time with days" nil)
827 (const :tag "Compare time with seconds" t)))
829 (defcustom org-agenda-tags-todo-honor-ignore-options nil
830 "Non-nil means honor todo-list ignores options also in tags-todo search.
831 The variables
832 `org-agenda-todo-ignore-with-date',
833 `org-agenda-todo-ignore-timestamp',
834 `org-agenda-todo-ignore-scheduled',
835 `org-agenda-todo-ignore-deadlines'
836 make the global TODO list skip entries that have time stamps of certain
837 kinds. If this option is set, the same options will also apply for the
838 tags-todo search, which is the general tags/property matcher
839 restricted to unfinished TODO entries only."
840 :group 'org-agenda-skip
841 :group 'org-agenda-todo-list
842 :group 'org-agenda-match-view
843 :type 'boolean)
845 (defcustom org-agenda-skip-scheduled-if-done nil
846 "Non-nil means don't show scheduled items in agenda when they are done.
847 This is relevant for the daily/weekly agenda, not for the TODO list. It
848 applies only to the actual date of the scheduling. Warnings about an item
849 with a past scheduling dates are always turned off when the item is DONE."
850 :group 'org-agenda-skip
851 :group 'org-agenda-daily/weekly
852 :type 'boolean)
854 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
855 "Non-nil means skip scheduling line if same entry shows because of deadline.
857 In the agenda of today, an entry can show up multiple times
858 because it is both scheduled and has a nearby deadline, and maybe
859 a plain time stamp as well.
861 When this variable is nil, the entry will be shown several times.
863 When set to t, then only the deadline is shown and the fact that
864 the entry is scheduled today or was scheduled previously is not
865 shown.
867 When set to the symbol `not-today', skip scheduled previously,
868 but not scheduled today.
870 When set to the symbol `repeated-after-deadline', skip scheduled
871 items if they are repeated beyond the current deadline."
872 :group 'org-agenda-skip
873 :group 'org-agenda-daily/weekly
874 :type '(choice
875 (const :tag "Never" nil)
876 (const :tag "Always" t)
877 (const :tag "Not when scheduled today" not-today)
878 (const :tag "When repeated past deadline" repeated-after-deadline)))
880 (defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
881 "Non-nil means skip timestamp line if same entry shows because of deadline.
882 In the agenda of today, an entry can show up multiple times
883 because it has both a plain timestamp and has a nearby deadline.
884 When this variable is t, then only the deadline is shown and the
885 fact that the entry has a timestamp for or including today is not
886 shown. When this variable is nil, the entry will be shown
887 several times."
888 :group 'org-agenda-skip
889 :group 'org-agenda-daily/weekly
890 :version "24.1"
891 :type '(choice
892 (const :tag "Never" nil)
893 (const :tag "Always" t)))
895 (defcustom org-agenda-skip-deadline-if-done nil
896 "Non-nil means don't show deadlines when the corresponding item is done.
897 When nil, the deadline is still shown and should give you a happy feeling.
898 This is relevant for the daily/weekly agenda. It applies only to the
899 actual date of the deadline. Warnings about approaching and past-due
900 deadlines are always turned off when the item is DONE."
901 :group 'org-agenda-skip
902 :group 'org-agenda-daily/weekly
903 :type 'boolean)
905 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
906 "Non-nil means skip deadline prewarning when entry is also scheduled.
907 This will apply on all days where a prewarning for the deadline would
908 be shown, but not at the day when the entry is actually due. On that day,
909 the deadline will be shown anyway.
910 This variable may be set to nil, t, the symbol `pre-scheduled',
911 or a number which will then give the number of days before the actual
912 deadline when the prewarnings should resume. The symbol `pre-scheduled'
913 eliminates the deadline prewarning only prior to the scheduled date.
914 This can be used in a workflow where the first showing of the deadline will
915 trigger you to schedule it, and then you don't want to be reminded of it
916 because you will take care of it on the day when scheduled."
917 :group 'org-agenda-skip
918 :group 'org-agenda-daily/weekly
919 :version "24.1"
920 :type '(choice
921 (const :tag "Always show prewarning" nil)
922 (const :tag "Remove prewarning prior to scheduled date" pre-scheduled)
923 (const :tag "Remove prewarning if entry is scheduled" t)
924 (integer :tag "Restart prewarning N days before deadline")))
926 (defcustom org-agenda-skip-scheduled-delay-if-deadline nil
927 "Non-nil means skip scheduled delay when entry also has a deadline.
928 This variable may be set to nil, t, the symbol `post-deadline',
929 or a number which will then give the number of days after the actual
930 scheduled date when the delay should expire. The symbol `post-deadline'
931 eliminates the schedule delay when the date is posterior to the deadline."
932 :group 'org-agenda-skip
933 :group 'org-agenda-daily/weekly
934 :version "24.4"
935 :package-version '(Org . "8.0")
936 :type '(choice
937 (const :tag "Always honor delay" nil)
938 (const :tag "Ignore delay if posterior to the deadline" post-deadline)
939 (const :tag "Ignore delay if entry has a deadline" t)
940 (integer :tag "Honor delay up until N days after the scheduled date")))
942 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
943 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
944 When non-nil, after the search for timestamps has matched once in an
945 entry, the rest of the entry will not be searched."
946 :group 'org-agenda-skip
947 :type 'boolean)
949 (defcustom org-agenda-skip-timestamp-if-done nil
950 "Non-nil means don't select item by timestamp or -range if it is DONE."
951 :group 'org-agenda-skip
952 :group 'org-agenda-daily/weekly
953 :type 'boolean)
955 (defcustom org-agenda-dim-blocked-tasks t
956 "Non-nil means dim blocked tasks in the agenda display.
957 This causes some overhead during agenda construction, but if you
958 have turned on `org-enforce-todo-dependencies',
959 `org-enforce-todo-checkbox-dependencies', or any other blocking
960 mechanism, this will create useful feedback in the agenda.
962 Instead of t, this variable can also have the value `invisible'.
963 Then blocked tasks will be invisible and only become visible when
964 they become unblocked. An exemption to this behavior is when a task is
965 blocked because of unchecked checkboxes below it. Since checkboxes do
966 not show up in the agenda views, making this task invisible you remove any
967 trace from agenda views that there is something to do. Therefore, a task
968 that is blocked because of checkboxes will never be made invisible, it
969 will only be dimmed."
970 :group 'org-agenda-daily/weekly
971 :group 'org-agenda-todo-list
972 :version "24.3"
973 :type '(choice
974 (const :tag "Do not dim" nil)
975 (const :tag "Dim to a gray face" t)
976 (const :tag "Make invisible" invisible)))
978 (defgroup org-agenda-startup nil
979 "Options concerning initial settings in the Agenda in Org Mode."
980 :tag "Org Agenda Startup"
981 :group 'org-agenda)
983 (defcustom org-agenda-menu-show-matcher t
984 "Non-nil means show the match string in the agenda dispatcher menu.
985 When nil, the matcher string is not shown, but is put into the help-echo
986 property so than moving the mouse over the command shows it.
987 Setting it to nil is good if matcher strings are very long and/or if
988 you want to use two-columns display (see `org-agenda-menu-two-columns')."
989 :group 'org-agenda
990 :version "24.1"
991 :type 'boolean)
993 (defcustom org-agenda-menu-two-columns nil
994 "Non-nil means, use two columns to show custom commands in the dispatcher.
995 If you use this, you probably want to set `org-agenda-menu-show-matcher'
996 to nil."
997 :group 'org-agenda
998 :version "24.1"
999 :type 'boolean)
1001 (defcustom org-agenda-finalize-hook nil
1002 "Hook run just before displaying an agenda buffer.
1003 The buffer is still writable when the hook is called.
1005 You can modify some of the buffer substrings but you should be
1006 extra careful not to modify the text properties of the agenda
1007 headlines as the agenda display heavily relies on them."
1008 :group 'org-agenda-startup
1009 :type 'hook)
1011 (defcustom org-agenda-mouse-1-follows-link nil
1012 "Non-nil means mouse-1 on a link will follow the link in the agenda.
1013 A longer mouse click will still set point. Needs to be set
1014 before org.el is loaded."
1015 :group 'org-agenda-startup
1016 :type 'boolean)
1018 (defcustom org-agenda-start-with-follow-mode nil
1019 "The initial value of follow mode in a newly created agenda window."
1020 :group 'org-agenda-startup
1021 :type 'boolean)
1023 (defcustom org-agenda-follow-indirect nil
1024 "Non-nil means `org-agenda-follow-mode' displays only the
1025 current item's tree, in an indirect buffer."
1026 :group 'org-agenda
1027 :version "24.1"
1028 :type 'boolean)
1030 (defcustom org-agenda-show-outline-path t
1031 "Non-nil means show outline path in echo area after line motion."
1032 :group 'org-agenda-startup
1033 :type 'boolean)
1035 (defcustom org-agenda-start-with-entry-text-mode nil
1036 "The initial value of entry-text-mode in a newly created agenda window."
1037 :group 'org-agenda-startup
1038 :type 'boolean)
1040 (defcustom org-agenda-entry-text-maxlines 5
1041 "Number of text lines to be added when `E' is pressed in the agenda.
1043 Note that this variable only used during agenda display. To add entry text
1044 when exporting the agenda, configure the variable
1045 `org-agenda-add-entry-text-maxlines'."
1046 :group 'org-agenda
1047 :type 'integer)
1049 (defcustom org-agenda-entry-text-exclude-regexps nil
1050 "List of regular expressions to clean up entry text.
1051 The complete matches of all regular expressions in this list will be
1052 removed from entry text before it is shown in the agenda."
1053 :group 'org-agenda
1054 :type '(repeat (regexp)))
1056 (defcustom org-agenda-entry-text-leaders " > "
1057 "Text prepended to the entry text in agenda buffers."
1058 :version "24.4"
1059 :package-version '(Org . "8.0")
1060 :group 'org-agenda
1061 :type 'string)
1063 (defvar org-agenda-entry-text-cleanup-hook nil
1064 "Hook that is run after basic cleanup of entry text to be shown in agenda.
1065 This cleanup is done in a temporary buffer, so the function may inspect and
1066 change the entire buffer.
1067 Some default stuff like drawers and scheduling/deadline dates will already
1068 have been removed when this is called, as will any matches for regular
1069 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
1071 (defvar org-agenda-include-inactive-timestamps nil
1072 "Non-nil means include inactive time stamps in agenda and timeline.
1073 Dynamically scoped.")
1075 (defgroup org-agenda-windows nil
1076 "Options concerning the windows used by the Agenda in Org Mode."
1077 :tag "Org Agenda Windows"
1078 :group 'org-agenda)
1080 (defcustom org-agenda-window-setup 'reorganize-frame
1081 "How the agenda buffer should be displayed.
1082 Possible values for this option are:
1084 current-window Show agenda in the current window, keeping all other windows.
1085 other-window Use `switch-to-buffer-other-window' to display agenda.
1086 only-window Show agenda, deleting all other windows.
1087 reorganize-frame Show only two windows on the current frame, the current
1088 window and the agenda.
1089 other-frame Use `switch-to-buffer-other-frame' to display agenda.
1090 Also, when exiting the agenda, kill that frame.
1091 See also the variable `org-agenda-restore-windows-after-quit'."
1092 :group 'org-agenda-windows
1093 :type '(choice
1094 (const current-window)
1095 (const other-frame)
1096 (const other-window)
1097 (const only-window)
1098 (const reorganize-frame)))
1100 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
1101 "The min and max height of the agenda window as a fraction of frame height.
1102 The value of the variable is a cons cell with two numbers between 0 and 1.
1103 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
1104 :group 'org-agenda-windows
1105 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
1107 (defcustom org-agenda-restore-windows-after-quit nil
1108 "Non-nil means restore window configuration upon exiting agenda.
1109 Before the window configuration is changed for displaying the agenda,
1110 the current status is recorded. When the agenda is exited with
1111 `q' or `x' and this option is set, the old state is restored. If
1112 `org-agenda-window-setup' is `other-frame', the value of this
1113 option will be ignored."
1114 :group 'org-agenda-windows
1115 :type 'boolean)
1117 (defcustom org-agenda-span 'week
1118 "Number of days to include in overview display.
1119 Can be day, week, month, year, or any number of days.
1120 Custom commands can set this variable in the options section."
1121 :group 'org-agenda-daily/weekly
1122 :type '(choice (const :tag "Day" day)
1123 (const :tag "Week" week)
1124 (const :tag "Fortnight" fortnight)
1125 (const :tag "Month" month)
1126 (const :tag "Year" year)
1127 (integer :tag "Custom")))
1129 (defcustom org-agenda-start-on-weekday 1
1130 "Non-nil means start the overview always on the specified weekday.
1131 0 denotes Sunday, 1 denotes Monday, etc.
1132 When nil, always start on the current day.
1133 Custom commands can set this variable in the options section."
1134 :group 'org-agenda-daily/weekly
1135 :type '(choice (const :tag "Today" nil)
1136 (integer :tag "Weekday No.")))
1138 (defcustom org-agenda-show-all-dates t
1139 "Non-nil means `org-agenda' shows every day in the selected range.
1140 When nil, only the days which actually have entries are shown."
1141 :group 'org-agenda-daily/weekly
1142 :type 'boolean)
1144 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1145 "Format string for displaying dates in the agenda.
1146 Used by the daily/weekly agenda and by the timeline. This should be
1147 a format string understood by `format-time-string', or a function returning
1148 the formatted date as a string. The function must take a single argument,
1149 a calendar-style date list like (month day year)."
1150 :group 'org-agenda-daily/weekly
1151 :type '(choice
1152 (string :tag "Format string")
1153 (function :tag "Function")))
1155 (defun org-agenda-format-date-aligned (date)
1156 "Format a DATE string for display in the daily/weekly agenda, or timeline.
1157 This function makes sure that dates are aligned for easy reading."
1158 (require 'cal-iso)
1159 (let* ((dayname (calendar-day-name date))
1160 (day (cadr date))
1161 (day-of-week (calendar-day-of-week date))
1162 (month (car date))
1163 (monthname (calendar-month-name month))
1164 (year (nth 2 date))
1165 (iso-week (org-days-to-iso-week
1166 (calendar-absolute-from-gregorian date)))
1167 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1168 (1- year))
1169 ((and (= month 12) (<= iso-week 1))
1170 (1+ year))
1171 (t year)))
1172 (weekstring (if (= day-of-week 1)
1173 (format " W%02d" iso-week)
1174 "")))
1175 (format "%-10s %2d %s %4d%s"
1176 dayname day monthname year weekstring)))
1178 (defcustom org-agenda-time-leading-zero nil
1179 "Non-nil means use leading zero for military times in agenda.
1180 For example, 9:30am would become 09:30 rather than 9:30."
1181 :group 'org-agenda-daily/weekly
1182 :version "24.1"
1183 :type 'boolean)
1185 (defcustom org-agenda-timegrid-use-ampm nil
1186 "When set, show AM/PM style timestamps on the timegrid."
1187 :group 'org-agenda
1188 :version "24.1"
1189 :type 'boolean)
1191 (defun org-agenda-time-of-day-to-ampm (time)
1192 "Convert TIME of a string like \"13:45\" to an AM/PM style time string."
1193 (let* ((hour-number (string-to-number (substring time 0 -3)))
1194 (minute (substring time -2))
1195 (ampm "am"))
1196 (cond
1197 ((equal hour-number 12)
1198 (setq ampm "pm"))
1199 ((> hour-number 12)
1200 (setq ampm "pm")
1201 (setq hour-number (- hour-number 12))))
1202 (concat
1203 (if org-agenda-time-leading-zero
1204 (format "%02d" hour-number)
1205 (format "%02s" (number-to-string hour-number)))
1206 ":" minute ampm)))
1208 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1209 "Conditionally convert TIME to AM/PM format based on `org-agenda-timegrid-use-ampm'."
1210 (if org-agenda-timegrid-use-ampm
1211 (org-agenda-time-of-day-to-ampm time)
1212 time))
1214 (defcustom org-agenda-weekend-days '(6 0)
1215 "Which days are weekend?
1216 These days get the special face `org-agenda-date-weekend' in the agenda
1217 and timeline buffers."
1218 :group 'org-agenda-daily/weekly
1219 :type '(set :greedy t
1220 (const :tag "Monday" 1)
1221 (const :tag "Tuesday" 2)
1222 (const :tag "Wednesday" 3)
1223 (const :tag "Thursday" 4)
1224 (const :tag "Friday" 5)
1225 (const :tag "Saturday" 6)
1226 (const :tag "Sunday" 0)))
1228 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1229 "Non-nil means jump to today when moving a past date forward in time.
1230 When using S-right in the agenda to move a a date forward, and the date
1231 stamp currently points to the past, the first key press will move it
1232 to today. WHen nil, just move one day forward even if the date stays
1233 in the past."
1234 :group 'org-agenda-daily/weekly
1235 :version "24.1"
1236 :type 'boolean)
1238 (defcustom org-agenda-include-diary nil
1239 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1240 Custom commands can set this variable in the options section."
1241 :group 'org-agenda-daily/weekly
1242 :type 'boolean)
1244 (defcustom org-agenda-include-deadlines t
1245 "If non-nil, include entries within their deadline warning period.
1246 Custom commands can set this variable in the options section."
1247 :group 'org-agenda-daily/weekly
1248 :version "24.1"
1249 :type 'boolean)
1251 (defcustom org-agenda-show-future-repeats t
1252 "Non-nil shows repeated entries in the future part of the agenda.
1253 When set to the symbol `next' only the first future repeat is shown."
1254 :group 'org-agenda-daily/weekly
1255 :type '(choice
1256 (const :tag "Show all repeated entries" t)
1257 (const :tag "Show next repeated entry" next)
1258 (const :tag "Do not show repeated entries" nil))
1259 :version "26.1"
1260 :package-version '(Org . "9.1")
1261 :safe #'symbolp)
1263 (defcustom org-agenda-prefer-last-repeat nil
1264 "Non-nil sets date for repeated entries to their last repeat.
1266 When nil, display SCHEDULED and DEADLINE dates at their base
1267 date, and in today's agenda, as a reminder. Display plain
1268 time-stamps, on the other hand, at every repeat date in the past
1269 in addition to the base date.
1271 When non-nil, show a repeated entry at its latest repeat date,
1272 possibly being today even if it wasn't marked as done. This
1273 setting is useful if you do not always mark repeated entries as
1274 done and, yet, consider that reaching repeat date starts the task
1275 anew.
1277 When set to a list of strings, prefer last repeats only for
1278 entries with these TODO keywords."
1279 :group 'org-agenda-daily/weekly
1280 :type '(choice
1281 (const :tag "Prefer last repeat" t)
1282 (const :tag "Prefer base date" nil)
1283 (repeat :tag "Prefer last repeat for entries with these TODO keywords"
1284 (string :tag "TODO keyword")))
1285 :version "26.1"
1286 :package-version '(Org . "9.1")
1287 :safe (lambda (x) (or (booleanp x) (consp x))))
1289 (defcustom org-scheduled-past-days 10000
1290 "Number of days to continue listing scheduled items not marked DONE.
1291 When an item is scheduled on a date, it shows up in the agenda on
1292 this day and will be listed until it is marked done or for the
1293 number of days given here."
1294 :group 'org-agenda-daily/weekly
1295 :type 'integer
1296 :safe 'integerp)
1298 (defcustom org-deadline-past-days 10000
1299 "Number of days to warn about missed deadlines.
1300 When an item has deadline on a date, it shows up in the agenda on
1301 this day and will appear as a reminder until it is marked DONE or
1302 for the number of days given here."
1303 :group 'org-agenda-daily/weekly
1304 :type 'integer
1305 :version "26.1"
1306 :package-version '(Org . "9.1")
1307 :safe 'integerp)
1309 (defcustom org-agenda-log-mode-items '(closed clock)
1310 "List of items that should be shown in agenda log mode.
1311 \\<org-agenda-mode-map>\
1312 This list may contain the following symbols:
1314 closed Show entries that have been closed on that day.
1315 clock Show entries that have received clocked time on that day.
1316 state Show all logged state changes.
1317 Note that instead of changing this variable, you can also press \
1318 `\\[universal-argument] \\[org-agenda-log-mode]' in
1319 the agenda to display all available LOG items temporarily."
1320 :group 'org-agenda-daily/weekly
1321 :type '(set :greedy t (const closed) (const clock) (const state)))
1323 (defcustom org-agenda-clock-consistency-checks
1324 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1325 :gap-ok-around ("4:00")
1326 :default-face ((:background "DarkRed") (:foreground "white"))
1327 :overlap-face nil :gap-face nil :no-end-time-face nil
1328 :long-face nil :short-face nil)
1329 "This is a property list, with the following keys:
1331 :max-duration Mark clocking chunks that are longer than this time.
1332 This is a time string like \"HH:MM\", or the number
1333 of minutes as an integer.
1335 :min-duration Mark clocking chunks that are shorter that this.
1336 This is a time string like \"HH:MM\", or the number
1337 of minutes as an integer.
1339 :max-gap Mark gaps between clocking chunks that are longer than
1340 this duration. A number of minutes, or a string
1341 like \"HH:MM\".
1343 :gap-ok-around List of times during the day which are usually not working
1344 times. When a gap is detected, but the gap contains any
1345 of these times, the gap is *not* reported. For example,
1346 if this is (\"4:00\" \"13:00\") then gaps that contain
1347 4:00 in the morning (i.e. the night) and 13:00
1348 (i.e. a typical lunch time) do not cause a warning.
1349 You should have at least one time during the night in this
1350 list, or otherwise the first task each morning will trigger
1351 a warning because it follows a long gap.
1353 Furthermore, the following properties can be used to define faces for
1354 issue display.
1356 :default-face the default face, if the specific face is undefined
1357 :overlap-face face for overlapping clocks
1358 :gap-face face for gaps between clocks
1359 :no-end-time-face face for incomplete clocks
1360 :long-face face for clock intervals that are too long
1361 :short-face face for clock intervals that are too short"
1362 :group 'org-agenda-daily/weekly
1363 :group 'org-clock
1364 :version "24.1"
1365 :type 'plist)
1367 (defcustom org-agenda-log-mode-add-notes t
1368 "Non-nil means add first line of notes to log entries in agenda views.
1369 If a log item like a state change or a clock entry is associated with
1370 notes, the first line of these notes will be added to the entry in the
1371 agenda display."
1372 :group 'org-agenda-daily/weekly
1373 :type 'boolean)
1375 (defcustom org-agenda-start-with-log-mode nil
1376 "The initial value of log-mode in a newly created agenda window.
1377 See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
1378 explanations on the possible values."
1379 :group 'org-agenda-startup
1380 :group 'org-agenda-daily/weekly
1381 :type '(choice (const :tag "Don't show log items" nil)
1382 (const :tag "Show only log items" only)
1383 (const :tag "Show all possible log items" clockcheck)
1384 (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
1385 (choice (const :tag "Show closed log items" closed)
1386 (const :tag "Show clocked log items" clock)
1387 (const :tag "Show all logged state changes" state)))))
1389 (defcustom org-agenda-start-with-clockreport-mode nil
1390 "The initial value of clockreport-mode in a newly created agenda window."
1391 :group 'org-agenda-startup
1392 :group 'org-agenda-daily/weekly
1393 :type 'boolean)
1395 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1396 "Property list with parameters for the clocktable in clockreport mode.
1397 This is the display mode that shows a clock table in the daily/weekly
1398 agenda, the properties for this dynamic block can be set here.
1399 The usual clocktable parameters are allowed here, but you cannot set
1400 the properties :name, :tstart, :tend, :block, and :scope - these will
1401 be overwritten to make sure the content accurately reflects the
1402 current display in the agenda."
1403 :group 'org-agenda-daily/weekly
1404 :type 'plist)
1406 (defcustom org-agenda-search-view-always-boolean nil
1407 "Non-nil means the search string is interpreted as individual parts.
1409 The search string for search view can either be interpreted as a phrase,
1410 or as a list of snippets that define a boolean search for a number of
1411 strings.
1413 When this is non-nil, the string will be split on whitespace, and each
1414 snippet will be searched individually, and all must match in order to
1415 select an entry. A snippet is then a single string of non-white
1416 characters, or a string in double quotes, or a regexp in {} braces.
1417 If a snippet is preceded by \"-\", the snippet must *not* match.
1418 \"+\" is syntactic sugar for positive selection. Each snippet may
1419 be found as a full word or a partial word, but see the variable
1420 `org-agenda-search-view-force-full-words'.
1422 When this is nil, search will look for the entire search phrase as one,
1423 with each space character matching any amount of whitespace, including
1424 line breaks.
1426 Even when this is nil, you can still switch to Boolean search dynamically
1427 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1428 is a regexp marked with braces like \"{abc}\", this will also switch to
1429 boolean search."
1430 :group 'org-agenda-search-view
1431 :version "24.1"
1432 :type 'boolean)
1434 (defvaralias 'org-agenda-search-view-search-words-only
1435 'org-agenda-search-view-always-boolean)
1437 (defcustom org-agenda-search-view-force-full-words nil
1438 "Non-nil means, search words must be matches as complete words.
1439 When nil, they may also match part of a word."
1440 :group 'org-agenda-search-view
1441 :version "24.1"
1442 :type 'boolean)
1444 (defcustom org-agenda-search-view-max-outline-level 0
1445 "Maximum outline level to display in search view.
1446 E.g. when this is set to 1, the search view will only
1447 show headlines of level 1. When set to 0, the default
1448 value, don't limit agenda view by outline level."
1449 :group 'org-agenda-search-view
1450 :version "24.4"
1451 :package-version '(Org . "8.3")
1452 :type 'integer)
1454 (defgroup org-agenda-time-grid nil
1455 "Options concerning the time grid in the Org Agenda."
1456 :tag "Org Agenda Time Grid"
1457 :group 'org-agenda)
1459 (defcustom org-agenda-search-headline-for-time t
1460 "Non-nil means search headline for a time-of-day.
1461 If the headline contains a time-of-day in one format or another, it will
1462 be used to sort the entry into the time sequence of items for a day.
1463 Some people have time stamps in the headline that refer to the creation
1464 time or so, and then this produces an unwanted side effect. If this is
1465 the case for your, use this variable to turn off searching the headline
1466 for a time."
1467 :group 'org-agenda-time-grid
1468 :type 'boolean)
1470 (defcustom org-agenda-use-time-grid t
1471 "Non-nil means show a time grid in the agenda schedule.
1472 A time grid is a set of lines for specific times (like every two hours between
1473 8:00 and 20:00). The items scheduled for a day at specific times are
1474 sorted in between these lines.
1475 For details about when the grid will be shown, and what it will look like, see
1476 the variable `org-agenda-time-grid'."
1477 :group 'org-agenda-time-grid
1478 :type 'boolean)
1480 (defcustom org-agenda-time-grid
1481 '((daily today require-timed)
1482 "----------------"
1483 (800 1000 1200 1400 1600 1800 2000))
1485 "The settings for time grid for agenda display.
1486 This is a list of three items. The first item is again a list. It contains
1487 symbols specifying conditions when the grid should be displayed:
1489 daily if the agenda shows a single day
1490 weekly if the agenda shows an entire week
1491 today show grid on current date, independent of daily/weekly display
1492 require-timed show grid only if at least one item has a time specification
1493 remove-match skip grid times already present in an entry
1495 The second item is a string which will be placed behind the grid time.
1497 The third item is a list of integers, indicating the times that should have
1498 a grid line."
1499 :group 'org-agenda-time-grid
1500 :type
1501 '(list
1502 (set :greedy t :tag "Grid Display Options"
1503 (const :tag "Show grid in single day agenda display" daily)
1504 (const :tag "Show grid in weekly agenda display" weekly)
1505 (const :tag "Always show grid for today" today)
1506 (const :tag "Show grid only if any timed entries are present"
1507 require-timed)
1508 (const :tag "Skip grid times already present in an entry"
1509 remove-match))
1510 (string :tag "Grid String")
1511 (repeat :tag "Grid Times" (integer :tag "Time"))))
1513 (defcustom org-agenda-show-current-time-in-grid t
1514 "Non-nil means show the current time in the time grid."
1515 :group 'org-agenda-time-grid
1516 :version "24.1"
1517 :type 'boolean)
1519 (defcustom org-agenda-current-time-string
1520 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1521 "The string for the current time marker in the agenda."
1522 :group 'org-agenda-time-grid
1523 :version "24.1"
1524 :type 'string)
1526 (defgroup org-agenda-sorting nil
1527 "Options concerning sorting in the Org Agenda."
1528 :tag "Org Agenda Sorting"
1529 :group 'org-agenda)
1531 (defcustom org-agenda-sorting-strategy
1532 '((agenda habit-down time-up priority-down category-keep)
1533 (todo priority-down category-keep)
1534 (tags priority-down category-keep)
1535 (search category-keep))
1536 "Sorting structure for the agenda items of a single day.
1537 This is a list of symbols which will be used in sequence to determine
1538 if an entry should be listed before another entry. The following
1539 symbols are recognized:
1541 time-up Put entries with time-of-day indications first, early first
1542 time-down Put entries with time-of-day indications first, late first
1543 timestamp-up Sort by any timestamp, early first
1544 timestamp-down Sort by any timestamp, late first
1545 scheduled-up Sort by scheduled timestamp, early first
1546 scheduled-down Sort by scheduled timestamp, late first
1547 deadline-up Sort by deadline timestamp, early first
1548 deadline-down Sort by deadline timestamp, late first
1549 ts-up Sort by active timestamp, early first
1550 ts-down Sort by active timestamp, late first
1551 tsia-up Sort by inactive timestamp, early first
1552 tsia-down Sort by inactive timestamp, late first
1553 category-keep Keep the default order of categories, corresponding to the
1554 sequence in `org-agenda-files'.
1555 category-up Sort alphabetically by category, A-Z.
1556 category-down Sort alphabetically by category, Z-A.
1557 tag-up Sort alphabetically by last tag, A-Z.
1558 tag-down Sort alphabetically by last tag, Z-A.
1559 priority-up Sort numerically by priority, high priority last.
1560 priority-down Sort numerically by priority, high priority first.
1561 todo-state-up Sort by todo state, tasks that are done last.
1562 todo-state-down Sort by todo state, tasks that are done first.
1563 effort-up Sort numerically by estimated effort, high effort last.
1564 effort-down Sort numerically by estimated effort, high effort first.
1565 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1566 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1567 habit-up Put entries that are habits first
1568 habit-down Put entries that are habits last
1569 alpha-up Sort headlines alphabetically
1570 alpha-down Sort headlines alphabetically, reversed
1572 The different possibilities will be tried in sequence, and testing stops
1573 if one comparison returns a \"not-equal\". For example, the default
1574 '(time-up category-keep priority-down)
1575 means: Pull out all entries having a specified time of day and sort them,
1576 in order to make a time schedule for the current day the first thing in the
1577 agenda listing for the day. Of the entries without a time indication, keep
1578 the grouped in categories, don't sort the categories, but keep them in
1579 the sequence given in `org-agenda-files'. Within each category sort by
1580 priority.
1582 Leaving out `category-keep' would mean that items will be sorted across
1583 categories by priority.
1585 Instead of a single list, this can also be a set of list for specific
1586 contents, with a context symbol in the car of the list, any of
1587 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1589 Custom commands can bind this variable in the options section."
1590 :group 'org-agenda-sorting
1591 :type `(choice
1592 (repeat :tag "General" ,org-sorting-choice)
1593 (list :tag "Individually"
1594 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1595 (repeat ,org-sorting-choice))
1596 (cons (const :tag "Strategy for TODO lists" todo)
1597 (repeat ,org-sorting-choice))
1598 (cons (const :tag "Strategy for Tags matches" tags)
1599 (repeat ,org-sorting-choice))
1600 (cons (const :tag "Strategy for search matches" search)
1601 (repeat ,org-sorting-choice)))))
1603 (defcustom org-agenda-cmp-user-defined nil
1604 "A function to define the comparison `user-defined'.
1605 This function must receive two arguments, agenda entry a and b.
1606 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1607 the user comparison, return nil.
1608 When this is defined, you can make `user-defined-up' and `user-defined-down'
1609 part of an agenda sorting strategy."
1610 :group 'org-agenda-sorting
1611 :type 'symbol)
1613 (defcustom org-sort-agenda-notime-is-late t
1614 "Non-nil means items without time are considered late.
1615 This is only relevant for sorting. When t, items which have no explicit
1616 time like 15:30 will be considered as 99:01, i.e. later than any items which
1617 do have a time. When nil, the default time is before 0:00. You can use this
1618 option to decide if the schedule for today should come before or after timeless
1619 agenda entries."
1620 :group 'org-agenda-sorting
1621 :type 'boolean)
1623 (defcustom org-sort-agenda-noeffort-is-high t
1624 "Non-nil means items without effort estimate are sorted as high effort.
1625 This also applies when filtering an agenda view with respect to the
1626 < or > effort operator. Then, tasks with no effort defined will be treated
1627 as tasks with high effort.
1628 When nil, such items are sorted as 0 minutes effort."
1629 :group 'org-agenda-sorting
1630 :type 'boolean)
1632 (defgroup org-agenda-line-format nil
1633 "Options concerning the entry prefix in the Org agenda display."
1634 :tag "Org Agenda Line Format"
1635 :group 'org-agenda)
1637 (defcustom org-agenda-prefix-format
1638 '((agenda . " %i %-12:c%?-12t% s")
1639 (timeline . " % s")
1640 (todo . " %i %-12:c")
1641 (tags . " %i %-12:c")
1642 (search . " %i %-12:c"))
1643 "Format specifications for the prefix of items in the agenda views.
1644 An alist with five entries, each for the different agenda types. The
1645 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1646 The values are format strings.
1648 This format works similar to a printf format, with the following meaning:
1650 %c the category of the item, \"Diary\" for entries from the diary,
1651 or as given by the CATEGORY keyword or derived from the file name
1652 %e the effort required by the item
1653 %l the level of the item (insert X space(s) if item is of level X)
1654 %i the icon category of the item, see `org-agenda-category-icon-alist'
1655 %T the last tag of the item (ignore inherited tags, which come first)
1656 %t the HH:MM time-of-day specification if one applies to the entry
1657 %s Scheduling/Deadline information, a short string
1658 %b show breadcrumbs, i.e., the names of the higher levels
1659 %(expression) Eval EXPRESSION and replace the control string
1660 by the result
1662 All specifiers work basically like the standard `%s' of printf, but may
1663 contain two additional characters: a question mark just after the `%'
1664 and a whitespace/punctuation character just before the final letter.
1666 If the first character after `%' is a question mark, the entire field
1667 will only be included if the corresponding value applies to the current
1668 entry. This is useful for fields which should have fixed width when
1669 present, but zero width when absent. For example, \"%?-12t\" will
1670 result in a 12 character time field if a time of the day is specified,
1671 but will completely disappear in entries which do not contain a time.
1673 If there is punctuation or whitespace character just before the
1674 final format letter, this character will be appended to the field
1675 value if the value is not empty. For example, the format
1676 \"%-12:c\" leads to \"Diary: \" if the category is \"Diary\". If
1677 the category is empty, no additional colon is inserted.
1679 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1680 which means:
1682 - Indent the line with two space characters
1683 - Give the category a 12 chars wide field, padded with whitespace on
1684 the right (because of `-'). Append a colon if there is a category
1685 (because of `:').
1686 - If there is a time-of-day, put it into a 12 chars wide field. If no
1687 time, don't put in an empty field, just skip it (because of '?').
1688 - Finally, put the scheduling information.
1690 See also the variables `org-agenda-remove-times-when-in-prefix' and
1691 `org-agenda-remove-tags'.
1693 Custom commands can set this variable in the options section."
1694 :type '(choice
1695 (string :tag "General format")
1696 (list :greedy t :tag "View dependent"
1697 (cons (const agenda) (string :tag "Format"))
1698 (cons (const timeline) (string :tag "Format"))
1699 (cons (const todo) (string :tag "Format"))
1700 (cons (const tags) (string :tag "Format"))
1701 (cons (const search) (string :tag "Format"))))
1702 :group 'org-agenda-line-format)
1704 (defvar org-prefix-format-compiled nil
1705 "The compiled prefix format and associated variables.
1706 This is a list where first element is a list of variable bindings, and second
1707 element is the compiled format expression. See the variable
1708 `org-agenda-prefix-format'.")
1710 (defcustom org-agenda-todo-keyword-format "%-1s"
1711 "Format for the TODO keyword in agenda lines.
1712 Set this to something like \"%-12s\" if you want all TODO keywords
1713 to occupy a fixed space in the agenda display."
1714 :group 'org-agenda-line-format
1715 :type 'string)
1717 (defcustom org-agenda-diary-sexp-prefix nil
1718 "A regexp that matches part of a diary sexp entry
1719 which should be treated as scheduling/deadline information in
1720 `org-agenda'.
1722 For example, you can use this to extract the `diary-remind-message' from
1723 `diary-remind' entries."
1724 :group 'org-agenda-line-format
1725 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1727 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1728 "Text preceding timerange entries in the agenda view.
1729 This is a list with two strings. The first applies when the range
1730 is entirely on one day. The second applies if the range spans several days.
1731 The strings may have two \"%d\" format specifiers which will be filled
1732 with the sequence number of the days, and the total number of days in the
1733 range, respectively."
1734 :group 'org-agenda-line-format
1735 :type '(list
1736 (string :tag "Deadline today ")
1737 (choice :tag "Deadline relative"
1738 (string :tag "Format string")
1739 (function))))
1741 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1742 "Text preceding scheduled items in the agenda view.
1743 This is a list with two strings. The first applies when the item is
1744 scheduled on the current day. The second applies when it has been scheduled
1745 previously, it may contain a %d indicating that this is the nth time that
1746 this item is scheduled, due to automatic rescheduling of unfinished items
1747 for the following day. So this number is one larger than the number of days
1748 that passed since this item was scheduled first."
1749 :group 'org-agenda-line-format
1750 :version "24.4"
1751 :package-version '(Org . "8.0")
1752 :type '(list
1753 (string :tag "Scheduled today ")
1754 (string :tag "Scheduled previously")))
1756 (defcustom org-agenda-inactive-leader "["
1757 "Text preceding item pulled into the agenda by inactive time stamps.
1758 These entries are added to the agenda when pressing \"[\"."
1759 :group 'org-agenda-line-format
1760 :version "24.1"
1761 :type 'string)
1763 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: " "%2d d. ago: ")
1764 "Text preceding deadline items in the agenda view.
1765 This is a list with three strings. The first applies when the item has its
1766 deadline on the current day. The second applies when the deadline is in the
1767 future, the third one when it is in the past. The strings may contain %d
1768 to capture the number of days."
1769 :group 'org-agenda-line-format
1770 :version "24.4"
1771 :package-version '(Org . "8.0")
1772 :type '(list
1773 (string :tag "Deadline today ")
1774 (string :tag "Deadline in the future ")
1775 (string :tag "Deadline in the past ")))
1777 (defcustom org-agenda-remove-times-when-in-prefix t
1778 "Non-nil means remove duplicate time specifications in agenda items.
1779 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1780 time-of-day specification in a headline or diary entry is extracted and
1781 placed into the prefix. If this option is non-nil, the original specification
1782 \(a timestamp or -range, or just a plain time(range) specification like
1783 11:30-4pm) will be removed for agenda display. This makes the agenda less
1784 cluttered.
1785 The option can be t or nil. It may also be the symbol `beg', indicating
1786 that the time should only be removed when it is located at the beginning of
1787 the headline/diary entry."
1788 :group 'org-agenda-line-format
1789 :type '(choice
1790 (const :tag "Always" t)
1791 (const :tag "Never" nil)
1792 (const :tag "When at beginning of entry" beg)))
1794 (defcustom org-agenda-remove-timeranges-from-blocks nil
1795 "Non-nil means remove time ranges specifications in agenda
1796 items that span on several days."
1797 :group 'org-agenda-line-format
1798 :version "24.1"
1799 :type 'boolean)
1801 (defcustom org-agenda-default-appointment-duration nil
1802 "Default duration for appointments that only have a starting time.
1803 When nil, no duration is specified in such cases.
1804 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1805 :group 'org-agenda-line-format
1806 :type '(choice
1807 (integer :tag "Minutes")
1808 (const :tag "No default duration")))
1810 (defcustom org-agenda-show-inherited-tags t
1811 "Non-nil means show inherited tags in each agenda line.
1813 When this option is set to `always', it take precedences over
1814 `org-agenda-use-tag-inheritance' and inherited tags are shown
1815 in every agenda.
1817 When this option is set to t (the default), inherited tags are
1818 shown when they are available, i.e. when the value of
1819 `org-agenda-use-tag-inheritance' enables tag inheritance for the
1820 given agenda type.
1822 This can be set to a list of agenda types in which the agenda
1823 must display the inherited tags. Available types are `todo',
1824 `agenda', `search' and `timeline'.
1826 When set to nil, never show inherited tags in agenda lines."
1827 :group 'org-agenda-line-format
1828 :group 'org-agenda
1829 :version "24.3"
1830 :type '(choice
1831 (const :tag "Show inherited tags when available" t)
1832 (const :tag "Always show inherited tags" always)
1833 (repeat :tag "Show inherited tags only in selected agenda types"
1834 (symbol :tag "Agenda type"))))
1836 (defcustom org-agenda-use-tag-inheritance '(todo search timeline agenda)
1837 "List of agenda view types where to use tag inheritance.
1839 In tags/tags-todo/tags-tree agenda views, tag inheritance is
1840 controlled by `org-use-tag-inheritance'. In other agenda types,
1841 `org-use-tag-inheritance' is not used for the selection of the
1842 agenda entries. Still, you may want the agenda to be aware of
1843 the inherited tags anyway, e.g. for later tag filtering.
1845 Allowed value are `todo', `search', `timeline' and `agenda'.
1847 This variable has no effect if `org-agenda-show-inherited-tags'
1848 is set to `always'. In that case, the agenda is aware of those
1849 tags.
1851 The default value sets tags in every agenda type. Setting this
1852 option to nil will speed up non-tags agenda view a lot."
1853 :group 'org-agenda
1854 :version "24.3"
1855 :type '(choice
1856 (const :tag "Use tag inheritance in all agenda types" t)
1857 (repeat :tag "Use tag inheritance in selected agenda types"
1858 (symbol :tag "Agenda type"))))
1860 (defcustom org-agenda-hide-tags-regexp nil
1861 "Regular expression used to filter away specific tags in agenda views.
1862 This means that these tags will be present, but not be shown in the agenda
1863 line. Secondary filtering will still work on the hidden tags.
1864 Nil means don't hide any tags."
1865 :group 'org-agenda-line-format
1866 :type '(choice
1867 (const :tag "Hide none" nil)
1868 (string :tag "Regexp ")))
1870 (defcustom org-agenda-remove-tags nil
1871 "Non-nil means remove the tags from the headline copy in the agenda.
1872 When this is the symbol `prefix', only remove tags when
1873 `org-agenda-prefix-format' contains a `%T' specifier."
1874 :group 'org-agenda-line-format
1875 :type '(choice
1876 (const :tag "Always" t)
1877 (const :tag "Never" nil)
1878 (const :tag "When prefix format contains %T" prefix)))
1880 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1881 'org-agenda-remove-tags)
1883 (defcustom org-agenda-tags-column -80
1884 "Shift tags in agenda items to this column.
1885 If this number is positive, it specifies the column. If it is negative,
1886 it means that the tags should be flushright to that column. For example,
1887 -80 works well for a normal 80 character screen."
1888 :group 'org-agenda-line-format
1889 :type 'integer)
1891 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column)
1893 (defcustom org-agenda-fontify-priorities 'cookies
1894 "Non-nil means highlight low and high priorities in agenda.
1895 When t, the highest priority entries are bold, lowest priority italic.
1896 However, settings in `org-priority-faces' will overrule these faces.
1897 When this variable is the symbol `cookies', only fontify the
1898 cookies, not the entire task.
1899 This may also be an association list of priority faces, whose
1900 keys are the character values of `org-highest-priority',
1901 `org-default-priority', and `org-lowest-priority' (the default values
1902 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1903 color as a string, or a list like `(:background \"Red\")'.
1904 If it is a color, the variable `org-faces-easy-properties'
1905 determines if it is a foreground or a background color."
1906 :group 'org-agenda-line-format
1907 :type '(choice
1908 (const :tag "Never" nil)
1909 (const :tag "Defaults" t)
1910 (const :tag "Cookies only" cookies)
1911 (repeat :tag "Specify"
1912 (list (character :tag "Priority" :value ?A)
1913 (choice :tag "Face "
1914 (string :tag "Color")
1915 (sexp :tag "Face"))))))
1917 (defcustom org-agenda-day-face-function nil
1918 "Function called to determine what face should be used to display a day.
1919 The only argument passed to that function is the day. It should
1920 returns a face, or nil if does not want to specify a face and let
1921 the normal rules apply."
1922 :group 'org-agenda-line-format
1923 :version "24.1"
1924 :type '(choice (const nil) (function)))
1926 (defcustom org-agenda-category-icon-alist nil
1927 "Alist of category icon to be displayed in agenda views.
1929 Each entry should have the following format:
1931 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1933 Where CATEGORY-REGEXP is a regexp matching the categories where
1934 the icon should be displayed.
1935 FILE-OR-DATA either a file path or a string containing image data.
1937 The other fields can be omitted safely if not needed:
1938 TYPE indicates the image type.
1939 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1940 image data.
1941 PROPS are additional image attributes to assign to the image,
1942 like, e.g. `:ascent center'.
1944 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1946 If you want to set the display properties yourself, just put a
1947 list as second element:
1949 (CATEGORY-REGEXP (MY PROPERTY LIST))
1951 For example, to display a 16px horizontal space for Emacs
1952 category, you can use:
1954 (\"Emacs\" \\='(space . (:width (16))))"
1955 :group 'org-agenda-line-format
1956 :version "24.1"
1957 :type '(alist :key-type (string :tag "Regexp matching category")
1958 :value-type (choice (list :tag "Icon"
1959 (string :tag "File or data")
1960 (symbol :tag "Type")
1961 (boolean :tag "Data?")
1962 (repeat :tag "Extra image properties" :inline t symbol))
1963 (list :tag "Display properties" sexp))))
1965 (defgroup org-agenda-column-view nil
1966 "Options concerning column view in the agenda."
1967 :tag "Org Agenda Column View"
1968 :group 'org-agenda)
1970 (defcustom org-agenda-view-columns-initially nil
1971 "When non-nil, switch to columns view right after creating the agenda."
1972 :group 'org-agenda-column-view
1973 :type 'boolean
1974 :version "26.1"
1975 :package-version '(Org . "9.0")
1976 :safe #'booleanp)
1978 (defcustom org-agenda-columns-show-summaries t
1979 "Non-nil means show summaries for columns displayed in the agenda view."
1980 :group 'org-agenda-column-view
1981 :type 'boolean)
1983 (defcustom org-agenda-columns-compute-summary-properties t
1984 "Non-nil means recompute all summary properties before column view.
1985 When column view in the agenda is listing properties that have a summary
1986 operator, it can go to all relevant buffers and recompute the summaries
1987 there. This can mean overhead for the agenda column view, but is necessary
1988 to have thing up to date.
1989 As a special case, a CLOCKSUM property also makes sure that the clock
1990 computations are current."
1991 :group 'org-agenda-column-view
1992 :type 'boolean)
1994 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1995 "Non-nil means the duration of an appointment will add to day effort.
1996 The property to which appointment durations will be added is the one given
1997 in the option `org-effort-property'. If an appointment does not have
1998 an end time, `org-agenda-default-appointment-duration' will be used. If that
1999 is not set, an appointment without end time will not contribute to the time
2000 estimate."
2001 :group 'org-agenda-column-view
2002 :type 'boolean)
2004 (defcustom org-agenda-auto-exclude-function nil
2005 "A function called with a tag to decide if it is filtered on \
2006 \\<org-agenda-mode-map>`\\[org-agenda-filter-by-tag] RET'.
2007 The sole argument to the function, which is called once for each
2008 possible tag, is a string giving the name of the tag. The
2009 function should return either nil if the tag should be included
2010 as normal, or \"-<TAG>\" to exclude the tag.
2011 Note that for the purpose of tag filtering, only the lower-case version of
2012 all tags will be considered, so that this function will only ever see
2013 the lower-case version of all tags."
2014 :group 'org-agenda
2015 :type '(choice (const nil) (function)))
2017 (defcustom org-agenda-bulk-custom-functions nil
2018 "Alist of characters and custom functions for bulk actions.
2019 For example, this value makes those two functions available:
2021 \\='((?R set-category)
2022 (?C bulk-cut))
2024 With selected entries in an agenda buffer, `B R' will call
2025 the custom function `set-category' on the selected entries.
2026 Note that functions in this alist don't need to be quoted."
2027 :type '(alist :key-type character :value-type (group function))
2028 :version "24.1"
2029 :group 'org-agenda)
2031 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
2032 "Execute BODY with point at location given by `org-hd-marker' property.
2033 If STRING is non-nil, the text property will be fetched from position 0
2034 in that string. If STRING is nil, it will be fetched from the beginning
2035 of the current line."
2036 (org-with-gensyms (marker)
2037 `(let ((,marker (get-text-property (if ,string 0 (point-at-bol))
2038 'org-hd-marker ,string)))
2039 (with-current-buffer (marker-buffer ,marker)
2040 (save-excursion
2041 (goto-char ,marker)
2042 ,@body)))))
2043 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
2045 (defun org-add-agenda-custom-command (entry)
2046 "Replace or add a command in `org-agenda-custom-commands'.
2047 This is mostly for hacking and trying a new command - once the command
2048 works you probably want to add it to `org-agenda-custom-commands' for good."
2049 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
2050 (if ass
2051 (setcdr ass (cdr entry))
2052 (push entry org-agenda-custom-commands))))
2054 ;;; Define the org-agenda-mode
2056 (defvar org-agenda-mode-map (make-sparse-keymap)
2057 "Keymap for `org-agenda-mode'.")
2058 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map)
2060 (defvar org-agenda-menu) ; defined later in this file.
2061 (defvar org-agenda-restrict nil) ; defined later in this file.
2062 (defvar org-agenda-follow-mode nil)
2063 (defvar org-agenda-entry-text-mode nil)
2064 (defvar org-agenda-clockreport-mode nil)
2065 (defvar org-agenda-show-log nil)
2066 (defvar org-agenda-redo-command nil)
2067 (defvar org-agenda-query-string nil)
2068 (defvar org-agenda-mode-hook nil
2069 "Hook run after `org-agenda-mode' is turned on.
2070 The buffer is still writable when this hook is called.")
2071 (defvar org-agenda-type nil)
2072 (defvar org-agenda-force-single-file nil)
2073 (defvar org-agenda-bulk-marked-entries nil
2074 "List of markers that refer to marked entries in the agenda.")
2075 (defvar org-agenda-current-date nil
2076 "Active date when building the agenda.")
2078 ;;; Multiple agenda buffers support
2080 (defcustom org-agenda-sticky nil
2081 "Non-nil means agenda q key will bury agenda buffers.
2082 Agenda commands will then show existing buffer instead of generating new ones.
2083 When nil, `q' will kill the single agenda buffer."
2084 :group 'org-agenda
2085 :version "24.3"
2086 :type 'boolean)
2089 ;;;###autoload
2090 (defun org-toggle-sticky-agenda (&optional arg)
2091 "Toggle `org-agenda-sticky'."
2092 (interactive "P")
2093 (let ((new-value (if arg
2094 (> (prefix-numeric-value arg) 0)
2095 (not org-agenda-sticky))))
2096 (if (equal new-value org-agenda-sticky)
2097 (and (called-interactively-p 'interactive)
2098 (message "Sticky agenda was already %s"
2099 (if org-agenda-sticky "enabled" "disabled")))
2100 (setq org-agenda-sticky new-value)
2101 (org-agenda-kill-all-agenda-buffers)
2102 (and (called-interactively-p 'interactive)
2103 (message "Sticky agenda %s"
2104 (if org-agenda-sticky "enabled" "disabled"))))))
2106 (defvar org-agenda-buffer nil
2107 "Agenda buffer currently being generated.")
2109 (defvar org-agenda-last-prefix-arg nil)
2110 (defvar org-agenda-this-buffer-name nil)
2111 (defvar org-agenda-doing-sticky-redo nil)
2112 (defvar org-agenda-this-buffer-is-sticky nil)
2113 (defvar org-agenda-last-indirect-buffer nil
2114 "Last buffer loaded by `org-agenda-tree-to-indirect-buffer'.")
2116 (defconst org-agenda-local-vars
2117 '(org-agenda-this-buffer-name
2118 org-agenda-undo-list
2119 org-agenda-pending-undo-list
2120 org-agenda-follow-mode
2121 org-agenda-entry-text-mode
2122 org-agenda-clockreport-mode
2123 org-agenda-show-log
2124 org-agenda-redo-command
2125 org-agenda-query-string
2126 org-agenda-type
2127 org-agenda-bulk-marked-entries
2128 org-agenda-undo-has-started-in
2129 org-agenda-info
2130 org-agenda-pre-window-conf
2131 org-agenda-columns-active
2132 org-agenda-tag-filter
2133 org-agenda-category-filter
2134 org-agenda-top-headline-filter
2135 org-agenda-regexp-filter
2136 org-agenda-effort-filter
2137 org-agenda-markers
2138 org-agenda-last-search-view-search-was-boolean
2139 org-agenda-last-indirect-buffer
2140 org-agenda-filtered-by-category
2141 org-agenda-filter-form
2142 org-agenda-cycle-counter
2143 org-agenda-last-prefix-arg)
2144 "Variables that must be local in agenda buffers to allow multiple buffers.")
2146 (defun org-agenda-mode ()
2147 "Mode for time-sorted view on action items in Org files.
2149 The following commands are available:
2151 \\{org-agenda-mode-map}"
2152 (interactive)
2153 (cond (org-agenda-doing-sticky-redo
2154 ;; Refreshing sticky agenda-buffer
2156 ;; Preserve the value of `org-agenda-local-vars' variables,
2157 ;; while letting `kill-all-local-variables' kill the rest
2158 (let ((save (buffer-local-variables)))
2159 (kill-all-local-variables)
2160 (mapc 'make-local-variable org-agenda-local-vars)
2161 (dolist (elem save)
2162 (let ((var (car elem))
2163 (val (cdr elem)))
2164 (when (and val
2165 (member var org-agenda-local-vars))
2166 (set var val)))))
2167 (setq-local org-agenda-this-buffer-is-sticky t))
2168 (org-agenda-sticky
2169 ;; Creating a sticky Agenda buffer for the first time
2170 (kill-all-local-variables)
2171 (mapc 'make-local-variable org-agenda-local-vars)
2172 (setq-local org-agenda-this-buffer-is-sticky t))
2174 ;; Creating a non-sticky agenda buffer
2175 (kill-all-local-variables)
2176 (setq-local org-agenda-this-buffer-is-sticky nil)))
2177 (setq org-agenda-undo-list nil
2178 org-agenda-pending-undo-list nil
2179 org-agenda-bulk-marked-entries nil)
2180 (setq major-mode 'org-agenda-mode)
2181 ;; Keep global-font-lock-mode from turning on font-lock-mode
2182 (setq-local font-lock-global-modes (list 'not major-mode))
2183 (setq mode-name "Org-Agenda")
2184 (setq indent-tabs-mode nil)
2185 (use-local-map org-agenda-mode-map)
2186 (easy-menu-add org-agenda-menu)
2187 (if org-startup-truncated (setq truncate-lines t))
2188 (setq-local line-move-visual nil)
2189 (add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
2190 (add-hook 'pre-command-hook 'org-unhighlight nil 'local)
2191 ;; Make sure properties are removed when copying text
2192 (add-hook 'filter-buffer-substring-functions
2193 (lambda (fun start end delete)
2194 (substring-no-properties (funcall fun start end delete)))
2195 nil t)
2196 (unless org-agenda-keep-modes
2197 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
2198 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode))
2199 (setq org-agenda-show-log org-agenda-start-with-log-mode)
2200 (setq org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode)
2201 (add-to-invisibility-spec '(org-filtered))
2202 (add-to-invisibility-spec '(org-link))
2203 (easy-menu-change
2204 '("Agenda") "Agenda Files"
2205 (append
2206 (list
2207 (vector
2208 (if (get 'org-agenda-files 'org-restrict)
2209 "Restricted to single file"
2210 "Edit File List")
2211 '(org-edit-agenda-file-list)
2212 (not (get 'org-agenda-files 'org-restrict)))
2213 "--")
2214 (mapcar 'org-file-menu-entry (org-agenda-files))))
2215 (org-agenda-set-mode-name)
2216 (apply
2217 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2218 (list 'org-agenda-mode-hook)))
2220 (substitute-key-definition 'undo 'org-agenda-undo
2221 org-agenda-mode-map global-map)
2222 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2223 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2224 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2225 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2226 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2227 (org-defkey org-agenda-mode-map [(meta down)] 'org-agenda-drag-line-forward)
2228 (org-defkey org-agenda-mode-map [(meta up)] 'org-agenda-drag-line-backward)
2229 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2230 (org-defkey org-agenda-mode-map "\M-m" 'org-agenda-bulk-toggle)
2231 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2232 (org-defkey org-agenda-mode-map "\M-*" 'org-agenda-bulk-toggle-all)
2233 (org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
2234 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2235 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2236 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2237 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2238 (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2239 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2240 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2241 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2242 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2243 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2244 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2245 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2246 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2247 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2248 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2249 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2250 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2251 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2252 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2253 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2254 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2255 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2256 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2257 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2258 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2259 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2260 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2261 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2262 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2263 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2264 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2265 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2266 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2267 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2268 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2269 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2270 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2271 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2273 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2274 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2275 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2276 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2277 (while l (org-defkey org-agenda-mode-map
2278 (int-to-string (pop l)) 'digit-argument)))
2280 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2281 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2282 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2283 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2284 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2285 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2286 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2287 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2288 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2289 (org-defkey org-agenda-mode-map "g" (lambda () (interactive) (org-agenda-redo t)))
2290 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2291 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2292 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2293 'org-clock-modify-effort-estimate)
2294 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2295 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2296 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2297 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2298 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2299 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2300 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2301 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2302 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2303 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2304 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2305 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2306 (substitute-key-definition 'next-line 'org-agenda-next-line
2307 org-agenda-mode-map global-map)
2308 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2309 org-agenda-mode-map global-map)
2310 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2311 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2312 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2313 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2314 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2315 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2316 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2317 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2318 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2319 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2320 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2321 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2322 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2323 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2324 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2325 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2326 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2327 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2328 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2329 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2330 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2331 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2332 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2333 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2334 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2335 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2336 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2337 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2338 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2339 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2340 (org-defkey org-agenda-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock-from-agenda)
2342 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2343 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2344 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2345 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2346 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2347 (org-defkey org-agenda-mode-map "_" 'org-agenda-filter-by-effort)
2348 (org-defkey org-agenda-mode-map "=" 'org-agenda-filter-by-regexp)
2349 (org-defkey org-agenda-mode-map "|" 'org-agenda-filter-remove-all)
2350 (org-defkey org-agenda-mode-map "~" 'org-agenda-limit-interactively)
2351 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2352 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-headline)
2353 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2354 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2355 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2356 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2358 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2359 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2361 (define-key org-agenda-mode-map [remap forward-paragraph] 'org-agenda-forward-block)
2362 (define-key org-agenda-mode-map [remap backward-paragraph] 'org-agenda-backward-block)
2364 (when org-agenda-mouse-1-follows-link
2365 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2366 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2367 '("Agenda"
2368 ("Agenda Files")
2369 "--"
2370 ("Agenda Dates"
2371 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2372 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2373 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2374 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2375 "--"
2376 ("View"
2377 ["Day View" org-agenda-day-view
2378 :active (org-agenda-check-type nil 'agenda)
2379 :style radio :selected (eq org-agenda-current-span 'day)
2380 :keys "v d (or just d)"]
2381 ["Week View" org-agenda-week-view
2382 :active (org-agenda-check-type nil 'agenda)
2383 :style radio :selected (eq org-agenda-current-span 'week)
2384 :keys "v w"]
2385 ["Fortnight View" org-agenda-fortnight-view
2386 :active (org-agenda-check-type nil 'agenda)
2387 :style radio :selected (eq org-agenda-current-span 'fortnight)
2388 :keys "v t"]
2389 ["Month View" org-agenda-month-view
2390 :active (org-agenda-check-type nil 'agenda)
2391 :style radio :selected (eq org-agenda-current-span 'month)
2392 :keys "v m"]
2393 ["Year View" org-agenda-year-view
2394 :active (org-agenda-check-type nil 'agenda)
2395 :style radio :selected (eq org-agenda-current-span 'year)
2396 :keys "v y"]
2397 "--"
2398 ["Include Diary" org-agenda-toggle-diary
2399 :style toggle :selected org-agenda-include-diary
2400 :active (org-agenda-check-type nil 'agenda)]
2401 ["Include Deadlines" org-agenda-toggle-deadlines
2402 :style toggle :selected org-agenda-include-deadlines
2403 :active (org-agenda-check-type nil 'agenda)]
2404 ["Use Time Grid" org-agenda-toggle-time-grid
2405 :style toggle :selected org-agenda-use-time-grid
2406 :active (org-agenda-check-type nil 'agenda)]
2407 "--"
2408 ["Show clock report" org-agenda-clockreport-mode
2409 :style toggle :selected org-agenda-clockreport-mode
2410 :active (org-agenda-check-type nil 'agenda)]
2411 ["Show some entry text" org-agenda-entry-text-mode
2412 :style toggle :selected org-agenda-entry-text-mode
2413 :active t]
2414 "--"
2415 ["Show Logbook entries" org-agenda-log-mode
2416 :style toggle :selected org-agenda-show-log
2417 :active (org-agenda-check-type nil 'agenda 'timeline)
2418 :keys "v l (or just l)"]
2419 ["Include archived trees" org-agenda-archives-mode
2420 :style toggle :selected org-agenda-archives-mode :active t
2421 :keys "v a"]
2422 ["Include archive files" (org-agenda-archives-mode t)
2423 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2424 :keys "v A"]
2425 "--"
2426 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2427 ["Write view to file" org-agenda-write t]
2428 ["Rebuild buffer" org-agenda-redo t]
2429 ["Save all Org buffers" org-save-all-org-buffers t]
2430 "--"
2431 ["Show original entry" org-agenda-show t]
2432 ["Go To (other window)" org-agenda-goto t]
2433 ["Go To (this window)" org-agenda-switch-to t]
2434 ["Capture with cursor date" org-agenda-capture t]
2435 ["Follow Mode" org-agenda-follow-mode
2436 :style toggle :selected org-agenda-follow-mode :active t]
2437 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2438 "--"
2439 ("TODO"
2440 ["Cycle TODO" org-agenda-todo t]
2441 ["Next TODO set" org-agenda-todo-nextset t]
2442 ["Previous TODO set" org-agenda-todo-previousset t]
2443 ["Add note" org-agenda-add-note t])
2444 ("Archive/Refile/Delete"
2445 ["Archive default" org-agenda-archive-default t]
2446 ["Archive default" org-agenda-archive-default-with-confirmation t]
2447 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2448 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2449 ["Archive subtree" org-agenda-archive t]
2450 "--"
2451 ["Refile" org-agenda-refile t]
2452 "--"
2453 ["Delete subtree" org-agenda-kill t])
2454 ("Bulk action"
2455 ["Mark entry" org-agenda-bulk-mark t]
2456 ["Mark all" org-agenda-bulk-mark-all t]
2457 ["Unmark entry" org-agenda-bulk-unmark t]
2458 ["Unmark all" org-agenda-bulk-unmark-all :active t :keys "U"]
2459 ["Toggle mark" org-agenda-bulk-toggle t]
2460 ["Toggle all" org-agenda-bulk-toggle-all t]
2461 ["Mark regexp" org-agenda-bulk-mark-regexp t])
2462 ["Act on all marked" org-agenda-bulk-action t]
2463 "--"
2464 ("Tags and Properties"
2465 ["Show all Tags" org-agenda-show-tags t]
2466 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2467 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2468 "--"
2469 ["Column View" org-columns t])
2470 ("Deadline/Schedule"
2471 ["Schedule" org-agenda-schedule t]
2472 ["Set Deadline" org-agenda-deadline t]
2473 "--"
2474 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2475 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2476 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2477 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2478 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2479 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2480 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2481 ("Clock and Effort"
2482 ["Clock in" org-agenda-clock-in t]
2483 ["Clock out" org-agenda-clock-out t]
2484 ["Clock cancel" org-agenda-clock-cancel t]
2485 ["Goto running clock" org-clock-goto t]
2486 "--"
2487 ["Set Effort" org-agenda-set-effort t]
2488 ["Change clocked effort" org-clock-modify-effort-estimate
2489 (org-clock-is-active)])
2490 ("Priority"
2491 ["Set Priority" org-agenda-priority t]
2492 ["Increase Priority" org-agenda-priority-up t]
2493 ["Decrease Priority" org-agenda-priority-down t]
2494 ["Show Priority" org-show-priority t])
2495 ("Calendar/Diary"
2496 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2497 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2498 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2499 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2500 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2501 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2502 "--"
2503 ["Create iCalendar File" org-icalendar-combine-agenda-files t])
2504 "--"
2505 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2506 "--"
2507 ("MobileOrg"
2508 ["Push Files and Views" org-mobile-push t]
2509 ["Get Captured and Flagged" org-mobile-pull t]
2510 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2511 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2512 "--"
2513 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2514 "--"
2515 ["Quit" org-agenda-quit t]
2516 ["Exit and Release Buffers" org-agenda-exit t]
2519 ;;; Agenda undo
2521 (defvar org-agenda-allow-remote-undo t
2522 "Non-nil means allow remote undo from the agenda buffer.")
2523 (defvar org-agenda-undo-has-started-in nil
2524 "Buffers that have already seen `undo-start' in the current undo sequence.")
2526 (defun org-agenda-undo ()
2527 "Undo a remote editing step in the agenda.
2528 This undoes changes both in the agenda buffer and in the remote buffer
2529 that have been changed along."
2530 (interactive)
2531 (or org-agenda-allow-remote-undo
2532 (user-error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2533 (if (not (eq this-command last-command))
2534 (setq org-agenda-undo-has-started-in nil
2535 org-agenda-pending-undo-list org-agenda-undo-list))
2536 (if (not org-agenda-pending-undo-list)
2537 (user-error "No further undo information"))
2538 (let* ((entry (pop org-agenda-pending-undo-list))
2539 buf line cmd rembuf)
2540 (setq cmd (pop entry) line (pop entry))
2541 (setq rembuf (nth 2 entry))
2542 (org-with-remote-undo rembuf
2543 (while (bufferp (setq buf (pop entry)))
2544 (if (pop entry)
2545 (with-current-buffer buf
2546 (let ((last-undo-buffer buf)
2547 (inhibit-read-only t))
2548 (unless (memq buf org-agenda-undo-has-started-in)
2549 (push buf org-agenda-undo-has-started-in)
2550 (make-local-variable 'pending-undo-list)
2551 (undo-start))
2552 (while (and pending-undo-list
2553 (listp pending-undo-list)
2554 (not (car pending-undo-list)))
2555 (pop pending-undo-list))
2556 (undo-more 1))))))
2557 (org-goto-line line)
2558 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2560 (defun org-verify-change-for-undo (l1 l2)
2561 "Verify that a real change occurred between the undo lists L1 and L2."
2562 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2563 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2564 (not (eq l1 l2)))
2566 ;;; Agenda dispatch
2568 (defvar org-agenda-restrict-begin (make-marker))
2569 (defvar org-agenda-restrict-end (make-marker))
2570 (defvar org-agenda-last-dispatch-buffer nil)
2571 (defvar org-agenda-overriding-restriction nil)
2573 (defcustom org-agenda-custom-commands-contexts nil
2574 "Alist of custom agenda keys and contextual rules.
2576 For example, if you have a custom agenda command \"p\" and you
2577 want this command to be accessible only from plain text files,
2578 use this:
2580 \\='((\"p\" ((in-file . \"\\\\.txt\\\\'\"))))
2582 Here are the available contexts definitions:
2584 in-file: command displayed only in matching files
2585 in-mode: command displayed only in matching modes
2586 not-in-file: command not displayed in matching files
2587 not-in-mode: command not displayed in matching modes
2588 in-buffer: command displayed only in matching buffers
2589 not-in-buffer: command not displayed in matching buffers
2590 [function]: a custom function taking no argument
2592 If you define several checks, the agenda command will be
2593 accessible if there is at least one valid check.
2595 You can also bind a key to another agenda custom command
2596 depending on contextual rules.
2598 \\='((\"p\" \"q\" ((in-file . \"\\\\.txt\\\\'\"))))
2600 Here it means: in .txt files, use \"p\" as the key for the
2601 agenda command otherwise associated with \"q\". (The command
2602 originally associated with \"q\" is not displayed to avoid
2603 duplicates.)"
2604 :version "24.3"
2605 :group 'org-agenda-custom-commands
2606 :type '(repeat (list :tag "Rule"
2607 (string :tag " Agenda key")
2608 (string :tag "Replace by command")
2609 (repeat :tag "Available when"
2610 (choice
2611 (cons :tag "Condition"
2612 (choice
2613 (const :tag "In file" in-file)
2614 (const :tag "Not in file" not-in-file)
2615 (const :tag "In buffer" in-buffer)
2616 (const :tag "Not in buffer" not-in-buffer)
2617 (const :tag "In mode" in-mode)
2618 (const :tag "Not in mode" not-in-mode))
2619 (regexp))
2620 (function :tag "Custom function"))))))
2622 (defcustom org-agenda-max-entries nil
2623 "Maximum number of entries to display in an agenda.
2624 This can be nil (no limit) or an integer or an alist of agenda
2625 types with an associated number of entries to display in this
2626 type."
2627 :version "24.4"
2628 :package-version '(Org . "8.0")
2629 :group 'org-agenda-custom-commands
2630 :type '(choice (symbol :tag "No limit" nil)
2631 (integer :tag "Max number of entries")
2632 (repeat
2633 (cons (choice :tag "Agenda type"
2634 (const agenda)
2635 (const todo)
2636 (const tags)
2637 (const search)
2638 (const timeline))
2639 (integer :tag "Max number of entries")))))
2641 (defcustom org-agenda-max-todos nil
2642 "Maximum number of TODOs to display in an agenda.
2643 This can be nil (no limit) or an integer or an alist of agenda
2644 types with an associated number of entries to display in this
2645 type."
2646 :version "24.4"
2647 :package-version '(Org . "8.0")
2648 :group 'org-agenda-custom-commands
2649 :type '(choice (symbol :tag "No limit" nil)
2650 (integer :tag "Max number of TODOs")
2651 (repeat
2652 (cons (choice :tag "Agenda type"
2653 (const agenda)
2654 (const todo)
2655 (const tags)
2656 (const search)
2657 (const timeline))
2658 (integer :tag "Max number of TODOs")))))
2660 (defcustom org-agenda-max-tags nil
2661 "Maximum number of tagged entries to display in an agenda.
2662 This can be nil (no limit) or an integer or an alist of agenda
2663 types with an associated number of entries to display in this
2664 type."
2665 :version "24.4"
2666 :package-version '(Org . "8.0")
2667 :group 'org-agenda-custom-commands
2668 :type '(choice (symbol :tag "No limit" nil)
2669 (integer :tag "Max number of tagged entries")
2670 (repeat
2671 (cons (choice :tag "Agenda type"
2672 (const agenda)
2673 (const todo)
2674 (const tags)
2675 (const search)
2676 (const timeline))
2677 (integer :tag "Max number of tagged entries")))))
2679 (defcustom org-agenda-max-effort nil
2680 "Maximum cumulated effort duration for the agenda.
2681 This can be nil (no limit) or a number of minutes (as an integer)
2682 or an alist of agenda types with an associated number of minutes
2683 to limit entries to in this type."
2684 :version "24.4"
2685 :package-version '(Org . "8.0")
2686 :group 'org-agenda-custom-commands
2687 :type '(choice (symbol :tag "No limit" nil)
2688 (integer :tag "Max number of minutes")
2689 (repeat
2690 (cons (choice :tag "Agenda type"
2691 (const agenda)
2692 (const todo)
2693 (const tags)
2694 (const search)
2695 (const timeline))
2696 (integer :tag "Max number of minutes")))))
2698 (defvar org-agenda-keep-restricted-file-list nil)
2699 (defvar org-keys nil)
2700 (defvar org-match nil)
2701 ;;;###autoload
2702 (defun org-agenda (&optional arg org-keys restriction)
2703 "Dispatch agenda commands to collect entries to the agenda buffer.
2704 Prompts for a command to execute. Any prefix arg will be passed
2705 on to the selected command. The default selections are:
2707 a Call `org-agenda-list' to display the agenda for current day or week.
2708 t Call `org-todo-list' to display the global todo list.
2709 T Call `org-todo-list' to display the global todo list, select only
2710 entries with a specific TODO keyword (the user gets a prompt).
2711 m Call `org-tags-view' to display headlines with tags matching
2712 a condition (the user is prompted for the condition).
2713 M Like `m', but select only TODO entries, no ordinary headlines.
2714 e Export views to associated files.
2715 s Search entries for keywords.
2716 S Search entries for keywords, only with TODO keywords.
2717 / Multi occur across all agenda files and also files listed
2718 in `org-agenda-text-search-extra-files'.
2719 < Restrict agenda commands to buffer, subtree, or region.
2720 Press several times to get the desired effect.
2721 > Remove a previous restriction.
2722 # List \"stuck\" projects.
2723 ! Configure what \"stuck\" means.
2724 C Configure custom agenda commands.
2726 More commands can be added by configuring the variable
2727 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2728 searches can be pre-defined in this way.
2730 If the current buffer is in Org mode and visiting a file, you can also
2731 first press `<' once to indicate that the agenda should be temporarily
2732 \(until the next use of `\\[org-agenda]') restricted to the current file.
2733 Pressing `<' twice means to restrict to the current subtree or region
2734 \(if active)."
2735 (interactive "P")
2736 (catch 'exit
2737 (let* ((prefix-descriptions nil)
2738 (org-agenda-buffer-name org-agenda-buffer-name)
2739 (org-agenda-window-setup (if (equal (buffer-name)
2740 org-agenda-buffer-name)
2741 'current-window
2742 org-agenda-window-setup))
2743 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2744 (org-agenda-custom-commands
2745 ;; normalize different versions
2746 (delq nil
2747 (mapcar
2748 (lambda (x)
2749 (cond ((stringp (cdr x))
2750 (push x prefix-descriptions)
2751 nil)
2752 ((stringp (nth 1 x)) x)
2753 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2754 (t (cons (car x) (cons "" (cdr x))))))
2755 org-agenda-custom-commands)))
2756 (org-agenda-custom-commands
2757 (org-contextualize-keys
2758 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2759 (buf (current-buffer))
2760 (bfn (buffer-file-name (buffer-base-buffer)))
2761 entry key type org-match lprops ans)
2762 ;; Turn off restriction unless there is an overriding one,
2763 (unless org-agenda-overriding-restriction
2764 (unless org-agenda-keep-restricted-file-list
2765 ;; There is a request to keep the file list in place
2766 (put 'org-agenda-files 'org-restrict nil))
2767 (setq org-agenda-restrict nil)
2768 (move-marker org-agenda-restrict-begin nil)
2769 (move-marker org-agenda-restrict-end nil))
2770 ;; Delete old local properties
2771 (put 'org-agenda-redo-command 'org-lprops nil)
2772 ;; Delete previously set last-arguments
2773 (put 'org-agenda-redo-command 'last-args nil)
2774 ;; Remember where this call originated
2775 (setq org-agenda-last-dispatch-buffer (current-buffer))
2776 (unless org-keys
2777 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2778 org-keys (car ans)
2779 restriction (cdr ans)))
2780 ;; If we have sticky agenda buffers, set a name for the buffer,
2781 ;; depending on the invoking keys. The user may still set this
2782 ;; as a command option, which will overwrite what we do here.
2783 (if org-agenda-sticky
2784 (setq org-agenda-buffer-name
2785 (format "*Org Agenda(%s)*" org-keys)))
2786 ;; Establish the restriction, if any
2787 (when (and (not org-agenda-overriding-restriction) restriction)
2788 (put 'org-agenda-files 'org-restrict (list bfn))
2789 (cond
2790 ((eq restriction 'region)
2791 (setq org-agenda-restrict (current-buffer))
2792 (move-marker org-agenda-restrict-begin (region-beginning))
2793 (move-marker org-agenda-restrict-end (region-end)))
2794 ((eq restriction 'subtree)
2795 (save-excursion
2796 (setq org-agenda-restrict (current-buffer))
2797 (org-back-to-heading t)
2798 (move-marker org-agenda-restrict-begin (point))
2799 (move-marker org-agenda-restrict-end
2800 (progn (org-end-of-subtree t)))))))
2802 ;; For example the todo list should not need it (but does...)
2803 (cond
2804 ((setq entry (assoc org-keys org-agenda-custom-commands))
2805 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2806 (progn
2807 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
2808 lprops (nth 4 entry))
2809 (if org-agenda-sticky
2810 (setq org-agenda-buffer-name
2811 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2812 (format "*Org Agenda(%s)*" org-keys))))
2813 (put 'org-agenda-redo-command 'org-lprops lprops)
2814 (cond
2815 ((eq type 'agenda)
2816 (org-let lprops '(org-agenda-list current-prefix-arg)))
2817 ((eq type 'agenda*)
2818 (org-let lprops '(org-agenda-list current-prefix-arg nil nil t)))
2819 ((eq type 'alltodo)
2820 (org-let lprops '(org-todo-list current-prefix-arg)))
2821 ((eq type 'search)
2822 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
2823 ((eq type 'stuck)
2824 (org-let lprops '(org-agenda-list-stuck-projects
2825 current-prefix-arg)))
2826 ((eq type 'tags)
2827 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2828 ((eq type 'tags-todo)
2829 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2830 ((eq type 'todo)
2831 (org-let lprops '(org-todo-list org-match)))
2832 ((eq type 'tags-tree)
2833 (org-check-for-org-mode)
2834 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
2835 ((eq type 'todo-tree)
2836 (org-check-for-org-mode)
2837 (org-let lprops
2838 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2839 (regexp-quote org-match) "\\>"))))
2840 ((eq type 'occur-tree)
2841 (org-check-for-org-mode)
2842 (org-let lprops '(org-occur org-match)))
2843 ((functionp type)
2844 (org-let lprops '(funcall type org-match)))
2845 ((fboundp type)
2846 (org-let lprops '(funcall type org-match)))
2847 (t (user-error "Invalid custom agenda command type %s" type))))
2848 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2849 ((equal org-keys "C")
2850 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2851 (customize-variable 'org-agenda-custom-commands))
2852 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2853 ((equal org-keys "s") (call-interactively 'org-search-view))
2854 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2855 ((equal org-keys "t") (call-interactively 'org-todo-list))
2856 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2857 ((equal org-keys "m") (call-interactively 'org-tags-view))
2858 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2859 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2860 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
2861 (add-hook
2862 'post-command-hook
2863 (lambda ()
2864 (unless (current-message)
2865 (let* ((m (org-agenda-get-any-marker))
2866 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2867 (when note
2868 (message (concat
2869 "FLAGGING-NOTE ([?] for more info): "
2870 (org-add-props
2871 (replace-regexp-in-string
2872 "\\\\n" "//"
2873 (copy-sequence note))
2874 nil 'face 'org-warning)))))))
2875 t t))
2876 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2877 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2878 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
2879 (t (user-error "Invalid agenda key"))))))
2881 (defvar org-agenda-multi)
2883 (defun org-agenda-append-agenda ()
2884 "Append another agenda view to the current one.
2885 This function allows interactive building of block agendas.
2886 Agenda views are separated by `org-agenda-block-separator'."
2887 (interactive)
2888 (unless (derived-mode-p 'org-agenda-mode)
2889 (user-error "Can only append from within agenda buffer"))
2890 (let ((org-agenda-multi t))
2891 (org-agenda)
2892 (widen)
2893 (org-agenda-finalize)
2894 (setq buffer-read-only t)
2895 (org-agenda-fit-window-to-buffer)))
2897 (defun org-agenda-normalize-custom-commands (cmds)
2898 "Normalize custom commands CMDS."
2899 (delq nil
2900 (mapcar
2901 (lambda (x)
2902 (cond ((stringp (cdr x)) nil)
2903 ((stringp (nth 1 x)) x)
2904 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2905 (t (cons (car x) (cons "" (cdr x))))))
2906 cmds)))
2908 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2909 "The user interface for selecting an agenda command."
2910 (catch 'exit
2911 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2912 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2913 (region-p (org-region-active-p))
2914 (custom org-agenda-custom-commands)
2915 (selstring "")
2916 restriction second-time
2917 c entry key type match prefixes rmheader header-end custom1 desc
2918 line lines left right n n1)
2919 (save-window-excursion
2920 (delete-other-windows)
2921 (org-switch-to-buffer-other-window " *Agenda Commands*")
2922 (erase-buffer)
2923 (insert (eval-when-compile
2924 (let ((header
2925 "Press key for an agenda command:
2926 -------------------------------- < Buffer, subtree/region restriction
2927 a Agenda for current week or day > Remove restriction
2928 t List of all TODO entries e Export agenda views
2929 m Match a TAGS/PROP/TODO query T Entries with special TODO kwd
2930 s Search for keywords M Like m, but only TODO entries
2931 / Multi-occur S Like s, but only TODO entries
2932 ? Find :FLAGGED: entries C Configure custom agenda commands
2933 * Toggle sticky agenda views # List stuck projects (!=configure)
2935 (start 0))
2936 (while (string-match
2937 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2938 header start)
2939 (setq start (match-end 0))
2940 (add-text-properties (match-beginning 2) (match-end 2)
2941 '(face bold) header))
2942 header)))
2943 (setq header-end (point-marker))
2944 (while t
2945 (setq custom1 custom)
2946 (when (eq rmheader t)
2947 (org-goto-line 1)
2948 (re-search-forward ":" nil t)
2949 (delete-region (match-end 0) (point-at-eol))
2950 (forward-char 1)
2951 (looking-at "-+")
2952 (delete-region (match-end 0) (point-at-eol))
2953 (move-marker header-end (match-end 0)))
2954 (goto-char header-end)
2955 (delete-region (point) (point-max))
2957 ;; Produce all the lines that describe custom commands and prefixes
2958 (setq lines nil)
2959 (while (setq entry (pop custom1))
2960 (setq key (car entry) desc (nth 1 entry)
2961 type (nth 2 entry)
2962 match (nth 3 entry))
2963 (if (> (length key) 1)
2964 (cl-pushnew (string-to-char key) prefixes :test #'equal)
2965 (setq line
2966 (format
2967 "%-4s%-14s"
2968 (org-add-props (copy-sequence key)
2969 '(face bold))
2970 (cond
2971 ((string-match "\\S-" desc) desc)
2972 ((eq type 'agenda) "Agenda for current week or day")
2973 ((eq type 'agenda*) "Appointments for current week or day")
2974 ((eq type 'alltodo) "List of all TODO entries")
2975 ((eq type 'search) "Word search")
2976 ((eq type 'stuck) "List of stuck projects")
2977 ((eq type 'todo) "TODO keyword")
2978 ((eq type 'tags) "Tags query")
2979 ((eq type 'tags-todo) "Tags (TODO)")
2980 ((eq type 'tags-tree) "Tags tree")
2981 ((eq type 'todo-tree) "TODO kwd tree")
2982 ((eq type 'occur-tree) "Occur tree")
2983 ((functionp type) (if (symbolp type)
2984 (symbol-name type)
2985 "Lambda expression"))
2986 (t "???"))))
2987 (if org-agenda-menu-show-matcher
2988 (setq line
2989 (concat line ": "
2990 (cond
2991 ((stringp match)
2992 (setq match (copy-sequence match))
2993 (org-add-props match nil 'face 'org-warning))
2994 ((listp type)
2995 (format "set of %d commands" (length type))))))
2996 (if (org-string-nw-p match)
2997 (add-text-properties
2998 0 (length line) (list 'help-echo
2999 (concat "Matcher: " match)) line)))
3000 (push line lines)))
3001 (setq lines (nreverse lines))
3002 (when prefixes
3003 (mapc (lambda (x)
3004 (push
3005 (format "%s %s"
3006 (org-add-props (char-to-string x)
3007 nil 'face 'bold)
3008 (or (cdr (assoc (concat selstring
3009 (char-to-string x))
3010 prefix-descriptions))
3011 "Prefix key"))
3012 lines))
3013 prefixes))
3015 ;; Check if we should display in two columns
3016 (if org-agenda-menu-two-columns
3017 (progn
3018 (setq n (length lines)
3019 n1 (+ (/ n 2) (mod n 2))
3020 right (nthcdr n1 lines)
3021 left (copy-sequence lines))
3022 (setcdr (nthcdr (1- n1) left) nil))
3023 (setq left lines right nil))
3024 (while left
3025 (insert "\n" (pop left))
3026 (when right
3027 (if (< (current-column) 40)
3028 (move-to-column 40 t)
3029 (insert " "))
3030 (insert (pop right))))
3032 ;; Make the window the right size
3033 (goto-char (point-min))
3034 (if second-time
3035 (if (not (pos-visible-in-window-p (point-max)))
3036 (org-fit-window-to-buffer))
3037 (setq second-time t)
3038 (org-fit-window-to-buffer))
3040 ;; Ask for selection
3041 (message "Press key for agenda command%s:"
3042 (if (or restrict-ok org-agenda-overriding-restriction)
3043 (if org-agenda-overriding-restriction
3044 " (restriction lock active)"
3045 (if restriction
3046 (format " (restricted to %s)" restriction)
3047 " (unrestricted)"))
3048 ""))
3049 (setq c (read-char-exclusive))
3050 (message "")
3051 (cond
3052 ((assoc (char-to-string c) custom)
3053 (setq selstring (concat selstring (char-to-string c)))
3054 (throw 'exit (cons selstring restriction)))
3055 ((memq c prefixes)
3056 (setq selstring (concat selstring (char-to-string c))
3057 prefixes nil
3058 rmheader (or rmheader t)
3059 custom (delq nil (mapcar
3060 (lambda (x)
3061 (if (or (= (length (car x)) 1)
3062 (/= (string-to-char (car x)) c))
3064 (cons (substring (car x) 1) (cdr x))))
3065 custom))))
3066 ((eq c ?*)
3067 (call-interactively 'org-toggle-sticky-agenda)
3068 (sit-for 2))
3069 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
3070 (message "Restriction is only possible in Org buffers")
3071 (ding) (sit-for 1))
3072 ((eq c ?1)
3073 (org-agenda-remove-restriction-lock 'noupdate)
3074 (setq restriction 'buffer))
3075 ((eq c ?0)
3076 (org-agenda-remove-restriction-lock 'noupdate)
3077 (setq restriction (if region-p 'region 'subtree)))
3078 ((eq c ?<)
3079 (org-agenda-remove-restriction-lock 'noupdate)
3080 (setq restriction
3081 (cond
3082 ((eq restriction 'buffer)
3083 (if region-p 'region 'subtree))
3084 ((memq restriction '(subtree region))
3085 nil)
3086 (t 'buffer))))
3087 ((eq c ?>)
3088 (org-agenda-remove-restriction-lock 'noupdate)
3089 (setq restriction nil))
3090 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
3091 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
3092 ((and (> (length selstring) 0) (eq c ?\d))
3093 (delete-window)
3094 (org-agenda-get-restriction-and-command prefix-descriptions))
3096 ((equal c ?q) (error "Abort"))
3097 (t (user-error "Invalid key %c" c))))))))
3099 (defun org-agenda-fit-window-to-buffer ()
3100 "Fit the window to the buffer size."
3101 (and (memq org-agenda-window-setup '(reorganize-frame))
3102 (fboundp 'fit-window-to-buffer)
3103 (if (and (= (cdr org-agenda-window-frame-fractions) 1.0)
3104 (= (car org-agenda-window-frame-fractions) 1.0))
3105 (delete-other-windows)
3106 (org-fit-window-to-buffer
3108 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3109 (floor (* (frame-height) (car org-agenda-window-frame-fractions)))))))
3111 (defvar org-cmd nil)
3112 (defvar org-agenda-overriding-cmd nil)
3113 (defvar org-agenda-overriding-arguments nil)
3114 (defvar org-agenda-overriding-cmd-arguments nil)
3115 (defun org-agenda-run-series (name series)
3116 "Run agenda NAME as a SERIES of agenda commands."
3117 (org-let (nth 1 series) '(org-agenda-prepare name))
3118 ;; We need to reset agenda markers here, because when constructing a
3119 ;; block agenda, the individual blocks do not do that.
3120 (org-agenda-reset-markers)
3121 (let* ((org-agenda-multi t)
3122 (redo (list 'org-agenda-run-series name (list 'quote series)))
3123 (cmds (car series))
3124 (gprops (nth 1 series))
3125 match ;; The byte compiler incorrectly complains about this. Keep it!
3126 org-cmd type lprops)
3127 (while (setq org-cmd (pop cmds))
3128 (setq type (car org-cmd))
3129 (setq match (eval (nth 1 org-cmd)))
3130 (setq lprops (nth 2 org-cmd))
3131 (let ((org-agenda-overriding-arguments
3132 (if (eq org-agenda-overriding-cmd org-cmd)
3133 (or org-agenda-overriding-arguments
3134 org-agenda-overriding-cmd-arguments))))
3135 (cond
3136 ((eq type 'agenda)
3137 (org-let2 gprops lprops
3138 '(call-interactively 'org-agenda-list)))
3139 ((eq type 'agenda*)
3140 (org-let2 gprops lprops
3141 '(funcall 'org-agenda-list nil nil t)))
3142 ((eq type 'alltodo)
3143 (org-let2 gprops lprops
3144 '(call-interactively 'org-todo-list)))
3145 ((eq type 'search)
3146 (org-let2 gprops lprops
3147 '(org-search-view current-prefix-arg match nil)))
3148 ((eq type 'stuck)
3149 (org-let2 gprops lprops
3150 '(call-interactively 'org-agenda-list-stuck-projects)))
3151 ((eq type 'tags)
3152 (org-let2 gprops lprops
3153 '(org-tags-view current-prefix-arg match)))
3154 ((eq type 'tags-todo)
3155 (org-let2 gprops lprops
3156 '(org-tags-view current-prefix-arg match)))
3157 ((eq type 'todo)
3158 (org-let2 gprops lprops
3159 '(org-todo-list match)))
3160 ((fboundp type)
3161 (org-let2 gprops lprops
3162 '(funcall type match)))
3163 (t (error "Invalid type in command series")))))
3164 (widen)
3165 (let ((inhibit-read-only t))
3166 (add-text-properties (point-min) (point-max)
3167 `(org-series t org-series-redo-cmd ,redo)))
3168 (setq org-agenda-redo-command redo)
3169 (goto-char (point-min)))
3170 (org-agenda-fit-window-to-buffer)
3171 (org-let (nth 1 series) '(org-agenda-finalize)))
3173 ;;;###autoload
3174 (defmacro org-batch-agenda (cmd-key &rest parameters)
3175 "Run an agenda command in batch mode and send the result to STDOUT.
3176 If CMD-KEY is a string of length 1, it is used as a key in
3177 `org-agenda-custom-commands' and triggers this command. If it is a
3178 longer string it is used as a tags/todo match string.
3179 Parameters are alternating variable names and values that will be bound
3180 before running the agenda command."
3181 (org-eval-in-environment (org-make-parameter-alist parameters)
3182 (let (org-agenda-sticky)
3183 (if (> (length cmd-key) 1)
3184 (org-tags-view nil cmd-key)
3185 (org-agenda nil cmd-key))))
3186 (set-buffer org-agenda-buffer-name)
3187 (princ (buffer-string)))
3189 (defvar org-agenda-info nil)
3191 ;;;###autoload
3192 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
3193 "Run an agenda command in batch mode and send the result to STDOUT.
3194 If CMD-KEY is a string of length 1, it is used as a key in
3195 `org-agenda-custom-commands' and triggers this command. If it is a
3196 longer string it is used as a tags/todo match string.
3197 Parameters are alternating variable names and values that will be bound
3198 before running the agenda command.
3200 The output gives a line for each selected agenda item. Each
3201 item is a list of comma-separated values, like this:
3203 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3205 category The category of the item
3206 head The headline, without TODO kwd, TAGS and PRIORITY
3207 type The type of the agenda entry, can be
3208 todo selected in TODO match
3209 tagsmatch selected in tags match
3210 diary imported from diary
3211 deadline a deadline on given date
3212 scheduled scheduled on given date
3213 timestamp entry has timestamp on given date
3214 closed entry was closed on given date
3215 upcoming-deadline warning about deadline
3216 past-scheduled forwarded scheduled item
3217 block entry has date block including g. date
3218 todo The todo keyword, if any
3219 tags All tags including inherited ones, separated by colons
3220 date The relevant date, like 2007-2-14
3221 time The time, like 15:00-16:50
3222 extra Sting with extra planning info
3223 priority-l The priority letter if any was given
3224 priority-n The computed numerical priority
3225 agenda-day The day in the agenda where this is listed"
3226 (org-eval-in-environment (append '((org-agenda-remove-tags t))
3227 (org-make-parameter-alist parameters))
3228 (if (> (length cmd-key) 2)
3229 (org-tags-view nil cmd-key)
3230 (org-agenda nil cmd-key)))
3231 (set-buffer org-agenda-buffer-name)
3232 (let* ((lines (org-split-string (buffer-string) "\n"))
3233 line)
3234 (while (setq line (pop lines))
3235 (catch 'next
3236 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
3237 (setq org-agenda-info
3238 (org-fix-agenda-info (text-properties-at 0 line)))
3239 (princ
3240 (mapconcat 'org-agenda-export-csv-mapper
3241 '(org-category txt type todo tags date time extra
3242 priority-letter priority agenda-day)
3243 ","))
3244 (princ "\n")))))
3246 (defun org-fix-agenda-info (props)
3247 "Make sure all properties on an agenda item have a canonical form.
3248 This ensures the export commands can easily use it."
3249 (let (tmp re)
3250 (when (setq tmp (plist-get props 'tags))
3251 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3252 (when (setq tmp (plist-get props 'date))
3253 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3254 (let ((calendar-date-display-form '(year "-" month "-" day)))
3255 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3257 (setq tmp (calendar-date-string tmp)))
3258 (setq props (plist-put props 'date tmp)))
3259 (when (setq tmp (plist-get props 'day))
3260 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3261 (let ((calendar-date-display-form '(year "-" month "-" day)))
3262 (setq tmp (calendar-date-string tmp)))
3263 (setq props (plist-put props 'day tmp))
3264 (setq props (plist-put props 'agenda-day tmp)))
3265 (when (setq tmp (plist-get props 'txt))
3266 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3267 (plist-put props 'priority-letter (match-string 1 tmp))
3268 (setq tmp (replace-match "" t t tmp)))
3269 (when (and (setq re (plist-get props 'org-todo-regexp))
3270 (setq re (concat "\\`\\.*" re " ?"))
3271 (let ((case-fold-search nil)) (string-match re tmp)))
3272 (plist-put props 'todo (match-string 1 tmp))
3273 (setq tmp (replace-match "" t t tmp)))
3274 (plist-put props 'txt tmp)))
3275 props)
3277 (defun org-agenda-export-csv-mapper (prop)
3278 (let ((res (plist-get org-agenda-info prop)))
3279 (setq res
3280 (cond
3281 ((not res) "")
3282 ((stringp res) res)
3283 (t (prin1-to-string res))))
3284 (org-trim (replace-regexp-in-string "," ";" res nil t))))
3286 ;;;###autoload
3287 (defun org-store-agenda-views (&rest parameters)
3288 "Store agenda views."
3289 (interactive)
3290 (eval (list 'org-batch-store-agenda-views)))
3292 ;;;###autoload
3293 (defmacro org-batch-store-agenda-views (&rest parameters)
3294 "Run all custom agenda commands that have a file argument."
3295 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3296 (pop-up-frames nil)
3297 (dir default-directory)
3298 (pars (org-make-parameter-alist parameters))
3299 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
3300 (save-window-excursion
3301 (while cmds
3302 (setq cmd (pop cmds)
3303 thiscmdkey (car cmd)
3304 thiscmdcmd (cdr cmd)
3305 match (nth 2 thiscmdcmd)
3306 bufname (if org-agenda-sticky
3307 (or (and (stringp match)
3308 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3309 (format "*Org Agenda(%s)*" thiscmdkey))
3310 org-agenda-buffer-name)
3311 cmd-or-set (nth 2 cmd)
3312 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3313 files (nth (if (listp cmd-or-set) 4 5) cmd))
3314 (if (stringp files) (setq files (list files)))
3315 (when files
3316 (org-eval-in-environment (append org-agenda-exporter-settings
3317 opts pars)
3318 (org-agenda nil thiscmdkey))
3319 (set-buffer bufname)
3320 (while files
3321 (org-eval-in-environment (append org-agenda-exporter-settings
3322 opts pars)
3323 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3324 (and (get-buffer bufname)
3325 (kill-buffer bufname)))))))
3327 (defvar org-agenda-current-span nil
3328 "The current span used in the agenda view.") ; local variable in the agenda buffer
3329 (defun org-agenda-mark-header-line (pos)
3330 "Mark the line at POS as an agenda structure header."
3331 (save-excursion
3332 (goto-char pos)
3333 (put-text-property (point-at-bol) (point-at-eol)
3334 'org-agenda-structural-header t)
3335 (when org-agenda-title-append
3336 (put-text-property (point-at-bol) (point-at-eol)
3337 'org-agenda-title-append org-agenda-title-append))))
3339 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3340 (defvar org-agenda-write-buffer-name "Agenda View")
3341 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3342 "Write the current buffer (an agenda view) as a file.
3344 Depending on the extension of the file name, plain text (.txt),
3345 HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
3346 If the extension is .ics, translate visible agenda into iCalendar
3347 format. If the extension is .org, collect all subtrees
3348 corresponding to the agenda entries and add them in an .org file.
3350 With prefix argument OPEN, open the new file immediately. If
3351 NOSETTINGS is given, do not scope the settings of
3352 `org-agenda-exporter-settings' into the export commands. This is
3353 used when the settings have already been scoped and we do not
3354 wish to overrule other, higher priority settings. If
3355 AGENDA-BUFFER-NAME is provided, use this as the buffer name for
3356 the agenda to write."
3357 (interactive "FWrite agenda to file: \nP")
3358 (if (or (not (file-writable-p file))
3359 (and (file-exists-p file)
3360 (if (called-interactively-p 'any)
3361 (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
3362 (user-error "Cannot write agenda to file %s" file))
3363 (org-let (if nosettings nil org-agenda-exporter-settings)
3364 '(save-excursion
3365 (save-window-excursion
3366 (let ((bs (copy-sequence (buffer-string)))
3367 (extension (file-name-extension file))
3368 (default-directory (file-name-directory file))
3369 beg content)
3370 (with-temp-buffer
3371 (rename-buffer org-agenda-write-buffer-name t)
3372 (set-buffer-modified-p nil)
3373 (insert bs)
3374 (org-agenda-remove-marked-text 'invisible 'org-filtered)
3375 (run-hooks 'org-agenda-before-write-hook)
3376 (cond
3377 ((bound-and-true-p org-mobile-creating-agendas)
3378 (org-mobile-write-agenda-for-mobile file))
3379 ((string= "org" extension)
3380 (let (content p m message-log-max)
3381 (goto-char (point-min))
3382 (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
3383 (goto-char p)
3384 (setq m (get-text-property (point) 'org-hd-marker))
3385 (when m
3386 (push (save-excursion
3387 (set-buffer (marker-buffer m))
3388 (goto-char m)
3389 (org-copy-subtree 1 nil t t)
3390 org-subtree-clip)
3391 content)))
3392 (find-file file)
3393 (erase-buffer)
3394 (dolist (s content) (org-paste-subtree 1 s))
3395 (write-file file)
3396 (kill-buffer (current-buffer))
3397 (message "Org file written to %s" file)))
3398 ((member extension '("html" "htm"))
3399 (require 'htmlize)
3400 (set-buffer (htmlize-buffer (current-buffer)))
3401 (when org-agenda-export-html-style
3402 ;; replace <style> section with org-agenda-export-html-style
3403 (goto-char (point-min))
3404 (kill-region (- (search-forward "<style") 6)
3405 (search-forward "</style>"))
3406 (insert org-agenda-export-html-style))
3407 (write-file file)
3408 (kill-buffer (current-buffer))
3409 (message "HTML written to %s" file))
3410 ((string= "ps" extension)
3411 (require 'ps-print)
3412 (ps-print-buffer-with-faces file)
3413 (message "Postscript written to %s" file))
3414 ((string= "pdf" extension)
3415 (require 'ps-print)
3416 (ps-print-buffer-with-faces
3417 (concat (file-name-sans-extension file) ".ps"))
3418 (call-process "ps2pdf" nil nil nil
3419 (expand-file-name
3420 (concat (file-name-sans-extension file) ".ps"))
3421 (expand-file-name file))
3422 (delete-file (concat (file-name-sans-extension file) ".ps"))
3423 (message "PDF written to %s" file))
3424 ((string= "ics" extension)
3425 (require 'ox-icalendar)
3426 (org-icalendar-export-current-agenda (expand-file-name file)))
3428 (let ((bs (buffer-string)))
3429 (find-file file)
3430 (erase-buffer)
3431 (insert bs)
3432 (save-buffer 0)
3433 (kill-buffer (current-buffer))
3434 (message "Plain text written to %s" file))))))))
3435 (set-buffer (or agenda-bufname
3436 (and (called-interactively-p 'any) (buffer-name))
3437 org-agenda-buffer-name)))
3438 (when open (org-open-file file)))
3440 (defun org-agenda-remove-marked-text (property &optional value)
3441 "Delete all text marked with VALUE of PROPERTY.
3442 VALUE defaults to t."
3443 (let (beg)
3444 (setq value (or value t))
3445 (while (setq beg (text-property-any (point-min) (point-max)
3446 property value))
3447 (delete-region
3448 beg (or (next-single-property-change beg property)
3449 (point-max))))))
3451 (defun org-agenda-add-entry-text ()
3452 "Add entry text to agenda lines.
3453 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3454 entry text following headings shown in the agenda.
3455 Drawers will be excluded, also the line with scheduling/deadline info."
3456 (when (and (> org-agenda-add-entry-text-maxlines 0)
3457 (not (bound-and-true-p org-mobile-creating-agendas)))
3458 (let (m txt)
3459 (goto-char (point-min))
3460 (while (not (eobp))
3461 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3462 (beginning-of-line 2)
3463 (setq txt (org-agenda-get-some-entry-text
3464 m org-agenda-add-entry-text-maxlines " > "))
3465 (end-of-line 1)
3466 (if (string-match "\\S-" txt)
3467 (insert "\n" txt)
3468 (or (eobp) (forward-char 1))))))))
3470 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3471 &rest keep)
3472 "Extract entry text from MARKER, at most N-LINES lines.
3473 This will ignore drawers etc, just get the text.
3474 If INDENT is given, prefix every line with this string. If KEEP is
3475 given, it is a list of symbols, defining stuff that should not be
3476 removed from the entry content. Currently only `planning' is allowed here."
3477 (let (txt drawer-re kwd-time-re ind)
3478 (save-excursion
3479 (with-current-buffer (marker-buffer marker)
3480 (if (not (derived-mode-p 'org-mode))
3481 (setq txt "")
3482 (org-with-wide-buffer
3483 (goto-char marker)
3484 (end-of-line 1)
3485 (setq txt (buffer-substring
3486 (min (1+ (point)) (point-max))
3487 (progn (outline-next-heading) (point)))
3488 drawer-re org-drawer-regexp
3489 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3490 ".*\n?"))
3491 (with-temp-buffer
3492 (insert txt)
3493 (when org-agenda-add-entry-text-descriptive-links
3494 (goto-char (point-min))
3495 (while (org-activate-bracket-links (point-max))
3496 (add-text-properties (match-beginning 0) (match-end 0)
3497 '(face org-link))))
3498 (goto-char (point-min))
3499 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3500 (set-text-properties (match-beginning 0) (match-end 0)
3501 nil))
3502 (goto-char (point-min))
3503 (while (re-search-forward drawer-re nil t)
3504 (delete-region
3505 (match-beginning 0)
3506 (progn (re-search-forward
3507 "^[ \t]*:END:.*\n?" nil 'move)
3508 (point))))
3509 (unless (member 'planning keep)
3510 (goto-char (point-min))
3511 (while (re-search-forward kwd-time-re nil t)
3512 (replace-match "")))
3513 (goto-char (point-min))
3514 (when org-agenda-entry-text-exclude-regexps
3515 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3516 (while (setq re (pop re-list))
3517 (goto-char (point-min))
3518 (while (re-search-forward re nil t)
3519 (replace-match "")))))
3520 (goto-char (point-max))
3521 (skip-chars-backward " \t\n")
3522 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3524 ;; find and remove min common indentation
3525 (goto-char (point-min))
3526 (untabify (point-min) (point-max))
3527 (setq ind (org-get-indentation))
3528 (while (not (eobp))
3529 (unless (looking-at "[ \t]*$")
3530 (setq ind (min ind (org-get-indentation))))
3531 (beginning-of-line 2))
3532 (goto-char (point-min))
3533 (while (not (eobp))
3534 (unless (looking-at "[ \t]*$")
3535 (move-to-column ind)
3536 (delete-region (point-at-bol) (point)))
3537 (beginning-of-line 2))
3539 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3541 (goto-char (point-min))
3542 (when indent
3543 (while (and (not (eobp)) (re-search-forward "^" nil t))
3544 (replace-match indent t t)))
3545 (goto-char (point-min))
3546 (while (looking-at "[ \t]*\n") (replace-match ""))
3547 (goto-char (point-max))
3548 (when (> (org-current-line)
3549 n-lines)
3550 (org-goto-line (1+ n-lines))
3551 (backward-char 1))
3552 (setq txt (buffer-substring (point-min) (point))))))))
3553 txt))
3555 (defun org-check-for-org-mode ()
3556 "Make sure current buffer is in org-mode. Error if not."
3557 (or (derived-mode-p 'org-mode)
3558 (error "Cannot execute Org agenda command on buffer in %s"
3559 major-mode)))
3561 ;;; Agenda prepare and finalize
3563 (defvar org-agenda-multi nil) ; dynamically scoped
3564 (defvar org-agenda-pre-window-conf nil)
3565 (defvar org-agenda-columns-active nil)
3566 (defvar org-agenda-name nil)
3567 (defvar org-agenda-tag-filter nil)
3568 (defvar org-agenda-category-filter nil)
3569 (defvar org-agenda-regexp-filter nil)
3570 (defvar org-agenda-effort-filter nil)
3571 (defvar org-agenda-top-headline-filter nil)
3572 (defvar org-agenda-tag-filter-preset nil
3573 "A preset of the tags filter used for secondary agenda filtering.
3574 This must be a list of strings, each string must be a single tag preceded
3575 by \"+\" or \"-\".
3576 This variable should not be set directly, but agenda custom commands can
3577 bind it in the options section. The preset filter is a global property of
3578 the entire agenda view. In a block agenda, it will not work reliably to
3579 define a filter for one of the individual blocks. You need to set it in
3580 the global options and expect it to be applied to the entire view.")
3582 (defvar org-agenda-category-filter-preset nil
3583 "A preset of the category filter used for secondary agenda filtering.
3584 This must be a list of strings, each string must be a single category
3585 preceded by \"+\" or \"-\".
3586 This variable should not be set directly, but agenda custom commands can
3587 bind it in the options section. The preset filter is a global property of
3588 the entire agenda view. In a block agenda, it will not work reliably to
3589 define a filter for one of the individual blocks. You need to set it in
3590 the global options and expect it to be applied to the entire view.")
3592 (defvar org-agenda-regexp-filter-preset nil
3593 "A preset of the regexp filter used for secondary agenda filtering.
3594 This must be a list of strings, each string must be a single regexp
3595 preceded by \"+\" or \"-\".
3596 This variable should not be set directly, but agenda custom commands can
3597 bind it in the options section. The preset filter is a global property of
3598 the entire agenda view. In a block agenda, it will not work reliably to
3599 define a filter for one of the individual blocks. You need to set it in
3600 the global options and expect it to be applied to the entire view.")
3602 (defvar org-agenda-effort-filter-preset nil
3603 "A preset of the effort condition used for secondary agenda filtering.
3604 This must be a list of strings, each string must be a single regexp
3605 preceded by \"+\" or \"-\".
3606 This variable should not be set directly, but agenda custom commands can
3607 bind it in the options section. The preset filter is a global property of
3608 the entire agenda view. In a block agenda, it will not work reliably to
3609 define a filter for one of the individual blocks. You need to set it in
3610 the global options and expect it to be applied to the entire view.")
3612 (defun org-agenda-use-sticky-p ()
3613 "Return non-nil if an agenda buffer named
3614 `org-agenda-buffer-name' exists and should be shown instead of
3615 generating a new one."
3616 (and
3617 ;; turned off by user
3618 org-agenda-sticky
3619 ;; For multi-agenda buffer already exists
3620 (not org-agenda-multi)
3621 ;; buffer found
3622 (get-buffer org-agenda-buffer-name)
3623 ;; C-u parameter is same as last call
3624 (with-current-buffer (get-buffer org-agenda-buffer-name)
3625 (and
3626 (equal current-prefix-arg
3627 org-agenda-last-prefix-arg)
3628 ;; In case user turned stickiness on, while having existing
3629 ;; Agenda buffer active, don't reuse that buffer, because it
3630 ;; does not have org variables local
3631 org-agenda-this-buffer-is-sticky))))
3633 (defun org-agenda-prepare-window (abuf filter-alist)
3634 "Setup agenda buffer in the window.
3635 ABUF is the buffer for the agenda window.
3636 FILTER-ALIST is an alist of filters we need to apply when
3637 `org-agenda-persistent-filter' is non-nil."
3638 (let* ((awin (get-buffer-window abuf)) wconf)
3639 (cond
3640 ((equal (current-buffer) abuf) nil)
3641 (awin (select-window awin))
3642 ((not (setq wconf (current-window-configuration))))
3643 ((eq org-agenda-window-setup 'current-window)
3644 (pop-to-buffer-same-window abuf))
3645 ((eq org-agenda-window-setup 'other-window)
3646 (org-switch-to-buffer-other-window abuf))
3647 ((eq org-agenda-window-setup 'other-frame)
3648 (switch-to-buffer-other-frame abuf))
3649 ((eq org-agenda-window-setup 'only-window)
3650 (delete-other-windows)
3651 (pop-to-buffer-same-window abuf))
3652 ((eq org-agenda-window-setup 'reorganize-frame)
3653 (delete-other-windows)
3654 (org-switch-to-buffer-other-window abuf)))
3655 (setq org-agenda-tag-filter (cdr (assq 'tag filter-alist)))
3656 (setq org-agenda-category-filter (cdr (assq 'cat filter-alist)))
3657 (setq org-agenda-effort-filter (cdr (assq 'effort filter-alist)))
3658 (setq org-agenda-regexp-filter (cdr (assq 're filter-alist)))
3659 ;; Additional test in case agenda is invoked from within agenda
3660 ;; buffer via elisp link.
3661 (unless (equal (current-buffer) abuf)
3662 (pop-to-buffer-same-window abuf))
3663 (setq org-agenda-pre-window-conf
3664 (or wconf org-agenda-pre-window-conf))))
3666 (defun org-agenda-prepare (&optional name)
3667 (let ((filter-alist (if org-agenda-persistent-filter
3668 (with-current-buffer
3669 (get-buffer-create org-agenda-buffer-name)
3670 (list `(tag . ,org-agenda-tag-filter)
3671 `(re . ,org-agenda-regexp-filter)
3672 `(effort . ,org-agenda-effort-filter)
3673 `(cat . ,org-agenda-category-filter))))))
3674 (if (org-agenda-use-sticky-p)
3675 (progn
3676 (put 'org-agenda-tag-filter :preset-filter nil)
3677 (put 'org-agenda-category-filter :preset-filter nil)
3678 (put 'org-agenda-regexp-filter :preset-filter nil)
3679 ;; Popup existing buffer
3680 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name)
3681 filter-alist)
3682 (message "Sticky Agenda buffer, use `r' to refresh")
3683 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3684 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3685 (setq org-todo-keywords-for-agenda nil)
3686 (put 'org-agenda-tag-filter :preset-filter
3687 org-agenda-tag-filter-preset)
3688 (put 'org-agenda-category-filter :preset-filter
3689 org-agenda-category-filter-preset)
3690 (put 'org-agenda-regexp-filter :preset-filter
3691 org-agenda-regexp-filter-preset)
3692 (put 'org-agenda-effort-filter :preset-filter
3693 org-agenda-effort-filter-preset)
3694 (if org-agenda-multi
3695 (progn
3696 (setq buffer-read-only nil)
3697 (goto-char (point-max))
3698 (unless (or (bobp) org-agenda-compact-blocks
3699 (not org-agenda-block-separator))
3700 (insert "\n"
3701 (if (stringp org-agenda-block-separator)
3702 org-agenda-block-separator
3703 (make-string (window-width) org-agenda-block-separator))
3704 "\n"))
3705 (narrow-to-region (point) (point-max)))
3706 (setq org-done-keywords-for-agenda nil)
3707 ;; Setting any org variables that are in org-agenda-local-vars
3708 ;; list need to be done after the prepare call
3709 (org-agenda-prepare-window
3710 (get-buffer-create org-agenda-buffer-name) filter-alist)
3711 (setq buffer-read-only nil)
3712 (org-agenda-reset-markers)
3713 (let ((inhibit-read-only t)) (erase-buffer))
3714 (org-agenda-mode)
3715 (setq org-agenda-buffer (current-buffer))
3716 (setq org-agenda-contributing-files nil)
3717 (setq org-agenda-columns-active nil)
3718 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3719 (setq org-todo-keywords-for-agenda
3720 (org-uniquify org-todo-keywords-for-agenda))
3721 (setq org-done-keywords-for-agenda
3722 (org-uniquify org-done-keywords-for-agenda))
3723 (setq org-agenda-last-prefix-arg current-prefix-arg)
3724 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3725 (and name (not org-agenda-name)
3726 (setq-local org-agenda-name name)))
3727 (setq buffer-read-only nil))))
3729 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3730 (defun org-agenda-finalize ()
3731 "Finishing touch for the agenda buffer, called just before displaying it."
3732 (unless org-agenda-multi
3733 (save-excursion
3734 (let ((inhibit-read-only t))
3735 (goto-char (point-min))
3736 (save-excursion
3737 (while (org-activate-bracket-links (point-max))
3738 (add-text-properties (match-beginning 0) (match-end 0)
3739 '(face org-link))))
3740 (save-excursion
3741 (while (org-activate-plain-links (point-max))
3742 (add-text-properties (match-beginning 0) (match-end 0)
3743 '(face org-link))))
3744 (unless (eq org-agenda-remove-tags t)
3745 (org-agenda-align-tags))
3746 (unless org-agenda-with-colors
3747 (remove-text-properties (point-min) (point-max) '(face nil)))
3748 (if (and (boundp 'org-agenda-overriding-columns-format)
3749 org-agenda-overriding-columns-format)
3750 (setq-local org-agenda-overriding-columns-format
3751 org-agenda-overriding-columns-format))
3752 (if (and (boundp 'org-agenda-view-columns-initially)
3753 org-agenda-view-columns-initially)
3754 (org-agenda-columns))
3755 (when org-agenda-fontify-priorities
3756 (org-agenda-fontify-priorities))
3757 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3758 (org-agenda-dim-blocked-tasks))
3759 (org-agenda-mark-clocking-task)
3760 (when org-agenda-entry-text-mode
3761 (org-agenda-entry-text-hide)
3762 (org-agenda-entry-text-show))
3763 (if (and (functionp 'org-habit-insert-consistency-graphs)
3764 (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
3765 (org-habit-insert-consistency-graphs))
3766 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3767 (unless (or (eq org-agenda-show-inherited-tags 'always)
3768 (and (listp org-agenda-show-inherited-tags)
3769 (memq org-agenda-type org-agenda-show-inherited-tags))
3770 (and (eq org-agenda-show-inherited-tags t)
3771 (or (eq org-agenda-use-tag-inheritance t)
3772 (and (listp org-agenda-use-tag-inheritance)
3773 (not (memq org-agenda-type
3774 org-agenda-use-tag-inheritance))))))
3775 (let (mrk)
3776 (save-excursion
3777 (goto-char (point-min))
3778 (while (equal (forward-line) 0)
3779 (when (setq mrk (get-text-property (point) 'org-hd-marker))
3780 (put-text-property (point-at-bol) (point-at-eol)
3781 'tags (org-with-point-at mrk
3782 (delete-dups
3783 (mapcar 'downcase (org-get-tags-at))))))))))
3784 (run-hooks 'org-agenda-finalize-hook)
3785 (when org-agenda-top-headline-filter
3786 (org-agenda-filter-top-headline-apply
3787 org-agenda-top-headline-filter))
3788 (when org-agenda-tag-filter
3789 (org-agenda-filter-apply org-agenda-tag-filter 'tag t))
3790 (when (get 'org-agenda-tag-filter :preset-filter)
3791 (org-agenda-filter-apply
3792 (get 'org-agenda-tag-filter :preset-filter) 'tag t))
3793 (when org-agenda-category-filter
3794 (org-agenda-filter-apply org-agenda-category-filter 'category))
3795 (when (get 'org-agenda-category-filter :preset-filter)
3796 (org-agenda-filter-apply
3797 (get 'org-agenda-category-filter :preset-filter) 'category))
3798 (when org-agenda-regexp-filter
3799 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
3800 (when (get 'org-agenda-regexp-filter :preset-filter)
3801 (org-agenda-filter-apply
3802 (get 'org-agenda-regexp-filter :preset-filter) 'regexp))
3803 (when org-agenda-effort-filter
3804 (org-agenda-filter-apply org-agenda-effort-filter 'effort))
3805 (when (get 'org-agenda-effort-filter :preset-filter)
3806 (org-agenda-filter-apply
3807 (get 'org-agenda-effort-filter :preset-filter) 'effort))
3808 (add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)))))
3810 (defun org-agenda-mark-clocking-task ()
3811 "Mark the current clock entry in the agenda if it is present."
3812 ;; We need to widen when `org-agenda-finalize' is called from
3813 ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
3814 (when (bound-and-true-p org-clock-current-task)
3815 (save-restriction
3816 (widen)
3817 (org-agenda-unmark-clocking-task)
3818 (when (marker-buffer org-clock-hd-marker)
3819 (save-excursion
3820 (goto-char (point-min))
3821 (let (s ov)
3822 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3823 (goto-char s)
3824 (when (equal (org-get-at-bol 'org-hd-marker)
3825 org-clock-hd-marker)
3826 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3827 (overlay-put ov 'type 'org-agenda-clocking)
3828 (overlay-put ov 'face 'org-agenda-clocking)
3829 (overlay-put ov 'help-echo
3830 "The clock is running in this item")))))))))
3832 (defun org-agenda-unmark-clocking-task ()
3833 "Unmark the current clocking task."
3834 (mapc (lambda (o)
3835 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3836 (delete-overlay o)))
3837 (overlays-in (point-min) (point-max))))
3839 (defun org-agenda-fontify-priorities ()
3840 "Make highest priority lines bold, and lowest italic."
3841 (interactive)
3842 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3843 (delete-overlay o)))
3844 (overlays-in (point-min) (point-max)))
3845 (save-excursion
3846 (let (b e p ov h l)
3847 (goto-char (point-min))
3848 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3849 (setq h (or (get-char-property (point) 'org-highest-priority)
3850 org-highest-priority)
3851 l (or (get-char-property (point) 'org-lowest-priority)
3852 org-lowest-priority)
3853 p (string-to-char (match-string 1))
3854 b (match-beginning 0)
3855 e (if (eq org-agenda-fontify-priorities 'cookies)
3856 (match-end 0)
3857 (point-at-eol))
3858 ov (make-overlay b e))
3859 (overlay-put
3860 ov 'face
3861 (let ((special-face
3862 (cond ((org-face-from-face-or-color
3863 'priority 'org-priority
3864 (cdr (assoc p org-priority-faces))))
3865 ((and (listp org-agenda-fontify-priorities)
3866 (org-face-from-face-or-color
3867 'priority 'org-priority
3868 (cdr (assoc p org-agenda-fontify-priorities)))))
3869 ((equal p l) 'italic)
3870 ((equal p h) 'bold))))
3871 (if special-face (list special-face 'org-priority) 'org-priority)))
3872 (overlay-put ov 'org-type 'org-priority)))))
3874 (defvar org-depend-tag-blocked)
3876 (defun org-agenda-dim-blocked-tasks (&optional invisible)
3877 "Dim currently blocked TODOs in the agenda display.
3878 When INVISIBLE is non-nil, hide currently blocked TODO instead of
3879 dimming them."
3880 (interactive "P")
3881 (when (called-interactively-p 'interactive)
3882 (message "Dim or hide blocked tasks..."))
3883 (dolist (o (overlays-in (point-min) (point-max)))
3884 (when (eq (overlay-get o 'org-type) 'org-blocked-todo)
3885 (delete-overlay o)))
3886 (save-excursion
3887 (let ((inhibit-read-only t)
3888 (org-depend-tag-blocked nil)
3889 org-blocked-by-checkboxes)
3890 (goto-char (point-min))
3891 (while (let ((pos (text-property-not-all
3892 (point) (point-max) 'todo-state nil)))
3893 (when pos (goto-char pos)))
3894 (setq org-blocked-by-checkboxes nil)
3895 (let ((marker (org-get-at-bol 'org-hd-marker)))
3896 (when (and (markerp marker)
3897 (with-current-buffer (marker-buffer marker)
3898 (save-excursion (goto-char marker)
3899 (org-entry-blocked-p))))
3900 ;; Entries blocked by checkboxes cannot be made invisible.
3901 ;; See `org-agenda-dim-blocked-tasks' for details.
3902 (let* ((really-invisible
3903 (and (not org-blocked-by-checkboxes)
3904 (or invisible (eq org-agenda-dim-blocked-tasks
3905 'invisible))))
3906 (ov (make-overlay (if really-invisible (line-end-position 0)
3907 (line-beginning-position))
3908 (line-end-position))))
3909 (if really-invisible (overlay-put ov 'invisible t)
3910 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3911 (overlay-put ov 'org-type 'org-blocked-todo))))
3912 (forward-line))))
3913 (when (called-interactively-p 'interactive)
3914 (message "Dim or hide blocked tasks...done")))
3916 (defvar org-agenda-skip-function nil
3917 "Function to be called at each match during agenda construction.
3918 If this function returns nil, the current match should not be skipped.
3919 Otherwise, the function must return a position from where the search
3920 should be continued.
3921 This may also be a Lisp form, it will be evaluated.
3922 Never set this variable using `setq' or so, because then it will apply
3923 to all future agenda commands. If you do want a global skipping condition,
3924 use the option `org-agenda-skip-function-global' instead.
3925 The correct usage for `org-agenda-skip-function' is to bind it with
3926 `let' to scope it dynamically into the agenda-constructing command.
3927 A good way to set it is through options in `org-agenda-custom-commands'.")
3929 (defun org-agenda-skip ()
3930 "Throw to `:skip' in places that should be skipped.
3931 Also moves point to the end of the skipped region, so that search can
3932 continue from there."
3933 (let ((p (point-at-bol)) to)
3934 (when (or
3935 (save-excursion (goto-char p) (looking-at comment-start-skip))
3936 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3937 (get-text-property p :org-archived)
3938 (org-end-of-subtree t))
3939 (and org-agenda-skip-comment-trees
3940 (get-text-property p :org-comment)
3941 (org-end-of-subtree t))
3942 (and (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3943 (org-agenda-skip-eval org-agenda-skip-function)))
3944 (goto-char to))
3945 (org-in-src-block-p t))
3946 (throw :skip t))))
3948 (defun org-agenda-skip-eval (form)
3949 "If FORM is a function or a list, call (or eval) it and return the result.
3950 `save-excursion' and `save-match-data' are wrapped around the call, so point
3951 and match data are returned to the previous state no matter what these
3952 functions do."
3953 (let (fp)
3954 (and form
3955 (or (setq fp (functionp form))
3956 (consp form))
3957 (save-excursion
3958 (save-match-data
3959 (if fp
3960 (funcall form)
3961 (eval form)))))))
3963 (defvar org-agenda-markers nil
3964 "List of all currently active markers created by `org-agenda'.")
3965 (defvar org-agenda-last-marker-time (float-time)
3966 "Creation time of the last agenda marker.")
3968 (defun org-agenda-new-marker (&optional pos)
3969 "Return a new agenda marker.
3970 Maker is at point, or at POS if non-nil. Org mode keeps a list of
3971 these markers and resets them when they are no longer in use."
3972 (let ((m (copy-marker (or pos (point)) t)))
3973 (setq org-agenda-last-marker-time (float-time))
3974 (if org-agenda-buffer
3975 (with-current-buffer org-agenda-buffer
3976 (push m org-agenda-markers))
3977 (push m org-agenda-markers))
3980 (defun org-agenda-reset-markers ()
3981 "Reset markers created by `org-agenda'."
3982 (while org-agenda-markers
3983 (move-marker (pop org-agenda-markers) nil)))
3985 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3986 "Save relative positions of markers in region.
3987 This check for agenda markers in all agenda buffers currently active."
3988 (dolist (buf (buffer-list))
3989 (with-current-buffer buf
3990 (when (eq major-mode 'org-agenda-mode)
3991 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3992 org-agenda-markers)))))
3994 ;;; Entry text mode
3996 (defun org-agenda-entry-text-show-here ()
3997 "Add some text from the entry as context to the current line."
3998 (let (m txt o)
3999 (setq m (org-get-at-bol 'org-hd-marker))
4000 (unless (marker-buffer m)
4001 (error "No marker points to an entry here"))
4002 (setq txt (concat "\n" (org-no-properties
4003 (org-agenda-get-some-entry-text
4004 m org-agenda-entry-text-maxlines
4005 org-agenda-entry-text-leaders))))
4006 (when (string-match "\\S-" txt)
4007 (setq o (make-overlay (point-at-bol) (point-at-eol)))
4008 (overlay-put o 'evaporate t)
4009 (overlay-put o 'org-overlay-type 'agenda-entry-content)
4010 (overlay-put o 'after-string txt))))
4012 (defun org-agenda-entry-text-show ()
4013 "Add entry context for all agenda lines."
4014 (interactive)
4015 (save-excursion
4016 (goto-char (point-max))
4017 (beginning-of-line 1)
4018 (while (not (bobp))
4019 (when (org-get-at-bol 'org-hd-marker)
4020 (org-agenda-entry-text-show-here))
4021 (beginning-of-line 0))))
4023 (defun org-agenda-entry-text-hide ()
4024 "Remove any shown entry context."
4025 (delq nil
4026 (mapcar (lambda (o)
4027 (if (eq (overlay-get o 'org-overlay-type)
4028 'agenda-entry-content)
4029 (progn (delete-overlay o) t)))
4030 (overlays-in (point-min) (point-max)))))
4032 (defun org-agenda-get-day-face (date)
4033 "Return the face DATE should be displayed with."
4034 (cond ((and (functionp org-agenda-day-face-function)
4035 (funcall org-agenda-day-face-function date)))
4036 ((org-agenda-today-p date) 'org-agenda-date-today)
4037 ((memq (calendar-day-of-week date) org-agenda-weekend-days)
4038 'org-agenda-date-weekend)
4039 (t 'org-agenda-date)))
4041 (defvar org-agenda-show-log-scoped)
4043 ;;; Agenda Daily/Weekly
4045 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4046 "Start day for the agenda view.
4047 Custom commands can set this variable in the options section.
4048 This is usually a string like \"2007-11-01\", \"+2d\" or any other
4049 input allowed when reading a date through the Org calendar.
4050 See the docstring of `org-read-date' for details.")
4051 (defvar org-starting-day nil) ; local variable in the agenda buffer
4052 (defvar org-arg-loc nil) ; local variable
4054 (defvar org-agenda-buffer-tmp-name nil)
4055 ;;;###autoload
4056 (defun org-agenda-list (&optional arg start-day span with-hour)
4057 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4058 The view will be for the current day or week, but from the overview buffer
4059 you will be able to go to other days/weeks.
4061 With a numeric prefix argument in an interactive call, the agenda will
4062 span ARG days. Lisp programs should instead specify SPAN to change
4063 the number of days. SPAN defaults to `org-agenda-span'.
4065 START-DAY defaults to TODAY, or to the most recent match for the weekday
4066 given in `org-agenda-start-on-weekday'.
4068 When WITH-HOUR is non-nil, only include scheduled and deadline
4069 items if they have an hour specification like [h]h:mm."
4070 (interactive "P")
4071 (if org-agenda-overriding-arguments
4072 (setq arg (car org-agenda-overriding-arguments)
4073 start-day (nth 1 org-agenda-overriding-arguments)
4074 span (nth 2 org-agenda-overriding-arguments)))
4075 (if (and (integerp arg) (> arg 0))
4076 (setq span arg arg nil))
4077 (catch 'exit
4078 (setq org-agenda-buffer-name
4079 (or org-agenda-buffer-tmp-name
4080 (and org-agenda-doing-sticky-redo org-agenda-buffer-name)
4081 (if org-agenda-sticky
4082 (cond ((and org-keys (stringp org-match))
4083 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4084 (org-keys
4085 (format "*Org Agenda(%s)*" org-keys))
4086 (t "*Org Agenda(a)*")))
4087 "*Org Agenda*"))
4088 (org-agenda-prepare "Day/Week")
4089 (setq start-day (or start-day org-agenda-start-day))
4090 (if (stringp start-day)
4091 ;; Convert to an absolute day number
4092 (setq start-day (time-to-days (org-read-date nil t start-day))))
4093 (org-compile-prefix-format 'agenda)
4094 (org-set-sorting-strategy 'agenda)
4095 (let* ((span (org-agenda-ndays-to-span (or span org-agenda-span)))
4096 (today (org-today))
4097 (sd (or start-day today))
4098 (ndays (org-agenda-span-to-ndays span sd))
4099 (org-agenda-start-on-weekday
4100 (if (or (eq ndays 7) (eq ndays 14))
4101 org-agenda-start-on-weekday))
4102 (thefiles (org-agenda-files nil 'ifmode))
4103 (files thefiles)
4104 (start (if (or (null org-agenda-start-on-weekday)
4105 (< ndays 7))
4107 (let* ((nt (calendar-day-of-week
4108 (calendar-gregorian-from-absolute sd)))
4109 (n1 org-agenda-start-on-weekday)
4110 (d (- nt n1)))
4111 (- sd (+ (if (< d 0) 7 0) d)))))
4112 (day-numbers (list start))
4113 (day-cnt 0)
4114 (inhibit-redisplay (not debug-on-error))
4115 (org-agenda-show-log-scoped org-agenda-show-log)
4116 s e rtn rtnall file date d start-pos end-pos todayp
4117 clocktable-start clocktable-end filter)
4118 (setq org-agenda-redo-command
4119 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4120 (dotimes (n (1- ndays))
4121 (push (1+ (car day-numbers)) day-numbers))
4122 (setq day-numbers (nreverse day-numbers))
4123 (setq clocktable-start (car day-numbers)
4124 clocktable-end (1+ (or (org-last day-numbers) 0)))
4125 (setq-local org-starting-day (car day-numbers))
4126 (setq-local org-arg-loc arg)
4127 (setq-local org-agenda-current-span (org-agenda-ndays-to-span span))
4128 (unless org-agenda-compact-blocks
4129 (let* ((d1 (car day-numbers))
4130 (d2 (org-last day-numbers))
4131 (w1 (org-days-to-iso-week d1))
4132 (w2 (org-days-to-iso-week d2)))
4133 (setq s (point))
4134 (if org-agenda-overriding-header
4135 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4136 nil 'face 'org-agenda-structure) "\n")
4137 (insert (org-agenda-span-name span)
4138 "-agenda"
4139 (if (< (- d2 d1) 350)
4140 (if (= w1 w2)
4141 (format " (W%02d)" w1)
4142 (format " (W%02d-W%02d)" w1 w2))
4144 ":\n")))
4145 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4146 'org-date-line t))
4147 (org-agenda-mark-header-line s))
4148 (while (setq d (pop day-numbers))
4149 (setq date (calendar-gregorian-from-absolute d)
4150 s (point))
4151 (if (or (setq todayp (= d today))
4152 (and (not start-pos) (= d sd)))
4153 (setq start-pos (point))
4154 (if (and start-pos (not end-pos))
4155 (setq end-pos (point))))
4156 (setq files thefiles
4157 rtnall nil)
4158 (while (setq file (pop files))
4159 (catch 'nextfile
4160 (org-check-agenda-file file)
4161 (let ((org-agenda-entry-types org-agenda-entry-types))
4162 ;; Starred types override non-starred equivalents
4163 (when (member :deadline* org-agenda-entry-types)
4164 (setq org-agenda-entry-types
4165 (delq :deadline org-agenda-entry-types)))
4166 (when (member :scheduled* org-agenda-entry-types)
4167 (setq org-agenda-entry-types
4168 (delq :scheduled org-agenda-entry-types)))
4169 ;; Honor with-hour
4170 (when with-hour
4171 (when (member :deadline org-agenda-entry-types)
4172 (setq org-agenda-entry-types
4173 (delq :deadline org-agenda-entry-types))
4174 (push :deadline* org-agenda-entry-types))
4175 (when (member :scheduled org-agenda-entry-types)
4176 (setq org-agenda-entry-types
4177 (delq :scheduled org-agenda-entry-types))
4178 (push :scheduled* org-agenda-entry-types)))
4179 (unless org-agenda-include-deadlines
4180 (setq org-agenda-entry-types
4181 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4182 (cond
4183 ((memq org-agenda-show-log-scoped '(only clockcheck))
4184 (setq rtn (org-agenda-get-day-entries
4185 file date :closed)))
4186 (org-agenda-show-log-scoped
4187 (setq rtn (apply 'org-agenda-get-day-entries
4188 file date
4189 (append '(:closed) org-agenda-entry-types))))
4191 (setq rtn (apply 'org-agenda-get-day-entries
4192 file date
4193 org-agenda-entry-types)))))
4194 (setq rtnall (append rtnall rtn)))) ;; all entries
4195 (if org-agenda-include-diary
4196 (let ((org-agenda-search-headline-for-time t))
4197 (require 'diary-lib)
4198 (setq rtn (org-get-entries-from-diary date))
4199 (setq rtnall (append rtnall rtn))))
4200 (if (or rtnall org-agenda-show-all-dates)
4201 (progn
4202 (setq day-cnt (1+ day-cnt))
4203 (insert
4204 (if (stringp org-agenda-format-date)
4205 (format-time-string org-agenda-format-date
4206 (org-time-from-absolute date))
4207 (funcall org-agenda-format-date date))
4208 "\n")
4209 (put-text-property s (1- (point)) 'face
4210 (org-agenda-get-day-face date))
4211 (put-text-property s (1- (point)) 'org-date-line t)
4212 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4213 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4214 (when todayp
4215 (put-text-property s (1- (point)) 'org-today t))
4216 (setq rtnall
4217 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4218 (if rtnall (insert ;; all entries
4219 (org-agenda-finalize-entries rtnall 'agenda)
4220 "\n"))
4221 (put-text-property s (1- (point)) 'day d)
4222 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4223 (when (and org-agenda-clockreport-mode clocktable-start)
4224 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4225 ;; the above line is to ensure the restricted range!
4226 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4227 tbl)
4228 (setq p (org-plist-delete p :block))
4229 (setq p (plist-put p :tstart clocktable-start))
4230 (setq p (plist-put p :tend clocktable-end))
4231 (setq p (plist-put p :scope 'agenda))
4232 (setq tbl (apply 'org-clock-get-clocktable p))
4233 (insert tbl)))
4234 (goto-char (point-min))
4235 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4236 (unless (and (pos-visible-in-window-p (point-min))
4237 (pos-visible-in-window-p (point-max)))
4238 (goto-char (1- (point-max)))
4239 (recenter -1)
4240 (if (not (pos-visible-in-window-p (or start-pos 1)))
4241 (progn
4242 (goto-char (or start-pos 1))
4243 (recenter 1))))
4244 (goto-char (or start-pos 1))
4245 (add-text-properties (point-min) (point-max)
4246 `(org-agenda-type agenda
4247 org-last-args (,arg ,start-day ,span)
4248 org-redo-cmd ,org-agenda-redo-command
4249 org-series-cmd ,org-cmd))
4250 (if (eq org-agenda-show-log-scoped 'clockcheck)
4251 (org-agenda-show-clocking-issues))
4252 (org-agenda-finalize)
4253 (setq buffer-read-only t)
4254 (message ""))))
4256 (defun org-agenda-ndays-to-span (n)
4257 "Return a span symbol for a span of N days, or N if none matches."
4258 (cond ((symbolp n) n)
4259 ((= n 1) 'day)
4260 ((= n 7) 'week)
4261 ((= n 14) 'fortnight)
4262 (t n)))
4264 (defun org-agenda-span-to-ndays (span &optional start-day)
4265 "Return ndays from SPAN, possibly starting at START-DAY.
4266 START-DAY is an absolute time value."
4267 (cond ((numberp span) span)
4268 ((eq span 'day) 1)
4269 ((eq span 'week) 7)
4270 ((eq span 'fortnight) 14)
4271 ((eq span 'month)
4272 (let ((date (calendar-gregorian-from-absolute start-day)))
4273 (calendar-last-day-of-month (car date) (cl-caddr date))))
4274 ((eq span 'year)
4275 (let ((date (calendar-gregorian-from-absolute start-day)))
4276 (if (calendar-leap-year-p (cl-caddr date)) 366 365)))))
4278 (defun org-agenda-span-name (span)
4279 "Return a SPAN name."
4280 (if (null span)
4282 (if (symbolp span)
4283 (capitalize (symbol-name span))
4284 (format "%d days" span))))
4286 ;;; Agenda word search
4288 (defvar org-agenda-search-history nil)
4290 (defvar org-search-syntax-table nil
4291 "Special syntax table for Org search.
4292 In this table, we have single quotes not as word constituents, to
4293 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4295 (defvar org-mode-syntax-table) ; From org.el
4296 (defun org-search-syntax-table ()
4297 (unless org-search-syntax-table
4298 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4299 (modify-syntax-entry ?' "." org-search-syntax-table)
4300 (modify-syntax-entry ?` "." org-search-syntax-table))
4301 org-search-syntax-table)
4303 (defvar org-agenda-last-search-view-search-was-boolean nil)
4305 ;;;###autoload
4306 (defun org-search-view (&optional todo-only string edit-at)
4307 "Show all entries that contain a phrase or words or regular expressions.
4309 With optional prefix argument TODO-ONLY, only consider entries that are
4310 TODO entries. The argument STRING can be used to pass a default search
4311 string into this function. If EDIT-AT is non-nil, it means that the
4312 user should get a chance to edit this string, with cursor at position
4313 EDIT-AT.
4315 The search string can be viewed either as a phrase that should be found as
4316 is, or it can be broken into a number of snippets, each of which must match
4317 in a Boolean way to select an entry. The default depends on the variable
4318 `org-agenda-search-view-always-boolean'.
4319 Even if this is turned off (the default) you can always switch to
4320 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4322 The default is a direct search of the whole phrase, where each space in
4323 the search string can expand to an arbitrary amount of whitespace,
4324 including newlines.
4326 If using a Boolean search, the search string is split on whitespace and
4327 each snippet is searched separately, with logical AND to select an entry.
4328 Words prefixed with a minus must *not* occur in the entry. Words without
4329 a prefix or prefixed with a plus must occur in the entry. Matching is
4330 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4331 match whole words, not parts of a word) if
4332 `org-agenda-search-view-force-full-words' is set (default is nil).
4334 Boolean search snippets enclosed by curly braces are interpreted as
4335 regular expressions that must or (when preceded with \"-\") must not
4336 match in the entry. Snippets enclosed into double quotes will be taken
4337 as a whole, to include whitespace.
4339 - If the search string starts with an asterisk, search only in headlines.
4340 - If (possibly after the leading star) the search string starts with an
4341 exclamation mark, this also means to look at TODO entries only, an effect
4342 that can also be achieved with a prefix argument.
4343 - If (possibly after star and exclamation mark) the search string starts
4344 with a colon, this will mean that the (non-regexp) snippets of the
4345 Boolean search must match as full words.
4347 This command searches the agenda files, and in addition the files listed
4348 in `org-agenda-text-search-extra-files'."
4349 (interactive "P")
4350 (if org-agenda-overriding-arguments
4351 (setq todo-only (car org-agenda-overriding-arguments)
4352 string (nth 1 org-agenda-overriding-arguments)
4353 edit-at (nth 2 org-agenda-overriding-arguments)))
4354 (let* ((props (list 'face nil
4355 'done-face 'org-agenda-done
4356 'org-not-done-regexp org-not-done-regexp
4357 'org-todo-regexp org-todo-regexp
4358 'org-complex-heading-regexp org-complex-heading-regexp
4359 'mouse-face 'highlight
4360 'help-echo (format "mouse-2 or RET jump to location")))
4361 (full-words org-agenda-search-view-force-full-words)
4362 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4363 regexp rtn rtnall files file pos inherited-tags
4364 marker category level tags c neg re boolean
4365 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4366 (unless (and (not edit-at)
4367 (stringp string)
4368 (string-match "\\S-" string))
4369 (setq string (read-string
4370 (if org-agenda-search-view-always-boolean
4371 "[+-]Word/{Regexp} ...: "
4372 "Phrase or [+-]Word/{Regexp} ...: ")
4373 (cond
4374 ((integerp edit-at) (cons string edit-at))
4375 (edit-at string))
4376 'org-agenda-search-history)))
4377 (catch 'exit
4378 (if org-agenda-sticky
4379 (setq org-agenda-buffer-name
4380 (if (stringp string)
4381 (format "*Org Agenda(%s:%s)*"
4382 (or org-keys (or (and todo-only "S") "s")) string)
4383 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4384 (org-agenda-prepare "SEARCH")
4385 (org-compile-prefix-format 'search)
4386 (org-set-sorting-strategy 'search)
4387 (setq org-agenda-redo-command
4388 (list 'org-search-view (if todo-only t nil)
4389 (list 'if 'current-prefix-arg nil string)))
4390 (setq org-agenda-query-string string)
4391 (if (equal (string-to-char string) ?*)
4392 (setq hdl-only t
4393 words (substring string 1))
4394 (setq words string))
4395 (when (equal (string-to-char words) ?!)
4396 (setq todo-only t
4397 words (substring words 1)))
4398 (when (equal (string-to-char words) ?:)
4399 (setq full-words t
4400 words (substring words 1)))
4401 (if (or org-agenda-search-view-always-boolean
4402 (member (string-to-char words) '(?- ?+ ?\{)))
4403 (setq boolean t))
4404 (setq words (org-split-string words))
4405 (let (www w)
4406 (while (setq w (pop words))
4407 (while (and (string-match "\\\\\\'" w) words)
4408 (setq w (concat (substring w 0 -1) " " (pop words))))
4409 (push w www))
4410 (setq words (nreverse www) www nil)
4411 (while (setq w (pop words))
4412 (when (and (string-match "\\`[-+]?{" w)
4413 (not (string-match "}\\'" w)))
4414 (while (and words (not (string-match "}\\'" (car words))))
4415 (setq w (concat w " " (pop words))))
4416 (setq w (concat w " " (pop words))))
4417 (push w www))
4418 (setq words (nreverse www)))
4419 (setq org-agenda-last-search-view-search-was-boolean boolean)
4420 (when boolean
4421 (let (wds w)
4422 (while (setq w (pop words))
4423 (if (or (equal (substring w 0 1) "\"")
4424 (and (> (length w) 1)
4425 (member (substring w 0 1) '("+" "-"))
4426 (equal (substring w 1 2) "\"")))
4427 (while (and words (not (equal (substring w -1) "\"")))
4428 (setq w (concat w " " (pop words)))))
4429 (and (string-match "\\`\\([-+]?\\)\"" w)
4430 (setq w (replace-match "\\1" nil nil w)))
4431 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4432 (push w wds))
4433 (setq words (nreverse wds))))
4434 (if boolean
4435 (mapc (lambda (w)
4436 (setq c (string-to-char w))
4437 (if (equal c ?-)
4438 (setq neg t w (substring w 1))
4439 (if (equal c ?+)
4440 (setq neg nil w (substring w 1))
4441 (setq neg nil)))
4442 (if (string-match "\\`{.*}\\'" w)
4443 (setq re (substring w 1 -1))
4444 (if full-words
4445 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4446 (setq re (regexp-quote (downcase w)))))
4447 (if neg (push re regexps-) (push re regexps+)))
4448 words)
4449 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4450 regexps+))
4451 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4452 (if (not regexps+)
4453 (setq regexp org-outline-regexp-bol)
4454 (setq regexp (pop regexps+))
4455 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4456 regexp))))
4457 (setq files (org-agenda-files nil 'ifmode))
4458 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4459 (pop org-agenda-text-search-extra-files)
4460 (setq files (org-add-archive-files files)))
4461 (setq files (append files org-agenda-text-search-extra-files)
4462 rtnall nil)
4463 (while (setq file (pop files))
4464 (setq ee nil)
4465 (catch 'nextfile
4466 (org-check-agenda-file file)
4467 (setq buffer (if (file-exists-p file)
4468 (org-get-agenda-file-buffer file)
4469 (error "No such file %s" file)))
4470 (if (not buffer)
4471 ;; If file does not exist, make sure an error message is sent
4472 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4473 file))))
4474 (with-current-buffer buffer
4475 (with-syntax-table (org-search-syntax-table)
4476 (unless (derived-mode-p 'org-mode)
4477 (error "Agenda file %s is not in `org-mode'" file))
4478 (let ((case-fold-search t))
4479 (save-excursion
4480 (save-restriction
4481 (if (eq buffer org-agenda-restrict)
4482 (narrow-to-region org-agenda-restrict-begin
4483 org-agenda-restrict-end)
4484 (widen))
4485 (goto-char (point-min))
4486 (unless (or (org-at-heading-p)
4487 (outline-next-heading))
4488 (throw 'nextfile t))
4489 (goto-char (max (point-min) (1- (point))))
4490 (while (re-search-forward regexp nil t)
4491 (org-back-to-heading t)
4492 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4493 (> (org-reduced-level (org-outline-level))
4494 org-agenda-search-view-max-outline-level)
4495 (forward-line -1)
4496 (org-back-to-heading t)))
4497 (skip-chars-forward "* ")
4498 (setq beg (point-at-bol)
4499 beg1 (point)
4500 end (progn
4501 (outline-next-heading)
4502 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4503 (> (org-reduced-level (org-outline-level))
4504 org-agenda-search-view-max-outline-level)
4505 (forward-line 1)
4506 (outline-next-heading)))
4507 (point)))
4509 (catch :skip
4510 (goto-char beg)
4511 (org-agenda-skip)
4512 (setq str (buffer-substring-no-properties
4513 (point-at-bol)
4514 (if hdl-only (point-at-eol) end)))
4515 (mapc (lambda (wr) (when (string-match wr str)
4516 (goto-char (1- end))
4517 (throw :skip t)))
4518 regexps-)
4519 (mapc (lambda (wr) (unless (string-match wr str)
4520 (goto-char (1- end))
4521 (throw :skip t)))
4522 (if todo-only
4523 (cons (concat "^\\*+[ \t]+"
4524 org-not-done-regexp)
4525 regexps+)
4526 regexps+))
4527 (goto-char beg)
4528 (setq marker (org-agenda-new-marker (point))
4529 category (org-get-category)
4530 level (make-string (org-reduced-level (org-outline-level)) ? )
4531 inherited-tags
4532 (or (eq org-agenda-show-inherited-tags 'always)
4533 (and (listp org-agenda-show-inherited-tags)
4534 (memq 'todo org-agenda-show-inherited-tags))
4535 (and (eq org-agenda-show-inherited-tags t)
4536 (or (eq org-agenda-use-tag-inheritance t)
4537 (memq 'todo org-agenda-use-tag-inheritance))))
4538 tags (org-get-tags-at nil (not inherited-tags))
4539 txt (org-agenda-format-item
4541 (buffer-substring-no-properties
4542 beg1 (point-at-eol))
4543 level category tags t))
4544 (org-add-props txt props
4545 'org-marker marker 'org-hd-marker marker
4546 'org-todo-regexp org-todo-regexp
4547 'level level
4548 'org-complex-heading-regexp org-complex-heading-regexp
4549 'priority 1000
4550 'type "search")
4551 (push txt ee)
4552 (goto-char (1- end))))))))))
4553 (setq rtn (nreverse ee))
4554 (setq rtnall (append rtnall rtn)))
4555 (if org-agenda-overriding-header
4556 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4557 nil 'face 'org-agenda-structure) "\n")
4558 (insert "Search words: ")
4559 (add-text-properties (point-min) (1- (point))
4560 (list 'face 'org-agenda-structure))
4561 (setq pos (point))
4562 (insert string "\n")
4563 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4564 (setq pos (point))
4565 (unless org-agenda-multi
4566 (insert (substitute-command-keys "\
4567 Press `\\[org-agenda-manipulate-query-add]', \
4568 `\\[org-agenda-manipulate-query-subtract]' to add/sub word, \
4569 `\\[org-agenda-manipulate-query-add-re]', \
4570 `\\[org-agenda-manipulate-query-subtract-re]' to add/sub regexp, \
4571 `\\[universal-argument] \\[org-agenda-redo]' to edit\n"))
4572 (add-text-properties pos (1- (point))
4573 (list 'face 'org-agenda-structure))))
4574 (org-agenda-mark-header-line (point-min))
4575 (when rtnall
4576 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4577 (goto-char (point-min))
4578 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4579 (add-text-properties (point-min) (point-max)
4580 `(org-agenda-type search
4581 org-last-args (,todo-only ,string ,edit-at)
4582 org-redo-cmd ,org-agenda-redo-command
4583 org-series-cmd ,org-cmd))
4584 (org-agenda-finalize)
4585 (setq buffer-read-only t))))
4587 ;;; Agenda TODO list
4589 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4590 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4591 (concat
4592 (if (or (equal keywords "ALL") (not keywords))
4593 (propertize "ALL" 'face 'warning)
4594 (mapconcat
4595 (lambda (kw)
4596 (propertize kw 'face (org-get-todo-face kw)))
4597 (org-split-string keywords "|")
4598 "|"))
4599 "\n"))
4601 (defvar org-select-this-todo-keyword nil)
4602 (defvar org-last-arg nil)
4604 ;;;###autoload
4605 (defun org-todo-list (&optional arg)
4606 "Show all (not done) TODO entries from all agenda file in a single list.
4607 The prefix arg can be used to select a specific TODO keyword and limit
4608 the list to these. When using `\\[universal-argument]', you will be prompted
4609 for a keyword. A numeric prefix directly selects the Nth keyword in
4610 `org-todo-keywords-1'."
4611 (interactive "P")
4612 (if org-agenda-overriding-arguments
4613 (setq arg org-agenda-overriding-arguments))
4614 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4615 (let* ((today (org-today))
4616 (date (calendar-gregorian-from-absolute today))
4617 (kwds org-todo-keywords-for-agenda)
4618 (completion-ignore-case t)
4619 (org-select-this-todo-keyword
4620 (if (stringp arg) arg
4621 (and arg (integerp arg) (> arg 0)
4622 (nth (1- arg) kwds))))
4623 rtn rtnall files file pos)
4624 (when (equal arg '(4))
4625 (setq org-select-this-todo-keyword
4626 (completing-read "Keyword (or KWD1|K2D2|...): "
4627 (mapcar #'list kwds) nil nil)))
4628 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4629 (catch 'exit
4630 (if org-agenda-sticky
4631 (setq org-agenda-buffer-name
4632 (if (stringp org-select-this-todo-keyword)
4633 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4634 org-select-this-todo-keyword)
4635 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4636 (org-agenda-prepare "TODO")
4637 (org-compile-prefix-format 'todo)
4638 (org-set-sorting-strategy 'todo)
4639 (setq org-agenda-redo-command
4640 `(org-todo-list (or (and (numberp current-prefix-arg)
4641 current-prefix-arg)
4642 ,org-select-this-todo-keyword
4643 current-prefix-arg ,arg)))
4644 (setq files (org-agenda-files nil 'ifmode)
4645 rtnall nil)
4646 (while (setq file (pop files))
4647 (catch 'nextfile
4648 (org-check-agenda-file file)
4649 (setq rtn (org-agenda-get-day-entries file date :todo))
4650 (setq rtnall (append rtnall rtn))))
4651 (if org-agenda-overriding-header
4652 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4653 nil 'face 'org-agenda-structure) "\n")
4654 (insert "Global list of TODO items of type: ")
4655 (add-text-properties (point-min) (1- (point))
4656 (list 'face 'org-agenda-structure
4657 'short-heading
4658 (concat "ToDo: "
4659 (or org-select-this-todo-keyword "ALL"))))
4660 (org-agenda-mark-header-line (point-min))
4661 (insert (org-agenda-propertize-selected-todo-keywords
4662 org-select-this-todo-keyword))
4663 (setq pos (point))
4664 (unless org-agenda-multi
4665 (insert (substitute-command-keys "Available with \
4666 `N \\[org-agenda-redo]': (0)[ALL]"))
4667 (let ((n 0) s)
4668 (mapc (lambda (x)
4669 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4670 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4671 (insert "\n "))
4672 (insert " " s))
4673 kwds))
4674 (insert "\n"))
4675 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4676 (org-agenda-mark-header-line (point-min))
4677 (when rtnall
4678 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4679 (goto-char (point-min))
4680 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4681 (add-text-properties (point-min) (point-max)
4682 `(org-agenda-type todo
4683 org-last-args ,arg
4684 org-redo-cmd ,org-agenda-redo-command
4685 org-series-cmd ,org-cmd))
4686 (org-agenda-finalize)
4687 (setq buffer-read-only t))))
4689 ;;; Agenda tags match
4691 ;;;###autoload
4692 (defun org-tags-view (&optional todo-only match)
4693 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4694 The prefix arg TODO-ONLY limits the search to TODO entries."
4695 (interactive "P")
4696 (if org-agenda-overriding-arguments
4697 (setq todo-only (car org-agenda-overriding-arguments)
4698 match (nth 1 org-agenda-overriding-arguments)))
4699 (let* ((org-tags-match-list-sublevels
4700 org-tags-match-list-sublevels)
4701 (completion-ignore-case t)
4702 rtn rtnall files file pos matcher
4703 buffer)
4704 (when (and (stringp match) (not (string-match "\\S-" match)))
4705 (setq match nil))
4706 (catch 'exit
4707 (if org-agenda-sticky
4708 (setq org-agenda-buffer-name
4709 (if (stringp match)
4710 (format "*Org Agenda(%s:%s)*"
4711 (or org-keys (or (and todo-only "M") "m")) match)
4712 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4713 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4714 ;; expanding tags within `org-make-tags-matcher'
4715 (org-agenda-prepare (concat "TAGS " match))
4716 (setq org--matcher-tags-todo-only todo-only
4717 matcher (org-make-tags-matcher match)
4718 match (car matcher)
4719 matcher (cdr matcher))
4720 (org-compile-prefix-format 'tags)
4721 (org-set-sorting-strategy 'tags)
4722 (setq org-agenda-query-string match)
4723 (setq org-agenda-redo-command
4724 (list 'org-tags-view
4725 `(quote ,org--matcher-tags-todo-only)
4726 `(if current-prefix-arg nil ,org-agenda-query-string)))
4727 (setq files (org-agenda-files nil 'ifmode)
4728 rtnall nil)
4729 (while (setq file (pop files))
4730 (catch 'nextfile
4731 (org-check-agenda-file file)
4732 (setq buffer (if (file-exists-p file)
4733 (org-get-agenda-file-buffer file)
4734 (error "No such file %s" file)))
4735 (if (not buffer)
4736 ;; If file does not exist, error message to agenda
4737 (setq rtn (list
4738 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4739 rtnall (append rtnall rtn))
4740 (with-current-buffer buffer
4741 (unless (derived-mode-p 'org-mode)
4742 (error "Agenda file %s is not in `org-mode'" file))
4743 (save-excursion
4744 (save-restriction
4745 (if (eq buffer org-agenda-restrict)
4746 (narrow-to-region org-agenda-restrict-begin
4747 org-agenda-restrict-end)
4748 (widen))
4749 (setq rtn (org-scan-tags 'agenda
4750 matcher
4751 org--matcher-tags-todo-only))
4752 (setq rtnall (append rtnall rtn))))))))
4753 (if org-agenda-overriding-header
4754 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4755 nil 'face 'org-agenda-structure) "\n")
4756 (insert "Headlines with TAGS match: ")
4757 (add-text-properties (point-min) (1- (point))
4758 (list 'face 'org-agenda-structure
4759 'short-heading
4760 (concat "Match: " match)))
4761 (setq pos (point))
4762 (insert match "\n")
4763 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4764 (setq pos (point))
4765 (unless org-agenda-multi
4766 (insert (substitute-command-keys
4767 "Press `\\[universal-argument] \\[org-agenda-redo]' \
4768 to search again with new search string\n")))
4769 (add-text-properties pos (1- (point))
4770 (list 'face 'org-agenda-structure)))
4771 (org-agenda-mark-header-line (point-min))
4772 (when rtnall
4773 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4774 (goto-char (point-min))
4775 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4776 (add-text-properties
4777 (point-min) (point-max)
4778 `(org-agenda-type tags
4779 org-last-args (,org--matcher-tags-todo-only ,match)
4780 org-redo-cmd ,org-agenda-redo-command
4781 org-series-cmd ,org-cmd))
4782 (org-agenda-finalize)
4783 (setq buffer-read-only t))))
4785 ;;; Agenda Finding stuck projects
4787 (defvar org-agenda-skip-regexp nil
4788 "Regular expression used in skipping subtrees for the agenda.
4789 This is basically a temporary global variable that can be set and then
4790 used by user-defined selections using `org-agenda-skip-function'.")
4792 (defvar org-agenda-overriding-header nil
4793 "When set during agenda, todo and tags searches it replaces the header.
4794 This variable should not be set directly, but custom commands can bind it
4795 in the options section.")
4797 (defun org-agenda-skip-entry-if (&rest conditions)
4798 "Skip entry if any of CONDITIONS is true.
4799 See `org-agenda-skip-if' for details."
4800 (org-agenda-skip-if nil conditions))
4802 (defun org-agenda-skip-subtree-if (&rest conditions)
4803 "Skip subtree if any of CONDITIONS is true.
4804 See `org-agenda-skip-if' for details."
4805 (org-agenda-skip-if t conditions))
4807 (defun org-agenda-skip-if (subtree conditions)
4808 "Checks current entity for CONDITIONS.
4809 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4810 the entry (i.e. the text before the next heading) is checked.
4812 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4813 from different tests. Valid conditions are:
4815 scheduled Check if there is a scheduled cookie
4816 notscheduled Check if there is no scheduled cookie
4817 deadline Check if there is a deadline
4818 notdeadline Check if there is no deadline
4819 timestamp Check if there is a timestamp (also deadline or scheduled)
4820 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4821 regexp Check if regexp matches
4822 notregexp Check if regexp does not match.
4823 todo Check if TODO keyword matches
4824 nottodo Check if TODO keyword does not match
4826 The regexp is taken from the conditions list, it must come right after
4827 the `regexp' or `notregexp' element.
4829 `todo' and `nottodo' accept as an argument a list of todo
4830 keywords, which may include \"*\" to match any todo keyword.
4832 (org-agenda-skip-entry-if \\='todo \\='(\"TODO\" \"WAITING\"))
4834 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4836 Instead of a list, a keyword class may be given. For example:
4838 (org-agenda-skip-entry-if \\='nottodo \\='done)
4840 would skip entries that haven't been marked with any of \"DONE\"
4841 keywords. Possible classes are: `todo', `done', `any'.
4843 If any of these conditions is met, this function returns the end point of
4844 the entity, causing the search to continue from there. This is a function
4845 that can be put into `org-agenda-skip-function' for the duration of a command."
4846 (let (beg end m)
4847 (org-back-to-heading t)
4848 (setq beg (point)
4849 end (if subtree
4850 (progn (org-end-of-subtree t) (point))
4851 (progn (outline-next-heading) (1- (point)))))
4852 (goto-char beg)
4853 (and
4855 (and (memq 'scheduled conditions)
4856 (re-search-forward org-scheduled-time-regexp end t))
4857 (and (memq 'notscheduled conditions)
4858 (not (re-search-forward org-scheduled-time-regexp end t)))
4859 (and (memq 'deadline conditions)
4860 (re-search-forward org-deadline-time-regexp end t))
4861 (and (memq 'notdeadline conditions)
4862 (not (re-search-forward org-deadline-time-regexp end t)))
4863 (and (memq 'timestamp conditions)
4864 (re-search-forward org-ts-regexp end t))
4865 (and (memq 'nottimestamp conditions)
4866 (not (re-search-forward org-ts-regexp end t)))
4867 (and (setq m (memq 'regexp conditions))
4868 (stringp (nth 1 m))
4869 (re-search-forward (nth 1 m) end t))
4870 (and (setq m (memq 'notregexp conditions))
4871 (stringp (nth 1 m))
4872 (not (re-search-forward (nth 1 m) end t)))
4873 (and (or
4874 (setq m (memq 'nottodo conditions))
4875 (setq m (memq 'todo-unblocked conditions))
4876 (setq m (memq 'nottodo-unblocked conditions))
4877 (setq m (memq 'todo conditions)))
4878 (org-agenda-skip-if-todo m end)))
4879 end)))
4881 (defun org-agenda-skip-if-todo (args end)
4882 "Helper function for `org-agenda-skip-if', do not use it directly.
4883 ARGS is a list with first element either `todo', `nottodo',
4884 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
4885 a list of TODO keywords, or a state symbol `todo' or `done' or
4886 `any'."
4887 (let ((kw (car args))
4888 (arg (cadr args))
4889 todo-wds todo-re)
4890 (setq todo-wds
4891 (org-uniquify
4892 (cond
4893 ((listp arg) ;; list of keywords
4894 (if (member "*" arg)
4895 (mapcar 'substring-no-properties org-todo-keywords-1)
4896 arg))
4897 ((symbolp arg) ;; keyword class name
4898 (cond
4899 ((eq arg 'todo)
4900 (org-delete-all org-done-keywords
4901 (mapcar 'substring-no-properties
4902 org-todo-keywords-1)))
4903 ((eq arg 'done) org-done-keywords)
4904 ((eq arg 'any)
4905 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4906 (setq todo-re
4907 (concat "^\\*+[ \t]+\\<\\("
4908 (mapconcat 'identity todo-wds "\\|")
4909 "\\)\\>"))
4910 (cond
4911 ((eq kw 'todo) (re-search-forward todo-re end t))
4912 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
4913 ((eq kw 'todo-unblocked)
4914 (catch 'unblocked
4915 (while (re-search-forward todo-re end t)
4916 (or (org-entry-blocked-p) (throw 'unblocked t)))
4917 nil))
4918 ((eq kw 'nottodo-unblocked)
4919 (catch 'unblocked
4920 (while (re-search-forward todo-re end t)
4921 (or (org-entry-blocked-p) (throw 'unblocked nil)))
4925 ;;;###autoload
4926 (defun org-agenda-list-stuck-projects (&rest ignore)
4927 "Create agenda view for projects that are stuck.
4928 Stuck projects are project that have no next actions. For the definitions
4929 of what a project is and how to check if it stuck, customize the variable
4930 `org-stuck-projects'."
4931 (interactive)
4932 (let* ((org-agenda-overriding-header
4933 (or org-agenda-overriding-header "List of stuck projects: "))
4934 (matcher (nth 0 org-stuck-projects))
4935 (todo (nth 1 org-stuck-projects))
4936 (tags (nth 2 org-stuck-projects))
4937 (gen-re (org-string-nw-p (nth 3 org-stuck-projects)))
4938 (todo-wds
4939 (if (not (member "*" todo)) todo
4940 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
4941 (org-delete-all org-done-keywords-for-agenda
4942 (copy-sequence org-todo-keywords-for-agenda))))
4943 (todo-re (and todo
4944 (format "^\\*+[ \t]+\\(%s\\)\\>"
4945 (mapconcat #'identity todo-wds "\\|"))))
4946 (tags-re (cond ((null tags) nil)
4947 ((member "*" tags)
4948 (eval-when-compile
4949 (concat org-outline-regexp-bol
4950 ".*:[[:alnum:]_@#%]+:[ \t]*$")))
4951 (tags (concat org-outline-regexp-bol
4952 ".*:\\("
4953 (mapconcat #'identity tags "\\|")
4954 "\\):[[:alnum:]_@#%:]*[ \t]*$"))
4955 (t nil)))
4956 (re-list (delq nil (list todo-re tags-re gen-re)))
4957 (skip-re
4958 (if (null re-list)
4959 (error "Missing information to identify unstuck projects")
4960 (mapconcat #'identity re-list "\\|")))
4961 (org-agenda-skip-function
4962 ;; Skip entry if `org-agenda-skip-regexp' matches anywhere
4963 ;; in the subtree.
4964 `(lambda ()
4965 (and (save-excursion
4966 (let ((case-fold-search nil))
4967 (re-search-forward
4968 ,skip-re (save-excursion (org-end-of-subtree t)) t)))
4969 (progn (outline-next-heading) (point))))))
4970 (org-tags-view nil matcher)
4971 (setq org-agenda-buffer-name (buffer-name))
4972 (with-current-buffer org-agenda-buffer-name
4973 (setq org-agenda-redo-command
4974 `(org-agenda-list-stuck-projects ,current-prefix-arg))
4975 (let ((inhibit-read-only t))
4976 (add-text-properties
4977 (point-min) (point-max)
4978 `(org-redo-cmd ,org-agenda-redo-command))))))
4980 ;;; Diary integration
4982 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
4983 (defvar diary-list-entries-hook)
4984 (defvar diary-time-regexp)
4985 (defun org-get-entries-from-diary (date)
4986 "Get the (Emacs Calendar) diary entries for DATE."
4987 (require 'diary-lib)
4988 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
4989 (diary-display-function 'diary-fancy-display)
4990 (pop-up-frames nil)
4991 (diary-list-entries-hook
4992 (cons 'org-diary-default-entry diary-list-entries-hook))
4993 (diary-file-name-prefix nil) ; turn this feature off
4994 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
4995 entries
4996 (org-disable-agenda-to-diary t))
4997 (save-excursion
4998 (save-window-excursion
4999 (funcall (if (fboundp 'diary-list-entries)
5000 'diary-list-entries 'list-diary-entries)
5001 date 1)))
5002 (if (not (get-buffer diary-fancy-buffer))
5003 (setq entries nil)
5004 (with-current-buffer diary-fancy-buffer
5005 (setq buffer-read-only nil)
5006 (if (zerop (buffer-size))
5007 ;; No entries
5008 (setq entries nil)
5009 ;; Omit the date and other unnecessary stuff
5010 (org-agenda-cleanup-fancy-diary)
5011 ;; Add prefix to each line and extend the text properties
5012 (if (zerop (buffer-size))
5013 (setq entries nil)
5014 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
5015 (setq entries
5016 (with-temp-buffer
5017 (insert entries) (goto-char (point-min))
5018 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5019 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5020 (replace-match (concat "; " (match-string 1)))))
5021 (buffer-string)))))
5022 (set-buffer-modified-p nil)
5023 (kill-buffer diary-fancy-buffer)))
5024 (when entries
5025 (setq entries (org-split-string entries "\n"))
5026 (setq entries
5027 (mapcar
5028 (lambda (x)
5029 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5030 ;; Extend the text properties to the beginning of the line
5031 (org-add-props x (text-properties-at (1- (length x)) x)
5032 'type "diary" 'date date 'face 'org-agenda-diary))
5033 entries)))))
5035 (defvar org-agenda-cleanup-fancy-diary-hook nil
5036 "Hook run when the fancy diary buffer is cleaned up.")
5038 (defun org-agenda-cleanup-fancy-diary ()
5039 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5040 This gets rid of the date, the underline under the date, and
5041 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5042 date. It also removes lines that contain only whitespace."
5043 (goto-char (point-min))
5044 (if (looking-at ".*?:[ \t]*")
5045 (progn
5046 (replace-match "")
5047 (re-search-forward "\n=+$" nil t)
5048 (replace-match "")
5049 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5050 (re-search-forward "\n=+$" nil t)
5051 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5052 (goto-char (point-min))
5053 (while (re-search-forward "^ +\n" nil t)
5054 (replace-match ""))
5055 (goto-char (point-min))
5056 (if (re-search-forward "^Org mode dummy\n?" nil t)
5057 (replace-match ""))
5058 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5060 ;; Make sure entries from the diary have the right text properties.
5061 (eval-after-load "diary-lib"
5062 '(if (boundp 'diary-modify-entry-list-string-function)
5063 ;; We can rely on the hook, nothing to do
5065 ;; Hook not available, must use advice to make this work
5066 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5067 "Make the position visible."
5068 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5069 (stringp string)
5070 buffer-file-name)
5071 (setq string (org-modify-diary-entry-string string))))))
5073 (defun org-modify-diary-entry-string (string)
5074 "Add text properties to string, allowing Org to act on it."
5075 (org-add-props string nil
5076 'mouse-face 'highlight
5077 'help-echo (if buffer-file-name
5078 (format "mouse-2 or RET jump to diary file %s"
5079 (abbreviate-file-name buffer-file-name))
5081 'org-agenda-diary-link t
5082 'org-marker (org-agenda-new-marker (point-at-bol))))
5084 (defun org-diary-default-entry ()
5085 "Add a dummy entry to the diary.
5086 Needed to avoid empty dates which mess up holiday display."
5087 ;; Catch the error if dealing with the new add-to-diary-alist
5088 (when org-disable-agenda-to-diary
5089 (condition-case nil
5090 (org-add-to-diary-list original-date "Org mode dummy" "")
5091 (error
5092 (org-add-to-diary-list original-date "Org mode dummy" "" nil)))))
5094 (defun org-add-to-diary-list (&rest args)
5095 (if (fboundp 'diary-add-to-list)
5096 (apply 'diary-add-to-list args)
5097 (apply 'add-to-diary-list args)))
5099 (defvar org-diary-last-run-time nil)
5101 ;;;###autoload
5102 (defun org-diary (&rest args)
5103 "Return diary information from org files.
5104 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5105 It accesses org files and extracts information from those files to be
5106 listed in the diary. The function accepts arguments specifying what
5107 items should be listed. For a list of arguments allowed here, see the
5108 variable `org-agenda-entry-types'.
5110 The call in the diary file should look like this:
5112 &%%(org-diary) ~/path/to/some/orgfile.org
5114 Use a separate line for each org file to check. Or, if you omit the file name,
5115 all files listed in `org-agenda-files' will be checked automatically:
5117 &%%(org-diary)
5119 If you don't give any arguments (as in the example above), the default value
5120 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5121 So the example above may also be written as
5123 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5125 The function expects the lisp variables `entry' and `date' to be provided
5126 by the caller, because this is how the calendar works. Don't use this
5127 function from a program - use `org-agenda-get-day-entries' instead."
5128 (when (> (- (float-time)
5129 org-agenda-last-marker-time)
5131 ;; I am not sure if this works with sticky agendas, because the marker
5132 ;; list is then no longer a global variable.
5133 (org-agenda-reset-markers))
5134 (org-compile-prefix-format 'agenda)
5135 (org-set-sorting-strategy 'agenda)
5136 (setq args (or args org-agenda-entry-types))
5137 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5138 (list entry)
5139 (org-agenda-files t)))
5140 (time (float-time))
5141 file rtn results)
5142 (when (or (not org-diary-last-run-time)
5143 (> (- time
5144 org-diary-last-run-time)
5146 (org-agenda-prepare-buffers files))
5147 (setq org-diary-last-run-time time)
5148 ;; If this is called during org-agenda, don't return any entries to
5149 ;; the calendar. Org Agenda will list these entries itself.
5150 (if org-disable-agenda-to-diary (setq files nil))
5151 (while (setq file (pop files))
5152 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5153 (setq results (append results rtn)))
5154 (when results
5155 (setq results
5156 (mapcar (lambda (i) (replace-regexp-in-string
5157 org-bracket-link-regexp "\\3" i)) results))
5158 (concat (org-agenda-finalize-entries results) "\n"))))
5160 ;;; Agenda entry finders
5162 (defun org-agenda--timestamp-to-absolute (&rest args)
5163 "Call `org-time-string-to-absolute' with ARGS.
5164 However, throw `:skip' whenever an error is raised."
5165 (condition-case e
5166 (apply #'org-time-string-to-absolute args)
5167 (org-diary-sexp-no-match (throw :skip nil))
5168 (error
5169 (message "%s; Skipping entry" (error-message-string e))
5170 (throw :skip nil))))
5172 (defun org-agenda-get-day-entries (file date &rest args)
5173 "Does the work for `org-diary' and `org-agenda'.
5174 FILE is the path to a file to be checked for entries. DATE is date like
5175 the one returned by `calendar-current-date'. ARGS are symbols indicating
5176 which kind of entries should be extracted. For details about these, see
5177 the documentation of `org-diary'."
5178 (let* ((org-startup-folded nil)
5179 (org-startup-align-all-tables nil)
5180 (buffer (if (file-exists-p file) (org-get-agenda-file-buffer file)
5181 (error "No such file %s" file))))
5182 (if (not buffer)
5183 ;; If file does not exist, signal it in diary nonetheless.
5184 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5185 (with-current-buffer buffer
5186 (unless (derived-mode-p 'org-mode)
5187 (error "Agenda file %s is not in `org-mode'" file))
5188 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5189 (setf org-agenda-current-date date)
5190 (save-excursion
5191 (save-restriction
5192 (if (eq buffer org-agenda-restrict)
5193 (narrow-to-region org-agenda-restrict-begin
5194 org-agenda-restrict-end)
5195 (widen))
5196 ;; Rationalize ARGS. Also make sure `:deadline' comes
5197 ;; first in order to populate DEADLINES before passing it.
5199 ;; We use `delq' since `org-uniquify' duplicates ARGS,
5200 ;; guarding us from modifying `org-agenda-entry-types'.
5201 (setf args (org-uniquify (or args org-agenda-entry-types)))
5202 (when (and (memq :scheduled args) (memq :scheduled* args))
5203 (setf args (delq :scheduled* args)))
5204 (cond
5205 ((memq :deadline args)
5206 (setf args (cons :deadline
5207 (delq :deadline (delq :deadline* args)))))
5208 ((memq :deadline* args)
5209 (setf args (cons :deadline* (delq :deadline* args)))))
5210 ;; Collect list of headlines. Return them flattened.
5211 (let ((case-fold-search nil) results deadlines)
5212 (dolist (arg args (apply #'nconc (nreverse results)))
5213 (pcase arg
5214 ((and :todo (guard (org-agenda-today-p date)))
5215 (push (org-agenda-get-todos) results))
5216 (:timestamp
5217 (push (org-agenda-get-blocks) results)
5218 (push (org-agenda-get-timestamps deadlines) results))
5219 (:sexp
5220 (push (org-agenda-get-sexps) results))
5221 (:scheduled
5222 (push (org-agenda-get-scheduled deadlines) results))
5223 (:scheduled*
5224 (push (org-agenda-get-scheduled deadlines t) results))
5225 (:closed
5226 (push (org-agenda-get-progress) results))
5227 (:deadline
5228 (setf deadlines (org-agenda-get-deadlines))
5229 (push deadlines results))
5230 (:deadline*
5231 (setf deadlines (org-agenda-get-deadlines t))
5232 (push deadlines results)))))))))))
5234 (defsubst org-em (x y list)
5235 "Is X or Y a member of LIST?"
5236 (or (memq x list) (memq y list)))
5238 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5239 (defvar org-agenda-sorting-strategy-selected nil)
5241 (defun org-agenda-entry-get-agenda-timestamp (pom)
5242 "Retrieve timestamp information for sorting agenda views.
5243 Given a point or marker POM, returns a cons cell of the timestamp
5244 and the timestamp type relevant for the sorting strategy in
5245 `org-agenda-sorting-strategy-selected'."
5246 (let (ts ts-date-type)
5247 (save-match-data
5248 (cond ((org-em 'scheduled-up 'scheduled-down
5249 org-agenda-sorting-strategy-selected)
5250 (setq ts (org-entry-get pom "SCHEDULED")
5251 ts-date-type " scheduled"))
5252 ((org-em 'deadline-up 'deadline-down
5253 org-agenda-sorting-strategy-selected)
5254 (setq ts (org-entry-get pom "DEADLINE")
5255 ts-date-type " deadline"))
5256 ((org-em 'ts-up 'ts-down
5257 org-agenda-sorting-strategy-selected)
5258 (setq ts (org-entry-get pom "TIMESTAMP")
5259 ts-date-type " timestamp"))
5260 ((org-em 'tsia-up 'tsia-down
5261 org-agenda-sorting-strategy-selected)
5262 (setq ts (org-entry-get pom "TIMESTAMP_IA")
5263 ts-date-type " timestamp_ia"))
5264 ((org-em 'timestamp-up 'timestamp-down
5265 org-agenda-sorting-strategy-selected)
5266 (setq ts (or (org-entry-get pom "SCHEDULED")
5267 (org-entry-get pom "DEADLINE")
5268 (org-entry-get pom "TIMESTAMP")
5269 (org-entry-get pom "TIMESTAMP_IA"))
5270 ts-date-type ""))
5271 (t (setq ts-date-type "")))
5272 (cons (when ts (ignore-errors (org-time-string-to-absolute ts)))
5273 ts-date-type))))
5275 (defun org-agenda-get-todos ()
5276 "Return the TODO information for agenda display."
5277 (let* ((props (list 'face nil
5278 'done-face 'org-agenda-done
5279 'org-not-done-regexp org-not-done-regexp
5280 'org-todo-regexp org-todo-regexp
5281 'org-complex-heading-regexp org-complex-heading-regexp
5282 'mouse-face 'highlight
5283 'help-echo
5284 (format "mouse-2 or RET jump to org file %s"
5285 (abbreviate-file-name buffer-file-name))))
5286 (case-fold-search nil)
5287 (regexp (format org-heading-keyword-regexp-format
5288 (cond
5289 ((and org-select-this-todo-keyword
5290 (equal org-select-this-todo-keyword "*"))
5291 org-todo-regexp)
5292 (org-select-this-todo-keyword
5293 (concat "\\("
5294 (mapconcat 'identity
5295 (org-split-string
5296 org-select-this-todo-keyword
5297 "|")
5298 "\\|") "\\)"))
5299 (t org-not-done-regexp))))
5300 marker priority category level tags todo-state
5301 ts-date ts-date-type ts-date-pair
5302 ee txt beg end inherited-tags todo-state-end-pos)
5303 (goto-char (point-min))
5304 (while (re-search-forward regexp nil t)
5305 (catch :skip
5306 (save-match-data
5307 (beginning-of-line)
5308 (org-agenda-skip)
5309 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5310 (unless (and (setq todo-state (org-get-todo-state))
5311 (setq todo-state-end-pos (match-end 2)))
5312 (goto-char end)
5313 (throw :skip nil))
5314 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5315 (goto-char (1+ beg))
5316 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5317 (throw :skip nil)))
5318 (goto-char (match-beginning 2))
5319 (setq marker (org-agenda-new-marker (match-beginning 0))
5320 category (org-get-category)
5321 ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
5322 ts-date (car ts-date-pair)
5323 ts-date-type (cdr ts-date-pair)
5324 txt (org-trim (buffer-substring (match-beginning 2) (match-end 0)))
5325 inherited-tags
5326 (or (eq org-agenda-show-inherited-tags 'always)
5327 (and (listp org-agenda-show-inherited-tags)
5328 (memq 'todo org-agenda-show-inherited-tags))
5329 (and (eq org-agenda-show-inherited-tags t)
5330 (or (eq org-agenda-use-tag-inheritance t)
5331 (memq 'todo org-agenda-use-tag-inheritance))))
5332 tags (org-get-tags-at nil (not inherited-tags))
5333 level (make-string (org-reduced-level (org-outline-level)) ? )
5334 txt (org-agenda-format-item "" txt level category tags t)
5335 priority (1+ (org-get-priority txt)))
5336 (org-add-props txt props
5337 'org-marker marker 'org-hd-marker marker
5338 'priority priority
5339 'level level
5340 'ts-date ts-date
5341 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5342 (push txt ee)
5343 (if org-agenda-todo-list-sublevels
5344 (goto-char todo-state-end-pos)
5345 (org-end-of-subtree 'invisible))))
5346 (nreverse ee)))
5348 (defun org-agenda-todo-custom-ignore-p (time n)
5349 "Check whether timestamp is farther away than n number of days.
5350 This function is invoked if `org-agenda-todo-ignore-deadlines',
5351 `org-agenda-todo-ignore-scheduled' or
5352 `org-agenda-todo-ignore-timestamp' is set to an integer."
5353 (let ((days (org-time-stamp-to-now
5354 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5355 (if (>= n 0)
5356 (>= days n)
5357 (<= days n))))
5359 ;;;###autoload
5360 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5361 (&optional end)
5362 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5363 (when (or org-agenda-todo-ignore-with-date
5364 org-agenda-todo-ignore-scheduled
5365 org-agenda-todo-ignore-deadlines
5366 org-agenda-todo-ignore-timestamp)
5367 (setq end (or end (save-excursion (outline-next-heading) (point))))
5368 (save-excursion
5369 (or (and org-agenda-todo-ignore-with-date
5370 (re-search-forward org-ts-regexp end t))
5371 (and org-agenda-todo-ignore-scheduled
5372 (re-search-forward org-scheduled-time-regexp end t)
5373 (cond
5374 ((eq org-agenda-todo-ignore-scheduled 'future)
5375 (> (org-time-stamp-to-now
5376 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5377 ((eq org-agenda-todo-ignore-scheduled 'past)
5378 (<= (org-time-stamp-to-now
5379 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5380 ((numberp org-agenda-todo-ignore-scheduled)
5381 (org-agenda-todo-custom-ignore-p
5382 (match-string 1) org-agenda-todo-ignore-scheduled))
5383 (t)))
5384 (and org-agenda-todo-ignore-deadlines
5385 (re-search-forward org-deadline-time-regexp end t)
5386 (cond
5387 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5388 ((eq org-agenda-todo-ignore-deadlines 'far)
5389 (not (org-deadline-close-p (match-string 1))))
5390 ((eq org-agenda-todo-ignore-deadlines 'future)
5391 (> (org-time-stamp-to-now
5392 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5393 ((eq org-agenda-todo-ignore-deadlines 'past)
5394 (<= (org-time-stamp-to-now
5395 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5396 ((numberp org-agenda-todo-ignore-deadlines)
5397 (org-agenda-todo-custom-ignore-p
5398 (match-string 1) org-agenda-todo-ignore-deadlines))
5399 (t (org-deadline-close-p (match-string 1)))))
5400 (and org-agenda-todo-ignore-timestamp
5401 (let ((buffer (current-buffer))
5402 (regexp
5403 (concat
5404 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5405 (start (point)))
5406 ;; Copy current buffer into a temporary one
5407 (with-temp-buffer
5408 (insert-buffer-substring buffer start end)
5409 (goto-char (point-min))
5410 ;; Delete SCHEDULED and DEADLINE items
5411 (while (re-search-forward regexp end t)
5412 (delete-region (match-beginning 0) (match-end 0)))
5413 (goto-char (point-min))
5414 ;; No search for timestamp left
5415 (when (re-search-forward org-ts-regexp nil t)
5416 (cond
5417 ((eq org-agenda-todo-ignore-timestamp 'future)
5418 (> (org-time-stamp-to-now
5419 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5420 ((eq org-agenda-todo-ignore-timestamp 'past)
5421 (<= (org-time-stamp-to-now
5422 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5423 ((numberp org-agenda-todo-ignore-timestamp)
5424 (org-agenda-todo-custom-ignore-p
5425 (match-string 1) org-agenda-todo-ignore-timestamp))
5426 (t))))))))))
5428 (defun org-agenda-get-timestamps (&optional deadlines)
5429 "Return the date stamp information for agenda display.
5430 Optional argument DEADLINES is a list of deadline items to be
5431 displayed in agenda view."
5432 (let* ((props (list 'face 'org-agenda-calendar-event
5433 'org-not-done-regexp org-not-done-regexp
5434 'org-todo-regexp org-todo-regexp
5435 'org-complex-heading-regexp org-complex-heading-regexp
5436 'mouse-face 'highlight
5437 'help-echo
5438 (format "mouse-2 or RET jump to Org file %s"
5439 (abbreviate-file-name buffer-file-name))))
5440 (current (calendar-absolute-from-gregorian date))
5441 (today (org-today))
5442 (deadline-position-alist
5443 (mapcar (lambda (d)
5444 (let ((m (get-text-property 0 'org-hd-marker d)))
5445 (and m (marker-position m))))
5446 deadlines))
5447 ;; Match time-stamps set to current date, time-stamps with
5448 ;; a repeater, and S-exp time-stamps.
5449 (regexp
5450 (concat
5451 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5452 (regexp-quote
5453 (substring
5454 (format-time-string
5455 (car org-time-stamp-formats)
5456 (apply #'encode-time ; DATE bound by calendar
5457 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5458 1 11))
5459 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5460 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5461 timestamp-items)
5462 (goto-char (point-min))
5463 (while (re-search-forward regexp nil t)
5464 ;; Skip date ranges, scheduled and deadlines, which are handled
5465 ;; specially. Also skip time-stamps before first headline as
5466 ;; there would be no entry to add to the agenda. Eventually,
5467 ;; ignore clock entries.
5468 (catch :skip
5469 (save-match-data
5470 (when (or (org-at-date-range-p)
5471 (org-at-planning-p)
5472 (org-before-first-heading-p)
5473 (and org-agenda-include-inactive-timestamps
5474 (org-at-clock-log-p)))
5475 (throw :skip nil))
5476 (org-agenda-skip))
5477 (let* ((pos (match-beginning 0))
5478 (repeat (match-string 1))
5479 (sexp-entry (match-string 3))
5480 (time-stamp (if (or repeat sexp-entry) (match-string 0)
5481 (save-excursion
5482 (goto-char pos)
5483 (looking-at org-ts-regexp-both)
5484 (match-string 0))))
5485 (todo-state (org-get-todo-state))
5486 (warntime (get-text-property (point) 'org-appt-warntime))
5487 (done? (member todo-state org-done-keywords)))
5488 ;; Possibly skip done tasks.
5489 (when (and done? org-agenda-skip-timestamp-if-done)
5490 (throw :skip t))
5491 ;; S-exp entry doesn't match current day: skip it.
5492 (when (and sexp-entry (not (org-diary-sexp-entry sexp-entry "" date)))
5493 (throw :skip nil))
5494 (when repeat
5495 (let* ((past
5496 ;; A repeating time stamp is shown at its base
5497 ;; date and every repeated date up to TODAY. If
5498 ;; `org-agenda-prefer-last-repeat' is non-nil,
5499 ;; however, only the last repeat before today
5500 ;; (inclusive) is shown.
5501 (org-agenda--timestamp-to-absolute
5502 repeat
5503 (if (or (> current today)
5504 (eq org-agenda-prefer-last-repeat t)
5505 (member todo-state org-agenda-prefer-last-repeat))
5506 today
5507 current)
5508 'past (current-buffer) pos))
5509 (future
5510 ;; Display every repeated date past TODAY
5511 ;; (exclusive) unless
5512 ;; `org-agenda-show-future-repeats' is nil. If
5513 ;; this variable is set to `next', only display
5514 ;; the first repeated date after TODAY
5515 ;; (exclusive).
5516 (cond
5517 ((<= current today) past)
5518 ((not org-agenda-show-future-repeats) past)
5520 (let ((base (if (eq org-agenda-show-future-repeats 'next)
5521 (1+ today)
5522 current)))
5523 (org-agenda--timestamp-to-absolute
5524 repeat base 'future (current-buffer) pos))))))
5525 (when (and (/= current past) (/= current future))
5526 (throw :skip nil))))
5527 (save-excursion
5528 (re-search-backward org-outline-regexp-bol nil t)
5529 ;; Possibly skip time-stamp when a deadline is set.
5530 (when (and org-agenda-skip-timestamp-if-deadline-is-shown
5531 (assq (point) deadline-position-alist))
5532 (throw :skip nil))
5533 (let* ((category (org-get-category pos))
5534 (inherited-tags
5535 (or (eq org-agenda-show-inherited-tags 'always)
5536 (and (consp org-agenda-show-inherited-tags)
5537 (memq 'agenda org-agenda-show-inherited-tags))
5538 (and (eq org-agenda-show-inherited-tags t)
5539 (or (eq org-agenda-use-tag-inheritance t)
5540 (memq 'agenda
5541 org-agenda-use-tag-inheritance)))))
5542 (tags (org-get-tags-at nil (not inherited-tags)))
5543 (level (make-string (org-reduced-level (org-outline-level))
5544 ?\s))
5545 (head (and (looking-at "\\*+[ \t]+\\(.*\\)")
5546 (match-string 1)))
5547 (inactive? (= (char-after pos) ?\[))
5548 (habit? (and (fboundp 'org-is-habit-p) (org-is-habit-p)))
5549 (item
5550 (org-agenda-format-item
5551 (and inactive? org-agenda-inactive-leader)
5552 head level category tags time-stamp org-ts-regexp habit?)))
5553 (org-add-props item props
5554 'priority (if habit?
5555 (org-habit-get-priority (org-habit-parse-todo))
5556 (org-get-priority item))
5557 'org-marker (org-agenda-new-marker pos)
5558 'org-hd-marker (org-agenda-new-marker)
5559 'date date
5560 'level level
5561 'ts-date (if repeat (org-agenda--timestamp-to-absolute repeat)
5562 current)
5563 'todo-state todo-state
5564 'warntime warntime
5565 'type "timestamp")
5566 (push item timestamp-items))))
5567 (when org-agenda-skip-additional-timestamps-same-entry
5568 (outline-next-heading))))
5569 (nreverse timestamp-items)))
5571 (defun org-agenda-get-sexps ()
5572 "Return the sexp information for agenda display."
5573 (require 'diary-lib)
5574 (let* ((props (list 'face 'org-agenda-calendar-sexp
5575 'mouse-face 'highlight
5576 'help-echo
5577 (format "mouse-2 or RET jump to org file %s"
5578 (abbreviate-file-name buffer-file-name))))
5579 (regexp "^&?%%(")
5580 marker category extra level ee txt tags entry
5581 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5582 (goto-char (point-min))
5583 (while (re-search-forward regexp nil t)
5584 (catch :skip
5585 (org-agenda-skip)
5586 (setq beg (match-beginning 0))
5587 (goto-char (1- (match-end 0)))
5588 (setq b (point))
5589 (forward-sexp 1)
5590 (setq sexp (buffer-substring b (point)))
5591 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5592 (org-trim (match-string 1))
5593 ""))
5594 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5595 (when result
5596 (setq marker (org-agenda-new-marker beg)
5597 level (make-string (org-reduced-level (org-outline-level)) ? )
5598 category (org-get-category beg)
5599 inherited-tags
5600 (or (eq org-agenda-show-inherited-tags 'always)
5601 (and (listp org-agenda-show-inherited-tags)
5602 (memq 'agenda org-agenda-show-inherited-tags))
5603 (and (eq org-agenda-show-inherited-tags t)
5604 (or (eq org-agenda-use-tag-inheritance t)
5605 (memq 'agenda org-agenda-use-tag-inheritance))))
5606 tags (org-get-tags-at nil (not inherited-tags))
5607 todo-state (org-get-todo-state)
5608 warntime (get-text-property (point) 'org-appt-warntime)
5609 extra nil)
5611 (dolist (r (if (stringp result)
5612 (list result)
5613 result)) ;; we expect a list here
5614 (when (and org-agenda-diary-sexp-prefix
5615 (string-match org-agenda-diary-sexp-prefix r))
5616 (setq extra (match-string 0 r)
5617 r (replace-match "" nil nil r)))
5618 (if (string-match "\\S-" r)
5619 (setq txt r)
5620 (setq txt "SEXP entry returned empty string"))
5621 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5622 (org-add-props txt props 'org-marker marker
5623 'date date 'todo-state todo-state
5624 'level level 'type "sexp" 'warntime warntime)
5625 (push txt ee)))))
5626 (nreverse ee)))
5628 ;; Calendar sanity: define some functions that are independent of
5629 ;; `calendar-date-style'.
5630 (defun org-anniversary (year month day &optional mark)
5631 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5632 (with-no-warnings
5633 (let ((calendar-date-style 'iso))
5634 (diary-anniversary year month day mark))))
5635 (defun org-cyclic (N year month day &optional mark)
5636 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5637 (with-no-warnings
5638 (let ((calendar-date-style 'iso))
5639 (diary-cyclic N year month day mark))))
5640 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5641 "Like `diary-block', but with fixed (ISO) order of arguments."
5642 (with-no-warnings
5643 (let ((calendar-date-style 'iso))
5644 (diary-block Y1 M1 D1 Y2 M2 D2 mark))))
5645 (defun org-date (year month day &optional mark)
5646 "Like `diary-date', but with fixed (ISO) order of arguments."
5647 (with-no-warnings
5648 (let ((calendar-date-style 'iso))
5649 (diary-date year month day mark))))
5651 ;; Define the `org-class' function
5652 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5653 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5654 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5655 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5656 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5657 `holidays', then any date that is known by the Emacs calendar to be a
5658 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5659 then those holidays will be skipped."
5660 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5661 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5662 (d (calendar-absolute-from-gregorian date))
5663 (h (when skip-weeks (calendar-check-holidays date))))
5664 (and
5665 (<= date1 d)
5666 (<= d date2)
5667 (= (calendar-day-of-week date) dayname)
5668 (or (not skip-weeks)
5669 (progn
5670 (require 'cal-iso)
5671 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5672 (not (or (and h (memq 'holidays skip-weeks))
5673 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5674 entry)))
5676 (defalias 'org-get-closed 'org-agenda-get-progress)
5677 (defun org-agenda-get-progress ()
5678 "Return the logged TODO entries for agenda display."
5679 (let* ((props (list 'mouse-face 'highlight
5680 'org-not-done-regexp org-not-done-regexp
5681 'org-todo-regexp org-todo-regexp
5682 'org-complex-heading-regexp org-complex-heading-regexp
5683 'help-echo
5684 (format "mouse-2 or RET jump to org file %s"
5685 (abbreviate-file-name buffer-file-name))))
5686 (items (if (consp org-agenda-show-log-scoped)
5687 org-agenda-show-log-scoped
5688 (if (eq org-agenda-show-log-scoped 'clockcheck)
5689 '(clock)
5690 org-agenda-log-mode-items)))
5691 (parts
5692 (delq nil
5693 (list
5694 (if (memq 'closed items) (concat "\\<" org-closed-string))
5695 (if (memq 'clock items) (concat "\\<" org-clock-string))
5696 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5697 (parts-re (if parts (mapconcat 'identity parts "\\|")
5698 (error "`org-agenda-log-mode-items' is empty")))
5699 (regexp (concat
5700 "\\(" parts-re "\\)"
5701 " *\\["
5702 (regexp-quote
5703 (substring
5704 (format-time-string
5705 (car org-time-stamp-formats)
5706 (apply 'encode-time ; DATE bound by calendar
5707 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5708 1 11))))
5709 (org-agenda-search-headline-for-time nil)
5710 marker hdmarker priority category level tags closedp
5711 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5712 (goto-char (point-min))
5713 (while (re-search-forward regexp nil t)
5714 (catch :skip
5715 (org-agenda-skip)
5716 (setq marker (org-agenda-new-marker (match-beginning 0))
5717 closedp (equal (match-string 1) org-closed-string)
5718 statep (equal (string-to-char (match-string 1)) ?-)
5719 clockp (not (or closedp statep))
5720 state (and statep (match-string 2))
5721 category (org-get-category (match-beginning 0))
5722 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5723 (when (string-match "\\]" timestr)
5724 ;; substring should only run to end of time stamp
5725 (setq rest (substring timestr (match-end 0))
5726 timestr (substring timestr 0 (match-end 0)))
5727 (if (and (not closedp) (not statep)
5728 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5729 rest))
5730 (progn (setq timestr (concat (substring timestr 0 -1)
5731 "-" (match-string 1 rest) "]"))
5732 (setq clocked (match-string 2 rest)))
5733 (setq clocked "-")))
5734 (save-excursion
5735 (setq extra
5736 (cond
5737 ((not org-agenda-log-mode-add-notes) nil)
5738 (statep
5739 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5740 (match-string 1)))
5741 (clockp
5742 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5743 (match-string 1)))))
5744 (if (not (re-search-backward org-outline-regexp-bol nil t))
5745 (throw :skip nil)
5746 (goto-char (match-beginning 0))
5747 (setq hdmarker (org-agenda-new-marker)
5748 inherited-tags
5749 (or (eq org-agenda-show-inherited-tags 'always)
5750 (and (listp org-agenda-show-inherited-tags)
5751 (memq 'todo org-agenda-show-inherited-tags))
5752 (and (eq org-agenda-show-inherited-tags t)
5753 (or (eq org-agenda-use-tag-inheritance t)
5754 (memq 'todo org-agenda-use-tag-inheritance))))
5755 tags (org-get-tags-at nil (not inherited-tags))
5756 level (make-string (org-reduced-level (org-outline-level)) ? ))
5757 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5758 (setq txt (match-string 1))
5759 (when extra
5760 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5761 (setq txt (concat (substring txt 0 (match-beginning 1))
5762 " - " extra " " (match-string 2 txt)))
5763 (setq txt (concat txt " - " extra))))
5764 (setq txt (org-agenda-format-item
5765 (cond
5766 (closedp "Closed: ")
5767 (statep (concat "State: (" state ")"))
5768 (t (concat "Clocked: (" clocked ")")))
5769 txt level category tags timestr)))
5770 (setq priority 100000)
5771 (org-add-props txt props
5772 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5773 'priority priority 'level level
5774 'type "closed" 'date date
5775 'undone-face 'org-warning 'done-face 'org-agenda-done)
5776 (push txt ee))
5777 (goto-char (point-at-eol))))
5778 (nreverse ee)))
5780 (defun org-agenda-show-clocking-issues ()
5781 "Add overlays, showing issues with clocking.
5782 See also the user option `org-agenda-clock-consistency-checks'."
5783 (interactive)
5784 (let* ((pl org-agenda-clock-consistency-checks)
5785 (re (concat "^[ \t]*"
5786 org-clock-string
5787 "[ \t]+"
5788 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5789 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5790 (tlstart 0.)
5791 (tlend 0.)
5792 (maxtime (org-duration-to-minutes
5793 (or (plist-get pl :max-duration) "24:00")))
5794 (mintime (org-duration-to-minutes
5795 (or (plist-get pl :min-duration) 0)))
5796 (maxgap (org-duration-to-minutes
5797 ;; default 30:00 means never complain
5798 (or (plist-get pl :max-gap) "30:00")))
5799 (gapok (mapcar #'org-duration-to-minutes
5800 (plist-get pl :gap-ok-around)))
5801 (def-face (or (plist-get pl :default-face)
5802 '((:background "DarkRed") (:foreground "white"))))
5803 issue face m te ts dt ov)
5804 (goto-char (point-min))
5805 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5806 (setq issue nil face def-face)
5807 (catch 'next
5808 (setq m (org-get-at-bol 'org-marker)
5809 te nil ts nil)
5810 (unless (and m (markerp m))
5811 (setq issue "No valid clock line") (throw 'next t))
5812 (org-with-point-at m
5813 (save-excursion
5814 (goto-char (point-at-bol))
5815 (unless (looking-at re)
5816 (error "No valid Clock line")
5817 (throw 'next t))
5818 (unless (match-end 3)
5819 (setq issue "No end time"
5820 face (or (plist-get pl :no-end-time-face) face))
5821 (throw 'next t))
5822 (setq ts (match-string 1)
5823 te (match-string 3)
5824 ts (float-time
5825 (apply #'encode-time (org-parse-time-string ts)))
5826 te (float-time
5827 (apply #'encode-time (org-parse-time-string te)))
5828 dt (- te ts))))
5829 (cond
5830 ((> dt (* 60 maxtime))
5831 ;; a very long clocking chunk
5832 (setq issue (format "Clocking interval is very long: %s"
5833 (org-duration-from-minutes (floor (/ dt 60.))))
5834 face (or (plist-get pl :long-face) face)))
5835 ((< dt (* 60 mintime))
5836 ;; a very short clocking chunk
5837 (setq issue (format "Clocking interval is very short: %s"
5838 (org-duration-from-minutes (floor (/ dt 60.))))
5839 face (or (plist-get pl :short-face) face)))
5840 ((and (> tlend 0) (< ts tlend))
5841 ;; Two clock entries are overlapping
5842 (setq issue (format "Clocking overlap: %d minutes"
5843 (/ (- tlend ts) 60))
5844 face (or (plist-get pl :overlap-face) face)))
5845 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5846 ;; There is a gap, lets see if we need to report it
5847 (unless (org-agenda-check-clock-gap tlend ts gapok)
5848 (setq issue (format "Clocking gap: %d minutes"
5849 (/ (- ts tlend) 60))
5850 face (or (plist-get pl :gap-face) face))))
5851 (t nil)))
5852 (setq tlend (or te tlend) tlstart (or ts tlstart))
5853 (when issue
5854 ;; OK, there was some issue, add an overlay to show the issue
5855 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5856 (overlay-put ov 'before-string
5857 (concat
5858 (org-add-props
5859 (format "%-43s" (concat " " issue))
5861 'face face)
5862 "\n"))
5863 (overlay-put ov 'evaporate t)))))
5865 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5866 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5867 (catch 'exit
5868 (unless ok-list
5869 ;; there are no OK times for gaps...
5870 (throw 'exit nil))
5871 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5872 ;; This is more than 24 hours, so it is OK.
5873 ;; because we have at least one OK time, that must be in the
5874 ;; 24 hour interval.
5875 (throw 'exit t))
5876 ;; We have a shorter gap.
5877 ;; Now we have to get the minute of the day when these times are
5878 (let* ((t1dec (decode-time (seconds-to-time t1)))
5879 (t2dec (decode-time (seconds-to-time t2)))
5880 ;; compute the minute on the day
5881 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5882 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5883 (when (< min2 min1)
5884 ;; if min2 is smaller than min1, this means it is on the next day.
5885 ;; Wrap it to after midnight.
5886 (setq min2 (+ min2 1440)))
5887 ;; Now check if any of the OK times is in the gap
5888 (mapc (lambda (x)
5889 ;; Wrap the time to after midnight if necessary
5890 (if (< x min1) (setq x (+ x 1440)))
5891 ;; Check if in interval
5892 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5893 ok-list)
5894 ;; Nope, this gap is not OK
5895 nil)))
5897 (defun org-agenda-get-deadlines (&optional with-hour)
5898 "Return the deadline information for agenda display.
5899 When WITH-HOUR is non-nil, only return deadlines with an hour
5900 specification like [h]h:mm."
5901 (let* ((props (list 'mouse-face 'highlight
5902 'org-not-done-regexp org-not-done-regexp
5903 'org-todo-regexp org-todo-regexp
5904 'org-complex-heading-regexp org-complex-heading-regexp
5905 'help-echo
5906 (format "mouse-2 or RET jump to org file %s"
5907 (abbreviate-file-name buffer-file-name))))
5908 (regexp (if with-hour
5909 org-deadline-time-hour-regexp
5910 org-deadline-time-regexp))
5911 (today (org-today))
5912 (today? (org-agenda-today-p date)) ; DATE bound by calendar.
5913 (current (calendar-absolute-from-gregorian date))
5914 deadline-items)
5915 (goto-char (point-min))
5916 (while (re-search-forward regexp nil t)
5917 (catch :skip
5918 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
5919 (org-agenda-skip)
5920 (let* ((s (match-string 1))
5921 (pos (1- (match-beginning 1)))
5922 (todo-state (save-match-data (org-get-todo-state)))
5923 (done? (member todo-state org-done-keywords))
5924 (sexp? (string-prefix-p "%%" s))
5925 ;; DEADLINE is the deadline date for the entry. It is
5926 ;; either the base date or the last repeat, according
5927 ;; to `org-agenda-prefer-last-repeat'.
5928 (deadline
5929 (cond
5930 (sexp? (org-agenda--timestamp-to-absolute s current))
5931 ((or (eq org-agenda-prefer-last-repeat t)
5932 (member todo-state org-agenda-prefer-last-repeat))
5933 (org-agenda--timestamp-to-absolute
5934 s today 'past (current-buffer) pos))
5935 (t (org-agenda--timestamp-to-absolute s))))
5936 ;; REPEAT is the future repeat closest from CURRENT,
5937 ;; according to `org-agenda-show-future-repeats'. If
5938 ;; the latter is nil, or if the time stamp has no
5939 ;; repeat part, default to DEADLINE.
5940 (repeat
5941 (cond
5942 (sexp? deadline)
5943 ((<= current today) deadline)
5944 ((not org-agenda-show-future-repeats) deadline)
5946 (let ((base (if (eq org-agenda-show-future-repeats 'next)
5947 (1+ today)
5948 current)))
5949 (org-agenda--timestamp-to-absolute
5950 s base 'future (current-buffer) pos)))))
5951 (diff (- deadline current))
5952 (suppress-prewarning
5953 (let ((scheduled
5954 (and org-agenda-skip-deadline-prewarning-if-scheduled
5955 (org-entry-get nil "SCHEDULED"))))
5956 (cond
5957 ((not scheduled) nil)
5958 ;; The current item has a scheduled date, so
5959 ;; evaluate its prewarning lead time.
5960 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
5961 ;; Use global prewarning-restart lead time.
5962 org-agenda-skip-deadline-prewarning-if-scheduled)
5963 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
5964 'pre-scheduled)
5965 ;; Set pre-warning to no earlier than SCHEDULED.
5966 (min (- deadline
5967 (org-agenda--timestamp-to-absolute scheduled))
5968 org-deadline-warning-days))
5969 ;; Set pre-warning to deadline.
5970 (t 0))))
5971 (wdays (if suppress-prewarning
5972 (let ((org-deadline-warning-days suppress-prewarning))
5973 (org-get-wdays s))
5974 (org-get-wdays s))))
5975 (cond
5976 ;; Only display deadlines at their base date, at future
5977 ;; repeat occurrences or in today agenda.
5978 ((= current deadline) nil)
5979 ((= current repeat) nil)
5980 ((not today?) (throw :skip nil))
5981 ;; Upcoming deadline: display within warning period WDAYS.
5982 ((> deadline current) (when (> diff wdays) (throw :skip nil)))
5983 ;; Overdue deadline: warn about it for
5984 ;; `org-deadline-past-days' duration.
5985 (t (when (< org-deadline-past-days (- diff)) (throw :skip nil))))
5986 ;; Possibly skip done tasks.
5987 (when (and done?
5988 (or org-agenda-skip-deadline-if-done
5989 (/= deadline current)))
5990 (throw :skip nil))
5991 (save-excursion
5992 (re-search-backward "^\\*+[ \t]+" nil t)
5993 (goto-char (match-end 0))
5994 (let* ((category (org-get-category))
5995 (level (make-string (org-reduced-level (org-outline-level))
5996 ?\s))
5997 (head (buffer-substring (point) (line-end-position)))
5998 (inherited-tags
5999 (or (eq org-agenda-show-inherited-tags 'always)
6000 (and (listp org-agenda-show-inherited-tags)
6001 (memq 'agenda org-agenda-show-inherited-tags))
6002 (and (eq org-agenda-show-inherited-tags t)
6003 (or (eq org-agenda-use-tag-inheritance t)
6004 (memq 'agenda
6005 org-agenda-use-tag-inheritance)))))
6006 (tags (org-get-tags-at nil (not inherited-tags)))
6007 (time
6008 (cond
6009 ;; No time of day designation if it is only
6010 ;; a reminder.
6011 ((and (/= current deadline) (/= current repeat)) nil)
6012 ((string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6013 (concat (substring s (match-beginning 1)) " "))
6014 (t 'time)))
6015 (item
6016 (org-agenda-format-item
6017 ;; Insert appropriate suffixes before deadlines.
6018 ;; Those only apply to today agenda.
6019 (pcase-let ((`(,now ,future ,past)
6020 org-agenda-deadline-leaders))
6021 (cond
6022 ((and today? (< deadline today)) (format past (- diff)))
6023 ((and today? (> deadline today)) (format future diff))
6024 (t now)))
6025 head level category tags time))
6026 (face (org-agenda-deadline-face
6027 (- 1 (/ (float diff) (max wdays 1)))))
6028 (upcoming? (and today? (> deadline today)))
6029 (warntime (get-text-property (point) 'org-appt-warntime)))
6030 (org-add-props item props
6031 'org-marker (org-agenda-new-marker pos)
6032 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
6033 'warntime warntime
6034 'level level
6035 'ts-date deadline
6036 'priority
6037 ;; Adjust priority to today reminders about deadlines.
6038 ;; Overdue deadlines get the highest priority
6039 ;; increase, then imminent deadlines and eventually
6040 ;; more distant deadlines.
6041 (let ((adjust (if today? (- diff) 0)))
6042 (+ adjust (org-get-priority item)))
6043 'todo-state todo-state
6044 'type (if upcoming? "upcoming-deadline" "deadline")
6045 'date (if upcoming? date deadline)
6046 'face (if done? 'org-agenda-done face)
6047 'undone-face face
6048 'done-face 'org-agenda-done)
6049 (push item deadline-items))))))
6050 (nreverse deadline-items)))
6052 (defun org-agenda-deadline-face (fraction)
6053 "Return the face to displaying a deadline item.
6054 FRACTION is what fraction of the head-warning time has passed."
6055 (assoc-default fraction org-agenda-deadline-faces #'<=))
6057 (defun org-agenda-get-scheduled (&optional deadlines with-hour)
6058 "Return the scheduled information for agenda display.
6059 Optional argument DEADLINES is a list of deadline items to be
6060 displayed in agenda view. When WITH-HOUR is non-nil, only return
6061 scheduled items with an hour specification like [h]h:mm."
6062 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
6063 'org-todo-regexp org-todo-regexp
6064 'org-complex-heading-regexp org-complex-heading-regexp
6065 'done-face 'org-agenda-done
6066 'mouse-face 'highlight
6067 'help-echo
6068 (format "mouse-2 or RET jump to Org file %s"
6069 (abbreviate-file-name buffer-file-name))))
6070 (regexp (if with-hour
6071 org-scheduled-time-hour-regexp
6072 org-scheduled-time-regexp))
6073 (today (org-today))
6074 (todayp (org-agenda-today-p date)) ; DATE bound by calendar.
6075 (current (calendar-absolute-from-gregorian date))
6076 (deadline-pos
6077 (mapcar (lambda (d)
6078 (let ((m (get-text-property 0 'org-hd-marker d)))
6079 (and m (marker-position m))))
6080 deadlines))
6081 scheduled-items)
6082 (goto-char (point-min))
6083 (while (re-search-forward regexp nil t)
6084 (catch :skip
6085 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
6086 (org-agenda-skip)
6087 (let* ((s (match-string 1))
6088 (pos (1- (match-beginning 1)))
6089 (todo-state (save-match-data (org-get-todo-state)))
6090 (donep (member todo-state org-done-keywords))
6091 (sexp? (string-prefix-p "%%" s))
6092 ;; SCHEDULE is the scheduled date for the entry. It is
6093 ;; either the bare date or the last repeat, according
6094 ;; to `org-agenda-prefer-last-repeat'.
6095 (schedule
6096 (cond
6097 (sexp? (org-agenda--timestamp-to-absolute s current))
6098 ((or (eq org-agenda-prefer-last-repeat t)
6099 (member todo-state org-agenda-prefer-last-repeat))
6100 (org-agenda--timestamp-to-absolute
6101 s today 'past (current-buffer) pos))
6102 (t (org-agenda--timestamp-to-absolute s))))
6103 ;; REPEAT is the future repeat closest from CURRENT,
6104 ;; according to `org-agenda-show-future-repeats'. If
6105 ;; the latter is nil, or if the time stamp has no
6106 ;; repeat part, default to SCHEDULE.
6107 (repeat
6108 (cond
6109 (sexp? schedule)
6110 ((<= current today) schedule)
6111 ((not org-agenda-show-future-repeats) schedule)
6113 (let ((base (if (eq org-agenda-show-future-repeats 'next)
6114 (1+ today)
6115 current)))
6116 (org-agenda--timestamp-to-absolute
6117 s base 'future (current-buffer) pos)))))
6118 (diff (- current schedule))
6119 (warntime (get-text-property (point) 'org-appt-warntime))
6120 (pastschedp (< schedule today))
6121 (habitp (and (fboundp 'org-is-habit-p) (org-is-habit-p)))
6122 (suppress-delay
6123 (let ((deadline (and org-agenda-skip-scheduled-delay-if-deadline
6124 (org-entry-get nil "DEADLINE"))))
6125 (cond
6126 ((not deadline) nil)
6127 ;; The current item has a deadline date, so
6128 ;; evaluate its delay time.
6129 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6130 ;; Use global delay time.
6131 (- org-agenda-skip-scheduled-delay-if-deadline))
6132 ((eq org-agenda-skip-scheduled-delay-if-deadline
6133 'post-deadline)
6134 ;; Set delay to no later than DEADLINE.
6135 (min (- schedule
6136 (org-agenda--timestamp-to-absolute deadline))
6137 org-scheduled-delay-days))
6138 (t 0))))
6139 (ddays
6140 (cond
6141 ;; Nullify delay when a repeater triggered already
6142 ;; and the delay is of the form --Xd.
6143 ((and (string-match-p "--[0-9]+[hdwmy]" s)
6144 (> current schedule))
6146 (suppress-delay
6147 (let ((org-scheduled-delay-days suppress-delay))
6148 (org-get-wdays s t t)))
6149 (t (org-get-wdays s t)))))
6150 ;; Display scheduled items at base date (SCHEDULE), today if
6151 ;; scheduled before the current date, and at any repeat past
6152 ;; today. However, skip delayed items and items that have
6153 ;; been displayed for more than `org-scheduled-past-days'.
6154 (unless (and todayp
6155 habitp
6156 (bound-and-true-p org-habit-show-all-today))
6157 (when (or (and (> ddays 0) (< diff ddays))
6158 (> diff org-scheduled-past-days)
6159 (> schedule current)
6160 (and (/= current schedule)
6161 (/= current today)
6162 (/= current repeat)))
6163 (throw :skip nil)))
6164 ;; Possibly skip done tasks.
6165 (when (and donep
6166 (or org-agenda-skip-scheduled-if-done
6167 (/= schedule current)))
6168 (throw :skip nil))
6169 ;; Skip entry if it already appears as a deadline, per
6170 ;; `org-agenda-skip-scheduled-if-deadline-is-shown'. This
6171 ;; doesn't apply to habits.
6172 (when (pcase org-agenda-skip-scheduled-if-deadline-is-shown
6173 ((guard
6174 (or (not (memq (line-beginning-position 0) deadline-pos))
6175 habitp))
6176 nil)
6177 (`repeated-after-deadline
6178 (let ((deadline (time-to-days
6179 (org-get-deadline-time (point)))))
6180 (and (<= schedule deadline) (> current deadline))))
6181 (`not-today pastschedp)
6182 (`t t)
6183 (_ nil))
6184 (throw :skip nil))
6185 ;; Skip habits if `org-habit-show-habits' is nil, or if we
6186 ;; only show them for today. Also skip done habits.
6187 (when (and habitp
6188 (or donep
6189 (not (bound-and-true-p org-habit-show-habits))
6190 (and (not todayp)
6191 (bound-and-true-p
6192 org-habit-show-habits-only-for-today))))
6193 (throw :skip nil))
6194 (save-excursion
6195 (re-search-backward "^\\*+[ \t]+" nil t)
6196 (goto-char (match-end 0))
6197 (let* ((category (org-get-category))
6198 (inherited-tags
6199 (or (eq org-agenda-show-inherited-tags 'always)
6200 (and (listp org-agenda-show-inherited-tags)
6201 (memq 'agenda org-agenda-show-inherited-tags))
6202 (and (eq org-agenda-show-inherited-tags t)
6203 (or (eq org-agenda-use-tag-inheritance t)
6204 (memq 'agenda
6205 org-agenda-use-tag-inheritance)))))
6206 (tags (org-get-tags-at nil (not inherited-tags)))
6207 (level (make-string (org-reduced-level (org-outline-level))
6208 ?\s))
6209 (head (buffer-substring (point) (line-end-position)))
6210 (time
6211 (cond
6212 ;; No time of day designation if it is only
6213 ;; a reminder.
6214 ((and (/= current schedule) (/= current repeat)) nil)
6215 ((string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6216 (concat (substring s (match-beginning 1)) " "))
6217 (t 'time)))
6218 (item
6219 (org-agenda-format-item
6220 (pcase-let ((`(,first ,past) org-agenda-scheduled-leaders))
6221 ;; Show a reminder of a past scheduled today.
6222 (if (and todayp pastschedp)
6223 (format past diff)
6224 first))
6225 head level category tags time nil habitp))
6226 (face (cond ((and (not habitp) pastschedp)
6227 'org-scheduled-previously)
6228 (todayp 'org-scheduled-today)
6229 (t 'org-scheduled)))
6230 (habitp (and habitp (org-habit-parse-todo))))
6231 (org-add-props item props
6232 'undone-face face
6233 'face (if donep 'org-agenda-done face)
6234 'org-marker (org-agenda-new-marker pos)
6235 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
6236 'type (if pastschedp "past-scheduled" "scheduled")
6237 'date (if pastschedp schedule date)
6238 'ts-date schedule
6239 'warntime warntime
6240 'level level
6241 'priority (if habitp (org-habit-get-priority habitp)
6242 (+ 99 diff (org-get-priority item)))
6243 'org-habit-p habitp
6244 'todo-state todo-state)
6245 (push item scheduled-items))))))
6246 (nreverse scheduled-items)))
6248 (defun org-agenda-get-blocks ()
6249 "Return the date-range information for agenda display."
6250 (let* ((props (list 'face nil
6251 'org-not-done-regexp org-not-done-regexp
6252 'org-todo-regexp org-todo-regexp
6253 'org-complex-heading-regexp org-complex-heading-regexp
6254 'mouse-face 'highlight
6255 'help-echo
6256 (format "mouse-2 or RET jump to org file %s"
6257 (abbreviate-file-name buffer-file-name))))
6258 (regexp org-tr-regexp)
6259 (d0 (calendar-absolute-from-gregorian date))
6260 marker hdmarker ee txt d1 d2 s1 s2 category
6261 level todo-state tags pos head donep inherited-tags)
6262 (goto-char (point-min))
6263 (while (re-search-forward regexp nil t)
6264 (catch :skip
6265 (org-agenda-skip)
6266 (setq pos (point))
6267 (let ((start-time (match-string 1))
6268 (end-time (match-string 2)))
6269 (setq s1 (match-string 1)
6270 s2 (match-string 2)
6271 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
6272 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
6273 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6274 ;; Only allow days between the limits, because the normal
6275 ;; date stamps will catch the limits.
6276 (save-excursion
6277 (setq todo-state (org-get-todo-state))
6278 (setq donep (member todo-state org-done-keywords))
6279 (if (and donep org-agenda-skip-timestamp-if-done)
6280 (throw :skip t))
6281 (setq marker (org-agenda-new-marker (point))
6282 category (org-get-category))
6283 (if (not (re-search-backward org-outline-regexp-bol nil t))
6284 (throw :skip nil)
6285 (goto-char (match-beginning 0))
6286 (setq hdmarker (org-agenda-new-marker (point))
6287 inherited-tags
6288 (or (eq org-agenda-show-inherited-tags 'always)
6289 (and (listp org-agenda-show-inherited-tags)
6290 (memq 'agenda org-agenda-show-inherited-tags))
6291 (and (eq org-agenda-show-inherited-tags t)
6292 (or (eq org-agenda-use-tag-inheritance t)
6293 (memq 'agenda org-agenda-use-tag-inheritance))))
6295 tags (org-get-tags-at nil (not inherited-tags)))
6296 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6297 (looking-at "\\*+[ \t]+\\(.*\\)")
6298 (setq head (match-string 1))
6299 (let ((remove-re
6300 (if org-agenda-remove-timeranges-from-blocks
6301 (concat
6302 "<" (regexp-quote s1) ".*?>"
6303 "--"
6304 "<" (regexp-quote s2) ".*?>")
6305 nil)))
6306 (setq txt (org-agenda-format-item
6307 (format
6308 (nth (if (= d1 d2) 0 1)
6309 org-agenda-timerange-leaders)
6310 (1+ (- d0 d1)) (1+ (- d2 d1)))
6311 head level category tags
6312 (cond ((and (= d1 d0) (= d2 d0))
6313 (concat "<" start-time ">--<" end-time ">"))
6314 ((= d1 d0)
6315 (concat "<" start-time ">"))
6316 ((= d2 d0)
6317 (concat "<" end-time ">")))
6318 remove-re))))
6319 (org-add-props txt props
6320 'org-marker marker 'org-hd-marker hdmarker
6321 'type "block" 'date date
6322 'level level
6323 'todo-state todo-state
6324 'priority (org-get-priority txt))
6325 (push txt ee))))
6326 (goto-char pos)))
6327 ;; Sort the entries by expiration date.
6328 (nreverse ee)))
6330 ;;; Agenda presentation and sorting
6332 (defvar org-prefix-has-time nil
6333 "A flag, set by `org-compile-prefix-format'.
6334 The flag is set if the currently compiled format contains a `%t'.")
6335 (defvar org-prefix-has-tag nil
6336 "A flag, set by `org-compile-prefix-format'.
6337 The flag is set if the currently compiled format contains a `%T'.")
6338 (defvar org-prefix-has-effort nil
6339 "A flag, set by `org-compile-prefix-format'.
6340 The flag is set if the currently compiled format contains a `%e'.")
6341 (defvar org-prefix-has-breadcrumbs nil
6342 "A flag, set by `org-compile-prefix-format'.
6343 The flag is set if the currently compiled format contains a `%b'.")
6344 (defvar org-prefix-category-length nil
6345 "Used by `org-compile-prefix-format' to remember the category field width.")
6346 (defvar org-prefix-category-max-length nil
6347 "Used by `org-compile-prefix-format' to remember the category field width.")
6349 (defun org-agenda-get-category-icon (category)
6350 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6351 (cl-dolist (entry org-agenda-category-icon-alist)
6352 (when (string-match-p (car entry) category)
6353 (if (listp (cadr entry))
6354 (cl-return (cadr entry))
6355 (cl-return (apply #'create-image (cdr entry)))))))
6357 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6358 remove-re habitp)
6359 "Format TXT to be inserted into the agenda buffer.
6360 In particular, add the prefix and corresponding text properties.
6362 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6363 LEVEL may be a string to replace the `%l' specifier.
6364 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6365 category taken from local variable or file name. It will replace the `%c'
6366 specifier in the format.
6367 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6368 TXT for sorting of this entry, and for the `%t' specifier in the format.
6369 When DOTIME is a string, this string is searched for a time before TXT is.
6370 TAGS can be the tags of the headline.
6371 Any match of REMOVE-RE will be removed from TXT."
6372 ;; We keep the org-prefix-* variable values along with a compiled
6373 ;; formatter, so that multiple agendas existing at the same time do
6374 ;; not step on each other toes.
6376 ;; It was inconvenient to make these variables buffer local in
6377 ;; Agenda buffers, because this function expects to be called with
6378 ;; the buffer where item comes from being current, and not agenda
6379 ;; buffer
6380 (let* ((bindings (car org-prefix-format-compiled))
6381 (formatter (cadr org-prefix-format-compiled)))
6382 (cl-loop for (var value) in bindings
6383 do (set var value))
6384 (save-match-data
6385 ;; Diary entries sometimes have extra whitespace at the beginning
6386 (setq txt (org-trim txt))
6388 ;; Fix the tags part in txt
6389 (setq txt (org-agenda-fix-displayed-tags
6390 txt tags
6391 org-agenda-show-inherited-tags
6392 org-agenda-hide-tags-regexp))
6394 (let* ((category (or category
6395 (if buffer-file-name
6396 (file-name-sans-extension
6397 (file-name-nondirectory buffer-file-name))
6398 "")))
6399 (category-icon (org-agenda-get-category-icon category))
6400 (category-icon (if category-icon
6401 (propertize " " 'display category-icon)
6402 ""))
6403 (effort (and (not (string= txt ""))
6404 (get-text-property 1 'effort txt)))
6405 ;; time, tag, effort are needed for the eval of the prefix format
6406 (tag (if tags (nth (1- (length tags)) tags) ""))
6407 time
6408 (ts (if dotime (concat
6409 (if (stringp dotime) dotime "")
6410 (and org-agenda-search-headline-for-time txt))))
6411 (time-of-day (and dotime (org-get-time-of-day ts)))
6412 stamp plain s0 s1 s2 rtn srp l
6413 duration breadcrumbs)
6414 (and (derived-mode-p 'org-mode) buffer-file-name
6415 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6416 (when (and dotime time-of-day)
6417 ;; Extract starting and ending time and move them to prefix
6418 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6419 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6420 (setq s0 (match-string 0 ts)
6421 srp (and stamp (match-end 3))
6422 s1 (match-string (if plain 1 2) ts)
6423 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6425 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6426 ;; them, we might want to remove them there to avoid duplication.
6427 ;; The user can turn this off with a variable.
6428 (if (and org-prefix-has-time
6429 org-agenda-remove-times-when-in-prefix (or stamp plain)
6430 (string-match (concat (regexp-quote s0) " *") txt)
6431 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6432 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6433 (= (match-beginning 0) 0)
6435 (setq txt (replace-match "" nil nil txt))))
6436 ;; Normalize the time(s) to 24 hour
6437 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6438 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6440 ;; Try to set s2 if s1 and
6441 ;; `org-agenda-default-appointment-duration' are set
6442 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6443 (setq s2
6444 (org-duration-from-minutes
6445 (+ (org-duration-to-minutes s1 t)
6446 org-agenda-default-appointment-duration)
6447 nil t)))
6449 ;; Compute the duration
6450 (when s2
6451 (setq duration (- (org-duration-to-minutes s2)
6452 (org-duration-to-minutes s1)))))
6454 (when (string-match "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" txt)
6455 ;; Tags are in the string
6456 (if (or (eq org-agenda-remove-tags t)
6457 (and org-agenda-remove-tags
6458 org-prefix-has-tag))
6459 (setq txt (replace-match "" t t txt))
6460 (setq txt (replace-match
6461 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6462 (match-string 2 txt))
6463 t t txt))))
6465 (when remove-re
6466 (while (string-match remove-re txt)
6467 (setq txt (replace-match "" t t txt))))
6469 ;; Set org-heading property on `txt' to mark the start of the
6470 ;; heading.
6471 (add-text-properties 0 (length txt) '(org-heading t) txt)
6473 ;; Prepare the variables needed in the eval of the compiled format
6474 (if org-prefix-has-breadcrumbs
6475 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6476 (let ((s (org-display-outline-path nil nil "->" t)))
6477 (if (eq "" s) "" (concat s "->"))))))
6478 (setq time (cond (s2 (concat
6479 (org-agenda-time-of-day-to-ampm-maybe s1)
6480 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6481 (if org-agenda-timegrid-use-ampm " ")))
6482 (s1 (concat
6483 (org-agenda-time-of-day-to-ampm-maybe s1)
6484 (if org-agenda-timegrid-use-ampm
6485 "........ "
6486 "......")))
6487 (t ""))
6488 extra (or (and (not habitp) extra) "")
6489 category (if (symbolp category) (symbol-name category) category)
6490 level (or level ""))
6491 (if (string-match org-bracket-link-regexp category)
6492 (progn
6493 (setq l (if (match-end 3)
6494 (- (match-end 3) (match-beginning 3))
6495 (- (match-end 1) (match-beginning 1))))
6496 (when (< l (or org-prefix-category-length 0))
6497 (setq category (copy-sequence category))
6498 (org-add-props category nil
6499 'extra-space (make-string
6500 (- org-prefix-category-length l 1) ?\ ))))
6501 (if (and org-prefix-category-max-length
6502 (>= (length category) org-prefix-category-max-length))
6503 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6504 ;; Evaluate the compiled format
6505 (setq rtn (concat (eval formatter) txt))
6507 ;; And finally add the text properties
6508 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6509 (org-add-props rtn nil
6510 'org-category category
6511 'tags (mapcar 'org-downcase-keep-props tags)
6512 'org-highest-priority org-highest-priority
6513 'org-lowest-priority org-lowest-priority
6514 'time-of-day time-of-day
6515 'duration duration
6516 'breadcrumbs breadcrumbs
6517 'txt txt
6518 'level level
6519 'time time
6520 'extra extra
6521 'format org-prefix-format-compiled
6522 'dotime dotime)))))
6524 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6525 "Remove tags string from TXT, and add a modified list of tags.
6526 The modified list may contain inherited tags, and tags matched by
6527 `org-agenda-hide-tags-regexp' will be removed."
6528 (when (or add-inherited hide-re)
6529 (if (string-match "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" txt)
6530 (setq txt (substring txt 0 (match-beginning 0))))
6531 (setq tags
6532 (delq nil
6533 (mapcar (lambda (tg)
6534 (if (or (and hide-re (string-match hide-re tg))
6535 (and (not add-inherited)
6536 (get-text-property 0 'inherited tg)))
6538 tg))
6539 tags)))
6540 (when tags
6541 (let ((have-i (get-text-property 0 'inherited (car tags)))
6543 (setq txt (concat txt " :"
6544 (mapconcat
6545 (lambda (x)
6546 (setq i (get-text-property 0 'inherited x))
6547 (if (and have-i (not i))
6548 (progn
6549 (setq have-i nil)
6550 (concat ":" x))
6552 tags ":")
6553 (if have-i "::" ":"))))))
6554 txt)
6556 (defun org-downcase-keep-props (s)
6557 (let ((props (text-properties-at 0 s)))
6558 (setq s (downcase s))
6559 (add-text-properties 0 (length s) props s)
6562 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6564 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6565 "Add a time-grid for agenda items which need it.
6567 LIST is the list of agenda items formatted by `org-agenda-list'.
6568 NDAYS is the span of the current agenda view.
6569 TODAYP is t when the current agenda view is on today."
6570 (catch 'exit
6571 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6572 ((and todayp (member 'today (car org-agenda-time-grid))))
6573 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6574 ((member 'weekly (car org-agenda-time-grid)))
6575 (t (throw 'exit list)))
6576 (let* ((have (delq nil (mapcar
6577 (lambda (x) (get-text-property 1 'time-of-day x))
6578 list)))
6579 (string (nth 1 org-agenda-time-grid))
6580 (gridtimes (nth 2 org-agenda-time-grid))
6581 (req (car org-agenda-time-grid))
6582 (remove (member 'remove-match req))
6583 new time)
6584 (if (and (member 'require-timed req) (not have))
6585 ;; don't show empty grid
6586 (throw 'exit list))
6587 (while (setq time (pop gridtimes))
6588 (unless (and remove (member time have))
6589 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6590 (push (org-agenda-format-item
6591 nil string nil "" nil
6592 (concat (substring time 0 -2) ":" (substring time -2)))
6593 new)
6594 (put-text-property
6595 2 (length (car new)) 'face 'org-time-grid (car new))))
6596 (when (and todayp org-agenda-show-current-time-in-grid)
6597 (push (org-agenda-format-item
6598 nil org-agenda-current-time-string nil "" nil
6599 (format-time-string "%H:%M "))
6600 new)
6601 (put-text-property
6602 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6604 (if (member 'time-up org-agenda-sorting-strategy-selected)
6605 (append new list)
6606 (append list new)))))
6608 (defun org-compile-prefix-format (key)
6609 "Compile the prefix format into a Lisp form that can be evaluated.
6610 The resulting form and associated variable bindings is returned
6611 and stored in the variable `org-prefix-format-compiled'."
6612 (setq org-prefix-has-time nil
6613 org-prefix-has-tag nil
6614 org-prefix-category-length nil
6615 org-prefix-has-effort nil
6616 org-prefix-has-breadcrumbs nil)
6617 (let ((s (cond
6618 ((stringp org-agenda-prefix-format)
6619 org-agenda-prefix-format)
6620 ((assq key org-agenda-prefix-format)
6621 (cdr (assq key org-agenda-prefix-format)))
6622 (t " %-12:c%?-12t% s")))
6623 (start 0)
6624 varform vars var e c f opt)
6625 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6626 s start)
6627 (setq var (or (cdr (assoc (match-string 4 s)
6628 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6629 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6630 'eval)
6631 c (or (match-string 3 s) "")
6632 opt (match-beginning 1)
6633 start (1+ (match-beginning 0)))
6634 (if (eq var 'time) (setq org-prefix-has-time t))
6635 (if (eq var 'tag) (setq org-prefix-has-tag t))
6636 (if (eq var 'effort) (setq org-prefix-has-effort t))
6637 (if (eq var 'breadcrumbs) (setq org-prefix-has-breadcrumbs t))
6638 (setq f (concat "%" (match-string 2 s) "s"))
6639 (when (eq var 'category)
6640 (setq org-prefix-category-length
6641 (floor (abs (string-to-number (match-string 2 s)))))
6642 (setq org-prefix-category-max-length
6643 (let ((x (match-string 2 s)))
6644 (save-match-data
6645 (if (string-match "\\.[0-9]+" x)
6646 (string-to-number (substring (match-string 0 x) 1)))))))
6647 (if (eq var 'eval)
6648 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6649 (if opt
6650 (setq varform
6651 `(if (or (equal "" ,var) (equal nil ,var))
6653 (format ,f (concat ,var ,c))))
6654 (setq varform
6655 `(format ,f (if (or (equal ,var "")
6656 (equal ,var nil)) ""
6657 (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6658 (setq s (replace-match "%s" t nil s))
6659 (push varform vars))
6660 (setq vars (nreverse vars))
6661 (with-current-buffer (or org-agenda-buffer (current-buffer))
6662 (setq org-prefix-format-compiled
6663 (list
6664 `((org-prefix-has-time ,org-prefix-has-time)
6665 (org-prefix-has-tag ,org-prefix-has-tag)
6666 (org-prefix-category-length ,org-prefix-category-length)
6667 (org-prefix-has-effort ,org-prefix-has-effort)
6668 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6669 `(format ,s ,@vars))))))
6671 (defun org-set-sorting-strategy (key)
6672 (if (symbolp (car org-agenda-sorting-strategy))
6673 ;; the old format
6674 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6675 (setq org-agenda-sorting-strategy-selected
6676 (or (cdr (assq key org-agenda-sorting-strategy))
6677 (cdr (assq 'agenda org-agenda-sorting-strategy))
6678 '(time-up category-keep priority-down)))))
6680 (defun org-get-time-of-day (s &optional string mod24)
6681 "Check string S for a time of day.
6682 If found, return it as a military time number between 0 and 2400.
6683 If not found, return nil.
6684 The optional STRING argument forces conversion into a 5 character wide string
6685 HH:MM."
6686 (save-match-data
6687 (when
6688 (and
6689 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6690 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6691 (not (eq (get-text-property 1 'face s) 'org-link)))
6692 (let* ((h (string-to-number (match-string 1 s)))
6693 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6694 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6695 (am-p (equal ampm "am"))
6696 (h1 (cond ((not ampm) h)
6697 ((= h 12) (if am-p 0 12))
6698 (t (+ h (if am-p 0 12)))))
6699 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6700 (mod h1 24) h1))
6701 (t0 (+ (* 100 h2) m))
6702 (t1 (concat (if (>= h1 24) "+" " ")
6703 (if (and org-agenda-time-leading-zero
6704 (< t0 1000)) "0" "")
6705 (if (< t0 100) "0" "")
6706 (if (< t0 10) "0" "")
6707 (int-to-string t0))))
6708 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6710 (defvar org-agenda-before-sorting-filter-function nil
6711 "Function to be applied to agenda items prior to sorting.
6712 Prior to sorting also means just before they are inserted into the agenda.
6714 To aid sorting, you may revisit the original entries and add more text
6715 properties which will later be used by the sorting functions.
6717 The function should take a string argument, an agenda line.
6718 It has access to the text properties in that line, which contain among
6719 other things, the property `org-hd-marker' that points to the entry
6720 where the line comes from. Note that not all lines going into the agenda
6721 have this property, only most.
6723 The function should return the modified string. It is probably best
6724 to ONLY change text properties.
6726 You can also use this function as a filter, by returning nil for lines
6727 you don't want to have in the agenda at all. For this application, you
6728 could bind the variable in the options section of a custom command.")
6730 (defun org-agenda-finalize-entries (list &optional type)
6731 "Sort, limit and concatenate the LIST of agenda items.
6732 The optional argument TYPE tells the agenda type."
6733 (let ((max-effort (cond ((listp org-agenda-max-effort)
6734 (cdr (assoc type org-agenda-max-effort)))
6735 (t org-agenda-max-effort)))
6736 (max-todo (cond ((listp org-agenda-max-todos)
6737 (cdr (assoc type org-agenda-max-todos)))
6738 (t org-agenda-max-todos)))
6739 (max-tags (cond ((listp org-agenda-max-tags)
6740 (cdr (assoc type org-agenda-max-tags)))
6741 (t org-agenda-max-tags)))
6742 (max-entries (cond ((listp org-agenda-max-entries)
6743 (cdr (assoc type org-agenda-max-entries)))
6744 (t org-agenda-max-entries))))
6745 (when org-agenda-before-sorting-filter-function
6746 (setq list
6747 (delq nil
6748 (mapcar
6749 org-agenda-before-sorting-filter-function list))))
6750 (setq list (mapcar 'org-agenda-highlight-todo list)
6751 list (mapcar 'identity (sort list 'org-entries-lessp)))
6752 (when max-effort
6753 (setq list (org-agenda-limit-entries
6754 list 'effort-minutes max-effort
6755 (lambda (e) (or e (if org-sort-agenda-noeffort-is-high
6756 32767 -1))))))
6757 (when max-todo
6758 (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
6759 (when max-tags
6760 (setq list (org-agenda-limit-entries list 'tags max-tags)))
6761 (when max-entries
6762 (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
6763 (mapconcat 'identity list "\n")))
6765 (defun org-agenda-limit-entries (list prop limit &optional fn)
6766 "Limit the number of agenda entries."
6767 (let ((include (and limit (< limit 0))))
6768 (if limit
6769 (let ((fun (or fn (lambda (p) (if p 1))))
6770 (lim 0))
6771 (delq nil
6772 (mapcar
6773 (lambda (e)
6774 (let ((pval (funcall
6775 fun (get-text-property (1- (length e))
6776 prop e))))
6777 (if pval (setq lim (+ lim pval)))
6778 (cond ((and pval (<= lim (abs limit))) e)
6779 ((and include (not pval)) e))))
6780 list)))
6781 list)))
6783 (defun org-agenda-limit-interactively (remove)
6784 "In agenda, interactively limit entries to various maximums."
6785 (interactive "P")
6786 (if remove
6787 (progn (setq org-agenda-max-entries nil
6788 org-agenda-max-todos nil
6789 org-agenda-max-tags nil
6790 org-agenda-max-effort nil)
6791 (org-agenda-redo))
6792 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6793 (msg (cond ((= max ?E) "How many minutes? ")
6794 ((= max ?e) "How many entries? ")
6795 ((= max ?t) "How many TODO entries? ")
6796 ((= max ?T) "How many tagged entries? ")
6797 (t (user-error "Wrong input"))))
6798 (num (string-to-number (read-from-minibuffer msg))))
6799 (cond ((equal max ?e)
6800 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6801 ((equal max ?t)
6802 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6803 ((equal max ?T)
6804 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6805 ((equal max ?E)
6806 (let ((org-agenda-max-effort num)) (org-agenda-redo))))))
6807 (org-agenda-fit-window-to-buffer))
6809 (defun org-agenda-highlight-todo (x)
6810 (let ((org-done-keywords org-done-keywords-for-agenda)
6811 (case-fold-search nil)
6813 (if (eq x 'line)
6814 (save-excursion
6815 (beginning-of-line 1)
6816 (setq re (org-get-at-bol 'org-todo-regexp))
6817 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6818 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6819 (add-text-properties (match-beginning 0) (match-end 1)
6820 (list 'face (org-get-todo-face 1)))
6821 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6822 (delete-region (match-beginning 1) (1- (match-end 0)))
6823 (goto-char (match-beginning 1))
6824 (insert (format org-agenda-todo-keyword-format s)))))
6825 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6826 (setq re (get-text-property 0 'org-todo-regexp x))
6827 (when (and re
6828 ;; Test `pl' because if there's no heading content,
6829 ;; there's no point matching to highlight. Note
6830 ;; that if we didn't test `pl' first, and there
6831 ;; happened to be no keyword from `org-todo-regexp'
6832 ;; on this heading line, then the `equal' comparison
6833 ;; afterwards would spuriously succeed in the case
6834 ;; where `pl' is nil -- causing an args-out-of-range
6835 ;; error when we try to add text properties to text
6836 ;; that isn't there.
6838 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6839 x pl) pl))
6840 (add-text-properties
6841 (or (match-end 1) (match-end 0)) (match-end 0)
6842 (list 'face (org-get-todo-face (match-string 2 x)))
6844 (when (match-end 1)
6845 (setq x (concat (substring x 0 (match-end 1))
6846 (format org-agenda-todo-keyword-format
6847 (match-string 2 x))
6848 (org-add-props " " (text-properties-at 0 x))
6849 (substring x (match-end 3)))))))
6850 x)))
6852 (defsubst org-cmp-values (a b property)
6853 "Compare the numeric value of text PROPERTY for string A and B."
6854 (let ((pa (or (get-text-property (1- (length a)) property a) 0))
6855 (pb (or (get-text-property (1- (length b)) property b) 0)))
6856 (cond ((> pa pb) +1)
6857 ((< pa pb) -1))))
6859 (defsubst org-cmp-effort (a b)
6860 "Compare the effort values of string A and B."
6861 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6862 ;; `effort-minutes' property is not directly accessible from
6863 ;; the strings, but is stored as a property in `txt'.
6864 (ea (or (get-text-property
6865 0 'effort-minutes (get-text-property 0 'txt a))
6866 def))
6867 (eb (or (get-text-property
6868 0 'effort-minutes (get-text-property 0 'txt b))
6869 def)))
6870 (cond ((> ea eb) +1)
6871 ((< ea eb) -1))))
6873 (defsubst org-cmp-category (a b)
6874 "Compare the string values of categories of strings A and B."
6875 (let ((ca (or (get-text-property (1- (length a)) 'org-category a) ""))
6876 (cb (or (get-text-property (1- (length b)) 'org-category b) "")))
6877 (cond ((string-lessp ca cb) -1)
6878 ((string-lessp cb ca) +1))))
6880 (defsubst org-cmp-todo-state (a b)
6881 "Compare the todo states of strings A and B."
6882 (let* ((ma (or (get-text-property 1 'org-marker a)
6883 (get-text-property 1 'org-hd-marker a)))
6884 (mb (or (get-text-property 1 'org-marker b)
6885 (get-text-property 1 'org-hd-marker b)))
6886 (fa (and ma (marker-buffer ma)))
6887 (fb (and mb (marker-buffer mb)))
6888 (todo-kwds
6889 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6890 (and fb (with-current-buffer fb org-todo-keywords-1))))
6891 (ta (or (get-text-property 1 'todo-state a) ""))
6892 (tb (or (get-text-property 1 'todo-state b) ""))
6893 (la (- (length (member ta todo-kwds))))
6894 (lb (- (length (member tb todo-kwds))))
6895 (donepa (member ta org-done-keywords-for-agenda))
6896 (donepb (member tb org-done-keywords-for-agenda)))
6897 (cond ((and donepa (not donepb)) -1)
6898 ((and (not donepa) donepb) +1)
6899 ((< la lb) -1)
6900 ((< lb la) +1))))
6902 (defsubst org-cmp-alpha (a b)
6903 "Compare the headlines, alphabetically."
6904 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6905 (plb (text-property-any 0 (length b) 'org-heading t b))
6906 (ta (and pla (substring a pla)))
6907 (tb (and plb (substring b plb)))
6908 (case-fold-search nil))
6909 (when pla
6910 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6911 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6912 (setq ta (substring ta (match-end 0))))
6913 (setq ta (downcase ta)))
6914 (when plb
6915 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6916 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6917 (setq tb (substring tb (match-end 0))))
6918 (setq tb (downcase tb)))
6919 (cond ((not ta) +1)
6920 ((not tb) -1)
6921 ((string-lessp ta tb) -1)
6922 ((string-lessp tb ta) +1))))
6924 (defsubst org-cmp-tag (a b)
6925 "Compare the string values of the first tags of A and B."
6926 (let ((ta (car (last (get-text-property 1 'tags a))))
6927 (tb (car (last (get-text-property 1 'tags b)))))
6928 (cond ((not ta) +1)
6929 ((not tb) -1)
6930 ((string-lessp ta tb) -1)
6931 ((string-lessp tb ta) +1))))
6933 (defsubst org-cmp-time (a b)
6934 "Compare the time-of-day values of strings A and B."
6935 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6936 (ta (or (get-text-property 1 'time-of-day a) def))
6937 (tb (or (get-text-property 1 'time-of-day b) def)))
6938 (cond ((< ta tb) -1)
6939 ((< tb ta) +1))))
6941 (defsubst org-cmp-ts (a b type)
6942 "Compare the timestamps values of entries A and B.
6943 When TYPE is \"scheduled\", \"deadline\", \"timestamp\" or
6944 \"timestamp_ia\", compare within each of these type. When TYPE
6945 is the empty string, compare all timestamps without respect of
6946 their type."
6947 (let* ((def (if org-sort-agenda-notime-is-late most-positive-fixnum -1))
6948 (ta (or (and (string-match type (or (get-text-property 1 'type a) ""))
6949 (get-text-property 1 'ts-date a))
6950 def))
6951 (tb (or (and (string-match type (or (get-text-property 1 'type b) ""))
6952 (get-text-property 1 'ts-date b))
6953 def)))
6954 (cond ((< ta tb) -1)
6955 ((< tb ta) +1))))
6957 (defsubst org-cmp-habit-p (a b)
6958 "Compare the todo states of strings A and B."
6959 (let ((ha (get-text-property 1 'org-habit-p a))
6960 (hb (get-text-property 1 'org-habit-p b)))
6961 (cond ((and ha (not hb)) -1)
6962 ((and (not ha) hb) +1))))
6964 (defun org-entries-lessp (a b)
6965 "Predicate for sorting agenda entries."
6966 ;; The following variables will be used when the form is evaluated.
6967 ;; So even though the compiler complains, keep them.
6968 (let* ((ss org-agenda-sorting-strategy-selected)
6969 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
6970 (org-cmp-ts a b "")))
6971 (timestamp-down (if timestamp-up (- timestamp-up) nil))
6972 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
6973 (org-cmp-ts a b "scheduled")))
6974 (scheduled-down (if scheduled-up (- scheduled-up) nil))
6975 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
6976 (org-cmp-ts a b "deadline")))
6977 (deadline-down (if deadline-up (- deadline-up) nil))
6978 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
6979 (org-cmp-ts a b "timestamp_ia")))
6980 (tsia-down (if tsia-up (- tsia-up) nil))
6981 (ts-up (and (org-em 'ts-up 'ts-down ss)
6982 (org-cmp-ts a b "timestamp")))
6983 (ts-down (if ts-up (- ts-up) nil))
6984 (time-up (and (org-em 'time-up 'time-down ss)
6985 (org-cmp-time a b)))
6986 (time-down (if time-up (- time-up) nil))
6987 (stats-up (and (org-em 'stats-up 'stats-down ss)
6988 (org-cmp-values a b 'org-stats)))
6989 (stats-down (if stats-up (- stats-up) nil))
6990 (priority-up (and (org-em 'priority-up 'priority-down ss)
6991 (org-cmp-values a b 'priority)))
6992 (priority-down (if priority-up (- priority-up) nil))
6993 (effort-up (and (org-em 'effort-up 'effort-down ss)
6994 (org-cmp-effort a b)))
6995 (effort-down (if effort-up (- effort-up) nil))
6996 (category-up (and (or (org-em 'category-up 'category-down ss)
6997 (memq 'category-keep ss))
6998 (org-cmp-category a b)))
6999 (category-down (if category-up (- category-up) nil))
7000 (category-keep (if category-up +1 nil))
7001 (tag-up (and (org-em 'tag-up 'tag-down ss)
7002 (org-cmp-tag a b)))
7003 (tag-down (if tag-up (- tag-up) nil))
7004 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
7005 (org-cmp-todo-state a b)))
7006 (todo-state-down (if todo-state-up (- todo-state-up) nil))
7007 (habit-up (and (org-em 'habit-up 'habit-down ss)
7008 (org-cmp-habit-p a b)))
7009 (habit-down (if habit-up (- habit-up) nil))
7010 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
7011 (org-cmp-alpha a b)))
7012 (alpha-down (if alpha-up (- alpha-up) nil))
7013 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
7014 user-defined-up user-defined-down)
7015 (if (and need-user-cmp org-agenda-cmp-user-defined
7016 (functionp org-agenda-cmp-user-defined))
7017 (setq user-defined-up
7018 (funcall org-agenda-cmp-user-defined a b)
7019 user-defined-down (if user-defined-up (- user-defined-up) nil)))
7020 (cdr (assoc
7021 (eval (cons 'or org-agenda-sorting-strategy-selected))
7022 '((-1 . t) (1 . nil) (nil . nil))))))
7024 ;;; Agenda restriction lock
7026 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
7027 "Overlay to mark the headline to which agenda commands are restricted.")
7028 (overlay-put org-agenda-restriction-lock-overlay
7029 'face 'org-agenda-restriction-lock)
7030 (overlay-put org-agenda-restriction-lock-overlay
7031 'help-echo "Agendas are currently limited to this subtree.")
7032 (delete-overlay org-agenda-restriction-lock-overlay)
7034 (defun org-agenda-set-restriction-lock-from-agenda (arg)
7035 "Set the restriction lock to the agenda item at point from within the agenda.
7036 When called with a `\\[universal-argument]' prefix, restrict to
7037 the file which contains the item.
7038 Argument ARG is the prefix argument."
7039 (interactive "P")
7040 (unless (derived-mode-p 'org-agenda-mode)
7041 (user-error "Not in an Org agenda buffer"))
7042 (let* ((marker (or (org-get-at-bol 'org-marker)
7043 (org-agenda-error)))
7044 (buffer (marker-buffer marker))
7045 (pos (marker-position marker)))
7046 (with-current-buffer buffer
7047 (goto-char pos)
7048 (org-agenda-set-restriction-lock arg))))
7050 ;;;###autoload
7051 (defun org-agenda-set-restriction-lock (&optional type)
7052 "Set restriction lock for agenda, to current subtree or file.
7053 Restriction will be the file if TYPE is `file', or if type is the
7054 universal prefix \\='(4), or if the cursor is before the first headline
7055 in the file. Otherwise, restriction will be to the current subtree."
7056 (interactive "P")
7057 (org-agenda-remove-restriction-lock 'noupdate)
7058 (and (equal type '(4)) (setq type 'file))
7059 (setq type (cond
7060 (type type)
7061 ((org-at-heading-p) 'subtree)
7062 ((condition-case nil (org-back-to-heading t) (error nil))
7063 'subtree)
7064 (t 'file)))
7065 (if (eq type 'subtree)
7066 (progn
7067 (setq org-agenda-restrict (current-buffer))
7068 (setq org-agenda-overriding-restriction 'subtree)
7069 (put 'org-agenda-files 'org-restrict
7070 (list (buffer-file-name (buffer-base-buffer))))
7071 (org-back-to-heading t)
7072 (move-overlay org-agenda-restriction-lock-overlay
7073 (point)
7074 (if org-agenda-restriction-lock-highlight-subtree
7075 (save-excursion (org-end-of-subtree t t) (point))
7076 (point-at-eol)))
7077 (move-marker org-agenda-restrict-begin (point))
7078 (move-marker org-agenda-restrict-end
7079 (save-excursion (org-end-of-subtree t t)))
7080 (message "Locking agenda restriction to subtree"))
7081 (put 'org-agenda-files 'org-restrict
7082 (list (buffer-file-name (buffer-base-buffer))))
7083 (setq org-agenda-restrict nil)
7084 (setq org-agenda-overriding-restriction 'file)
7085 (move-marker org-agenda-restrict-begin nil)
7086 (move-marker org-agenda-restrict-end nil)
7087 (message "Locking agenda restriction to file"))
7088 (setq current-prefix-arg nil)
7089 (org-agenda-maybe-redo))
7091 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7092 "Remove the agenda restriction lock."
7093 (interactive "P")
7094 (delete-overlay org-agenda-restriction-lock-overlay)
7095 (delete-overlay org-speedbar-restriction-lock-overlay)
7096 (setq org-agenda-overriding-restriction nil)
7097 (setq org-agenda-restrict nil)
7098 (put 'org-agenda-files 'org-restrict nil)
7099 (move-marker org-agenda-restrict-begin nil)
7100 (move-marker org-agenda-restrict-end nil)
7101 (setq current-prefix-arg nil)
7102 (message "Agenda restriction lock removed")
7103 (or noupdate (org-agenda-maybe-redo)))
7105 (defun org-agenda-maybe-redo ()
7106 "If there is any window showing the agenda view, update it."
7107 (let ((w (get-buffer-window (or org-agenda-this-buffer-name
7108 org-agenda-buffer-name)
7110 (w0 (selected-window)))
7111 (when w
7112 (select-window w)
7113 (org-agenda-redo)
7114 (select-window w0)
7115 (if org-agenda-overriding-restriction
7116 (message "Agenda view shifted to new %s restriction"
7117 org-agenda-overriding-restriction)
7118 (message "Agenda restriction lock removed")))))
7120 ;;; Agenda commands
7122 (defun org-agenda-check-type (error &rest types)
7123 "Check if agenda buffer is of allowed type.
7124 If ERROR is non-nil, throw an error, otherwise just return nil.
7125 Allowed types are `agenda' `timeline' `todo' `tags' `search'."
7126 (if (not org-agenda-type)
7127 (error "No Org agenda currently displayed")
7128 (if (memq org-agenda-type types)
7130 (if error
7131 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
7132 nil))))
7134 (defun org-agenda-Quit ()
7135 "Exit the agenda, killing the agenda buffer.
7136 Like `org-agenda-quit', but kill the buffer even when
7137 `org-agenda-sticky' is non-nil."
7138 (interactive)
7139 (org-agenda--quit))
7141 (defun org-agenda-quit ()
7142 "Exit the agenda.
7144 When `org-agenda-sticky' is non-nil, bury the agenda buffer
7145 instead of killing it.
7147 When `org-agenda-restore-windows-after-quit' is non-nil, restore
7148 the pre-agenda window configuration.
7150 When column view is active, exit column view instead of the
7151 agenda."
7152 (interactive)
7153 (org-agenda--quit org-agenda-sticky))
7155 (defun org-agenda--quit (&optional bury)
7156 (if org-agenda-columns-active
7157 (org-columns-quit)
7158 (let ((wconf org-agenda-pre-window-conf)
7159 (buf (current-buffer))
7160 (org-agenda-last-indirect-window
7161 (and (eq org-indirect-buffer-display 'other-window)
7162 org-agenda-last-indirect-buffer
7163 (get-buffer-window org-agenda-last-indirect-buffer))))
7164 (cond
7165 ((eq org-agenda-window-setup 'other-frame)
7166 (delete-frame))
7167 ((and org-agenda-restore-windows-after-quit
7168 wconf)
7169 ;; Maybe restore the pre-agenda window configuration. Reset
7170 ;; `org-agenda-pre-window-conf' before running
7171 ;; `set-window-configuration', which loses the current buffer.
7172 (setq org-agenda-pre-window-conf nil)
7173 (set-window-configuration wconf))
7175 (when org-agenda-last-indirect-window
7176 (delete-window org-agenda-last-indirect-window))
7177 (and (not (eq org-agenda-window-setup 'current-window))
7178 (not (one-window-p))
7179 (delete-window))))
7180 (if bury
7181 ;; Set the agenda buffer as the current buffer instead of
7182 ;; passing it as an argument to `bury-buffer' so that
7183 ;; `bury-buffer' removes it from the window.
7184 (with-current-buffer buf
7185 (bury-buffer))
7186 (kill-buffer buf)
7187 (setq org-agenda-archives-mode nil
7188 org-agenda-buffer nil)))))
7190 (defun org-agenda-exit ()
7191 "Exit the agenda, killing Org buffers loaded by the agenda.
7192 Like `org-agenda-Quit', but kill any buffers that were created by
7193 the agenda. Org buffers visited directly by the user will not be
7194 touched. Also, exit the agenda even if it is in column view."
7195 (interactive)
7196 (when org-agenda-columns-active
7197 (org-columns-quit))
7198 (org-release-buffers org-agenda-new-buffers)
7199 (setq org-agenda-new-buffers nil)
7200 (org-agenda-Quit))
7202 (defun org-agenda-kill-all-agenda-buffers ()
7203 "Kill all buffers in `org-agenda-mode'.
7204 This is used when toggling sticky agendas."
7205 (interactive)
7206 (let (blist)
7207 (dolist (buf (buffer-list))
7208 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7209 (push buf blist)))
7210 (mapc 'kill-buffer blist)))
7212 (defun org-agenda-execute (arg)
7213 "Execute another agenda command, keeping same window.
7214 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7215 in the agenda."
7216 (interactive "P")
7217 (let ((org-agenda-window-setup 'current-window))
7218 (org-agenda arg)))
7220 (defun org-agenda-redo (&optional all)
7221 "Rebuild possibly ALL agenda view(s) in the current buffer."
7222 (interactive "P")
7223 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7224 (cpa (unless (eq all t) current-prefix-arg))
7225 (org-agenda-doing-sticky-redo org-agenda-sticky)
7226 (org-agenda-sticky nil)
7227 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7228 org-agenda-buffer-name))
7229 (org-agenda-keep-modes t)
7230 (tag-filter org-agenda-tag-filter)
7231 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7232 (top-hl-filter org-agenda-top-headline-filter)
7233 (cat-filter org-agenda-category-filter)
7234 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7235 (re-filter org-agenda-regexp-filter)
7236 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7237 (effort-filter org-agenda-effort-filter)
7238 (effort-preset (get 'org-agenda-effort-filter :preset-filter))
7239 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7240 (cols org-agenda-columns-active)
7241 (line (org-current-line))
7242 (window-line (- line (org-current-line (window-start))))
7243 (lprops (get 'org-agenda-redo-command 'org-lprops))
7244 (redo-cmd (get-text-property p 'org-redo-cmd))
7245 (last-args (get-text-property p 'org-last-args))
7246 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7247 (org-agenda-overriding-cmd-arguments
7248 (unless (eq all t)
7249 (cond ((listp last-args)
7250 (cons (or cpa (car last-args)) (cdr last-args)))
7251 ((stringp last-args)
7252 last-args))))
7253 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7254 (put 'org-agenda-tag-filter :preset-filter nil)
7255 (put 'org-agenda-category-filter :preset-filter nil)
7256 (put 'org-agenda-regexp-filter :preset-filter nil)
7257 (put 'org-agenda-effort-filter :preset-filter nil)
7258 (and cols (org-columns-quit))
7259 (message "Rebuilding agenda buffer...")
7260 (if series-redo-cmd
7261 (eval series-redo-cmd)
7262 (org-let lprops redo-cmd))
7263 (setq org-agenda-undo-list nil
7264 org-agenda-pending-undo-list nil
7265 org-agenda-tag-filter tag-filter
7266 org-agenda-category-filter cat-filter
7267 org-agenda-regexp-filter re-filter
7268 org-agenda-effort-filter effort-filter
7269 org-agenda-top-headline-filter top-hl-filter)
7270 (message "Rebuilding agenda buffer...done")
7271 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7272 (put 'org-agenda-category-filter :preset-filter cat-preset)
7273 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7274 (put 'org-agenda-effort-filter :preset-filter effort-preset)
7275 (let ((tag (or tag-filter tag-preset))
7276 (cat (or cat-filter cat-preset))
7277 (effort (or effort-filter effort-preset))
7278 (re (or re-filter re-preset)))
7279 (when tag (org-agenda-filter-apply tag 'tag t))
7280 (when cat (org-agenda-filter-apply cat 'category))
7281 (when effort (org-agenda-filter-apply effort 'effort))
7282 (when re (org-agenda-filter-apply re 'regexp)))
7283 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7284 (and cols (called-interactively-p 'any) (org-agenda-columns))
7285 (org-goto-line line)
7286 (recenter window-line)))
7288 (defvar org-global-tags-completion-table nil)
7289 (defvar org-agenda-filter-form nil)
7290 (defvar org-agenda-filtered-by-category nil)
7292 (defun org-agenda-filter-by-category (strip)
7293 "Filter lines in the agenda buffer that have a specific category.
7294 The category is that of the current line.
7295 Without prefix argument, keep only the lines of that category.
7296 With a prefix argument, exclude the lines of that category.
7298 (interactive "P")
7299 (if (and org-agenda-filtered-by-category
7300 org-agenda-category-filter)
7301 (org-agenda-filter-show-all-cat)
7302 (let ((cat (org-no-properties (org-get-at-eol 'org-category 1))))
7303 (cond
7304 ((and cat strip)
7305 (org-agenda-filter-apply
7306 (push (concat "-" cat) org-agenda-category-filter) 'category))
7307 (cat
7308 (org-agenda-filter-apply
7309 (setq org-agenda-category-filter
7310 (list (concat "+" cat))) 'category))
7311 (t (error "No category at point"))))))
7313 (defun org-find-top-headline (&optional pos)
7314 "Find the topmost parent headline and return it.
7315 POS when non-nil is the marker or buffer position to start the
7316 search from."
7317 (save-excursion
7318 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
7319 (when pos (goto-char pos))
7320 ;; Skip up to the topmost parent.
7321 (while (org-up-heading-safe))
7322 (ignore-errors (nth 4 (org-heading-components))))))
7324 (defvar org-agenda-filtered-by-top-headline nil)
7325 (defun org-agenda-filter-by-top-headline (strip)
7326 "Keep only those lines that are descendants from the same top headline.
7327 The top headline is that of the current line."
7328 (interactive "P")
7329 (if org-agenda-filtered-by-top-headline
7330 (progn
7331 (setq org-agenda-filtered-by-top-headline nil
7332 org-agenda-top-headline-filter nil)
7333 (org-agenda-filter-show-all-top-filter))
7334 (let ((toph (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7335 (if toph (org-agenda-filter-top-headline-apply toph strip)
7336 (error "No top-level headline at point")))))
7338 (defvar org-agenda-regexp-filter nil)
7339 (defun org-agenda-filter-by-regexp (strip)
7340 "Filter agenda entries by a regular expression.
7341 Regexp filters are cumulative.
7342 With no prefix argument, keep entries matching the regexp.
7343 With one prefix argument, filter out entries matching the regexp.
7344 With two prefix arguments, remove the regexp filters."
7345 (interactive "P")
7346 (if (not (equal strip '(16)))
7347 (let ((flt (concat (if (equal strip '(4)) "-" "+")
7348 (read-from-minibuffer
7349 (if (equal strip '(4))
7350 "Filter out entries matching regexp: "
7351 "Narrow to entries matching regexp: ")))))
7352 (push flt org-agenda-regexp-filter)
7353 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
7354 (org-agenda-filter-show-all-re)
7355 (message "Regexp filter removed")))
7357 (defvar org-agenda-effort-filter nil)
7358 (defun org-agenda-filter-by-effort (strip)
7359 "Filter agenda entries by effort.
7360 With no prefix argument, keep entries matching the effort condition.
7361 With one prefix argument, filter out entries matching the condition.
7362 With two prefix arguments, remove the effort filters."
7363 (interactive "P")
7364 (cond
7365 ((member strip '(nil 4))
7366 (let* ((efforts (split-string
7367 (or (cdr (assoc (concat org-effort-property "_ALL")
7368 org-global-properties))
7369 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00")))
7370 ;; XXX: the following handles only up to 10 different
7371 ;; effort values.
7372 (allowed-keys (if (null efforts) nil
7373 (mapcar (lambda (n) (mod n 10)) ;turn 10 into 0
7374 (number-sequence 1 (length efforts)))))
7375 (op nil))
7376 (while (not (memq op '(?< ?> ?=)))
7377 (setq op (read-char-exclusive "Effort operator? (> = or <)")))
7378 ;; Select appropriate duration. Ignore non-digit characters.
7379 (let ((prompt
7380 (apply #'format
7381 (concat "Effort %c "
7382 (mapconcat (lambda (s) (concat "[%d]" s))
7383 efforts
7384 " "))
7385 op allowed-keys))
7386 (eff -1))
7387 (while (not (memq eff allowed-keys))
7388 (message prompt)
7389 (setq eff (- (read-char-exclusive) 48)))
7390 (setq org-agenda-effort-filter
7391 (list (concat (if strip "-" "+")
7392 (char-to-string op)
7393 ;; Numbering is 1 2 3 ... 9 0, but we want
7394 ;; 0 1 2 ... 8 9.
7395 (nth (mod (1- eff) 10) efforts)))))
7396 (org-agenda-filter-apply org-agenda-effort-filter 'effort)))
7397 (t (org-agenda-filter-show-all-effort)
7398 (message "Effort filter removed"))))
7400 (defun org-agenda-filter-remove-all ()
7401 "Remove all filters from the current agenda buffer."
7402 (interactive)
7403 (when org-agenda-tag-filter
7404 (org-agenda-filter-show-all-tag))
7405 (when org-agenda-category-filter
7406 (org-agenda-filter-show-all-cat))
7407 (when org-agenda-regexp-filter
7408 (org-agenda-filter-show-all-re))
7409 (when org-agenda-top-headline-filter
7410 (org-agenda-filter-show-all-top-filter))
7411 (when org-agenda-effort-filter
7412 (org-agenda-filter-show-all-effort))
7413 (org-agenda-finalize))
7415 (defun org-agenda-filter-by-tag (arg &optional char exclude)
7416 "Keep only those lines in the agenda buffer that have a specific tag.
7418 The tag is selected with its fast selection letter, as configured.
7420 With a `\\[universal-argument]' prefix, exclude the agenda search.
7422 With a `\\[universal-argument] \\[universal-argument]' prefix, filter the literal tag, \
7423 i.e. don't
7424 filter on all its group members.
7426 A lisp caller can specify CHAR. EXCLUDE means that the new tag
7427 should be used to exclude the search - the interactive user can
7428 also press `-' or `+' to switch between filtering and excluding."
7429 (interactive "P")
7430 (let* ((alist org-tag-alist-for-agenda)
7431 (tag-chars (mapconcat
7432 (lambda (x) (if (and (not (symbolp (car x)))
7433 (cdr x))
7434 (char-to-string (cdr x))
7435 ""))
7436 org-tag-alist-for-agenda ""))
7437 (valid-char-list (append '(?\t ?\r ?/ ?. ?\s ?q)
7438 (string-to-list tag-chars)))
7439 (exclude (or exclude (equal arg '(4))))
7440 (expand (not (equal arg '(16))))
7441 (inhibit-read-only t)
7442 (current org-agenda-tag-filter)
7443 a n tag)
7444 (unless char
7445 (while (not (memq char valid-char-list))
7446 (message
7447 "%s by tag [%s ], [TAB], %s[/]:off, [+/-]:filter/exclude%s, [q]:quit"
7448 (if exclude "Exclude" "Filter") tag-chars
7449 (if org-agenda-auto-exclude-function "[RET], " "")
7450 (if expand "" ", no grouptag expand"))
7451 (setq char (read-char-exclusive))
7452 ;; Excluding or filtering down
7453 (cond ((eq char ?-) (setq exclude t))
7454 ((eq char ?+) (setq exclude nil)))))
7455 (when (eq char ?\t)
7456 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7457 (setq-local org-global-tags-completion-table
7458 (org-global-tags-completion-table)))
7459 (let ((completion-ignore-case t))
7460 (setq tag (completing-read
7461 "Tag: " org-global-tags-completion-table))))
7462 (cond
7463 ((eq char ?\r)
7464 (org-agenda-filter-show-all-tag)
7465 (when org-agenda-auto-exclude-function
7466 (setq org-agenda-tag-filter nil)
7467 (dolist (tag (org-agenda-get-represented-tags))
7468 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7469 (if modifier
7470 (push modifier org-agenda-tag-filter))))
7471 (if (not (null org-agenda-tag-filter))
7472 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))))
7473 ((eq char ?/)
7474 (org-agenda-filter-show-all-tag)
7475 (when (get 'org-agenda-tag-filter :preset-filter)
7476 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand)))
7477 ((eq char ?.)
7478 (setq org-agenda-tag-filter
7479 (mapcar (lambda(tag) (concat "+" tag))
7480 (org-get-at-bol 'tags)))
7481 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7482 ((eq char ?q)) ;If q, abort (even if there is a q-key for a tag...)
7483 ((or (eq char ?\s)
7484 (setq a (rassoc char alist))
7485 (and tag (setq a (cons tag nil))))
7486 (org-agenda-filter-show-all-tag)
7487 (setq tag (car a))
7488 (setq org-agenda-tag-filter
7489 (cons (concat (if exclude "-" "+") tag)
7490 current))
7491 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7492 (t (error "Invalid tag selection character %c" char)))))
7494 (defun org-agenda-get-represented-tags ()
7495 "Get a list of all tags currently represented in the agenda."
7496 (let (p tags)
7497 (save-excursion
7498 (goto-char (point-min))
7499 (while (setq p (next-single-property-change (point) 'tags))
7500 (goto-char p)
7501 (mapc (lambda (x) (add-to-list 'tags x))
7502 (get-text-property (point) 'tags))))
7503 tags))
7506 (defun org-agenda-filter-make-matcher (filter type &optional expand)
7507 "Create the form that tests a line for agenda filter. Optional
7508 argument EXPAND can be used for the TYPE tag and will expand the
7509 tags in the FILTER if any of the tags in FILTER are grouptags."
7510 (let (f f1)
7511 (cond
7512 ;; Tag filter
7513 ((eq type 'tag)
7514 (setq filter
7515 (delete-dups
7516 (append (get 'org-agenda-tag-filter :preset-filter)
7517 filter)))
7518 (dolist (x filter)
7519 (let ((op (string-to-char x)))
7520 (if expand (setq x (org-agenda-filter-expand-tags (list x) t))
7521 (setq x (list x)))
7522 (setq f1 (org-agenda-filter-make-matcher-tag-exp x op))
7523 (push f1 f))))
7524 ;; Category filter
7525 ((eq type 'category)
7526 (setq filter
7527 (delete-dups
7528 (append (get 'org-agenda-category-filter :preset-filter)
7529 filter)))
7530 (dolist (x filter)
7531 (if (equal "-" (substring x 0 1))
7532 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7533 (setq f1 (list 'equal (substring x 1) 'cat)))
7534 (push f1 f)))
7535 ;; Regexp filter
7536 ((eq type 'regexp)
7537 (setq filter
7538 (delete-dups
7539 (append (get 'org-agenda-regexp-filter :preset-filter)
7540 filter)))
7541 (dolist (x filter)
7542 (if (equal "-" (substring x 0 1))
7543 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7544 (setq f1 (list 'string-match (substring x 1) 'txt)))
7545 (push f1 f)))
7546 ;; Effort filter
7547 ((eq type 'effort)
7548 (setq filter
7549 (delete-dups
7550 (append (get 'org-agenda-effort-filter :preset-filter)
7551 filter)))
7552 (dolist (x filter)
7553 (push (org-agenda-filter-effort-form x) f))))
7554 (cons 'and (nreverse f))))
7556 (defun org-agenda-filter-make-matcher-tag-exp (tags op)
7557 "Return a form associated to tag-expression TAGS.
7558 Build a form testing a line for agenda filter for
7559 tag-expressions. OP is an operator of type CHAR that allows the
7560 function to set the right switches in the returned form."
7561 (let (form)
7562 ;; Any of the expressions can match if OP is +, all must match if
7563 ;; the operator is -.
7564 (dolist (x tags (cons (if (eq op ?-) 'and 'or) form))
7565 (let* ((tag (substring x 1))
7566 (f (cond
7567 ((string= "" tag) '(not tags))
7568 ((and (string-match-p "\\`{" tag) (string-match-p "}\\'" tag))
7569 ;; TAG is a regexp.
7570 (list 'org-match-any-p (substring tag 1 -1) 'tags))
7571 (t (list 'member (downcase tag) 'tags)))))
7572 (push (if (eq op ?-) (list 'not f) f) form)))))
7574 (defun org-agenda-filter-effort-form (e)
7575 "Return the form to compare the effort of the current line with what E says.
7576 E looks like \"+<2:25\"."
7577 (let (op)
7578 (setq e (substring e 1))
7579 (setq op (string-to-char e) e (substring e 1))
7580 (setq op (cond ((equal op ?<) '<=)
7581 ((equal op ?>) '>=)
7582 ((equal op ??) op)
7583 (t '=)))
7584 (list 'org-agenda-compare-effort (list 'quote op)
7585 (org-duration-to-minutes e))))
7587 (defun org-agenda-compare-effort (op value)
7588 "Compare the effort of the current line with VALUE, using OP.
7589 If the line does not have an effort defined, return nil."
7590 ;; `effort-minutes' property cannot be extracted directly from
7591 ;; current line but is stored as a property in `txt'.
7592 (let ((effort (get-text-property 0 'effort-minutes (org-get-at-bol 'txt))))
7593 (funcall op
7594 (or effort (if org-sort-agenda-noeffort-is-high 32767 -1))
7595 value)))
7597 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7598 "Expand group tags in FILTER for the agenda.
7599 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7600 (if org-group-tags
7601 (let ((case-fold-search t) rtn)
7602 (mapc
7603 (lambda (f)
7604 (let (f0 dir)
7605 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7606 (setq dir (match-string 1 f) f0 (match-string 2 f))
7607 (setq dir (if no-operator "" "+") f0 f))
7608 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7609 (org-tags-expand f0 t t))
7610 rtn))))
7611 filter)
7612 (reverse rtn))
7613 filter))
7615 (defun org-agenda-filter-apply (filter type &optional expand)
7616 "Set FILTER as the new agenda filter and apply it. Optional
7617 argument EXPAND can be used for the TYPE tag and will expand the
7618 tags in the FILTER if any of the tags in FILTER are grouptags."
7619 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7620 (if org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7621 (let (tags cat txt)
7622 (setq org-agenda-filter-form (org-agenda-filter-make-matcher filter type expand))
7623 ;; Only set `org-agenda-filtered-by-category' to t when a unique
7624 ;; category is used as the filter:
7625 (setq org-agenda-filtered-by-category
7626 (and (eq type 'category)
7627 (not (equal (substring (car filter) 0 1) "-"))))
7628 (org-agenda-set-mode-name)
7629 (save-excursion
7630 (goto-char (point-min))
7631 (while (not (eobp))
7632 (if (org-get-at-bol 'org-marker)
7633 (progn
7634 (setq tags (org-get-at-bol 'tags)
7635 cat (org-get-at-eol 'org-category 1)
7636 txt (org-get-at-bol 'txt))
7637 (if (not (eval org-agenda-filter-form))
7638 (org-agenda-filter-hide-line type))
7639 (beginning-of-line 2))
7640 (beginning-of-line 2))))
7641 (if (get-char-property (point) 'invisible)
7642 (ignore-errors (org-agenda-previous-line)))))
7644 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7645 "Filter by top headline HL."
7646 (org-agenda-set-mode-name)
7647 (save-excursion
7648 (goto-char (point-min))
7649 (while (not (eobp))
7650 (let* ((pos (org-get-at-bol 'org-hd-marker))
7651 (tophl (and pos (org-find-top-headline pos))))
7652 (if (and tophl (funcall (if negative 'identity 'not)
7653 (string= hl tophl)))
7654 (org-agenda-filter-hide-line 'top-headline)))
7655 (beginning-of-line 2)))
7656 (if (get-char-property (point) 'invisible)
7657 (org-agenda-previous-line))
7658 (setq org-agenda-top-headline-filter hl
7659 org-agenda-filtered-by-top-headline t))
7661 (defun org-agenda-filter-hide-line (type)
7662 "Hide lines with TYPE in the agenda buffer."
7663 (let* ((b (max (point-min) (1- (point-at-bol))))
7664 (e (point-at-eol)))
7665 (let ((inhibit-read-only t))
7666 (add-text-properties
7667 b e `(invisible org-filtered org-filter-type ,type)))))
7669 (defun org-agenda-remove-filter (type)
7670 (interactive)
7671 "Remove filter of type TYPE from the agenda buffer."
7672 (save-excursion
7673 (goto-char (point-min))
7674 (let ((inhibit-read-only t) pos)
7675 (while (setq pos (text-property-any (point) (point-max) 'org-filter-type type))
7676 (goto-char pos)
7677 (remove-text-properties
7678 (point) (next-single-property-change (point) 'org-filter-type)
7679 `(invisible org-filtered org-filter-type ,type))))
7680 (set (intern (format "org-agenda-%s-filter" (intern-soft type))) nil)
7681 (setq org-agenda-filter-form nil)
7682 (org-agenda-set-mode-name)
7683 (org-agenda-finalize)))
7685 (defun org-agenda-filter-show-all-tag nil
7686 (org-agenda-remove-filter 'tag))
7687 (defun org-agenda-filter-show-all-re nil
7688 (org-agenda-remove-filter 'regexp))
7689 (defun org-agenda-filter-show-all-effort nil
7690 (org-agenda-remove-filter 'effort))
7691 (defun org-agenda-filter-show-all-cat nil
7692 (org-agenda-remove-filter 'category))
7693 (defun org-agenda-filter-show-all-top-filter nil
7694 (org-agenda-remove-filter 'top-headline))
7696 (defun org-agenda-manipulate-query-add ()
7697 "Manipulate the query by adding a search term with positive selection.
7698 Positive selection means the term must be matched for selection of an entry."
7699 (interactive)
7700 (org-agenda-manipulate-query ?\[))
7701 (defun org-agenda-manipulate-query-subtract ()
7702 "Manipulate the query by adding a search term with negative selection.
7703 Negative selection means term must not be matched for selection of an entry."
7704 (interactive)
7705 (org-agenda-manipulate-query ?\]))
7706 (defun org-agenda-manipulate-query-add-re ()
7707 "Manipulate the query by adding a search regexp with positive selection.
7708 Positive selection means the regexp must match for selection of an entry."
7709 (interactive)
7710 (org-agenda-manipulate-query ?\{))
7711 (defun org-agenda-manipulate-query-subtract-re ()
7712 "Manipulate the query by adding a search regexp with negative selection.
7713 Negative selection means regexp must not match for selection of an entry."
7714 (interactive)
7715 (org-agenda-manipulate-query ?\}))
7716 (defun org-agenda-manipulate-query (char)
7717 (cond
7718 ((memq org-agenda-type '(timeline agenda))
7719 (let ((org-agenda-include-inactive-timestamps t))
7720 (org-agenda-redo))
7721 (message "Display now includes inactive timestamps as well"))
7722 ((eq org-agenda-type 'search)
7723 (org-add-to-string
7724 'org-agenda-query-string
7725 (if org-agenda-last-search-view-search-was-boolean
7726 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7727 (?\{ . " +{}") (?\} . " -{}"))))
7728 " "))
7729 (setq org-agenda-redo-command
7730 (list 'org-search-view
7731 (car (get-text-property (min (1- (point-max)) (point))
7732 'org-last-args))
7733 org-agenda-query-string
7734 (+ (length org-agenda-query-string)
7735 (if (member char '(?\{ ?\})) 0 1))))
7736 (set-register org-agenda-query-register org-agenda-query-string)
7737 (let ((org-agenda-overriding-arguments
7738 (cdr org-agenda-redo-command)))
7739 (org-agenda-redo)))
7740 (t (error "Cannot manipulate query for %s-type agenda buffers"
7741 org-agenda-type))))
7743 (defun org-add-to-string (var string)
7744 (set var (concat (symbol-value var) string)))
7746 (defun org-agenda-goto-date (span)
7747 "Jump to DATE in agenda."
7748 (interactive "P")
7749 (let* ((org-read-date-prefer-future
7750 (eval org-agenda-jump-prefer-future))
7751 (date (org-read-date))
7752 (day (time-to-days (org-time-string-to-time date)))
7753 (org-agenda-sticky-orig org-agenda-sticky)
7754 (org-agenda-buffer-tmp-name (buffer-name))
7755 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7756 (0-arg (or current-prefix-arg (car args)))
7757 (2-arg (nth 2 args))
7758 (with-hour-p (nth 4 org-agenda-redo-command))
7759 (newcmd (list 'org-agenda-list 0-arg date
7760 (org-agenda-span-to-ndays
7761 2-arg (org-time-string-to-absolute date))
7762 with-hour-p))
7763 (newargs (cdr newcmd))
7764 (inhibit-read-only t)
7765 org-agenda-sticky)
7766 (if (not (org-agenda-check-type t 'agenda))
7767 (error "Not available in non-agenda views")
7768 (add-text-properties (point-min) (point-max)
7769 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7770 (org-agenda-redo)
7771 (goto-char (point-min))
7772 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7773 (save-excursion (move-beginning-of-line 2) (eobp))))
7774 (move-beginning-of-line 2))
7775 (setq org-agenda-sticky org-agenda-sticky-orig
7776 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7778 (defun org-agenda-goto-today ()
7779 "Go to today."
7780 (interactive)
7781 (org-agenda-check-type t 'timeline 'agenda)
7782 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7783 (curspan (nth 2 args))
7784 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7785 (cond
7786 (tdpos (goto-char tdpos))
7787 ((eq org-agenda-type 'agenda)
7788 (let* ((sd (org-agenda-compute-starting-span
7789 (org-today) (or curspan org-agenda-span)))
7790 (org-agenda-overriding-arguments args))
7791 (setf (nth 1 org-agenda-overriding-arguments) sd)
7792 (org-agenda-redo)
7793 (org-agenda-find-same-or-today-or-agenda)))
7794 (t (error "Cannot find today")))))
7796 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7797 (goto-char
7798 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7799 (text-property-any (point-min) (point-max) 'org-today t)
7800 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7801 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7802 (org-agenda-backward-block))
7803 (point-min))))
7805 (defun org-agenda-backward-block ()
7806 "Move backward by one agenda block."
7807 (interactive)
7808 (org-agenda-forward-block 'backward))
7810 (defun org-agenda-forward-block (&optional backward)
7811 "Move forward by one agenda block.
7812 When optional argument BACKWARD is set, go backward"
7813 (interactive)
7814 (cond ((not (derived-mode-p 'org-agenda-mode))
7815 (user-error
7816 "Cannot execute this command outside of org-agenda-mode buffers"))
7817 ((looking-at (if backward "\\`" "\\'"))
7818 (message "Already at the %s block" (if backward "first" "last")))
7819 (t (let ((pos (prog1 (point)
7820 (ignore-errors (if backward (backward-char 1)
7821 (move-end-of-line 1)))))
7822 (f (if backward
7823 'previous-single-property-change
7824 'next-single-property-change))
7825 moved dest)
7826 (while (and (setq dest (funcall
7827 f (point) 'org-agenda-structural-header))
7828 (not (get-text-property
7829 (point) 'org-agenda-structural-header)))
7830 (setq moved t)
7831 (goto-char dest))
7832 (if moved (move-beginning-of-line 1)
7833 (goto-char (if backward (point-min) (point-max)))
7834 (move-beginning-of-line 1)
7835 (message "No %s block" (if backward "previous" "further")))))))
7837 (defun org-agenda-later (arg)
7838 "Go forward in time by the current span.
7839 With prefix ARG, go forward that many times the current span."
7840 (interactive "p")
7841 (org-agenda-check-type t 'agenda)
7842 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7843 (span (or (nth 2 args) org-agenda-current-span))
7844 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7845 (greg (calendar-gregorian-from-absolute sd))
7846 (cnt (org-get-at-bol 'org-day-cnt))
7847 greg2)
7848 (cond
7849 ((numberp span)
7850 (setq sd (+ (* span arg) sd)))
7851 ((eq span 'day)
7852 (setq sd (+ arg sd)))
7853 ((eq span 'week)
7854 (setq sd (+ (* 7 arg) sd)))
7855 ((eq span 'fortnight)
7856 (setq sd (+ (* 14 arg) sd)))
7857 ((eq span 'month)
7858 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7859 sd (calendar-absolute-from-gregorian greg2))
7860 (setcar greg2 (1+ (car greg2))))
7861 ((eq span 'year)
7862 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7863 sd (calendar-absolute-from-gregorian greg2))
7864 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7866 (setq sd (+ (* span arg) sd))))
7867 (let ((org-agenda-overriding-cmd
7868 ;; `cmd' may have been set by `org-agenda-run-series' which
7869 ;; uses `org-agenda-overriding-cmd' to decide whether
7870 ;; overriding is allowed for `cmd'
7871 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7872 (org-agenda-overriding-arguments
7873 (list (car args) sd span)))
7874 (org-agenda-redo)
7875 (org-agenda-find-same-or-today-or-agenda cnt))))
7877 (defun org-agenda-earlier (arg)
7878 "Go backward in time by the current span.
7879 With prefix ARG, go backward that many times the current span."
7880 (interactive "p")
7881 (org-agenda-later (- arg)))
7883 (defun org-agenda-view-mode-dispatch ()
7884 "Call one of the view mode commands."
7885 (interactive)
7886 (message "View: [d]ay [w]eek for[t]night [m]onth [y]ear [SPC]reset [q]uit/abort
7887 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7888 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7889 (pcase (read-char-exclusive)
7890 (?\ (call-interactively 'org-agenda-reset-view))
7891 (?d (call-interactively 'org-agenda-day-view))
7892 (?w (call-interactively 'org-agenda-week-view))
7893 (?t (call-interactively 'org-agenda-fortnight-view))
7894 (?m (call-interactively 'org-agenda-month-view))
7895 (?y (call-interactively 'org-agenda-year-view))
7896 (?l (call-interactively 'org-agenda-log-mode))
7897 (?L (org-agenda-log-mode '(4)))
7898 (?c (org-agenda-log-mode 'clockcheck))
7899 ((or ?F ?f) (call-interactively 'org-agenda-follow-mode))
7900 (?a (call-interactively 'org-agenda-archives-mode))
7901 (?A (org-agenda-archives-mode 'files))
7902 ((or ?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7903 ((or ?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7904 (?G (call-interactively 'org-agenda-toggle-time-grid))
7905 (?D (call-interactively 'org-agenda-toggle-diary))
7906 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7907 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7908 (org-agenda-check-type t 'timeline 'agenda)
7909 (org-agenda-redo))
7910 (message "Display now includes inactive timestamps as well"))
7911 (?q (message "Abort"))
7912 (key (user-error "Invalid key: %s" key))))
7914 (defun org-agenda-reset-view ()
7915 "Switch to default view for agenda."
7916 (interactive)
7917 (org-agenda-change-time-span org-agenda-span))
7919 (defun org-agenda-day-view (&optional day-of-month)
7920 "Switch to daily view for agenda.
7921 With argument DAY-OF-MONTH, switch to that day of the month."
7922 (interactive "P")
7923 (org-agenda-change-time-span 'day day-of-month))
7925 (defun org-agenda-week-view (&optional iso-week)
7926 "Switch to weekly view for agenda.
7927 With argument ISO-WEEK, switch to the corresponding ISO week.
7928 If ISO-WEEK has more then 2 digits, only the last two encode
7929 the week. Any digits before this encode a year. So 200712
7930 means week 12 of year 2007. Years ranging from 70 years ago
7931 to 30 years in the future can also be written as 2-digit years."
7932 (interactive "P")
7933 (org-agenda-change-time-span 'week iso-week))
7935 (defun org-agenda-fortnight-view (&optional iso-week)
7936 "Switch to fortnightly view for agenda.
7937 With argument ISO-WEEK, switch to the corresponding ISO week.
7938 If ISO-WEEK has more then 2 digits, only the last two encode
7939 the week. Any digits before this encode a year. So 200712
7940 means week 12 of year 2007. Years ranging from 70 years ago
7941 to 30 years in the future can also be written as 2-digit years."
7942 (interactive "P")
7943 (org-agenda-change-time-span 'fortnight iso-week))
7945 (defun org-agenda-month-view (&optional month)
7946 "Switch to monthly view for agenda.
7947 With argument MONTH, switch to that month. If MONTH has more
7948 then 2 digits, only the last two encode the month. Any digits
7949 before this encode a year. So 200712 means December year 2007.
7950 Years ranging from 70 years ago to 30 years in the future can
7951 also be written as 2-digit years."
7952 (interactive "P")
7953 (org-agenda-change-time-span 'month month))
7955 (defun org-agenda-year-view (&optional year)
7956 "Switch to yearly view for agenda.
7957 With argument YEAR, switch to that year. Years ranging from 70
7958 years ago to 30 years in the future can also be written as
7959 2-digit years."
7960 (interactive "P")
7961 (when year
7962 (setq year (org-small-year-to-year year)))
7963 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
7964 (org-agenda-change-time-span 'year year)
7965 (error "Abort")))
7967 (defun org-agenda-change-time-span (span &optional n)
7968 "Change the agenda view to SPAN.
7969 SPAN may be `day', `week', `fortnight', `month', `year'."
7970 (org-agenda-check-type t 'agenda)
7971 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7972 (curspan (nth 2 args)))
7973 (if (and (not n) (equal curspan span))
7974 (error "Viewing span is already \"%s\"" span))
7975 (let* ((sd (or (org-get-at-bol 'day)
7976 (nth 1 args)
7977 org-starting-day))
7978 (sd (org-agenda-compute-starting-span sd span n))
7979 (org-agenda-overriding-cmd
7980 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7981 (org-agenda-overriding-arguments
7982 (list (car args) sd span)))
7983 (org-agenda-redo)
7984 (org-agenda-find-same-or-today-or-agenda))
7985 (org-agenda-set-mode-name)
7986 (message "Switched to %s view" span)))
7988 (defun org-agenda-compute-starting-span (sd span &optional n)
7989 "Compute starting date for agenda.
7990 SPAN may be `day', `week', `fortnight', `month', `year'. The return value
7991 is a cons cell with the starting date and the number of days,
7992 so that the date SD will be in that range."
7993 (let* ((greg (calendar-gregorian-from-absolute sd))
7994 (dg (nth 1 greg))
7995 (mg (car greg))
7996 (yg (nth 2 greg)))
7997 (cond
7998 ((eq span 'day)
7999 (when n
8000 (setq sd (+ (calendar-absolute-from-gregorian
8001 (list mg 1 yg))
8002 n -1))))
8003 ((or (eq span 'week) (eq span 'fortnight))
8004 (let* ((nt (calendar-day-of-week
8005 (calendar-gregorian-from-absolute sd)))
8006 (d (if org-agenda-start-on-weekday
8007 (- nt org-agenda-start-on-weekday)
8010 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
8011 (when n
8012 (require 'cal-iso)
8013 (when (> n 99)
8014 (setq y1 (org-small-year-to-year (/ n 100))
8015 n (mod n 100)))
8016 (setq sd
8017 (calendar-iso-to-absolute
8018 (list n 1
8019 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
8020 ((eq span 'month)
8021 (let (y1)
8022 (when (and n (> n 99))
8023 (setq y1 (org-small-year-to-year (/ n 100))
8024 n (mod n 100)))
8025 (setq sd (calendar-absolute-from-gregorian
8026 (list (or n mg) 1 (or y1 yg))))))
8027 ((eq span 'year)
8028 (setq sd (calendar-absolute-from-gregorian
8029 (list 1 1 (or n yg))))))
8030 sd))
8032 (defun org-agenda-next-date-line (&optional arg)
8033 "Jump to the next line indicating a date in agenda buffer."
8034 (interactive "p")
8035 (org-agenda-check-type t 'agenda 'timeline)
8036 (beginning-of-line 1)
8037 ;; This does not work if user makes date format that starts with a blank
8038 (if (looking-at "^\\S-") (forward-char 1))
8039 (if (not (re-search-forward "^\\S-" nil t arg))
8040 (progn
8041 (backward-char 1)
8042 (error "No next date after this line in this buffer")))
8043 (goto-char (match-beginning 0)))
8045 (defun org-agenda-previous-date-line (&optional arg)
8046 "Jump to the previous line indicating a date in agenda buffer."
8047 (interactive "p")
8048 (org-agenda-check-type t 'agenda 'timeline)
8049 (beginning-of-line 1)
8050 (if (not (re-search-backward "^\\S-" nil t arg))
8051 (error "No previous date before this line in this buffer")))
8053 ;; Initialize the highlight
8054 (defvar org-hl (make-overlay 1 1))
8055 (overlay-put org-hl 'face 'highlight)
8057 (defun org-highlight (begin end &optional buffer)
8058 "Highlight a region with overlay."
8059 (move-overlay org-hl begin end (or buffer (current-buffer))))
8061 (defun org-unhighlight ()
8062 "Detach overlay INDEX."
8063 (delete-overlay org-hl))
8065 (defun org-unhighlight-once ()
8066 "Remove the highlight from its position, and this function from the hook."
8067 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8068 (org-unhighlight))
8070 (defvar org-agenda-pre-follow-window-conf nil)
8071 (defun org-agenda-follow-mode ()
8072 "Toggle follow mode in an agenda buffer."
8073 (interactive)
8074 (unless org-agenda-follow-mode
8075 (setq org-agenda-pre-follow-window-conf
8076 (current-window-configuration)))
8077 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8078 (unless org-agenda-follow-mode
8079 (set-window-configuration org-agenda-pre-follow-window-conf))
8080 (org-agenda-set-mode-name)
8081 (org-agenda-do-context-action)
8082 (message "Follow mode is %s"
8083 (if org-agenda-follow-mode "on" "off")))
8085 (defun org-agenda-entry-text-mode (&optional arg)
8086 "Toggle entry text mode in an agenda buffer."
8087 (interactive "P")
8088 (if (or org-agenda-tag-filter
8089 org-agenda-category-filter
8090 org-agenda-regexp-filter
8091 org-agenda-top-headline-filter)
8092 (user-error "Can't show entry text in filtered views")
8093 (setq org-agenda-entry-text-mode (or (integerp arg)
8094 (not org-agenda-entry-text-mode)))
8095 (org-agenda-entry-text-hide)
8096 (and org-agenda-entry-text-mode
8097 (let ((org-agenda-entry-text-maxlines
8098 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8099 (org-agenda-entry-text-show)))
8100 (org-agenda-set-mode-name)
8101 (message "Entry text mode is %s%s"
8102 (if org-agenda-entry-text-mode "on" "off")
8103 (if (not org-agenda-entry-text-mode) ""
8104 (format " (maximum number of lines is %d)"
8105 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8107 (defun org-agenda-clockreport-mode ()
8108 "Toggle clocktable mode in an agenda buffer."
8109 (interactive)
8110 (org-agenda-check-type t 'agenda)
8111 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
8112 (setq org-agenda-start-with-clockreport-mode org-agenda-clockreport-mode)
8113 (org-agenda-set-mode-name)
8114 (org-agenda-redo)
8115 (message "Clocktable mode is %s"
8116 (if org-agenda-clockreport-mode "on" "off")))
8118 (defun org-agenda-log-mode (&optional special)
8119 "Toggle log mode in an agenda buffer.
8121 With argument SPECIAL, show all possible log items, not only the ones
8122 configured in `org-agenda-log-mode-items'.
8124 With a `\\[universal-argument] \\[universal-argument]' prefix, show *only* \
8125 log items, nothing else."
8126 (interactive "P")
8127 (org-agenda-check-type t 'agenda 'timeline)
8128 (setq org-agenda-show-log
8129 (cond
8130 ((equal special '(16)) 'only)
8131 ((eq special 'clockcheck)
8132 (if (eq org-agenda-show-log 'clockcheck)
8133 nil 'clockcheck))
8134 (special '(closed clock state))
8135 (t (not org-agenda-show-log))))
8136 (setq org-agenda-start-with-log-mode org-agenda-show-log)
8137 (org-agenda-set-mode-name)
8138 (org-agenda-redo)
8139 (message "Log mode is %s" (if org-agenda-show-log "on" "off")))
8141 (defun org-agenda-archives-mode (&optional with-files)
8142 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8143 When called with a prefix argument, include all archive files as well."
8144 (interactive "P")
8145 (setq org-agenda-archives-mode
8146 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8147 (org-agenda-set-mode-name)
8148 (org-agenda-redo)
8149 (message
8150 "%s"
8151 (cond
8152 ((eq org-agenda-archives-mode nil)
8153 "No archives are included")
8154 ((eq org-agenda-archives-mode 'trees)
8155 (format "Trees with :%s: tag are included" org-archive-tag))
8156 ((eq org-agenda-archives-mode t)
8157 (format "Trees with :%s: tag and all active archive files are included"
8158 org-archive-tag)))))
8160 (defun org-agenda-toggle-diary ()
8161 "Toggle diary inclusion in an agenda buffer."
8162 (interactive)
8163 (org-agenda-check-type t 'agenda)
8164 (setq org-agenda-include-diary (not org-agenda-include-diary))
8165 (org-agenda-redo)
8166 (org-agenda-set-mode-name)
8167 (message "Diary inclusion turned %s"
8168 (if org-agenda-include-diary "on" "off")))
8170 (defun org-agenda-toggle-deadlines ()
8171 "Toggle inclusion of entries with a deadline in an agenda buffer."
8172 (interactive)
8173 (org-agenda-check-type t 'agenda)
8174 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8175 (org-agenda-redo)
8176 (org-agenda-set-mode-name)
8177 (message "Deadlines inclusion turned %s"
8178 (if org-agenda-include-deadlines "on" "off")))
8180 (defun org-agenda-toggle-time-grid ()
8181 "Toggle time grid in an agenda buffer."
8182 (interactive)
8183 (org-agenda-check-type t 'agenda)
8184 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8185 (org-agenda-redo)
8186 (org-agenda-set-mode-name)
8187 (message "Time-grid turned %s"
8188 (if org-agenda-use-time-grid "on" "off")))
8190 (defun org-agenda-set-mode-name ()
8191 "Set the mode name to indicate all the small mode settings."
8192 (setq mode-name
8193 (list "Org-Agenda"
8194 (if (get 'org-agenda-files 'org-restrict) " []" "")
8196 '(:eval (org-agenda-span-name org-agenda-current-span))
8197 (if org-agenda-follow-mode " Follow" "")
8198 (if org-agenda-entry-text-mode " ETxt" "")
8199 (if org-agenda-include-diary " Diary" "")
8200 (if org-agenda-include-deadlines " Ddl" "")
8201 (if org-agenda-use-time-grid " Grid" "")
8202 (if (and (boundp 'org-habit-show-habits)
8203 org-habit-show-habits) " Habit" "")
8204 (cond
8205 ((consp org-agenda-show-log) " LogAll")
8206 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8207 (org-agenda-show-log " Log")
8208 (t ""))
8209 (if (or org-agenda-category-filter
8210 (get 'org-agenda-category-filter :preset-filter))
8211 '(:eval (propertize
8212 (concat " <"
8213 (mapconcat
8214 'identity
8215 (append
8216 (get 'org-agenda-category-filter :preset-filter)
8217 org-agenda-category-filter)
8219 ">")
8220 'face 'org-agenda-filter-category
8221 'help-echo "Category used in filtering")) "")
8222 (if (or org-agenda-tag-filter
8223 (get 'org-agenda-tag-filter :preset-filter))
8224 '(:eval (propertize
8225 (concat " {"
8226 (mapconcat
8227 'identity
8228 (append
8229 (get 'org-agenda-tag-filter :preset-filter)
8230 org-agenda-tag-filter)
8232 "}")
8233 'face 'org-agenda-filter-tags
8234 'help-echo "Tags used in filtering")) "")
8235 (if (or org-agenda-effort-filter
8236 (get 'org-agenda-effort-filter :preset-filter))
8237 '(:eval (propertize
8238 (concat " {"
8239 (mapconcat
8240 'identity
8241 (append
8242 (get 'org-agenda-effort-filter :preset-filter)
8243 org-agenda-effort-filter)
8245 "}")
8246 'face 'org-agenda-filter-effort
8247 'help-echo "Effort conditions used in filtering")) "")
8248 (if (or org-agenda-regexp-filter
8249 (get 'org-agenda-regexp-filter :preset-filter))
8250 '(:eval (propertize
8251 (concat " ["
8252 (mapconcat
8253 'identity
8254 (append
8255 (get 'org-agenda-regexp-filter :preset-filter)
8256 org-agenda-regexp-filter)
8258 "]")
8259 'face 'org-agenda-filter-regexp
8260 'help-echo "Regexp used in filtering")) "")
8261 (if org-agenda-archives-mode
8262 (if (eq org-agenda-archives-mode t)
8263 " Archives"
8264 (format " :%s:" org-archive-tag))
8266 (if org-agenda-clockreport-mode " Clock" "")))
8267 (force-mode-line-update))
8269 (defun org-agenda-update-agenda-type ()
8270 "Update the agenda type after each command."
8271 (setq org-agenda-type
8272 (or (get-text-property (point) 'org-agenda-type)
8273 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8275 (defun org-agenda-next-line ()
8276 "Move cursor to the next line, and show if follow mode is active."
8277 (interactive)
8278 (call-interactively 'next-line)
8279 (org-agenda-do-context-action))
8281 (defun org-agenda-previous-line ()
8282 "Move cursor to the previous line, and show if follow-mode is active."
8283 (interactive)
8284 (call-interactively 'previous-line)
8285 (org-agenda-do-context-action))
8287 (defun org-agenda-next-item (n)
8288 "Move cursor to next agenda item."
8289 (interactive "p")
8290 (let ((col (current-column)))
8291 (dotimes (c n)
8292 (when (next-single-property-change (point-at-eol) 'org-marker)
8293 (move-end-of-line 1)
8294 (goto-char (next-single-property-change (point) 'org-marker))))
8295 (org-move-to-column col))
8296 (org-agenda-do-context-action))
8298 (defun org-agenda-previous-item (n)
8299 "Move cursor to next agenda item."
8300 (interactive "p")
8301 (dotimes (c n)
8302 (let ((col (current-column))
8303 (goto (save-excursion
8304 (move-end-of-line 0)
8305 (previous-single-property-change (point) 'org-marker))))
8306 (if goto (goto-char goto))
8307 (org-move-to-column col)))
8308 (org-agenda-do-context-action))
8310 (defun org-agenda-do-context-action ()
8311 "Show outline path and, maybe, follow mode window."
8312 (let ((m (org-get-at-bol 'org-marker)))
8313 (when (and (markerp m) (marker-buffer m))
8314 (and org-agenda-follow-mode
8315 (if org-agenda-follow-indirect
8316 (org-agenda-tree-to-indirect-buffer nil)
8317 (org-agenda-show)))
8318 (and org-agenda-show-outline-path
8319 (org-with-point-at m (org-display-outline-path t))))))
8321 (defun org-agenda-show-tags ()
8322 "Show the tags applicable to the current item."
8323 (interactive)
8324 (let* ((tags (org-get-at-bol 'tags)))
8325 (if tags
8326 (message "Tags are :%s:"
8327 (org-no-properties (mapconcat 'identity tags ":")))
8328 (message "No tags associated with this line"))))
8330 (defun org-agenda-goto (&optional highlight)
8331 "Go to the entry at point in the corresponding Org file."
8332 (interactive)
8333 (let* ((marker (or (org-get-at-bol 'org-marker)
8334 (org-agenda-error)))
8335 (buffer (marker-buffer marker))
8336 (pos (marker-position marker)))
8337 (switch-to-buffer-other-window buffer)
8338 (widen)
8339 (push-mark)
8340 (goto-char pos)
8341 (when (derived-mode-p 'org-mode)
8342 (org-show-context 'agenda)
8343 (recenter (/ (window-height) 2))
8344 (org-back-to-heading t)
8345 (let ((case-fold-search nil))
8346 (when (re-search-forward org-complex-heading-regexp nil t)
8347 (goto-char (match-beginning 4)))))
8348 (run-hooks 'org-agenda-after-show-hook)
8349 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8351 (defvar org-agenda-after-show-hook nil
8352 "Normal hook run after an item has been shown from the agenda.
8353 Point is in the buffer where the item originated.")
8355 (defun org-agenda-kill ()
8356 "Kill the entry or subtree belonging to the current agenda entry."
8357 (interactive)
8358 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8359 (let* ((bufname-orig (buffer-name))
8360 (marker (or (org-get-at-bol 'org-marker)
8361 (org-agenda-error)))
8362 (buffer (marker-buffer marker))
8363 (pos (marker-position marker))
8364 (type (org-get-at-bol 'type))
8365 dbeg dend (n 0) conf)
8366 (org-with-remote-undo buffer
8367 (with-current-buffer buffer
8368 (save-excursion
8369 (goto-char pos)
8370 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8371 (setq dbeg (progn (org-back-to-heading t) (point))
8372 dend (org-end-of-subtree t t))
8373 (setq dbeg (point-at-bol)
8374 dend (min (point-max) (1+ (point-at-eol)))))
8375 (goto-char dbeg)
8376 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8377 (setq conf (or (eq t org-agenda-confirm-kill)
8378 (and (numberp org-agenda-confirm-kill)
8379 (> n org-agenda-confirm-kill))))
8380 (and conf
8381 (not (y-or-n-p
8382 (format "Delete entry with %d lines in buffer \"%s\"? "
8383 n (buffer-name buffer))))
8384 (error "Abort"))
8385 (let ((org-agenda-buffer-name bufname-orig))
8386 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8387 (with-current-buffer buffer (delete-region dbeg dend))
8388 (message "Agenda item and source killed"))))
8390 (defvar org-archive-default-command) ; defined in org-archive.el
8391 (defun org-agenda-archive-default ()
8392 "Archive the entry or subtree belonging to the current agenda entry."
8393 (interactive)
8394 (require 'org-archive)
8395 (org-agenda-archive-with org-archive-default-command))
8397 (defun org-agenda-archive-default-with-confirmation ()
8398 "Archive the entry or subtree belonging to the current agenda entry."
8399 (interactive)
8400 (require 'org-archive)
8401 (org-agenda-archive-with org-archive-default-command 'confirm))
8403 (defun org-agenda-archive ()
8404 "Archive the entry or subtree belonging to the current agenda entry."
8405 (interactive)
8406 (org-agenda-archive-with 'org-archive-subtree))
8408 (defun org-agenda-archive-to-archive-sibling ()
8409 "Move the entry to the archive sibling."
8410 (interactive)
8411 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8413 (defun org-agenda-archive-with (cmd &optional confirm)
8414 "Move the entry to the archive sibling."
8415 (interactive)
8416 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8417 (let* ((bufname-orig (buffer-name))
8418 (marker (or (org-get-at-bol 'org-marker)
8419 (org-agenda-error)))
8420 (buffer (marker-buffer marker))
8421 (pos (marker-position marker)))
8422 (org-with-remote-undo buffer
8423 (with-current-buffer buffer
8424 (if (derived-mode-p 'org-mode)
8425 (if (and confirm
8426 (not (y-or-n-p "Archive this subtree or entry? ")))
8427 (error "Abort")
8428 (save-window-excursion
8429 (goto-char pos)
8430 (let ((org-agenda-buffer-name bufname-orig))
8431 (org-remove-subtree-entries-from-agenda))
8432 (org-back-to-heading t)
8433 (funcall cmd)))
8434 (error "Archiving works only in Org files"))))))
8436 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8437 "Remove all lines in the agenda that correspond to a given subtree.
8438 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8439 If this information is not given, the function uses the tree at point."
8440 (let ((buf (or buf (current-buffer))) m p)
8441 (save-excursion
8442 (unless (and beg end)
8443 (org-back-to-heading t)
8444 (setq beg (point))
8445 (org-end-of-subtree t)
8446 (setq end (point)))
8447 (set-buffer (get-buffer org-agenda-buffer-name))
8448 (save-excursion
8449 (goto-char (point-max))
8450 (beginning-of-line 1)
8451 (while (not (bobp))
8452 (when (and (setq m (org-get-at-bol 'org-marker))
8453 (equal buf (marker-buffer m))
8454 (setq p (marker-position m))
8455 (>= p beg)
8456 (< p end))
8457 (let ((inhibit-read-only t))
8458 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8459 (beginning-of-line 0))))))
8461 (defun org-agenda-refile (&optional goto rfloc no-update)
8462 "Refile the item at point.
8464 When called with `\\[universal-argument] \\[universal-argument]', \
8465 go to the location of the last
8466 refiled item.
8468 When called with `\\[universal-argument] \\[universal-argument] \
8469 \\[universal-argument]' prefix or when GOTO is 0, clear
8470 the refile cache.
8472 RFLOC can be a refile location obtained in a different way.
8474 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8475 (interactive "P")
8476 (cond
8477 ((member goto '(0 (64)))
8478 (org-refile-cache-clear))
8479 ((equal goto '(16))
8480 (org-refile-goto-last-stored))
8482 (let* ((buffer-orig (buffer-name))
8483 (marker (or (org-get-at-bol 'org-hd-marker)
8484 (org-agenda-error)))
8485 (buffer (marker-buffer marker))
8486 (pos (marker-position marker))
8487 (rfloc (or rfloc
8488 (org-refile-get-location
8489 (if goto "Goto" "Refile to") buffer
8490 org-refile-allow-creating-parent-nodes))))
8491 (with-current-buffer buffer
8492 (org-with-wide-buffer
8493 (goto-char marker)
8494 (let ((org-agenda-buffer-name buffer-orig))
8495 (org-remove-subtree-entries-from-agenda))
8496 (org-refile goto buffer rfloc))))
8497 (unless no-update (org-agenda-redo)))))
8499 (defun org-agenda-open-link (&optional arg)
8500 "Open the link(s) in the current entry, if any.
8501 This looks for a link in the displayed line in the agenda.
8502 It also looks at the text of the entry itself."
8503 (interactive "P")
8504 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8505 (org-get-at-bol 'org-marker)))
8506 (buffer (and marker (marker-buffer marker)))
8507 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8508 (lkall (and buffer (org-offer-links-in-entry
8509 buffer marker arg prefix)))
8510 (lk0 (car lkall))
8511 (lk (if (stringp lk0) (list lk0) lk0))
8512 (lkend (cdr lkall))
8513 trg)
8514 (cond
8515 ((and buffer lk)
8516 (mapcar (lambda(l)
8517 (with-current-buffer buffer
8518 (setq trg (and (string-match org-bracket-link-regexp l)
8519 (match-string 1 l)))
8520 (if (or (not trg) (string-match org-any-link-re trg))
8521 (org-with-wide-buffer
8522 (goto-char marker)
8523 (when (search-forward l nil lkend)
8524 (goto-char (match-beginning 0))
8525 (org-open-at-point)))
8526 ;; This is an internal link, widen the buffer
8527 (switch-to-buffer-other-window buffer)
8528 (widen)
8529 (goto-char marker)
8530 (when (search-forward l nil lkend)
8531 (goto-char (match-beginning 0))
8532 (org-open-at-point)))))
8533 lk))
8534 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8535 (save-excursion
8536 (beginning-of-line 1)
8537 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8538 (org-open-link-from-string (match-string 1)))
8539 (t (message "No link to open here")))))
8541 (defun org-agenda-copy-local-variable (var)
8542 "Get a variable from a referenced buffer and install it here."
8543 (let ((m (org-get-at-bol 'org-marker)))
8544 (when (and m (buffer-live-p (marker-buffer m)))
8545 (set (make-local-variable var)
8546 (with-current-buffer (marker-buffer m)
8547 (symbol-value var))))))
8549 (defun org-agenda-switch-to (&optional delete-other-windows)
8550 "Go to the Org mode file which contains the item at point.
8551 When optional argument DELETE-OTHER-WINDOWS is non-nil, the
8552 displayed Org file fills the frame."
8553 (interactive)
8554 (if (and org-return-follows-link
8555 (not (org-get-at-bol 'org-marker))
8556 (org-in-regexp org-bracket-link-regexp))
8557 (org-open-link-from-string (match-string 0))
8558 (let* ((marker (or (org-get-at-bol 'org-marker)
8559 (org-agenda-error)))
8560 (buffer (marker-buffer marker))
8561 (pos (marker-position marker)))
8562 (unless buffer (user-error "Trying to switch to non-existent buffer"))
8563 (pop-to-buffer-same-window buffer)
8564 (when delete-other-windows (delete-other-windows))
8565 (widen)
8566 (goto-char pos)
8567 (when (derived-mode-p 'org-mode)
8568 (org-show-context 'agenda)
8569 (run-hooks 'org-agenda-after-show-hook)))))
8571 (defun org-agenda-goto-mouse (ev)
8572 "Go to the Org file which contains the item at the mouse click."
8573 (interactive "e")
8574 (mouse-set-point ev)
8575 (org-agenda-goto))
8577 (defun org-agenda-show (&optional full-entry)
8578 "Display the Org file which contains the item at point.
8579 With prefix argument FULL-ENTRY, make the entire entry visible
8580 if it was hidden in the outline."
8581 (interactive "P")
8582 (let ((win (selected-window)))
8583 (org-agenda-goto t)
8584 (when full-entry (org-show-entry))
8585 (select-window win)))
8587 (defvar org-agenda-show-window nil)
8588 (defun org-agenda-show-and-scroll-up (&optional arg)
8589 "Display the Org file which contains the item at point.
8591 When called repeatedly, scroll the window that is displaying the buffer.
8593 With a `\\[universal-argument]' prefix, use `org-show-entry' instead of \
8594 `outline-show-subtree'
8595 to display the item, so that drawers and logbooks stay folded."
8596 (interactive "P")
8597 (let ((win (selected-window)))
8598 (if (and (window-live-p org-agenda-show-window)
8599 (eq this-command last-command))
8600 (progn
8601 (select-window org-agenda-show-window)
8602 (ignore-errors (scroll-up)))
8603 (org-agenda-goto t)
8604 (if arg (org-show-entry) (outline-show-subtree))
8605 (setq org-agenda-show-window (selected-window)))
8606 (select-window win)))
8608 (defun org-agenda-show-scroll-down ()
8609 "Scroll down the window showing the agenda."
8610 (interactive)
8611 (let ((win (selected-window)))
8612 (when (window-live-p org-agenda-show-window)
8613 (select-window org-agenda-show-window)
8614 (ignore-errors (scroll-down))
8615 (select-window win))))
8617 (defun org-agenda-show-1 (&optional more)
8618 "Display the Org file which contains the item at point.
8619 The prefix arg selects the amount of information to display:
8621 0 hide the subtree
8622 1 just show the entry according to defaults.
8623 2 show the children view
8624 3 show the subtree view
8625 4 show the entire subtree and any LOGBOOK drawers
8626 5 show the entire subtree and any drawers
8627 With prefix argument FULL-ENTRY, make the entire entry visible
8628 if it was hidden in the outline."
8629 (interactive "p")
8630 (let ((win (selected-window)))
8631 (org-agenda-goto t)
8632 (org-back-to-heading)
8633 (set-window-start (selected-window) (point-at-bol))
8634 (cond
8635 ((= more 0)
8636 (outline-hide-subtree)
8637 (save-excursion
8638 (org-back-to-heading)
8639 (run-hook-with-args 'org-cycle-hook 'folded))
8640 (message "Remote: FOLDED"))
8641 ((and (called-interactively-p 'any) (= more 1))
8642 (message "Remote: show with default settings"))
8643 ((= more 2)
8644 (outline-show-entry)
8645 (org-show-children)
8646 (save-excursion
8647 (org-back-to-heading)
8648 (run-hook-with-args 'org-cycle-hook 'children))
8649 (message "Remote: CHILDREN"))
8650 ((= more 3)
8651 (outline-show-subtree)
8652 (save-excursion
8653 (org-back-to-heading)
8654 (run-hook-with-args 'org-cycle-hook 'subtree))
8655 (message "Remote: SUBTREE"))
8656 ((= more 4)
8657 (outline-show-subtree)
8658 (save-excursion
8659 (org-back-to-heading)
8660 (org-cycle-hide-drawers 'subtree '("LOGBOOK")))
8661 (message "Remote: SUBTREE AND LOGBOOK"))
8662 ((> more 4)
8663 (outline-show-subtree)
8664 (message "Remote: SUBTREE AND ALL DRAWERS")))
8665 (select-window win)))
8667 (defvar org-agenda-cycle-counter nil)
8668 (defun org-agenda-cycle-show (&optional n)
8669 "Show the current entry in another window, with default settings.
8671 Default settings are taken from `org-show-context-detail'. When
8672 use repeatedly in immediate succession, the remote entry will
8673 cycle through visibility
8675 children -> subtree -> folded
8677 When called with a numeric prefix arg, that arg will be passed through to
8678 `org-agenda-show-1'. For the interpretation of that argument, see the
8679 docstring of `org-agenda-show-1'."
8680 (interactive "P")
8681 (if (integerp n)
8682 (setq org-agenda-cycle-counter n)
8683 (if (not (eq last-command this-command))
8684 (setq org-agenda-cycle-counter 1)
8685 (if (equal org-agenda-cycle-counter 0)
8686 (setq org-agenda-cycle-counter 2)
8687 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8688 (if (> org-agenda-cycle-counter 3)
8689 (setq org-agenda-cycle-counter 0)))))
8690 (org-agenda-show-1 org-agenda-cycle-counter))
8692 (defun org-agenda-recenter (arg)
8693 "Display the Org file which contains the item at point and recenter."
8694 (interactive "P")
8695 (let ((win (selected-window)))
8696 (org-agenda-goto t)
8697 (recenter arg)
8698 (select-window win)))
8700 (defun org-agenda-show-mouse (ev)
8701 "Display the Org file which contains the item at the mouse click."
8702 (interactive "e")
8703 (mouse-set-point ev)
8704 (org-agenda-show))
8706 (defun org-agenda-check-no-diary ()
8707 "Check if the entry is a diary link and abort if yes."
8708 (if (org-get-at-bol 'org-agenda-diary-link)
8709 (org-agenda-error)))
8711 (defun org-agenda-error ()
8712 "Throw an error when a command is not allowed in the agenda."
8713 (user-error "Command not allowed in this line"))
8715 (defun org-agenda-tree-to-indirect-buffer (arg)
8716 "Show the subtree corresponding to the current entry in an indirect buffer.
8717 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8719 With a numerical prefix ARG, go up to this level and then take that tree.
8720 With a negative numeric ARG, go up by this number of levels.
8722 With a `\\[universal-argument]' prefix, make a separate frame for this tree, \
8723 i.e. don't use
8724 the dedicated frame."
8725 (interactive "P")
8726 (if current-prefix-arg
8727 (org-agenda-do-tree-to-indirect-buffer arg)
8728 (let ((agenda-buffer (buffer-name))
8729 (agenda-window (selected-window))
8730 (indirect-window
8731 (and org-last-indirect-buffer
8732 (get-buffer-window org-last-indirect-buffer))))
8733 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8734 (unless (or (eq org-indirect-buffer-display 'new-frame)
8735 (eq org-indirect-buffer-display 'dedicated-frame))
8736 (unwind-protect
8737 (unless (and indirect-window (window-live-p indirect-window))
8738 (setq indirect-window (split-window agenda-window)))
8739 (and indirect-window (select-window indirect-window))
8740 (switch-to-buffer org-last-indirect-buffer :norecord)
8741 (fit-window-to-buffer indirect-window)))
8742 (select-window (get-buffer-window agenda-buffer))
8743 (setq org-agenda-last-indirect-buffer org-last-indirect-buffer))))
8745 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8746 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8747 (org-agenda-check-no-diary)
8748 (let* ((marker (or (org-get-at-bol 'org-marker)
8749 (org-agenda-error)))
8750 (buffer (marker-buffer marker))
8751 (pos (marker-position marker)))
8752 (with-current-buffer buffer
8753 (save-excursion
8754 (goto-char pos)
8755 (funcall 'org-tree-to-indirect-buffer arg)))))
8757 (defvar org-last-heading-marker (make-marker)
8758 "Marker pointing to the headline that last changed its TODO state
8759 by a remote command from the agenda.")
8761 (defun org-agenda-todo-nextset ()
8762 "Switch TODO entry to next sequence."
8763 (interactive)
8764 (org-agenda-todo 'nextset))
8766 (defun org-agenda-todo-previousset ()
8767 "Switch TODO entry to previous sequence."
8768 (interactive)
8769 (org-agenda-todo 'previousset))
8771 (defun org-agenda-todo (&optional arg)
8772 "Cycle TODO state of line at point, also in Org file.
8773 This changes the line at point, all other lines in the agenda referring to
8774 the same tree node, and the headline of the tree node in the Org file."
8775 (interactive "P")
8776 (org-agenda-check-no-diary)
8777 (let* ((col (current-column))
8778 (marker (or (org-get-at-bol 'org-marker)
8779 (org-agenda-error)))
8780 (buffer (marker-buffer marker))
8781 (pos (marker-position marker))
8782 (hdmarker (org-get-at-bol 'org-hd-marker))
8783 (todayp (org-agenda-today-p (org-get-at-bol 'day)))
8784 (inhibit-read-only t)
8785 org-agenda-headline-snapshot-before-repeat newhead just-one)
8786 (org-with-remote-undo buffer
8787 (with-current-buffer buffer
8788 (widen)
8789 (goto-char pos)
8790 (org-show-context 'agenda)
8791 (let ((current-prefix-arg arg))
8792 (call-interactively 'org-todo))
8793 (and (bolp) (forward-char 1))
8794 (setq newhead (org-get-heading))
8795 (when (and (bound-and-true-p
8796 org-agenda-headline-snapshot-before-repeat)
8797 (not (equal org-agenda-headline-snapshot-before-repeat
8798 newhead))
8799 todayp)
8800 (setq newhead org-agenda-headline-snapshot-before-repeat
8801 just-one t))
8802 (save-excursion
8803 (org-back-to-heading)
8804 (move-marker org-last-heading-marker (point))))
8805 (beginning-of-line 1)
8806 (save-window-excursion
8807 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8808 (when (bound-and-true-p org-clock-out-when-done)
8809 (string-match (concat "^" (regexp-opt org-done-keywords-for-agenda))
8810 newhead)
8811 (org-agenda-unmark-clocking-task))
8812 (org-move-to-column col)
8813 (org-agenda-mark-clocking-task))))
8815 (defun org-agenda-add-note (&optional arg)
8816 "Add a time-stamped note to the entry at point."
8817 (interactive "P")
8818 (org-agenda-check-no-diary)
8819 (let* ((marker (or (org-get-at-bol 'org-marker)
8820 (org-agenda-error)))
8821 (buffer (marker-buffer marker))
8822 (pos (marker-position marker))
8823 (hdmarker (org-get-at-bol 'org-hd-marker))
8824 (inhibit-read-only t))
8825 (with-current-buffer buffer
8826 (widen)
8827 (goto-char pos)
8828 (org-show-context 'agenda)
8829 (org-add-note))))
8831 (defun org-agenda-change-all-lines (newhead hdmarker
8832 &optional fixface just-this)
8833 "Change all lines in the agenda buffer which match HDMARKER.
8834 The new content of the line will be NEWHEAD (as modified by
8835 `org-agenda-format-item'). HDMARKER is checked with
8836 `equal' against all `org-hd-marker' text properties in the file.
8837 If FIXFACE is non-nil, the face of each item is modified according to
8838 the new TODO state.
8839 If JUST-THIS is non-nil, change just the current line, not all.
8840 If FORCE-TAGS is non nil, the car of it returns the new tags."
8841 (let* ((inhibit-read-only t)
8842 (line (org-current-line))
8843 (org-agenda-buffer (current-buffer))
8844 (thetags (with-current-buffer (marker-buffer hdmarker)
8845 (org-with-wide-buffer
8846 (goto-char hdmarker)
8847 (org-get-tags-at))))
8848 props m pl undone-face done-face finish new dotime level cat tags)
8849 (save-excursion
8850 (goto-char (point-max))
8851 (beginning-of-line 1)
8852 (while (not finish)
8853 (setq finish (bobp))
8854 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8855 (or (not just-this) (= (org-current-line) line))
8856 (equal m hdmarker))
8857 (setq props (text-properties-at (point))
8858 dotime (org-get-at-bol 'dotime)
8859 cat (org-get-at-eol 'org-category 1)
8860 level (org-get-at-bol 'level)
8861 tags thetags
8863 (let ((org-prefix-format-compiled
8864 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8865 org-prefix-format-compiled))
8866 (extra (org-get-at-bol 'extra)))
8867 (with-current-buffer (marker-buffer hdmarker)
8868 (org-with-wide-buffer
8869 (org-agenda-format-item extra newhead level cat tags dotime))))
8870 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8871 undone-face (org-get-at-bol 'undone-face)
8872 done-face (org-get-at-bol 'done-face))
8873 (beginning-of-line 1)
8874 (cond
8875 ((equal new "") (delete-region (point) (line-beginning-position 2)))
8876 ((looking-at ".*")
8877 ;; When replacing the whole line, preserve bulk mark
8878 ;; overlay, if any.
8879 (let ((mark (catch :overlay
8880 (dolist (o (overlays-in (point) (+ 2 (point))))
8881 (when (eq (overlay-get o 'type)
8882 'org-marked-entry-overlay)
8883 (throw :overlay o))))))
8884 (replace-match new t t)
8885 (beginning-of-line)
8886 (when mark (move-overlay mark (point) (+ 2 (point)))))
8887 (add-text-properties (point-at-bol) (point-at-eol) props)
8888 (when fixface
8889 (add-text-properties
8890 (point-at-bol) (point-at-eol)
8891 (list 'face
8892 (if org-last-todo-state-is-todo
8893 undone-face done-face))))
8894 (org-agenda-highlight-todo 'line)
8895 (beginning-of-line 1))
8896 (t (error "Line update did not work")))
8897 (save-restriction
8898 (narrow-to-region (point-at-bol) (point-at-eol))
8899 (org-agenda-finalize)))
8900 (beginning-of-line 0)))))
8902 (defun org-agenda-align-tags (&optional line)
8903 "Align all tags in agenda items to `org-agenda-tags-column'."
8904 (let ((inhibit-read-only t) l c)
8905 (save-excursion
8906 (goto-char (if line (point-at-bol) (point-min)))
8907 (while (re-search-forward "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"
8908 (if line (point-at-eol) nil) t)
8909 (add-text-properties
8910 (match-beginning 2) (match-end 2)
8911 (list 'face (delq nil (let ((prop (get-text-property
8912 (match-beginning 2) 'face)))
8913 (or (listp prop) (setq prop (list prop)))
8914 (if (memq 'org-tag prop)
8915 prop
8916 (cons 'org-tag prop))))))
8917 (setq l (- (match-end 2) (match-beginning 2))
8918 c (if (< org-agenda-tags-column 0)
8919 (- (abs org-agenda-tags-column) l)
8920 org-agenda-tags-column))
8921 (delete-region (match-beginning 1) (match-end 1))
8922 (goto-char (match-beginning 1))
8923 (insert (org-add-props
8924 (make-string (max 1 (- c (current-column))) ?\ )
8925 (plist-put (copy-sequence (text-properties-at (point)))
8926 'face nil))))
8927 (goto-char (point-min))
8928 (org-font-lock-add-tag-faces (point-max)))))
8930 (defun org-agenda-priority-up ()
8931 "Increase the priority of line at point, also in Org file."
8932 (interactive)
8933 (org-agenda-priority 'up))
8935 (defun org-agenda-priority-down ()
8936 "Decrease the priority of line at point, also in Org file."
8937 (interactive)
8938 (org-agenda-priority 'down))
8940 (defun org-agenda-priority (&optional force-direction)
8941 "Set the priority of line at point, also in Org file.
8942 This changes the line at point, all other lines in the agenda referring to
8943 the same tree node, and the headline of the tree node in the Org file.
8944 Called with a universal prefix arg, show the priority instead of setting it."
8945 (interactive "P")
8946 (if (equal force-direction '(4))
8947 (org-show-priority)
8948 (unless org-enable-priority-commands
8949 (error "Priority commands are disabled"))
8950 (org-agenda-check-no-diary)
8951 (let* ((col (current-column))
8952 (marker (or (org-get-at-bol 'org-marker)
8953 (org-agenda-error)))
8954 (hdmarker (org-get-at-bol 'org-hd-marker))
8955 (buffer (marker-buffer hdmarker))
8956 (pos (marker-position hdmarker))
8957 (inhibit-read-only t)
8958 newhead)
8959 (org-with-remote-undo buffer
8960 (with-current-buffer buffer
8961 (widen)
8962 (goto-char pos)
8963 (org-show-context 'agenda)
8964 (funcall 'org-priority force-direction)
8965 (end-of-line 1)
8966 (setq newhead (org-get-heading)))
8967 (org-agenda-change-all-lines newhead hdmarker)
8968 (org-move-to-column col)))))
8970 ;; FIXME: should fix the tags property of the agenda line.
8971 (defun org-agenda-set-tags (&optional tag onoff)
8972 "Set tags for the current headline."
8973 (interactive)
8974 (org-agenda-check-no-diary)
8975 (if (and (org-region-active-p) (called-interactively-p 'any))
8976 (call-interactively 'org-change-tag-in-region)
8977 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8978 (org-agenda-error)))
8979 (buffer (marker-buffer hdmarker))
8980 (pos (marker-position hdmarker))
8981 (inhibit-read-only t)
8982 newhead)
8983 (org-with-remote-undo buffer
8984 (with-current-buffer buffer
8985 (widen)
8986 (goto-char pos)
8987 (org-show-context 'agenda)
8988 (if tag
8989 (org-toggle-tag tag onoff)
8990 (call-interactively 'org-set-tags))
8991 (end-of-line 1)
8992 (setq newhead (org-get-heading)))
8993 (org-agenda-change-all-lines newhead hdmarker)
8994 (beginning-of-line 1)))))
8996 (defun org-agenda-set-property ()
8997 "Set a property for the current headline."
8998 (interactive)
8999 (org-agenda-check-no-diary)
9000 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9001 (org-agenda-error)))
9002 (buffer (marker-buffer hdmarker))
9003 (pos (marker-position hdmarker))
9004 (inhibit-read-only t)
9005 newhead)
9006 (org-with-remote-undo buffer
9007 (with-current-buffer buffer
9008 (widen)
9009 (goto-char pos)
9010 (org-show-context 'agenda)
9011 (call-interactively 'org-set-property)))))
9013 (defun org-agenda-set-effort ()
9014 "Set the effort property for the current headline."
9015 (interactive)
9016 (org-agenda-check-no-diary)
9017 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9018 (org-agenda-error)))
9019 (buffer (marker-buffer hdmarker))
9020 (pos (marker-position hdmarker))
9021 (inhibit-read-only t)
9022 newhead)
9023 (org-with-remote-undo buffer
9024 (with-current-buffer buffer
9025 (widen)
9026 (goto-char pos)
9027 (org-show-context 'agenda)
9028 (call-interactively 'org-set-effort)
9029 (end-of-line 1)
9030 (setq newhead (org-get-heading)))
9031 (org-agenda-change-all-lines newhead hdmarker))))
9033 (defun org-agenda-toggle-archive-tag ()
9034 "Toggle the archive tag for the current entry."
9035 (interactive)
9036 (org-agenda-check-no-diary)
9037 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9038 (org-agenda-error)))
9039 (buffer (marker-buffer hdmarker))
9040 (pos (marker-position hdmarker))
9041 (inhibit-read-only t)
9042 newhead)
9043 (org-with-remote-undo buffer
9044 (with-current-buffer buffer
9045 (widen)
9046 (goto-char pos)
9047 (org-show-context 'agenda)
9048 (call-interactively 'org-toggle-archive-tag)
9049 (end-of-line 1)
9050 (setq newhead (org-get-heading)))
9051 (org-agenda-change-all-lines newhead hdmarker)
9052 (beginning-of-line 1))))
9054 (defun org-agenda-do-date-later (arg)
9055 (interactive "P")
9056 (cond
9057 ((or (equal arg '(16))
9058 (memq last-command
9059 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9060 (setq this-command 'org-agenda-date-later-minutes)
9061 (org-agenda-date-later-minutes 1))
9062 ((or (equal arg '(4))
9063 (memq last-command
9064 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9065 (setq this-command 'org-agenda-date-later-hours)
9066 (org-agenda-date-later-hours 1))
9068 (org-agenda-date-later (prefix-numeric-value arg)))))
9070 (defun org-agenda-do-date-earlier (arg)
9071 (interactive "P")
9072 (cond
9073 ((or (equal arg '(16))
9074 (memq last-command
9075 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9076 (setq this-command 'org-agenda-date-earlier-minutes)
9077 (org-agenda-date-earlier-minutes 1))
9078 ((or (equal arg '(4))
9079 (memq last-command
9080 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9081 (setq this-command 'org-agenda-date-earlier-hours)
9082 (org-agenda-date-earlier-hours 1))
9084 (org-agenda-date-earlier (prefix-numeric-value arg)))))
9086 (defun org-agenda-date-later (arg &optional what)
9087 "Change the date of this item to ARG day(s) later."
9088 (interactive "p")
9089 (org-agenda-check-type t 'agenda 'timeline)
9090 (org-agenda-check-no-diary)
9091 (let* ((marker (or (org-get-at-bol 'org-marker)
9092 (org-agenda-error)))
9093 (buffer (marker-buffer marker))
9094 (pos (marker-position marker))
9095 cdate today)
9096 (org-with-remote-undo buffer
9097 (with-current-buffer buffer
9098 (widen)
9099 (goto-char pos)
9100 (if (not (org-at-timestamp-p))
9101 (error "Cannot find time stamp"))
9102 (when (and org-agenda-move-date-from-past-immediately-to-today
9103 (equal arg 1)
9104 (or (not what) (eq what 'day))
9105 (not (save-match-data (org-at-date-range-p))))
9106 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9107 cdate (calendar-absolute-from-gregorian
9108 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9109 today (org-today))
9110 (if (> today cdate)
9111 ;; immediately shift to today
9112 (setq arg (- today cdate))))
9113 (org-timestamp-change arg (or what 'day))
9114 (when (and (org-at-date-range-p)
9115 (re-search-backward org-tr-regexp-both (point-at-bol)))
9116 (let ((end org-last-changed-timestamp))
9117 (org-timestamp-change arg (or what 'day))
9118 (setq org-last-changed-timestamp
9119 (concat org-last-changed-timestamp "--" end)))))
9120 (org-agenda-show-new-time marker org-last-changed-timestamp))
9121 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9123 (defun org-agenda-date-earlier (arg &optional what)
9124 "Change the date of this item to ARG day(s) earlier."
9125 (interactive "p")
9126 (org-agenda-date-later (- arg) what))
9128 (defun org-agenda-date-later-minutes (arg)
9129 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9130 (interactive "p")
9131 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9132 (org-agenda-date-later arg 'minute))
9134 (defun org-agenda-date-earlier-minutes (arg)
9135 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9136 (interactive "p")
9137 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9138 (org-agenda-date-earlier arg 'minute))
9140 (defun org-agenda-date-later-hours (arg)
9141 "Change the time of this item, in hour steps."
9142 (interactive "p")
9143 (org-agenda-date-later arg 'hour))
9145 (defun org-agenda-date-earlier-hours (arg)
9146 "Change the time of this item, in hour steps."
9147 (interactive "p")
9148 (org-agenda-date-earlier arg 'hour))
9150 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9151 "Show new date stamp via text properties."
9152 ;; We use text properties to make this undoable
9153 (let ((inhibit-read-only t))
9154 (setq stamp (concat prefix " => " stamp " "))
9155 (save-excursion
9156 (goto-char (point-max))
9157 (while (not (bobp))
9158 (when (equal marker (org-get-at-bol 'org-marker))
9159 (remove-text-properties (point-at-bol) (point-at-eol) '(display))
9160 (org-move-to-column (- (window-width) (length stamp)) t)
9161 (add-text-properties
9162 (1- (point)) (point-at-eol)
9163 (list 'display (org-add-props stamp nil
9164 'face '(secondary-selection default))))
9165 (beginning-of-line 1))
9166 (beginning-of-line 0)))))
9168 (defun org-agenda-date-prompt (arg)
9169 "Change the date of this item. Date is prompted for, with default today.
9170 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9171 be used to request time specification in the time stamp."
9172 (interactive "P")
9173 (org-agenda-check-type t 'agenda 'timeline)
9174 (org-agenda-check-no-diary)
9175 (let* ((marker (or (org-get-at-bol 'org-marker)
9176 (org-agenda-error)))
9177 (buffer (marker-buffer marker))
9178 (pos (marker-position marker)))
9179 (org-with-remote-undo buffer
9180 (with-current-buffer buffer
9181 (widen)
9182 (goto-char pos)
9183 (if (not (org-at-timestamp-p t))
9184 (error "Cannot find time stamp"))
9185 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
9186 (org-agenda-show-new-time marker org-last-changed-timestamp))
9187 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9189 (defun org-agenda-schedule (arg &optional time)
9190 "Schedule the item at point.
9191 ARG is passed through to `org-schedule'."
9192 (interactive "P")
9193 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9194 (org-agenda-check-no-diary)
9195 (let* ((marker (or (org-get-at-bol 'org-marker)
9196 (org-agenda-error)))
9197 (type (marker-insertion-type marker))
9198 (buffer (marker-buffer marker))
9199 (pos (marker-position marker))
9201 (set-marker-insertion-type marker t)
9202 (org-with-remote-undo buffer
9203 (with-current-buffer buffer
9204 (widen)
9205 (goto-char pos)
9206 (setq ts (org-schedule arg time)))
9207 (org-agenda-show-new-time marker ts " S"))
9208 (message "%s" ts)))
9210 (defun org-agenda-deadline (arg &optional time)
9211 "Schedule the item at point.
9212 ARG is passed through to `org-deadline'."
9213 (interactive "P")
9214 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9215 (org-agenda-check-no-diary)
9216 (let* ((marker (or (org-get-at-bol 'org-marker)
9217 (org-agenda-error)))
9218 (buffer (marker-buffer marker))
9219 (pos (marker-position marker))
9221 (org-with-remote-undo buffer
9222 (with-current-buffer buffer
9223 (widen)
9224 (goto-char pos)
9225 (setq ts (org-deadline arg time)))
9226 (org-agenda-show-new-time marker ts " D"))
9227 (message "%s" ts)))
9229 (defun org-agenda-clock-in (&optional arg)
9230 "Start the clock on the currently selected item."
9231 (interactive "P")
9232 (org-agenda-check-no-diary)
9233 (if (equal arg '(4))
9234 (org-clock-in arg)
9235 (let* ((marker (or (org-get-at-bol 'org-marker)
9236 (org-agenda-error)))
9237 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9238 (pos (marker-position marker))
9239 (col (current-column))
9240 newhead)
9241 (org-with-remote-undo (marker-buffer marker)
9242 (with-current-buffer (marker-buffer marker)
9243 (widen)
9244 (goto-char pos)
9245 (org-show-context 'agenda)
9246 (org-cycle-hide-drawers 'children)
9247 (org-clock-in arg)
9248 (setq newhead (org-get-heading)))
9249 (org-agenda-change-all-lines newhead hdmarker))
9250 (org-move-to-column col))))
9252 (defun org-agenda-clock-out ()
9253 "Stop the currently running clock."
9254 (interactive)
9255 (unless (marker-buffer org-clock-marker)
9256 (error "No running clock"))
9257 (let ((marker (make-marker)) (col (current-column)) newhead)
9258 (org-with-remote-undo (marker-buffer org-clock-marker)
9259 (with-current-buffer (marker-buffer org-clock-marker)
9260 (org-with-wide-buffer
9261 (goto-char org-clock-marker)
9262 (org-back-to-heading t)
9263 (move-marker marker (point))
9264 (org-clock-out)
9265 (setq newhead (org-get-heading)))))
9266 (org-agenda-change-all-lines newhead marker)
9267 (move-marker marker nil)
9268 (org-move-to-column col)
9269 (org-agenda-unmark-clocking-task)))
9271 (defun org-agenda-clock-cancel (&optional arg)
9272 "Cancel the currently running clock."
9273 (interactive "P")
9274 (unless (marker-buffer org-clock-marker)
9275 (user-error "No running clock"))
9276 (org-with-remote-undo (marker-buffer org-clock-marker)
9277 (org-clock-cancel)))
9279 (defun org-agenda-clock-goto ()
9280 "Jump to the currently clocked in task within the agenda.
9281 If the currently clocked in task is not listed in the agenda
9282 buffer, display it in another window."
9283 (interactive)
9284 (let (pos)
9285 (mapc (lambda (o)
9286 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
9287 (setq pos (overlay-start o))))
9288 (overlays-in (point-min) (point-max)))
9289 (cond (pos (goto-char pos))
9290 ;; If the currently clocked entry is not in the agenda
9291 ;; buffer, we visit it in another window:
9292 ((bound-and-true-p org-clock-current-task)
9293 (org-switch-to-buffer-other-window (org-clock-goto)))
9294 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9296 (defun org-agenda-diary-entry-in-org-file ()
9297 "Make a diary entry in the file `org-agenda-diary-file'."
9298 (let (d1 d2 char (text "") dp1 dp2)
9299 (if (equal (buffer-name) "*Calendar*")
9300 (setq d1 (calendar-cursor-to-date t)
9301 d2 (car calendar-mark-ring))
9302 (setq dp1 (get-text-property (point-at-bol) 'day))
9303 (unless dp1 (user-error "No date defined in current line"))
9304 (setq d1 (calendar-gregorian-from-absolute dp1)
9305 d2 (and (ignore-errors (mark))
9306 (save-excursion
9307 (goto-char (mark))
9308 (setq dp2 (get-text-property (point-at-bol) 'day)))
9309 (calendar-gregorian-from-absolute dp2))))
9310 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9311 (setq char (read-char-exclusive))
9312 (cond
9313 ((equal char ?d)
9314 (setq text (read-string "Day entry: "))
9315 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9316 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9317 ((equal char ?a)
9318 (setq d1 (list (car d1) (nth 1 d1)
9319 (read-number (format "Reference year [%d]: " (nth 2 d1))
9320 (nth 2 d1))))
9321 (setq text (read-string "Anniversary (use %d to show years): "))
9322 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9323 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9324 ((equal char ?b)
9325 (setq text (read-string "Block entry: "))
9326 (unless (and d1 d2 (not (equal d1 d2)))
9327 (user-error "No block of days selected"))
9328 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9329 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9330 ((equal char ?j)
9331 (org-switch-to-buffer-other-window
9332 (find-file-noselect org-agenda-diary-file))
9333 (require 'org-datetree)
9334 (org-datetree-find-date-create d1)
9335 (org-reveal t))
9336 (t (user-error "Invalid selection character `%c'" char)))))
9338 (defcustom org-agenda-insert-diary-strategy 'date-tree
9339 "Where in `org-agenda-diary-file' should new entries be added?
9340 Valid values:
9342 date-tree in the date tree, as first child of the date
9343 date-tree-last in the date tree, as last child of the date
9344 top-level as top-level entries at the end of the file."
9345 :group 'org-agenda
9346 :type '(choice
9347 (const :tag "first in a date tree" date-tree)
9348 (const :tag "last in a date tree" date-tree-last)
9349 (const :tag "as top level at end of file" top-level)))
9351 (defcustom org-agenda-insert-diary-extract-time nil
9352 "Non-nil means extract any time specification from the diary entry."
9353 :group 'org-agenda
9354 :version "24.1"
9355 :type 'boolean)
9357 (defcustom org-agenda-bulk-mark-char ">"
9358 "A single-character string to be used as the bulk mark."
9359 :group 'org-agenda
9360 :version "24.1"
9361 :type 'string)
9363 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9364 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9365 If TEXT is not empty, it will become the headline of the new entry, and
9366 the resulting entry will not be shown. When TEXT is empty, switch to
9367 `org-agenda-diary-file' and let the user finish the entry there."
9368 (let ((cw (current-window-configuration)))
9369 (org-switch-to-buffer-other-window
9370 (find-file-noselect org-agenda-diary-file))
9371 (widen)
9372 (goto-char (point-min))
9373 (cond
9374 ((eq type 'anniversary)
9375 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
9376 (progn
9377 (or (org-at-heading-p t)
9378 (progn
9379 (outline-next-heading)
9380 (insert "* Anniversaries\n\n")
9381 (beginning-of-line -1)))))
9382 (outline-next-heading)
9383 (org-back-over-empty-lines)
9384 (backward-char 1)
9385 (insert "\n")
9386 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9387 (nth 2 d1) (car d1) (nth 1 d1) text)))
9388 ((eq type 'day)
9389 (let ((org-prefix-has-time t)
9390 (org-agenda-time-leading-zero t)
9391 fmt time time2)
9392 (if org-agenda-insert-diary-extract-time
9393 ;; Use org-agenda-format-item to parse text for a time-range and
9394 ;; remove it. FIXME: This is a hack, we should refactor
9395 ;; that function to make time extraction available separately
9396 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9397 time (get-text-property 0 'time fmt)
9398 time2 (if (> (length time) 0)
9399 ;; split-string removes trailing ...... if
9400 ;; no end time given. First space
9401 ;; separates time from date.
9402 (concat " " (car (split-string time "\\.")))
9403 nil)
9404 text (get-text-property 0 'txt fmt)))
9405 (if (eq org-agenda-insert-diary-strategy 'top-level)
9406 (org-agenda-insert-diary-as-top-level text)
9407 (require 'org-datetree)
9408 (org-datetree-find-date-create d1)
9409 (org-agenda-insert-diary-make-new-entry text))
9410 (org-insert-time-stamp (org-time-from-absolute
9411 (calendar-absolute-from-gregorian d1))
9412 nil nil nil nil time2))
9413 (end-of-line 0))
9414 ((eq type 'block)
9415 (if (> (calendar-absolute-from-gregorian d1)
9416 (calendar-absolute-from-gregorian d2))
9417 (setq d1 (prog1 d2 (setq d2 d1))))
9418 (if (eq org-agenda-insert-diary-strategy 'top-level)
9419 (org-agenda-insert-diary-as-top-level text)
9420 (require 'org-datetree)
9421 (org-datetree-find-date-create d1)
9422 (org-agenda-insert-diary-make-new-entry text))
9423 (org-insert-time-stamp (org-time-from-absolute
9424 (calendar-absolute-from-gregorian d1)))
9425 (insert "--")
9426 (org-insert-time-stamp (org-time-from-absolute
9427 (calendar-absolute-from-gregorian d2)))
9428 (end-of-line 0)))
9429 (if (string-match "\\S-" text)
9430 (progn
9431 (set-window-configuration cw)
9432 (message "%s entry added to %s"
9433 (capitalize (symbol-name type))
9434 (abbreviate-file-name org-agenda-diary-file)))
9435 (org-reveal t)
9436 (message "Please finish entry here"))))
9438 (defun org-agenda-insert-diary-as-top-level (text)
9439 "Make new entry as a top-level entry at the end of the file.
9440 Add TEXT as headline, and position the cursor in the second line so that
9441 a timestamp can be added there."
9442 (widen)
9443 (goto-char (point-max))
9444 (unless (bolp) (insert "\n"))
9445 (org-insert-heading nil t t)
9446 (insert text)
9447 (org-end-of-meta-data)
9448 (unless (bolp) (insert "\n"))
9449 (when org-adapt-indentation (indent-to-column 2)))
9451 (defun org-agenda-insert-diary-make-new-entry (text)
9452 "Make a new entry with TEXT as a child of the current subtree.
9453 Position the point in the heading's first body line so that
9454 a timestamp can be added there."
9455 (cond
9456 ((eq org-agenda-insert-diary-strategy 'date-tree-last)
9457 (end-of-line)
9458 (org-insert-heading '(4) t)
9459 (org-do-demote))
9461 (outline-next-heading)
9462 (org-back-over-empty-lines)
9463 (unless (looking-at "[ \t]*$") (save-excursion (insert "\n")))
9464 (org-insert-heading nil t)
9465 (org-do-demote)))
9466 (let ((col (current-column)))
9467 (insert text)
9468 (org-end-of-meta-data)
9469 ;; Ensure point is left on a blank line, at proper indentation.
9470 (unless (bolp) (insert "\n"))
9471 (unless (looking-at-p "^[ \t]*$") (save-excursion (insert "\n")))
9472 (when org-adapt-indentation (indent-to-column col)))
9473 (org-show-set-visibility 'lineage))
9475 (defun org-agenda-diary-entry ()
9476 "Make a diary entry, like the `i' command from the calendar.
9477 All the standard commands work: block, weekly etc.
9478 When `org-agenda-diary-file' points to a file,
9479 `org-agenda-diary-entry-in-org-file' is called instead to create
9480 entries in that Org file."
9481 (interactive)
9482 (if (not (eq org-agenda-diary-file 'diary-file))
9483 (org-agenda-diary-entry-in-org-file)
9484 (require 'diary-lib)
9485 (let* ((char (progn
9486 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
9487 (read-char-exclusive)))
9488 (cmd (cdr (assoc char
9489 '((?d . diary-insert-entry)
9490 (?w . diary-insert-weekly-entry)
9491 (?m . diary-insert-monthly-entry)
9492 (?y . diary-insert-yearly-entry)
9493 (?a . diary-insert-anniversary-entry)
9494 (?b . diary-insert-block-entry)
9495 (?c . diary-insert-cyclic-entry)))))
9496 (oldf (symbol-function 'calendar-cursor-to-date))
9497 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9498 (point (point))
9499 (mark (or (mark t) (point))))
9500 (unless cmd
9501 (user-error "No command associated with <%c>" char))
9502 (unless (and (get-text-property point 'day)
9503 (or (not (equal ?b char))
9504 (get-text-property mark 'day)))
9505 (user-error "Don't know which date to use for diary entry"))
9506 ;; We implement this by hacking the `calendar-cursor-to-date' function
9507 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9508 (let ((calendar-mark-ring
9509 (list (calendar-gregorian-from-absolute
9510 (or (get-text-property mark 'day)
9511 (get-text-property point 'day))))))
9512 (unwind-protect
9513 (progn
9514 (fset 'calendar-cursor-to-date
9515 (lambda (&optional error dummy)
9516 (calendar-gregorian-from-absolute
9517 (get-text-property point 'day))))
9518 (call-interactively cmd))
9519 (fset 'calendar-cursor-to-date oldf))))))
9521 (defun org-agenda-execute-calendar-command (cmd)
9522 "Execute a calendar command from the agenda with date from cursor."
9523 (org-agenda-check-type t 'agenda 'timeline)
9524 (require 'diary-lib)
9525 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9526 (user-error "Don't know which date to use for the calendar command"))
9527 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9528 (point (point))
9529 (date (calendar-gregorian-from-absolute
9530 (get-text-property point 'day)))
9531 ;; the following 2 vars are needed in the calendar
9532 (displayed-month (car date))
9533 (displayed-year (nth 2 date)))
9534 (unwind-protect
9535 (progn
9536 (fset 'calendar-cursor-to-date
9537 (lambda (&optional error dummy)
9538 (calendar-gregorian-from-absolute
9539 (get-text-property point 'day))))
9540 (call-interactively cmd))
9541 (fset 'calendar-cursor-to-date oldf))))
9543 (defun org-agenda-phases-of-moon ()
9544 "Display the phases of the moon for the 3 months around the cursor date."
9545 (interactive)
9546 (org-agenda-execute-calendar-command 'calendar-lunar-phases))
9548 (defun org-agenda-holidays ()
9549 "Display the holidays for the 3 months around the cursor date."
9550 (interactive)
9551 (org-agenda-execute-calendar-command 'calendar-list-holidays))
9553 (defvar calendar-longitude) ; defined in calendar.el
9554 (defvar calendar-latitude) ; defined in calendar.el
9555 (defvar calendar-location-name) ; defined in calendar.el
9557 (defun org-agenda-sunrise-sunset (arg)
9558 "Display sunrise and sunset for the cursor date.
9559 Latitude and longitude can be specified with the variables
9560 `calendar-latitude' and `calendar-longitude'. When called with prefix
9561 argument, latitude and longitude will be prompted for."
9562 (interactive "P")
9563 (require 'solar)
9564 (let ((calendar-longitude (if arg nil calendar-longitude))
9565 (calendar-latitude (if arg nil calendar-latitude))
9566 (calendar-location-name
9567 (if arg "the given coordinates" calendar-location-name)))
9568 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9570 (defun org-agenda-goto-calendar ()
9571 "Open the Emacs calendar with the date at the cursor."
9572 (interactive)
9573 (org-agenda-check-type t 'agenda 'timeline)
9574 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9575 (user-error "Don't know which date to open in calendar")))
9576 (date (calendar-gregorian-from-absolute day))
9577 (calendar-move-hook nil)
9578 (calendar-view-holidays-initially-flag nil)
9579 (calendar-view-diary-initially-flag nil))
9580 (calendar)
9581 (calendar-goto-date date)))
9583 ;;;###autoload
9584 (defun org-calendar-goto-agenda ()
9585 "Compute the Org agenda for the calendar date displayed at the cursor.
9586 This is a command that has to be installed in `calendar-mode-map'."
9587 (interactive)
9588 ;; Temporarily disable sticky agenda since user clearly wants to
9589 ;; refresh view anyway.
9590 (let ((org-agenda-buffer-tmp-name "*Org Agenda(a)*")
9591 (org-agenda-sticky nil))
9592 (org-agenda-list nil (calendar-absolute-from-gregorian
9593 (calendar-cursor-to-date))
9594 nil)))
9596 (defun org-agenda-convert-date ()
9597 (interactive)
9598 (org-agenda-check-type t 'agenda 'timeline)
9599 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9600 date s)
9601 (unless day
9602 (user-error "Don't know which date to convert"))
9603 (setq date (calendar-gregorian-from-absolute day))
9604 (setq s (concat
9605 "Gregorian: " (calendar-date-string date) "\n"
9606 "ISO: " (calendar-iso-date-string date) "\n"
9607 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9608 "Julian: " (calendar-julian-date-string date) "\n"
9609 "Astron. JD: " (calendar-astro-date-string date)
9610 " (Julian date number at noon UTC)\n"
9611 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9612 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9613 "French: " (calendar-french-date-string date) "\n"
9614 "Bahá’í: " (calendar-bahai-date-string date) " (until sunset)\n"
9615 "Mayan: " (calendar-mayan-date-string date) "\n"
9616 "Coptic: " (calendar-coptic-date-string date) "\n"
9617 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9618 "Persian: " (calendar-persian-date-string date) "\n"
9619 "Chinese: " (calendar-chinese-date-string date) "\n"))
9620 (with-output-to-temp-buffer "*Dates*"
9621 (princ s))
9622 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9624 ;;; Bulk commands
9626 (defun org-agenda-bulk-marked-p ()
9627 "Non-nil when current entry is marked for bulk action."
9628 (eq (get-char-property (point-at-bol) 'type)
9629 'org-marked-entry-overlay))
9631 (defun org-agenda-bulk-mark (&optional arg)
9632 "Mark the entry at point for future bulk action."
9633 (interactive "p")
9634 (dotimes (i (or arg 1))
9635 (unless (org-get-at-bol 'org-agenda-diary-link)
9636 (let* ((m (org-get-at-bol 'org-hd-marker))
9638 (unless (org-agenda-bulk-marked-p)
9639 (unless m (user-error "Nothing to mark at point"))
9640 (push m org-agenda-bulk-marked-entries)
9641 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9642 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9643 (org-get-todo-face "TODO")
9644 'evaporate)
9645 (overlay-put ov 'type 'org-marked-entry-overlay))
9646 (end-of-line 1)
9647 (or (ignore-errors
9648 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9649 (beginning-of-line 2))
9650 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9651 (beginning-of-line 2))
9652 (message "%d entries marked for bulk action"
9653 (length org-agenda-bulk-marked-entries))))))
9655 (defun org-agenda-bulk-mark-all ()
9656 "Mark all entries for future agenda bulk action."
9657 (interactive)
9658 (org-agenda-bulk-mark-regexp "."))
9660 (defun org-agenda-bulk-mark-regexp (regexp)
9661 "Mark entries matching REGEXP for future agenda bulk action."
9662 (interactive "sMark entries matching regexp: ")
9663 (let ((entries-marked 0) txt-at-point)
9664 (save-excursion
9665 (goto-char (point-min))
9666 (goto-char (next-single-property-change (point) 'org-hd-marker))
9667 (while (and (re-search-forward regexp nil t)
9668 (setq txt-at-point (get-text-property (point) 'txt)))
9669 (if (get-char-property (point) 'invisible)
9670 (beginning-of-line 2)
9671 (when (string-match regexp txt-at-point)
9672 (setq entries-marked (1+ entries-marked))
9673 (call-interactively 'org-agenda-bulk-mark)))))
9675 (if (not entries-marked)
9676 (message "No entry matching this regexp."))))
9678 (defun org-agenda-bulk-unmark (&optional arg)
9679 "Unmark the entry at point for future bulk action."
9680 (interactive "P")
9681 (if arg
9682 (org-agenda-bulk-unmark-all)
9683 (cond ((org-agenda-bulk-marked-p)
9684 (org-agenda-bulk-remove-overlays
9685 (point-at-bol) (+ 2 (point-at-bol)))
9686 (setq org-agenda-bulk-marked-entries
9687 (delete (org-get-at-bol 'org-hd-marker)
9688 org-agenda-bulk-marked-entries))
9689 (end-of-line 1)
9690 (or (ignore-errors
9691 (goto-char (next-single-property-change (point) 'txt)))
9692 (beginning-of-line 2))
9693 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9694 (beginning-of-line 2))
9695 (message "%d entries left marked for bulk action"
9696 (length org-agenda-bulk-marked-entries)))
9697 (t (message "No entry to unmark here")))))
9699 (defun org-agenda-bulk-toggle-all ()
9700 "Toggle all marks for bulk action."
9701 (interactive)
9702 (save-excursion
9703 (goto-char (point-min))
9704 (while (ignore-errors
9705 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9706 (org-agenda-bulk-toggle))))
9708 (defun org-agenda-bulk-toggle ()
9709 "Toggle the mark at point for bulk action."
9710 (interactive)
9711 (if (org-agenda-bulk-marked-p)
9712 (org-agenda-bulk-unmark)
9713 (org-agenda-bulk-mark)))
9715 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9716 "Remove the mark overlays between BEG and END in the agenda buffer.
9717 BEG and END default to the buffer limits.
9719 This only removes the overlays, it does not remove the markers
9720 from the list in `org-agenda-bulk-marked-entries'."
9721 (interactive)
9722 (mapc (lambda (ov)
9723 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9724 (delete-overlay ov)))
9725 (overlays-in (or beg (point-min)) (or end (point-max)))))
9727 (defun org-agenda-bulk-unmark-all ()
9728 "Remove all marks in the agenda buffer.
9729 This will remove the markers and the overlays."
9730 (interactive)
9731 (if (null org-agenda-bulk-marked-entries)
9732 (message "No entry to unmark")
9733 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
9734 (setq org-agenda-bulk-marked-entries nil)
9735 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9737 (defcustom org-agenda-persistent-marks nil
9738 "Non-nil means marked items will stay marked after a bulk action.
9739 You can toggle this interactively by typing `p' when prompted for a
9740 bulk action."
9741 :group 'org-agenda
9742 :version "24.1"
9743 :type 'boolean)
9745 (defun org-agenda-bulk-action (&optional arg)
9746 "Execute an remote-editing action on all marked entries.
9747 The prefix arg is passed through to the command if possible."
9748 (interactive "P")
9749 ;; Make sure we have markers, and only valid ones
9750 (unless org-agenda-bulk-marked-entries (user-error "No entries are marked"))
9751 (mapc
9752 (lambda (m)
9753 (unless (and (markerp m)
9754 (marker-buffer m)
9755 (buffer-live-p (marker-buffer m))
9756 (marker-position m))
9757 (user-error "Marker %s for bulk command is invalid" m)))
9758 org-agenda-bulk-marked-entries)
9760 ;; Prompt for the bulk command
9761 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
9762 (message (concat msg "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9763 "[S]catter [f]unction "
9764 (when org-agenda-bulk-custom-functions
9765 (concat " Custom: ["
9766 (mapconcat (lambda(f) (char-to-string (car f)))
9767 org-agenda-bulk-custom-functions "")
9768 "]"))))
9769 (catch 'exit
9770 (let* ((action (read-char-exclusive))
9771 (org-log-refile (if org-log-refile 'time nil))
9772 (entries (reverse org-agenda-bulk-marked-entries))
9773 (org-overriding-default-time
9774 (if (get-text-property (point) 'org-agenda-date-header)
9775 (org-get-cursor-date)))
9776 redo-at-end
9777 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
9778 (cond
9779 ((equal action ?p)
9780 (let ((org-agenda-persistent-marks
9781 (not org-agenda-persistent-marks)))
9782 (org-agenda-bulk-action)
9783 (throw 'exit nil)))
9785 ((equal action ?$)
9786 (setq cmd '(org-agenda-archive)))
9788 ((equal action ?A)
9789 (setq cmd '(org-agenda-archive-to-archive-sibling)))
9791 ((member action '(?r ?w))
9792 (setq rfloc (org-refile-get-location
9793 "Refile to"
9794 (marker-buffer (car entries))
9795 org-refile-allow-creating-parent-nodes))
9796 (if (nth 3 rfloc)
9797 (setcar (nthcdr 3 rfloc)
9798 (move-marker (make-marker) (nth 3 rfloc)
9799 (or (get-file-buffer (nth 1 rfloc))
9800 (find-buffer-visiting (nth 1 rfloc))
9801 (error "This should not happen")))))
9803 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
9804 redo-at-end t))
9806 ((equal action ?t)
9807 (setq state (completing-read
9808 "Todo state: "
9809 (with-current-buffer (marker-buffer (car entries))
9810 (mapcar #'list org-todo-keywords-1))))
9811 (setq cmd `(let ((org-inhibit-blocking t)
9812 (org-inhibit-logging 'note))
9813 (org-agenda-todo ,state))))
9815 ((memq action '(?- ?+))
9816 (setq tag (completing-read
9817 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9818 (with-current-buffer (marker-buffer (car entries))
9819 (delq nil
9820 (mapcar (lambda (x) (and (stringp (car x)) x))
9821 org-current-tag-alist)))))
9822 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
9824 ((memq action '(?s ?d))
9825 (let* ((time
9826 (unless arg
9827 (org-read-date
9828 nil nil nil
9829 (if (eq action ?s) "(Re)Schedule to" "(Re)Set Deadline to")
9830 org-overriding-default-time)))
9831 (c1 (if (eq action ?s) 'org-agenda-schedule
9832 'org-agenda-deadline)))
9833 ;; Make sure to not prompt for a note when bulk
9834 ;; rescheduling as Org cannot cope with simultaneous Org.
9835 ;; Besides, it could be annoying depending on the number
9836 ;; of items re-scheduled.
9837 (setq cmd `(eval '(let ((org-log-reschedule
9838 (and org-log-reschedule 'time)))
9839 (,c1 arg ,time))))))
9841 ((equal action ?S)
9842 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
9843 (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
9844 (let ((days (read-number
9845 (format "Scatter tasks across how many %sdays: "
9846 (if arg "week" "")) 7)))
9847 (setq cmd
9848 `(let ((distance (1+ (random ,days))))
9849 (if arg
9850 (let ((dist distance)
9851 (day-of-week
9852 (calendar-day-of-week
9853 (calendar-gregorian-from-absolute (org-today)))))
9854 (dotimes (i (1+ dist))
9855 (while (member day-of-week org-agenda-weekend-days)
9856 (cl-incf distance)
9857 (cl-incf day-of-week)
9858 (when (= day-of-week 7)
9859 (setq day-of-week 0)))
9860 (cl-incf day-of-week)
9861 (when (= day-of-week 7)
9862 (setq day-of-week 0)))))
9863 ;; silently fail when try to replan a sexp entry
9864 (condition-case nil
9865 (let* ((date (calendar-gregorian-from-absolute
9866 (+ (org-today) distance)))
9867 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9868 (nth 2 date))))
9869 (org-agenda-schedule nil time))
9870 (error nil)))))))
9872 ((assoc action org-agenda-bulk-custom-functions)
9873 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
9874 redo-at-end t))
9876 ((equal action ?f)
9877 (setq cmd (list (intern
9878 (completing-read "Function: "
9879 obarray 'fboundp t nil nil)))))
9881 (t (user-error "Invalid bulk action")))
9883 ;; Sort the markers, to make sure that parents are handled before children
9884 (setq entries (sort entries
9885 (lambda (a b)
9886 (cond
9887 ((equal (marker-buffer a) (marker-buffer b))
9888 (< (marker-position a) (marker-position b)))
9890 (string< (buffer-name (marker-buffer a))
9891 (buffer-name (marker-buffer b))))))))
9893 ;; Now loop over all markers and apply cmd
9894 (while (setq e (pop entries))
9895 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
9896 (if (not pos)
9897 (progn (message "Skipping removed entry at %s" e)
9898 (setq cntskip (1+ cntskip)))
9899 (goto-char pos)
9900 (let (org-loop-over-headlines-in-active-region)
9901 (eval cmd))
9902 ;; `post-command-hook' is not run yet. We make sure any
9903 ;; pending log note is processed.
9904 (when (or (memq 'org-add-log-note (default-value 'post-command-hook))
9905 (memq 'org-add-log-note post-command-hook))
9906 (org-add-log-note))
9907 (setq cnt (1+ cnt))))
9908 (when redo-at-end (org-agenda-redo))
9909 (unless org-agenda-persistent-marks
9910 (org-agenda-bulk-unmark-all))
9911 (message "Acted on %d entries%s%s"
9913 (if (= cntskip 0)
9915 (format ", skipped %d (disappeared before their turn)"
9916 cntskip))
9917 (if (not org-agenda-persistent-marks)
9918 "" " (kept marked)"))))))
9920 (defun org-agenda-capture (&optional with-time)
9921 "Call `org-capture' with the date at point.
9922 With a `C-1' prefix, use the HH:MM value at point (if any) or the
9923 current HH:MM time."
9924 (interactive "P")
9925 (if (not (eq major-mode 'org-agenda-mode))
9926 (user-error "You cannot do this outside of agenda buffers")
9927 (let ((org-overriding-default-time
9928 (org-get-cursor-date (equal with-time 1))))
9929 (call-interactively 'org-capture))))
9931 ;;; Dragging agenda lines forward/backward
9933 (defun org-agenda-reapply-filters ()
9934 "Re-apply all agenda filters."
9935 (mapcar
9936 (lambda(f) (when (car f) (org-agenda-filter-apply (car f) (cadr f) t)))
9937 `((,org-agenda-tag-filter tag)
9938 (,org-agenda-category-filter category)
9939 (,org-agenda-regexp-filter regexp)
9940 (,org-agenda-effort-filter effort)
9941 (,(get 'org-agenda-tag-filter :preset-filter) tag)
9942 (,(get 'org-agenda-category-filter :preset-filter) category)
9943 (,(get 'org-agenda-effort-filter :preset-filter) effort)
9944 (,(get 'org-agenda-regexp-filter :preset-filter) regexp))))
9946 (defun org-agenda-drag-line-forward (arg &optional backward)
9947 "Drag an agenda line forward by ARG lines.
9948 When the optional argument `backward' is non-nil, move backward."
9949 (interactive "p")
9950 (let ((inhibit-read-only t) lst line)
9951 (if (or (not (get-text-property (point) 'txt))
9952 (save-excursion
9953 (dotimes (n arg)
9954 (move-beginning-of-line (if backward 0 2))
9955 (push (not (get-text-property (point) 'txt)) lst))
9956 (delq nil lst)))
9957 (message "Cannot move line forward")
9958 (let ((end (save-excursion (move-beginning-of-line 2) (point))))
9959 (move-beginning-of-line 1)
9960 (setq line (buffer-substring (point) end))
9961 (delete-region (point) end)
9962 (move-beginning-of-line (funcall (if backward '1- '1+) arg))
9963 (insert line)
9964 (org-agenda-reapply-filters)
9965 (org-agenda-mark-clocking-task)
9966 (move-beginning-of-line 0)))))
9968 (defun org-agenda-drag-line-backward (arg)
9969 "Drag an agenda line backward by ARG lines."
9970 (interactive "p")
9971 (org-agenda-drag-line-forward arg t))
9973 ;;; Flagging notes
9975 (defun org-agenda-show-the-flagging-note ()
9976 "Display the flagging note in the other window.
9977 When called a second time in direct sequence, offer to remove the FLAGGING
9978 tag and (if present) the flagging note."
9979 (interactive)
9980 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
9981 (win (selected-window))
9982 note heading newhead)
9983 (unless hdmarker
9984 (user-error "No linked entry at point"))
9985 (if (and (eq this-command last-command)
9986 (y-or-n-p "Unflag and remove any flagging note? "))
9987 (progn
9988 (org-agenda-remove-flag hdmarker)
9989 (let ((win (get-buffer-window "*Flagging Note*")))
9990 (and win (delete-window win)))
9991 (message "Entry unflagged"))
9992 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
9993 (unless note
9994 (user-error "No flagging note"))
9995 (org-kill-new note)
9996 (org-switch-to-buffer-other-window "*Flagging Note*")
9997 (erase-buffer)
9998 (insert note)
9999 (goto-char (point-min))
10000 (while (re-search-forward "\\\\n" nil t)
10001 (replace-match "\n" t t))
10002 (goto-char (point-min))
10003 (select-window win)
10004 (message "%s" (substitute-command-keys "Flagging note pushed to \
10005 kill ring. Press `\\[org-agenda-show-the-flagging-note]' again to remove \
10006 tag and note")))))
10008 (defun org-agenda-remove-flag (marker)
10009 "Remove the FLAGGED tag and any flagging note in the entry."
10010 (let (newhead)
10011 (org-with-point-at marker
10012 (org-toggle-tag "FLAGGED" 'off)
10013 (org-entry-delete nil "THEFLAGGINGNOTE")
10014 (setq newhead (org-get-heading)))
10015 (org-agenda-change-all-lines newhead marker)
10016 (message "Entry unflagged")))
10018 (defun org-agenda-get-any-marker (&optional pos)
10019 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
10020 (get-text-property (or pos (point-at-bol)) 'org-marker)))
10022 ;;; Appointment reminders
10024 (defvar appt-time-msg-list) ; defined in appt.el
10026 ;;;###autoload
10027 (defun org-agenda-to-appt (&optional refresh filter &rest args)
10028 "Activate appointments found in `org-agenda-files'.
10030 With a `\\[universal-argument]' prefix, refresh the list of \
10031 appointments.
10033 If FILTER is t, interactively prompt the user for a regular
10034 expression, and filter out entries that don't match it.
10036 If FILTER is a string, use this string as a regular expression
10037 for filtering entries out.
10039 If FILTER is a function, filter out entries against which
10040 calling the function returns nil. This function takes one
10041 argument: an entry from `org-agenda-get-day-entries'.
10043 FILTER can also be an alist with the car of each cell being
10044 either `headline' or `category'. For example:
10046 \\='((headline \"IMPORTANT\")
10047 (category \"Work\"))
10049 will only add headlines containing IMPORTANT or headlines
10050 belonging to the \"Work\" category.
10052 ARGS are symbols indicating what kind of entries to consider.
10053 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
10054 \(i.e., deadlines and scheduled items with a hh:mm specification)
10055 and :timestamp entries. See the docstring of `org-diary' for
10056 details and examples.
10058 If an entry has a APPT_WARNTIME property, its value will be used
10059 to override `appt-message-warning-time'."
10060 (interactive "P")
10061 (if refresh (setq appt-time-msg-list nil))
10062 (if (eq filter t)
10063 (setq filter (read-from-minibuffer "Regexp filter: ")))
10064 (let* ((cnt 0) ; count added events
10065 (scope (or args '(:deadline* :scheduled* :timestamp)))
10066 (org-agenda-new-buffers nil)
10067 (org-deadline-warning-days 0)
10068 ;; Do not use `org-today' here because appt only takes
10069 ;; time and without date as argument, so it may pass wrong
10070 ;; information otherwise
10071 (today (org-date-to-gregorian
10072 (time-to-days (current-time))))
10073 (org-agenda-restrict nil)
10074 (files (org-agenda-files 'unrestricted)) entries file
10075 (org-agenda-buffer nil))
10076 ;; Get all entries which may contain an appt
10077 (org-agenda-prepare-buffers files)
10078 (while (setq file (pop files))
10079 (setq entries
10080 (delq nil
10081 (append entries
10082 (apply 'org-agenda-get-day-entries
10083 file today scope)))))
10084 ;; Map thru entries and find if we should filter them out
10085 (mapc
10086 (lambda (x)
10087 (let* ((evt (org-trim
10088 (replace-regexp-in-string
10089 org-bracket-link-regexp "\\3"
10090 (or (get-text-property 1 'txt x) ""))))
10091 (cat (get-text-property (1- (length x)) 'org-category x))
10092 (tod (get-text-property 1 'time-of-day x))
10093 (ok (or (null filter)
10094 (and (stringp filter) (string-match filter evt))
10095 (and (functionp filter) (funcall filter x))
10096 (and (listp filter)
10097 (let ((cat-filter (cadr (assq 'category filter)))
10098 (evt-filter (cadr (assq 'headline filter))))
10099 (or (and (stringp cat-filter)
10100 (string-match cat-filter cat))
10101 (and (stringp evt-filter)
10102 (string-match evt-filter evt)))))))
10103 (wrn (get-text-property 1 'warntime x)))
10104 ;; FIXME: Shall we remove text-properties for the appt text?
10105 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
10106 (when (and ok tod (not (string-match "\\`DONE\\|CANCELLED" evt)))
10107 (setq tod (concat "00" (number-to-string tod)))
10108 (setq tod (when (string-match
10109 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
10110 (concat (match-string 1 tod) ":"
10111 (match-string 2 tod))))
10112 (when (if (version< emacs-version "23.3")
10113 (appt-add tod evt)
10114 (appt-add tod evt wrn))
10115 (setq cnt (1+ cnt))))))
10116 entries)
10117 (org-release-buffers org-agenda-new-buffers)
10118 (if (eq cnt 0)
10119 (message "No event to add")
10120 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
10122 (defun org-agenda-today-p (date)
10123 "Non nil when DATE means today.
10124 DATE is either a list of the form (month day year) or a number of
10125 days as returned by `calendar-absolute-from-gregorian' or
10126 `org-today'. This function considers `org-extend-today-until'
10127 when defining today."
10128 (eq (org-today)
10129 (if (consp date) (calendar-absolute-from-gregorian date) date)))
10131 (defun org-agenda-todo-yesterday (&optional arg)
10132 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
10133 (interactive "P")
10134 (let* ((org-use-effective-time t)
10135 (hour (nth 2 (decode-time (org-current-time))))
10136 (org-extend-today-until (1+ hour)))
10137 (org-agenda-todo arg)))
10139 (provide 'org-agenda)
10141 ;;; org-agenda.el ends here