org-compat: Fix migration to `org-duration-format'
[org-mode/org-tableheadings.git] / lisp / org-agenda.el
blob4f18f4b34b3bacfd87160e346f54a4fd5d601aeb
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 non-nil, show a repeated entry at its latest repeat date,
1267 possibly being today, instead of its base date, even if it wasn't
1268 marked as done. This setting is useful if you do not always mark
1269 repeated entries as done and, yet, consider that reaching repeat
1270 date starts the task anew.
1272 When set to a list of strings, prefer last repeats only for
1273 entries with these TODO keywords."
1274 :group 'org-agenda-daily/weekly
1275 :type '(choice
1276 (const :tag "Prefer last repeat" t)
1277 (const :tag "Prefer base date" nil)
1278 (repeat :tag "Prefer last repeat for entries with these TODO keywords"
1279 (string :tag "TODO keyword")))
1280 :version "26.1"
1281 :package-version '(Org . "9.1")
1282 :safe (lambda (x) (or (booleanp x) (consp x))))
1284 (defcustom org-scheduled-past-days 10000
1285 "Number of days to continue listing scheduled items not marked DONE.
1286 When an item is scheduled on a date, it shows up in the agenda on
1287 this day and will be listed until it is marked done or for the
1288 number of days given here."
1289 :group 'org-agenda-daily/weekly
1290 :type 'integer)
1292 (defcustom org-agenda-log-mode-items '(closed clock)
1293 "List of items that should be shown in agenda log mode.
1294 \\<org-agenda-mode-map>\
1295 This list may contain the following symbols:
1297 closed Show entries that have been closed on that day.
1298 clock Show entries that have received clocked time on that day.
1299 state Show all logged state changes.
1300 Note that instead of changing this variable, you can also press \
1301 `\\[universal-argument] \\[org-agenda-log-mode]' in
1302 the agenda to display all available LOG items temporarily."
1303 :group 'org-agenda-daily/weekly
1304 :type '(set :greedy t (const closed) (const clock) (const state)))
1306 (defcustom org-agenda-clock-consistency-checks
1307 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1308 :gap-ok-around ("4:00")
1309 :default-face ((:background "DarkRed") (:foreground "white"))
1310 :overlap-face nil :gap-face nil :no-end-time-face nil
1311 :long-face nil :short-face nil)
1312 "This is a property list, with the following keys:
1314 :max-duration Mark clocking chunks that are longer than this time.
1315 This is a time string like \"HH:MM\", or the number
1316 of minutes as an integer.
1318 :min-duration Mark clocking chunks that are shorter that this.
1319 This is a time string like \"HH:MM\", or the number
1320 of minutes as an integer.
1322 :max-gap Mark gaps between clocking chunks that are longer than
1323 this duration. A number of minutes, or a string
1324 like \"HH:MM\".
1326 :gap-ok-around List of times during the day which are usually not working
1327 times. When a gap is detected, but the gap contains any
1328 of these times, the gap is *not* reported. For example,
1329 if this is (\"4:00\" \"13:00\") then gaps that contain
1330 4:00 in the morning (i.e. the night) and 13:00
1331 (i.e. a typical lunch time) do not cause a warning.
1332 You should have at least one time during the night in this
1333 list, or otherwise the first task each morning will trigger
1334 a warning because it follows a long gap.
1336 Furthermore, the following properties can be used to define faces for
1337 issue display.
1339 :default-face the default face, if the specific face is undefined
1340 :overlap-face face for overlapping clocks
1341 :gap-face face for gaps between clocks
1342 :no-end-time-face face for incomplete clocks
1343 :long-face face for clock intervals that are too long
1344 :short-face face for clock intervals that are too short"
1345 :group 'org-agenda-daily/weekly
1346 :group 'org-clock
1347 :version "24.1"
1348 :type 'plist)
1350 (defcustom org-agenda-log-mode-add-notes t
1351 "Non-nil means add first line of notes to log entries in agenda views.
1352 If a log item like a state change or a clock entry is associated with
1353 notes, the first line of these notes will be added to the entry in the
1354 agenda display."
1355 :group 'org-agenda-daily/weekly
1356 :type 'boolean)
1358 (defcustom org-agenda-start-with-log-mode nil
1359 "The initial value of log-mode in a newly created agenda window.
1360 See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
1361 explanations on the possible values."
1362 :group 'org-agenda-startup
1363 :group 'org-agenda-daily/weekly
1364 :type '(choice (const :tag "Don't show log items" nil)
1365 (const :tag "Show only log items" only)
1366 (const :tag "Show all possible log items" clockcheck)
1367 (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
1368 (choice (const :tag "Show closed log items" closed)
1369 (const :tag "Show clocked log items" clock)
1370 (const :tag "Show all logged state changes" state)))))
1372 (defcustom org-agenda-start-with-clockreport-mode nil
1373 "The initial value of clockreport-mode in a newly created agenda window."
1374 :group 'org-agenda-startup
1375 :group 'org-agenda-daily/weekly
1376 :type 'boolean)
1378 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1379 "Property list with parameters for the clocktable in clockreport mode.
1380 This is the display mode that shows a clock table in the daily/weekly
1381 agenda, the properties for this dynamic block can be set here.
1382 The usual clocktable parameters are allowed here, but you cannot set
1383 the properties :name, :tstart, :tend, :block, and :scope - these will
1384 be overwritten to make sure the content accurately reflects the
1385 current display in the agenda."
1386 :group 'org-agenda-daily/weekly
1387 :type 'plist)
1389 (defcustom org-agenda-search-view-always-boolean nil
1390 "Non-nil means the search string is interpreted as individual parts.
1392 The search string for search view can either be interpreted as a phrase,
1393 or as a list of snippets that define a boolean search for a number of
1394 strings.
1396 When this is non-nil, the string will be split on whitespace, and each
1397 snippet will be searched individually, and all must match in order to
1398 select an entry. A snippet is then a single string of non-white
1399 characters, or a string in double quotes, or a regexp in {} braces.
1400 If a snippet is preceded by \"-\", the snippet must *not* match.
1401 \"+\" is syntactic sugar for positive selection. Each snippet may
1402 be found as a full word or a partial word, but see the variable
1403 `org-agenda-search-view-force-full-words'.
1405 When this is nil, search will look for the entire search phrase as one,
1406 with each space character matching any amount of whitespace, including
1407 line breaks.
1409 Even when this is nil, you can still switch to Boolean search dynamically
1410 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1411 is a regexp marked with braces like \"{abc}\", this will also switch to
1412 boolean search."
1413 :group 'org-agenda-search-view
1414 :version "24.1"
1415 :type 'boolean)
1417 (defvaralias 'org-agenda-search-view-search-words-only
1418 'org-agenda-search-view-always-boolean)
1420 (defcustom org-agenda-search-view-force-full-words nil
1421 "Non-nil means, search words must be matches as complete words.
1422 When nil, they may also match part of a word."
1423 :group 'org-agenda-search-view
1424 :version "24.1"
1425 :type 'boolean)
1427 (defcustom org-agenda-search-view-max-outline-level 0
1428 "Maximum outline level to display in search view.
1429 E.g. when this is set to 1, the search view will only
1430 show headlines of level 1. When set to 0, the default
1431 value, don't limit agenda view by outline level."
1432 :group 'org-agenda-search-view
1433 :version "24.4"
1434 :package-version '(Org . "8.3")
1435 :type 'integer)
1437 (defgroup org-agenda-time-grid nil
1438 "Options concerning the time grid in the Org Agenda."
1439 :tag "Org Agenda Time Grid"
1440 :group 'org-agenda)
1442 (defcustom org-agenda-search-headline-for-time t
1443 "Non-nil means search headline for a time-of-day.
1444 If the headline contains a time-of-day in one format or another, it will
1445 be used to sort the entry into the time sequence of items for a day.
1446 Some people have time stamps in the headline that refer to the creation
1447 time or so, and then this produces an unwanted side effect. If this is
1448 the case for your, use this variable to turn off searching the headline
1449 for a time."
1450 :group 'org-agenda-time-grid
1451 :type 'boolean)
1453 (defcustom org-agenda-use-time-grid t
1454 "Non-nil means show a time grid in the agenda schedule.
1455 A time grid is a set of lines for specific times (like every two hours between
1456 8:00 and 20:00). The items scheduled for a day at specific times are
1457 sorted in between these lines.
1458 For details about when the grid will be shown, and what it will look like, see
1459 the variable `org-agenda-time-grid'."
1460 :group 'org-agenda-time-grid
1461 :type 'boolean)
1463 (defcustom org-agenda-time-grid
1464 '((daily today require-timed)
1465 "----------------"
1466 (800 1000 1200 1400 1600 1800 2000))
1468 "The settings for time grid for agenda display.
1469 This is a list of three items. The first item is again a list. It contains
1470 symbols specifying conditions when the grid should be displayed:
1472 daily if the agenda shows a single day
1473 weekly if the agenda shows an entire week
1474 today show grid on current date, independent of daily/weekly display
1475 require-timed show grid only if at least one item has a time specification
1476 remove-match skip grid times already present in an entry
1478 The second item is a string which will be placed behind the grid time.
1480 The third item is a list of integers, indicating the times that should have
1481 a grid line."
1482 :group 'org-agenda-time-grid
1483 :type
1484 '(list
1485 (set :greedy t :tag "Grid Display Options"
1486 (const :tag "Show grid in single day agenda display" daily)
1487 (const :tag "Show grid in weekly agenda display" weekly)
1488 (const :tag "Always show grid for today" today)
1489 (const :tag "Show grid only if any timed entries are present"
1490 require-timed)
1491 (const :tag "Skip grid times already present in an entry"
1492 remove-match))
1493 (string :tag "Grid String")
1494 (repeat :tag "Grid Times" (integer :tag "Time"))))
1496 (defcustom org-agenda-show-current-time-in-grid t
1497 "Non-nil means show the current time in the time grid."
1498 :group 'org-agenda-time-grid
1499 :version "24.1"
1500 :type 'boolean)
1502 (defcustom org-agenda-current-time-string
1503 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1504 "The string for the current time marker in the agenda."
1505 :group 'org-agenda-time-grid
1506 :version "24.1"
1507 :type 'string)
1509 (defgroup org-agenda-sorting nil
1510 "Options concerning sorting in the Org Agenda."
1511 :tag "Org Agenda Sorting"
1512 :group 'org-agenda)
1514 (defcustom org-agenda-sorting-strategy
1515 '((agenda habit-down time-up priority-down category-keep)
1516 (todo priority-down category-keep)
1517 (tags priority-down category-keep)
1518 (search category-keep))
1519 "Sorting structure for the agenda items of a single day.
1520 This is a list of symbols which will be used in sequence to determine
1521 if an entry should be listed before another entry. The following
1522 symbols are recognized:
1524 time-up Put entries with time-of-day indications first, early first
1525 time-down Put entries with time-of-day indications first, late first
1526 timestamp-up Sort by any timestamp, early first
1527 timestamp-down Sort by any timestamp, late first
1528 scheduled-up Sort by scheduled timestamp, early first
1529 scheduled-down Sort by scheduled timestamp, late first
1530 deadline-up Sort by deadline timestamp, early first
1531 deadline-down Sort by deadline timestamp, late first
1532 ts-up Sort by active timestamp, early first
1533 ts-down Sort by active timestamp, late first
1534 tsia-up Sort by inactive timestamp, early first
1535 tsia-down Sort by inactive timestamp, late first
1536 category-keep Keep the default order of categories, corresponding to the
1537 sequence in `org-agenda-files'.
1538 category-up Sort alphabetically by category, A-Z.
1539 category-down Sort alphabetically by category, Z-A.
1540 tag-up Sort alphabetically by last tag, A-Z.
1541 tag-down Sort alphabetically by last tag, Z-A.
1542 priority-up Sort numerically by priority, high priority last.
1543 priority-down Sort numerically by priority, high priority first.
1544 todo-state-up Sort by todo state, tasks that are done last.
1545 todo-state-down Sort by todo state, tasks that are done first.
1546 effort-up Sort numerically by estimated effort, high effort last.
1547 effort-down Sort numerically by estimated effort, high effort first.
1548 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1549 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1550 habit-up Put entries that are habits first
1551 habit-down Put entries that are habits last
1552 alpha-up Sort headlines alphabetically
1553 alpha-down Sort headlines alphabetically, reversed
1555 The different possibilities will be tried in sequence, and testing stops
1556 if one comparison returns a \"not-equal\". For example, the default
1557 '(time-up category-keep priority-down)
1558 means: Pull out all entries having a specified time of day and sort them,
1559 in order to make a time schedule for the current day the first thing in the
1560 agenda listing for the day. Of the entries without a time indication, keep
1561 the grouped in categories, don't sort the categories, but keep them in
1562 the sequence given in `org-agenda-files'. Within each category sort by
1563 priority.
1565 Leaving out `category-keep' would mean that items will be sorted across
1566 categories by priority.
1568 Instead of a single list, this can also be a set of list for specific
1569 contents, with a context symbol in the car of the list, any of
1570 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1572 Custom commands can bind this variable in the options section."
1573 :group 'org-agenda-sorting
1574 :type `(choice
1575 (repeat :tag "General" ,org-sorting-choice)
1576 (list :tag "Individually"
1577 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1578 (repeat ,org-sorting-choice))
1579 (cons (const :tag "Strategy for TODO lists" todo)
1580 (repeat ,org-sorting-choice))
1581 (cons (const :tag "Strategy for Tags matches" tags)
1582 (repeat ,org-sorting-choice))
1583 (cons (const :tag "Strategy for search matches" search)
1584 (repeat ,org-sorting-choice)))))
1586 (defcustom org-agenda-cmp-user-defined nil
1587 "A function to define the comparison `user-defined'.
1588 This function must receive two arguments, agenda entry a and b.
1589 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1590 the user comparison, return nil.
1591 When this is defined, you can make `user-defined-up' and `user-defined-down'
1592 part of an agenda sorting strategy."
1593 :group 'org-agenda-sorting
1594 :type 'symbol)
1596 (defcustom org-sort-agenda-notime-is-late t
1597 "Non-nil means items without time are considered late.
1598 This is only relevant for sorting. When t, items which have no explicit
1599 time like 15:30 will be considered as 99:01, i.e. later than any items which
1600 do have a time. When nil, the default time is before 0:00. You can use this
1601 option to decide if the schedule for today should come before or after timeless
1602 agenda entries."
1603 :group 'org-agenda-sorting
1604 :type 'boolean)
1606 (defcustom org-sort-agenda-noeffort-is-high t
1607 "Non-nil means items without effort estimate are sorted as high effort.
1608 This also applies when filtering an agenda view with respect to the
1609 < or > effort operator. Then, tasks with no effort defined will be treated
1610 as tasks with high effort.
1611 When nil, such items are sorted as 0 minutes effort."
1612 :group 'org-agenda-sorting
1613 :type 'boolean)
1615 (defgroup org-agenda-line-format nil
1616 "Options concerning the entry prefix in the Org agenda display."
1617 :tag "Org Agenda Line Format"
1618 :group 'org-agenda)
1620 (defcustom org-agenda-prefix-format
1621 '((agenda . " %i %-12:c%?-12t% s")
1622 (timeline . " % s")
1623 (todo . " %i %-12:c")
1624 (tags . " %i %-12:c")
1625 (search . " %i %-12:c"))
1626 "Format specifications for the prefix of items in the agenda views.
1627 An alist with five entries, each for the different agenda types. The
1628 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1629 The values are format strings.
1631 This format works similar to a printf format, with the following meaning:
1633 %c the category of the item, \"Diary\" for entries from the diary,
1634 or as given by the CATEGORY keyword or derived from the file name
1635 %e the effort required by the item
1636 %l the level of the item (insert X space(s) if item is of level X)
1637 %i the icon category of the item, see `org-agenda-category-icon-alist'
1638 %T the last tag of the item (ignore inherited tags, which come first)
1639 %t the HH:MM time-of-day specification if one applies to the entry
1640 %s Scheduling/Deadline information, a short string
1641 %b show breadcrumbs, i.e., the names of the higher levels
1642 %(expression) Eval EXPRESSION and replace the control string
1643 by the result
1645 All specifiers work basically like the standard `%s' of printf, but may
1646 contain two additional characters: a question mark just after the `%'
1647 and a whitespace/punctuation character just before the final letter.
1649 If the first character after `%' is a question mark, the entire field
1650 will only be included if the corresponding value applies to the current
1651 entry. This is useful for fields which should have fixed width when
1652 present, but zero width when absent. For example, \"%?-12t\" will
1653 result in a 12 character time field if a time of the day is specified,
1654 but will completely disappear in entries which do not contain a time.
1656 If there is punctuation or whitespace character just before the
1657 final format letter, this character will be appended to the field
1658 value if the value is not empty. For example, the format
1659 \"%-12:c\" leads to \"Diary: \" if the category is \"Diary\". If
1660 the category is empty, no additional colon is inserted.
1662 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1663 which means:
1665 - Indent the line with two space characters
1666 - Give the category a 12 chars wide field, padded with whitespace on
1667 the right (because of `-'). Append a colon if there is a category
1668 (because of `:').
1669 - If there is a time-of-day, put it into a 12 chars wide field. If no
1670 time, don't put in an empty field, just skip it (because of '?').
1671 - Finally, put the scheduling information.
1673 See also the variables `org-agenda-remove-times-when-in-prefix' and
1674 `org-agenda-remove-tags'.
1676 Custom commands can set this variable in the options section."
1677 :type '(choice
1678 (string :tag "General format")
1679 (list :greedy t :tag "View dependent"
1680 (cons (const agenda) (string :tag "Format"))
1681 (cons (const timeline) (string :tag "Format"))
1682 (cons (const todo) (string :tag "Format"))
1683 (cons (const tags) (string :tag "Format"))
1684 (cons (const search) (string :tag "Format"))))
1685 :group 'org-agenda-line-format)
1687 (defvar org-prefix-format-compiled nil
1688 "The compiled prefix format and associated variables.
1689 This is a list where first element is a list of variable bindings, and second
1690 element is the compiled format expression. See the variable
1691 `org-agenda-prefix-format'.")
1693 (defcustom org-agenda-todo-keyword-format "%-1s"
1694 "Format for the TODO keyword in agenda lines.
1695 Set this to something like \"%-12s\" if you want all TODO keywords
1696 to occupy a fixed space in the agenda display."
1697 :group 'org-agenda-line-format
1698 :type 'string)
1700 (defcustom org-agenda-diary-sexp-prefix nil
1701 "A regexp that matches part of a diary sexp entry
1702 which should be treated as scheduling/deadline information in
1703 `org-agenda'.
1705 For example, you can use this to extract the `diary-remind-message' from
1706 `diary-remind' entries."
1707 :group 'org-agenda-line-format
1708 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1710 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1711 "Text preceding timerange entries in the agenda view.
1712 This is a list with two strings. The first applies when the range
1713 is entirely on one day. The second applies if the range spans several days.
1714 The strings may have two \"%d\" format specifiers which will be filled
1715 with the sequence number of the days, and the total number of days in the
1716 range, respectively."
1717 :group 'org-agenda-line-format
1718 :type '(list
1719 (string :tag "Deadline today ")
1720 (choice :tag "Deadline relative"
1721 (string :tag "Format string")
1722 (function))))
1724 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1725 "Text preceding scheduled items in the agenda view.
1726 This is a list with two strings. The first applies when the item is
1727 scheduled on the current day. The second applies when it has been scheduled
1728 previously, it may contain a %d indicating that this is the nth time that
1729 this item is scheduled, due to automatic rescheduling of unfinished items
1730 for the following day. So this number is one larger than the number of days
1731 that passed since this item was scheduled first."
1732 :group 'org-agenda-line-format
1733 :version "24.4"
1734 :package-version '(Org . "8.0")
1735 :type '(list
1736 (string :tag "Scheduled today ")
1737 (string :tag "Scheduled previously")))
1739 (defcustom org-agenda-inactive-leader "["
1740 "Text preceding item pulled into the agenda by inactive time stamps.
1741 These entries are added to the agenda when pressing \"[\"."
1742 :group 'org-agenda-line-format
1743 :version "24.1"
1744 :type 'string)
1746 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: " "%2d d. ago: ")
1747 "Text preceding deadline items in the agenda view.
1748 This is a list with three strings. The first applies when the item has its
1749 deadline on the current day. The second applies when the deadline is in the
1750 future, the third one when it is in the past. The strings may contain %d
1751 to capture the number of days."
1752 :group 'org-agenda-line-format
1753 :version "24.4"
1754 :package-version '(Org . "8.0")
1755 :type '(list
1756 (string :tag "Deadline today ")
1757 (string :tag "Deadline in the future ")
1758 (string :tag "Deadline in the past ")))
1760 (defcustom org-agenda-remove-times-when-in-prefix t
1761 "Non-nil means remove duplicate time specifications in agenda items.
1762 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1763 time-of-day specification in a headline or diary entry is extracted and
1764 placed into the prefix. If this option is non-nil, the original specification
1765 \(a timestamp or -range, or just a plain time(range) specification like
1766 11:30-4pm) will be removed for agenda display. This makes the agenda less
1767 cluttered.
1768 The option can be t or nil. It may also be the symbol `beg', indicating
1769 that the time should only be removed when it is located at the beginning of
1770 the headline/diary entry."
1771 :group 'org-agenda-line-format
1772 :type '(choice
1773 (const :tag "Always" t)
1774 (const :tag "Never" nil)
1775 (const :tag "When at beginning of entry" beg)))
1777 (defcustom org-agenda-remove-timeranges-from-blocks nil
1778 "Non-nil means remove time ranges specifications in agenda
1779 items that span on several days."
1780 :group 'org-agenda-line-format
1781 :version "24.1"
1782 :type 'boolean)
1784 (defcustom org-agenda-default-appointment-duration nil
1785 "Default duration for appointments that only have a starting time.
1786 When nil, no duration is specified in such cases.
1787 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1788 :group 'org-agenda-line-format
1789 :type '(choice
1790 (integer :tag "Minutes")
1791 (const :tag "No default duration")))
1793 (defcustom org-agenda-show-inherited-tags t
1794 "Non-nil means show inherited tags in each agenda line.
1796 When this option is set to `always', it take precedences over
1797 `org-agenda-use-tag-inheritance' and inherited tags are shown
1798 in every agenda.
1800 When this option is set to t (the default), inherited tags are
1801 shown when they are available, i.e. when the value of
1802 `org-agenda-use-tag-inheritance' enables tag inheritance for the
1803 given agenda type.
1805 This can be set to a list of agenda types in which the agenda
1806 must display the inherited tags. Available types are `todo',
1807 `agenda', `search' and `timeline'.
1809 When set to nil, never show inherited tags in agenda lines."
1810 :group 'org-agenda-line-format
1811 :group 'org-agenda
1812 :version "24.3"
1813 :type '(choice
1814 (const :tag "Show inherited tags when available" t)
1815 (const :tag "Always show inherited tags" always)
1816 (repeat :tag "Show inherited tags only in selected agenda types"
1817 (symbol :tag "Agenda type"))))
1819 (defcustom org-agenda-use-tag-inheritance '(todo search timeline agenda)
1820 "List of agenda view types where to use tag inheritance.
1822 In tags/tags-todo/tags-tree agenda views, tag inheritance is
1823 controlled by `org-use-tag-inheritance'. In other agenda types,
1824 `org-use-tag-inheritance' is not used for the selection of the
1825 agenda entries. Still, you may want the agenda to be aware of
1826 the inherited tags anyway, e.g. for later tag filtering.
1828 Allowed value are `todo', `search', `timeline' and `agenda'.
1830 This variable has no effect if `org-agenda-show-inherited-tags'
1831 is set to `always'. In that case, the agenda is aware of those
1832 tags.
1834 The default value sets tags in every agenda type. Setting this
1835 option to nil will speed up non-tags agenda view a lot."
1836 :group 'org-agenda
1837 :version "24.3"
1838 :type '(choice
1839 (const :tag "Use tag inheritance in all agenda types" t)
1840 (repeat :tag "Use tag inheritance in selected agenda types"
1841 (symbol :tag "Agenda type"))))
1843 (defcustom org-agenda-hide-tags-regexp nil
1844 "Regular expression used to filter away specific tags in agenda views.
1845 This means that these tags will be present, but not be shown in the agenda
1846 line. Secondary filtering will still work on the hidden tags.
1847 Nil means don't hide any tags."
1848 :group 'org-agenda-line-format
1849 :type '(choice
1850 (const :tag "Hide none" nil)
1851 (string :tag "Regexp ")))
1853 (defcustom org-agenda-remove-tags nil
1854 "Non-nil means remove the tags from the headline copy in the agenda.
1855 When this is the symbol `prefix', only remove tags when
1856 `org-agenda-prefix-format' contains a `%T' specifier."
1857 :group 'org-agenda-line-format
1858 :type '(choice
1859 (const :tag "Always" t)
1860 (const :tag "Never" nil)
1861 (const :tag "When prefix format contains %T" prefix)))
1863 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1864 'org-agenda-remove-tags)
1866 (defcustom org-agenda-tags-column -80
1867 "Shift tags in agenda items to this column.
1868 If this number is positive, it specifies the column. If it is negative,
1869 it means that the tags should be flushright to that column. For example,
1870 -80 works well for a normal 80 character screen."
1871 :group 'org-agenda-line-format
1872 :type 'integer)
1874 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column)
1876 (defcustom org-agenda-fontify-priorities 'cookies
1877 "Non-nil means highlight low and high priorities in agenda.
1878 When t, the highest priority entries are bold, lowest priority italic.
1879 However, settings in `org-priority-faces' will overrule these faces.
1880 When this variable is the symbol `cookies', only fontify the
1881 cookies, not the entire task.
1882 This may also be an association list of priority faces, whose
1883 keys are the character values of `org-highest-priority',
1884 `org-default-priority', and `org-lowest-priority' (the default values
1885 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1886 color as a string, or a list like `(:background \"Red\")'.
1887 If it is a color, the variable `org-faces-easy-properties'
1888 determines if it is a foreground or a background color."
1889 :group 'org-agenda-line-format
1890 :type '(choice
1891 (const :tag "Never" nil)
1892 (const :tag "Defaults" t)
1893 (const :tag "Cookies only" cookies)
1894 (repeat :tag "Specify"
1895 (list (character :tag "Priority" :value ?A)
1896 (choice :tag "Face "
1897 (string :tag "Color")
1898 (sexp :tag "Face"))))))
1900 (defcustom org-agenda-day-face-function nil
1901 "Function called to determine what face should be used to display a day.
1902 The only argument passed to that function is the day. It should
1903 returns a face, or nil if does not want to specify a face and let
1904 the normal rules apply."
1905 :group 'org-agenda-line-format
1906 :version "24.1"
1907 :type '(choice (const nil) (function)))
1909 (defcustom org-agenda-category-icon-alist nil
1910 "Alist of category icon to be displayed in agenda views.
1912 Each entry should have the following format:
1914 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1916 Where CATEGORY-REGEXP is a regexp matching the categories where
1917 the icon should be displayed.
1918 FILE-OR-DATA either a file path or a string containing image data.
1920 The other fields can be omitted safely if not needed:
1921 TYPE indicates the image type.
1922 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1923 image data.
1924 PROPS are additional image attributes to assign to the image,
1925 like, e.g. `:ascent center'.
1927 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1929 If you want to set the display properties yourself, just put a
1930 list as second element:
1932 (CATEGORY-REGEXP (MY PROPERTY LIST))
1934 For example, to display a 16px horizontal space for Emacs
1935 category, you can use:
1937 (\"Emacs\" \\='(space . (:width (16))))"
1938 :group 'org-agenda-line-format
1939 :version "24.1"
1940 :type '(alist :key-type (string :tag "Regexp matching category")
1941 :value-type (choice (list :tag "Icon"
1942 (string :tag "File or data")
1943 (symbol :tag "Type")
1944 (boolean :tag "Data?")
1945 (repeat :tag "Extra image properties" :inline t symbol))
1946 (list :tag "Display properties" sexp))))
1948 (defgroup org-agenda-column-view nil
1949 "Options concerning column view in the agenda."
1950 :tag "Org Agenda Column View"
1951 :group 'org-agenda)
1953 (defcustom org-agenda-view-columns-initially nil
1954 "When non-nil, switch to columns view right after creating the agenda."
1955 :group 'org-agenda-column-view
1956 :type 'boolean
1957 :version "26.1"
1958 :package-version '(Org . "9.0")
1959 :safe #'booleanp)
1961 (defcustom org-agenda-columns-show-summaries t
1962 "Non-nil means show summaries for columns displayed in the agenda view."
1963 :group 'org-agenda-column-view
1964 :type 'boolean)
1966 (defcustom org-agenda-columns-compute-summary-properties t
1967 "Non-nil means recompute all summary properties before column view.
1968 When column view in the agenda is listing properties that have a summary
1969 operator, it can go to all relevant buffers and recompute the summaries
1970 there. This can mean overhead for the agenda column view, but is necessary
1971 to have thing up to date.
1972 As a special case, a CLOCKSUM property also makes sure that the clock
1973 computations are current."
1974 :group 'org-agenda-column-view
1975 :type 'boolean)
1977 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1978 "Non-nil means the duration of an appointment will add to day effort.
1979 The property to which appointment durations will be added is the one given
1980 in the option `org-effort-property'. If an appointment does not have
1981 an end time, `org-agenda-default-appointment-duration' will be used. If that
1982 is not set, an appointment without end time will not contribute to the time
1983 estimate."
1984 :group 'org-agenda-column-view
1985 :type 'boolean)
1987 (defcustom org-agenda-auto-exclude-function nil
1988 "A function called with a tag to decide if it is filtered on \
1989 \\<org-agenda-mode-map>`\\[org-agenda-filter-by-tag] RET'.
1990 The sole argument to the function, which is called once for each
1991 possible tag, is a string giving the name of the tag. The
1992 function should return either nil if the tag should be included
1993 as normal, or \"-<TAG>\" to exclude the tag.
1994 Note that for the purpose of tag filtering, only the lower-case version of
1995 all tags will be considered, so that this function will only ever see
1996 the lower-case version of all tags."
1997 :group 'org-agenda
1998 :type '(choice (const nil) (function)))
2000 (defcustom org-agenda-bulk-custom-functions nil
2001 "Alist of characters and custom functions for bulk actions.
2002 For example, this value makes those two functions available:
2004 \\='((?R set-category)
2005 (?C bulk-cut))
2007 With selected entries in an agenda buffer, `B R' will call
2008 the custom function `set-category' on the selected entries.
2009 Note that functions in this alist don't need to be quoted."
2010 :type '(alist :key-type character :value-type (group function))
2011 :version "24.1"
2012 :group 'org-agenda)
2014 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
2015 "Execute BODY with point at location given by `org-hd-marker' property.
2016 If STRING is non-nil, the text property will be fetched from position 0
2017 in that string. If STRING is nil, it will be fetched from the beginning
2018 of the current line."
2019 (org-with-gensyms (marker)
2020 `(let ((,marker (get-text-property (if ,string 0 (point-at-bol))
2021 'org-hd-marker ,string)))
2022 (with-current-buffer (marker-buffer ,marker)
2023 (save-excursion
2024 (goto-char ,marker)
2025 ,@body)))))
2026 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
2028 (defun org-add-agenda-custom-command (entry)
2029 "Replace or add a command in `org-agenda-custom-commands'.
2030 This is mostly for hacking and trying a new command - once the command
2031 works you probably want to add it to `org-agenda-custom-commands' for good."
2032 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
2033 (if ass
2034 (setcdr ass (cdr entry))
2035 (push entry org-agenda-custom-commands))))
2037 ;;; Define the org-agenda-mode
2039 (defvar org-agenda-mode-map (make-sparse-keymap)
2040 "Keymap for `org-agenda-mode'.")
2041 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map)
2043 (defvar org-agenda-menu) ; defined later in this file.
2044 (defvar org-agenda-restrict nil) ; defined later in this file.
2045 (defvar org-agenda-follow-mode nil)
2046 (defvar org-agenda-entry-text-mode nil)
2047 (defvar org-agenda-clockreport-mode nil)
2048 (defvar org-agenda-show-log nil)
2049 (defvar org-agenda-redo-command nil)
2050 (defvar org-agenda-query-string nil)
2051 (defvar org-agenda-mode-hook nil
2052 "Hook run after `org-agenda-mode' is turned on.
2053 The buffer is still writable when this hook is called.")
2054 (defvar org-agenda-type nil)
2055 (defvar org-agenda-force-single-file nil)
2056 (defvar org-agenda-bulk-marked-entries nil
2057 "List of markers that refer to marked entries in the agenda.")
2058 (defvar org-agenda-current-date nil
2059 "Active date when building the agenda.")
2061 ;;; Multiple agenda buffers support
2063 (defcustom org-agenda-sticky nil
2064 "Non-nil means agenda q key will bury agenda buffers.
2065 Agenda commands will then show existing buffer instead of generating new ones.
2066 When nil, `q' will kill the single agenda buffer."
2067 :group 'org-agenda
2068 :version "24.3"
2069 :type 'boolean)
2072 ;;;###autoload
2073 (defun org-toggle-sticky-agenda (&optional arg)
2074 "Toggle `org-agenda-sticky'."
2075 (interactive "P")
2076 (let ((new-value (if arg
2077 (> (prefix-numeric-value arg) 0)
2078 (not org-agenda-sticky))))
2079 (if (equal new-value org-agenda-sticky)
2080 (and (called-interactively-p 'interactive)
2081 (message "Sticky agenda was already %s"
2082 (if org-agenda-sticky "enabled" "disabled")))
2083 (setq org-agenda-sticky new-value)
2084 (org-agenda-kill-all-agenda-buffers)
2085 (and (called-interactively-p 'interactive)
2086 (message "Sticky agenda %s"
2087 (if org-agenda-sticky "enabled" "disabled"))))))
2089 (defvar org-agenda-buffer nil
2090 "Agenda buffer currently being generated.")
2092 (defvar org-agenda-last-prefix-arg nil)
2093 (defvar org-agenda-this-buffer-name nil)
2094 (defvar org-agenda-doing-sticky-redo nil)
2095 (defvar org-agenda-this-buffer-is-sticky nil)
2096 (defvar org-agenda-last-indirect-buffer nil
2097 "Last buffer loaded by `org-agenda-tree-to-indirect-buffer'.")
2099 (defconst org-agenda-local-vars
2100 '(org-agenda-this-buffer-name
2101 org-agenda-undo-list
2102 org-agenda-pending-undo-list
2103 org-agenda-follow-mode
2104 org-agenda-entry-text-mode
2105 org-agenda-clockreport-mode
2106 org-agenda-show-log
2107 org-agenda-redo-command
2108 org-agenda-query-string
2109 org-agenda-type
2110 org-agenda-bulk-marked-entries
2111 org-agenda-undo-has-started-in
2112 org-agenda-info
2113 org-agenda-pre-window-conf
2114 org-agenda-columns-active
2115 org-agenda-tag-filter
2116 org-agenda-category-filter
2117 org-agenda-top-headline-filter
2118 org-agenda-regexp-filter
2119 org-agenda-effort-filter
2120 org-agenda-markers
2121 org-agenda-last-search-view-search-was-boolean
2122 org-agenda-last-indirect-buffer
2123 org-agenda-filtered-by-category
2124 org-agenda-filter-form
2125 org-agenda-cycle-counter
2126 org-agenda-last-prefix-arg)
2127 "Variables that must be local in agenda buffers to allow multiple buffers.")
2129 (defun org-agenda-mode ()
2130 "Mode for time-sorted view on action items in Org files.
2132 The following commands are available:
2134 \\{org-agenda-mode-map}"
2135 (interactive)
2136 (cond (org-agenda-doing-sticky-redo
2137 ;; Refreshing sticky agenda-buffer
2139 ;; Preserve the value of `org-agenda-local-vars' variables,
2140 ;; while letting `kill-all-local-variables' kill the rest
2141 (let ((save (buffer-local-variables)))
2142 (kill-all-local-variables)
2143 (mapc 'make-local-variable org-agenda-local-vars)
2144 (dolist (elem save)
2145 (let ((var (car elem))
2146 (val (cdr elem)))
2147 (when (and val
2148 (member var org-agenda-local-vars))
2149 (set var val)))))
2150 (setq-local org-agenda-this-buffer-is-sticky t))
2151 (org-agenda-sticky
2152 ;; Creating a sticky Agenda buffer for the first time
2153 (kill-all-local-variables)
2154 (mapc 'make-local-variable org-agenda-local-vars)
2155 (setq-local org-agenda-this-buffer-is-sticky t))
2157 ;; Creating a non-sticky agenda buffer
2158 (kill-all-local-variables)
2159 (setq-local org-agenda-this-buffer-is-sticky nil)))
2160 (setq org-agenda-undo-list nil
2161 org-agenda-pending-undo-list nil
2162 org-agenda-bulk-marked-entries nil)
2163 (setq major-mode 'org-agenda-mode)
2164 ;; Keep global-font-lock-mode from turning on font-lock-mode
2165 (setq-local font-lock-global-modes (list 'not major-mode))
2166 (setq mode-name "Org-Agenda")
2167 (setq indent-tabs-mode nil)
2168 (use-local-map org-agenda-mode-map)
2169 (easy-menu-add org-agenda-menu)
2170 (if org-startup-truncated (setq truncate-lines t))
2171 (setq-local line-move-visual nil)
2172 (add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
2173 (add-hook 'pre-command-hook 'org-unhighlight nil 'local)
2174 ;; Make sure properties are removed when copying text
2175 (add-hook 'filter-buffer-substring-functions
2176 (lambda (fun start end delete)
2177 (substring-no-properties (funcall fun start end delete)))
2178 nil t)
2179 (unless org-agenda-keep-modes
2180 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
2181 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode))
2182 (setq org-agenda-show-log org-agenda-start-with-log-mode)
2183 (setq org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode)
2184 (add-to-invisibility-spec '(org-filtered))
2185 (add-to-invisibility-spec '(org-link))
2186 (easy-menu-change
2187 '("Agenda") "Agenda Files"
2188 (append
2189 (list
2190 (vector
2191 (if (get 'org-agenda-files 'org-restrict)
2192 "Restricted to single file"
2193 "Edit File List")
2194 '(org-edit-agenda-file-list)
2195 (not (get 'org-agenda-files 'org-restrict)))
2196 "--")
2197 (mapcar 'org-file-menu-entry (org-agenda-files))))
2198 (org-agenda-set-mode-name)
2199 (apply
2200 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2201 (list 'org-agenda-mode-hook)))
2203 (substitute-key-definition 'undo 'org-agenda-undo
2204 org-agenda-mode-map global-map)
2205 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2206 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2207 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2208 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2209 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2210 (org-defkey org-agenda-mode-map [(meta down)] 'org-agenda-drag-line-forward)
2211 (org-defkey org-agenda-mode-map [(meta up)] 'org-agenda-drag-line-backward)
2212 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2213 (org-defkey org-agenda-mode-map "\M-m" 'org-agenda-bulk-toggle)
2214 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2215 (org-defkey org-agenda-mode-map "\M-*" 'org-agenda-bulk-toggle-all)
2216 (org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
2217 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2218 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2219 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2220 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2221 (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2222 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2223 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2224 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2225 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2226 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2227 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2228 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2229 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2230 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2231 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2232 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2233 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2234 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2235 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2236 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2237 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2238 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2239 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2240 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2241 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2242 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2243 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2244 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2245 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2246 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2247 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2248 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2249 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2250 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2251 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2252 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2253 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2254 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2256 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2257 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2258 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2259 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2260 (while l (org-defkey org-agenda-mode-map
2261 (int-to-string (pop l)) 'digit-argument)))
2263 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2264 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2265 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2266 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2267 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2268 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2269 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2270 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2271 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2272 (org-defkey org-agenda-mode-map "g" (lambda () (interactive) (org-agenda-redo t)))
2273 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2274 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2275 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2276 'org-clock-modify-effort-estimate)
2277 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2278 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2279 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2280 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2281 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2282 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2283 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2284 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2285 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2286 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2287 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2288 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2289 (substitute-key-definition 'next-line 'org-agenda-next-line
2290 org-agenda-mode-map global-map)
2291 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2292 org-agenda-mode-map global-map)
2293 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2294 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2295 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2296 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2297 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2298 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2299 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2300 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2301 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2302 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2303 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2304 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2305 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2306 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2307 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2308 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2309 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2310 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2311 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2312 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2313 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2314 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2315 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2316 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2317 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2318 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2319 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2320 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2321 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2322 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2323 (org-defkey org-agenda-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock-from-agenda)
2325 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2326 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2327 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2328 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2329 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2330 (org-defkey org-agenda-mode-map "_" 'org-agenda-filter-by-effort)
2331 (org-defkey org-agenda-mode-map "=" 'org-agenda-filter-by-regexp)
2332 (org-defkey org-agenda-mode-map "|" 'org-agenda-filter-remove-all)
2333 (org-defkey org-agenda-mode-map "~" 'org-agenda-limit-interactively)
2334 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2335 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-headline)
2336 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2337 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2338 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2339 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2341 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2342 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2344 (define-key org-agenda-mode-map [remap forward-paragraph] 'org-agenda-forward-block)
2345 (define-key org-agenda-mode-map [remap backward-paragraph] 'org-agenda-backward-block)
2347 (when org-agenda-mouse-1-follows-link
2348 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2349 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2350 '("Agenda"
2351 ("Agenda Files")
2352 "--"
2353 ("Agenda Dates"
2354 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2355 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2356 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2357 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2358 "--"
2359 ("View"
2360 ["Day View" org-agenda-day-view
2361 :active (org-agenda-check-type nil 'agenda)
2362 :style radio :selected (eq org-agenda-current-span 'day)
2363 :keys "v d (or just d)"]
2364 ["Week View" org-agenda-week-view
2365 :active (org-agenda-check-type nil 'agenda)
2366 :style radio :selected (eq org-agenda-current-span 'week)
2367 :keys "v w"]
2368 ["Fortnight View" org-agenda-fortnight-view
2369 :active (org-agenda-check-type nil 'agenda)
2370 :style radio :selected (eq org-agenda-current-span 'fortnight)
2371 :keys "v f"]
2372 ["Month View" org-agenda-month-view
2373 :active (org-agenda-check-type nil 'agenda)
2374 :style radio :selected (eq org-agenda-current-span 'month)
2375 :keys "v m"]
2376 ["Year View" org-agenda-year-view
2377 :active (org-agenda-check-type nil 'agenda)
2378 :style radio :selected (eq org-agenda-current-span 'year)
2379 :keys "v y"]
2380 "--"
2381 ["Include Diary" org-agenda-toggle-diary
2382 :style toggle :selected org-agenda-include-diary
2383 :active (org-agenda-check-type nil 'agenda)]
2384 ["Include Deadlines" org-agenda-toggle-deadlines
2385 :style toggle :selected org-agenda-include-deadlines
2386 :active (org-agenda-check-type nil 'agenda)]
2387 ["Use Time Grid" org-agenda-toggle-time-grid
2388 :style toggle :selected org-agenda-use-time-grid
2389 :active (org-agenda-check-type nil 'agenda)]
2390 "--"
2391 ["Show clock report" org-agenda-clockreport-mode
2392 :style toggle :selected org-agenda-clockreport-mode
2393 :active (org-agenda-check-type nil 'agenda)]
2394 ["Show some entry text" org-agenda-entry-text-mode
2395 :style toggle :selected org-agenda-entry-text-mode
2396 :active t]
2397 "--"
2398 ["Show Logbook entries" org-agenda-log-mode
2399 :style toggle :selected org-agenda-show-log
2400 :active (org-agenda-check-type nil 'agenda 'timeline)
2401 :keys "v l (or just l)"]
2402 ["Include archived trees" org-agenda-archives-mode
2403 :style toggle :selected org-agenda-archives-mode :active t
2404 :keys "v a"]
2405 ["Include archive files" (org-agenda-archives-mode t)
2406 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2407 :keys "v A"]
2408 "--"
2409 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2410 ["Write view to file" org-agenda-write t]
2411 ["Rebuild buffer" org-agenda-redo t]
2412 ["Save all Org buffers" org-save-all-org-buffers t]
2413 "--"
2414 ["Show original entry" org-agenda-show t]
2415 ["Go To (other window)" org-agenda-goto t]
2416 ["Go To (this window)" org-agenda-switch-to t]
2417 ["Capture with cursor date" org-agenda-capture t]
2418 ["Follow Mode" org-agenda-follow-mode
2419 :style toggle :selected org-agenda-follow-mode :active t]
2420 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2421 "--"
2422 ("TODO"
2423 ["Cycle TODO" org-agenda-todo t]
2424 ["Next TODO set" org-agenda-todo-nextset t]
2425 ["Previous TODO set" org-agenda-todo-previousset t]
2426 ["Add note" org-agenda-add-note t])
2427 ("Archive/Refile/Delete"
2428 ["Archive default" org-agenda-archive-default t]
2429 ["Archive default" org-agenda-archive-default-with-confirmation t]
2430 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2431 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2432 ["Archive subtree" org-agenda-archive t]
2433 "--"
2434 ["Refile" org-agenda-refile t]
2435 "--"
2436 ["Delete subtree" org-agenda-kill t])
2437 ("Bulk action"
2438 ["Mark entry" org-agenda-bulk-mark t]
2439 ["Mark all" org-agenda-bulk-mark-all t]
2440 ["Unmark entry" org-agenda-bulk-unmark t]
2441 ["Unmark all" org-agenda-bulk-unmark-all :active t :keys "U"]
2442 ["Toggle mark" org-agenda-bulk-toggle t]
2443 ["Toggle all" org-agenda-bulk-toggle-all t]
2444 ["Mark regexp" org-agenda-bulk-mark-regexp t])
2445 ["Act on all marked" org-agenda-bulk-action t]
2446 "--"
2447 ("Tags and Properties"
2448 ["Show all Tags" org-agenda-show-tags t]
2449 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2450 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2451 "--"
2452 ["Column View" org-columns t])
2453 ("Deadline/Schedule"
2454 ["Schedule" org-agenda-schedule t]
2455 ["Set Deadline" org-agenda-deadline t]
2456 "--"
2457 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2458 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2459 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2460 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2461 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2462 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2463 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2464 ("Clock and Effort"
2465 ["Clock in" org-agenda-clock-in t]
2466 ["Clock out" org-agenda-clock-out t]
2467 ["Clock cancel" org-agenda-clock-cancel t]
2468 ["Goto running clock" org-clock-goto t]
2469 "--"
2470 ["Set Effort" org-agenda-set-effort t]
2471 ["Change clocked effort" org-clock-modify-effort-estimate
2472 (org-clock-is-active)])
2473 ("Priority"
2474 ["Set Priority" org-agenda-priority t]
2475 ["Increase Priority" org-agenda-priority-up t]
2476 ["Decrease Priority" org-agenda-priority-down t]
2477 ["Show Priority" org-show-priority t])
2478 ("Calendar/Diary"
2479 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2480 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2481 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2482 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2483 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2484 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2485 "--"
2486 ["Create iCalendar File" org-icalendar-combine-agenda-files t])
2487 "--"
2488 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2489 "--"
2490 ("MobileOrg"
2491 ["Push Files and Views" org-mobile-push t]
2492 ["Get Captured and Flagged" org-mobile-pull t]
2493 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2494 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2495 "--"
2496 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2497 "--"
2498 ["Quit" org-agenda-quit t]
2499 ["Exit and Release Buffers" org-agenda-exit t]
2502 ;;; Agenda undo
2504 (defvar org-agenda-allow-remote-undo t
2505 "Non-nil means allow remote undo from the agenda buffer.")
2506 (defvar org-agenda-undo-has-started-in nil
2507 "Buffers that have already seen `undo-start' in the current undo sequence.")
2509 (defun org-agenda-undo ()
2510 "Undo a remote editing step in the agenda.
2511 This undoes changes both in the agenda buffer and in the remote buffer
2512 that have been changed along."
2513 (interactive)
2514 (or org-agenda-allow-remote-undo
2515 (user-error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2516 (if (not (eq this-command last-command))
2517 (setq org-agenda-undo-has-started-in nil
2518 org-agenda-pending-undo-list org-agenda-undo-list))
2519 (if (not org-agenda-pending-undo-list)
2520 (user-error "No further undo information"))
2521 (let* ((entry (pop org-agenda-pending-undo-list))
2522 buf line cmd rembuf)
2523 (setq cmd (pop entry) line (pop entry))
2524 (setq rembuf (nth 2 entry))
2525 (org-with-remote-undo rembuf
2526 (while (bufferp (setq buf (pop entry)))
2527 (if (pop entry)
2528 (with-current-buffer buf
2529 (let ((last-undo-buffer buf)
2530 (inhibit-read-only t))
2531 (unless (memq buf org-agenda-undo-has-started-in)
2532 (push buf org-agenda-undo-has-started-in)
2533 (make-local-variable 'pending-undo-list)
2534 (undo-start))
2535 (while (and pending-undo-list
2536 (listp pending-undo-list)
2537 (not (car pending-undo-list)))
2538 (pop pending-undo-list))
2539 (undo-more 1))))))
2540 (org-goto-line line)
2541 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2543 (defun org-verify-change-for-undo (l1 l2)
2544 "Verify that a real change occurred between the undo lists L1 and L2."
2545 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2546 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2547 (not (eq l1 l2)))
2549 ;;; Agenda dispatch
2551 (defvar org-agenda-restrict-begin (make-marker))
2552 (defvar org-agenda-restrict-end (make-marker))
2553 (defvar org-agenda-last-dispatch-buffer nil)
2554 (defvar org-agenda-overriding-restriction nil)
2556 (defcustom org-agenda-custom-commands-contexts nil
2557 "Alist of custom agenda keys and contextual rules.
2559 For example, if you have a custom agenda command \"p\" and you
2560 want this command to be accessible only from plain text files,
2561 use this:
2563 \\='((\"p\" ((in-file . \"\\\\.txt\\\\'\"))))
2565 Here are the available contexts definitions:
2567 in-file: command displayed only in matching files
2568 in-mode: command displayed only in matching modes
2569 not-in-file: command not displayed in matching files
2570 not-in-mode: command not displayed in matching modes
2571 in-buffer: command displayed only in matching buffers
2572 not-in-buffer: command not displayed in matching buffers
2573 [function]: a custom function taking no argument
2575 If you define several checks, the agenda command will be
2576 accessible if there is at least one valid check.
2578 You can also bind a key to another agenda custom command
2579 depending on contextual rules.
2581 \\='((\"p\" \"q\" ((in-file . \"\\\\.txt\\\\'\"))))
2583 Here it means: in .txt files, use \"p\" as the key for the
2584 agenda command otherwise associated with \"q\". (The command
2585 originally associated with \"q\" is not displayed to avoid
2586 duplicates.)"
2587 :version "24.3"
2588 :group 'org-agenda-custom-commands
2589 :type '(repeat (list :tag "Rule"
2590 (string :tag " Agenda key")
2591 (string :tag "Replace by command")
2592 (repeat :tag "Available when"
2593 (choice
2594 (cons :tag "Condition"
2595 (choice
2596 (const :tag "In file" in-file)
2597 (const :tag "Not in file" not-in-file)
2598 (const :tag "In buffer" in-buffer)
2599 (const :tag "Not in buffer" not-in-buffer)
2600 (const :tag "In mode" in-mode)
2601 (const :tag "Not in mode" not-in-mode))
2602 (regexp))
2603 (function :tag "Custom function"))))))
2605 (defcustom org-agenda-max-entries nil
2606 "Maximum number of entries to display in an agenda.
2607 This can be nil (no limit) or an integer or an alist of agenda
2608 types with an associated number of entries to display in this
2609 type."
2610 :version "24.4"
2611 :package-version '(Org . "8.0")
2612 :group 'org-agenda-custom-commands
2613 :type '(choice (symbol :tag "No limit" nil)
2614 (integer :tag "Max number of entries")
2615 (repeat
2616 (cons (choice :tag "Agenda type"
2617 (const agenda)
2618 (const todo)
2619 (const tags)
2620 (const search)
2621 (const timeline))
2622 (integer :tag "Max number of entries")))))
2624 (defcustom org-agenda-max-todos nil
2625 "Maximum number of TODOs to display in an agenda.
2626 This can be nil (no limit) or an integer or an alist of agenda
2627 types with an associated number of entries to display in this
2628 type."
2629 :version "24.4"
2630 :package-version '(Org . "8.0")
2631 :group 'org-agenda-custom-commands
2632 :type '(choice (symbol :tag "No limit" nil)
2633 (integer :tag "Max number of TODOs")
2634 (repeat
2635 (cons (choice :tag "Agenda type"
2636 (const agenda)
2637 (const todo)
2638 (const tags)
2639 (const search)
2640 (const timeline))
2641 (integer :tag "Max number of TODOs")))))
2643 (defcustom org-agenda-max-tags nil
2644 "Maximum number of tagged entries to display in an agenda.
2645 This can be nil (no limit) or an integer or an alist of agenda
2646 types with an associated number of entries to display in this
2647 type."
2648 :version "24.4"
2649 :package-version '(Org . "8.0")
2650 :group 'org-agenda-custom-commands
2651 :type '(choice (symbol :tag "No limit" nil)
2652 (integer :tag "Max number of tagged entries")
2653 (repeat
2654 (cons (choice :tag "Agenda type"
2655 (const agenda)
2656 (const todo)
2657 (const tags)
2658 (const search)
2659 (const timeline))
2660 (integer :tag "Max number of tagged entries")))))
2662 (defcustom org-agenda-max-effort nil
2663 "Maximum cumulated effort duration for the agenda.
2664 This can be nil (no limit) or a number of minutes (as an integer)
2665 or an alist of agenda types with an associated number of minutes
2666 to limit entries to in this type."
2667 :version "24.4"
2668 :package-version '(Org . "8.0")
2669 :group 'org-agenda-custom-commands
2670 :type '(choice (symbol :tag "No limit" nil)
2671 (integer :tag "Max number of minutes")
2672 (repeat
2673 (cons (choice :tag "Agenda type"
2674 (const agenda)
2675 (const todo)
2676 (const tags)
2677 (const search)
2678 (const timeline))
2679 (integer :tag "Max number of minutes")))))
2681 (defvar org-agenda-keep-restricted-file-list nil)
2682 (defvar org-keys nil)
2683 (defvar org-match nil)
2684 ;;;###autoload
2685 (defun org-agenda (&optional arg org-keys restriction)
2686 "Dispatch agenda commands to collect entries to the agenda buffer.
2687 Prompts for a command to execute. Any prefix arg will be passed
2688 on to the selected command. The default selections are:
2690 a Call `org-agenda-list' to display the agenda for current day or week.
2691 t Call `org-todo-list' to display the global todo list.
2692 T Call `org-todo-list' to display the global todo list, select only
2693 entries with a specific TODO keyword (the user gets a prompt).
2694 m Call `org-tags-view' to display headlines with tags matching
2695 a condition (the user is prompted for the condition).
2696 M Like `m', but select only TODO entries, no ordinary headlines.
2697 e Export views to associated files.
2698 s Search entries for keywords.
2699 S Search entries for keywords, only with TODO keywords.
2700 / Multi occur across all agenda files and also files listed
2701 in `org-agenda-text-search-extra-files'.
2702 < Restrict agenda commands to buffer, subtree, or region.
2703 Press several times to get the desired effect.
2704 > Remove a previous restriction.
2705 # List \"stuck\" projects.
2706 ! Configure what \"stuck\" means.
2707 C Configure custom agenda commands.
2709 More commands can be added by configuring the variable
2710 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2711 searches can be pre-defined in this way.
2713 If the current buffer is in Org mode and visiting a file, you can also
2714 first press `<' once to indicate that the agenda should be temporarily
2715 \(until the next use of `\\[org-agenda]') restricted to the current file.
2716 Pressing `<' twice means to restrict to the current subtree or region
2717 \(if active)."
2718 (interactive "P")
2719 (catch 'exit
2720 (let* ((prefix-descriptions nil)
2721 (org-agenda-buffer-name org-agenda-buffer-name)
2722 (org-agenda-window-setup (if (equal (buffer-name)
2723 org-agenda-buffer-name)
2724 'current-window
2725 org-agenda-window-setup))
2726 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2727 (org-agenda-custom-commands
2728 ;; normalize different versions
2729 (delq nil
2730 (mapcar
2731 (lambda (x)
2732 (cond ((stringp (cdr x))
2733 (push x prefix-descriptions)
2734 nil)
2735 ((stringp (nth 1 x)) x)
2736 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2737 (t (cons (car x) (cons "" (cdr x))))))
2738 org-agenda-custom-commands)))
2739 (org-agenda-custom-commands
2740 (org-contextualize-keys
2741 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2742 (buf (current-buffer))
2743 (bfn (buffer-file-name (buffer-base-buffer)))
2744 entry key type org-match lprops ans)
2745 ;; Turn off restriction unless there is an overriding one,
2746 (unless org-agenda-overriding-restriction
2747 (unless org-agenda-keep-restricted-file-list
2748 ;; There is a request to keep the file list in place
2749 (put 'org-agenda-files 'org-restrict nil))
2750 (setq org-agenda-restrict nil)
2751 (move-marker org-agenda-restrict-begin nil)
2752 (move-marker org-agenda-restrict-end nil))
2753 ;; Delete old local properties
2754 (put 'org-agenda-redo-command 'org-lprops nil)
2755 ;; Delete previously set last-arguments
2756 (put 'org-agenda-redo-command 'last-args nil)
2757 ;; Remember where this call originated
2758 (setq org-agenda-last-dispatch-buffer (current-buffer))
2759 (unless org-keys
2760 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2761 org-keys (car ans)
2762 restriction (cdr ans)))
2763 ;; If we have sticky agenda buffers, set a name for the buffer,
2764 ;; depending on the invoking keys. The user may still set this
2765 ;; as a command option, which will overwrite what we do here.
2766 (if org-agenda-sticky
2767 (setq org-agenda-buffer-name
2768 (format "*Org Agenda(%s)*" org-keys)))
2769 ;; Establish the restriction, if any
2770 (when (and (not org-agenda-overriding-restriction) restriction)
2771 (put 'org-agenda-files 'org-restrict (list bfn))
2772 (cond
2773 ((eq restriction 'region)
2774 (setq org-agenda-restrict (current-buffer))
2775 (move-marker org-agenda-restrict-begin (region-beginning))
2776 (move-marker org-agenda-restrict-end (region-end)))
2777 ((eq restriction 'subtree)
2778 (save-excursion
2779 (setq org-agenda-restrict (current-buffer))
2780 (org-back-to-heading t)
2781 (move-marker org-agenda-restrict-begin (point))
2782 (move-marker org-agenda-restrict-end
2783 (progn (org-end-of-subtree t)))))))
2785 ;; For example the todo list should not need it (but does...)
2786 (cond
2787 ((setq entry (assoc org-keys org-agenda-custom-commands))
2788 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2789 (progn
2790 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
2791 lprops (nth 4 entry))
2792 (if org-agenda-sticky
2793 (setq org-agenda-buffer-name
2794 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2795 (format "*Org Agenda(%s)*" org-keys))))
2796 (put 'org-agenda-redo-command 'org-lprops lprops)
2797 (cond
2798 ((eq type 'agenda)
2799 (org-let lprops '(org-agenda-list current-prefix-arg)))
2800 ((eq type 'agenda*)
2801 (org-let lprops '(org-agenda-list current-prefix-arg nil nil t)))
2802 ((eq type 'alltodo)
2803 (org-let lprops '(org-todo-list current-prefix-arg)))
2804 ((eq type 'search)
2805 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
2806 ((eq type 'stuck)
2807 (org-let lprops '(org-agenda-list-stuck-projects
2808 current-prefix-arg)))
2809 ((eq type 'tags)
2810 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2811 ((eq type 'tags-todo)
2812 (org-let lprops '(org-tags-view '(4) org-match)))
2813 ((eq type 'todo)
2814 (org-let lprops '(org-todo-list org-match)))
2815 ((eq type 'tags-tree)
2816 (org-check-for-org-mode)
2817 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
2818 ((eq type 'todo-tree)
2819 (org-check-for-org-mode)
2820 (org-let lprops
2821 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2822 (regexp-quote org-match) "\\>"))))
2823 ((eq type 'occur-tree)
2824 (org-check-for-org-mode)
2825 (org-let lprops '(org-occur org-match)))
2826 ((functionp type)
2827 (org-let lprops '(funcall type org-match)))
2828 ((fboundp type)
2829 (org-let lprops '(funcall type org-match)))
2830 (t (user-error "Invalid custom agenda command type %s" type))))
2831 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2832 ((equal org-keys "C")
2833 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2834 (customize-variable 'org-agenda-custom-commands))
2835 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2836 ((equal org-keys "s") (call-interactively 'org-search-view))
2837 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2838 ((equal org-keys "t") (call-interactively 'org-todo-list))
2839 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2840 ((equal org-keys "m") (call-interactively 'org-tags-view))
2841 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2842 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2843 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
2844 (add-hook
2845 'post-command-hook
2846 (lambda ()
2847 (unless (current-message)
2848 (let* ((m (org-agenda-get-any-marker))
2849 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2850 (when note
2851 (message (concat
2852 "FLAGGING-NOTE ([?] for more info): "
2853 (org-add-props
2854 (replace-regexp-in-string
2855 "\\\\n" "//"
2856 (copy-sequence note))
2857 nil 'face 'org-warning)))))))
2858 t t))
2859 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2860 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2861 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
2862 (t (user-error "Invalid agenda key"))))))
2864 (defvar org-agenda-multi)
2866 (defun org-agenda-append-agenda ()
2867 "Append another agenda view to the current one.
2868 This function allows interactive building of block agendas.
2869 Agenda views are separated by `org-agenda-block-separator'."
2870 (interactive)
2871 (unless (derived-mode-p 'org-agenda-mode)
2872 (user-error "Can only append from within agenda buffer"))
2873 (let ((org-agenda-multi t))
2874 (org-agenda)
2875 (widen)
2876 (org-agenda-finalize)
2877 (setq buffer-read-only t)
2878 (org-agenda-fit-window-to-buffer)))
2880 (defun org-agenda-normalize-custom-commands (cmds)
2881 "Normalize custom commands CMDS."
2882 (delq nil
2883 (mapcar
2884 (lambda (x)
2885 (cond ((stringp (cdr x)) nil)
2886 ((stringp (nth 1 x)) x)
2887 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2888 (t (cons (car x) (cons "" (cdr x))))))
2889 cmds)))
2891 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2892 "The user interface for selecting an agenda command."
2893 (catch 'exit
2894 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2895 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2896 (region-p (org-region-active-p))
2897 (custom org-agenda-custom-commands)
2898 (selstring "")
2899 restriction second-time
2900 c entry key type match prefixes rmheader header-end custom1 desc
2901 line lines left right n n1)
2902 (save-window-excursion
2903 (delete-other-windows)
2904 (org-switch-to-buffer-other-window " *Agenda Commands*")
2905 (erase-buffer)
2906 (insert (eval-when-compile
2907 (let ((header
2908 "Press key for an agenda command:
2909 -------------------------------- < Buffer, subtree/region restriction
2910 a Agenda for current week or day > Remove restriction
2911 t List of all TODO entries e Export agenda views
2912 m Match a TAGS/PROP/TODO query T Entries with special TODO kwd
2913 s Search for keywords M Like m, but only TODO entries
2914 / Multi-occur S Like s, but only TODO entries
2915 ? Find :FLAGGED: entries C Configure custom agenda commands
2916 * Toggle sticky agenda views # List stuck projects (!=configure)
2918 (start 0))
2919 (while (string-match
2920 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2921 header start)
2922 (setq start (match-end 0))
2923 (add-text-properties (match-beginning 2) (match-end 2)
2924 '(face bold) header))
2925 header)))
2926 (setq header-end (point-marker))
2927 (while t
2928 (setq custom1 custom)
2929 (when (eq rmheader t)
2930 (org-goto-line 1)
2931 (re-search-forward ":" nil t)
2932 (delete-region (match-end 0) (point-at-eol))
2933 (forward-char 1)
2934 (looking-at "-+")
2935 (delete-region (match-end 0) (point-at-eol))
2936 (move-marker header-end (match-end 0)))
2937 (goto-char header-end)
2938 (delete-region (point) (point-max))
2940 ;; Produce all the lines that describe custom commands and prefixes
2941 (setq lines nil)
2942 (while (setq entry (pop custom1))
2943 (setq key (car entry) desc (nth 1 entry)
2944 type (nth 2 entry)
2945 match (nth 3 entry))
2946 (if (> (length key) 1)
2947 (cl-pushnew (string-to-char key) prefixes :test #'equal)
2948 (setq line
2949 (format
2950 "%-4s%-14s"
2951 (org-add-props (copy-sequence key)
2952 '(face bold))
2953 (cond
2954 ((string-match "\\S-" desc) desc)
2955 ((eq type 'agenda) "Agenda for current week or day")
2956 ((eq type 'agenda*) "Appointments for current week or day")
2957 ((eq type 'alltodo) "List of all TODO entries")
2958 ((eq type 'search) "Word search")
2959 ((eq type 'stuck) "List of stuck projects")
2960 ((eq type 'todo) "TODO keyword")
2961 ((eq type 'tags) "Tags query")
2962 ((eq type 'tags-todo) "Tags (TODO)")
2963 ((eq type 'tags-tree) "Tags tree")
2964 ((eq type 'todo-tree) "TODO kwd tree")
2965 ((eq type 'occur-tree) "Occur tree")
2966 ((functionp type) (if (symbolp type)
2967 (symbol-name type)
2968 "Lambda expression"))
2969 (t "???"))))
2970 (if org-agenda-menu-show-matcher
2971 (setq line
2972 (concat line ": "
2973 (cond
2974 ((stringp match)
2975 (setq match (copy-sequence match))
2976 (org-add-props match nil 'face 'org-warning))
2977 ((listp type)
2978 (format "set of %d commands" (length type))))))
2979 (if (org-string-nw-p match)
2980 (add-text-properties
2981 0 (length line) (list 'help-echo
2982 (concat "Matcher: " match)) line)))
2983 (push line lines)))
2984 (setq lines (nreverse lines))
2985 (when prefixes
2986 (mapc (lambda (x)
2987 (push
2988 (format "%s %s"
2989 (org-add-props (char-to-string x)
2990 nil 'face 'bold)
2991 (or (cdr (assoc (concat selstring
2992 (char-to-string x))
2993 prefix-descriptions))
2994 "Prefix key"))
2995 lines))
2996 prefixes))
2998 ;; Check if we should display in two columns
2999 (if org-agenda-menu-two-columns
3000 (progn
3001 (setq n (length lines)
3002 n1 (+ (/ n 2) (mod n 2))
3003 right (nthcdr n1 lines)
3004 left (copy-sequence lines))
3005 (setcdr (nthcdr (1- n1) left) nil))
3006 (setq left lines right nil))
3007 (while left
3008 (insert "\n" (pop left))
3009 (when right
3010 (if (< (current-column) 40)
3011 (move-to-column 40 t)
3012 (insert " "))
3013 (insert (pop right))))
3015 ;; Make the window the right size
3016 (goto-char (point-min))
3017 (if second-time
3018 (if (not (pos-visible-in-window-p (point-max)))
3019 (org-fit-window-to-buffer))
3020 (setq second-time t)
3021 (org-fit-window-to-buffer))
3023 ;; Ask for selection
3024 (message "Press key for agenda command%s:"
3025 (if (or restrict-ok org-agenda-overriding-restriction)
3026 (if org-agenda-overriding-restriction
3027 " (restriction lock active)"
3028 (if restriction
3029 (format " (restricted to %s)" restriction)
3030 " (unrestricted)"))
3031 ""))
3032 (setq c (read-char-exclusive))
3033 (message "")
3034 (cond
3035 ((assoc (char-to-string c) custom)
3036 (setq selstring (concat selstring (char-to-string c)))
3037 (throw 'exit (cons selstring restriction)))
3038 ((memq c prefixes)
3039 (setq selstring (concat selstring (char-to-string c))
3040 prefixes nil
3041 rmheader (or rmheader t)
3042 custom (delq nil (mapcar
3043 (lambda (x)
3044 (if (or (= (length (car x)) 1)
3045 (/= (string-to-char (car x)) c))
3047 (cons (substring (car x) 1) (cdr x))))
3048 custom))))
3049 ((eq c ?*)
3050 (call-interactively 'org-toggle-sticky-agenda)
3051 (sit-for 2))
3052 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
3053 (message "Restriction is only possible in Org buffers")
3054 (ding) (sit-for 1))
3055 ((eq c ?1)
3056 (org-agenda-remove-restriction-lock 'noupdate)
3057 (setq restriction 'buffer))
3058 ((eq c ?0)
3059 (org-agenda-remove-restriction-lock 'noupdate)
3060 (setq restriction (if region-p 'region 'subtree)))
3061 ((eq c ?<)
3062 (org-agenda-remove-restriction-lock 'noupdate)
3063 (setq restriction
3064 (cond
3065 ((eq restriction 'buffer)
3066 (if region-p 'region 'subtree))
3067 ((memq restriction '(subtree region))
3068 nil)
3069 (t 'buffer))))
3070 ((eq c ?>)
3071 (org-agenda-remove-restriction-lock 'noupdate)
3072 (setq restriction nil))
3073 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
3074 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
3075 ((and (> (length selstring) 0) (eq c ?\d))
3076 (delete-window)
3077 (org-agenda-get-restriction-and-command prefix-descriptions))
3079 ((equal c ?q) (error "Abort"))
3080 (t (user-error "Invalid key %c" c))))))))
3082 (defun org-agenda-fit-window-to-buffer ()
3083 "Fit the window to the buffer size."
3084 (and (memq org-agenda-window-setup '(reorganize-frame))
3085 (fboundp 'fit-window-to-buffer)
3086 (if (and (= (cdr org-agenda-window-frame-fractions) 1.0)
3087 (= (car org-agenda-window-frame-fractions) 1.0))
3088 (delete-other-windows)
3089 (org-fit-window-to-buffer
3091 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3092 (floor (* (frame-height) (car org-agenda-window-frame-fractions)))))))
3094 (defvar org-cmd nil)
3095 (defvar org-agenda-overriding-cmd nil)
3096 (defvar org-agenda-overriding-arguments nil)
3097 (defvar org-agenda-overriding-cmd-arguments nil)
3098 (defun org-agenda-run-series (name series)
3099 "Run agenda NAME as a SERIES of agenda commands."
3100 (org-let (nth 1 series) '(org-agenda-prepare name))
3101 ;; We need to reset agenda markers here, because when constructing a
3102 ;; block agenda, the individual blocks do not do that.
3103 (org-agenda-reset-markers)
3104 (let* ((org-agenda-multi t)
3105 (redo (list 'org-agenda-run-series name (list 'quote series)))
3106 (cmds (car series))
3107 (gprops (nth 1 series))
3108 match ;; The byte compiler incorrectly complains about this. Keep it!
3109 org-cmd type lprops)
3110 (while (setq org-cmd (pop cmds))
3111 (setq type (car org-cmd))
3112 (setq match (eval (nth 1 org-cmd)))
3113 (setq lprops (nth 2 org-cmd))
3114 (let ((org-agenda-overriding-arguments
3115 (if (eq org-agenda-overriding-cmd org-cmd)
3116 (or org-agenda-overriding-arguments
3117 org-agenda-overriding-cmd-arguments))))
3118 (cond
3119 ((eq type 'agenda)
3120 (org-let2 gprops lprops
3121 '(call-interactively 'org-agenda-list)))
3122 ((eq type 'agenda*)
3123 (org-let2 gprops lprops
3124 '(funcall 'org-agenda-list nil nil t)))
3125 ((eq type 'alltodo)
3126 (org-let2 gprops lprops
3127 '(call-interactively 'org-todo-list)))
3128 ((eq type 'search)
3129 (org-let2 gprops lprops
3130 '(org-search-view current-prefix-arg match nil)))
3131 ((eq type 'stuck)
3132 (org-let2 gprops lprops
3133 '(call-interactively 'org-agenda-list-stuck-projects)))
3134 ((eq type 'tags)
3135 (org-let2 gprops lprops
3136 '(org-tags-view current-prefix-arg match)))
3137 ((eq type 'tags-todo)
3138 (org-let2 gprops lprops
3139 '(org-tags-view '(4) match)))
3140 ((eq type 'todo)
3141 (org-let2 gprops lprops
3142 '(org-todo-list match)))
3143 ((fboundp type)
3144 (org-let2 gprops lprops
3145 '(funcall type match)))
3146 (t (error "Invalid type in command series")))))
3147 (widen)
3148 (let ((inhibit-read-only t))
3149 (add-text-properties (point-min) (point-max)
3150 `(org-series t org-series-redo-cmd ,redo)))
3151 (setq org-agenda-redo-command redo)
3152 (goto-char (point-min)))
3153 (org-agenda-fit-window-to-buffer)
3154 (org-let (nth 1 series) '(org-agenda-finalize)))
3156 ;;;###autoload
3157 (defmacro org-batch-agenda (cmd-key &rest parameters)
3158 "Run an agenda command in batch mode and send the result to STDOUT.
3159 If CMD-KEY is a string of length 1, it is used as a key in
3160 `org-agenda-custom-commands' and triggers this command. If it is a
3161 longer string it is used as a tags/todo match string.
3162 Parameters are alternating variable names and values that will be bound
3163 before running the agenda command."
3164 (org-eval-in-environment (org-make-parameter-alist parameters)
3165 (let (org-agenda-sticky)
3166 (if (> (length cmd-key) 1)
3167 (org-tags-view nil cmd-key)
3168 (org-agenda nil cmd-key))))
3169 (set-buffer org-agenda-buffer-name)
3170 (princ (buffer-string)))
3172 (defvar org-agenda-info nil)
3174 ;;;###autoload
3175 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
3176 "Run an agenda command in batch mode and send the result to STDOUT.
3177 If CMD-KEY is a string of length 1, it is used as a key in
3178 `org-agenda-custom-commands' and triggers this command. If it is a
3179 longer string it is used as a tags/todo match string.
3180 Parameters are alternating variable names and values that will be bound
3181 before running the agenda command.
3183 The output gives a line for each selected agenda item. Each
3184 item is a list of comma-separated values, like this:
3186 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3188 category The category of the item
3189 head The headline, without TODO kwd, TAGS and PRIORITY
3190 type The type of the agenda entry, can be
3191 todo selected in TODO match
3192 tagsmatch selected in tags match
3193 diary imported from diary
3194 deadline a deadline on given date
3195 scheduled scheduled on given date
3196 timestamp entry has timestamp on given date
3197 closed entry was closed on given date
3198 upcoming-deadline warning about deadline
3199 past-scheduled forwarded scheduled item
3200 block entry has date block including g. date
3201 todo The todo keyword, if any
3202 tags All tags including inherited ones, separated by colons
3203 date The relevant date, like 2007-2-14
3204 time The time, like 15:00-16:50
3205 extra Sting with extra planning info
3206 priority-l The priority letter if any was given
3207 priority-n The computed numerical priority
3208 agenda-day The day in the agenda where this is listed"
3209 (org-eval-in-environment (append '((org-agenda-remove-tags t))
3210 (org-make-parameter-alist parameters))
3211 (if (> (length cmd-key) 2)
3212 (org-tags-view nil cmd-key)
3213 (org-agenda nil cmd-key)))
3214 (set-buffer org-agenda-buffer-name)
3215 (let* ((lines (org-split-string (buffer-string) "\n"))
3216 line)
3217 (while (setq line (pop lines))
3218 (catch 'next
3219 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
3220 (setq org-agenda-info
3221 (org-fix-agenda-info (text-properties-at 0 line)))
3222 (princ
3223 (mapconcat 'org-agenda-export-csv-mapper
3224 '(org-category txt type todo tags date time extra
3225 priority-letter priority agenda-day)
3226 ","))
3227 (princ "\n")))))
3229 (defun org-fix-agenda-info (props)
3230 "Make sure all properties on an agenda item have a canonical form.
3231 This ensures the export commands can easily use it."
3232 (let (tmp re)
3233 (when (setq tmp (plist-get props 'tags))
3234 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3235 (when (setq tmp (plist-get props 'date))
3236 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3237 (let ((calendar-date-display-form '(year "-" month "-" day)))
3238 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3240 (setq tmp (calendar-date-string tmp)))
3241 (setq props (plist-put props 'date tmp)))
3242 (when (setq tmp (plist-get props 'day))
3243 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3244 (let ((calendar-date-display-form '(year "-" month "-" day)))
3245 (setq tmp (calendar-date-string tmp)))
3246 (setq props (plist-put props 'day tmp))
3247 (setq props (plist-put props 'agenda-day tmp)))
3248 (when (setq tmp (plist-get props 'txt))
3249 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3250 (plist-put props 'priority-letter (match-string 1 tmp))
3251 (setq tmp (replace-match "" t t tmp)))
3252 (when (and (setq re (plist-get props 'org-todo-regexp))
3253 (setq re (concat "\\`\\.*" re " ?"))
3254 (let ((case-fold-search nil)) (string-match re tmp)))
3255 (plist-put props 'todo (match-string 1 tmp))
3256 (setq tmp (replace-match "" t t tmp)))
3257 (plist-put props 'txt tmp)))
3258 props)
3260 (defun org-agenda-export-csv-mapper (prop)
3261 (let ((res (plist-get org-agenda-info prop)))
3262 (setq res
3263 (cond
3264 ((not res) "")
3265 ((stringp res) res)
3266 (t (prin1-to-string res))))
3267 (org-trim (replace-regexp-in-string "," ";" res nil t))))
3269 ;;;###autoload
3270 (defun org-store-agenda-views (&rest parameters)
3271 "Store agenda views."
3272 (interactive)
3273 (eval (list 'org-batch-store-agenda-views)))
3275 ;;;###autoload
3276 (defmacro org-batch-store-agenda-views (&rest parameters)
3277 "Run all custom agenda commands that have a file argument."
3278 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3279 (pop-up-frames nil)
3280 (dir default-directory)
3281 (pars (org-make-parameter-alist parameters))
3282 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
3283 (save-window-excursion
3284 (while cmds
3285 (setq cmd (pop cmds)
3286 thiscmdkey (car cmd)
3287 thiscmdcmd (cdr cmd)
3288 match (nth 2 thiscmdcmd)
3289 bufname (if org-agenda-sticky
3290 (or (and (stringp match)
3291 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3292 (format "*Org Agenda(%s)*" thiscmdkey))
3293 org-agenda-buffer-name)
3294 cmd-or-set (nth 2 cmd)
3295 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3296 files (nth (if (listp cmd-or-set) 4 5) cmd))
3297 (if (stringp files) (setq files (list files)))
3298 (when files
3299 (org-eval-in-environment (append org-agenda-exporter-settings
3300 opts pars)
3301 (org-agenda nil thiscmdkey))
3302 (set-buffer bufname)
3303 (while files
3304 (org-eval-in-environment (append org-agenda-exporter-settings
3305 opts pars)
3306 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3307 (and (get-buffer bufname)
3308 (kill-buffer bufname)))))))
3310 (defvar org-agenda-current-span nil
3311 "The current span used in the agenda view.") ; local variable in the agenda buffer
3312 (defun org-agenda-mark-header-line (pos)
3313 "Mark the line at POS as an agenda structure header."
3314 (save-excursion
3315 (goto-char pos)
3316 (put-text-property (point-at-bol) (point-at-eol)
3317 'org-agenda-structural-header t)
3318 (when org-agenda-title-append
3319 (put-text-property (point-at-bol) (point-at-eol)
3320 'org-agenda-title-append org-agenda-title-append))))
3322 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3323 (defvar org-agenda-write-buffer-name "Agenda View")
3324 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3325 "Write the current buffer (an agenda view) as a file.
3327 Depending on the extension of the file name, plain text (.txt),
3328 HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
3329 If the extension is .ics, translate visible agenda into iCalendar
3330 format. If the extension is .org, collect all subtrees
3331 corresponding to the agenda entries and add them in an .org file.
3333 With prefix argument OPEN, open the new file immediately. If
3334 NOSETTINGS is given, do not scope the settings of
3335 `org-agenda-exporter-settings' into the export commands. This is
3336 used when the settings have already been scoped and we do not
3337 wish to overrule other, higher priority settings. If
3338 AGENDA-BUFFER-NAME is provided, use this as the buffer name for
3339 the agenda to write."
3340 (interactive "FWrite agenda to file: \nP")
3341 (if (or (not (file-writable-p file))
3342 (and (file-exists-p file)
3343 (if (called-interactively-p 'any)
3344 (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
3345 (user-error "Cannot write agenda to file %s" file))
3346 (org-let (if nosettings nil org-agenda-exporter-settings)
3347 '(save-excursion
3348 (save-window-excursion
3349 (let ((bs (copy-sequence (buffer-string)))
3350 (extension (file-name-extension file))
3351 beg content)
3352 (with-temp-buffer
3353 (rename-buffer org-agenda-write-buffer-name t)
3354 (set-buffer-modified-p nil)
3355 (insert bs)
3356 (org-agenda-remove-marked-text 'invisible 'org-filtered)
3357 (run-hooks 'org-agenda-before-write-hook)
3358 (cond
3359 ((bound-and-true-p org-mobile-creating-agendas)
3360 (org-mobile-write-agenda-for-mobile file))
3361 ((string= "org" extension)
3362 (let (content p m message-log-max)
3363 (goto-char (point-min))
3364 (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
3365 (goto-char p)
3366 (setq m (get-text-property (point) 'org-hd-marker))
3367 (when m
3368 (push (save-excursion
3369 (set-buffer (marker-buffer m))
3370 (goto-char m)
3371 (org-copy-subtree 1 nil t t)
3372 org-subtree-clip)
3373 content)))
3374 (find-file file)
3375 (erase-buffer)
3376 (dolist (s content) (org-paste-subtree 1 s))
3377 (write-file file)
3378 (kill-buffer (current-buffer))
3379 (message "Org file written to %s" file)))
3380 ((member extension '("html" "htm"))
3381 (require 'htmlize)
3382 (set-buffer (htmlize-buffer (current-buffer)))
3383 (when org-agenda-export-html-style
3384 ;; replace <style> section with org-agenda-export-html-style
3385 (goto-char (point-min))
3386 (kill-region (- (search-forward "<style") 6)
3387 (search-forward "</style>"))
3388 (insert org-agenda-export-html-style))
3389 (write-file file)
3390 (kill-buffer (current-buffer))
3391 (message "HTML written to %s" file))
3392 ((string= "ps" extension)
3393 (require 'ps-print)
3394 (ps-print-buffer-with-faces file)
3395 (message "Postscript written to %s" file))
3396 ((string= "pdf" extension)
3397 (require 'ps-print)
3398 (ps-print-buffer-with-faces
3399 (concat (file-name-sans-extension file) ".ps"))
3400 (call-process "ps2pdf" nil nil nil
3401 (expand-file-name
3402 (concat (file-name-sans-extension file) ".ps"))
3403 (expand-file-name file))
3404 (delete-file (concat (file-name-sans-extension file) ".ps"))
3405 (message "PDF written to %s" file))
3406 ((string= "ics" extension)
3407 (require 'ox-icalendar)
3408 (org-icalendar-export-current-agenda (expand-file-name file)))
3410 (let ((bs (buffer-string)))
3411 (find-file file)
3412 (erase-buffer)
3413 (insert bs)
3414 (save-buffer 0)
3415 (kill-buffer (current-buffer))
3416 (message "Plain text written to %s" file))))))))
3417 (set-buffer (or agenda-bufname
3418 (and (called-interactively-p 'any) (buffer-name))
3419 org-agenda-buffer-name)))
3420 (when open (org-open-file file)))
3422 (defun org-agenda-remove-marked-text (property &optional value)
3423 "Delete all text marked with VALUE of PROPERTY.
3424 VALUE defaults to t."
3425 (let (beg)
3426 (setq value (or value t))
3427 (while (setq beg (text-property-any (point-min) (point-max)
3428 property value))
3429 (delete-region
3430 beg (or (next-single-property-change beg property)
3431 (point-max))))))
3433 (defun org-agenda-add-entry-text ()
3434 "Add entry text to agenda lines.
3435 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3436 entry text following headings shown in the agenda.
3437 Drawers will be excluded, also the line with scheduling/deadline info."
3438 (when (and (> org-agenda-add-entry-text-maxlines 0)
3439 (not (bound-and-true-p org-mobile-creating-agendas)))
3440 (let (m txt)
3441 (goto-char (point-min))
3442 (while (not (eobp))
3443 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3444 (beginning-of-line 2)
3445 (setq txt (org-agenda-get-some-entry-text
3446 m org-agenda-add-entry-text-maxlines " > "))
3447 (end-of-line 1)
3448 (if (string-match "\\S-" txt)
3449 (insert "\n" txt)
3450 (or (eobp) (forward-char 1))))))))
3452 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3453 &rest keep)
3454 "Extract entry text from MARKER, at most N-LINES lines.
3455 This will ignore drawers etc, just get the text.
3456 If INDENT is given, prefix every line with this string. If KEEP is
3457 given, it is a list of symbols, defining stuff that should not be
3458 removed from the entry content. Currently only `planning' is allowed here."
3459 (let (txt drawer-re kwd-time-re ind)
3460 (save-excursion
3461 (with-current-buffer (marker-buffer marker)
3462 (if (not (derived-mode-p 'org-mode))
3463 (setq txt "")
3464 (org-with-wide-buffer
3465 (goto-char marker)
3466 (end-of-line 1)
3467 (setq txt (buffer-substring
3468 (min (1+ (point)) (point-max))
3469 (progn (outline-next-heading) (point)))
3470 drawer-re org-drawer-regexp
3471 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3472 ".*\n?"))
3473 (with-temp-buffer
3474 (insert txt)
3475 (when org-agenda-add-entry-text-descriptive-links
3476 (goto-char (point-min))
3477 (while (org-activate-bracket-links (point-max))
3478 (add-text-properties (match-beginning 0) (match-end 0)
3479 '(face org-link))))
3480 (goto-char (point-min))
3481 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3482 (set-text-properties (match-beginning 0) (match-end 0)
3483 nil))
3484 (goto-char (point-min))
3485 (while (re-search-forward drawer-re nil t)
3486 (delete-region
3487 (match-beginning 0)
3488 (progn (re-search-forward
3489 "^[ \t]*:END:.*\n?" nil 'move)
3490 (point))))
3491 (unless (member 'planning keep)
3492 (goto-char (point-min))
3493 (while (re-search-forward kwd-time-re nil t)
3494 (replace-match "")))
3495 (goto-char (point-min))
3496 (when org-agenda-entry-text-exclude-regexps
3497 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3498 (while (setq re (pop re-list))
3499 (goto-char (point-min))
3500 (while (re-search-forward re nil t)
3501 (replace-match "")))))
3502 (goto-char (point-max))
3503 (skip-chars-backward " \t\n")
3504 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3506 ;; find and remove min common indentation
3507 (goto-char (point-min))
3508 (untabify (point-min) (point-max))
3509 (setq ind (org-get-indentation))
3510 (while (not (eobp))
3511 (unless (looking-at "[ \t]*$")
3512 (setq ind (min ind (org-get-indentation))))
3513 (beginning-of-line 2))
3514 (goto-char (point-min))
3515 (while (not (eobp))
3516 (unless (looking-at "[ \t]*$")
3517 (move-to-column ind)
3518 (delete-region (point-at-bol) (point)))
3519 (beginning-of-line 2))
3521 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3523 (goto-char (point-min))
3524 (when indent
3525 (while (and (not (eobp)) (re-search-forward "^" nil t))
3526 (replace-match indent t t)))
3527 (goto-char (point-min))
3528 (while (looking-at "[ \t]*\n") (replace-match ""))
3529 (goto-char (point-max))
3530 (when (> (org-current-line)
3531 n-lines)
3532 (org-goto-line (1+ n-lines))
3533 (backward-char 1))
3534 (setq txt (buffer-substring (point-min) (point))))))))
3535 txt))
3537 (defun org-check-for-org-mode ()
3538 "Make sure current buffer is in org-mode. Error if not."
3539 (or (derived-mode-p 'org-mode)
3540 (error "Cannot execute Org agenda command on buffer in %s"
3541 major-mode)))
3543 ;;; Agenda prepare and finalize
3545 (defvar org-agenda-multi nil) ; dynamically scoped
3546 (defvar org-agenda-pre-window-conf nil)
3547 (defvar org-agenda-columns-active nil)
3548 (defvar org-agenda-name nil)
3549 (defvar org-agenda-tag-filter nil)
3550 (defvar org-agenda-category-filter nil)
3551 (defvar org-agenda-regexp-filter nil)
3552 (defvar org-agenda-effort-filter nil)
3553 (defvar org-agenda-top-headline-filter nil)
3554 (defvar org-agenda-tag-filter-preset nil
3555 "A preset of the tags filter used for secondary agenda filtering.
3556 This must be a list of strings, each string must be a single tag preceded
3557 by \"+\" or \"-\".
3558 This variable should not be set directly, but agenda custom commands can
3559 bind it in the options section. The preset filter is a global property of
3560 the entire agenda view. In a block agenda, it will not work reliably to
3561 define a filter for one of the individual blocks. You need to set it in
3562 the global options and expect it to be applied to the entire view.")
3564 (defvar org-agenda-category-filter-preset nil
3565 "A preset of the category filter used for secondary agenda filtering.
3566 This must be a list of strings, each string must be a single category
3567 preceded by \"+\" or \"-\".
3568 This variable should not be set directly, but agenda custom commands can
3569 bind it in the options section. The preset filter is a global property of
3570 the entire agenda view. In a block agenda, it will not work reliably to
3571 define a filter for one of the individual blocks. You need to set it in
3572 the global options and expect it to be applied to the entire view.")
3574 (defvar org-agenda-regexp-filter-preset nil
3575 "A preset of the regexp filter used for secondary agenda filtering.
3576 This must be a list of strings, each string must be a single regexp
3577 preceded by \"+\" or \"-\".
3578 This variable should not be set directly, but agenda custom commands can
3579 bind it in the options section. The preset filter is a global property of
3580 the entire agenda view. In a block agenda, it will not work reliably to
3581 define a filter for one of the individual blocks. You need to set it in
3582 the global options and expect it to be applied to the entire view.")
3584 (defvar org-agenda-effort-filter-preset nil
3585 "A preset of the effort condition used for secondary agenda filtering.
3586 This must be a list of strings, each string must be a single regexp
3587 preceded by \"+\" or \"-\".
3588 This variable should not be set directly, but agenda custom commands can
3589 bind it in the options section. The preset filter is a global property of
3590 the entire agenda view. In a block agenda, it will not work reliably to
3591 define a filter for one of the individual blocks. You need to set it in
3592 the global options and expect it to be applied to the entire view.")
3594 (defun org-agenda-use-sticky-p ()
3595 "Return non-nil if an agenda buffer named
3596 `org-agenda-buffer-name' exists and should be shown instead of
3597 generating a new one."
3598 (and
3599 ;; turned off by user
3600 org-agenda-sticky
3601 ;; For multi-agenda buffer already exists
3602 (not org-agenda-multi)
3603 ;; buffer found
3604 (get-buffer org-agenda-buffer-name)
3605 ;; C-u parameter is same as last call
3606 (with-current-buffer (get-buffer org-agenda-buffer-name)
3607 (and
3608 (equal current-prefix-arg
3609 org-agenda-last-prefix-arg)
3610 ;; In case user turned stickiness on, while having existing
3611 ;; Agenda buffer active, don't reuse that buffer, because it
3612 ;; does not have org variables local
3613 org-agenda-this-buffer-is-sticky))))
3615 (defun org-agenda-prepare-window (abuf filter-alist)
3616 "Setup agenda buffer in the window.
3617 ABUF is the buffer for the agenda window.
3618 FILTER-ALIST is an alist of filters we need to apply when
3619 `org-agenda-persistent-filter' is non-nil."
3620 (let* ((awin (get-buffer-window abuf)) wconf)
3621 (cond
3622 ((equal (current-buffer) abuf) nil)
3623 (awin (select-window awin))
3624 ((not (setq wconf (current-window-configuration))))
3625 ((eq org-agenda-window-setup 'current-window)
3626 (pop-to-buffer-same-window abuf))
3627 ((eq org-agenda-window-setup 'other-window)
3628 (org-switch-to-buffer-other-window abuf))
3629 ((eq org-agenda-window-setup 'other-frame)
3630 (switch-to-buffer-other-frame abuf))
3631 ((eq org-agenda-window-setup 'only-window)
3632 (delete-other-windows)
3633 (pop-to-buffer-same-window abuf))
3634 ((eq org-agenda-window-setup 'reorganize-frame)
3635 (delete-other-windows)
3636 (org-switch-to-buffer-other-window abuf)))
3637 (setq org-agenda-tag-filter (cdr (assq 'tag filter-alist)))
3638 (setq org-agenda-category-filter (cdr (assq 'cat filter-alist)))
3639 (setq org-agenda-effort-filter (cdr (assq 'effort filter-alist)))
3640 (setq org-agenda-regexp-filter (cdr (assq 're filter-alist)))
3641 ;; Additional test in case agenda is invoked from within agenda
3642 ;; buffer via elisp link.
3643 (unless (equal (current-buffer) abuf)
3644 (pop-to-buffer-same-window abuf))
3645 (setq org-agenda-pre-window-conf
3646 (or wconf org-agenda-pre-window-conf))))
3648 (defun org-agenda-prepare (&optional name)
3649 (let ((filter-alist (if org-agenda-persistent-filter
3650 (with-current-buffer
3651 (get-buffer-create org-agenda-buffer-name)
3652 (list `(tag . ,org-agenda-tag-filter)
3653 `(re . ,org-agenda-regexp-filter)
3654 `(effort . ,org-agenda-effort-filter)
3655 `(cat . ,org-agenda-category-filter))))))
3656 (if (org-agenda-use-sticky-p)
3657 (progn
3658 (put 'org-agenda-tag-filter :preset-filter nil)
3659 (put 'org-agenda-category-filter :preset-filter nil)
3660 (put 'org-agenda-regexp-filter :preset-filter nil)
3661 ;; Popup existing buffer
3662 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name)
3663 filter-alist)
3664 (message "Sticky Agenda buffer, use `r' to refresh")
3665 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3666 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3667 (setq org-todo-keywords-for-agenda nil)
3668 (put 'org-agenda-tag-filter :preset-filter
3669 org-agenda-tag-filter-preset)
3670 (put 'org-agenda-category-filter :preset-filter
3671 org-agenda-category-filter-preset)
3672 (put 'org-agenda-regexp-filter :preset-filter
3673 org-agenda-regexp-filter-preset)
3674 (put 'org-agenda-effort-filter :preset-filter
3675 org-agenda-effort-filter-preset)
3676 (if org-agenda-multi
3677 (progn
3678 (setq buffer-read-only nil)
3679 (goto-char (point-max))
3680 (unless (or (bobp) org-agenda-compact-blocks
3681 (not org-agenda-block-separator))
3682 (insert "\n"
3683 (if (stringp org-agenda-block-separator)
3684 org-agenda-block-separator
3685 (make-string (window-width) org-agenda-block-separator))
3686 "\n"))
3687 (narrow-to-region (point) (point-max)))
3688 (setq org-done-keywords-for-agenda nil)
3689 ;; Setting any org variables that are in org-agenda-local-vars
3690 ;; list need to be done after the prepare call
3691 (org-agenda-prepare-window
3692 (get-buffer-create org-agenda-buffer-name) filter-alist)
3693 (setq buffer-read-only nil)
3694 (org-agenda-reset-markers)
3695 (let ((inhibit-read-only t)) (erase-buffer))
3696 (org-agenda-mode)
3697 (setq org-agenda-buffer (current-buffer))
3698 (setq org-agenda-contributing-files nil)
3699 (setq org-agenda-columns-active nil)
3700 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3701 (setq org-todo-keywords-for-agenda
3702 (org-uniquify org-todo-keywords-for-agenda))
3703 (setq org-done-keywords-for-agenda
3704 (org-uniquify org-done-keywords-for-agenda))
3705 (setq org-agenda-last-prefix-arg current-prefix-arg)
3706 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3707 (and name (not org-agenda-name)
3708 (setq-local org-agenda-name name)))
3709 (setq buffer-read-only nil))))
3711 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3712 (defun org-agenda-finalize ()
3713 "Finishing touch for the agenda buffer, called just before displaying it."
3714 (unless org-agenda-multi
3715 (save-excursion
3716 (let ((inhibit-read-only t))
3717 (goto-char (point-min))
3718 (save-excursion
3719 (while (org-activate-bracket-links (point-max))
3720 (add-text-properties (match-beginning 0) (match-end 0)
3721 '(face org-link))))
3722 (save-excursion
3723 (while (org-activate-plain-links (point-max))
3724 (add-text-properties (match-beginning 0) (match-end 0)
3725 '(face org-link))))
3726 (unless (eq org-agenda-remove-tags t)
3727 (org-agenda-align-tags))
3728 (unless org-agenda-with-colors
3729 (remove-text-properties (point-min) (point-max) '(face nil)))
3730 (if (and (boundp 'org-agenda-overriding-columns-format)
3731 org-agenda-overriding-columns-format)
3732 (setq-local org-agenda-overriding-columns-format
3733 org-agenda-overriding-columns-format))
3734 (if (and (boundp 'org-agenda-view-columns-initially)
3735 org-agenda-view-columns-initially)
3736 (org-agenda-columns))
3737 (when org-agenda-fontify-priorities
3738 (org-agenda-fontify-priorities))
3739 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3740 (org-agenda-dim-blocked-tasks))
3741 (org-agenda-mark-clocking-task)
3742 (when org-agenda-entry-text-mode
3743 (org-agenda-entry-text-hide)
3744 (org-agenda-entry-text-show))
3745 (if (and (functionp 'org-habit-insert-consistency-graphs)
3746 (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
3747 (org-habit-insert-consistency-graphs))
3748 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3749 (unless (or (eq org-agenda-show-inherited-tags 'always)
3750 (and (listp org-agenda-show-inherited-tags)
3751 (memq org-agenda-type org-agenda-show-inherited-tags))
3752 (and (eq org-agenda-show-inherited-tags t)
3753 (or (eq org-agenda-use-tag-inheritance t)
3754 (and (listp org-agenda-use-tag-inheritance)
3755 (not (memq org-agenda-type
3756 org-agenda-use-tag-inheritance))))))
3757 (let (mrk)
3758 (save-excursion
3759 (goto-char (point-min))
3760 (while (equal (forward-line) 0)
3761 (when (setq mrk (get-text-property (point) 'org-hd-marker))
3762 (put-text-property (point-at-bol) (point-at-eol)
3763 'tags (org-with-point-at mrk
3764 (delete-dups
3765 (mapcar 'downcase (org-get-tags-at))))))))))
3766 (run-hooks 'org-agenda-finalize-hook)
3767 (when org-agenda-top-headline-filter
3768 (org-agenda-filter-top-headline-apply
3769 org-agenda-top-headline-filter))
3770 (when org-agenda-tag-filter
3771 (org-agenda-filter-apply org-agenda-tag-filter 'tag t))
3772 (when (get 'org-agenda-tag-filter :preset-filter)
3773 (org-agenda-filter-apply
3774 (get 'org-agenda-tag-filter :preset-filter) 'tag t))
3775 (when org-agenda-category-filter
3776 (org-agenda-filter-apply org-agenda-category-filter 'category))
3777 (when (get 'org-agenda-category-filter :preset-filter)
3778 (org-agenda-filter-apply
3779 (get 'org-agenda-category-filter :preset-filter) 'category))
3780 (when org-agenda-regexp-filter
3781 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
3782 (when (get 'org-agenda-regexp-filter :preset-filter)
3783 (org-agenda-filter-apply
3784 (get 'org-agenda-regexp-filter :preset-filter) 'regexp))
3785 (when org-agenda-effort-filter
3786 (org-agenda-filter-apply org-agenda-effort-filter 'effort))
3787 (when (get 'org-agenda-effort-filter :preset-filter)
3788 (org-agenda-filter-apply
3789 (get 'org-agenda-effort-filter :preset-filter) 'effort))
3790 (add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)))))
3792 (defun org-agenda-mark-clocking-task ()
3793 "Mark the current clock entry in the agenda if it is present."
3794 ;; We need to widen when `org-agenda-finalize' is called from
3795 ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
3796 (when (bound-and-true-p org-clock-current-task)
3797 (save-restriction
3798 (widen)
3799 (org-agenda-unmark-clocking-task)
3800 (when (marker-buffer org-clock-hd-marker)
3801 (save-excursion
3802 (goto-char (point-min))
3803 (let (s ov)
3804 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3805 (goto-char s)
3806 (when (equal (org-get-at-bol 'org-hd-marker)
3807 org-clock-hd-marker)
3808 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3809 (overlay-put ov 'type 'org-agenda-clocking)
3810 (overlay-put ov 'face 'org-agenda-clocking)
3811 (overlay-put ov 'help-echo
3812 "The clock is running in this item")))))))))
3814 (defun org-agenda-unmark-clocking-task ()
3815 "Unmark the current clocking task."
3816 (mapc (lambda (o)
3817 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3818 (delete-overlay o)))
3819 (overlays-in (point-min) (point-max))))
3821 (defun org-agenda-fontify-priorities ()
3822 "Make highest priority lines bold, and lowest italic."
3823 (interactive)
3824 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3825 (delete-overlay o)))
3826 (overlays-in (point-min) (point-max)))
3827 (save-excursion
3828 (let (b e p ov h l)
3829 (goto-char (point-min))
3830 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3831 (setq h (or (get-char-property (point) 'org-highest-priority)
3832 org-highest-priority)
3833 l (or (get-char-property (point) 'org-lowest-priority)
3834 org-lowest-priority)
3835 p (string-to-char (match-string 1))
3836 b (match-beginning 0)
3837 e (if (eq org-agenda-fontify-priorities 'cookies)
3838 (match-end 0)
3839 (point-at-eol))
3840 ov (make-overlay b e))
3841 (overlay-put
3842 ov 'face
3843 (let ((special-face
3844 (cond ((org-face-from-face-or-color
3845 'priority 'org-priority
3846 (cdr (assoc p org-priority-faces))))
3847 ((and (listp org-agenda-fontify-priorities)
3848 (org-face-from-face-or-color
3849 'priority 'org-priority
3850 (cdr (assoc p org-agenda-fontify-priorities)))))
3851 ((equal p l) 'italic)
3852 ((equal p h) 'bold))))
3853 (if special-face (list special-face 'org-priority) 'org-priority)))
3854 (overlay-put ov 'org-type 'org-priority)))))
3856 (defvar org-depend-tag-blocked)
3858 (defun org-agenda-dim-blocked-tasks (&optional invisible)
3859 "Dim currently blocked TODOs in the agenda display.
3860 When INVISIBLE is non-nil, hide currently blocked TODO instead of
3861 dimming them."
3862 (interactive "P")
3863 (when (called-interactively-p 'interactive)
3864 (message "Dim or hide blocked tasks..."))
3865 (dolist (o (overlays-in (point-min) (point-max)))
3866 (when (eq (overlay-get o 'org-type) 'org-blocked-todo)
3867 (delete-overlay o)))
3868 (save-excursion
3869 (let ((inhibit-read-only t)
3870 (org-depend-tag-blocked nil)
3871 org-blocked-by-checkboxes)
3872 (goto-char (point-min))
3873 (while (let ((pos (text-property-not-all
3874 (point) (point-max) 'todo-state nil)))
3875 (when pos (goto-char pos)))
3876 (setq org-blocked-by-checkboxes nil)
3877 (let ((marker (org-get-at-bol 'org-hd-marker)))
3878 (when (and (markerp marker)
3879 (with-current-buffer (marker-buffer marker)
3880 (save-excursion (goto-char marker)
3881 (org-entry-blocked-p))))
3882 ;; Entries blocked by checkboxes cannot be made invisible.
3883 ;; See `org-agenda-dim-blocked-tasks' for details.
3884 (let* ((really-invisible
3885 (and (not org-blocked-by-checkboxes)
3886 (or invisible (eq org-agenda-dim-blocked-tasks
3887 'invisible))))
3888 (ov (make-overlay (if really-invisible (line-end-position 0)
3889 (line-beginning-position))
3890 (line-end-position))))
3891 (if really-invisible (overlay-put ov 'invisible t)
3892 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3893 (overlay-put ov 'org-type 'org-blocked-todo))))
3894 (forward-line))))
3895 (when (called-interactively-p 'interactive)
3896 (message "Dim or hide blocked tasks...done")))
3898 (defvar org-agenda-skip-function nil
3899 "Function to be called at each match during agenda construction.
3900 If this function returns nil, the current match should not be skipped.
3901 Otherwise, the function must return a position from where the search
3902 should be continued.
3903 This may also be a Lisp form, it will be evaluated.
3904 Never set this variable using `setq' or so, because then it will apply
3905 to all future agenda commands. If you do want a global skipping condition,
3906 use the option `org-agenda-skip-function-global' instead.
3907 The correct usage for `org-agenda-skip-function' is to bind it with
3908 `let' to scope it dynamically into the agenda-constructing command.
3909 A good way to set it is through options in `org-agenda-custom-commands'.")
3911 (defun org-agenda-skip ()
3912 "Throw to `:skip' in places that should be skipped.
3913 Also moves point to the end of the skipped region, so that search can
3914 continue from there."
3915 (let ((p (point-at-bol)) to)
3916 (when (or
3917 (save-excursion (goto-char p) (looking-at comment-start-skip))
3918 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3919 (get-text-property p :org-archived)
3920 (org-end-of-subtree t))
3921 (and org-agenda-skip-comment-trees
3922 (get-text-property p :org-comment)
3923 (org-end-of-subtree t))
3924 (and (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3925 (org-agenda-skip-eval org-agenda-skip-function)))
3926 (goto-char to))
3927 (org-in-src-block-p t))
3928 (throw :skip t))))
3930 (defun org-agenda-skip-eval (form)
3931 "If FORM is a function or a list, call (or eval) it and return the result.
3932 `save-excursion' and `save-match-data' are wrapped around the call, so point
3933 and match data are returned to the previous state no matter what these
3934 functions do."
3935 (let (fp)
3936 (and form
3937 (or (setq fp (functionp form))
3938 (consp form))
3939 (save-excursion
3940 (save-match-data
3941 (if fp
3942 (funcall form)
3943 (eval form)))))))
3945 (defvar org-agenda-markers nil
3946 "List of all currently active markers created by `org-agenda'.")
3947 (defvar org-agenda-last-marker-time (float-time)
3948 "Creation time of the last agenda marker.")
3950 (defun org-agenda-new-marker (&optional pos)
3951 "Return a new agenda marker.
3952 Maker is at point, or at POS if non-nil. Org mode keeps a list of
3953 these markers and resets them when they are no longer in use."
3954 (let ((m (copy-marker (or pos (point)) t)))
3955 (setq org-agenda-last-marker-time (float-time))
3956 (if org-agenda-buffer
3957 (with-current-buffer org-agenda-buffer
3958 (push m org-agenda-markers))
3959 (push m org-agenda-markers))
3962 (defun org-agenda-reset-markers ()
3963 "Reset markers created by `org-agenda'."
3964 (while org-agenda-markers
3965 (move-marker (pop org-agenda-markers) nil)))
3967 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3968 "Save relative positions of markers in region.
3969 This check for agenda markers in all agenda buffers currently active."
3970 (dolist (buf (buffer-list))
3971 (with-current-buffer buf
3972 (when (eq major-mode 'org-agenda-mode)
3973 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3974 org-agenda-markers)))))
3976 ;;; Entry text mode
3978 (defun org-agenda-entry-text-show-here ()
3979 "Add some text from the entry as context to the current line."
3980 (let (m txt o)
3981 (setq m (org-get-at-bol 'org-hd-marker))
3982 (unless (marker-buffer m)
3983 (error "No marker points to an entry here"))
3984 (setq txt (concat "\n" (org-no-properties
3985 (org-agenda-get-some-entry-text
3986 m org-agenda-entry-text-maxlines
3987 org-agenda-entry-text-leaders))))
3988 (when (string-match "\\S-" txt)
3989 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3990 (overlay-put o 'evaporate t)
3991 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3992 (overlay-put o 'after-string txt))))
3994 (defun org-agenda-entry-text-show ()
3995 "Add entry context for all agenda lines."
3996 (interactive)
3997 (save-excursion
3998 (goto-char (point-max))
3999 (beginning-of-line 1)
4000 (while (not (bobp))
4001 (when (org-get-at-bol 'org-hd-marker)
4002 (org-agenda-entry-text-show-here))
4003 (beginning-of-line 0))))
4005 (defun org-agenda-entry-text-hide ()
4006 "Remove any shown entry context."
4007 (delq nil
4008 (mapcar (lambda (o)
4009 (if (eq (overlay-get o 'org-overlay-type)
4010 'agenda-entry-content)
4011 (progn (delete-overlay o) t)))
4012 (overlays-in (point-min) (point-max)))))
4014 (defun org-agenda-get-day-face (date)
4015 "Return the face DATE should be displayed with."
4016 (cond ((and (functionp org-agenda-day-face-function)
4017 (funcall org-agenda-day-face-function date)))
4018 ((org-agenda-today-p date) 'org-agenda-date-today)
4019 ((memq (calendar-day-of-week date) org-agenda-weekend-days)
4020 'org-agenda-date-weekend)
4021 (t 'org-agenda-date)))
4023 (defvar org-agenda-show-log-scoped)
4025 ;;; Agenda Daily/Weekly
4027 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4028 "Start day for the agenda view.
4029 Custom commands can set this variable in the options section.
4030 This is usually a string like \"2007-11-01\", \"+2d\" or any other
4031 input allowed when reading a date through the Org calendar.
4032 See the docstring of `org-read-date' for details.")
4033 (defvar org-starting-day nil) ; local variable in the agenda buffer
4034 (defvar org-arg-loc nil) ; local variable
4036 (defvar org-agenda-buffer-tmp-name nil)
4037 ;;;###autoload
4038 (defun org-agenda-list (&optional arg start-day span with-hour)
4039 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4040 The view will be for the current day or week, but from the overview buffer
4041 you will be able to go to other days/weeks.
4043 With a numeric prefix argument in an interactive call, the agenda will
4044 span ARG days. Lisp programs should instead specify SPAN to change
4045 the number of days. SPAN defaults to `org-agenda-span'.
4047 START-DAY defaults to TODAY, or to the most recent match for the weekday
4048 given in `org-agenda-start-on-weekday'.
4050 When WITH-HOUR is non-nil, only include scheduled and deadline
4051 items if they have an hour specification like [h]h:mm."
4052 (interactive "P")
4053 (if org-agenda-overriding-arguments
4054 (setq arg (car org-agenda-overriding-arguments)
4055 start-day (nth 1 org-agenda-overriding-arguments)
4056 span (nth 2 org-agenda-overriding-arguments)))
4057 (if (and (integerp arg) (> arg 0))
4058 (setq span arg arg nil))
4059 (catch 'exit
4060 (setq org-agenda-buffer-name
4061 (or org-agenda-buffer-tmp-name
4062 (if org-agenda-sticky
4063 (cond ((and org-keys (stringp org-match))
4064 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4065 (org-keys
4066 (format "*Org Agenda(%s)*" org-keys))
4067 (t "*Org Agenda(a)*")))
4068 org-agenda-buffer-name))
4069 (org-agenda-prepare "Day/Week")
4070 (setq start-day (or start-day org-agenda-start-day))
4071 (if (stringp start-day)
4072 ;; Convert to an absolute day number
4073 (setq start-day (time-to-days (org-read-date nil t start-day))))
4074 (org-compile-prefix-format 'agenda)
4075 (org-set-sorting-strategy 'agenda)
4076 (let* ((span (org-agenda-ndays-to-span (or span org-agenda-span)))
4077 (today (org-today))
4078 (sd (or start-day today))
4079 (ndays (org-agenda-span-to-ndays span sd))
4080 (org-agenda-start-on-weekday
4081 (if (or (eq ndays 7) (eq ndays 14))
4082 org-agenda-start-on-weekday))
4083 (thefiles (org-agenda-files nil 'ifmode))
4084 (files thefiles)
4085 (start (if (or (null org-agenda-start-on-weekday)
4086 (< ndays 7))
4088 (let* ((nt (calendar-day-of-week
4089 (calendar-gregorian-from-absolute sd)))
4090 (n1 org-agenda-start-on-weekday)
4091 (d (- nt n1)))
4092 (- sd (+ (if (< d 0) 7 0) d)))))
4093 (day-numbers (list start))
4094 (day-cnt 0)
4095 (inhibit-redisplay (not debug-on-error))
4096 (org-agenda-show-log-scoped org-agenda-show-log)
4097 s e rtn rtnall file date d start-pos end-pos todayp
4098 clocktable-start clocktable-end filter)
4099 (setq org-agenda-redo-command
4100 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4101 (dotimes (n (1- ndays))
4102 (push (1+ (car day-numbers)) day-numbers))
4103 (setq day-numbers (nreverse day-numbers))
4104 (setq clocktable-start (car day-numbers)
4105 clocktable-end (1+ (or (org-last day-numbers) 0)))
4106 (setq-local org-starting-day (car day-numbers))
4107 (setq-local org-arg-loc arg)
4108 (setq-local org-agenda-current-span (org-agenda-ndays-to-span span))
4109 (unless org-agenda-compact-blocks
4110 (let* ((d1 (car day-numbers))
4111 (d2 (org-last day-numbers))
4112 (w1 (org-days-to-iso-week d1))
4113 (w2 (org-days-to-iso-week d2)))
4114 (setq s (point))
4115 (if org-agenda-overriding-header
4116 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4117 nil 'face 'org-agenda-structure) "\n")
4118 (insert (org-agenda-span-name span)
4119 "-agenda"
4120 (if (< (- d2 d1) 350)
4121 (if (= w1 w2)
4122 (format " (W%02d)" w1)
4123 (format " (W%02d-W%02d)" w1 w2))
4125 ":\n")))
4126 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4127 'org-date-line t))
4128 (org-agenda-mark-header-line s))
4129 (while (setq d (pop day-numbers))
4130 (setq date (calendar-gregorian-from-absolute d)
4131 s (point))
4132 (if (or (setq todayp (= d today))
4133 (and (not start-pos) (= d sd)))
4134 (setq start-pos (point))
4135 (if (and start-pos (not end-pos))
4136 (setq end-pos (point))))
4137 (setq files thefiles
4138 rtnall nil)
4139 (while (setq file (pop files))
4140 (catch 'nextfile
4141 (org-check-agenda-file file)
4142 (let ((org-agenda-entry-types org-agenda-entry-types))
4143 ;; Starred types override non-starred equivalents
4144 (when (member :deadline* org-agenda-entry-types)
4145 (setq org-agenda-entry-types
4146 (delq :deadline org-agenda-entry-types)))
4147 (when (member :scheduled* org-agenda-entry-types)
4148 (setq org-agenda-entry-types
4149 (delq :scheduled org-agenda-entry-types)))
4150 ;; Honor with-hour
4151 (when with-hour
4152 (when (member :deadline org-agenda-entry-types)
4153 (setq org-agenda-entry-types
4154 (delq :deadline org-agenda-entry-types))
4155 (push :deadline* org-agenda-entry-types))
4156 (when (member :scheduled org-agenda-entry-types)
4157 (setq org-agenda-entry-types
4158 (delq :scheduled org-agenda-entry-types))
4159 (push :scheduled* org-agenda-entry-types)))
4160 (unless org-agenda-include-deadlines
4161 (setq org-agenda-entry-types
4162 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4163 (cond
4164 ((memq org-agenda-show-log-scoped '(only clockcheck))
4165 (setq rtn (org-agenda-get-day-entries
4166 file date :closed)))
4167 (org-agenda-show-log-scoped
4168 (setq rtn (apply 'org-agenda-get-day-entries
4169 file date
4170 (append '(:closed) org-agenda-entry-types))))
4172 (setq rtn (apply 'org-agenda-get-day-entries
4173 file date
4174 org-agenda-entry-types)))))
4175 (setq rtnall (append rtnall rtn)))) ;; all entries
4176 (if org-agenda-include-diary
4177 (let ((org-agenda-search-headline-for-time t))
4178 (require 'diary-lib)
4179 (setq rtn (org-get-entries-from-diary date))
4180 (setq rtnall (append rtnall rtn))))
4181 (if (or rtnall org-agenda-show-all-dates)
4182 (progn
4183 (setq day-cnt (1+ day-cnt))
4184 (insert
4185 (if (stringp org-agenda-format-date)
4186 (format-time-string org-agenda-format-date
4187 (org-time-from-absolute date))
4188 (funcall org-agenda-format-date date))
4189 "\n")
4190 (put-text-property s (1- (point)) 'face
4191 (org-agenda-get-day-face date))
4192 (put-text-property s (1- (point)) 'org-date-line t)
4193 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4194 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4195 (when todayp
4196 (put-text-property s (1- (point)) 'org-today t))
4197 (setq rtnall
4198 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4199 (if rtnall (insert ;; all entries
4200 (org-agenda-finalize-entries rtnall 'agenda)
4201 "\n"))
4202 (put-text-property s (1- (point)) 'day d)
4203 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4204 (when (and org-agenda-clockreport-mode clocktable-start)
4205 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4206 ;; the above line is to ensure the restricted range!
4207 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4208 tbl)
4209 (setq p (org-plist-delete p :block))
4210 (setq p (plist-put p :tstart clocktable-start))
4211 (setq p (plist-put p :tend clocktable-end))
4212 (setq p (plist-put p :scope 'agenda))
4213 (setq tbl (apply 'org-clock-get-clocktable p))
4214 (insert tbl)))
4215 (goto-char (point-min))
4216 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4217 (unless (and (pos-visible-in-window-p (point-min))
4218 (pos-visible-in-window-p (point-max)))
4219 (goto-char (1- (point-max)))
4220 (recenter -1)
4221 (if (not (pos-visible-in-window-p (or start-pos 1)))
4222 (progn
4223 (goto-char (or start-pos 1))
4224 (recenter 1))))
4225 (goto-char (or start-pos 1))
4226 (add-text-properties (point-min) (point-max)
4227 `(org-agenda-type agenda
4228 org-last-args (,arg ,start-day ,span)
4229 org-redo-cmd ,org-agenda-redo-command
4230 org-series-cmd ,org-cmd))
4231 (if (eq org-agenda-show-log-scoped 'clockcheck)
4232 (org-agenda-show-clocking-issues))
4233 (org-agenda-finalize)
4234 (setq buffer-read-only t)
4235 (message ""))))
4237 (defun org-agenda-ndays-to-span (n)
4238 "Return a span symbol for a span of N days, or N if none matches."
4239 (cond ((symbolp n) n)
4240 ((= n 1) 'day)
4241 ((= n 7) 'week)
4242 ((= n 14) 'fortnight)
4243 (t n)))
4245 (defun org-agenda-span-to-ndays (span &optional start-day)
4246 "Return ndays from SPAN, possibly starting at START-DAY.
4247 START-DAY is an absolute time value."
4248 (cond ((numberp span) span)
4249 ((eq span 'day) 1)
4250 ((eq span 'week) 7)
4251 ((eq span 'fortnight) 14)
4252 ((eq span 'month)
4253 (let ((date (calendar-gregorian-from-absolute start-day)))
4254 (calendar-last-day-of-month (car date) (cl-caddr date))))
4255 ((eq span 'year)
4256 (let ((date (calendar-gregorian-from-absolute start-day)))
4257 (if (calendar-leap-year-p (cl-caddr date)) 366 365)))))
4259 (defun org-agenda-span-name (span)
4260 "Return a SPAN name."
4261 (if (null span)
4263 (if (symbolp span)
4264 (capitalize (symbol-name span))
4265 (format "%d days" span))))
4267 ;;; Agenda word search
4269 (defvar org-agenda-search-history nil)
4271 (defvar org-search-syntax-table nil
4272 "Special syntax table for Org search.
4273 In this table, we have single quotes not as word constituents, to
4274 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4276 (defvar org-mode-syntax-table) ; From org.el
4277 (defun org-search-syntax-table ()
4278 (unless org-search-syntax-table
4279 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4280 (modify-syntax-entry ?' "." org-search-syntax-table)
4281 (modify-syntax-entry ?` "." org-search-syntax-table))
4282 org-search-syntax-table)
4284 (defvar org-agenda-last-search-view-search-was-boolean nil)
4286 ;;;###autoload
4287 (defun org-search-view (&optional todo-only string edit-at)
4288 "Show all entries that contain a phrase or words or regular expressions.
4290 With optional prefix argument TODO-ONLY, only consider entries that are
4291 TODO entries. The argument STRING can be used to pass a default search
4292 string into this function. If EDIT-AT is non-nil, it means that the
4293 user should get a chance to edit this string, with cursor at position
4294 EDIT-AT.
4296 The search string can be viewed either as a phrase that should be found as
4297 is, or it can be broken into a number of snippets, each of which must match
4298 in a Boolean way to select an entry. The default depends on the variable
4299 `org-agenda-search-view-always-boolean'.
4300 Even if this is turned off (the default) you can always switch to
4301 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4303 The default is a direct search of the whole phrase, where each space in
4304 the search string can expand to an arbitrary amount of whitespace,
4305 including newlines.
4307 If using a Boolean search, the search string is split on whitespace and
4308 each snippet is searched separately, with logical AND to select an entry.
4309 Words prefixed with a minus must *not* occur in the entry. Words without
4310 a prefix or prefixed with a plus must occur in the entry. Matching is
4311 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4312 match whole words, not parts of a word) if
4313 `org-agenda-search-view-force-full-words' is set (default is nil).
4315 Boolean search snippets enclosed by curly braces are interpreted as
4316 regular expressions that must or (when preceded with \"-\") must not
4317 match in the entry. Snippets enclosed into double quotes will be taken
4318 as a whole, to include whitespace.
4320 - If the search string starts with an asterisk, search only in headlines.
4321 - If (possibly after the leading star) the search string starts with an
4322 exclamation mark, this also means to look at TODO entries only, an effect
4323 that can also be achieved with a prefix argument.
4324 - If (possibly after star and exclamation mark) the search string starts
4325 with a colon, this will mean that the (non-regexp) snippets of the
4326 Boolean search must match as full words.
4328 This command searches the agenda files, and in addition the files listed
4329 in `org-agenda-text-search-extra-files'."
4330 (interactive "P")
4331 (if org-agenda-overriding-arguments
4332 (setq todo-only (car org-agenda-overriding-arguments)
4333 string (nth 1 org-agenda-overriding-arguments)
4334 edit-at (nth 2 org-agenda-overriding-arguments)))
4335 (let* ((props (list 'face nil
4336 'done-face 'org-agenda-done
4337 'org-not-done-regexp org-not-done-regexp
4338 'org-todo-regexp org-todo-regexp
4339 'org-complex-heading-regexp org-complex-heading-regexp
4340 'mouse-face 'highlight
4341 'help-echo (format "mouse-2 or RET jump to location")))
4342 (full-words org-agenda-search-view-force-full-words)
4343 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4344 regexp rtn rtnall files file pos inherited-tags
4345 marker category level tags c neg re boolean
4346 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4347 (unless (and (not edit-at)
4348 (stringp string)
4349 (string-match "\\S-" string))
4350 (setq string (read-string
4351 (if org-agenda-search-view-always-boolean
4352 "[+-]Word/{Regexp} ...: "
4353 "Phrase or [+-]Word/{Regexp} ...: ")
4354 (cond
4355 ((integerp edit-at) (cons string edit-at))
4356 (edit-at string))
4357 'org-agenda-search-history)))
4358 (catch 'exit
4359 (if org-agenda-sticky
4360 (setq org-agenda-buffer-name
4361 (if (stringp string)
4362 (format "*Org Agenda(%s:%s)*"
4363 (or org-keys (or (and todo-only "S") "s")) string)
4364 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4365 (org-agenda-prepare "SEARCH")
4366 (org-compile-prefix-format 'search)
4367 (org-set-sorting-strategy 'search)
4368 (setq org-agenda-redo-command
4369 (list 'org-search-view (if todo-only t nil)
4370 (list 'if 'current-prefix-arg nil string)))
4371 (setq org-agenda-query-string string)
4372 (if (equal (string-to-char string) ?*)
4373 (setq hdl-only t
4374 words (substring string 1))
4375 (setq words string))
4376 (when (equal (string-to-char words) ?!)
4377 (setq todo-only t
4378 words (substring words 1)))
4379 (when (equal (string-to-char words) ?:)
4380 (setq full-words t
4381 words (substring words 1)))
4382 (if (or org-agenda-search-view-always-boolean
4383 (member (string-to-char words) '(?- ?+ ?\{)))
4384 (setq boolean t))
4385 (setq words (org-split-string words))
4386 (let (www w)
4387 (while (setq w (pop words))
4388 (while (and (string-match "\\\\\\'" w) words)
4389 (setq w (concat (substring w 0 -1) " " (pop words))))
4390 (push w www))
4391 (setq words (nreverse www) www nil)
4392 (while (setq w (pop words))
4393 (when (and (string-match "\\`[-+]?{" w)
4394 (not (string-match "}\\'" w)))
4395 (while (and words (not (string-match "}\\'" (car words))))
4396 (setq w (concat w " " (pop words))))
4397 (setq w (concat w " " (pop words))))
4398 (push w www))
4399 (setq words (nreverse www)))
4400 (setq org-agenda-last-search-view-search-was-boolean boolean)
4401 (when boolean
4402 (let (wds w)
4403 (while (setq w (pop words))
4404 (if (or (equal (substring w 0 1) "\"")
4405 (and (> (length w) 1)
4406 (member (substring w 0 1) '("+" "-"))
4407 (equal (substring w 1 2) "\"")))
4408 (while (and words (not (equal (substring w -1) "\"")))
4409 (setq w (concat w " " (pop words)))))
4410 (and (string-match "\\`\\([-+]?\\)\"" w)
4411 (setq w (replace-match "\\1" nil nil w)))
4412 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4413 (push w wds))
4414 (setq words (nreverse wds))))
4415 (if boolean
4416 (mapc (lambda (w)
4417 (setq c (string-to-char w))
4418 (if (equal c ?-)
4419 (setq neg t w (substring w 1))
4420 (if (equal c ?+)
4421 (setq neg nil w (substring w 1))
4422 (setq neg nil)))
4423 (if (string-match "\\`{.*}\\'" w)
4424 (setq re (substring w 1 -1))
4425 (if full-words
4426 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4427 (setq re (regexp-quote (downcase w)))))
4428 (if neg (push re regexps-) (push re regexps+)))
4429 words)
4430 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4431 regexps+))
4432 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4433 (if (not regexps+)
4434 (setq regexp org-outline-regexp-bol)
4435 (setq regexp (pop regexps+))
4436 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4437 regexp))))
4438 (setq files (org-agenda-files nil 'ifmode))
4439 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4440 (pop org-agenda-text-search-extra-files)
4441 (setq files (org-add-archive-files files)))
4442 (setq files (append files org-agenda-text-search-extra-files)
4443 rtnall nil)
4444 (while (setq file (pop files))
4445 (setq ee nil)
4446 (catch 'nextfile
4447 (org-check-agenda-file file)
4448 (setq buffer (if (file-exists-p file)
4449 (org-get-agenda-file-buffer file)
4450 (error "No such file %s" file)))
4451 (if (not buffer)
4452 ;; If file does not exist, make sure an error message is sent
4453 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4454 file))))
4455 (with-current-buffer buffer
4456 (with-syntax-table (org-search-syntax-table)
4457 (unless (derived-mode-p 'org-mode)
4458 (error "Agenda file %s is not in `org-mode'" file))
4459 (let ((case-fold-search t))
4460 (save-excursion
4461 (save-restriction
4462 (if (eq buffer org-agenda-restrict)
4463 (narrow-to-region org-agenda-restrict-begin
4464 org-agenda-restrict-end)
4465 (widen))
4466 (goto-char (point-min))
4467 (unless (or (org-at-heading-p)
4468 (outline-next-heading))
4469 (throw 'nextfile t))
4470 (goto-char (max (point-min) (1- (point))))
4471 (while (re-search-forward regexp nil t)
4472 (org-back-to-heading t)
4473 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4474 (> (org-reduced-level (org-outline-level))
4475 org-agenda-search-view-max-outline-level)
4476 (forward-line -1)
4477 (org-back-to-heading t)))
4478 (skip-chars-forward "* ")
4479 (setq beg (point-at-bol)
4480 beg1 (point)
4481 end (progn
4482 (outline-next-heading)
4483 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4484 (> (org-reduced-level (org-outline-level))
4485 org-agenda-search-view-max-outline-level)
4486 (forward-line 1)
4487 (outline-next-heading)))
4488 (point)))
4490 (catch :skip
4491 (goto-char beg)
4492 (org-agenda-skip)
4493 (setq str (buffer-substring-no-properties
4494 (point-at-bol)
4495 (if hdl-only (point-at-eol) end)))
4496 (mapc (lambda (wr) (when (string-match wr str)
4497 (goto-char (1- end))
4498 (throw :skip t)))
4499 regexps-)
4500 (mapc (lambda (wr) (unless (string-match wr str)
4501 (goto-char (1- end))
4502 (throw :skip t)))
4503 (if todo-only
4504 (cons (concat "^\\*+[ \t]+"
4505 org-not-done-regexp)
4506 regexps+)
4507 regexps+))
4508 (goto-char beg)
4509 (setq marker (org-agenda-new-marker (point))
4510 category (org-get-category)
4511 level (make-string (org-reduced-level (org-outline-level)) ? )
4512 inherited-tags
4513 (or (eq org-agenda-show-inherited-tags 'always)
4514 (and (listp org-agenda-show-inherited-tags)
4515 (memq 'todo org-agenda-show-inherited-tags))
4516 (and (eq org-agenda-show-inherited-tags t)
4517 (or (eq org-agenda-use-tag-inheritance t)
4518 (memq 'todo org-agenda-use-tag-inheritance))))
4519 tags (org-get-tags-at nil (not inherited-tags))
4520 txt (org-agenda-format-item
4522 (buffer-substring-no-properties
4523 beg1 (point-at-eol))
4524 level category tags t))
4525 (org-add-props txt props
4526 'org-marker marker 'org-hd-marker marker
4527 'org-todo-regexp org-todo-regexp
4528 'level level
4529 'org-complex-heading-regexp org-complex-heading-regexp
4530 'priority 1000
4531 'type "search")
4532 (push txt ee)
4533 (goto-char (1- end))))))))))
4534 (setq rtn (nreverse ee))
4535 (setq rtnall (append rtnall rtn)))
4536 (if org-agenda-overriding-header
4537 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4538 nil 'face 'org-agenda-structure) "\n")
4539 (insert "Search words: ")
4540 (add-text-properties (point-min) (1- (point))
4541 (list 'face 'org-agenda-structure))
4542 (setq pos (point))
4543 (insert string "\n")
4544 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4545 (setq pos (point))
4546 (unless org-agenda-multi
4547 (insert (substitute-command-keys "\
4548 Press `\\[org-agenda-manipulate-query-add]', \
4549 `\\[org-agenda-manipulate-query-subtract]' to add/sub word, \
4550 `\\[org-agenda-manipulate-query-add-re]', \
4551 `\\[org-agenda-manipulate-query-subtract-re]' to add/sub regexp, \
4552 `\\[universal-argument] \\[org-agenda-redo]' to edit\n"))
4553 (add-text-properties pos (1- (point))
4554 (list 'face 'org-agenda-structure))))
4555 (org-agenda-mark-header-line (point-min))
4556 (when rtnall
4557 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4558 (goto-char (point-min))
4559 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4560 (add-text-properties (point-min) (point-max)
4561 `(org-agenda-type search
4562 org-last-args (,todo-only ,string ,edit-at)
4563 org-redo-cmd ,org-agenda-redo-command
4564 org-series-cmd ,org-cmd))
4565 (org-agenda-finalize)
4566 (setq buffer-read-only t))))
4568 ;;; Agenda TODO list
4570 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4571 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4572 (concat
4573 (if (or (equal keywords "ALL") (not keywords))
4574 (propertize "ALL" 'face 'warning)
4575 (mapconcat
4576 (lambda (kw)
4577 (propertize kw 'face (org-get-todo-face kw)))
4578 (org-split-string keywords "|")
4579 "|"))
4580 "\n"))
4582 (defvar org-select-this-todo-keyword nil)
4583 (defvar org-last-arg nil)
4585 ;;;###autoload
4586 (defun org-todo-list (&optional arg)
4587 "Show all (not done) TODO entries from all agenda file in a single list.
4588 The prefix arg can be used to select a specific TODO keyword and limit
4589 the list to these. When using `\\[universal-argument]', you will be prompted
4590 for a keyword. A numeric prefix directly selects the Nth keyword in
4591 `org-todo-keywords-1'."
4592 (interactive "P")
4593 (if org-agenda-overriding-arguments
4594 (setq arg org-agenda-overriding-arguments))
4595 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4596 (let* ((today (org-today))
4597 (date (calendar-gregorian-from-absolute today))
4598 (kwds org-todo-keywords-for-agenda)
4599 (completion-ignore-case t)
4600 (org-select-this-todo-keyword
4601 (if (stringp arg) arg
4602 (and arg (integerp arg) (> arg 0)
4603 (nth (1- arg) kwds))))
4604 rtn rtnall files file pos)
4605 (when (equal arg '(4))
4606 (setq org-select-this-todo-keyword
4607 (completing-read "Keyword (or KWD1|K2D2|...): "
4608 (mapcar #'list kwds) nil nil)))
4609 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4610 (catch 'exit
4611 (if org-agenda-sticky
4612 (setq org-agenda-buffer-name
4613 (if (stringp org-select-this-todo-keyword)
4614 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4615 org-select-this-todo-keyword)
4616 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4617 (org-agenda-prepare "TODO")
4618 (org-compile-prefix-format 'todo)
4619 (org-set-sorting-strategy 'todo)
4620 (setq org-agenda-redo-command
4621 `(org-todo-list (or (and (numberp current-prefix-arg)
4622 current-prefix-arg)
4623 ,org-select-this-todo-keyword
4624 current-prefix-arg ,arg)))
4625 (setq files (org-agenda-files nil 'ifmode)
4626 rtnall nil)
4627 (while (setq file (pop files))
4628 (catch 'nextfile
4629 (org-check-agenda-file file)
4630 (setq rtn (org-agenda-get-day-entries file date :todo))
4631 (setq rtnall (append rtnall rtn))))
4632 (if org-agenda-overriding-header
4633 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4634 nil 'face 'org-agenda-structure) "\n")
4635 (insert "Global list of TODO items of type: ")
4636 (add-text-properties (point-min) (1- (point))
4637 (list 'face 'org-agenda-structure
4638 'short-heading
4639 (concat "ToDo: "
4640 (or org-select-this-todo-keyword "ALL"))))
4641 (org-agenda-mark-header-line (point-min))
4642 (insert (org-agenda-propertize-selected-todo-keywords
4643 org-select-this-todo-keyword))
4644 (setq pos (point))
4645 (unless org-agenda-multi
4646 (insert (substitute-command-keys "Available with \
4647 `N \\[org-agenda-redo]': (0)[ALL]"))
4648 (let ((n 0) s)
4649 (mapc (lambda (x)
4650 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4651 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4652 (insert "\n "))
4653 (insert " " s))
4654 kwds))
4655 (insert "\n"))
4656 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4657 (org-agenda-mark-header-line (point-min))
4658 (when rtnall
4659 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4660 (goto-char (point-min))
4661 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4662 (add-text-properties (point-min) (point-max)
4663 `(org-agenda-type todo
4664 org-last-args ,arg
4665 org-redo-cmd ,org-agenda-redo-command
4666 org-series-cmd ,org-cmd))
4667 (org-agenda-finalize)
4668 (setq buffer-read-only t))))
4670 ;;; Agenda tags match
4672 ;;;###autoload
4673 (defun org-tags-view (&optional todo-only match)
4674 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4675 The prefix arg TODO-ONLY limits the search to TODO entries."
4676 (interactive "P")
4677 (if org-agenda-overriding-arguments
4678 (setq todo-only (car org-agenda-overriding-arguments)
4679 match (nth 1 org-agenda-overriding-arguments)))
4680 (let* ((org-tags-match-list-sublevels
4681 org-tags-match-list-sublevels)
4682 (completion-ignore-case t)
4683 rtn rtnall files file pos matcher
4684 buffer)
4685 (when (and (stringp match) (not (string-match "\\S-" match)))
4686 (setq match nil))
4687 (catch 'exit
4688 (if org-agenda-sticky
4689 (setq org-agenda-buffer-name
4690 (if (stringp match)
4691 (format "*Org Agenda(%s:%s)*"
4692 (or org-keys (or (and todo-only "M") "m")) match)
4693 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4694 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4695 ;; expanding tags within `org-make-tags-matcher'
4696 (org-agenda-prepare (concat "TAGS " match))
4697 (setq org--matcher-tags-todo-only todo-only
4698 matcher (org-make-tags-matcher match)
4699 match (car matcher)
4700 matcher (cdr matcher))
4701 (org-compile-prefix-format 'tags)
4702 (org-set-sorting-strategy 'tags)
4703 (setq org-agenda-query-string match)
4704 (setq org-agenda-redo-command
4705 (list 'org-tags-view
4706 `(quote ,org--matcher-tags-todo-only)
4707 `(if current-prefix-arg nil ,org-agenda-query-string)))
4708 (setq files (org-agenda-files nil 'ifmode)
4709 rtnall nil)
4710 (while (setq file (pop files))
4711 (catch 'nextfile
4712 (org-check-agenda-file file)
4713 (setq buffer (if (file-exists-p file)
4714 (org-get-agenda-file-buffer file)
4715 (error "No such file %s" file)))
4716 (if (not buffer)
4717 ;; If file does not exist, error message to agenda
4718 (setq rtn (list
4719 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4720 rtnall (append rtnall rtn))
4721 (with-current-buffer buffer
4722 (unless (derived-mode-p 'org-mode)
4723 (error "Agenda file %s is not in `org-mode'" file))
4724 (save-excursion
4725 (save-restriction
4726 (if (eq buffer org-agenda-restrict)
4727 (narrow-to-region org-agenda-restrict-begin
4728 org-agenda-restrict-end)
4729 (widen))
4730 (setq rtn (org-scan-tags 'agenda
4731 matcher
4732 org--matcher-tags-todo-only))
4733 (setq rtnall (append rtnall rtn))))))))
4734 (if org-agenda-overriding-header
4735 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4736 nil 'face 'org-agenda-structure) "\n")
4737 (insert "Headlines with TAGS match: ")
4738 (add-text-properties (point-min) (1- (point))
4739 (list 'face 'org-agenda-structure
4740 'short-heading
4741 (concat "Match: " match)))
4742 (setq pos (point))
4743 (insert match "\n")
4744 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4745 (setq pos (point))
4746 (unless org-agenda-multi
4747 (insert (substitute-command-keys
4748 "Press `\\[universal-argument] \\[org-agenda-redo]' \
4749 to search again with new search string\n")))
4750 (add-text-properties pos (1- (point))
4751 (list 'face 'org-agenda-structure)))
4752 (org-agenda-mark-header-line (point-min))
4753 (when rtnall
4754 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4755 (goto-char (point-min))
4756 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4757 (add-text-properties
4758 (point-min) (point-max)
4759 `(org-agenda-type tags
4760 org-last-args (,org--matcher-tags-todo-only ,match)
4761 org-redo-cmd ,org-agenda-redo-command
4762 org-series-cmd ,org-cmd))
4763 (org-agenda-finalize)
4764 (setq buffer-read-only t))))
4766 ;;; Agenda Finding stuck projects
4768 (defvar org-agenda-skip-regexp nil
4769 "Regular expression used in skipping subtrees for the agenda.
4770 This is basically a temporary global variable that can be set and then
4771 used by user-defined selections using `org-agenda-skip-function'.")
4773 (defvar org-agenda-overriding-header nil
4774 "When set during agenda, todo and tags searches it replaces the header.
4775 This variable should not be set directly, but custom commands can bind it
4776 in the options section.")
4778 (defun org-agenda-skip-entry-if (&rest conditions)
4779 "Skip entry if any of CONDITIONS is true.
4780 See `org-agenda-skip-if' for details."
4781 (org-agenda-skip-if nil conditions))
4783 (defun org-agenda-skip-subtree-if (&rest conditions)
4784 "Skip subtree if any of CONDITIONS is true.
4785 See `org-agenda-skip-if' for details."
4786 (org-agenda-skip-if t conditions))
4788 (defun org-agenda-skip-if (subtree conditions)
4789 "Checks current entity for CONDITIONS.
4790 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4791 the entry (i.e. the text before the next heading) is checked.
4793 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4794 from different tests. Valid conditions are:
4796 scheduled Check if there is a scheduled cookie
4797 notscheduled Check if there is no scheduled cookie
4798 deadline Check if there is a deadline
4799 notdeadline Check if there is no deadline
4800 timestamp Check if there is a timestamp (also deadline or scheduled)
4801 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4802 regexp Check if regexp matches
4803 notregexp Check if regexp does not match.
4804 todo Check if TODO keyword matches
4805 nottodo Check if TODO keyword does not match
4807 The regexp is taken from the conditions list, it must come right after
4808 the `regexp' or `notregexp' element.
4810 `todo' and `nottodo' accept as an argument a list of todo
4811 keywords, which may include \"*\" to match any todo keyword.
4813 (org-agenda-skip-entry-if \\='todo \\='(\"TODO\" \"WAITING\"))
4815 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4817 Instead of a list, a keyword class may be given. For example:
4819 (org-agenda-skip-entry-if \\='nottodo \\='done)
4821 would skip entries that haven't been marked with any of \"DONE\"
4822 keywords. Possible classes are: `todo', `done', `any'.
4824 If any of these conditions is met, this function returns the end point of
4825 the entity, causing the search to continue from there. This is a function
4826 that can be put into `org-agenda-skip-function' for the duration of a command."
4827 (let (beg end m)
4828 (org-back-to-heading t)
4829 (setq beg (point)
4830 end (if subtree
4831 (progn (org-end-of-subtree t) (point))
4832 (progn (outline-next-heading) (1- (point)))))
4833 (goto-char beg)
4834 (and
4836 (and (memq 'scheduled conditions)
4837 (re-search-forward org-scheduled-time-regexp end t))
4838 (and (memq 'notscheduled conditions)
4839 (not (re-search-forward org-scheduled-time-regexp end t)))
4840 (and (memq 'deadline conditions)
4841 (re-search-forward org-deadline-time-regexp end t))
4842 (and (memq 'notdeadline conditions)
4843 (not (re-search-forward org-deadline-time-regexp end t)))
4844 (and (memq 'timestamp conditions)
4845 (re-search-forward org-ts-regexp end t))
4846 (and (memq 'nottimestamp conditions)
4847 (not (re-search-forward org-ts-regexp end t)))
4848 (and (setq m (memq 'regexp conditions))
4849 (stringp (nth 1 m))
4850 (re-search-forward (nth 1 m) end t))
4851 (and (setq m (memq 'notregexp conditions))
4852 (stringp (nth 1 m))
4853 (not (re-search-forward (nth 1 m) end t)))
4854 (and (or
4855 (setq m (memq 'nottodo conditions))
4856 (setq m (memq 'todo-unblocked conditions))
4857 (setq m (memq 'nottodo-unblocked conditions))
4858 (setq m (memq 'todo conditions)))
4859 (org-agenda-skip-if-todo m end)))
4860 end)))
4862 (defun org-agenda-skip-if-todo (args end)
4863 "Helper function for `org-agenda-skip-if', do not use it directly.
4864 ARGS is a list with first element either `todo', `nottodo',
4865 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
4866 a list of TODO keywords, or a state symbol `todo' or `done' or
4867 `any'."
4868 (let ((kw (car args))
4869 (arg (cadr args))
4870 todo-wds todo-re)
4871 (setq todo-wds
4872 (org-uniquify
4873 (cond
4874 ((listp arg) ;; list of keywords
4875 (if (member "*" arg)
4876 (mapcar 'substring-no-properties org-todo-keywords-1)
4877 arg))
4878 ((symbolp arg) ;; keyword class name
4879 (cond
4880 ((eq arg 'todo)
4881 (org-delete-all org-done-keywords
4882 (mapcar 'substring-no-properties
4883 org-todo-keywords-1)))
4884 ((eq arg 'done) org-done-keywords)
4885 ((eq arg 'any)
4886 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4887 (setq todo-re
4888 (concat "^\\*+[ \t]+\\<\\("
4889 (mapconcat 'identity todo-wds "\\|")
4890 "\\)\\>"))
4891 (cond
4892 ((eq kw 'todo) (re-search-forward todo-re end t))
4893 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
4894 ((eq kw 'todo-unblocked)
4895 (catch 'unblocked
4896 (while (re-search-forward todo-re end t)
4897 (or (org-entry-blocked-p) (throw 'unblocked t)))
4898 nil))
4899 ((eq kw 'nottodo-unblocked)
4900 (catch 'unblocked
4901 (while (re-search-forward todo-re end t)
4902 (or (org-entry-blocked-p) (throw 'unblocked nil)))
4906 ;;;###autoload
4907 (defun org-agenda-list-stuck-projects (&rest ignore)
4908 "Create agenda view for projects that are stuck.
4909 Stuck projects are project that have no next actions. For the definitions
4910 of what a project is and how to check if it stuck, customize the variable
4911 `org-stuck-projects'."
4912 (interactive)
4913 (let* ((org-agenda-overriding-header
4914 (or org-agenda-overriding-header "List of stuck projects: "))
4915 (matcher (nth 0 org-stuck-projects))
4916 (todo (nth 1 org-stuck-projects))
4917 (tags (nth 2 org-stuck-projects))
4918 (gen-re (org-string-nw-p (nth 3 org-stuck-projects)))
4919 (todo-wds
4920 (if (not (member "*" todo)) todo
4921 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
4922 (org-delete-all org-done-keywords-for-agenda
4923 (copy-sequence org-todo-keywords-for-agenda))))
4924 (todo-re (and todo
4925 (format "^\\*+[ \t]+\\(%s\\)\\>"
4926 (mapconcat #'identity todo-wds "\\|"))))
4927 (tags-re (cond ((null tags) nil)
4928 ((member "*" tags)
4929 (eval-when-compile
4930 (concat org-outline-regexp-bol
4931 ".*:[[:alnum:]_@#%]+:[ \t]*$")))
4932 (tags (concat org-outline-regexp-bol
4933 ".*:\\("
4934 (mapconcat #'identity tags "\\|")
4935 "\\):[[:alnum:]_@#%:]*[ \t]*$"))
4936 (t nil)))
4937 (re-list (delq nil (list todo-re tags-re gen-re)))
4938 (skip-re
4939 (if (null re-list)
4940 (error "Missing information to identify unstuck projects")
4941 (mapconcat #'identity re-list "\\|")))
4942 (org-agenda-skip-function
4943 ;; Skip entry if `org-agenda-skip-regexp' matches anywhere
4944 ;; in the subtree.
4945 `(lambda ()
4946 (and (save-excursion
4947 (let ((case-fold-search nil))
4948 (re-search-forward
4949 ,skip-re (save-excursion (org-end-of-subtree t)) t)))
4950 (progn (outline-next-heading) (point))))))
4951 (org-tags-view nil matcher)
4952 (setq org-agenda-buffer-name (buffer-name))
4953 (with-current-buffer org-agenda-buffer-name
4954 (setq org-agenda-redo-command
4955 `(org-agenda-list-stuck-projects ,current-prefix-arg)))))
4957 ;;; Diary integration
4959 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
4960 (defvar diary-list-entries-hook)
4961 (defvar diary-time-regexp)
4962 (defun org-get-entries-from-diary (date)
4963 "Get the (Emacs Calendar) diary entries for DATE."
4964 (require 'diary-lib)
4965 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
4966 (diary-display-function 'diary-fancy-display)
4967 (pop-up-frames nil)
4968 (diary-list-entries-hook
4969 (cons 'org-diary-default-entry diary-list-entries-hook))
4970 (diary-file-name-prefix nil) ; turn this feature off
4971 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
4972 entries
4973 (org-disable-agenda-to-diary t))
4974 (save-excursion
4975 (save-window-excursion
4976 (funcall (if (fboundp 'diary-list-entries)
4977 'diary-list-entries 'list-diary-entries)
4978 date 1)))
4979 (if (not (get-buffer diary-fancy-buffer))
4980 (setq entries nil)
4981 (with-current-buffer diary-fancy-buffer
4982 (setq buffer-read-only nil)
4983 (if (zerop (buffer-size))
4984 ;; No entries
4985 (setq entries nil)
4986 ;; Omit the date and other unnecessary stuff
4987 (org-agenda-cleanup-fancy-diary)
4988 ;; Add prefix to each line and extend the text properties
4989 (if (zerop (buffer-size))
4990 (setq entries nil)
4991 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
4992 (setq entries
4993 (with-temp-buffer
4994 (insert entries) (goto-char (point-min))
4995 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
4996 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
4997 (replace-match (concat "; " (match-string 1)))))
4998 (buffer-string)))))
4999 (set-buffer-modified-p nil)
5000 (kill-buffer diary-fancy-buffer)))
5001 (when entries
5002 (setq entries (org-split-string entries "\n"))
5003 (setq entries
5004 (mapcar
5005 (lambda (x)
5006 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5007 ;; Extend the text properties to the beginning of the line
5008 (org-add-props x (text-properties-at (1- (length x)) x)
5009 'type "diary" 'date date 'face 'org-agenda-diary))
5010 entries)))))
5012 (defvar org-agenda-cleanup-fancy-diary-hook nil
5013 "Hook run when the fancy diary buffer is cleaned up.")
5015 (defun org-agenda-cleanup-fancy-diary ()
5016 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5017 This gets rid of the date, the underline under the date, and
5018 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5019 date. It also removes lines that contain only whitespace."
5020 (goto-char (point-min))
5021 (if (looking-at ".*?:[ \t]*")
5022 (progn
5023 (replace-match "")
5024 (re-search-forward "\n=+$" nil t)
5025 (replace-match "")
5026 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5027 (re-search-forward "\n=+$" nil t)
5028 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5029 (goto-char (point-min))
5030 (while (re-search-forward "^ +\n" nil t)
5031 (replace-match ""))
5032 (goto-char (point-min))
5033 (if (re-search-forward "^Org mode dummy\n?" nil t)
5034 (replace-match ""))
5035 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5037 ;; Make sure entries from the diary have the right text properties.
5038 (eval-after-load "diary-lib"
5039 '(if (boundp 'diary-modify-entry-list-string-function)
5040 ;; We can rely on the hook, nothing to do
5042 ;; Hook not available, must use advice to make this work
5043 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5044 "Make the position visible."
5045 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5046 (stringp string)
5047 buffer-file-name)
5048 (setq string (org-modify-diary-entry-string string))))))
5050 (defun org-modify-diary-entry-string (string)
5051 "Add text properties to string, allowing Org to act on it."
5052 (org-add-props string nil
5053 'mouse-face 'highlight
5054 'help-echo (if buffer-file-name
5055 (format "mouse-2 or RET jump to diary file %s"
5056 (abbreviate-file-name buffer-file-name))
5058 'org-agenda-diary-link t
5059 'org-marker (org-agenda-new-marker (point-at-bol))))
5061 (defun org-diary-default-entry ()
5062 "Add a dummy entry to the diary.
5063 Needed to avoid empty dates which mess up holiday display."
5064 ;; Catch the error if dealing with the new add-to-diary-alist
5065 (when org-disable-agenda-to-diary
5066 (condition-case nil
5067 (org-add-to-diary-list original-date "Org mode dummy" "")
5068 (error
5069 (org-add-to-diary-list original-date "Org mode dummy" "" nil)))))
5071 (defun org-add-to-diary-list (&rest args)
5072 (if (fboundp 'diary-add-to-list)
5073 (apply 'diary-add-to-list args)
5074 (apply 'add-to-diary-list args)))
5076 (defvar org-diary-last-run-time nil)
5078 ;;;###autoload
5079 (defun org-diary (&rest args)
5080 "Return diary information from org files.
5081 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5082 It accesses org files and extracts information from those files to be
5083 listed in the diary. The function accepts arguments specifying what
5084 items should be listed. For a list of arguments allowed here, see the
5085 variable `org-agenda-entry-types'.
5087 The call in the diary file should look like this:
5089 &%%(org-diary) ~/path/to/some/orgfile.org
5091 Use a separate line for each org file to check. Or, if you omit the file name,
5092 all files listed in `org-agenda-files' will be checked automatically:
5094 &%%(org-diary)
5096 If you don't give any arguments (as in the example above), the default value
5097 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5098 So the example above may also be written as
5100 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5102 The function expects the lisp variables `entry' and `date' to be provided
5103 by the caller, because this is how the calendar works. Don't use this
5104 function from a program - use `org-agenda-get-day-entries' instead."
5105 (when (> (- (float-time)
5106 org-agenda-last-marker-time)
5108 ;; I am not sure if this works with sticky agendas, because the marker
5109 ;; list is then no longer a global variable.
5110 (org-agenda-reset-markers))
5111 (org-compile-prefix-format 'agenda)
5112 (org-set-sorting-strategy 'agenda)
5113 (setq args (or args org-agenda-entry-types))
5114 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5115 (list entry)
5116 (org-agenda-files t)))
5117 (time (float-time))
5118 file rtn results)
5119 (when (or (not org-diary-last-run-time)
5120 (> (- time
5121 org-diary-last-run-time)
5123 (org-agenda-prepare-buffers files))
5124 (setq org-diary-last-run-time time)
5125 ;; If this is called during org-agenda, don't return any entries to
5126 ;; the calendar. Org Agenda will list these entries itself.
5127 (if org-disable-agenda-to-diary (setq files nil))
5128 (while (setq file (pop files))
5129 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5130 (setq results (append results rtn)))
5131 (when results
5132 (setq results
5133 (mapcar (lambda (i) (replace-regexp-in-string
5134 org-bracket-link-regexp "\\3" i)) results))
5135 (concat (org-agenda-finalize-entries results) "\n"))))
5137 ;;; Agenda entry finders
5139 (defun org-agenda--timestamp-to-absolute (&rest args)
5140 "Call `org-time-string-to-absolute' with ARGS.
5141 However, throw `:skip' whenever an error is raised."
5142 (condition-case e
5143 (apply #'org-time-string-to-absolute args)
5144 (org-diary-sexp-no-match (throw :skip nil))
5145 (error
5146 (message "%s; Skipping entry" (error-message-string e))
5147 (throw :skip nil))))
5149 (defun org-agenda-get-day-entries (file date &rest args)
5150 "Does the work for `org-diary' and `org-agenda'.
5151 FILE is the path to a file to be checked for entries. DATE is date like
5152 the one returned by `calendar-current-date'. ARGS are symbols indicating
5153 which kind of entries should be extracted. For details about these, see
5154 the documentation of `org-diary'."
5155 (let* ((org-startup-folded nil)
5156 (org-startup-align-all-tables nil)
5157 (buffer (if (file-exists-p file) (org-get-agenda-file-buffer file)
5158 (error "No such file %s" file))))
5159 (if (not buffer)
5160 ;; If file does not exist, signal it in diary nonetheless.
5161 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5162 (with-current-buffer buffer
5163 (unless (derived-mode-p 'org-mode)
5164 (error "Agenda file %s is not in `org-mode'" file))
5165 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5166 (setf org-agenda-current-date date)
5167 (save-excursion
5168 (save-restriction
5169 (if (eq buffer org-agenda-restrict)
5170 (narrow-to-region org-agenda-restrict-begin
5171 org-agenda-restrict-end)
5172 (widen))
5173 ;; Rationalize ARGS. Also make sure `:deadline' comes
5174 ;; first in order to populate DEADLINES before passing it.
5176 ;; We use `delq' since `org-uniquify' duplicates ARGS,
5177 ;; guarding us from modifying `org-agenda-entry-types'.
5178 (setf args (org-uniquify (or args org-agenda-entry-types)))
5179 (when (and (memq :scheduled args) (memq :scheduled* args))
5180 (setf args (delq :scheduled* args)))
5181 (cond
5182 ((memq :deadline args)
5183 (setf args (cons :deadline
5184 (delq :deadline (delq :deadline* args)))))
5185 ((memq :deadline* args)
5186 (setf args (cons :deadline* (delq :deadline* args)))))
5187 ;; Collect list of headlines. Return them flattened.
5188 (let ((case-fold-search nil) results deadlines)
5189 (dolist (arg args (apply #'nconc (nreverse results)))
5190 (pcase arg
5191 ((and :todo (guard (org-agenda-today-p date)))
5192 (push (org-agenda-get-todos) results))
5193 (:timestamp
5194 (push (org-agenda-get-blocks) results)
5195 (push (org-agenda-get-timestamps deadlines) results))
5196 (:sexp
5197 (push (org-agenda-get-sexps) results))
5198 (:scheduled
5199 (push (org-agenda-get-scheduled deadlines) results))
5200 (:scheduled*
5201 (push (org-agenda-get-scheduled deadlines t) results))
5202 (:closed
5203 (push (org-agenda-get-progress) results))
5204 (:deadline
5205 (setf deadlines (org-agenda-get-deadlines))
5206 (push deadlines results))
5207 (:deadline*
5208 (setf deadlines (org-agenda-get-deadlines t))
5209 (push deadlines results)))))))))))
5211 (defsubst org-em (x y list)
5212 "Is X or Y a member of LIST?"
5213 (or (memq x list) (memq y list)))
5215 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5216 (defvar org-agenda-sorting-strategy-selected nil)
5218 (defun org-agenda-entry-get-agenda-timestamp (pom)
5219 "Retrieve timestamp information for sorting agenda views.
5220 Given a point or marker POM, returns a cons cell of the timestamp
5221 and the timestamp type relevant for the sorting strategy in
5222 `org-agenda-sorting-strategy-selected'."
5223 (let (ts ts-date-type)
5224 (save-match-data
5225 (cond ((org-em 'scheduled-up 'scheduled-down
5226 org-agenda-sorting-strategy-selected)
5227 (setq ts (org-entry-get pom "SCHEDULED")
5228 ts-date-type " scheduled"))
5229 ((org-em 'deadline-up 'deadline-down
5230 org-agenda-sorting-strategy-selected)
5231 (setq ts (org-entry-get pom "DEADLINE")
5232 ts-date-type " deadline"))
5233 ((org-em 'ts-up 'ts-down
5234 org-agenda-sorting-strategy-selected)
5235 (setq ts (org-entry-get pom "TIMESTAMP")
5236 ts-date-type " timestamp"))
5237 ((org-em 'tsia-up 'tsia-down
5238 org-agenda-sorting-strategy-selected)
5239 (setq ts (org-entry-get pom "TIMESTAMP_IA")
5240 ts-date-type " timestamp_ia"))
5241 ((org-em 'timestamp-up 'timestamp-down
5242 org-agenda-sorting-strategy-selected)
5243 (setq ts (or (org-entry-get pom "SCHEDULED")
5244 (org-entry-get pom "DEADLINE")
5245 (org-entry-get pom "TIMESTAMP")
5246 (org-entry-get pom "TIMESTAMP_IA"))
5247 ts-date-type ""))
5248 (t (setq ts-date-type "")))
5249 (cons (when ts (ignore-errors (org-time-string-to-absolute ts)))
5250 ts-date-type))))
5252 (defun org-agenda-get-todos ()
5253 "Return the TODO information for agenda display."
5254 (let* ((props (list 'face nil
5255 'done-face 'org-agenda-done
5256 'org-not-done-regexp org-not-done-regexp
5257 'org-todo-regexp org-todo-regexp
5258 'org-complex-heading-regexp org-complex-heading-regexp
5259 'mouse-face 'highlight
5260 'help-echo
5261 (format "mouse-2 or RET jump to org file %s"
5262 (abbreviate-file-name buffer-file-name))))
5263 (case-fold-search nil)
5264 (regexp (format org-heading-keyword-regexp-format
5265 (cond
5266 ((and org-select-this-todo-keyword
5267 (equal org-select-this-todo-keyword "*"))
5268 org-todo-regexp)
5269 (org-select-this-todo-keyword
5270 (concat "\\("
5271 (mapconcat 'identity
5272 (org-split-string
5273 org-select-this-todo-keyword
5274 "|")
5275 "\\|") "\\)"))
5276 (t org-not-done-regexp))))
5277 marker priority category level tags todo-state
5278 ts-date ts-date-type ts-date-pair
5279 ee txt beg end inherited-tags todo-state-end-pos)
5280 (goto-char (point-min))
5281 (while (re-search-forward regexp nil t)
5282 (catch :skip
5283 (save-match-data
5284 (beginning-of-line)
5285 (org-agenda-skip)
5286 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5287 (unless (and (setq todo-state (org-get-todo-state))
5288 (setq todo-state-end-pos (match-end 2)))
5289 (goto-char end)
5290 (throw :skip nil))
5291 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5292 (goto-char (1+ beg))
5293 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5294 (throw :skip nil)))
5295 (goto-char (match-beginning 2))
5296 (setq marker (org-agenda-new-marker (match-beginning 0))
5297 category (org-get-category)
5298 ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
5299 ts-date (car ts-date-pair)
5300 ts-date-type (cdr ts-date-pair)
5301 txt (org-trim (buffer-substring (match-beginning 2) (match-end 0)))
5302 inherited-tags
5303 (or (eq org-agenda-show-inherited-tags 'always)
5304 (and (listp org-agenda-show-inherited-tags)
5305 (memq 'todo org-agenda-show-inherited-tags))
5306 (and (eq org-agenda-show-inherited-tags t)
5307 (or (eq org-agenda-use-tag-inheritance t)
5308 (memq 'todo org-agenda-use-tag-inheritance))))
5309 tags (org-get-tags-at nil (not inherited-tags))
5310 level (make-string (org-reduced-level (org-outline-level)) ? )
5311 txt (org-agenda-format-item "" txt level category tags t)
5312 priority (1+ (org-get-priority txt)))
5313 (org-add-props txt props
5314 'org-marker marker 'org-hd-marker marker
5315 'priority priority
5316 'level level
5317 'ts-date ts-date
5318 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5319 (push txt ee)
5320 (if org-agenda-todo-list-sublevels
5321 (goto-char todo-state-end-pos)
5322 (org-end-of-subtree 'invisible))))
5323 (nreverse ee)))
5325 (defun org-agenda-todo-custom-ignore-p (time n)
5326 "Check whether timestamp is farther away than n number of days.
5327 This function is invoked if `org-agenda-todo-ignore-deadlines',
5328 `org-agenda-todo-ignore-scheduled' or
5329 `org-agenda-todo-ignore-timestamp' is set to an integer."
5330 (let ((days (org-time-stamp-to-now
5331 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5332 (if (>= n 0)
5333 (>= days n)
5334 (<= days n))))
5336 ;;;###autoload
5337 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5338 (&optional end)
5339 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5340 (when (or org-agenda-todo-ignore-with-date
5341 org-agenda-todo-ignore-scheduled
5342 org-agenda-todo-ignore-deadlines
5343 org-agenda-todo-ignore-timestamp)
5344 (setq end (or end (save-excursion (outline-next-heading) (point))))
5345 (save-excursion
5346 (or (and org-agenda-todo-ignore-with-date
5347 (re-search-forward org-ts-regexp end t))
5348 (and org-agenda-todo-ignore-scheduled
5349 (re-search-forward org-scheduled-time-regexp end t)
5350 (cond
5351 ((eq org-agenda-todo-ignore-scheduled 'future)
5352 (> (org-time-stamp-to-now
5353 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5354 ((eq org-agenda-todo-ignore-scheduled 'past)
5355 (<= (org-time-stamp-to-now
5356 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5357 ((numberp org-agenda-todo-ignore-scheduled)
5358 (org-agenda-todo-custom-ignore-p
5359 (match-string 1) org-agenda-todo-ignore-scheduled))
5360 (t)))
5361 (and org-agenda-todo-ignore-deadlines
5362 (re-search-forward org-deadline-time-regexp end t)
5363 (cond
5364 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5365 ((eq org-agenda-todo-ignore-deadlines 'far)
5366 (not (org-deadline-close-p (match-string 1))))
5367 ((eq org-agenda-todo-ignore-deadlines 'future)
5368 (> (org-time-stamp-to-now
5369 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5370 ((eq org-agenda-todo-ignore-deadlines 'past)
5371 (<= (org-time-stamp-to-now
5372 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5373 ((numberp org-agenda-todo-ignore-deadlines)
5374 (org-agenda-todo-custom-ignore-p
5375 (match-string 1) org-agenda-todo-ignore-deadlines))
5376 (t (org-deadline-close-p (match-string 1)))))
5377 (and org-agenda-todo-ignore-timestamp
5378 (let ((buffer (current-buffer))
5379 (regexp
5380 (concat
5381 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5382 (start (point)))
5383 ;; Copy current buffer into a temporary one
5384 (with-temp-buffer
5385 (insert-buffer-substring buffer start end)
5386 (goto-char (point-min))
5387 ;; Delete SCHEDULED and DEADLINE items
5388 (while (re-search-forward regexp end t)
5389 (delete-region (match-beginning 0) (match-end 0)))
5390 (goto-char (point-min))
5391 ;; No search for timestamp left
5392 (when (re-search-forward org-ts-regexp nil t)
5393 (cond
5394 ((eq org-agenda-todo-ignore-timestamp 'future)
5395 (> (org-time-stamp-to-now
5396 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5397 ((eq org-agenda-todo-ignore-timestamp 'past)
5398 (<= (org-time-stamp-to-now
5399 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5400 ((numberp org-agenda-todo-ignore-timestamp)
5401 (org-agenda-todo-custom-ignore-p
5402 (match-string 1) org-agenda-todo-ignore-timestamp))
5403 (t))))))))))
5405 (defun org-agenda-get-timestamps (&optional deadlines)
5406 "Return the date stamp information for agenda display.
5407 Optional argument DEADLINES is a list of deadline items to be
5408 displayed in agenda view."
5409 (let* ((props (list 'face 'org-agenda-calendar-event
5410 'org-not-done-regexp org-not-done-regexp
5411 'org-todo-regexp org-todo-regexp
5412 'org-complex-heading-regexp org-complex-heading-regexp
5413 'mouse-face 'highlight
5414 'help-echo
5415 (format "mouse-2 or RET jump to Org file %s"
5416 (abbreviate-file-name buffer-file-name))))
5417 (current (calendar-absolute-from-gregorian date))
5418 (today (org-today))
5419 (deadline-position-alist
5420 (mapcar (lambda (d)
5421 (let ((m (get-text-property 0 'org-hd-marker d)))
5422 (and m (marker-position m))))
5423 deadlines))
5424 ;; Match time-stamps set to current date, time-stamps with
5425 ;; a repeater, and S-exp time-stamps.
5426 (regexp
5427 (concat
5428 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5429 (regexp-quote
5430 (substring
5431 (format-time-string
5432 (car org-time-stamp-formats)
5433 (apply #'encode-time ; DATE bound by calendar
5434 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5435 1 11))
5436 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5437 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5438 timestamp-items)
5439 (goto-char (point-min))
5440 (while (re-search-forward regexp nil t)
5441 ;; Skip date ranges, scheduled and deadlines, which are handled
5442 ;; specially. Also skip time-stamps before first headline as
5443 ;; there would be no entry to add to the agenda. Eventually,
5444 ;; ignore clock entries.
5445 (catch :skip
5446 (save-match-data
5447 (when (or (org-at-date-range-p)
5448 (org-at-planning-p)
5449 (org-before-first-heading-p)
5450 (and org-agenda-include-inactive-timestamps
5451 (org-at-clock-log-p)))
5452 (throw :skip nil))
5453 (org-agenda-skip))
5454 (let* ((pos (match-beginning 0))
5455 (repeat (match-string 1))
5456 (sexp-entry (match-string 3))
5457 (time-stamp (if (or repeat sexp-entry) (match-string 0)
5458 (save-excursion
5459 (goto-char pos)
5460 (looking-at org-ts-regexp-both)
5461 (match-string 0))))
5462 (todo-state (org-get-todo-state))
5463 (warntime (get-text-property (point) 'org-appt-warntime))
5464 (done? (member todo-state org-done-keywords)))
5465 ;; Possibly skip done tasks.
5466 (when (and done? org-agenda-skip-timestamp-if-done)
5467 (throw :skip t))
5468 ;; S-exp entry doesn't match current day: skip it.
5469 (when (and sexp-entry (not (org-diary-sexp-entry sexp-entry "" date)))
5470 (throw :skip nil))
5471 ;; A repeating time stamp is shown at its base date, and at
5472 ;; every repeated date in the future.
5473 (when repeat
5474 (let* ((past
5475 (if (or (eq org-agenda-prefer-last-repeat t)
5476 (member todo-state org-agenda-prefer-last-repeat))
5477 (org-agenda--timestamp-to-absolute
5478 repeat today 'past (current-buffer) pos)
5479 (org-agenda--timestamp-to-absolute repeat)))
5480 (future
5481 (cond
5482 ((<= current today) past)
5483 ((not org-agenda-show-future-repeats) past)
5485 (let ((base (if (eq org-agenda-show-future-repeats 'next)
5486 (1+ today)
5487 current)))
5488 (org-agenda--timestamp-to-absolute
5489 repeat base 'future (current-buffer) pos))))))
5490 (when (and (/= current past) (/= current future))
5491 (throw :skip nil))))
5492 (save-excursion
5493 (re-search-backward org-outline-regexp-bol nil t)
5494 ;; Possibly skip time-stamp when a deadline is set.
5495 (when (and org-agenda-skip-timestamp-if-deadline-is-shown
5496 (assq (point) deadline-position-alist))
5497 (throw :skip nil))
5498 (let* ((category (org-get-category pos))
5499 (inherited-tags
5500 (or (eq org-agenda-show-inherited-tags 'always)
5501 (and (consp org-agenda-show-inherited-tags)
5502 (memq 'agenda org-agenda-show-inherited-tags))
5503 (and (eq org-agenda-show-inherited-tags t)
5504 (or (eq org-agenda-use-tag-inheritance t)
5505 (memq 'agenda
5506 org-agenda-use-tag-inheritance)))))
5507 (tags (org-get-tags-at nil (not inherited-tags)))
5508 (level (make-string (org-reduced-level (org-outline-level))
5509 ?\s))
5510 (head (and (looking-at "\\*+[ \t]+\\(.*\\)")
5511 (match-string 1)))
5512 (inactive? (= (char-after pos) ?\[))
5513 (habit? (and (fboundp 'org-is-habit-p) (org-is-habit-p)))
5514 (item
5515 (org-agenda-format-item
5516 (and inactive? org-agenda-inactive-leader)
5517 head level category tags time-stamp org-ts-regexp habit?)))
5518 (org-add-props item props
5519 'priority (if habit?
5520 (org-habit-get-priority (org-habit-parse-todo))
5521 (org-get-priority item))
5522 'org-marker (org-agenda-new-marker pos)
5523 'org-hd-marker (org-agenda-new-marker)
5524 'date date
5525 'level level
5526 'ts-date (if repeat (org-agenda--timestamp-to-absolute repeat)
5527 current)
5528 'todo-state todo-state
5529 'warntime warntime
5530 'type "timestamp")
5531 (push item timestamp-items))))
5532 (when org-agenda-skip-additional-timestamps-same-entry
5533 (outline-next-heading))))
5534 (nreverse timestamp-items)))
5536 (defun org-agenda-get-sexps ()
5537 "Return the sexp information for agenda display."
5538 (require 'diary-lib)
5539 (let* ((props (list 'face 'org-agenda-calendar-sexp
5540 'mouse-face 'highlight
5541 'help-echo
5542 (format "mouse-2 or RET jump to org file %s"
5543 (abbreviate-file-name buffer-file-name))))
5544 (regexp "^&?%%(")
5545 marker category extra level ee txt tags entry
5546 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5547 (goto-char (point-min))
5548 (while (re-search-forward regexp nil t)
5549 (catch :skip
5550 (org-agenda-skip)
5551 (setq beg (match-beginning 0))
5552 (goto-char (1- (match-end 0)))
5553 (setq b (point))
5554 (forward-sexp 1)
5555 (setq sexp (buffer-substring b (point)))
5556 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5557 (org-trim (match-string 1))
5558 ""))
5559 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5560 (when result
5561 (setq marker (org-agenda-new-marker beg)
5562 level (make-string (org-reduced-level (org-outline-level)) ? )
5563 category (org-get-category beg)
5564 inherited-tags
5565 (or (eq org-agenda-show-inherited-tags 'always)
5566 (and (listp org-agenda-show-inherited-tags)
5567 (memq 'agenda org-agenda-show-inherited-tags))
5568 (and (eq org-agenda-show-inherited-tags t)
5569 (or (eq org-agenda-use-tag-inheritance t)
5570 (memq 'agenda org-agenda-use-tag-inheritance))))
5571 tags (org-get-tags-at nil (not inherited-tags))
5572 todo-state (org-get-todo-state)
5573 warntime (get-text-property (point) 'org-appt-warntime)
5574 extra nil)
5576 (dolist (r (if (stringp result)
5577 (list result)
5578 result)) ;; we expect a list here
5579 (when (and org-agenda-diary-sexp-prefix
5580 (string-match org-agenda-diary-sexp-prefix r))
5581 (setq extra (match-string 0 r)
5582 r (replace-match "" nil nil r)))
5583 (if (string-match "\\S-" r)
5584 (setq txt r)
5585 (setq txt "SEXP entry returned empty string"))
5586 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5587 (org-add-props txt props 'org-marker marker
5588 'date date 'todo-state todo-state
5589 'level level 'type "sexp" 'warntime warntime)
5590 (push txt ee)))))
5591 (nreverse ee)))
5593 ;; Calendar sanity: define some functions that are independent of
5594 ;; `calendar-date-style'.
5595 (defun org-anniversary (year month day &optional mark)
5596 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5597 (with-no-warnings
5598 (let ((calendar-date-style 'iso))
5599 (diary-anniversary year month day mark))))
5600 (defun org-cyclic (N year month day &optional mark)
5601 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5602 (with-no-warnings
5603 (let ((calendar-date-style 'iso))
5604 (diary-cyclic N year month day mark))))
5605 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5606 "Like `diary-block', but with fixed (ISO) order of arguments."
5607 (with-no-warnings
5608 (let ((calendar-date-style 'iso))
5609 (diary-block Y1 M1 D1 Y2 M2 D2 mark))))
5610 (defun org-date (year month day &optional mark)
5611 "Like `diary-date', but with fixed (ISO) order of arguments."
5612 (with-no-warnings
5613 (let ((calendar-date-style 'iso))
5614 (diary-date year month day mark))))
5616 ;; Define the `org-class' function
5617 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5618 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5619 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5620 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5621 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5622 `holidays', then any date that is known by the Emacs calendar to be a
5623 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5624 then those holidays will be skipped."
5625 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5626 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5627 (d (calendar-absolute-from-gregorian date))
5628 (h (when skip-weeks (calendar-check-holidays date))))
5629 (and
5630 (<= date1 d)
5631 (<= d date2)
5632 (= (calendar-day-of-week date) dayname)
5633 (or (not skip-weeks)
5634 (progn
5635 (require 'cal-iso)
5636 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5637 (not (or (and h (memq 'holidays skip-weeks))
5638 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5639 entry)))
5641 (defalias 'org-get-closed 'org-agenda-get-progress)
5642 (defun org-agenda-get-progress ()
5643 "Return the logged TODO entries for agenda display."
5644 (let* ((props (list 'mouse-face 'highlight
5645 'org-not-done-regexp org-not-done-regexp
5646 'org-todo-regexp org-todo-regexp
5647 'org-complex-heading-regexp org-complex-heading-regexp
5648 'help-echo
5649 (format "mouse-2 or RET jump to org file %s"
5650 (abbreviate-file-name buffer-file-name))))
5651 (items (if (consp org-agenda-show-log-scoped)
5652 org-agenda-show-log-scoped
5653 (if (eq org-agenda-show-log-scoped 'clockcheck)
5654 '(clock)
5655 org-agenda-log-mode-items)))
5656 (parts
5657 (delq nil
5658 (list
5659 (if (memq 'closed items) (concat "\\<" org-closed-string))
5660 (if (memq 'clock items) (concat "\\<" org-clock-string))
5661 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5662 (parts-re (if parts (mapconcat 'identity parts "\\|")
5663 (error "`org-agenda-log-mode-items' is empty")))
5664 (regexp (concat
5665 "\\(" parts-re "\\)"
5666 " *\\["
5667 (regexp-quote
5668 (substring
5669 (format-time-string
5670 (car org-time-stamp-formats)
5671 (apply 'encode-time ; DATE bound by calendar
5672 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5673 1 11))))
5674 (org-agenda-search-headline-for-time nil)
5675 marker hdmarker priority category level tags closedp
5676 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5677 (goto-char (point-min))
5678 (while (re-search-forward regexp nil t)
5679 (catch :skip
5680 (org-agenda-skip)
5681 (setq marker (org-agenda-new-marker (match-beginning 0))
5682 closedp (equal (match-string 1) org-closed-string)
5683 statep (equal (string-to-char (match-string 1)) ?-)
5684 clockp (not (or closedp statep))
5685 state (and statep (match-string 2))
5686 category (org-get-category (match-beginning 0))
5687 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5688 (when (string-match "\\]" timestr)
5689 ;; substring should only run to end of time stamp
5690 (setq rest (substring timestr (match-end 0))
5691 timestr (substring timestr 0 (match-end 0)))
5692 (if (and (not closedp) (not statep)
5693 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5694 rest))
5695 (progn (setq timestr (concat (substring timestr 0 -1)
5696 "-" (match-string 1 rest) "]"))
5697 (setq clocked (match-string 2 rest)))
5698 (setq clocked "-")))
5699 (save-excursion
5700 (setq extra
5701 (cond
5702 ((not org-agenda-log-mode-add-notes) nil)
5703 (statep
5704 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5705 (match-string 1)))
5706 (clockp
5707 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5708 (match-string 1)))))
5709 (if (not (re-search-backward org-outline-regexp-bol nil t))
5710 (throw :skip nil)
5711 (goto-char (match-beginning 0))
5712 (setq hdmarker (org-agenda-new-marker)
5713 inherited-tags
5714 (or (eq org-agenda-show-inherited-tags 'always)
5715 (and (listp org-agenda-show-inherited-tags)
5716 (memq 'todo org-agenda-show-inherited-tags))
5717 (and (eq org-agenda-show-inherited-tags t)
5718 (or (eq org-agenda-use-tag-inheritance t)
5719 (memq 'todo org-agenda-use-tag-inheritance))))
5720 tags (org-get-tags-at nil (not inherited-tags))
5721 level (make-string (org-reduced-level (org-outline-level)) ? ))
5722 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5723 (setq txt (match-string 1))
5724 (when extra
5725 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5726 (setq txt (concat (substring txt 0 (match-beginning 1))
5727 " - " extra " " (match-string 2 txt)))
5728 (setq txt (concat txt " - " extra))))
5729 (setq txt (org-agenda-format-item
5730 (cond
5731 (closedp "Closed: ")
5732 (statep (concat "State: (" state ")"))
5733 (t (concat "Clocked: (" clocked ")")))
5734 txt level category tags timestr)))
5735 (setq priority 100000)
5736 (org-add-props txt props
5737 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5738 'priority priority 'level level
5739 'type "closed" 'date date
5740 'undone-face 'org-warning 'done-face 'org-agenda-done)
5741 (push txt ee))
5742 (goto-char (point-at-eol))))
5743 (nreverse ee)))
5745 (defun org-agenda-show-clocking-issues ()
5746 "Add overlays, showing issues with clocking.
5747 See also the user option `org-agenda-clock-consistency-checks'."
5748 (interactive)
5749 (let* ((pl org-agenda-clock-consistency-checks)
5750 (re (concat "^[ \t]*"
5751 org-clock-string
5752 "[ \t]+"
5753 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5754 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5755 (tlstart 0.)
5756 (tlend 0.)
5757 (maxtime (org-duration-to-minutes
5758 (or (plist-get pl :max-duration) "24:00")))
5759 (mintime (org-duration-to-minutes
5760 (or (plist-get pl :min-duration) 0)))
5761 (maxgap (org-duration-to-minutes
5762 ;; default 30:00 means never complain
5763 (or (plist-get pl :max-gap) "30:00")))
5764 (gapok (mapcar #'org-duration-to-minutes
5765 (plist-get pl :gap-ok-around)))
5766 (def-face (or (plist-get pl :default-face)
5767 '((:background "DarkRed") (:foreground "white"))))
5768 issue face m te ts dt ov)
5769 (goto-char (point-min))
5770 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5771 (setq issue nil face def-face)
5772 (catch 'next
5773 (setq m (org-get-at-bol 'org-marker)
5774 te nil ts nil)
5775 (unless (and m (markerp m))
5776 (setq issue "No valid clock line") (throw 'next t))
5777 (org-with-point-at m
5778 (save-excursion
5779 (goto-char (point-at-bol))
5780 (unless (looking-at re)
5781 (error "No valid Clock line")
5782 (throw 'next t))
5783 (unless (match-end 3)
5784 (setq issue "No end time"
5785 face (or (plist-get pl :no-end-time-face) face))
5786 (throw 'next t))
5787 (setq ts (match-string 1)
5788 te (match-string 3)
5789 ts (float-time
5790 (apply #'encode-time (org-parse-time-string ts)))
5791 te (float-time
5792 (apply #'encode-time (org-parse-time-string te)))
5793 dt (- te ts))))
5794 (cond
5795 ((> dt (* 60 maxtime))
5796 ;; a very long clocking chunk
5797 (setq issue (format "Clocking interval is very long: %s"
5798 (org-duration-from-minutes (floor (/ dt 60.))))
5799 face (or (plist-get pl :long-face) face)))
5800 ((< dt (* 60 mintime))
5801 ;; a very short clocking chunk
5802 (setq issue (format "Clocking interval is very short: %s"
5803 (org-duration-from-minutes (floor (/ dt 60.))))
5804 face (or (plist-get pl :short-face) face)))
5805 ((and (> tlend 0) (< ts tlend))
5806 ;; Two clock entries are overlapping
5807 (setq issue (format "Clocking overlap: %d minutes"
5808 (/ (- tlend ts) 60))
5809 face (or (plist-get pl :overlap-face) face)))
5810 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5811 ;; There is a gap, lets see if we need to report it
5812 (unless (org-agenda-check-clock-gap tlend ts gapok)
5813 (setq issue (format "Clocking gap: %d minutes"
5814 (/ (- ts tlend) 60))
5815 face (or (plist-get pl :gap-face) face))))
5816 (t nil)))
5817 (setq tlend (or te tlend) tlstart (or ts tlstart))
5818 (when issue
5819 ;; OK, there was some issue, add an overlay to show the issue
5820 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5821 (overlay-put ov 'before-string
5822 (concat
5823 (org-add-props
5824 (format "%-43s" (concat " " issue))
5826 'face face)
5827 "\n"))
5828 (overlay-put ov 'evaporate t)))))
5830 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5831 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5832 (catch 'exit
5833 (unless ok-list
5834 ;; there are no OK times for gaps...
5835 (throw 'exit nil))
5836 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5837 ;; This is more than 24 hours, so it is OK.
5838 ;; because we have at least one OK time, that must be in the
5839 ;; 24 hour interval.
5840 (throw 'exit t))
5841 ;; We have a shorter gap.
5842 ;; Now we have to get the minute of the day when these times are
5843 (let* ((t1dec (decode-time (seconds-to-time t1)))
5844 (t2dec (decode-time (seconds-to-time t2)))
5845 ;; compute the minute on the day
5846 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5847 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5848 (when (< min2 min1)
5849 ;; if min2 is smaller than min1, this means it is on the next day.
5850 ;; Wrap it to after midnight.
5851 (setq min2 (+ min2 1440)))
5852 ;; Now check if any of the OK times is in the gap
5853 (mapc (lambda (x)
5854 ;; Wrap the time to after midnight if necessary
5855 (if (< x min1) (setq x (+ x 1440)))
5856 ;; Check if in interval
5857 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5858 ok-list)
5859 ;; Nope, this gap is not OK
5860 nil)))
5862 (defun org-agenda-get-deadlines (&optional with-hour)
5863 "Return the deadline information for agenda display.
5864 When WITH-HOUR is non-nil, only return deadlines with an hour
5865 specification like [h]h:mm."
5866 (let* ((props (list 'mouse-face 'highlight
5867 'org-not-done-regexp org-not-done-regexp
5868 'org-todo-regexp org-todo-regexp
5869 'org-complex-heading-regexp org-complex-heading-regexp
5870 'help-echo
5871 (format "mouse-2 or RET jump to org file %s"
5872 (abbreviate-file-name buffer-file-name))))
5873 (regexp (if with-hour
5874 org-deadline-time-hour-regexp
5875 org-deadline-time-regexp))
5876 (today (org-today))
5877 (today? (org-agenda-today-p date)) ; DATE bound by calendar.
5878 (current (calendar-absolute-from-gregorian date))
5879 deadline-items)
5880 (goto-char (point-min))
5881 (while (re-search-forward regexp nil t)
5882 (catch :skip
5883 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
5884 (org-agenda-skip)
5885 (let* ((s (match-string 1))
5886 (pos (1- (match-beginning 1)))
5887 (todo-state (save-match-data (org-get-todo-state)))
5888 (done? (member todo-state org-done-keywords))
5889 (sexp? (string-prefix-p "%%" s))
5890 ;; DEADLINE is the deadline date for the entry. It is
5891 ;; either the base date or the last repeat, according
5892 ;; to `org-agenda-prefer-last-repeat'.
5893 (deadline
5894 (cond
5895 (sexp? (org-agenda--timestamp-to-absolute s current))
5896 ((or (eq org-agenda-prefer-last-repeat t)
5897 (member todo-state org-agenda-prefer-last-repeat))
5898 (org-agenda--timestamp-to-absolute
5899 s today 'past (current-buffer) pos))
5900 (t (org-agenda--timestamp-to-absolute s))))
5901 ;; REPEAT is the future repeat closest from CURRENT,
5902 ;; according to `org-agenda-show-future-repeats'. If
5903 ;; the latter is nil, or if the time stamp has no
5904 ;; repeat part, default to DEADLINE.
5905 (repeat
5906 (cond
5907 (sexp? deadline)
5908 ((<= current today) deadline)
5909 ((not org-agenda-show-future-repeats) deadline)
5911 (let ((base (if (eq org-agenda-show-future-repeats 'next)
5912 (1+ today)
5913 current)))
5914 (org-agenda--timestamp-to-absolute
5915 s base 'future (current-buffer) pos)))))
5916 (diff (- deadline current))
5917 (suppress-prewarning
5918 (let ((scheduled
5919 (and org-agenda-skip-deadline-prewarning-if-scheduled
5920 (org-entry-get nil "SCHEDULED"))))
5921 (cond
5922 ((not scheduled) nil)
5923 ;; The current item has a scheduled date, so
5924 ;; evaluate its prewarning lead time.
5925 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
5926 ;; Use global prewarning-restart lead time.
5927 org-agenda-skip-deadline-prewarning-if-scheduled)
5928 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
5929 'pre-scheduled)
5930 ;; Set pre-warning to no earlier than SCHEDULED.
5931 (min (- deadline
5932 (org-agenda--timestamp-to-absolute scheduled))
5933 org-deadline-warning-days))
5934 ;; Set pre-warning to deadline.
5935 (t 0))))
5936 (wdays (if suppress-prewarning
5937 (let ((org-deadline-warning-days suppress-prewarning))
5938 (org-get-wdays s))
5939 (org-get-wdays s))))
5940 ;; Display deadlines items at base date (DEADLINE), today,
5941 ;; if deadline is overdue or if the expiration of the
5942 ;; upcoming deadline is within WDAYS warning time. Also,
5943 ;; show any repeat past today.
5944 (when (or (and (/= current deadline)
5945 (/= current today)
5946 (/= current repeat))
5947 (and today?
5948 (> deadline current)
5949 (> diff wdays)))
5950 (throw :skip nil))
5951 ;; Possibly skip done tasks.
5952 (when (and done?
5953 (or org-agenda-skip-deadline-if-done
5954 (/= deadline current)))
5955 (throw :skip nil))
5956 (save-excursion
5957 (re-search-backward "^\\*+[ \t]+" nil t)
5958 (goto-char (match-end 0))
5959 (let* ((category (org-get-category))
5960 (level (make-string (org-reduced-level (org-outline-level))
5961 ?\s))
5962 (head (buffer-substring (point) (line-end-position)))
5963 (inherited-tags
5964 (or (eq org-agenda-show-inherited-tags 'always)
5965 (and (listp org-agenda-show-inherited-tags)
5966 (memq 'agenda org-agenda-show-inherited-tags))
5967 (and (eq org-agenda-show-inherited-tags t)
5968 (or (eq org-agenda-use-tag-inheritance t)
5969 (memq 'agenda
5970 org-agenda-use-tag-inheritance)))))
5971 (tags (org-get-tags-at nil (not inherited-tags)))
5972 (time
5973 (cond
5974 ;; No time of day designation if it is only
5975 ;; a reminder.
5976 ((and (/= current deadline) (/= current repeat)) nil)
5977 ((string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5978 (concat (substring s (match-beginning 1)) " "))
5979 (t 'time)))
5980 (item
5981 (org-agenda-format-item
5982 ;; Insert appropriate suffixes before deadlines.
5983 ;; Those only apply to today agenda.
5984 (pcase-let ((`(,now ,future ,past)
5985 org-agenda-deadline-leaders))
5986 (cond
5987 ((and today? (< deadline today)) (format past (- diff)))
5988 ((and today? (> deadline today)) (format future diff))
5989 (t now)))
5990 head level category tags time))
5991 (face (org-agenda-deadline-face
5992 (- 1 (/ (float diff) (max wdays 1)))))
5993 (upcoming? (and today? (> deadline today)))
5994 (warntime (get-text-property (point) 'org-appt-warntime)))
5995 (org-add-props item props
5996 'org-marker (org-agenda-new-marker pos)
5997 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
5998 'warntime warntime
5999 'level level
6000 'ts-date deadline
6001 'priority
6002 ;; Adjust priority to today reminders about deadlines.
6003 ;; Overdue deadlines get the highest priority
6004 ;; increase, then imminent deadlines and eventually
6005 ;; more distant deadlines.
6006 (let ((adjust (if today? (- diff) 0)))
6007 (+ adjust (org-get-priority item)))
6008 'todo-state todo-state
6009 'type (if upcoming? "upcoming-deadline" "deadline")
6010 'date (if upcoming? date deadline)
6011 'face (if done? 'org-agenda-done face)
6012 'undone-face face
6013 'done-face 'org-agenda-done)
6014 (push item deadline-items))))))
6015 (nreverse deadline-items)))
6017 (defun org-agenda-deadline-face (fraction)
6018 "Return the face to displaying a deadline item.
6019 FRACTION is what fraction of the head-warning time has passed."
6020 (assoc-default fraction org-agenda-deadline-faces #'<=))
6022 (defun org-agenda-get-scheduled (&optional deadlines with-hour)
6023 "Return the scheduled information for agenda display.
6024 Optional argument DEADLINES is a list of deadline items to be
6025 displayed in agenda view. When WITH-HOUR is non-nil, only return
6026 scheduled items with an hour specification like [h]h:mm."
6027 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
6028 'org-todo-regexp org-todo-regexp
6029 'org-complex-heading-regexp org-complex-heading-regexp
6030 'done-face 'org-agenda-done
6031 'mouse-face 'highlight
6032 'help-echo
6033 (format "mouse-2 or RET jump to Org file %s"
6034 (abbreviate-file-name buffer-file-name))))
6035 (regexp (if with-hour
6036 org-scheduled-time-hour-regexp
6037 org-scheduled-time-regexp))
6038 (today (org-today))
6039 (todayp (org-agenda-today-p date)) ; DATE bound by calendar.
6040 (current (calendar-absolute-from-gregorian date))
6041 (deadline-pos
6042 (mapcar (lambda (d)
6043 (let ((m (get-text-property 0 'org-hd-marker d)))
6044 (and m (marker-position m))))
6045 deadlines))
6046 scheduled-items)
6047 (goto-char (point-min))
6048 (while (re-search-forward regexp nil t)
6049 (catch :skip
6050 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
6051 (org-agenda-skip)
6052 (let* ((s (match-string 1))
6053 (pos (1- (match-beginning 1)))
6054 (todo-state (save-match-data (org-get-todo-state)))
6055 (donep (member todo-state org-done-keywords))
6056 (sexp? (string-prefix-p "%%" s))
6057 ;; SCHEDULE is the scheduled date for the entry. It is
6058 ;; either the bare date or the last repeat, according
6059 ;; to `org-agenda-prefer-last-repeat'.
6060 (schedule
6061 (cond
6062 (sexp? (org-agenda--timestamp-to-absolute s current))
6063 ((or (eq org-agenda-prefer-last-repeat t)
6064 (member todo-state org-agenda-prefer-last-repeat))
6065 (org-agenda--timestamp-to-absolute
6066 s today 'past (current-buffer) pos))
6067 (t (org-agenda--timestamp-to-absolute s))))
6068 ;; REPEAT is the future repeat closest from CURRENT,
6069 ;; according to `org-agenda-show-future-repeats'. If
6070 ;; the latter is nil, or if the time stamp has no
6071 ;; repeat part, default to SCHEDULE.
6072 (repeat
6073 (cond
6074 (sexp? schedule)
6075 ((<= current today) schedule)
6076 ((not org-agenda-show-future-repeats) schedule)
6078 (let ((base (if (eq org-agenda-show-future-repeats 'next)
6079 (1+ today)
6080 current)))
6081 (org-agenda--timestamp-to-absolute
6082 s base 'future (current-buffer) pos)))))
6083 (diff (- current schedule))
6084 (warntime (get-text-property (point) 'org-appt-warntime))
6085 (pastschedp (< schedule today))
6086 (habitp (and (fboundp 'org-is-habit-p) (org-is-habit-p)))
6087 (suppress-delay
6088 (let ((deadline (and org-agenda-skip-scheduled-delay-if-deadline
6089 (org-entry-get nil "DEADLINE"))))
6090 (cond
6091 ((not deadline) nil)
6092 ;; The current item has a deadline date, so
6093 ;; evaluate its delay time.
6094 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6095 ;; Use global delay time.
6096 (- org-agenda-skip-scheduled-delay-if-deadline))
6097 ((eq org-agenda-skip-scheduled-delay-if-deadline
6098 'post-deadline)
6099 ;; Set delay to no later than DEADLINE.
6100 (min (- schedule
6101 (org-agenda--timestamp-to-absolute deadline))
6102 org-scheduled-delay-days))
6103 (t 0))))
6104 (ddays
6105 (cond
6106 ;; Nullify delay when a repeater triggered already
6107 ;; and the delay is of the form --Xd.
6108 ((and (string-match-p "--[0-9]+[hdwmy]" s)
6109 (> current schedule))
6111 (suppress-delay
6112 (let ((org-scheduled-delay-days suppress-delay))
6113 (org-get-wdays s t t)))
6114 (t (org-get-wdays s t)))))
6115 ;; Display scheduled items at base date (SCHEDULE), today if
6116 ;; scheduled before the current date, and at any repeat past
6117 ;; today. However, skip delayed items and items that have
6118 ;; been displayed for more than `org-scheduled-past-days'.
6119 (unless (and todayp
6120 habitp
6121 (bound-and-true-p org-habit-show-all-today))
6122 (when (or (and (> ddays 0) (< diff ddays))
6123 (> diff org-scheduled-past-days)
6124 (> schedule current)
6125 (and (/= current schedule)
6126 (/= current today)
6127 (/= current repeat)))
6128 (throw :skip nil)))
6129 ;; Possibly skip done tasks.
6130 (when (and donep
6131 (or org-agenda-skip-scheduled-if-done
6132 (/= schedule current)))
6133 (throw :skip nil))
6134 ;; Skip entry if it already appears as a deadline, per
6135 ;; `org-agenda-skip-scheduled-if-deadline-is-shown'. This
6136 ;; doesn't apply to habits.
6137 (when (pcase org-agenda-skip-scheduled-if-deadline-is-shown
6138 ((guard
6139 (or (not (memq (line-beginning-position 0) deadline-pos))
6140 habitp))
6141 nil)
6142 (`repeated-after-deadline
6143 (let ((deadline (time-to-days
6144 (org-get-deadline-time (point)))))
6145 (and (<= schedule deadline) (> current deadline))))
6146 (`not-today pastschedp)
6147 (`t t)
6148 (_ nil))
6149 (throw :skip nil))
6150 ;; Skip habits if `org-habit-show-habits' is nil, or if we
6151 ;; only show them for today. Also skip done habits.
6152 (when (and habitp
6153 (or donep
6154 (not (bound-and-true-p org-habit-show-habits))
6155 (and (not todayp)
6156 (bound-and-true-p
6157 org-habit-show-habits-only-for-today))))
6158 (throw :skip nil))
6159 (save-excursion
6160 (re-search-backward "^\\*+[ \t]+" nil t)
6161 (goto-char (match-end 0))
6162 (let* ((category (org-get-category))
6163 (inherited-tags
6164 (or (eq org-agenda-show-inherited-tags 'always)
6165 (and (listp org-agenda-show-inherited-tags)
6166 (memq 'agenda org-agenda-show-inherited-tags))
6167 (and (eq org-agenda-show-inherited-tags t)
6168 (or (eq org-agenda-use-tag-inheritance t)
6169 (memq 'agenda
6170 org-agenda-use-tag-inheritance)))))
6171 (tags (org-get-tags-at nil (not inherited-tags)))
6172 (level (make-string (org-reduced-level (org-outline-level))
6173 ?\s))
6174 (head (buffer-substring (point) (line-end-position)))
6175 (time
6176 (cond
6177 ;; No time of day designation if it is only
6178 ;; a reminder.
6179 ((and (/= current schedule) (/= current repeat)) nil)
6180 ((string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6181 (concat (substring s (match-beginning 1)) " "))
6182 (t 'time)))
6183 (item
6184 (org-agenda-format-item
6185 (pcase-let ((`(,first ,past) org-agenda-scheduled-leaders))
6186 ;; Show a reminder of a past scheduled today.
6187 (if (and todayp pastschedp)
6188 (format past (1+ diff))
6189 first))
6190 head level category tags time nil habitp))
6191 (face (cond ((and (not habitp) pastschedp)
6192 'org-scheduled-previously)
6193 (todayp 'org-scheduled-today)
6194 (t 'org-scheduled)))
6195 (habitp (and habitp (org-habit-parse-todo))))
6196 (org-add-props item props
6197 'undone-face face
6198 'face (if donep 'org-agenda-done face)
6199 'org-marker (org-agenda-new-marker pos)
6200 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
6201 'type (if pastschedp "past-scheduled" "scheduled")
6202 'date (if pastschedp schedule date)
6203 'ts-date schedule
6204 'warntime warntime
6205 'level level
6206 'priority (if habitp (org-habit-get-priority habitp)
6207 (+ 99 diff (org-get-priority item)))
6208 'org-habit-p habitp
6209 'todo-state todo-state)
6210 (push item scheduled-items))))))
6211 (nreverse scheduled-items)))
6213 (defun org-agenda-get-blocks ()
6214 "Return the date-range information for agenda display."
6215 (let* ((props (list 'face nil
6216 'org-not-done-regexp org-not-done-regexp
6217 'org-todo-regexp org-todo-regexp
6218 'org-complex-heading-regexp org-complex-heading-regexp
6219 'mouse-face 'highlight
6220 'help-echo
6221 (format "mouse-2 or RET jump to org file %s"
6222 (abbreviate-file-name buffer-file-name))))
6223 (regexp org-tr-regexp)
6224 (d0 (calendar-absolute-from-gregorian date))
6225 marker hdmarker ee txt d1 d2 s1 s2 category
6226 level todo-state tags pos head donep inherited-tags)
6227 (goto-char (point-min))
6228 (while (re-search-forward regexp nil t)
6229 (catch :skip
6230 (org-agenda-skip)
6231 (setq pos (point))
6232 (let ((start-time (match-string 1))
6233 (end-time (match-string 2)))
6234 (setq s1 (match-string 1)
6235 s2 (match-string 2)
6236 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
6237 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
6238 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6239 ;; Only allow days between the limits, because the normal
6240 ;; date stamps will catch the limits.
6241 (save-excursion
6242 (setq todo-state (org-get-todo-state))
6243 (setq donep (member todo-state org-done-keywords))
6244 (if (and donep org-agenda-skip-timestamp-if-done)
6245 (throw :skip t))
6246 (setq marker (org-agenda-new-marker (point))
6247 category (org-get-category))
6248 (if (not (re-search-backward org-outline-regexp-bol nil t))
6249 (throw :skip nil)
6250 (goto-char (match-beginning 0))
6251 (setq hdmarker (org-agenda-new-marker (point))
6252 inherited-tags
6253 (or (eq org-agenda-show-inherited-tags 'always)
6254 (and (listp org-agenda-show-inherited-tags)
6255 (memq 'agenda org-agenda-show-inherited-tags))
6256 (and (eq org-agenda-show-inherited-tags t)
6257 (or (eq org-agenda-use-tag-inheritance t)
6258 (memq 'agenda org-agenda-use-tag-inheritance))))
6260 tags (org-get-tags-at nil (not inherited-tags)))
6261 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6262 (looking-at "\\*+[ \t]+\\(.*\\)")
6263 (setq head (match-string 1))
6264 (let ((remove-re
6265 (if org-agenda-remove-timeranges-from-blocks
6266 (concat
6267 "<" (regexp-quote s1) ".*?>"
6268 "--"
6269 "<" (regexp-quote s2) ".*?>")
6270 nil)))
6271 (setq txt (org-agenda-format-item
6272 (format
6273 (nth (if (= d1 d2) 0 1)
6274 org-agenda-timerange-leaders)
6275 (1+ (- d0 d1)) (1+ (- d2 d1)))
6276 head level category tags
6277 (cond ((and (= d1 d0) (= d2 d0))
6278 (concat "<" start-time ">--<" end-time ">"))
6279 ((= d1 d0)
6280 (concat "<" start-time ">"))
6281 ((= d2 d0)
6282 (concat "<" end-time ">")))
6283 remove-re))))
6284 (org-add-props txt props
6285 'org-marker marker 'org-hd-marker hdmarker
6286 'type "block" 'date date
6287 'level level
6288 'todo-state todo-state
6289 'priority (org-get-priority txt))
6290 (push txt ee))))
6291 (goto-char pos)))
6292 ;; Sort the entries by expiration date.
6293 (nreverse ee)))
6295 ;;; Agenda presentation and sorting
6297 (defvar org-prefix-has-time nil
6298 "A flag, set by `org-compile-prefix-format'.
6299 The flag is set if the currently compiled format contains a `%t'.")
6300 (defvar org-prefix-has-tag nil
6301 "A flag, set by `org-compile-prefix-format'.
6302 The flag is set if the currently compiled format contains a `%T'.")
6303 (defvar org-prefix-has-effort nil
6304 "A flag, set by `org-compile-prefix-format'.
6305 The flag is set if the currently compiled format contains a `%e'.")
6306 (defvar org-prefix-has-breadcrumbs nil
6307 "A flag, set by `org-compile-prefix-format'.
6308 The flag is set if the currently compiled format contains a `%b'.")
6309 (defvar org-prefix-category-length nil
6310 "Used by `org-compile-prefix-format' to remember the category field width.")
6311 (defvar org-prefix-category-max-length nil
6312 "Used by `org-compile-prefix-format' to remember the category field width.")
6314 (defun org-agenda-get-category-icon (category)
6315 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6316 (cl-dolist (entry org-agenda-category-icon-alist)
6317 (when (string-match-p (car entry) category)
6318 (if (listp (cadr entry))
6319 (cl-return (cadr entry))
6320 (cl-return (apply #'create-image (cdr entry)))))))
6322 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6323 remove-re habitp)
6324 "Format TXT to be inserted into the agenda buffer.
6325 In particular, add the prefix and corresponding text properties.
6327 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6328 LEVEL may be a string to replace the `%l' specifier.
6329 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6330 category taken from local variable or file name. It will replace the `%c'
6331 specifier in the format.
6332 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6333 TXT for sorting of this entry, and for the `%t' specifier in the format.
6334 When DOTIME is a string, this string is searched for a time before TXT is.
6335 TAGS can be the tags of the headline.
6336 Any match of REMOVE-RE will be removed from TXT."
6337 ;; We keep the org-prefix-* variable values along with a compiled
6338 ;; formatter, so that multiple agendas existing at the same time do
6339 ;; not step on each other toes.
6341 ;; It was inconvenient to make these variables buffer local in
6342 ;; Agenda buffers, because this function expects to be called with
6343 ;; the buffer where item comes from being current, and not agenda
6344 ;; buffer
6345 (let* ((bindings (car org-prefix-format-compiled))
6346 (formatter (cadr org-prefix-format-compiled)))
6347 (cl-loop for (var value) in bindings
6348 do (set var value))
6349 (save-match-data
6350 ;; Diary entries sometimes have extra whitespace at the beginning
6351 (setq txt (org-trim txt))
6353 ;; Fix the tags part in txt
6354 (setq txt (org-agenda-fix-displayed-tags
6355 txt tags
6356 org-agenda-show-inherited-tags
6357 org-agenda-hide-tags-regexp))
6359 (let* ((category (or category
6360 (if buffer-file-name
6361 (file-name-sans-extension
6362 (file-name-nondirectory buffer-file-name))
6363 "")))
6364 (category-icon (org-agenda-get-category-icon category))
6365 (category-icon (if category-icon
6366 (propertize " " 'display category-icon)
6367 ""))
6368 (effort (and (not (string= txt ""))
6369 (get-text-property 1 'effort txt)))
6370 ;; time, tag, effort are needed for the eval of the prefix format
6371 (tag (if tags (nth (1- (length tags)) tags) ""))
6372 time
6373 (ts (if dotime (concat
6374 (if (stringp dotime) dotime "")
6375 (and org-agenda-search-headline-for-time txt))))
6376 (time-of-day (and dotime (org-get-time-of-day ts)))
6377 stamp plain s0 s1 s2 rtn srp l
6378 duration breadcrumbs)
6379 (and (derived-mode-p 'org-mode) buffer-file-name
6380 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6381 (when (and dotime time-of-day)
6382 ;; Extract starting and ending time and move them to prefix
6383 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6384 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6385 (setq s0 (match-string 0 ts)
6386 srp (and stamp (match-end 3))
6387 s1 (match-string (if plain 1 2) ts)
6388 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6390 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6391 ;; them, we might want to remove them there to avoid duplication.
6392 ;; The user can turn this off with a variable.
6393 (if (and org-prefix-has-time
6394 org-agenda-remove-times-when-in-prefix (or stamp plain)
6395 (string-match (concat (regexp-quote s0) " *") txt)
6396 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6397 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6398 (= (match-beginning 0) 0)
6400 (setq txt (replace-match "" nil nil txt))))
6401 ;; Normalize the time(s) to 24 hour
6402 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6403 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6405 ;; Try to set s2 if s1 and
6406 ;; `org-agenda-default-appointment-duration' are set
6407 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6408 (setq s2
6409 (org-duration-from-minutes
6410 (+ (org-duration-to-minutes s1 t)
6411 org-agenda-default-appointment-duration)
6412 nil t)))
6414 ;; Compute the duration
6415 (when s2
6416 (setq duration (- (org-duration-to-minutes s2)
6417 (org-duration-to-minutes s1)))))
6419 (when (string-match "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" txt)
6420 ;; Tags are in the string
6421 (if (or (eq org-agenda-remove-tags t)
6422 (and org-agenda-remove-tags
6423 org-prefix-has-tag))
6424 (setq txt (replace-match "" t t txt))
6425 (setq txt (replace-match
6426 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6427 (match-string 2 txt))
6428 t t txt))))
6430 (when remove-re
6431 (while (string-match remove-re txt)
6432 (setq txt (replace-match "" t t txt))))
6434 ;; Set org-heading property on `txt' to mark the start of the
6435 ;; heading.
6436 (add-text-properties 0 (length txt) '(org-heading t) txt)
6438 ;; Prepare the variables needed in the eval of the compiled format
6439 (if org-prefix-has-breadcrumbs
6440 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6441 (let ((s (org-display-outline-path nil nil "->" t)))
6442 (if (eq "" s) "" (concat s "->"))))))
6443 (setq time (cond (s2 (concat
6444 (org-agenda-time-of-day-to-ampm-maybe s1)
6445 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6446 (if org-agenda-timegrid-use-ampm " ")))
6447 (s1 (concat
6448 (org-agenda-time-of-day-to-ampm-maybe s1)
6449 (if org-agenda-timegrid-use-ampm
6450 "........ "
6451 "......")))
6452 (t ""))
6453 extra (or (and (not habitp) extra) "")
6454 category (if (symbolp category) (symbol-name category) category)
6455 level (or level ""))
6456 (if (string-match org-bracket-link-regexp category)
6457 (progn
6458 (setq l (if (match-end 3)
6459 (- (match-end 3) (match-beginning 3))
6460 (- (match-end 1) (match-beginning 1))))
6461 (when (< l (or org-prefix-category-length 0))
6462 (setq category (copy-sequence category))
6463 (org-add-props category nil
6464 'extra-space (make-string
6465 (- org-prefix-category-length l 1) ?\ ))))
6466 (if (and org-prefix-category-max-length
6467 (>= (length category) org-prefix-category-max-length))
6468 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6469 ;; Evaluate the compiled format
6470 (setq rtn (concat (eval formatter) txt))
6472 ;; And finally add the text properties
6473 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6474 (org-add-props rtn nil
6475 'org-category category
6476 'tags (mapcar 'org-downcase-keep-props tags)
6477 'org-highest-priority org-highest-priority
6478 'org-lowest-priority org-lowest-priority
6479 'time-of-day time-of-day
6480 'duration duration
6481 'breadcrumbs breadcrumbs
6482 'txt txt
6483 'level level
6484 'time time
6485 'extra extra
6486 'format org-prefix-format-compiled
6487 'dotime dotime)))))
6489 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6490 "Remove tags string from TXT, and add a modified list of tags.
6491 The modified list may contain inherited tags, and tags matched by
6492 `org-agenda-hide-tags-regexp' will be removed."
6493 (when (or add-inherited hide-re)
6494 (if (string-match "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" txt)
6495 (setq txt (substring txt 0 (match-beginning 0))))
6496 (setq tags
6497 (delq nil
6498 (mapcar (lambda (tg)
6499 (if (or (and hide-re (string-match hide-re tg))
6500 (and (not add-inherited)
6501 (get-text-property 0 'inherited tg)))
6503 tg))
6504 tags)))
6505 (when tags
6506 (let ((have-i (get-text-property 0 'inherited (car tags)))
6508 (setq txt (concat txt " :"
6509 (mapconcat
6510 (lambda (x)
6511 (setq i (get-text-property 0 'inherited x))
6512 (if (and have-i (not i))
6513 (progn
6514 (setq have-i nil)
6515 (concat ":" x))
6517 tags ":")
6518 (if have-i "::" ":"))))))
6519 txt)
6521 (defun org-downcase-keep-props (s)
6522 (let ((props (text-properties-at 0 s)))
6523 (setq s (downcase s))
6524 (add-text-properties 0 (length s) props s)
6527 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6529 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6530 "Add a time-grid for agenda items which need it.
6532 LIST is the list of agenda items formatted by `org-agenda-list'.
6533 NDAYS is the span of the current agenda view.
6534 TODAYP is t when the current agenda view is on today."
6535 (catch 'exit
6536 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6537 ((and todayp (member 'today (car org-agenda-time-grid))))
6538 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6539 ((member 'weekly (car org-agenda-time-grid)))
6540 (t (throw 'exit list)))
6541 (let* ((have (delq nil (mapcar
6542 (lambda (x) (get-text-property 1 'time-of-day x))
6543 list)))
6544 (string (nth 1 org-agenda-time-grid))
6545 (gridtimes (nth 2 org-agenda-time-grid))
6546 (req (car org-agenda-time-grid))
6547 (remove (member 'remove-match req))
6548 new time)
6549 (if (and (member 'require-timed req) (not have))
6550 ;; don't show empty grid
6551 (throw 'exit list))
6552 (while (setq time (pop gridtimes))
6553 (unless (and remove (member time have))
6554 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6555 (push (org-agenda-format-item
6556 nil string nil "" nil
6557 (concat (substring time 0 -2) ":" (substring time -2)))
6558 new)
6559 (put-text-property
6560 2 (length (car new)) 'face 'org-time-grid (car new))))
6561 (when (and todayp org-agenda-show-current-time-in-grid)
6562 (push (org-agenda-format-item
6563 nil org-agenda-current-time-string nil "" nil
6564 (format-time-string "%H:%M "))
6565 new)
6566 (put-text-property
6567 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6569 (if (member 'time-up org-agenda-sorting-strategy-selected)
6570 (append new list)
6571 (append list new)))))
6573 (defun org-compile-prefix-format (key)
6574 "Compile the prefix format into a Lisp form that can be evaluated.
6575 The resulting form and associated variable bindings is returned
6576 and stored in the variable `org-prefix-format-compiled'."
6577 (setq org-prefix-has-time nil
6578 org-prefix-has-tag nil
6579 org-prefix-category-length nil
6580 org-prefix-has-effort nil
6581 org-prefix-has-breadcrumbs nil)
6582 (let ((s (cond
6583 ((stringp org-agenda-prefix-format)
6584 org-agenda-prefix-format)
6585 ((assq key org-agenda-prefix-format)
6586 (cdr (assq key org-agenda-prefix-format)))
6587 (t " %-12:c%?-12t% s")))
6588 (start 0)
6589 varform vars var e c f opt)
6590 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6591 s start)
6592 (setq var (or (cdr (assoc (match-string 4 s)
6593 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6594 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6595 'eval)
6596 c (or (match-string 3 s) "")
6597 opt (match-beginning 1)
6598 start (1+ (match-beginning 0)))
6599 (if (eq var 'time) (setq org-prefix-has-time t))
6600 (if (eq var 'tag) (setq org-prefix-has-tag t))
6601 (if (eq var 'effort) (setq org-prefix-has-effort t))
6602 (if (eq var 'breadcrumbs) (setq org-prefix-has-breadcrumbs t))
6603 (setq f (concat "%" (match-string 2 s) "s"))
6604 (when (eq var 'category)
6605 (setq org-prefix-category-length
6606 (floor (abs (string-to-number (match-string 2 s)))))
6607 (setq org-prefix-category-max-length
6608 (let ((x (match-string 2 s)))
6609 (save-match-data
6610 (if (string-match "\\.[0-9]+" x)
6611 (string-to-number (substring (match-string 0 x) 1)))))))
6612 (if (eq var 'eval)
6613 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6614 (if opt
6615 (setq varform
6616 `(if (or (equal "" ,var) (equal nil ,var))
6618 (format ,f (concat ,var ,c))))
6619 (setq varform
6620 `(format ,f (if (or (equal ,var "")
6621 (equal ,var nil)) ""
6622 (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6623 (setq s (replace-match "%s" t nil s))
6624 (push varform vars))
6625 (setq vars (nreverse vars))
6626 (with-current-buffer (or org-agenda-buffer (current-buffer))
6627 (setq org-prefix-format-compiled
6628 (list
6629 `((org-prefix-has-time ,org-prefix-has-time)
6630 (org-prefix-has-tag ,org-prefix-has-tag)
6631 (org-prefix-category-length ,org-prefix-category-length)
6632 (org-prefix-has-effort ,org-prefix-has-effort)
6633 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6634 `(format ,s ,@vars))))))
6636 (defun org-set-sorting-strategy (key)
6637 (if (symbolp (car org-agenda-sorting-strategy))
6638 ;; the old format
6639 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6640 (setq org-agenda-sorting-strategy-selected
6641 (or (cdr (assq key org-agenda-sorting-strategy))
6642 (cdr (assq 'agenda org-agenda-sorting-strategy))
6643 '(time-up category-keep priority-down)))))
6645 (defun org-get-time-of-day (s &optional string mod24)
6646 "Check string S for a time of day.
6647 If found, return it as a military time number between 0 and 2400.
6648 If not found, return nil.
6649 The optional STRING argument forces conversion into a 5 character wide string
6650 HH:MM."
6651 (save-match-data
6652 (when
6653 (and
6654 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6655 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6656 (not (eq (get-text-property 1 'face s) 'org-link)))
6657 (let* ((h (string-to-number (match-string 1 s)))
6658 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6659 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6660 (am-p (equal ampm "am"))
6661 (h1 (cond ((not ampm) h)
6662 ((= h 12) (if am-p 0 12))
6663 (t (+ h (if am-p 0 12)))))
6664 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6665 (mod h1 24) h1))
6666 (t0 (+ (* 100 h2) m))
6667 (t1 (concat (if (>= h1 24) "+" " ")
6668 (if (and org-agenda-time-leading-zero
6669 (< t0 1000)) "0" "")
6670 (if (< t0 100) "0" "")
6671 (if (< t0 10) "0" "")
6672 (int-to-string t0))))
6673 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6675 (defvar org-agenda-before-sorting-filter-function nil
6676 "Function to be applied to agenda items prior to sorting.
6677 Prior to sorting also means just before they are inserted into the agenda.
6679 To aid sorting, you may revisit the original entries and add more text
6680 properties which will later be used by the sorting functions.
6682 The function should take a string argument, an agenda line.
6683 It has access to the text properties in that line, which contain among
6684 other things, the property `org-hd-marker' that points to the entry
6685 where the line comes from. Note that not all lines going into the agenda
6686 have this property, only most.
6688 The function should return the modified string. It is probably best
6689 to ONLY change text properties.
6691 You can also use this function as a filter, by returning nil for lines
6692 you don't want to have in the agenda at all. For this application, you
6693 could bind the variable in the options section of a custom command.")
6695 (defun org-agenda-finalize-entries (list &optional type)
6696 "Sort, limit and concatenate the LIST of agenda items.
6697 The optional argument TYPE tells the agenda type."
6698 (let ((max-effort (cond ((listp org-agenda-max-effort)
6699 (cdr (assoc type org-agenda-max-effort)))
6700 (t org-agenda-max-effort)))
6701 (max-todo (cond ((listp org-agenda-max-todos)
6702 (cdr (assoc type org-agenda-max-todos)))
6703 (t org-agenda-max-todos)))
6704 (max-tags (cond ((listp org-agenda-max-tags)
6705 (cdr (assoc type org-agenda-max-tags)))
6706 (t org-agenda-max-tags)))
6707 (max-entries (cond ((listp org-agenda-max-entries)
6708 (cdr (assoc type org-agenda-max-entries)))
6709 (t org-agenda-max-entries))))
6710 (when org-agenda-before-sorting-filter-function
6711 (setq list
6712 (delq nil
6713 (mapcar
6714 org-agenda-before-sorting-filter-function list))))
6715 (setq list (mapcar 'org-agenda-highlight-todo list)
6716 list (mapcar 'identity (sort list 'org-entries-lessp)))
6717 (when max-effort
6718 (setq list (org-agenda-limit-entries
6719 list 'effort-minutes max-effort
6720 (lambda (e) (or e (if org-sort-agenda-noeffort-is-high
6721 32767 -1))))))
6722 (when max-todo
6723 (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
6724 (when max-tags
6725 (setq list (org-agenda-limit-entries list 'tags max-tags)))
6726 (when max-entries
6727 (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
6728 (mapconcat 'identity list "\n")))
6730 (defun org-agenda-limit-entries (list prop limit &optional fn)
6731 "Limit the number of agenda entries."
6732 (let ((include (and limit (< limit 0))))
6733 (if limit
6734 (let ((fun (or fn (lambda (p) (if p 1))))
6735 (lim 0))
6736 (delq nil
6737 (mapcar
6738 (lambda (e)
6739 (let ((pval (funcall
6740 fun (get-text-property (1- (length e))
6741 prop e))))
6742 (if pval (setq lim (+ lim pval)))
6743 (cond ((and pval (<= lim (abs limit))) e)
6744 ((and include (not pval)) e))))
6745 list)))
6746 list)))
6748 (defun org-agenda-limit-interactively (remove)
6749 "In agenda, interactively limit entries to various maximums."
6750 (interactive "P")
6751 (if remove
6752 (progn (setq org-agenda-max-entries nil
6753 org-agenda-max-todos nil
6754 org-agenda-max-tags nil
6755 org-agenda-max-effort nil)
6756 (org-agenda-redo))
6757 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6758 (msg (cond ((= max ?E) "How many minutes? ")
6759 ((= max ?e) "How many entries? ")
6760 ((= max ?t) "How many TODO entries? ")
6761 ((= max ?T) "How many tagged entries? ")
6762 (t (user-error "Wrong input"))))
6763 (num (string-to-number (read-from-minibuffer msg))))
6764 (cond ((equal max ?e)
6765 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6766 ((equal max ?t)
6767 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6768 ((equal max ?T)
6769 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6770 ((equal max ?E)
6771 (let ((org-agenda-max-effort num)) (org-agenda-redo))))))
6772 (org-agenda-fit-window-to-buffer))
6774 (defun org-agenda-highlight-todo (x)
6775 (let ((org-done-keywords org-done-keywords-for-agenda)
6776 (case-fold-search nil)
6778 (if (eq x 'line)
6779 (save-excursion
6780 (beginning-of-line 1)
6781 (setq re (org-get-at-bol 'org-todo-regexp))
6782 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6783 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6784 (add-text-properties (match-beginning 0) (match-end 1)
6785 (list 'face (org-get-todo-face 1)))
6786 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6787 (delete-region (match-beginning 1) (1- (match-end 0)))
6788 (goto-char (match-beginning 1))
6789 (insert (format org-agenda-todo-keyword-format s)))))
6790 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6791 (setq re (get-text-property 0 'org-todo-regexp x))
6792 (when (and re
6793 ;; Test `pl' because if there's no heading content,
6794 ;; there's no point matching to highlight. Note
6795 ;; that if we didn't test `pl' first, and there
6796 ;; happened to be no keyword from `org-todo-regexp'
6797 ;; on this heading line, then the `equal' comparison
6798 ;; afterwards would spuriously succeed in the case
6799 ;; where `pl' is nil -- causing an args-out-of-range
6800 ;; error when we try to add text properties to text
6801 ;; that isn't there.
6803 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6804 x pl) pl))
6805 (add-text-properties
6806 (or (match-end 1) (match-end 0)) (match-end 0)
6807 (list 'face (org-get-todo-face (match-string 2 x)))
6809 (when (match-end 1)
6810 (setq x (concat (substring x 0 (match-end 1))
6811 (format org-agenda-todo-keyword-format
6812 (match-string 2 x))
6813 (org-add-props " " (text-properties-at 0 x))
6814 (substring x (match-end 3)))))))
6815 x)))
6817 (defsubst org-cmp-values (a b property)
6818 "Compare the numeric value of text PROPERTY for string A and B."
6819 (let ((pa (or (get-text-property (1- (length a)) property a) 0))
6820 (pb (or (get-text-property (1- (length b)) property b) 0)))
6821 (cond ((> pa pb) +1)
6822 ((< pa pb) -1))))
6824 (defsubst org-cmp-effort (a b)
6825 "Compare the effort values of string A and B."
6826 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6827 ;; `effort-minutes' property is not directly accessible from
6828 ;; the strings, but is stored as a property in `txt'.
6829 (ea (or (get-text-property
6830 0 'effort-minutes (get-text-property 0 'txt a))
6831 def))
6832 (eb (or (get-text-property
6833 0 'effort-minutes (get-text-property 0 'txt b))
6834 def)))
6835 (cond ((> ea eb) +1)
6836 ((< ea eb) -1))))
6838 (defsubst org-cmp-category (a b)
6839 "Compare the string values of categories of strings A and B."
6840 (let ((ca (or (get-text-property (1- (length a)) 'org-category a) ""))
6841 (cb (or (get-text-property (1- (length b)) 'org-category b) "")))
6842 (cond ((string-lessp ca cb) -1)
6843 ((string-lessp cb ca) +1))))
6845 (defsubst org-cmp-todo-state (a b)
6846 "Compare the todo states of strings A and B."
6847 (let* ((ma (or (get-text-property 1 'org-marker a)
6848 (get-text-property 1 'org-hd-marker a)))
6849 (mb (or (get-text-property 1 'org-marker b)
6850 (get-text-property 1 'org-hd-marker b)))
6851 (fa (and ma (marker-buffer ma)))
6852 (fb (and mb (marker-buffer mb)))
6853 (todo-kwds
6854 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6855 (and fb (with-current-buffer fb org-todo-keywords-1))))
6856 (ta (or (get-text-property 1 'todo-state a) ""))
6857 (tb (or (get-text-property 1 'todo-state b) ""))
6858 (la (- (length (member ta todo-kwds))))
6859 (lb (- (length (member tb todo-kwds))))
6860 (donepa (member ta org-done-keywords-for-agenda))
6861 (donepb (member tb org-done-keywords-for-agenda)))
6862 (cond ((and donepa (not donepb)) -1)
6863 ((and (not donepa) donepb) +1)
6864 ((< la lb) -1)
6865 ((< lb la) +1))))
6867 (defsubst org-cmp-alpha (a b)
6868 "Compare the headlines, alphabetically."
6869 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6870 (plb (text-property-any 0 (length b) 'org-heading t b))
6871 (ta (and pla (substring a pla)))
6872 (tb (and plb (substring b plb)))
6873 (case-fold-search nil))
6874 (when pla
6875 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6876 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6877 (setq ta (substring ta (match-end 0))))
6878 (setq ta (downcase ta)))
6879 (when plb
6880 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6881 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6882 (setq tb (substring tb (match-end 0))))
6883 (setq tb (downcase tb)))
6884 (cond ((not ta) +1)
6885 ((not tb) -1)
6886 ((string-lessp ta tb) -1)
6887 ((string-lessp tb ta) +1))))
6889 (defsubst org-cmp-tag (a b)
6890 "Compare the string values of the first tags of A and B."
6891 (let ((ta (car (last (get-text-property 1 'tags a))))
6892 (tb (car (last (get-text-property 1 'tags b)))))
6893 (cond ((not ta) +1)
6894 ((not tb) -1)
6895 ((string-lessp ta tb) -1)
6896 ((string-lessp tb ta) +1))))
6898 (defsubst org-cmp-time (a b)
6899 "Compare the time-of-day values of strings A and B."
6900 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6901 (ta (or (get-text-property 1 'time-of-day a) def))
6902 (tb (or (get-text-property 1 'time-of-day b) def)))
6903 (cond ((< ta tb) -1)
6904 ((< tb ta) +1))))
6906 (defsubst org-cmp-ts (a b type)
6907 "Compare the timestamps values of entries A and B.
6908 When TYPE is \"scheduled\", \"deadline\", \"timestamp\" or
6909 \"timestamp_ia\", compare within each of these type. When TYPE
6910 is the empty string, compare all timestamps without respect of
6911 their type."
6912 (let* ((def (if org-sort-agenda-notime-is-late most-positive-fixnum -1))
6913 (ta (or (and (string-match type (or (get-text-property 1 'type a) ""))
6914 (get-text-property 1 'ts-date a))
6915 def))
6916 (tb (or (and (string-match type (or (get-text-property 1 'type b) ""))
6917 (get-text-property 1 'ts-date b))
6918 def)))
6919 (cond ((< ta tb) -1)
6920 ((< tb ta) +1))))
6922 (defsubst org-cmp-habit-p (a b)
6923 "Compare the todo states of strings A and B."
6924 (let ((ha (get-text-property 1 'org-habit-p a))
6925 (hb (get-text-property 1 'org-habit-p b)))
6926 (cond ((and ha (not hb)) -1)
6927 ((and (not ha) hb) +1))))
6929 (defun org-entries-lessp (a b)
6930 "Predicate for sorting agenda entries."
6931 ;; The following variables will be used when the form is evaluated.
6932 ;; So even though the compiler complains, keep them.
6933 (let* ((ss org-agenda-sorting-strategy-selected)
6934 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
6935 (org-cmp-ts a b "")))
6936 (timestamp-down (if timestamp-up (- timestamp-up) nil))
6937 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
6938 (org-cmp-ts a b "scheduled")))
6939 (scheduled-down (if scheduled-up (- scheduled-up) nil))
6940 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
6941 (org-cmp-ts a b "deadline")))
6942 (deadline-down (if deadline-up (- deadline-up) nil))
6943 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
6944 (org-cmp-ts a b "timestamp_ia")))
6945 (tsia-down (if tsia-up (- tsia-up) nil))
6946 (ts-up (and (org-em 'ts-up 'ts-down ss)
6947 (org-cmp-ts a b "timestamp")))
6948 (ts-down (if ts-up (- ts-up) nil))
6949 (time-up (and (org-em 'time-up 'time-down ss)
6950 (org-cmp-time a b)))
6951 (time-down (if time-up (- time-up) nil))
6952 (stats-up (and (org-em 'stats-up 'stats-down ss)
6953 (org-cmp-values a b 'org-stats)))
6954 (stats-down (if stats-up (- stats-up) nil))
6955 (priority-up (and (org-em 'priority-up 'priority-down ss)
6956 (org-cmp-values a b 'priority)))
6957 (priority-down (if priority-up (- priority-up) nil))
6958 (effort-up (and (org-em 'effort-up 'effort-down ss)
6959 (org-cmp-effort a b)))
6960 (effort-down (if effort-up (- effort-up) nil))
6961 (category-up (and (or (org-em 'category-up 'category-down ss)
6962 (memq 'category-keep ss))
6963 (org-cmp-category a b)))
6964 (category-down (if category-up (- category-up) nil))
6965 (category-keep (if category-up +1 nil))
6966 (tag-up (and (org-em 'tag-up 'tag-down ss)
6967 (org-cmp-tag a b)))
6968 (tag-down (if tag-up (- tag-up) nil))
6969 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
6970 (org-cmp-todo-state a b)))
6971 (todo-state-down (if todo-state-up (- todo-state-up) nil))
6972 (habit-up (and (org-em 'habit-up 'habit-down ss)
6973 (org-cmp-habit-p a b)))
6974 (habit-down (if habit-up (- habit-up) nil))
6975 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
6976 (org-cmp-alpha a b)))
6977 (alpha-down (if alpha-up (- alpha-up) nil))
6978 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
6979 user-defined-up user-defined-down)
6980 (if (and need-user-cmp org-agenda-cmp-user-defined
6981 (functionp org-agenda-cmp-user-defined))
6982 (setq user-defined-up
6983 (funcall org-agenda-cmp-user-defined a b)
6984 user-defined-down (if user-defined-up (- user-defined-up) nil)))
6985 (cdr (assoc
6986 (eval (cons 'or org-agenda-sorting-strategy-selected))
6987 '((-1 . t) (1 . nil) (nil . nil))))))
6989 ;;; Agenda restriction lock
6991 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
6992 "Overlay to mark the headline to which agenda commands are restricted.")
6993 (overlay-put org-agenda-restriction-lock-overlay
6994 'face 'org-agenda-restriction-lock)
6995 (overlay-put org-agenda-restriction-lock-overlay
6996 'help-echo "Agendas are currently limited to this subtree.")
6997 (delete-overlay org-agenda-restriction-lock-overlay)
6999 (defun org-agenda-set-restriction-lock-from-agenda (arg)
7000 "Set the restriction lock to the agenda item at point from within the agenda.
7001 When called with a `\\[universal-argument]' prefix, restrict to
7002 the file which contains the item.
7003 Argument ARG is the prefix argument."
7004 (interactive "P")
7005 (unless (derived-mode-p 'org-agenda-mode)
7006 (user-error "Not in an Org agenda buffer"))
7007 (let* ((marker (or (org-get-at-bol 'org-marker)
7008 (org-agenda-error)))
7009 (buffer (marker-buffer marker))
7010 (pos (marker-position marker)))
7011 (with-current-buffer buffer
7012 (goto-char pos)
7013 (org-agenda-set-restriction-lock arg))))
7015 ;;;###autoload
7016 (defun org-agenda-set-restriction-lock (&optional type)
7017 "Set restriction lock for agenda, to current subtree or file.
7018 Restriction will be the file if TYPE is `file', or if type is the
7019 universal prefix \\='(4), or if the cursor is before the first headline
7020 in the file. Otherwise, restriction will be to the current subtree."
7021 (interactive "P")
7022 (org-agenda-remove-restriction-lock 'noupdate)
7023 (and (equal type '(4)) (setq type 'file))
7024 (setq type (cond
7025 (type type)
7026 ((org-at-heading-p) 'subtree)
7027 ((condition-case nil (org-back-to-heading t) (error nil))
7028 'subtree)
7029 (t 'file)))
7030 (if (eq type 'subtree)
7031 (progn
7032 (setq org-agenda-restrict (current-buffer))
7033 (setq org-agenda-overriding-restriction 'subtree)
7034 (put 'org-agenda-files 'org-restrict
7035 (list (buffer-file-name (buffer-base-buffer))))
7036 (org-back-to-heading t)
7037 (move-overlay org-agenda-restriction-lock-overlay
7038 (point)
7039 (if org-agenda-restriction-lock-highlight-subtree
7040 (save-excursion (org-end-of-subtree t t) (point))
7041 (point-at-eol)))
7042 (move-marker org-agenda-restrict-begin (point))
7043 (move-marker org-agenda-restrict-end
7044 (save-excursion (org-end-of-subtree t t)))
7045 (message "Locking agenda restriction to subtree"))
7046 (put 'org-agenda-files 'org-restrict
7047 (list (buffer-file-name (buffer-base-buffer))))
7048 (setq org-agenda-restrict nil)
7049 (setq org-agenda-overriding-restriction 'file)
7050 (move-marker org-agenda-restrict-begin nil)
7051 (move-marker org-agenda-restrict-end nil)
7052 (message "Locking agenda restriction to file"))
7053 (setq current-prefix-arg nil)
7054 (org-agenda-maybe-redo))
7056 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7057 "Remove the agenda restriction lock."
7058 (interactive "P")
7059 (delete-overlay org-agenda-restriction-lock-overlay)
7060 (delete-overlay org-speedbar-restriction-lock-overlay)
7061 (setq org-agenda-overriding-restriction nil)
7062 (setq org-agenda-restrict nil)
7063 (put 'org-agenda-files 'org-restrict nil)
7064 (move-marker org-agenda-restrict-begin nil)
7065 (move-marker org-agenda-restrict-end nil)
7066 (setq current-prefix-arg nil)
7067 (message "Agenda restriction lock removed")
7068 (or noupdate (org-agenda-maybe-redo)))
7070 (defun org-agenda-maybe-redo ()
7071 "If there is any window showing the agenda view, update it."
7072 (let ((w (get-buffer-window (or org-agenda-this-buffer-name
7073 org-agenda-buffer-name)
7075 (w0 (selected-window)))
7076 (when w
7077 (select-window w)
7078 (org-agenda-redo)
7079 (select-window w0)
7080 (if org-agenda-overriding-restriction
7081 (message "Agenda view shifted to new %s restriction"
7082 org-agenda-overriding-restriction)
7083 (message "Agenda restriction lock removed")))))
7085 ;;; Agenda commands
7087 (defun org-agenda-check-type (error &rest types)
7088 "Check if agenda buffer is of allowed type.
7089 If ERROR is non-nil, throw an error, otherwise just return nil.
7090 Allowed types are `agenda' `timeline' `todo' `tags' `search'."
7091 (if (not org-agenda-type)
7092 (error "No Org agenda currently displayed")
7093 (if (memq org-agenda-type types)
7095 (if error
7096 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
7097 nil))))
7099 (defun org-agenda-Quit ()
7100 "Exit the agenda, killing the agenda buffer.
7101 Like `org-agenda-quit', but kill the buffer even when
7102 `org-agenda-sticky' is non-nil."
7103 (interactive)
7104 (org-agenda--quit))
7106 (defun org-agenda-quit ()
7107 "Exit the agenda.
7109 When `org-agenda-sticky' is non-nil, bury the agenda buffer
7110 instead of killing it.
7112 When `org-agenda-restore-windows-after-quit' is non-nil, restore
7113 the pre-agenda window configuration.
7115 When column view is active, exit column view instead of the
7116 agenda."
7117 (interactive)
7118 (org-agenda--quit org-agenda-sticky))
7120 (defun org-agenda--quit (&optional bury)
7121 (if org-agenda-columns-active
7122 (org-columns-quit)
7123 (let ((wconf org-agenda-pre-window-conf)
7124 (buf (current-buffer))
7125 (org-agenda-last-indirect-window
7126 (and (eq org-indirect-buffer-display 'other-window)
7127 org-agenda-last-indirect-buffer
7128 (get-buffer-window org-agenda-last-indirect-buffer))))
7129 (cond
7130 ((eq org-agenda-window-setup 'other-frame)
7131 (delete-frame))
7132 ((and org-agenda-restore-windows-after-quit
7133 wconf)
7134 ;; Maybe restore the pre-agenda window configuration. Reset
7135 ;; `org-agenda-pre-window-conf' before running
7136 ;; `set-window-configuration', which loses the current buffer.
7137 (setq org-agenda-pre-window-conf nil)
7138 (set-window-configuration wconf))
7140 (when org-agenda-last-indirect-window
7141 (delete-window org-agenda-last-indirect-window))
7142 (and (not (eq org-agenda-window-setup 'current-window))
7143 (not (one-window-p))
7144 (delete-window))))
7145 (if bury
7146 ;; Set the agenda buffer as the current buffer instead of
7147 ;; passing it as an argument to `bury-buffer' so that
7148 ;; `bury-buffer' removes it from the window.
7149 (with-current-buffer buf
7150 (bury-buffer))
7151 (kill-buffer buf)
7152 (setq org-agenda-archives-mode nil
7153 org-agenda-buffer nil)))))
7155 (defun org-agenda-exit ()
7156 "Exit the agenda, killing Org buffers loaded by the agenda.
7157 Like `org-agenda-Quit', but kill any buffers that were created by
7158 the agenda. Org buffers visited directly by the user will not be
7159 touched. Also, exit the agenda even if it is in column view."
7160 (interactive)
7161 (when org-agenda-columns-active
7162 (org-columns-quit))
7163 (org-release-buffers org-agenda-new-buffers)
7164 (setq org-agenda-new-buffers nil)
7165 (org-agenda-Quit))
7167 (defun org-agenda-kill-all-agenda-buffers ()
7168 "Kill all buffers in `org-agenda-mode'.
7169 This is used when toggling sticky agendas."
7170 (interactive)
7171 (let (blist)
7172 (dolist (buf (buffer-list))
7173 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7174 (push buf blist)))
7175 (mapc 'kill-buffer blist)))
7177 (defun org-agenda-execute (arg)
7178 "Execute another agenda command, keeping same window.
7179 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7180 in the agenda."
7181 (interactive "P")
7182 (let ((org-agenda-window-setup 'current-window))
7183 (org-agenda arg)))
7185 (defun org-agenda-redo (&optional all)
7186 "Rebuild possibly ALL agenda view(s) in the current buffer."
7187 (interactive "P")
7188 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7189 (cpa (unless (eq all t) current-prefix-arg))
7190 (org-agenda-doing-sticky-redo org-agenda-sticky)
7191 (org-agenda-sticky nil)
7192 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7193 org-agenda-buffer-name))
7194 (org-agenda-keep-modes t)
7195 (tag-filter org-agenda-tag-filter)
7196 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7197 (top-hl-filter org-agenda-top-headline-filter)
7198 (cat-filter org-agenda-category-filter)
7199 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7200 (re-filter org-agenda-regexp-filter)
7201 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7202 (effort-filter org-agenda-effort-filter)
7203 (effort-preset (get 'org-agenda-effort-filter :preset-filter))
7204 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7205 (cols org-agenda-columns-active)
7206 (line (org-current-line))
7207 (window-line (- line (org-current-line (window-start))))
7208 (lprops (get 'org-agenda-redo-command 'org-lprops))
7209 (redo-cmd (get-text-property p 'org-redo-cmd))
7210 (last-args (get-text-property p 'org-last-args))
7211 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7212 (org-agenda-overriding-cmd-arguments
7213 (unless (eq all t)
7214 (cond ((listp last-args)
7215 (cons (or cpa (car last-args)) (cdr last-args)))
7216 ((stringp last-args)
7217 last-args))))
7218 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7219 (put 'org-agenda-tag-filter :preset-filter nil)
7220 (put 'org-agenda-category-filter :preset-filter nil)
7221 (put 'org-agenda-regexp-filter :preset-filter nil)
7222 (put 'org-agenda-effort-filter :preset-filter nil)
7223 (and cols (org-columns-quit))
7224 (message "Rebuilding agenda buffer...")
7225 (if series-redo-cmd
7226 (eval series-redo-cmd)
7227 (org-let lprops redo-cmd))
7228 (setq org-agenda-undo-list nil
7229 org-agenda-pending-undo-list nil
7230 org-agenda-tag-filter tag-filter
7231 org-agenda-category-filter cat-filter
7232 org-agenda-regexp-filter re-filter
7233 org-agenda-effort-filter effort-filter
7234 org-agenda-top-headline-filter top-hl-filter)
7235 (message "Rebuilding agenda buffer...done")
7236 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7237 (put 'org-agenda-category-filter :preset-filter cat-preset)
7238 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7239 (put 'org-agenda-effort-filter :preset-filter effort-preset)
7240 (let ((tag (or tag-filter tag-preset))
7241 (cat (or cat-filter cat-preset))
7242 (effort (or effort-filter effort-preset))
7243 (re (or re-filter re-preset)))
7244 (when tag (org-agenda-filter-apply tag 'tag t))
7245 (when cat (org-agenda-filter-apply cat 'category))
7246 (when effort (org-agenda-filter-apply effort 'effort))
7247 (when re (org-agenda-filter-apply re 'regexp)))
7248 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7249 (and cols (called-interactively-p 'any) (org-agenda-columns))
7250 (org-goto-line line)
7251 (recenter window-line)))
7253 (defvar org-global-tags-completion-table nil)
7254 (defvar org-agenda-filter-form nil)
7255 (defvar org-agenda-filtered-by-category nil)
7257 (defun org-agenda-filter-by-category (strip)
7258 "Filter lines in the agenda buffer that have a specific category.
7259 The category is that of the current line.
7260 Without prefix argument, keep only the lines of that category.
7261 With a prefix argument, exclude the lines of that category.
7263 (interactive "P")
7264 (if (and org-agenda-filtered-by-category
7265 org-agenda-category-filter)
7266 (org-agenda-filter-show-all-cat)
7267 (let ((cat (org-no-properties (org-get-at-eol 'org-category 1))))
7268 (cond
7269 ((and cat strip)
7270 (org-agenda-filter-apply
7271 (push (concat "-" cat) org-agenda-category-filter) 'category))
7272 (cat
7273 (org-agenda-filter-apply
7274 (setq org-agenda-category-filter
7275 (list (concat "+" cat))) 'category))
7276 (t (error "No category at point"))))))
7278 (defun org-find-top-headline (&optional pos)
7279 "Find the topmost parent headline and return it."
7280 (save-excursion
7281 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
7282 (if pos (goto-char pos))
7283 ;; Skip up to the topmost parent
7284 (while (ignore-errors (outline-up-heading 1) t))
7285 (ignore-errors
7286 (nth 4 (org-heading-components))))))
7288 (defvar org-agenda-filtered-by-top-headline nil)
7289 (defun org-agenda-filter-by-top-headline (strip)
7290 "Keep only those lines that are descendants from the same top headline.
7291 The top headline is that of the current line."
7292 (interactive "P")
7293 (if org-agenda-filtered-by-top-headline
7294 (progn
7295 (setq org-agenda-filtered-by-top-headline nil
7296 org-agenda-top-headline-filter nil)
7297 (org-agenda-filter-show-all-top-filter))
7298 (let ((toph (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7299 (if toph (org-agenda-filter-top-headline-apply toph strip)
7300 (error "No top-level headline at point")))))
7302 (defvar org-agenda-regexp-filter nil)
7303 (defun org-agenda-filter-by-regexp (strip)
7304 "Filter agenda entries by a regular expression.
7305 Regexp filters are cumulative.
7306 With no prefix argument, keep entries matching the regexp.
7307 With one prefix argument, filter out entries matching the regexp.
7308 With two prefix arguments, remove the regexp filters."
7309 (interactive "P")
7310 (if (not (equal strip '(16)))
7311 (let ((flt (concat (if (equal strip '(4)) "-" "+")
7312 (read-from-minibuffer
7313 (if (equal strip '(4))
7314 "Filter out entries matching regexp: "
7315 "Narrow to entries matching regexp: ")))))
7316 (push flt org-agenda-regexp-filter)
7317 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
7318 (org-agenda-filter-show-all-re)
7319 (message "Regexp filter removed")))
7321 (defvar org-agenda-effort-filter nil)
7322 (defun org-agenda-filter-by-effort (strip)
7323 "Filter agenda entries by effort.
7324 With no prefix argument, keep entries matching the effort condition.
7325 With one prefix argument, filter out entries matching the condition.
7326 With two prefix arguments, remove the effort filters."
7327 (interactive "P")
7328 (cond
7329 ((member strip '(nil 4))
7330 (let* ((efforts (split-string
7331 (or (cdr (assoc (concat org-effort-property "_ALL")
7332 org-global-properties))
7333 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00")))
7334 ;; XXX: the following handles only up to 10 different
7335 ;; effort values.
7336 (allowed-keys (if (null efforts) nil
7337 (mapcar (lambda (n) (mod n 10)) ;turn 10 into 0
7338 (number-sequence 1 (length efforts)))))
7339 (op nil))
7340 (while (not (memq op '(?< ?> ?=)))
7341 (setq op (read-char-exclusive "Effort operator? (> = or <)")))
7342 ;; Select appropriate duration. Ignore non-digit characters.
7343 (let ((prompt
7344 (apply #'format
7345 (concat "Effort %c "
7346 (mapconcat (lambda (s) (concat "[%d]" s))
7347 efforts
7348 " "))
7349 op allowed-keys))
7350 (eff -1))
7351 (while (not (memq eff allowed-keys))
7352 (message prompt)
7353 (setq eff (- (read-char-exclusive) 48)))
7354 (setq org-agenda-effort-filter
7355 (list (concat (if strip "-" "+")
7356 (char-to-string op)
7357 ;; Numbering is 1 2 3 ... 9 0, but we want
7358 ;; 0 1 2 ... 8 9.
7359 (nth (mod (1- eff) 10) efforts)))))
7360 (org-agenda-filter-apply org-agenda-effort-filter 'effort)))
7361 (t (org-agenda-filter-show-all-effort)
7362 (message "Effort filter removed"))))
7364 (defun org-agenda-filter-remove-all ()
7365 "Remove all filters from the current agenda buffer."
7366 (interactive)
7367 (when org-agenda-tag-filter
7368 (org-agenda-filter-show-all-tag))
7369 (when org-agenda-category-filter
7370 (org-agenda-filter-show-all-cat))
7371 (when org-agenda-regexp-filter
7372 (org-agenda-filter-show-all-re))
7373 (when org-agenda-top-headline-filter
7374 (org-agenda-filter-show-all-top-filter))
7375 (when org-agenda-effort-filter
7376 (org-agenda-filter-show-all-effort))
7377 (org-agenda-finalize))
7379 (defun org-agenda-filter-by-tag (arg &optional char exclude)
7380 "Keep only those lines in the agenda buffer that have a specific tag.
7382 The tag is selected with its fast selection letter, as configured.
7384 With a `\\[universal-argument]' prefix, exclude the agenda search.
7386 With a `\\[universal-argument] \\[universal-argument]' prefix, filter the literal tag, \
7387 i.e. don't
7388 filter on all its group members.
7390 A lisp caller can specify CHAR. EXCLUDE means that the new tag
7391 should be used to exclude the search - the interactive user can
7392 also press `-' or `+' to switch between filtering and excluding."
7393 (interactive "P")
7394 (let* ((alist org-tag-alist-for-agenda)
7395 (tag-chars (mapconcat
7396 (lambda (x) (if (and (not (symbolp (car x)))
7397 (cdr x))
7398 (char-to-string (cdr x))
7399 ""))
7400 org-tag-alist-for-agenda ""))
7401 (valid-char-list (append '(?\t ?\r ?/ ?. ?\s ?q)
7402 (string-to-list tag-chars)))
7403 (exclude (or exclude (equal arg '(4))))
7404 (expand (not (equal arg '(16))))
7405 (inhibit-read-only t)
7406 (current org-agenda-tag-filter)
7407 a n tag)
7408 (unless char
7409 (while (not (memq char valid-char-list))
7410 (message
7411 "%s by tag [%s ], [TAB], %s[/]:off, [+/-]:filter/exclude%s, [q]:quit"
7412 (if exclude "Exclude" "Filter") tag-chars
7413 (if org-agenda-auto-exclude-function "[RET], " "")
7414 (if expand "" ", no grouptag expand"))
7415 (setq char (read-char-exclusive))
7416 ;; Excluding or filtering down
7417 (cond ((eq char ?-) (setq exclude t))
7418 ((eq char ?+) (setq exclude nil)))))
7419 (when (eq char ?\t)
7420 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7421 (setq-local org-global-tags-completion-table
7422 (org-global-tags-completion-table)))
7423 (let ((completion-ignore-case t))
7424 (setq tag (completing-read
7425 "Tag: " org-global-tags-completion-table))))
7426 (cond
7427 ((eq char ?\r)
7428 (org-agenda-filter-show-all-tag)
7429 (when org-agenda-auto-exclude-function
7430 (setq org-agenda-tag-filter nil)
7431 (dolist (tag (org-agenda-get-represented-tags))
7432 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7433 (if modifier
7434 (push modifier org-agenda-tag-filter))))
7435 (if (not (null org-agenda-tag-filter))
7436 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))))
7437 ((eq char ?/)
7438 (org-agenda-filter-show-all-tag)
7439 (when (get 'org-agenda-tag-filter :preset-filter)
7440 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand)))
7441 ((eq char ?.)
7442 (setq org-agenda-tag-filter
7443 (mapcar (lambda(tag) (concat "+" tag))
7444 (org-get-at-bol 'tags)))
7445 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7446 ((eq char ?q)) ;If q, abort (even if there is a q-key for a tag...)
7447 ((or (eq char ?\s)
7448 (setq a (rassoc char alist))
7449 (and tag (setq a (cons tag nil))))
7450 (org-agenda-filter-show-all-tag)
7451 (setq tag (car a))
7452 (setq org-agenda-tag-filter
7453 (cons (concat (if exclude "-" "+") tag)
7454 current))
7455 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7456 (t (error "Invalid tag selection character %c" char)))))
7458 (defun org-agenda-get-represented-tags ()
7459 "Get a list of all tags currently represented in the agenda."
7460 (let (p tags)
7461 (save-excursion
7462 (goto-char (point-min))
7463 (while (setq p (next-single-property-change (point) 'tags))
7464 (goto-char p)
7465 (mapc (lambda (x) (add-to-list 'tags x))
7466 (get-text-property (point) 'tags))))
7467 tags))
7470 (defun org-agenda-filter-make-matcher (filter type &optional expand)
7471 "Create the form that tests a line for agenda filter. Optional
7472 argument EXPAND can be used for the TYPE tag and will expand the
7473 tags in the FILTER if any of the tags in FILTER are grouptags."
7474 (let (f f1)
7475 (cond
7476 ;; Tag filter
7477 ((eq type 'tag)
7478 (setq filter
7479 (delete-dups
7480 (append (get 'org-agenda-tag-filter :preset-filter)
7481 filter)))
7482 (dolist (x filter)
7483 (let ((op (string-to-char x)))
7484 (if expand (setq x (org-agenda-filter-expand-tags (list x) t))
7485 (setq x (list x)))
7486 (setq f1 (org-agenda-filter-make-matcher-tag-exp x op))
7487 (push f1 f))))
7488 ;; Category filter
7489 ((eq type 'category)
7490 (setq filter
7491 (delete-dups
7492 (append (get 'org-agenda-category-filter :preset-filter)
7493 filter)))
7494 (dolist (x filter)
7495 (if (equal "-" (substring x 0 1))
7496 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7497 (setq f1 (list 'equal (substring x 1) 'cat)))
7498 (push f1 f)))
7499 ;; Regexp filter
7500 ((eq type 'regexp)
7501 (setq filter
7502 (delete-dups
7503 (append (get 'org-agenda-regexp-filter :preset-filter)
7504 filter)))
7505 (dolist (x filter)
7506 (if (equal "-" (substring x 0 1))
7507 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7508 (setq f1 (list 'string-match (substring x 1) 'txt)))
7509 (push f1 f)))
7510 ;; Effort filter
7511 ((eq type 'effort)
7512 (setq filter
7513 (delete-dups
7514 (append (get 'org-agenda-effort-filter :preset-filter)
7515 filter)))
7516 (dolist (x filter)
7517 (push (org-agenda-filter-effort-form x) f))))
7518 (cons 'and (nreverse f))))
7520 (defun org-agenda-filter-make-matcher-tag-exp (tags op)
7521 "Return a form associated to tag-expression TAGS.
7522 Build a form testing a line for agenda filter for
7523 tag-expressions. OP is an operator of type CHAR that allows the
7524 function to set the right switches in the returned form."
7525 (let (form)
7526 ;; Any of the expressions can match if OP is +, all must match if
7527 ;; the operator is -.
7528 (dolist (x tags (cons (if (eq op ?-) 'and 'or) form))
7529 (let* ((tag (substring x 1))
7530 (f (cond
7531 ((string= "" tag) '(not tags))
7532 ((and (string-match-p "\\`{" tag) (string-match-p "}\\'" tag))
7533 ;; TAG is a regexp.
7534 (list 'org-match-any-p (substring tag 1 -1) 'tags))
7535 (t (list 'member (downcase tag) 'tags)))))
7536 (push (if (eq op ?-) (list 'not f) f) form)))))
7538 (defun org-agenda-filter-effort-form (e)
7539 "Return the form to compare the effort of the current line with what E says.
7540 E looks like \"+<2:25\"."
7541 (let (op)
7542 (setq e (substring e 1))
7543 (setq op (string-to-char e) e (substring e 1))
7544 (setq op (cond ((equal op ?<) '<=)
7545 ((equal op ?>) '>=)
7546 ((equal op ??) op)
7547 (t '=)))
7548 (list 'org-agenda-compare-effort (list 'quote op)
7549 (org-duration-to-minutes e))))
7551 (defun org-agenda-compare-effort (op value)
7552 "Compare the effort of the current line with VALUE, using OP.
7553 If the line does not have an effort defined, return nil."
7554 ;; `effort-minutes' property cannot be extracted directly from
7555 ;; current line but is stored as a property in `txt'.
7556 (let ((effort (get-text-property 0 'effort-minutes (org-get-at-bol 'txt))))
7557 (funcall op
7558 (or effort (if org-sort-agenda-noeffort-is-high 32767 -1))
7559 value)))
7561 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7562 "Expand group tags in FILTER for the agenda.
7563 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7564 (if org-group-tags
7565 (let ((case-fold-search t) rtn)
7566 (mapc
7567 (lambda (f)
7568 (let (f0 dir)
7569 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7570 (setq dir (match-string 1 f) f0 (match-string 2 f))
7571 (setq dir (if no-operator "" "+") f0 f))
7572 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7573 (org-tags-expand f0 t t))
7574 rtn))))
7575 filter)
7576 (reverse rtn))
7577 filter))
7579 (defun org-agenda-filter-apply (filter type &optional expand)
7580 "Set FILTER as the new agenda filter and apply it. Optional
7581 argument EXPAND can be used for the TYPE tag and will expand the
7582 tags in the FILTER if any of the tags in FILTER are grouptags."
7583 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7584 (if org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7585 (let (tags cat txt)
7586 (setq org-agenda-filter-form (org-agenda-filter-make-matcher filter type expand))
7587 ;; Only set `org-agenda-filtered-by-category' to t when a unique
7588 ;; category is used as the filter:
7589 (setq org-agenda-filtered-by-category
7590 (and (eq type 'category)
7591 (not (equal (substring (car filter) 0 1) "-"))))
7592 (org-agenda-set-mode-name)
7593 (save-excursion
7594 (goto-char (point-min))
7595 (while (not (eobp))
7596 (if (org-get-at-bol 'org-marker)
7597 (progn
7598 (setq tags (org-get-at-bol 'tags)
7599 cat (org-get-at-eol 'org-category 1)
7600 txt (org-get-at-bol 'txt))
7601 (if (not (eval org-agenda-filter-form))
7602 (org-agenda-filter-hide-line type))
7603 (beginning-of-line 2))
7604 (beginning-of-line 2))))
7605 (if (get-char-property (point) 'invisible)
7606 (ignore-errors (org-agenda-previous-line)))))
7608 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7609 "Filter by top headline HL."
7610 (org-agenda-set-mode-name)
7611 (save-excursion
7612 (goto-char (point-min))
7613 (while (not (eobp))
7614 (let* ((pos (org-get-at-bol 'org-hd-marker))
7615 (tophl (and pos (org-find-top-headline pos))))
7616 (if (and tophl (funcall (if negative 'identity 'not)
7617 (string= hl tophl)))
7618 (org-agenda-filter-hide-line 'top-headline)))
7619 (beginning-of-line 2)))
7620 (if (get-char-property (point) 'invisible)
7621 (org-agenda-previous-line))
7622 (setq org-agenda-top-headline-filter hl
7623 org-agenda-filtered-by-top-headline t))
7625 (defun org-agenda-filter-hide-line (type)
7626 "Hide lines with TYPE in the agenda buffer."
7627 (let* ((b (max (point-min) (1- (point-at-bol))))
7628 (e (point-at-eol)))
7629 (let ((inhibit-read-only t))
7630 (add-text-properties
7631 b e `(invisible org-filtered org-filter-type ,type)))))
7633 (defun org-agenda-remove-filter (type)
7634 (interactive)
7635 "Remove filter of type TYPE from the agenda buffer."
7636 (save-excursion
7637 (goto-char (point-min))
7638 (let ((inhibit-read-only t) pos)
7639 (while (setq pos (text-property-any (point) (point-max) 'org-filter-type type))
7640 (goto-char pos)
7641 (remove-text-properties
7642 (point) (next-single-property-change (point) 'org-filter-type)
7643 `(invisible org-filtered org-filter-type ,type))))
7644 (set (intern (format "org-agenda-%s-filter" (intern-soft type))) nil)
7645 (setq org-agenda-filter-form nil)
7646 (org-agenda-set-mode-name)
7647 (org-agenda-finalize)))
7649 (defun org-agenda-filter-show-all-tag nil
7650 (org-agenda-remove-filter 'tag))
7651 (defun org-agenda-filter-show-all-re nil
7652 (org-agenda-remove-filter 'regexp))
7653 (defun org-agenda-filter-show-all-effort nil
7654 (org-agenda-remove-filter 'effort))
7655 (defun org-agenda-filter-show-all-cat nil
7656 (org-agenda-remove-filter 'category))
7657 (defun org-agenda-filter-show-all-top-filter nil
7658 (org-agenda-remove-filter 'top-headline))
7660 (defun org-agenda-manipulate-query-add ()
7661 "Manipulate the query by adding a search term with positive selection.
7662 Positive selection means the term must be matched for selection of an entry."
7663 (interactive)
7664 (org-agenda-manipulate-query ?\[))
7665 (defun org-agenda-manipulate-query-subtract ()
7666 "Manipulate the query by adding a search term with negative selection.
7667 Negative selection means term must not be matched for selection of an entry."
7668 (interactive)
7669 (org-agenda-manipulate-query ?\]))
7670 (defun org-agenda-manipulate-query-add-re ()
7671 "Manipulate the query by adding a search regexp with positive selection.
7672 Positive selection means the regexp must match for selection of an entry."
7673 (interactive)
7674 (org-agenda-manipulate-query ?\{))
7675 (defun org-agenda-manipulate-query-subtract-re ()
7676 "Manipulate the query by adding a search regexp with negative selection.
7677 Negative selection means regexp must not match for selection of an entry."
7678 (interactive)
7679 (org-agenda-manipulate-query ?\}))
7680 (defun org-agenda-manipulate-query (char)
7681 (cond
7682 ((memq org-agenda-type '(timeline agenda))
7683 (let ((org-agenda-include-inactive-timestamps t))
7684 (org-agenda-redo))
7685 (message "Display now includes inactive timestamps as well"))
7686 ((eq org-agenda-type 'search)
7687 (org-add-to-string
7688 'org-agenda-query-string
7689 (if org-agenda-last-search-view-search-was-boolean
7690 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7691 (?\{ . " +{}") (?\} . " -{}"))))
7692 " "))
7693 (setq org-agenda-redo-command
7694 (list 'org-search-view
7695 (car (get-text-property (min (1- (point-max)) (point))
7696 'org-last-args))
7697 org-agenda-query-string
7698 (+ (length org-agenda-query-string)
7699 (if (member char '(?\{ ?\})) 0 1))))
7700 (set-register org-agenda-query-register org-agenda-query-string)
7701 (let ((org-agenda-overriding-arguments
7702 (cdr org-agenda-redo-command)))
7703 (org-agenda-redo)))
7704 (t (error "Cannot manipulate query for %s-type agenda buffers"
7705 org-agenda-type))))
7707 (defun org-add-to-string (var string)
7708 (set var (concat (symbol-value var) string)))
7710 (defun org-agenda-goto-date (span)
7711 "Jump to DATE in agenda."
7712 (interactive "P")
7713 (let* ((org-read-date-prefer-future
7714 (eval org-agenda-jump-prefer-future))
7715 (date (org-read-date))
7716 (day (time-to-days (org-time-string-to-time date)))
7717 (org-agenda-sticky-orig org-agenda-sticky)
7718 (org-agenda-buffer-tmp-name (buffer-name))
7719 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7720 (0-arg (or current-prefix-arg (car args)))
7721 (2-arg (nth 2 args))
7722 (with-hour-p (nth 4 org-agenda-redo-command))
7723 (newcmd (list 'org-agenda-list 0-arg date
7724 (org-agenda-span-to-ndays
7725 2-arg (org-time-string-to-absolute date))
7726 with-hour-p))
7727 (newargs (cdr newcmd))
7728 (inhibit-read-only t)
7729 org-agenda-sticky)
7730 (if (not (org-agenda-check-type t 'agenda))
7731 (error "Not available in non-agenda views")
7732 (add-text-properties (point-min) (point-max)
7733 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7734 (org-agenda-redo)
7735 (goto-char (point-min))
7736 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7737 (save-excursion (move-beginning-of-line 2) (eobp))))
7738 (move-beginning-of-line 2))
7739 (setq org-agenda-sticky org-agenda-sticky-orig
7740 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7742 (defun org-agenda-goto-today ()
7743 "Go to today."
7744 (interactive)
7745 (org-agenda-check-type t 'timeline 'agenda)
7746 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7747 (curspan (nth 2 args))
7748 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7749 (cond
7750 (tdpos (goto-char tdpos))
7751 ((eq org-agenda-type 'agenda)
7752 (let* ((sd (org-agenda-compute-starting-span
7753 (org-today) (or curspan org-agenda-span)))
7754 (org-agenda-overriding-arguments args))
7755 (setf (nth 1 org-agenda-overriding-arguments) sd)
7756 (org-agenda-redo)
7757 (org-agenda-find-same-or-today-or-agenda)))
7758 (t (error "Cannot find today")))))
7760 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7761 (goto-char
7762 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7763 (text-property-any (point-min) (point-max) 'org-today t)
7764 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7765 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7766 (org-agenda-backward-block))
7767 (point-min))))
7769 (defun org-agenda-backward-block ()
7770 "Move backward by one agenda block."
7771 (interactive)
7772 (org-agenda-forward-block 'backward))
7774 (defun org-agenda-forward-block (&optional backward)
7775 "Move forward by one agenda block.
7776 When optional argument BACKWARD is set, go backward"
7777 (interactive)
7778 (cond ((not (derived-mode-p 'org-agenda-mode))
7779 (user-error
7780 "Cannot execute this command outside of org-agenda-mode buffers"))
7781 ((looking-at (if backward "\\`" "\\'"))
7782 (message "Already at the %s block" (if backward "first" "last")))
7783 (t (let ((pos (prog1 (point)
7784 (ignore-errors (if backward (backward-char 1)
7785 (move-end-of-line 1)))))
7786 (f (if backward
7787 'previous-single-property-change
7788 'next-single-property-change))
7789 moved dest)
7790 (while (and (setq dest (funcall
7791 f (point) 'org-agenda-structural-header))
7792 (not (get-text-property
7793 (point) 'org-agenda-structural-header)))
7794 (setq moved t)
7795 (goto-char dest))
7796 (if moved (move-beginning-of-line 1)
7797 (goto-char (if backward (point-min) (point-max)))
7798 (move-beginning-of-line 1)
7799 (message "No %s block" (if backward "previous" "further")))))))
7801 (defun org-agenda-later (arg)
7802 "Go forward in time by the current span.
7803 With prefix ARG, go forward that many times the current span."
7804 (interactive "p")
7805 (org-agenda-check-type t 'agenda)
7806 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7807 (span (or (nth 2 args) org-agenda-current-span))
7808 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7809 (greg (calendar-gregorian-from-absolute sd))
7810 (cnt (org-get-at-bol 'org-day-cnt))
7811 greg2)
7812 (cond
7813 ((numberp span)
7814 (setq sd (+ (* span arg) sd)))
7815 ((eq span 'day)
7816 (setq sd (+ arg sd)))
7817 ((eq span 'week)
7818 (setq sd (+ (* 7 arg) sd)))
7819 ((eq span 'fortnight)
7820 (setq sd (+ (* 14 arg) sd)))
7821 ((eq span 'month)
7822 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7823 sd (calendar-absolute-from-gregorian greg2))
7824 (setcar greg2 (1+ (car greg2))))
7825 ((eq span 'year)
7826 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7827 sd (calendar-absolute-from-gregorian greg2))
7828 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7830 (setq sd (+ (* span arg) sd))))
7831 (let ((org-agenda-overriding-cmd
7832 ;; `cmd' may have been set by `org-agenda-run-series' which
7833 ;; uses `org-agenda-overriding-cmd' to decide whether
7834 ;; overriding is allowed for `cmd'
7835 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7836 (org-agenda-overriding-arguments
7837 (list (car args) sd span)))
7838 (org-agenda-redo)
7839 (org-agenda-find-same-or-today-or-agenda cnt))))
7841 (defun org-agenda-earlier (arg)
7842 "Go backward in time by the current span.
7843 With prefix ARG, go backward that many times the current span."
7844 (interactive "p")
7845 (org-agenda-later (- arg)))
7847 (defun org-agenda-view-mode-dispatch ()
7848 "Call one of the view mode commands."
7849 (interactive)
7850 (message "View: [d]ay [w]eek for[t]night [m]onth [y]ear [SPC]reset [q]uit/abort
7851 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7852 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7853 (pcase (read-char-exclusive)
7854 (?\ (call-interactively 'org-agenda-reset-view))
7855 (?d (call-interactively 'org-agenda-day-view))
7856 (?w (call-interactively 'org-agenda-week-view))
7857 (?t (call-interactively 'org-agenda-fortnight-view))
7858 (?m (call-interactively 'org-agenda-month-view))
7859 (?y (call-interactively 'org-agenda-year-view))
7860 (?l (call-interactively 'org-agenda-log-mode))
7861 (?L (org-agenda-log-mode '(4)))
7862 (?c (org-agenda-log-mode 'clockcheck))
7863 ((or ?F ?f) (call-interactively 'org-agenda-follow-mode))
7864 (?a (call-interactively 'org-agenda-archives-mode))
7865 (?A (org-agenda-archives-mode 'files))
7866 ((or ?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7867 ((or ?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7868 (?G (call-interactively 'org-agenda-toggle-time-grid))
7869 (?D (call-interactively 'org-agenda-toggle-diary))
7870 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7871 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7872 (org-agenda-check-type t 'timeline 'agenda)
7873 (org-agenda-redo))
7874 (message "Display now includes inactive timestamps as well"))
7875 (?q (message "Abort"))
7876 (key (user-error "Invalid key: %s" key))))
7878 (defun org-agenda-reset-view ()
7879 "Switch to default view for agenda."
7880 (interactive)
7881 (org-agenda-change-time-span org-agenda-span))
7883 (defun org-agenda-day-view (&optional day-of-month)
7884 "Switch to daily view for agenda.
7885 With argument DAY-OF-MONTH, switch to that day of the month."
7886 (interactive "P")
7887 (org-agenda-change-time-span 'day day-of-month))
7889 (defun org-agenda-week-view (&optional iso-week)
7890 "Switch to weekly view for agenda.
7891 With argument ISO-WEEK, switch to the corresponding ISO week.
7892 If ISO-WEEK has more then 2 digits, only the last two encode
7893 the week. Any digits before this encode a year. So 200712
7894 means week 12 of year 2007. Years ranging from 70 years ago
7895 to 30 years in the future can also be written as 2-digit years."
7896 (interactive "P")
7897 (org-agenda-change-time-span 'week iso-week))
7899 (defun org-agenda-fortnight-view (&optional iso-week)
7900 "Switch to fortnightly view for agenda.
7901 With argument ISO-WEEK, switch to the corresponding ISO week.
7902 If ISO-WEEK has more then 2 digits, only the last two encode
7903 the week. Any digits before this encode a year. So 200712
7904 means week 12 of year 2007. Years ranging from 70 years ago
7905 to 30 years in the future can also be written as 2-digit years."
7906 (interactive "P")
7907 (org-agenda-change-time-span 'fortnight iso-week))
7909 (defun org-agenda-month-view (&optional month)
7910 "Switch to monthly view for agenda.
7911 With argument MONTH, switch to that month. If MONTH has more
7912 then 2 digits, only the last two encode the month. Any digits
7913 before this encode a year. So 200712 means December year 2007.
7914 Years ranging from 70 years ago to 30 years in the future can
7915 also be written as 2-digit years."
7916 (interactive "P")
7917 (org-agenda-change-time-span 'month month))
7919 (defun org-agenda-year-view (&optional year)
7920 "Switch to yearly view for agenda.
7921 With argument YEAR, switch to that year. Years ranging from 70
7922 years ago to 30 years in the future can also be written as
7923 2-digit years."
7924 (interactive "P")
7925 (when year
7926 (setq year (org-small-year-to-year year)))
7927 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
7928 (org-agenda-change-time-span 'year year)
7929 (error "Abort")))
7931 (defun org-agenda-change-time-span (span &optional n)
7932 "Change the agenda view to SPAN.
7933 SPAN may be `day', `week', `fortnight', `month', `year'."
7934 (org-agenda-check-type t 'agenda)
7935 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7936 (curspan (nth 2 args)))
7937 (if (and (not n) (equal curspan span))
7938 (error "Viewing span is already \"%s\"" span))
7939 (let* ((sd (or (org-get-at-bol 'day)
7940 (nth 1 args)
7941 org-starting-day))
7942 (sd (org-agenda-compute-starting-span sd span n))
7943 (org-agenda-overriding-cmd
7944 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7945 (org-agenda-overriding-arguments
7946 (list (car args) sd span)))
7947 (org-agenda-redo)
7948 (org-agenda-find-same-or-today-or-agenda))
7949 (org-agenda-set-mode-name)
7950 (message "Switched to %s view" span)))
7952 (defun org-agenda-compute-starting-span (sd span &optional n)
7953 "Compute starting date for agenda.
7954 SPAN may be `day', `week', `fortnight', `month', `year'. The return value
7955 is a cons cell with the starting date and the number of days,
7956 so that the date SD will be in that range."
7957 (let* ((greg (calendar-gregorian-from-absolute sd))
7958 (dg (nth 1 greg))
7959 (mg (car greg))
7960 (yg (nth 2 greg)))
7961 (cond
7962 ((eq span 'day)
7963 (when n
7964 (setq sd (+ (calendar-absolute-from-gregorian
7965 (list mg 1 yg))
7966 n -1))))
7967 ((or (eq span 'week) (eq span 'fortnight))
7968 (let* ((nt (calendar-day-of-week
7969 (calendar-gregorian-from-absolute sd)))
7970 (d (if org-agenda-start-on-weekday
7971 (- nt org-agenda-start-on-weekday)
7974 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
7975 (when n
7976 (require 'cal-iso)
7977 (when (> n 99)
7978 (setq y1 (org-small-year-to-year (/ n 100))
7979 n (mod n 100)))
7980 (setq sd
7981 (calendar-iso-to-absolute
7982 (list n 1
7983 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
7984 ((eq span 'month)
7985 (let (y1)
7986 (when (and n (> n 99))
7987 (setq y1 (org-small-year-to-year (/ n 100))
7988 n (mod n 100)))
7989 (setq sd (calendar-absolute-from-gregorian
7990 (list (or n mg) 1 (or y1 yg))))))
7991 ((eq span 'year)
7992 (setq sd (calendar-absolute-from-gregorian
7993 (list 1 1 (or n yg))))))
7994 sd))
7996 (defun org-agenda-next-date-line (&optional arg)
7997 "Jump to the next line indicating a date in agenda buffer."
7998 (interactive "p")
7999 (org-agenda-check-type t 'agenda 'timeline)
8000 (beginning-of-line 1)
8001 ;; This does not work if user makes date format that starts with a blank
8002 (if (looking-at "^\\S-") (forward-char 1))
8003 (if (not (re-search-forward "^\\S-" nil t arg))
8004 (progn
8005 (backward-char 1)
8006 (error "No next date after this line in this buffer")))
8007 (goto-char (match-beginning 0)))
8009 (defun org-agenda-previous-date-line (&optional arg)
8010 "Jump to the previous line indicating a date in agenda buffer."
8011 (interactive "p")
8012 (org-agenda-check-type t 'agenda 'timeline)
8013 (beginning-of-line 1)
8014 (if (not (re-search-backward "^\\S-" nil t arg))
8015 (error "No previous date before this line in this buffer")))
8017 ;; Initialize the highlight
8018 (defvar org-hl (make-overlay 1 1))
8019 (overlay-put org-hl 'face 'highlight)
8021 (defun org-highlight (begin end &optional buffer)
8022 "Highlight a region with overlay."
8023 (move-overlay org-hl begin end (or buffer (current-buffer))))
8025 (defun org-unhighlight ()
8026 "Detach overlay INDEX."
8027 (delete-overlay org-hl))
8029 (defun org-unhighlight-once ()
8030 "Remove the highlight from its position, and this function from the hook."
8031 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8032 (org-unhighlight))
8034 (defvar org-agenda-pre-follow-window-conf nil)
8035 (defun org-agenda-follow-mode ()
8036 "Toggle follow mode in an agenda buffer."
8037 (interactive)
8038 (unless org-agenda-follow-mode
8039 (setq org-agenda-pre-follow-window-conf
8040 (current-window-configuration)))
8041 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8042 (unless org-agenda-follow-mode
8043 (set-window-configuration org-agenda-pre-follow-window-conf))
8044 (org-agenda-set-mode-name)
8045 (org-agenda-do-context-action)
8046 (message "Follow mode is %s"
8047 (if org-agenda-follow-mode "on" "off")))
8049 (defun org-agenda-entry-text-mode (&optional arg)
8050 "Toggle entry text mode in an agenda buffer."
8051 (interactive "P")
8052 (if (or org-agenda-tag-filter
8053 org-agenda-category-filter
8054 org-agenda-regexp-filter
8055 org-agenda-top-headline-filter)
8056 (user-error "Can't show entry text in filtered views")
8057 (setq org-agenda-entry-text-mode (or (integerp arg)
8058 (not org-agenda-entry-text-mode)))
8059 (org-agenda-entry-text-hide)
8060 (and org-agenda-entry-text-mode
8061 (let ((org-agenda-entry-text-maxlines
8062 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8063 (org-agenda-entry-text-show)))
8064 (org-agenda-set-mode-name)
8065 (message "Entry text mode is %s%s"
8066 (if org-agenda-entry-text-mode "on" "off")
8067 (if (not org-agenda-entry-text-mode) ""
8068 (format " (maximum number of lines is %d)"
8069 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8071 (defun org-agenda-clockreport-mode ()
8072 "Toggle clocktable mode in an agenda buffer."
8073 (interactive)
8074 (org-agenda-check-type t 'agenda)
8075 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
8076 (setq org-agenda-start-with-clockreport-mode org-agenda-clockreport-mode)
8077 (org-agenda-set-mode-name)
8078 (org-agenda-redo)
8079 (message "Clocktable mode is %s"
8080 (if org-agenda-clockreport-mode "on" "off")))
8082 (defun org-agenda-log-mode (&optional special)
8083 "Toggle log mode in an agenda buffer.
8085 With argument SPECIAL, show all possible log items, not only the ones
8086 configured in `org-agenda-log-mode-items'.
8088 With a `\\[universal-argument] \\[universal-argument]' prefix, show *only* \
8089 log items, nothing else."
8090 (interactive "P")
8091 (org-agenda-check-type t 'agenda 'timeline)
8092 (setq org-agenda-show-log
8093 (cond
8094 ((equal special '(16)) 'only)
8095 ((eq special 'clockcheck)
8096 (if (eq org-agenda-show-log 'clockcheck)
8097 nil 'clockcheck))
8098 (special '(closed clock state))
8099 (t (not org-agenda-show-log))))
8100 (setq org-agenda-start-with-log-mode org-agenda-show-log)
8101 (org-agenda-set-mode-name)
8102 (org-agenda-redo)
8103 (message "Log mode is %s" (if org-agenda-show-log "on" "off")))
8105 (defun org-agenda-archives-mode (&optional with-files)
8106 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8107 When called with a prefix argument, include all archive files as well."
8108 (interactive "P")
8109 (setq org-agenda-archives-mode
8110 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8111 (org-agenda-set-mode-name)
8112 (org-agenda-redo)
8113 (message
8114 "%s"
8115 (cond
8116 ((eq org-agenda-archives-mode nil)
8117 "No archives are included")
8118 ((eq org-agenda-archives-mode 'trees)
8119 (format "Trees with :%s: tag are included" org-archive-tag))
8120 ((eq org-agenda-archives-mode t)
8121 (format "Trees with :%s: tag and all active archive files are included"
8122 org-archive-tag)))))
8124 (defun org-agenda-toggle-diary ()
8125 "Toggle diary inclusion in an agenda buffer."
8126 (interactive)
8127 (org-agenda-check-type t 'agenda)
8128 (setq org-agenda-include-diary (not org-agenda-include-diary))
8129 (org-agenda-redo)
8130 (org-agenda-set-mode-name)
8131 (message "Diary inclusion turned %s"
8132 (if org-agenda-include-diary "on" "off")))
8134 (defun org-agenda-toggle-deadlines ()
8135 "Toggle inclusion of entries with a deadline in an agenda buffer."
8136 (interactive)
8137 (org-agenda-check-type t 'agenda)
8138 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8139 (org-agenda-redo)
8140 (org-agenda-set-mode-name)
8141 (message "Deadlines inclusion turned %s"
8142 (if org-agenda-include-deadlines "on" "off")))
8144 (defun org-agenda-toggle-time-grid ()
8145 "Toggle time grid in an agenda buffer."
8146 (interactive)
8147 (org-agenda-check-type t 'agenda)
8148 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8149 (org-agenda-redo)
8150 (org-agenda-set-mode-name)
8151 (message "Time-grid turned %s"
8152 (if org-agenda-use-time-grid "on" "off")))
8154 (defun org-agenda-set-mode-name ()
8155 "Set the mode name to indicate all the small mode settings."
8156 (setq mode-name
8157 (list "Org-Agenda"
8158 (if (get 'org-agenda-files 'org-restrict) " []" "")
8160 '(:eval (org-agenda-span-name org-agenda-current-span))
8161 (if org-agenda-follow-mode " Follow" "")
8162 (if org-agenda-entry-text-mode " ETxt" "")
8163 (if org-agenda-include-diary " Diary" "")
8164 (if org-agenda-include-deadlines " Ddl" "")
8165 (if org-agenda-use-time-grid " Grid" "")
8166 (if (and (boundp 'org-habit-show-habits)
8167 org-habit-show-habits) " Habit" "")
8168 (cond
8169 ((consp org-agenda-show-log) " LogAll")
8170 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8171 (org-agenda-show-log " Log")
8172 (t ""))
8173 (if (or org-agenda-category-filter
8174 (get 'org-agenda-category-filter :preset-filter))
8175 '(:eval (propertize
8176 (concat " <"
8177 (mapconcat
8178 'identity
8179 (append
8180 (get 'org-agenda-category-filter :preset-filter)
8181 org-agenda-category-filter)
8183 ">")
8184 'face 'org-agenda-filter-category
8185 'help-echo "Category used in filtering")) "")
8186 (if (or org-agenda-tag-filter
8187 (get 'org-agenda-tag-filter :preset-filter))
8188 '(:eval (propertize
8189 (concat " {"
8190 (mapconcat
8191 'identity
8192 (append
8193 (get 'org-agenda-tag-filter :preset-filter)
8194 org-agenda-tag-filter)
8196 "}")
8197 'face 'org-agenda-filter-tags
8198 'help-echo "Tags used in filtering")) "")
8199 (if (or org-agenda-effort-filter
8200 (get 'org-agenda-effort-filter :preset-filter))
8201 '(:eval (propertize
8202 (concat " {"
8203 (mapconcat
8204 'identity
8205 (append
8206 (get 'org-agenda-effort-filter :preset-filter)
8207 org-agenda-effort-filter)
8209 "}")
8210 'face 'org-agenda-filter-effort
8211 'help-echo "Effort conditions used in filtering")) "")
8212 (if (or org-agenda-regexp-filter
8213 (get 'org-agenda-regexp-filter :preset-filter))
8214 '(:eval (propertize
8215 (concat " ["
8216 (mapconcat
8217 'identity
8218 (append
8219 (get 'org-agenda-regexp-filter :preset-filter)
8220 org-agenda-regexp-filter)
8222 "]")
8223 'face 'org-agenda-filter-regexp
8224 'help-echo "Regexp used in filtering")) "")
8225 (if org-agenda-archives-mode
8226 (if (eq org-agenda-archives-mode t)
8227 " Archives"
8228 (format " :%s:" org-archive-tag))
8230 (if org-agenda-clockreport-mode " Clock" "")))
8231 (force-mode-line-update))
8233 (defun org-agenda-update-agenda-type ()
8234 "Update the agenda type after each command."
8235 (setq org-agenda-type
8236 (or (get-text-property (point) 'org-agenda-type)
8237 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8239 (defun org-agenda-next-line ()
8240 "Move cursor to the next line, and show if follow mode is active."
8241 (interactive)
8242 (call-interactively 'next-line)
8243 (org-agenda-do-context-action))
8245 (defun org-agenda-previous-line ()
8246 "Move cursor to the previous line, and show if follow-mode is active."
8247 (interactive)
8248 (call-interactively 'previous-line)
8249 (org-agenda-do-context-action))
8251 (defun org-agenda-next-item (n)
8252 "Move cursor to next agenda item."
8253 (interactive "p")
8254 (let ((col (current-column)))
8255 (dotimes (c n)
8256 (when (next-single-property-change (point-at-eol) 'org-marker)
8257 (move-end-of-line 1)
8258 (goto-char (next-single-property-change (point) 'org-marker))))
8259 (org-move-to-column col))
8260 (org-agenda-do-context-action))
8262 (defun org-agenda-previous-item (n)
8263 "Move cursor to next agenda item."
8264 (interactive "p")
8265 (dotimes (c n)
8266 (let ((col (current-column))
8267 (goto (save-excursion
8268 (move-end-of-line 0)
8269 (previous-single-property-change (point) 'org-marker))))
8270 (if goto (goto-char goto))
8271 (org-move-to-column col)))
8272 (org-agenda-do-context-action))
8274 (defun org-agenda-do-context-action ()
8275 "Show outline path and, maybe, follow mode window."
8276 (let ((m (org-get-at-bol 'org-marker)))
8277 (when (and (markerp m) (marker-buffer m))
8278 (and org-agenda-follow-mode
8279 (if org-agenda-follow-indirect
8280 (org-agenda-tree-to-indirect-buffer nil)
8281 (org-agenda-show)))
8282 (and org-agenda-show-outline-path
8283 (org-with-point-at m (org-display-outline-path t))))))
8285 (defun org-agenda-show-tags ()
8286 "Show the tags applicable to the current item."
8287 (interactive)
8288 (let* ((tags (org-get-at-bol 'tags)))
8289 (if tags
8290 (message "Tags are :%s:"
8291 (org-no-properties (mapconcat 'identity tags ":")))
8292 (message "No tags associated with this line"))))
8294 (defun org-agenda-goto (&optional highlight)
8295 "Go to the entry at point in the corresponding Org file."
8296 (interactive)
8297 (let* ((marker (or (org-get-at-bol 'org-marker)
8298 (org-agenda-error)))
8299 (buffer (marker-buffer marker))
8300 (pos (marker-position marker)))
8301 (switch-to-buffer-other-window buffer)
8302 (widen)
8303 (push-mark)
8304 (goto-char pos)
8305 (when (derived-mode-p 'org-mode)
8306 (org-show-context 'agenda)
8307 (recenter (/ (window-height) 2))
8308 (org-back-to-heading t)
8309 (let ((case-fold-search nil))
8310 (when (re-search-forward org-complex-heading-regexp nil t)
8311 (goto-char (match-beginning 4)))))
8312 (run-hooks 'org-agenda-after-show-hook)
8313 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8315 (defvar org-agenda-after-show-hook nil
8316 "Normal hook run after an item has been shown from the agenda.
8317 Point is in the buffer where the item originated.")
8319 (defun org-agenda-kill ()
8320 "Kill the entry or subtree belonging to the current agenda entry."
8321 (interactive)
8322 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8323 (let* ((bufname-orig (buffer-name))
8324 (marker (or (org-get-at-bol 'org-marker)
8325 (org-agenda-error)))
8326 (buffer (marker-buffer marker))
8327 (pos (marker-position marker))
8328 (type (org-get-at-bol 'type))
8329 dbeg dend (n 0) conf)
8330 (org-with-remote-undo buffer
8331 (with-current-buffer buffer
8332 (save-excursion
8333 (goto-char pos)
8334 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8335 (setq dbeg (progn (org-back-to-heading t) (point))
8336 dend (org-end-of-subtree t t))
8337 (setq dbeg (point-at-bol)
8338 dend (min (point-max) (1+ (point-at-eol)))))
8339 (goto-char dbeg)
8340 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8341 (setq conf (or (eq t org-agenda-confirm-kill)
8342 (and (numberp org-agenda-confirm-kill)
8343 (> n org-agenda-confirm-kill))))
8344 (and conf
8345 (not (y-or-n-p
8346 (format "Delete entry with %d lines in buffer \"%s\"? "
8347 n (buffer-name buffer))))
8348 (error "Abort"))
8349 (let ((org-agenda-buffer-name bufname-orig))
8350 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8351 (with-current-buffer buffer (delete-region dbeg dend))
8352 (message "Agenda item and source killed"))))
8354 (defvar org-archive-default-command) ; defined in org-archive.el
8355 (defun org-agenda-archive-default ()
8356 "Archive the entry or subtree belonging to the current agenda entry."
8357 (interactive)
8358 (require 'org-archive)
8359 (org-agenda-archive-with org-archive-default-command))
8361 (defun org-agenda-archive-default-with-confirmation ()
8362 "Archive the entry or subtree belonging to the current agenda entry."
8363 (interactive)
8364 (require 'org-archive)
8365 (org-agenda-archive-with org-archive-default-command 'confirm))
8367 (defun org-agenda-archive ()
8368 "Archive the entry or subtree belonging to the current agenda entry."
8369 (interactive)
8370 (org-agenda-archive-with 'org-archive-subtree))
8372 (defun org-agenda-archive-to-archive-sibling ()
8373 "Move the entry to the archive sibling."
8374 (interactive)
8375 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8377 (defun org-agenda-archive-with (cmd &optional confirm)
8378 "Move the entry to the archive sibling."
8379 (interactive)
8380 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8381 (let* ((bufname-orig (buffer-name))
8382 (marker (or (org-get-at-bol 'org-marker)
8383 (org-agenda-error)))
8384 (buffer (marker-buffer marker))
8385 (pos (marker-position marker)))
8386 (org-with-remote-undo buffer
8387 (with-current-buffer buffer
8388 (if (derived-mode-p 'org-mode)
8389 (if (and confirm
8390 (not (y-or-n-p "Archive this subtree or entry? ")))
8391 (error "Abort")
8392 (save-window-excursion
8393 (goto-char pos)
8394 (let ((org-agenda-buffer-name bufname-orig))
8395 (org-remove-subtree-entries-from-agenda))
8396 (org-back-to-heading t)
8397 (funcall cmd)))
8398 (error "Archiving works only in Org files"))))))
8400 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8401 "Remove all lines in the agenda that correspond to a given subtree.
8402 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8403 If this information is not given, the function uses the tree at point."
8404 (let ((buf (or buf (current-buffer))) m p)
8405 (save-excursion
8406 (unless (and beg end)
8407 (org-back-to-heading t)
8408 (setq beg (point))
8409 (org-end-of-subtree t)
8410 (setq end (point)))
8411 (set-buffer (get-buffer org-agenda-buffer-name))
8412 (save-excursion
8413 (goto-char (point-max))
8414 (beginning-of-line 1)
8415 (while (not (bobp))
8416 (when (and (setq m (org-get-at-bol 'org-marker))
8417 (equal buf (marker-buffer m))
8418 (setq p (marker-position m))
8419 (>= p beg)
8420 (< p end))
8421 (let ((inhibit-read-only t))
8422 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8423 (beginning-of-line 0))))))
8425 (defun org-agenda-refile (&optional goto rfloc no-update)
8426 "Refile the item at point.
8428 When called with `\\[universal-argument] \\[universal-argument]', \
8429 go to the location of the last
8430 refiled item.
8432 When called with `\\[universal-argument] \\[universal-argument] \
8433 \\[universal-argument]' prefix or when GOTO is 0, clear
8434 the refile cache.
8436 RFLOC can be a refile location obtained in a different way.
8438 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8439 (interactive "P")
8440 (cond
8441 ((member goto '(0 (64)))
8442 (org-refile-cache-clear))
8443 ((equal goto '(16))
8444 (org-refile-goto-last-stored))
8446 (let* ((buffer-orig (buffer-name))
8447 (marker (or (org-get-at-bol 'org-hd-marker)
8448 (org-agenda-error)))
8449 (buffer (marker-buffer marker))
8450 (pos (marker-position marker))
8451 (rfloc (or rfloc
8452 (org-refile-get-location
8453 (if goto "Goto" "Refile to") buffer
8454 org-refile-allow-creating-parent-nodes))))
8455 (with-current-buffer buffer
8456 (org-with-wide-buffer
8457 (goto-char marker)
8458 (let ((org-agenda-buffer-name buffer-orig))
8459 (org-remove-subtree-entries-from-agenda))
8460 (org-refile goto buffer rfloc))))
8461 (unless no-update (org-agenda-redo)))))
8463 (defun org-agenda-open-link (&optional arg)
8464 "Open the link(s) in the current entry, if any.
8465 This looks for a link in the displayed line in the agenda.
8466 It also looks at the text of the entry itself."
8467 (interactive "P")
8468 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8469 (org-get-at-bol 'org-marker)))
8470 (buffer (and marker (marker-buffer marker)))
8471 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8472 (lkall (and buffer (org-offer-links-in-entry
8473 buffer marker arg prefix)))
8474 (lk0 (car lkall))
8475 (lk (if (stringp lk0) (list lk0) lk0))
8476 (lkend (cdr lkall))
8477 trg)
8478 (cond
8479 ((and buffer lk)
8480 (mapcar (lambda(l)
8481 (with-current-buffer buffer
8482 (setq trg (and (string-match org-bracket-link-regexp l)
8483 (match-string 1 l)))
8484 (if (or (not trg) (string-match org-any-link-re trg))
8485 (org-with-wide-buffer
8486 (goto-char marker)
8487 (when (search-forward l nil lkend)
8488 (goto-char (match-beginning 0))
8489 (org-open-at-point)))
8490 ;; This is an internal link, widen the buffer
8491 (switch-to-buffer-other-window buffer)
8492 (widen)
8493 (goto-char marker)
8494 (when (search-forward l nil lkend)
8495 (goto-char (match-beginning 0))
8496 (org-open-at-point)))))
8497 lk))
8498 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8499 (save-excursion
8500 (beginning-of-line 1)
8501 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8502 (org-open-link-from-string (match-string 1)))
8503 (t (message "No link to open here")))))
8505 (defun org-agenda-copy-local-variable (var)
8506 "Get a variable from a referenced buffer and install it here."
8507 (let ((m (org-get-at-bol 'org-marker)))
8508 (when (and m (buffer-live-p (marker-buffer m)))
8509 (set (make-local-variable var)
8510 (with-current-buffer (marker-buffer m)
8511 (symbol-value var))))))
8513 (defun org-agenda-switch-to (&optional delete-other-windows)
8514 "Go to the Org mode file which contains the item at point.
8515 When optional argument DELETE-OTHER-WINDOWS is non-nil, the
8516 displayed Org file fills the frame."
8517 (interactive)
8518 (if (and org-return-follows-link
8519 (not (org-get-at-bol 'org-marker))
8520 (org-in-regexp org-bracket-link-regexp))
8521 (org-open-link-from-string (match-string 0))
8522 (let* ((marker (or (org-get-at-bol 'org-marker)
8523 (org-agenda-error)))
8524 (buffer (marker-buffer marker))
8525 (pos (marker-position marker)))
8526 (unless buffer (user-error "Trying to switch to non-existent buffer"))
8527 (pop-to-buffer-same-window buffer)
8528 (when delete-other-windows (delete-other-windows))
8529 (widen)
8530 (goto-char pos)
8531 (when (derived-mode-p 'org-mode)
8532 (org-show-context 'agenda)
8533 (run-hooks 'org-agenda-after-show-hook)))))
8535 (defun org-agenda-goto-mouse (ev)
8536 "Go to the Org file which contains the item at the mouse click."
8537 (interactive "e")
8538 (mouse-set-point ev)
8539 (org-agenda-goto))
8541 (defun org-agenda-show (&optional full-entry)
8542 "Display the Org file which contains the item at point.
8543 With prefix argument FULL-ENTRY, make the entire entry visible
8544 if it was hidden in the outline."
8545 (interactive "P")
8546 (let ((win (selected-window)))
8547 (org-agenda-goto t)
8548 (when full-entry (org-show-entry))
8549 (select-window win)))
8551 (defvar org-agenda-show-window nil)
8552 (defun org-agenda-show-and-scroll-up (&optional arg)
8553 "Display the Org file which contains the item at point.
8555 When called repeatedly, scroll the window that is displaying the buffer.
8557 With a `\\[universal-argument]' prefix, use `org-show-entry' instead of \
8558 `outline-show-subtree'
8559 to display the item, so that drawers and logbooks stay folded."
8560 (interactive "P")
8561 (let ((win (selected-window)))
8562 (if (and (window-live-p org-agenda-show-window)
8563 (eq this-command last-command))
8564 (progn
8565 (select-window org-agenda-show-window)
8566 (ignore-errors (scroll-up)))
8567 (org-agenda-goto t)
8568 (if arg (org-show-entry) (outline-show-subtree))
8569 (setq org-agenda-show-window (selected-window)))
8570 (select-window win)))
8572 (defun org-agenda-show-scroll-down ()
8573 "Scroll down the window showing the agenda."
8574 (interactive)
8575 (let ((win (selected-window)))
8576 (when (window-live-p org-agenda-show-window)
8577 (select-window org-agenda-show-window)
8578 (ignore-errors (scroll-down))
8579 (select-window win))))
8581 (defun org-agenda-show-1 (&optional more)
8582 "Display the Org file which contains the item at point.
8583 The prefix arg selects the amount of information to display:
8585 0 hide the subtree
8586 1 just show the entry according to defaults.
8587 2 show the children view
8588 3 show the subtree view
8589 4 show the entire subtree and any LOGBOOK drawers
8590 5 show the entire subtree and any drawers
8591 With prefix argument FULL-ENTRY, make the entire entry visible
8592 if it was hidden in the outline."
8593 (interactive "p")
8594 (let ((win (selected-window)))
8595 (org-agenda-goto t)
8596 (org-back-to-heading)
8597 (set-window-start (selected-window) (point-at-bol))
8598 (cond
8599 ((= more 0)
8600 (outline-hide-subtree)
8601 (save-excursion
8602 (org-back-to-heading)
8603 (run-hook-with-args 'org-cycle-hook 'folded))
8604 (message "Remote: FOLDED"))
8605 ((and (called-interactively-p 'any) (= more 1))
8606 (message "Remote: show with default settings"))
8607 ((= more 2)
8608 (outline-show-entry)
8609 (org-show-children)
8610 (save-excursion
8611 (org-back-to-heading)
8612 (run-hook-with-args 'org-cycle-hook 'children))
8613 (message "Remote: CHILDREN"))
8614 ((= more 3)
8615 (outline-show-subtree)
8616 (save-excursion
8617 (org-back-to-heading)
8618 (run-hook-with-args 'org-cycle-hook 'subtree))
8619 (message "Remote: SUBTREE"))
8620 ((= more 4)
8621 (outline-show-subtree)
8622 (save-excursion
8623 (org-back-to-heading)
8624 (org-cycle-hide-drawers 'subtree '("LOGBOOK")))
8625 (message "Remote: SUBTREE AND LOGBOOK"))
8626 ((> more 4)
8627 (outline-show-subtree)
8628 (message "Remote: SUBTREE AND ALL DRAWERS")))
8629 (select-window win)))
8631 (defvar org-agenda-cycle-counter nil)
8632 (defun org-agenda-cycle-show (&optional n)
8633 "Show the current entry in another window, with default settings.
8635 Default settings are taken from `org-show-context-detail'. When
8636 use repeatedly in immediate succession, the remote entry will
8637 cycle through visibility
8639 children -> subtree -> folded
8641 When called with a numeric prefix arg, that arg will be passed through to
8642 `org-agenda-show-1'. For the interpretation of that argument, see the
8643 docstring of `org-agenda-show-1'."
8644 (interactive "P")
8645 (if (integerp n)
8646 (setq org-agenda-cycle-counter n)
8647 (if (not (eq last-command this-command))
8648 (setq org-agenda-cycle-counter 1)
8649 (if (equal org-agenda-cycle-counter 0)
8650 (setq org-agenda-cycle-counter 2)
8651 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8652 (if (> org-agenda-cycle-counter 3)
8653 (setq org-agenda-cycle-counter 0)))))
8654 (org-agenda-show-1 org-agenda-cycle-counter))
8656 (defun org-agenda-recenter (arg)
8657 "Display the Org file which contains the item at point and recenter."
8658 (interactive "P")
8659 (let ((win (selected-window)))
8660 (org-agenda-goto t)
8661 (recenter arg)
8662 (select-window win)))
8664 (defun org-agenda-show-mouse (ev)
8665 "Display the Org file which contains the item at the mouse click."
8666 (interactive "e")
8667 (mouse-set-point ev)
8668 (org-agenda-show))
8670 (defun org-agenda-check-no-diary ()
8671 "Check if the entry is a diary link and abort if yes."
8672 (if (org-get-at-bol 'org-agenda-diary-link)
8673 (org-agenda-error)))
8675 (defun org-agenda-error ()
8676 "Throw an error when a command is not allowed in the agenda."
8677 (user-error "Command not allowed in this line"))
8679 (defun org-agenda-tree-to-indirect-buffer (arg)
8680 "Show the subtree corresponding to the current entry in an indirect buffer.
8681 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8683 With a numerical prefix ARG, go up to this level and then take that tree.
8684 With a negative numeric ARG, go up by this number of levels.
8686 With a `\\[universal-argument]' prefix, make a separate frame for this tree, \
8687 i.e. don't use
8688 the dedicated frame."
8689 (interactive "P")
8690 (if current-prefix-arg
8691 (org-agenda-do-tree-to-indirect-buffer arg)
8692 (let ((agenda-buffer (buffer-name))
8693 (agenda-window (selected-window))
8694 (indirect-window
8695 (and org-last-indirect-buffer
8696 (get-buffer-window org-last-indirect-buffer))))
8697 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8698 (unless (or (eq org-indirect-buffer-display 'new-frame)
8699 (eq org-indirect-buffer-display 'dedicated-frame))
8700 (unwind-protect
8701 (unless (and indirect-window (window-live-p indirect-window))
8702 (setq indirect-window (split-window agenda-window)))
8703 (and indirect-window (select-window indirect-window))
8704 (switch-to-buffer org-last-indirect-buffer :norecord)
8705 (fit-window-to-buffer indirect-window)))
8706 (select-window (get-buffer-window agenda-buffer))
8707 (setq org-agenda-last-indirect-buffer org-last-indirect-buffer))))
8709 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8710 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8711 (org-agenda-check-no-diary)
8712 (let* ((marker (or (org-get-at-bol 'org-marker)
8713 (org-agenda-error)))
8714 (buffer (marker-buffer marker))
8715 (pos (marker-position marker)))
8716 (with-current-buffer buffer
8717 (save-excursion
8718 (goto-char pos)
8719 (funcall 'org-tree-to-indirect-buffer arg)))))
8721 (defvar org-last-heading-marker (make-marker)
8722 "Marker pointing to the headline that last changed its TODO state
8723 by a remote command from the agenda.")
8725 (defun org-agenda-todo-nextset ()
8726 "Switch TODO entry to next sequence."
8727 (interactive)
8728 (org-agenda-todo 'nextset))
8730 (defun org-agenda-todo-previousset ()
8731 "Switch TODO entry to previous sequence."
8732 (interactive)
8733 (org-agenda-todo 'previousset))
8735 (defun org-agenda-todo (&optional arg)
8736 "Cycle TODO state of line at point, also in Org file.
8737 This changes the line at point, all other lines in the agenda referring to
8738 the same tree node, and the headline of the tree node in the Org file."
8739 (interactive "P")
8740 (org-agenda-check-no-diary)
8741 (let* ((col (current-column))
8742 (marker (or (org-get-at-bol 'org-marker)
8743 (org-agenda-error)))
8744 (buffer (marker-buffer marker))
8745 (pos (marker-position marker))
8746 (hdmarker (org-get-at-bol 'org-hd-marker))
8747 (todayp (org-agenda-today-p (org-get-at-bol 'day)))
8748 (inhibit-read-only t)
8749 org-agenda-headline-snapshot-before-repeat newhead just-one)
8750 (org-with-remote-undo buffer
8751 (with-current-buffer buffer
8752 (widen)
8753 (goto-char pos)
8754 (org-show-context 'agenda)
8755 (let ((current-prefix-arg arg))
8756 (call-interactively 'org-todo))
8757 (and (bolp) (forward-char 1))
8758 (setq newhead (org-get-heading))
8759 (when (and (bound-and-true-p
8760 org-agenda-headline-snapshot-before-repeat)
8761 (not (equal org-agenda-headline-snapshot-before-repeat
8762 newhead))
8763 todayp)
8764 (setq newhead org-agenda-headline-snapshot-before-repeat
8765 just-one t))
8766 (save-excursion
8767 (org-back-to-heading)
8768 (move-marker org-last-heading-marker (point))))
8769 (beginning-of-line 1)
8770 (save-window-excursion
8771 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8772 (when (bound-and-true-p org-clock-out-when-done)
8773 (string-match (concat "^" (regexp-opt org-done-keywords-for-agenda))
8774 newhead)
8775 (org-agenda-unmark-clocking-task))
8776 (org-move-to-column col)
8777 (org-agenda-mark-clocking-task))))
8779 (defun org-agenda-add-note (&optional arg)
8780 "Add a time-stamped note to the entry at point."
8781 (interactive "P")
8782 (org-agenda-check-no-diary)
8783 (let* ((marker (or (org-get-at-bol 'org-marker)
8784 (org-agenda-error)))
8785 (buffer (marker-buffer marker))
8786 (pos (marker-position marker))
8787 (hdmarker (org-get-at-bol 'org-hd-marker))
8788 (inhibit-read-only t))
8789 (with-current-buffer buffer
8790 (widen)
8791 (goto-char pos)
8792 (org-show-context 'agenda)
8793 (org-add-note))))
8795 (defun org-agenda-change-all-lines (newhead hdmarker
8796 &optional fixface just-this)
8797 "Change all lines in the agenda buffer which match HDMARKER.
8798 The new content of the line will be NEWHEAD (as modified by
8799 `org-agenda-format-item'). HDMARKER is checked with
8800 `equal' against all `org-hd-marker' text properties in the file.
8801 If FIXFACE is non-nil, the face of each item is modified according to
8802 the new TODO state.
8803 If JUST-THIS is non-nil, change just the current line, not all.
8804 If FORCE-TAGS is non nil, the car of it returns the new tags."
8805 (let* ((inhibit-read-only t)
8806 (line (org-current-line))
8807 (org-agenda-buffer (current-buffer))
8808 (thetags (with-current-buffer (marker-buffer hdmarker)
8809 (org-with-wide-buffer
8810 (goto-char hdmarker)
8811 (org-get-tags-at))))
8812 props m pl undone-face done-face finish new dotime level cat tags)
8813 (save-excursion
8814 (goto-char (point-max))
8815 (beginning-of-line 1)
8816 (while (not finish)
8817 (setq finish (bobp))
8818 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8819 (or (not just-this) (= (org-current-line) line))
8820 (equal m hdmarker))
8821 (setq props (text-properties-at (point))
8822 dotime (org-get-at-bol 'dotime)
8823 cat (org-get-at-eol 'org-category 1)
8824 level (org-get-at-bol 'level)
8825 tags thetags
8827 (let ((org-prefix-format-compiled
8828 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8829 org-prefix-format-compiled))
8830 (extra (org-get-at-bol 'extra)))
8831 (with-current-buffer (marker-buffer hdmarker)
8832 (org-with-wide-buffer
8833 (org-agenda-format-item extra newhead level cat tags dotime))))
8834 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8835 undone-face (org-get-at-bol 'undone-face)
8836 done-face (org-get-at-bol 'done-face))
8837 (beginning-of-line 1)
8838 (cond
8839 ((equal new "") (delete-region (point) (line-beginning-position 2)))
8840 ((looking-at ".*")
8841 ;; When replacing the whole line, preserve bulk mark
8842 ;; overlay, if any.
8843 (let ((mark (catch :overlay
8844 (dolist (o (overlays-in (point) (+ 2 (point))))
8845 (when (eq (overlay-get o 'type)
8846 'org-marked-entry-overlay)
8847 (throw :overlay o))))))
8848 (replace-match new t t)
8849 (beginning-of-line)
8850 (when mark (move-overlay mark (point) (+ 2 (point)))))
8851 (add-text-properties (point-at-bol) (point-at-eol) props)
8852 (when fixface
8853 (add-text-properties
8854 (point-at-bol) (point-at-eol)
8855 (list 'face
8856 (if org-last-todo-state-is-todo
8857 undone-face done-face))))
8858 (org-agenda-highlight-todo 'line)
8859 (beginning-of-line 1))
8860 (t (error "Line update did not work")))
8861 (save-restriction
8862 (narrow-to-region (point-at-bol) (point-at-eol))
8863 (org-agenda-finalize)))
8864 (beginning-of-line 0)))))
8866 (defun org-agenda-align-tags (&optional line)
8867 "Align all tags in agenda items to `org-agenda-tags-column'."
8868 (let ((inhibit-read-only t) l c)
8869 (save-excursion
8870 (goto-char (if line (point-at-bol) (point-min)))
8871 (while (re-search-forward "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"
8872 (if line (point-at-eol) nil) t)
8873 (add-text-properties
8874 (match-beginning 2) (match-end 2)
8875 (list 'face (delq nil (let ((prop (get-text-property
8876 (match-beginning 2) 'face)))
8877 (or (listp prop) (setq prop (list prop)))
8878 (if (memq 'org-tag prop)
8879 prop
8880 (cons 'org-tag prop))))))
8881 (setq l (- (match-end 2) (match-beginning 2))
8882 c (if (< org-agenda-tags-column 0)
8883 (- (abs org-agenda-tags-column) l)
8884 org-agenda-tags-column))
8885 (delete-region (match-beginning 1) (match-end 1))
8886 (goto-char (match-beginning 1))
8887 (insert (org-add-props
8888 (make-string (max 1 (- c (current-column))) ?\ )
8889 (plist-put (copy-sequence (text-properties-at (point)))
8890 'face nil))))
8891 (goto-char (point-min))
8892 (org-font-lock-add-tag-faces (point-max)))))
8894 (defun org-agenda-priority-up ()
8895 "Increase the priority of line at point, also in Org file."
8896 (interactive)
8897 (org-agenda-priority 'up))
8899 (defun org-agenda-priority-down ()
8900 "Decrease the priority of line at point, also in Org file."
8901 (interactive)
8902 (org-agenda-priority 'down))
8904 (defun org-agenda-priority (&optional force-direction)
8905 "Set the priority of line at point, also in Org file.
8906 This changes the line at point, all other lines in the agenda referring to
8907 the same tree node, and the headline of the tree node in the Org file.
8908 Called with a universal prefix arg, show the priority instead of setting it."
8909 (interactive "P")
8910 (if (equal force-direction '(4))
8911 (org-show-priority)
8912 (unless org-enable-priority-commands
8913 (error "Priority commands are disabled"))
8914 (org-agenda-check-no-diary)
8915 (let* ((col (current-column))
8916 (marker (or (org-get-at-bol 'org-marker)
8917 (org-agenda-error)))
8918 (hdmarker (org-get-at-bol 'org-hd-marker))
8919 (buffer (marker-buffer hdmarker))
8920 (pos (marker-position hdmarker))
8921 (inhibit-read-only t)
8922 newhead)
8923 (org-with-remote-undo buffer
8924 (with-current-buffer buffer
8925 (widen)
8926 (goto-char pos)
8927 (org-show-context 'agenda)
8928 (funcall 'org-priority force-direction)
8929 (end-of-line 1)
8930 (setq newhead (org-get-heading)))
8931 (org-agenda-change-all-lines newhead hdmarker)
8932 (org-move-to-column col)))))
8934 ;; FIXME: should fix the tags property of the agenda line.
8935 (defun org-agenda-set-tags (&optional tag onoff)
8936 "Set tags for the current headline."
8937 (interactive)
8938 (org-agenda-check-no-diary)
8939 (if (and (org-region-active-p) (called-interactively-p 'any))
8940 (call-interactively 'org-change-tag-in-region)
8941 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8942 (org-agenda-error)))
8943 (buffer (marker-buffer hdmarker))
8944 (pos (marker-position hdmarker))
8945 (inhibit-read-only t)
8946 newhead)
8947 (org-with-remote-undo buffer
8948 (with-current-buffer buffer
8949 (widen)
8950 (goto-char pos)
8951 (org-show-context 'agenda)
8952 (if tag
8953 (org-toggle-tag tag onoff)
8954 (call-interactively 'org-set-tags))
8955 (end-of-line 1)
8956 (setq newhead (org-get-heading)))
8957 (org-agenda-change-all-lines newhead hdmarker)
8958 (beginning-of-line 1)))))
8960 (defun org-agenda-set-property ()
8961 "Set a property for the current headline."
8962 (interactive)
8963 (org-agenda-check-no-diary)
8964 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8965 (org-agenda-error)))
8966 (buffer (marker-buffer hdmarker))
8967 (pos (marker-position hdmarker))
8968 (inhibit-read-only t)
8969 newhead)
8970 (org-with-remote-undo buffer
8971 (with-current-buffer buffer
8972 (widen)
8973 (goto-char pos)
8974 (org-show-context 'agenda)
8975 (call-interactively 'org-set-property)))))
8977 (defun org-agenda-set-effort ()
8978 "Set the effort property for the current headline."
8979 (interactive)
8980 (org-agenda-check-no-diary)
8981 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8982 (org-agenda-error)))
8983 (buffer (marker-buffer hdmarker))
8984 (pos (marker-position hdmarker))
8985 (inhibit-read-only t)
8986 newhead)
8987 (org-with-remote-undo buffer
8988 (with-current-buffer buffer
8989 (widen)
8990 (goto-char pos)
8991 (org-show-context 'agenda)
8992 (call-interactively 'org-set-effort)
8993 (end-of-line 1)
8994 (setq newhead (org-get-heading)))
8995 (org-agenda-change-all-lines newhead hdmarker))))
8997 (defun org-agenda-toggle-archive-tag ()
8998 "Toggle the archive tag for the current entry."
8999 (interactive)
9000 (org-agenda-check-no-diary)
9001 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9002 (org-agenda-error)))
9003 (buffer (marker-buffer hdmarker))
9004 (pos (marker-position hdmarker))
9005 (inhibit-read-only t)
9006 newhead)
9007 (org-with-remote-undo buffer
9008 (with-current-buffer buffer
9009 (widen)
9010 (goto-char pos)
9011 (org-show-context 'agenda)
9012 (call-interactively 'org-toggle-archive-tag)
9013 (end-of-line 1)
9014 (setq newhead (org-get-heading)))
9015 (org-agenda-change-all-lines newhead hdmarker)
9016 (beginning-of-line 1))))
9018 (defun org-agenda-do-date-later (arg)
9019 (interactive "P")
9020 (cond
9021 ((or (equal arg '(16))
9022 (memq last-command
9023 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9024 (setq this-command 'org-agenda-date-later-minutes)
9025 (org-agenda-date-later-minutes 1))
9026 ((or (equal arg '(4))
9027 (memq last-command
9028 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9029 (setq this-command 'org-agenda-date-later-hours)
9030 (org-agenda-date-later-hours 1))
9032 (org-agenda-date-later (prefix-numeric-value arg)))))
9034 (defun org-agenda-do-date-earlier (arg)
9035 (interactive "P")
9036 (cond
9037 ((or (equal arg '(16))
9038 (memq last-command
9039 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9040 (setq this-command 'org-agenda-date-earlier-minutes)
9041 (org-agenda-date-earlier-minutes 1))
9042 ((or (equal arg '(4))
9043 (memq last-command
9044 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9045 (setq this-command 'org-agenda-date-earlier-hours)
9046 (org-agenda-date-earlier-hours 1))
9048 (org-agenda-date-earlier (prefix-numeric-value arg)))))
9050 (defun org-agenda-date-later (arg &optional what)
9051 "Change the date of this item to ARG day(s) later."
9052 (interactive "p")
9053 (org-agenda-check-type t 'agenda 'timeline)
9054 (org-agenda-check-no-diary)
9055 (let* ((marker (or (org-get-at-bol 'org-marker)
9056 (org-agenda-error)))
9057 (buffer (marker-buffer marker))
9058 (pos (marker-position marker))
9059 cdate today)
9060 (org-with-remote-undo buffer
9061 (with-current-buffer buffer
9062 (widen)
9063 (goto-char pos)
9064 (if (not (org-at-timestamp-p))
9065 (error "Cannot find time stamp"))
9066 (when (and org-agenda-move-date-from-past-immediately-to-today
9067 (equal arg 1)
9068 (or (not what) (eq what 'day))
9069 (not (save-match-data (org-at-date-range-p))))
9070 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9071 cdate (calendar-absolute-from-gregorian
9072 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9073 today (org-today))
9074 (if (> today cdate)
9075 ;; immediately shift to today
9076 (setq arg (- today cdate))))
9077 (org-timestamp-change arg (or what 'day))
9078 (when (and (org-at-date-range-p)
9079 (re-search-backward org-tr-regexp-both (point-at-bol)))
9080 (let ((end org-last-changed-timestamp))
9081 (org-timestamp-change arg (or what 'day))
9082 (setq org-last-changed-timestamp
9083 (concat org-last-changed-timestamp "--" end)))))
9084 (org-agenda-show-new-time marker org-last-changed-timestamp))
9085 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9087 (defun org-agenda-date-earlier (arg &optional what)
9088 "Change the date of this item to ARG day(s) earlier."
9089 (interactive "p")
9090 (org-agenda-date-later (- arg) what))
9092 (defun org-agenda-date-later-minutes (arg)
9093 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9094 (interactive "p")
9095 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9096 (org-agenda-date-later arg 'minute))
9098 (defun org-agenda-date-earlier-minutes (arg)
9099 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9100 (interactive "p")
9101 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9102 (org-agenda-date-earlier arg 'minute))
9104 (defun org-agenda-date-later-hours (arg)
9105 "Change the time of this item, in hour steps."
9106 (interactive "p")
9107 (org-agenda-date-later arg 'hour))
9109 (defun org-agenda-date-earlier-hours (arg)
9110 "Change the time of this item, in hour steps."
9111 (interactive "p")
9112 (org-agenda-date-earlier arg 'hour))
9114 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9115 "Show new date stamp via text properties."
9116 ;; We use text properties to make this undoable
9117 (let ((inhibit-read-only t))
9118 (setq stamp (concat prefix " => " stamp " "))
9119 (save-excursion
9120 (goto-char (point-max))
9121 (while (not (bobp))
9122 (when (equal marker (org-get-at-bol 'org-marker))
9123 (remove-text-properties (point-at-bol) (point-at-eol) '(display))
9124 (org-move-to-column (- (window-width) (length stamp)) t)
9125 (add-text-properties
9126 (1- (point)) (point-at-eol)
9127 (list 'display (org-add-props stamp nil
9128 'face '(secondary-selection default))))
9129 (beginning-of-line 1))
9130 (beginning-of-line 0)))))
9132 (defun org-agenda-date-prompt (arg)
9133 "Change the date of this item. Date is prompted for, with default today.
9134 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9135 be used to request time specification in the time stamp."
9136 (interactive "P")
9137 (org-agenda-check-type t 'agenda 'timeline)
9138 (org-agenda-check-no-diary)
9139 (let* ((marker (or (org-get-at-bol 'org-marker)
9140 (org-agenda-error)))
9141 (buffer (marker-buffer marker))
9142 (pos (marker-position marker)))
9143 (org-with-remote-undo buffer
9144 (with-current-buffer buffer
9145 (widen)
9146 (goto-char pos)
9147 (if (not (org-at-timestamp-p t))
9148 (error "Cannot find time stamp"))
9149 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
9150 (org-agenda-show-new-time marker org-last-changed-timestamp))
9151 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9153 (defun org-agenda-schedule (arg &optional time)
9154 "Schedule the item at point.
9155 ARG is passed through to `org-schedule'."
9156 (interactive "P")
9157 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9158 (org-agenda-check-no-diary)
9159 (let* ((marker (or (org-get-at-bol 'org-marker)
9160 (org-agenda-error)))
9161 (type (marker-insertion-type marker))
9162 (buffer (marker-buffer marker))
9163 (pos (marker-position marker))
9165 (set-marker-insertion-type marker t)
9166 (org-with-remote-undo buffer
9167 (with-current-buffer buffer
9168 (widen)
9169 (goto-char pos)
9170 (setq ts (org-schedule arg time)))
9171 (org-agenda-show-new-time marker ts " S"))
9172 (message "%s" ts)))
9174 (defun org-agenda-deadline (arg &optional time)
9175 "Schedule the item at point.
9176 ARG is passed through to `org-deadline'."
9177 (interactive "P")
9178 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9179 (org-agenda-check-no-diary)
9180 (let* ((marker (or (org-get-at-bol 'org-marker)
9181 (org-agenda-error)))
9182 (buffer (marker-buffer marker))
9183 (pos (marker-position marker))
9185 (org-with-remote-undo buffer
9186 (with-current-buffer buffer
9187 (widen)
9188 (goto-char pos)
9189 (setq ts (org-deadline arg time)))
9190 (org-agenda-show-new-time marker ts " D"))
9191 (message "%s" ts)))
9193 (defun org-agenda-clock-in (&optional arg)
9194 "Start the clock on the currently selected item."
9195 (interactive "P")
9196 (org-agenda-check-no-diary)
9197 (if (equal arg '(4))
9198 (org-clock-in arg)
9199 (let* ((marker (or (org-get-at-bol 'org-marker)
9200 (org-agenda-error)))
9201 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9202 (pos (marker-position marker))
9203 (col (current-column))
9204 newhead)
9205 (org-with-remote-undo (marker-buffer marker)
9206 (with-current-buffer (marker-buffer marker)
9207 (widen)
9208 (goto-char pos)
9209 (org-show-context 'agenda)
9210 (org-cycle-hide-drawers 'children)
9211 (org-clock-in arg)
9212 (setq newhead (org-get-heading)))
9213 (org-agenda-change-all-lines newhead hdmarker))
9214 (org-move-to-column col))))
9216 (defun org-agenda-clock-out ()
9217 "Stop the currently running clock."
9218 (interactive)
9219 (unless (marker-buffer org-clock-marker)
9220 (error "No running clock"))
9221 (let ((marker (make-marker)) (col (current-column)) newhead)
9222 (org-with-remote-undo (marker-buffer org-clock-marker)
9223 (with-current-buffer (marker-buffer org-clock-marker)
9224 (org-with-wide-buffer
9225 (goto-char org-clock-marker)
9226 (org-back-to-heading t)
9227 (move-marker marker (point))
9228 (org-clock-out)
9229 (setq newhead (org-get-heading)))))
9230 (org-agenda-change-all-lines newhead marker)
9231 (move-marker marker nil)
9232 (org-move-to-column col)
9233 (org-agenda-unmark-clocking-task)))
9235 (defun org-agenda-clock-cancel (&optional arg)
9236 "Cancel the currently running clock."
9237 (interactive "P")
9238 (unless (marker-buffer org-clock-marker)
9239 (user-error "No running clock"))
9240 (org-with-remote-undo (marker-buffer org-clock-marker)
9241 (org-clock-cancel)))
9243 (defun org-agenda-clock-goto ()
9244 "Jump to the currently clocked in task within the agenda.
9245 If the currently clocked in task is not listed in the agenda
9246 buffer, display it in another window."
9247 (interactive)
9248 (let (pos)
9249 (mapc (lambda (o)
9250 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
9251 (setq pos (overlay-start o))))
9252 (overlays-in (point-min) (point-max)))
9253 (cond (pos (goto-char pos))
9254 ;; If the currently clocked entry is not in the agenda
9255 ;; buffer, we visit it in another window:
9256 ((bound-and-true-p org-clock-current-task)
9257 (org-switch-to-buffer-other-window (org-clock-goto)))
9258 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9260 (defun org-agenda-diary-entry-in-org-file ()
9261 "Make a diary entry in the file `org-agenda-diary-file'."
9262 (let (d1 d2 char (text "") dp1 dp2)
9263 (if (equal (buffer-name) "*Calendar*")
9264 (setq d1 (calendar-cursor-to-date t)
9265 d2 (car calendar-mark-ring))
9266 (setq dp1 (get-text-property (point-at-bol) 'day))
9267 (unless dp1 (user-error "No date defined in current line"))
9268 (setq d1 (calendar-gregorian-from-absolute dp1)
9269 d2 (and (ignore-errors (mark))
9270 (save-excursion
9271 (goto-char (mark))
9272 (setq dp2 (get-text-property (point-at-bol) 'day)))
9273 (calendar-gregorian-from-absolute dp2))))
9274 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9275 (setq char (read-char-exclusive))
9276 (cond
9277 ((equal char ?d)
9278 (setq text (read-string "Day entry: "))
9279 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9280 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9281 ((equal char ?a)
9282 (setq d1 (list (car d1) (nth 1 d1)
9283 (read-number (format "Reference year [%d]: " (nth 2 d1))
9284 (nth 2 d1))))
9285 (setq text (read-string "Anniversary (use %d to show years): "))
9286 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9287 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9288 ((equal char ?b)
9289 (setq text (read-string "Block entry: "))
9290 (unless (and d1 d2 (not (equal d1 d2)))
9291 (user-error "No block of days selected"))
9292 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9293 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9294 ((equal char ?j)
9295 (org-switch-to-buffer-other-window
9296 (find-file-noselect org-agenda-diary-file))
9297 (require 'org-datetree)
9298 (org-datetree-find-date-create d1)
9299 (org-reveal t))
9300 (t (user-error "Invalid selection character `%c'" char)))))
9302 (defcustom org-agenda-insert-diary-strategy 'date-tree
9303 "Where in `org-agenda-diary-file' should new entries be added?
9304 Valid values:
9306 date-tree in the date tree, as first child of the date
9307 date-tree-last in the date tree, as last child of the date
9308 top-level as top-level entries at the end of the file."
9309 :group 'org-agenda
9310 :type '(choice
9311 (const :tag "first in a date tree" date-tree)
9312 (const :tag "last in a date tree" date-tree-last)
9313 (const :tag "as top level at end of file" top-level)))
9315 (defcustom org-agenda-insert-diary-extract-time nil
9316 "Non-nil means extract any time specification from the diary entry."
9317 :group 'org-agenda
9318 :version "24.1"
9319 :type 'boolean)
9321 (defcustom org-agenda-bulk-mark-char ">"
9322 "A single-character string to be used as the bulk mark."
9323 :group 'org-agenda
9324 :version "24.1"
9325 :type 'string)
9327 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9328 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9329 If TEXT is not empty, it will become the headline of the new entry, and
9330 the resulting entry will not be shown. When TEXT is empty, switch to
9331 `org-agenda-diary-file' and let the user finish the entry there."
9332 (let ((cw (current-window-configuration)))
9333 (org-switch-to-buffer-other-window
9334 (find-file-noselect org-agenda-diary-file))
9335 (widen)
9336 (goto-char (point-min))
9337 (cond
9338 ((eq type 'anniversary)
9339 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
9340 (progn
9341 (or (org-at-heading-p t)
9342 (progn
9343 (outline-next-heading)
9344 (insert "* Anniversaries\n\n")
9345 (beginning-of-line -1)))))
9346 (outline-next-heading)
9347 (org-back-over-empty-lines)
9348 (backward-char 1)
9349 (insert "\n")
9350 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9351 (nth 2 d1) (car d1) (nth 1 d1) text)))
9352 ((eq type 'day)
9353 (let ((org-prefix-has-time t)
9354 (org-agenda-time-leading-zero t)
9355 fmt time time2)
9356 (if org-agenda-insert-diary-extract-time
9357 ;; Use org-agenda-format-item to parse text for a time-range and
9358 ;; remove it. FIXME: This is a hack, we should refactor
9359 ;; that function to make time extraction available separately
9360 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9361 time (get-text-property 0 'time fmt)
9362 time2 (if (> (length time) 0)
9363 ;; split-string removes trailing ...... if
9364 ;; no end time given. First space
9365 ;; separates time from date.
9366 (concat " " (car (split-string time "\\.")))
9367 nil)
9368 text (get-text-property 0 'txt fmt)))
9369 (if (eq org-agenda-insert-diary-strategy 'top-level)
9370 (org-agenda-insert-diary-as-top-level text)
9371 (require 'org-datetree)
9372 (org-datetree-find-date-create d1)
9373 (org-agenda-insert-diary-make-new-entry text))
9374 (org-insert-time-stamp (org-time-from-absolute
9375 (calendar-absolute-from-gregorian d1))
9376 nil nil nil nil time2))
9377 (end-of-line 0))
9378 ((eq type 'block)
9379 (if (> (calendar-absolute-from-gregorian d1)
9380 (calendar-absolute-from-gregorian d2))
9381 (setq d1 (prog1 d2 (setq d2 d1))))
9382 (if (eq org-agenda-insert-diary-strategy 'top-level)
9383 (org-agenda-insert-diary-as-top-level text)
9384 (require 'org-datetree)
9385 (org-datetree-find-date-create d1)
9386 (org-agenda-insert-diary-make-new-entry text))
9387 (org-insert-time-stamp (org-time-from-absolute
9388 (calendar-absolute-from-gregorian d1)))
9389 (insert "--")
9390 (org-insert-time-stamp (org-time-from-absolute
9391 (calendar-absolute-from-gregorian d2)))
9392 (end-of-line 0)))
9393 (if (string-match "\\S-" text)
9394 (progn
9395 (set-window-configuration cw)
9396 (message "%s entry added to %s"
9397 (capitalize (symbol-name type))
9398 (abbreviate-file-name org-agenda-diary-file)))
9399 (org-reveal t)
9400 (message "Please finish entry here"))))
9402 (defun org-agenda-insert-diary-as-top-level (text)
9403 "Make new entry as a top-level entry at the end of the file.
9404 Add TEXT as headline, and position the cursor in the second line so that
9405 a timestamp can be added there."
9406 (widen)
9407 (goto-char (point-max))
9408 (unless (bolp) (insert "\n"))
9409 (org-insert-heading nil t t)
9410 (insert text)
9411 (org-end-of-meta-data)
9412 (unless (bolp) (insert "\n"))
9413 (when org-adapt-indentation (indent-to-column 2)))
9415 (defun org-agenda-insert-diary-make-new-entry (text)
9416 "Make a new entry with TEXT as a child of the current subtree.
9417 Position the point in the heading's first body line so that
9418 a timestamp can be added there."
9419 (cond
9420 ((eq org-agenda-insert-diary-strategy 'date-tree-last)
9421 (end-of-line)
9422 (org-insert-heading '(4) t)
9423 (org-do-demote))
9425 (outline-next-heading)
9426 (org-back-over-empty-lines)
9427 (unless (looking-at "[ \t]*$") (save-excursion (insert "\n")))
9428 (org-insert-heading nil t)
9429 (org-do-demote)))
9430 (let ((col (current-column)))
9431 (insert text)
9432 (org-end-of-meta-data)
9433 ;; Ensure point is left on a blank line, at proper indentation.
9434 (unless (bolp) (insert "\n"))
9435 (unless (looking-at-p "^[ \t]*$") (save-excursion (insert "\n")))
9436 (when org-adapt-indentation (indent-to-column col)))
9437 (org-show-set-visibility 'lineage))
9439 (defun org-agenda-diary-entry ()
9440 "Make a diary entry, like the `i' command from the calendar.
9441 All the standard commands work: block, weekly etc.
9442 When `org-agenda-diary-file' points to a file,
9443 `org-agenda-diary-entry-in-org-file' is called instead to create
9444 entries in that Org file."
9445 (interactive)
9446 (if (not (eq org-agenda-diary-file 'diary-file))
9447 (org-agenda-diary-entry-in-org-file)
9448 (require 'diary-lib)
9449 (let* ((char (progn
9450 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
9451 (read-char-exclusive)))
9452 (cmd (cdr (assoc char
9453 '((?d . diary-insert-entry)
9454 (?w . diary-insert-weekly-entry)
9455 (?m . diary-insert-monthly-entry)
9456 (?y . diary-insert-yearly-entry)
9457 (?a . diary-insert-anniversary-entry)
9458 (?b . diary-insert-block-entry)
9459 (?c . diary-insert-cyclic-entry)))))
9460 (oldf (symbol-function 'calendar-cursor-to-date))
9461 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9462 (point (point))
9463 (mark (or (mark t) (point))))
9464 (unless cmd
9465 (user-error "No command associated with <%c>" char))
9466 (unless (and (get-text-property point 'day)
9467 (or (not (equal ?b char))
9468 (get-text-property mark 'day)))
9469 (user-error "Don't know which date to use for diary entry"))
9470 ;; We implement this by hacking the `calendar-cursor-to-date' function
9471 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9472 (let ((calendar-mark-ring
9473 (list (calendar-gregorian-from-absolute
9474 (or (get-text-property mark 'day)
9475 (get-text-property point 'day))))))
9476 (unwind-protect
9477 (progn
9478 (fset 'calendar-cursor-to-date
9479 (lambda (&optional error dummy)
9480 (calendar-gregorian-from-absolute
9481 (get-text-property point 'day))))
9482 (call-interactively cmd))
9483 (fset 'calendar-cursor-to-date oldf))))))
9485 (defun org-agenda-execute-calendar-command (cmd)
9486 "Execute a calendar command from the agenda with date from cursor."
9487 (org-agenda-check-type t 'agenda 'timeline)
9488 (require 'diary-lib)
9489 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9490 (user-error "Don't know which date to use for the calendar command"))
9491 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9492 (point (point))
9493 (date (calendar-gregorian-from-absolute
9494 (get-text-property point 'day)))
9495 ;; the following 2 vars are needed in the calendar
9496 (displayed-month (car date))
9497 (displayed-year (nth 2 date)))
9498 (unwind-protect
9499 (progn
9500 (fset 'calendar-cursor-to-date
9501 (lambda (&optional error dummy)
9502 (calendar-gregorian-from-absolute
9503 (get-text-property point 'day))))
9504 (call-interactively cmd))
9505 (fset 'calendar-cursor-to-date oldf))))
9507 (defun org-agenda-phases-of-moon ()
9508 "Display the phases of the moon for the 3 months around the cursor date."
9509 (interactive)
9510 (org-agenda-execute-calendar-command 'calendar-lunar-phases))
9512 (defun org-agenda-holidays ()
9513 "Display the holidays for the 3 months around the cursor date."
9514 (interactive)
9515 (org-agenda-execute-calendar-command 'calendar-list-holidays))
9517 (defvar calendar-longitude) ; defined in calendar.el
9518 (defvar calendar-latitude) ; defined in calendar.el
9519 (defvar calendar-location-name) ; defined in calendar.el
9521 (defun org-agenda-sunrise-sunset (arg)
9522 "Display sunrise and sunset for the cursor date.
9523 Latitude and longitude can be specified with the variables
9524 `calendar-latitude' and `calendar-longitude'. When called with prefix
9525 argument, latitude and longitude will be prompted for."
9526 (interactive "P")
9527 (require 'solar)
9528 (let ((calendar-longitude (if arg nil calendar-longitude))
9529 (calendar-latitude (if arg nil calendar-latitude))
9530 (calendar-location-name
9531 (if arg "the given coordinates" calendar-location-name)))
9532 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9534 (defun org-agenda-goto-calendar ()
9535 "Open the Emacs calendar with the date at the cursor."
9536 (interactive)
9537 (org-agenda-check-type t 'agenda 'timeline)
9538 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9539 (user-error "Don't know which date to open in calendar")))
9540 (date (calendar-gregorian-from-absolute day))
9541 (calendar-move-hook nil)
9542 (calendar-view-holidays-initially-flag nil)
9543 (calendar-view-diary-initially-flag nil))
9544 (calendar)
9545 (calendar-goto-date date)))
9547 ;;;###autoload
9548 (defun org-calendar-goto-agenda ()
9549 "Compute the Org agenda for the calendar date displayed at the cursor.
9550 This is a command that has to be installed in `calendar-mode-map'."
9551 (interactive)
9552 ;; Temporarily disable sticky agenda since user clearly wants to
9553 ;; refresh view anyway.
9554 (let ((org-agenda-buffer-tmp-name "*Org Agenda(a)*")
9555 (org-agenda-sticky nil))
9556 (org-agenda-list nil (calendar-absolute-from-gregorian
9557 (calendar-cursor-to-date))
9558 nil)))
9560 (defun org-agenda-convert-date ()
9561 (interactive)
9562 (org-agenda-check-type t 'agenda 'timeline)
9563 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9564 date s)
9565 (unless day
9566 (user-error "Don't know which date to convert"))
9567 (setq date (calendar-gregorian-from-absolute day))
9568 (setq s (concat
9569 "Gregorian: " (calendar-date-string date) "\n"
9570 "ISO: " (calendar-iso-date-string date) "\n"
9571 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9572 "Julian: " (calendar-julian-date-string date) "\n"
9573 "Astron. JD: " (calendar-astro-date-string date)
9574 " (Julian date number at noon UTC)\n"
9575 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9576 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9577 "French: " (calendar-french-date-string date) "\n"
9578 "Bahá’í: " (calendar-bahai-date-string date) " (until sunset)\n"
9579 "Mayan: " (calendar-mayan-date-string date) "\n"
9580 "Coptic: " (calendar-coptic-date-string date) "\n"
9581 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9582 "Persian: " (calendar-persian-date-string date) "\n"
9583 "Chinese: " (calendar-chinese-date-string date) "\n"))
9584 (with-output-to-temp-buffer "*Dates*"
9585 (princ s))
9586 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9588 ;;; Bulk commands
9590 (defun org-agenda-bulk-marked-p ()
9591 "Non-nil when current entry is marked for bulk action."
9592 (eq (get-char-property (point-at-bol) 'type)
9593 'org-marked-entry-overlay))
9595 (defun org-agenda-bulk-mark (&optional arg)
9596 "Mark the entry at point for future bulk action."
9597 (interactive "p")
9598 (dotimes (i (or arg 1))
9599 (unless (org-get-at-bol 'org-agenda-diary-link)
9600 (let* ((m (org-get-at-bol 'org-hd-marker))
9602 (unless (org-agenda-bulk-marked-p)
9603 (unless m (user-error "Nothing to mark at point"))
9604 (push m org-agenda-bulk-marked-entries)
9605 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9606 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9607 (org-get-todo-face "TODO")
9608 'evaporate)
9609 (overlay-put ov 'type 'org-marked-entry-overlay))
9610 (end-of-line 1)
9611 (or (ignore-errors
9612 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9613 (beginning-of-line 2))
9614 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9615 (beginning-of-line 2))
9616 (message "%d entries marked for bulk action"
9617 (length org-agenda-bulk-marked-entries))))))
9619 (defun org-agenda-bulk-mark-all ()
9620 "Mark all entries for future agenda bulk action."
9621 (interactive)
9622 (org-agenda-bulk-mark-regexp "."))
9624 (defun org-agenda-bulk-mark-regexp (regexp)
9625 "Mark entries matching REGEXP for future agenda bulk action."
9626 (interactive "sMark entries matching regexp: ")
9627 (let ((entries-marked 0) txt-at-point)
9628 (save-excursion
9629 (goto-char (point-min))
9630 (goto-char (next-single-property-change (point) 'org-hd-marker))
9631 (while (and (re-search-forward regexp nil t)
9632 (setq txt-at-point (get-text-property (point) 'txt)))
9633 (if (get-char-property (point) 'invisible)
9634 (beginning-of-line 2)
9635 (when (string-match regexp txt-at-point)
9636 (setq entries-marked (1+ entries-marked))
9637 (call-interactively 'org-agenda-bulk-mark)))))
9639 (if (not entries-marked)
9640 (message "No entry matching this regexp."))))
9642 (defun org-agenda-bulk-unmark (&optional arg)
9643 "Unmark the entry at point for future bulk action."
9644 (interactive "P")
9645 (if arg
9646 (org-agenda-bulk-unmark-all)
9647 (cond ((org-agenda-bulk-marked-p)
9648 (org-agenda-bulk-remove-overlays
9649 (point-at-bol) (+ 2 (point-at-bol)))
9650 (setq org-agenda-bulk-marked-entries
9651 (delete (org-get-at-bol 'org-hd-marker)
9652 org-agenda-bulk-marked-entries))
9653 (end-of-line 1)
9654 (or (ignore-errors
9655 (goto-char (next-single-property-change (point) 'txt)))
9656 (beginning-of-line 2))
9657 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9658 (beginning-of-line 2))
9659 (message "%d entries left marked for bulk action"
9660 (length org-agenda-bulk-marked-entries)))
9661 (t (message "No entry to unmark here")))))
9663 (defun org-agenda-bulk-toggle-all ()
9664 "Toggle all marks for bulk action."
9665 (interactive)
9666 (save-excursion
9667 (goto-char (point-min))
9668 (while (ignore-errors
9669 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9670 (org-agenda-bulk-toggle))))
9672 (defun org-agenda-bulk-toggle ()
9673 "Toggle the mark at point for bulk action."
9674 (interactive)
9675 (if (org-agenda-bulk-marked-p)
9676 (org-agenda-bulk-unmark)
9677 (org-agenda-bulk-mark)))
9679 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9680 "Remove the mark overlays between BEG and END in the agenda buffer.
9681 BEG and END default to the buffer limits.
9683 This only removes the overlays, it does not remove the markers
9684 from the list in `org-agenda-bulk-marked-entries'."
9685 (interactive)
9686 (mapc (lambda (ov)
9687 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9688 (delete-overlay ov)))
9689 (overlays-in (or beg (point-min)) (or end (point-max)))))
9691 (defun org-agenda-bulk-unmark-all ()
9692 "Remove all marks in the agenda buffer.
9693 This will remove the markers and the overlays."
9694 (interactive)
9695 (if (null org-agenda-bulk-marked-entries)
9696 (message "No entry to unmark")
9697 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
9698 (setq org-agenda-bulk-marked-entries nil)
9699 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9701 (defcustom org-agenda-persistent-marks nil
9702 "Non-nil means marked items will stay marked after a bulk action.
9703 You can toggle this interactively by typing `p' when prompted for a
9704 bulk action."
9705 :group 'org-agenda
9706 :version "24.1"
9707 :type 'boolean)
9709 (defun org-agenda-bulk-action (&optional arg)
9710 "Execute an remote-editing action on all marked entries.
9711 The prefix arg is passed through to the command if possible."
9712 (interactive "P")
9713 ;; Make sure we have markers, and only valid ones
9714 (unless org-agenda-bulk-marked-entries (user-error "No entries are marked"))
9715 (mapc
9716 (lambda (m)
9717 (unless (and (markerp m)
9718 (marker-buffer m)
9719 (buffer-live-p (marker-buffer m))
9720 (marker-position m))
9721 (user-error "Marker %s for bulk command is invalid" m)))
9722 org-agenda-bulk-marked-entries)
9724 ;; Prompt for the bulk command
9725 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
9726 (message (concat msg "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9727 "[S]catter [f]unction "
9728 (when org-agenda-bulk-custom-functions
9729 (concat " Custom: ["
9730 (mapconcat (lambda(f) (char-to-string (car f)))
9731 org-agenda-bulk-custom-functions "")
9732 "]"))))
9733 (catch 'exit
9734 (let* ((action (read-char-exclusive))
9735 (org-log-refile (if org-log-refile 'time nil))
9736 (entries (reverse org-agenda-bulk-marked-entries))
9737 (org-overriding-default-time
9738 (if (get-text-property (point) 'org-agenda-date-header)
9739 (org-get-cursor-date)))
9740 redo-at-end
9741 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
9742 (cond
9743 ((equal action ?p)
9744 (let ((org-agenda-persistent-marks
9745 (not org-agenda-persistent-marks)))
9746 (org-agenda-bulk-action)
9747 (throw 'exit nil)))
9749 ((equal action ?$)
9750 (setq cmd '(org-agenda-archive)))
9752 ((equal action ?A)
9753 (setq cmd '(org-agenda-archive-to-archive-sibling)))
9755 ((member action '(?r ?w))
9756 (setq rfloc (org-refile-get-location
9757 "Refile to"
9758 (marker-buffer (car entries))
9759 org-refile-allow-creating-parent-nodes))
9760 (if (nth 3 rfloc)
9761 (setcar (nthcdr 3 rfloc)
9762 (move-marker (make-marker) (nth 3 rfloc)
9763 (or (get-file-buffer (nth 1 rfloc))
9764 (find-buffer-visiting (nth 1 rfloc))
9765 (error "This should not happen")))))
9767 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
9768 redo-at-end t))
9770 ((equal action ?t)
9771 (setq state (completing-read
9772 "Todo state: "
9773 (with-current-buffer (marker-buffer (car entries))
9774 (mapcar #'list org-todo-keywords-1))))
9775 (setq cmd `(let ((org-inhibit-blocking t)
9776 (org-inhibit-logging 'note))
9777 (org-agenda-todo ,state))))
9779 ((memq action '(?- ?+))
9780 (setq tag (completing-read
9781 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9782 (with-current-buffer (marker-buffer (car entries))
9783 (delq nil
9784 (mapcar (lambda (x) (and (stringp (car x)) x))
9785 org-current-tag-alist)))))
9786 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
9788 ((memq action '(?s ?d))
9789 (let* ((time
9790 (unless arg
9791 (org-read-date
9792 nil nil nil
9793 (if (eq action ?s) "(Re)Schedule to" "(Re)Set Deadline to")
9794 org-overriding-default-time)))
9795 (c1 (if (eq action ?s) 'org-agenda-schedule
9796 'org-agenda-deadline)))
9797 ;; Make sure to not prompt for a note when bulk
9798 ;; rescheduling as Org cannot cope with simultaneous Org.
9799 ;; Besides, it could be annoying depending on the number
9800 ;; of items re-scheduled.
9801 (setq cmd `(eval '(let ((org-log-reschedule
9802 (and org-log-reschedule 'time)))
9803 (,c1 arg ,time))))))
9805 ((equal action ?S)
9806 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
9807 (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
9808 (let ((days (read-number
9809 (format "Scatter tasks across how many %sdays: "
9810 (if arg "week" "")) 7)))
9811 (setq cmd
9812 `(let ((distance (1+ (random ,days))))
9813 (if arg
9814 (let ((dist distance)
9815 (day-of-week
9816 (calendar-day-of-week
9817 (calendar-gregorian-from-absolute (org-today)))))
9818 (dotimes (i (1+ dist))
9819 (while (member day-of-week org-agenda-weekend-days)
9820 (cl-incf distance)
9821 (cl-incf day-of-week)
9822 (when (= day-of-week 7)
9823 (setq day-of-week 0)))
9824 (cl-incf day-of-week)
9825 (when (= day-of-week 7)
9826 (setq day-of-week 0)))))
9827 ;; silently fail when try to replan a sexp entry
9828 (condition-case nil
9829 (let* ((date (calendar-gregorian-from-absolute
9830 (+ (org-today) distance)))
9831 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9832 (nth 2 date))))
9833 (org-agenda-schedule nil time))
9834 (error nil)))))))
9836 ((assoc action org-agenda-bulk-custom-functions)
9837 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
9838 redo-at-end t))
9840 ((equal action ?f)
9841 (setq cmd (list (intern
9842 (completing-read "Function: "
9843 obarray 'fboundp t nil nil)))))
9845 (t (user-error "Invalid bulk action")))
9847 ;; Sort the markers, to make sure that parents are handled before children
9848 (setq entries (sort entries
9849 (lambda (a b)
9850 (cond
9851 ((equal (marker-buffer a) (marker-buffer b))
9852 (< (marker-position a) (marker-position b)))
9854 (string< (buffer-name (marker-buffer a))
9855 (buffer-name (marker-buffer b))))))))
9857 ;; Now loop over all markers and apply cmd
9858 (while (setq e (pop entries))
9859 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
9860 (if (not pos)
9861 (progn (message "Skipping removed entry at %s" e)
9862 (setq cntskip (1+ cntskip)))
9863 (goto-char pos)
9864 (let (org-loop-over-headlines-in-active-region)
9865 (eval cmd))
9866 ;; `post-command-hook' is not run yet. We make sure any
9867 ;; pending log note is processed.
9868 (when (or (memq 'org-add-log-note (default-value 'post-command-hook))
9869 (memq 'org-add-log-note post-command-hook))
9870 (org-add-log-note))
9871 (setq cnt (1+ cnt))))
9872 (when redo-at-end (org-agenda-redo))
9873 (unless org-agenda-persistent-marks
9874 (org-agenda-bulk-unmark-all))
9875 (message "Acted on %d entries%s%s"
9877 (if (= cntskip 0)
9879 (format ", skipped %d (disappeared before their turn)"
9880 cntskip))
9881 (if (not org-agenda-persistent-marks)
9882 "" " (kept marked)"))))))
9884 (defun org-agenda-capture (&optional with-time)
9885 "Call `org-capture' with the date at point.
9886 With a `C-1' prefix, use the HH:MM value at point (if any) or the
9887 current HH:MM time."
9888 (interactive "P")
9889 (if (not (eq major-mode 'org-agenda-mode))
9890 (user-error "You cannot do this outside of agenda buffers")
9891 (let ((org-overriding-default-time
9892 (org-get-cursor-date (equal with-time 1))))
9893 (call-interactively 'org-capture))))
9895 ;;; Dragging agenda lines forward/backward
9897 (defun org-agenda-reapply-filters ()
9898 "Re-apply all agenda filters."
9899 (mapcar
9900 (lambda(f) (when (car f) (org-agenda-filter-apply (car f) (cadr f) t)))
9901 `((,org-agenda-tag-filter tag)
9902 (,org-agenda-category-filter category)
9903 (,org-agenda-regexp-filter regexp)
9904 (,org-agenda-effort-filter effort)
9905 (,(get 'org-agenda-tag-filter :preset-filter) tag)
9906 (,(get 'org-agenda-category-filter :preset-filter) category)
9907 (,(get 'org-agenda-effort-filter :preset-filter) effort)
9908 (,(get 'org-agenda-regexp-filter :preset-filter) regexp))))
9910 (defun org-agenda-drag-line-forward (arg &optional backward)
9911 "Drag an agenda line forward by ARG lines.
9912 When the optional argument `backward' is non-nil, move backward."
9913 (interactive "p")
9914 (let ((inhibit-read-only t) lst line)
9915 (if (or (not (get-text-property (point) 'txt))
9916 (save-excursion
9917 (dotimes (n arg)
9918 (move-beginning-of-line (if backward 0 2))
9919 (push (not (get-text-property (point) 'txt)) lst))
9920 (delq nil lst)))
9921 (message "Cannot move line forward")
9922 (let ((end (save-excursion (move-beginning-of-line 2) (point))))
9923 (move-beginning-of-line 1)
9924 (setq line (buffer-substring (point) end))
9925 (delete-region (point) end)
9926 (move-beginning-of-line (funcall (if backward '1- '1+) arg))
9927 (insert line)
9928 (org-agenda-reapply-filters)
9929 (org-agenda-mark-clocking-task)
9930 (move-beginning-of-line 0)))))
9932 (defun org-agenda-drag-line-backward (arg)
9933 "Drag an agenda line backward by ARG lines."
9934 (interactive "p")
9935 (org-agenda-drag-line-forward arg t))
9937 ;;; Flagging notes
9939 (defun org-agenda-show-the-flagging-note ()
9940 "Display the flagging note in the other window.
9941 When called a second time in direct sequence, offer to remove the FLAGGING
9942 tag and (if present) the flagging note."
9943 (interactive)
9944 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
9945 (win (selected-window))
9946 note heading newhead)
9947 (unless hdmarker
9948 (user-error "No linked entry at point"))
9949 (if (and (eq this-command last-command)
9950 (y-or-n-p "Unflag and remove any flagging note? "))
9951 (progn
9952 (org-agenda-remove-flag hdmarker)
9953 (let ((win (get-buffer-window "*Flagging Note*")))
9954 (and win (delete-window win)))
9955 (message "Entry unflagged"))
9956 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
9957 (unless note
9958 (user-error "No flagging note"))
9959 (org-kill-new note)
9960 (org-switch-to-buffer-other-window "*Flagging Note*")
9961 (erase-buffer)
9962 (insert note)
9963 (goto-char (point-min))
9964 (while (re-search-forward "\\\\n" nil t)
9965 (replace-match "\n" t t))
9966 (goto-char (point-min))
9967 (select-window win)
9968 (message "%s" (substitute-command-keys "Flagging note pushed to \
9969 kill ring. Press `\\[org-agenda-show-the-flagging-note]' again to remove \
9970 tag and note")))))
9972 (defun org-agenda-remove-flag (marker)
9973 "Remove the FLAGGED tag and any flagging note in the entry."
9974 (let (newhead)
9975 (org-with-point-at marker
9976 (org-toggle-tag "FLAGGED" 'off)
9977 (org-entry-delete nil "THEFLAGGINGNOTE")
9978 (setq newhead (org-get-heading)))
9979 (org-agenda-change-all-lines newhead marker)
9980 (message "Entry unflagged")))
9982 (defun org-agenda-get-any-marker (&optional pos)
9983 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
9984 (get-text-property (or pos (point-at-bol)) 'org-marker)))
9986 ;;; Appointment reminders
9988 (defvar appt-time-msg-list) ; defined in appt.el
9990 ;;;###autoload
9991 (defun org-agenda-to-appt (&optional refresh filter &rest args)
9992 "Activate appointments found in `org-agenda-files'.
9994 With a `\\[universal-argument]' prefix, refresh the list of \
9995 appointments.
9997 If FILTER is t, interactively prompt the user for a regular
9998 expression, and filter out entries that don't match it.
10000 If FILTER is a string, use this string as a regular expression
10001 for filtering entries out.
10003 If FILTER is a function, filter out entries against which
10004 calling the function returns nil. This function takes one
10005 argument: an entry from `org-agenda-get-day-entries'.
10007 FILTER can also be an alist with the car of each cell being
10008 either `headline' or `category'. For example:
10010 \\='((headline \"IMPORTANT\")
10011 (category \"Work\"))
10013 will only add headlines containing IMPORTANT or headlines
10014 belonging to the \"Work\" category.
10016 ARGS are symbols indicating what kind of entries to consider.
10017 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
10018 \(i.e., deadlines and scheduled items with a hh:mm specification)
10019 and :timestamp entries. See the docstring of `org-diary' for
10020 details and examples.
10022 If an entry has a APPT_WARNTIME property, its value will be used
10023 to override `appt-message-warning-time'."
10024 (interactive "P")
10025 (if refresh (setq appt-time-msg-list nil))
10026 (if (eq filter t)
10027 (setq filter (read-from-minibuffer "Regexp filter: ")))
10028 (let* ((cnt 0) ; count added events
10029 (scope (or args '(:deadline* :scheduled* :timestamp)))
10030 (org-agenda-new-buffers nil)
10031 (org-deadline-warning-days 0)
10032 ;; Do not use `org-today' here because appt only takes
10033 ;; time and without date as argument, so it may pass wrong
10034 ;; information otherwise
10035 (today (org-date-to-gregorian
10036 (time-to-days (current-time))))
10037 (org-agenda-restrict nil)
10038 (files (org-agenda-files 'unrestricted)) entries file
10039 (org-agenda-buffer nil))
10040 ;; Get all entries which may contain an appt
10041 (org-agenda-prepare-buffers files)
10042 (while (setq file (pop files))
10043 (setq entries
10044 (delq nil
10045 (append entries
10046 (apply 'org-agenda-get-day-entries
10047 file today scope)))))
10048 ;; Map thru entries and find if we should filter them out
10049 (mapc
10050 (lambda (x)
10051 (let* ((evt (org-trim
10052 (replace-regexp-in-string
10053 org-bracket-link-regexp "\\3"
10054 (or (get-text-property 1 'txt x) ""))))
10055 (cat (get-text-property (1- (length x)) 'org-category x))
10056 (tod (get-text-property 1 'time-of-day x))
10057 (ok (or (null filter)
10058 (and (stringp filter) (string-match filter evt))
10059 (and (functionp filter) (funcall filter x))
10060 (and (listp filter)
10061 (let ((cat-filter (cadr (assq 'category filter)))
10062 (evt-filter (cadr (assq 'headline filter))))
10063 (or (and (stringp cat-filter)
10064 (string-match cat-filter cat))
10065 (and (stringp evt-filter)
10066 (string-match evt-filter evt)))))))
10067 (wrn (get-text-property 1 'warntime x)))
10068 ;; FIXME: Shall we remove text-properties for the appt text?
10069 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
10070 (when (and ok tod (not (string-match "\\`DONE\\|CANCELLED" evt)))
10071 (setq tod (concat "00" (number-to-string tod)))
10072 (setq tod (when (string-match
10073 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
10074 (concat (match-string 1 tod) ":"
10075 (match-string 2 tod))))
10076 (when (if (version< emacs-version "23.3")
10077 (appt-add tod evt)
10078 (appt-add tod evt wrn))
10079 (setq cnt (1+ cnt))))))
10080 entries)
10081 (org-release-buffers org-agenda-new-buffers)
10082 (if (eq cnt 0)
10083 (message "No event to add")
10084 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
10086 (defun org-agenda-today-p (date)
10087 "Non nil when DATE means today.
10088 DATE is either a list of the form (month day year) or a number of
10089 days as returned by `calendar-absolute-from-gregorian' or
10090 `org-today'. This function considers `org-extend-today-until'
10091 when defining today."
10092 (eq (org-today)
10093 (if (consp date) (calendar-absolute-from-gregorian date) date)))
10095 (defun org-agenda-todo-yesterday (&optional arg)
10096 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
10097 (interactive "P")
10098 (let* ((org-use-effective-time t)
10099 (hour (nth 2 (decode-time (org-current-time))))
10100 (org-extend-today-until (1+ hour)))
10101 (org-agenda-todo arg)))
10103 (provide 'org-agenda)
10105 ;;; org-agenda.el ends here