Merge branch 'maint'
[org-mode/org-tableheadings.git] / lisp / org-agenda.el
blob374460aa73223359fd7e609b676846710a9ed483
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 file to write the produced agenda buffer to
473 with 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 "25.2"
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 "25.2"
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 "25.2"
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 ,skip-re (org-end-of-subtree t) t)))
4949 (progn (outline-next-heading) (point))))))
4950 (org-tags-view nil matcher)
4951 (setq org-agenda-buffer-name (buffer-name))
4952 (with-current-buffer org-agenda-buffer-name
4953 (setq org-agenda-redo-command
4954 `(org-agenda-list-stuck-projects ,current-prefix-arg)))))
4956 ;;; Diary integration
4958 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
4959 (defvar diary-list-entries-hook)
4960 (defvar diary-time-regexp)
4961 (defun org-get-entries-from-diary (date)
4962 "Get the (Emacs Calendar) diary entries for DATE."
4963 (require 'diary-lib)
4964 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
4965 (diary-display-function 'diary-fancy-display)
4966 (pop-up-frames nil)
4967 (diary-list-entries-hook
4968 (cons 'org-diary-default-entry diary-list-entries-hook))
4969 (diary-file-name-prefix nil) ; turn this feature off
4970 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
4971 entries
4972 (org-disable-agenda-to-diary t))
4973 (save-excursion
4974 (save-window-excursion
4975 (funcall (if (fboundp 'diary-list-entries)
4976 'diary-list-entries 'list-diary-entries)
4977 date 1)))
4978 (if (not (get-buffer diary-fancy-buffer))
4979 (setq entries nil)
4980 (with-current-buffer diary-fancy-buffer
4981 (setq buffer-read-only nil)
4982 (if (zerop (buffer-size))
4983 ;; No entries
4984 (setq entries nil)
4985 ;; Omit the date and other unnecessary stuff
4986 (org-agenda-cleanup-fancy-diary)
4987 ;; Add prefix to each line and extend the text properties
4988 (if (zerop (buffer-size))
4989 (setq entries nil)
4990 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
4991 (setq entries
4992 (with-temp-buffer
4993 (insert entries) (goto-char (point-min))
4994 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
4995 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
4996 (replace-match (concat "; " (match-string 1)))))
4997 (buffer-string)))))
4998 (set-buffer-modified-p nil)
4999 (kill-buffer diary-fancy-buffer)))
5000 (when entries
5001 (setq entries (org-split-string entries "\n"))
5002 (setq entries
5003 (mapcar
5004 (lambda (x)
5005 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5006 ;; Extend the text properties to the beginning of the line
5007 (org-add-props x (text-properties-at (1- (length x)) x)
5008 'type "diary" 'date date 'face 'org-agenda-diary))
5009 entries)))))
5011 (defvar org-agenda-cleanup-fancy-diary-hook nil
5012 "Hook run when the fancy diary buffer is cleaned up.")
5014 (defun org-agenda-cleanup-fancy-diary ()
5015 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5016 This gets rid of the date, the underline under the date, and
5017 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5018 date. It also removes lines that contain only whitespace."
5019 (goto-char (point-min))
5020 (if (looking-at ".*?:[ \t]*")
5021 (progn
5022 (replace-match "")
5023 (re-search-forward "\n=+$" nil t)
5024 (replace-match "")
5025 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5026 (re-search-forward "\n=+$" nil t)
5027 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5028 (goto-char (point-min))
5029 (while (re-search-forward "^ +\n" nil t)
5030 (replace-match ""))
5031 (goto-char (point-min))
5032 (if (re-search-forward "^Org mode dummy\n?" nil t)
5033 (replace-match ""))
5034 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5036 ;; Make sure entries from the diary have the right text properties.
5037 (eval-after-load "diary-lib"
5038 '(if (boundp 'diary-modify-entry-list-string-function)
5039 ;; We can rely on the hook, nothing to do
5041 ;; Hook not available, must use advice to make this work
5042 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5043 "Make the position visible."
5044 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5045 (stringp string)
5046 buffer-file-name)
5047 (setq string (org-modify-diary-entry-string string))))))
5049 (defun org-modify-diary-entry-string (string)
5050 "Add text properties to string, allowing Org to act on it."
5051 (org-add-props string nil
5052 'mouse-face 'highlight
5053 'help-echo (if buffer-file-name
5054 (format "mouse-2 or RET jump to diary file %s"
5055 (abbreviate-file-name buffer-file-name))
5057 'org-agenda-diary-link t
5058 'org-marker (org-agenda-new-marker (point-at-bol))))
5060 (defun org-diary-default-entry ()
5061 "Add a dummy entry to the diary.
5062 Needed to avoid empty dates which mess up holiday display."
5063 ;; Catch the error if dealing with the new add-to-diary-alist
5064 (when org-disable-agenda-to-diary
5065 (condition-case nil
5066 (org-add-to-diary-list original-date "Org mode dummy" "")
5067 (error
5068 (org-add-to-diary-list original-date "Org mode dummy" "" nil)))))
5070 (defun org-add-to-diary-list (&rest args)
5071 (if (fboundp 'diary-add-to-list)
5072 (apply 'diary-add-to-list args)
5073 (apply 'add-to-diary-list args)))
5075 (defvar org-diary-last-run-time nil)
5077 ;;;###autoload
5078 (defun org-diary (&rest args)
5079 "Return diary information from org files.
5080 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5081 It accesses org files and extracts information from those files to be
5082 listed in the diary. The function accepts arguments specifying what
5083 items should be listed. For a list of arguments allowed here, see the
5084 variable `org-agenda-entry-types'.
5086 The call in the diary file should look like this:
5088 &%%(org-diary) ~/path/to/some/orgfile.org
5090 Use a separate line for each org file to check. Or, if you omit the file name,
5091 all files listed in `org-agenda-files' will be checked automatically:
5093 &%%(org-diary)
5095 If you don't give any arguments (as in the example above), the default value
5096 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5097 So the example above may also be written as
5099 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5101 The function expects the lisp variables `entry' and `date' to be provided
5102 by the caller, because this is how the calendar works. Don't use this
5103 function from a program - use `org-agenda-get-day-entries' instead."
5104 (when (> (- (float-time)
5105 org-agenda-last-marker-time)
5107 ;; I am not sure if this works with sticky agendas, because the marker
5108 ;; list is then no longer a global variable.
5109 (org-agenda-reset-markers))
5110 (org-compile-prefix-format 'agenda)
5111 (org-set-sorting-strategy 'agenda)
5112 (setq args (or args org-agenda-entry-types))
5113 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5114 (list entry)
5115 (org-agenda-files t)))
5116 (time (float-time))
5117 file rtn results)
5118 (when (or (not org-diary-last-run-time)
5119 (> (- time
5120 org-diary-last-run-time)
5122 (org-agenda-prepare-buffers files))
5123 (setq org-diary-last-run-time time)
5124 ;; If this is called during org-agenda, don't return any entries to
5125 ;; the calendar. Org Agenda will list these entries itself.
5126 (if org-disable-agenda-to-diary (setq files nil))
5127 (while (setq file (pop files))
5128 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5129 (setq results (append results rtn)))
5130 (when results
5131 (setq results
5132 (mapcar (lambda (i) (replace-regexp-in-string
5133 org-bracket-link-regexp "\\3" i)) results))
5134 (concat (org-agenda-finalize-entries results) "\n"))))
5136 ;;; Agenda entry finders
5138 (defun org-agenda--timestamp-to-absolute (&rest args)
5139 "Call `org-time-string-to-absolute' with ARGS.
5140 However, throw `:skip' whenever an error is raised."
5141 (condition-case e
5142 (apply #'org-time-string-to-absolute args)
5143 (org-diary-sexp-no-match (throw :skip nil))
5144 (error
5145 (message "%s; Skipping entry" (error-message-string e))
5146 (throw :skip nil))))
5148 (defun org-agenda-get-day-entries (file date &rest args)
5149 "Does the work for `org-diary' and `org-agenda'.
5150 FILE is the path to a file to be checked for entries. DATE is date like
5151 the one returned by `calendar-current-date'. ARGS are symbols indicating
5152 which kind of entries should be extracted. For details about these, see
5153 the documentation of `org-diary'."
5154 (let* ((org-startup-folded nil)
5155 (org-startup-align-all-tables nil)
5156 (buffer (if (file-exists-p file) (org-get-agenda-file-buffer file)
5157 (error "No such file %s" file))))
5158 (if (not buffer)
5159 ;; If file does not exist, signal it in diary nonetheless.
5160 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5161 (with-current-buffer buffer
5162 (unless (derived-mode-p 'org-mode)
5163 (error "Agenda file %s is not in `org-mode'" file))
5164 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5165 (setf org-agenda-current-date date)
5166 (save-excursion
5167 (save-restriction
5168 (if (eq buffer org-agenda-restrict)
5169 (narrow-to-region org-agenda-restrict-begin
5170 org-agenda-restrict-end)
5171 (widen))
5172 ;; Rationalize ARGS. Also make sure `:deadline' comes
5173 ;; first in order to populate DEADLINES before passing it.
5175 ;; We use `delq' since `org-uniquify' duplicates ARGS,
5176 ;; guarding us from modifying `org-agenda-entry-types'.
5177 (setf args (org-uniquify (or args org-agenda-entry-types)))
5178 (when (and (memq :scheduled args) (memq :scheduled* args))
5179 (setf args (delq :scheduled* args)))
5180 (cond
5181 ((memq :deadline args)
5182 (setf args (cons :deadline
5183 (delq :deadline (delq :deadline* args)))))
5184 ((memq :deadline* args)
5185 (setf args (cons :deadline* (delq :deadline* args)))))
5186 ;; Collect list of headlines. Return them flattened.
5187 (let ((case-fold-search nil) results deadlines)
5188 (dolist (arg args (apply #'nconc (nreverse results)))
5189 (pcase arg
5190 ((and :todo (guard (org-agenda-today-p date)))
5191 (push (org-agenda-get-todos) results))
5192 (:timestamp
5193 (push (org-agenda-get-blocks) results)
5194 (push (org-agenda-get-timestamps deadlines) results))
5195 (:sexp
5196 (push (org-agenda-get-sexps) results))
5197 (:scheduled
5198 (push (org-agenda-get-scheduled deadlines) results))
5199 (:scheduled*
5200 (push (org-agenda-get-scheduled deadlines t) results))
5201 (:closed
5202 (push (org-agenda-get-progress) results))
5203 (:deadline
5204 (setf deadlines (org-agenda-get-deadlines))
5205 (push deadlines results))
5206 (:deadline*
5207 (setf deadlines (org-agenda-get-deadlines t))
5208 (push deadlines results)))))))))))
5210 (defsubst org-em (x y list)
5211 "Is X or Y a member of LIST?"
5212 (or (memq x list) (memq y list)))
5214 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5215 (defvar org-agenda-sorting-strategy-selected nil)
5217 (defun org-agenda-entry-get-agenda-timestamp (pom)
5218 "Retrieve timestamp information for sorting agenda views.
5219 Given a point or marker POM, returns a cons cell of the timestamp
5220 and the timestamp type relevant for the sorting strategy in
5221 `org-agenda-sorting-strategy-selected'."
5222 (let (ts ts-date-type)
5223 (save-match-data
5224 (cond ((org-em 'scheduled-up 'scheduled-down
5225 org-agenda-sorting-strategy-selected)
5226 (setq ts (org-entry-get pom "SCHEDULED")
5227 ts-date-type " scheduled"))
5228 ((org-em 'deadline-up 'deadline-down
5229 org-agenda-sorting-strategy-selected)
5230 (setq ts (org-entry-get pom "DEADLINE")
5231 ts-date-type " deadline"))
5232 ((org-em 'ts-up 'ts-down
5233 org-agenda-sorting-strategy-selected)
5234 (setq ts (org-entry-get pom "TIMESTAMP")
5235 ts-date-type " timestamp"))
5236 ((org-em 'tsia-up 'tsia-down
5237 org-agenda-sorting-strategy-selected)
5238 (setq ts (org-entry-get pom "TIMESTAMP_IA")
5239 ts-date-type " timestamp_ia"))
5240 ((org-em 'timestamp-up 'timestamp-down
5241 org-agenda-sorting-strategy-selected)
5242 (setq ts (or (org-entry-get pom "SCHEDULED")
5243 (org-entry-get pom "DEADLINE")
5244 (org-entry-get pom "TIMESTAMP")
5245 (org-entry-get pom "TIMESTAMP_IA"))
5246 ts-date-type ""))
5247 (t (setq ts-date-type "")))
5248 (cons (when ts (ignore-errors (org-time-string-to-absolute ts)))
5249 ts-date-type))))
5251 (defun org-agenda-get-todos ()
5252 "Return the TODO information for agenda display."
5253 (let* ((props (list 'face nil
5254 'done-face 'org-agenda-done
5255 'org-not-done-regexp org-not-done-regexp
5256 'org-todo-regexp org-todo-regexp
5257 'org-complex-heading-regexp org-complex-heading-regexp
5258 'mouse-face 'highlight
5259 'help-echo
5260 (format "mouse-2 or RET jump to org file %s"
5261 (abbreviate-file-name buffer-file-name))))
5262 (case-fold-search nil)
5263 (regexp (format org-heading-keyword-regexp-format
5264 (cond
5265 ((and org-select-this-todo-keyword
5266 (equal org-select-this-todo-keyword "*"))
5267 org-todo-regexp)
5268 (org-select-this-todo-keyword
5269 (concat "\\("
5270 (mapconcat 'identity
5271 (org-split-string
5272 org-select-this-todo-keyword
5273 "|")
5274 "\\|") "\\)"))
5275 (t org-not-done-regexp))))
5276 marker priority category level tags todo-state
5277 ts-date ts-date-type ts-date-pair
5278 ee txt beg end inherited-tags todo-state-end-pos)
5279 (goto-char (point-min))
5280 (while (re-search-forward regexp nil t)
5281 (catch :skip
5282 (save-match-data
5283 (beginning-of-line)
5284 (org-agenda-skip)
5285 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5286 (unless (and (setq todo-state (org-get-todo-state))
5287 (setq todo-state-end-pos (match-end 2)))
5288 (goto-char end)
5289 (throw :skip nil))
5290 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5291 (goto-char (1+ beg))
5292 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5293 (throw :skip nil)))
5294 (goto-char (match-beginning 2))
5295 (setq marker (org-agenda-new-marker (match-beginning 0))
5296 category (org-get-category)
5297 ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
5298 ts-date (car ts-date-pair)
5299 ts-date-type (cdr ts-date-pair)
5300 txt (org-trim (buffer-substring (match-beginning 2) (match-end 0)))
5301 inherited-tags
5302 (or (eq org-agenda-show-inherited-tags 'always)
5303 (and (listp org-agenda-show-inherited-tags)
5304 (memq 'todo org-agenda-show-inherited-tags))
5305 (and (eq org-agenda-show-inherited-tags t)
5306 (or (eq org-agenda-use-tag-inheritance t)
5307 (memq 'todo org-agenda-use-tag-inheritance))))
5308 tags (org-get-tags-at nil (not inherited-tags))
5309 level (make-string (org-reduced-level (org-outline-level)) ? )
5310 txt (org-agenda-format-item "" txt level category tags t)
5311 priority (1+ (org-get-priority txt)))
5312 (org-add-props txt props
5313 'org-marker marker 'org-hd-marker marker
5314 'priority priority
5315 'level level
5316 'ts-date ts-date
5317 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5318 (push txt ee)
5319 (if org-agenda-todo-list-sublevels
5320 (goto-char todo-state-end-pos)
5321 (org-end-of-subtree 'invisible))))
5322 (nreverse ee)))
5324 (defun org-agenda-todo-custom-ignore-p (time n)
5325 "Check whether timestamp is farther away than n number of days.
5326 This function is invoked if `org-agenda-todo-ignore-deadlines',
5327 `org-agenda-todo-ignore-scheduled' or
5328 `org-agenda-todo-ignore-timestamp' is set to an integer."
5329 (let ((days (org-time-stamp-to-now
5330 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5331 (if (>= n 0)
5332 (>= days n)
5333 (<= days n))))
5335 ;;;###autoload
5336 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5337 (&optional end)
5338 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5339 (when (or org-agenda-todo-ignore-with-date
5340 org-agenda-todo-ignore-scheduled
5341 org-agenda-todo-ignore-deadlines
5342 org-agenda-todo-ignore-timestamp)
5343 (setq end (or end (save-excursion (outline-next-heading) (point))))
5344 (save-excursion
5345 (or (and org-agenda-todo-ignore-with-date
5346 (re-search-forward org-ts-regexp end t))
5347 (and org-agenda-todo-ignore-scheduled
5348 (re-search-forward org-scheduled-time-regexp end t)
5349 (cond
5350 ((eq org-agenda-todo-ignore-scheduled 'future)
5351 (> (org-time-stamp-to-now
5352 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5353 ((eq org-agenda-todo-ignore-scheduled 'past)
5354 (<= (org-time-stamp-to-now
5355 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5356 ((numberp org-agenda-todo-ignore-scheduled)
5357 (org-agenda-todo-custom-ignore-p
5358 (match-string 1) org-agenda-todo-ignore-scheduled))
5359 (t)))
5360 (and org-agenda-todo-ignore-deadlines
5361 (re-search-forward org-deadline-time-regexp end t)
5362 (cond
5363 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5364 ((eq org-agenda-todo-ignore-deadlines 'far)
5365 (not (org-deadline-close-p (match-string 1))))
5366 ((eq org-agenda-todo-ignore-deadlines 'future)
5367 (> (org-time-stamp-to-now
5368 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5369 ((eq org-agenda-todo-ignore-deadlines 'past)
5370 (<= (org-time-stamp-to-now
5371 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5372 ((numberp org-agenda-todo-ignore-deadlines)
5373 (org-agenda-todo-custom-ignore-p
5374 (match-string 1) org-agenda-todo-ignore-deadlines))
5375 (t (org-deadline-close-p (match-string 1)))))
5376 (and org-agenda-todo-ignore-timestamp
5377 (let ((buffer (current-buffer))
5378 (regexp
5379 (concat
5380 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5381 (start (point)))
5382 ;; Copy current buffer into a temporary one
5383 (with-temp-buffer
5384 (insert-buffer-substring buffer start end)
5385 (goto-char (point-min))
5386 ;; Delete SCHEDULED and DEADLINE items
5387 (while (re-search-forward regexp end t)
5388 (delete-region (match-beginning 0) (match-end 0)))
5389 (goto-char (point-min))
5390 ;; No search for timestamp left
5391 (when (re-search-forward org-ts-regexp nil t)
5392 (cond
5393 ((eq org-agenda-todo-ignore-timestamp 'future)
5394 (> (org-time-stamp-to-now
5395 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5396 ((eq org-agenda-todo-ignore-timestamp 'past)
5397 (<= (org-time-stamp-to-now
5398 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5399 ((numberp org-agenda-todo-ignore-timestamp)
5400 (org-agenda-todo-custom-ignore-p
5401 (match-string 1) org-agenda-todo-ignore-timestamp))
5402 (t))))))))))
5404 (defun org-agenda-get-timestamps (&optional deadlines)
5405 "Return the date stamp information for agenda display.
5406 Optional argument DEADLINES is a list of deadline items to be
5407 displayed in agenda view."
5408 (let* ((props (list 'face 'org-agenda-calendar-event
5409 'org-not-done-regexp org-not-done-regexp
5410 'org-todo-regexp org-todo-regexp
5411 'org-complex-heading-regexp org-complex-heading-regexp
5412 'mouse-face 'highlight
5413 'help-echo
5414 (format "mouse-2 or RET jump to Org file %s"
5415 (abbreviate-file-name buffer-file-name))))
5416 (current (calendar-absolute-from-gregorian date))
5417 (today (org-today))
5418 (deadline-position-alist
5419 (mapcar (lambda (d)
5420 (let ((m (get-text-property 0 'org-hd-marker d)))
5421 (and m (marker-position m))))
5422 deadlines))
5423 ;; Match time-stamps set to current date, time-stamps with
5424 ;; a repeater, and S-exp time-stamps.
5425 (regexp
5426 (concat
5427 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5428 (regexp-quote
5429 (substring
5430 (format-time-string
5431 (car org-time-stamp-formats)
5432 (apply #'encode-time ; DATE bound by calendar
5433 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5434 1 11))
5435 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5436 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5437 timestamp-items)
5438 (goto-char (point-min))
5439 (while (re-search-forward regexp nil t)
5440 ;; Skip date ranges, scheduled and deadlines, which are handled
5441 ;; specially. Also skip time-stamps before first headline as
5442 ;; there would be no entry to add to the agenda. Eventually,
5443 ;; ignore clock entries.
5444 (catch :skip
5445 (save-match-data
5446 (when (or (org-at-date-range-p)
5447 (org-at-planning-p)
5448 (org-before-first-heading-p)
5449 (and org-agenda-include-inactive-timestamps
5450 (org-at-clock-log-p)))
5451 (throw :skip nil))
5452 (org-agenda-skip))
5453 (let* ((pos (match-beginning 0))
5454 (repeat (match-string 1))
5455 (sexp-entry (match-string 3))
5456 (time-stamp (if (or repeat sexp-entry) (match-string 0)
5457 (save-excursion
5458 (goto-char pos)
5459 (looking-at org-ts-regexp-both)
5460 (match-string 0))))
5461 (todo-state (org-get-todo-state))
5462 (warntime (get-text-property (point) 'org-appt-warntime))
5463 (done? (member todo-state org-done-keywords)))
5464 ;; Possibly skip done tasks.
5465 (when (and done? org-agenda-skip-timestamp-if-done)
5466 (throw :skip t))
5467 ;; S-exp entry doesn't match current day: skip it.
5468 (when (and sexp-entry (not (org-diary-sexp-entry sexp-entry "" date)))
5469 (throw :skip nil))
5470 ;; A repeating time stamp is shown at its base date, and at
5471 ;; every repeated date in the future.
5472 (when repeat
5473 (let* ((past
5474 (if (or (eq org-agenda-prefer-last-repeat t)
5475 (member todo-state org-agenda-prefer-last-repeat))
5476 (org-agenda--timestamp-to-absolute
5477 repeat today 'past (current-buffer) pos)
5478 (org-agenda--timestamp-to-absolute repeat)))
5479 (future
5480 (cond
5481 ((<= current today) past)
5482 ((not org-agenda-show-future-repeats) past)
5484 (let ((base (if (eq org-agenda-show-future-repeats 'next)
5485 (1+ today)
5486 current)))
5487 (org-agenda--timestamp-to-absolute
5488 repeat base 'future (current-buffer) pos))))))
5489 (when (and (/= current past) (/= current future))
5490 (throw :skip nil))))
5491 (save-excursion
5492 (re-search-backward org-outline-regexp-bol nil t)
5493 ;; Possibly skip time-stamp when a deadline is set.
5494 (when (and org-agenda-skip-timestamp-if-deadline-is-shown
5495 (assq (point) deadline-position-alist))
5496 (throw :skip nil))
5497 (let* ((category (org-get-category pos))
5498 (inherited-tags
5499 (or (eq org-agenda-show-inherited-tags 'always)
5500 (and (consp org-agenda-show-inherited-tags)
5501 (memq 'agenda org-agenda-show-inherited-tags))
5502 (and (eq org-agenda-show-inherited-tags t)
5503 (or (eq org-agenda-use-tag-inheritance t)
5504 (memq 'agenda
5505 org-agenda-use-tag-inheritance)))))
5506 (tags (org-get-tags-at nil (not inherited-tags)))
5507 (level (make-string (org-reduced-level (org-outline-level))
5508 ?\s))
5509 (head (and (looking-at "\\*+[ \t]+\\(.*\\)")
5510 (match-string 1)))
5511 (inactive? (= (char-after pos) ?\[))
5512 (habit? (and (fboundp 'org-is-habit-p) (org-is-habit-p)))
5513 (item
5514 (org-agenda-format-item
5515 (and inactive? org-agenda-inactive-leader)
5516 head level category tags time-stamp org-ts-regexp habit?)))
5517 (org-add-props item props
5518 'priority (if habit?
5519 (org-habit-get-priority (org-habit-parse-todo))
5520 (org-get-priority item))
5521 'org-marker (org-agenda-new-marker pos)
5522 'org-hd-marker (org-agenda-new-marker)
5523 'date date
5524 'level level
5525 'ts-date (if repeat (org-agenda--timestamp-to-absolute repeat)
5526 current)
5527 'todo-state todo-state
5528 'warntime warntime
5529 'type "timestamp")
5530 (push item timestamp-items))))
5531 (when org-agenda-skip-additional-timestamps-same-entry
5532 (outline-next-heading))))
5533 (nreverse timestamp-items)))
5535 (defun org-agenda-get-sexps ()
5536 "Return the sexp information for agenda display."
5537 (require 'diary-lib)
5538 (let* ((props (list 'face 'org-agenda-calendar-sexp
5539 'mouse-face 'highlight
5540 'help-echo
5541 (format "mouse-2 or RET jump to org file %s"
5542 (abbreviate-file-name buffer-file-name))))
5543 (regexp "^&?%%(")
5544 marker category extra level ee txt tags entry
5545 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5546 (goto-char (point-min))
5547 (while (re-search-forward regexp nil t)
5548 (catch :skip
5549 (org-agenda-skip)
5550 (setq beg (match-beginning 0))
5551 (goto-char (1- (match-end 0)))
5552 (setq b (point))
5553 (forward-sexp 1)
5554 (setq sexp (buffer-substring b (point)))
5555 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5556 (org-trim (match-string 1))
5557 ""))
5558 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5559 (when result
5560 (setq marker (org-agenda-new-marker beg)
5561 level (make-string (org-reduced-level (org-outline-level)) ? )
5562 category (org-get-category beg)
5563 inherited-tags
5564 (or (eq org-agenda-show-inherited-tags 'always)
5565 (and (listp org-agenda-show-inherited-tags)
5566 (memq 'agenda org-agenda-show-inherited-tags))
5567 (and (eq org-agenda-show-inherited-tags t)
5568 (or (eq org-agenda-use-tag-inheritance t)
5569 (memq 'agenda org-agenda-use-tag-inheritance))))
5570 tags (org-get-tags-at nil (not inherited-tags))
5571 todo-state (org-get-todo-state)
5572 warntime (get-text-property (point) 'org-appt-warntime)
5573 extra nil)
5575 (dolist (r (if (stringp result)
5576 (list result)
5577 result)) ;; we expect a list here
5578 (when (and org-agenda-diary-sexp-prefix
5579 (string-match org-agenda-diary-sexp-prefix r))
5580 (setq extra (match-string 0 r)
5581 r (replace-match "" nil nil r)))
5582 (if (string-match "\\S-" r)
5583 (setq txt r)
5584 (setq txt "SEXP entry returned empty string"))
5585 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5586 (org-add-props txt props 'org-marker marker
5587 'date date 'todo-state todo-state
5588 'level level 'type "sexp" 'warntime warntime)
5589 (push txt ee)))))
5590 (nreverse ee)))
5592 ;; Calendar sanity: define some functions that are independent of
5593 ;; `calendar-date-style'.
5594 (defun org-anniversary (year month day &optional mark)
5595 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5596 (with-no-warnings
5597 (let ((calendar-date-style 'iso))
5598 (diary-anniversary year month day mark))))
5599 (defun org-cyclic (N year month day &optional mark)
5600 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5601 (with-no-warnings
5602 (let ((calendar-date-style 'iso))
5603 (diary-cyclic N year month day mark))))
5604 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5605 "Like `diary-block', but with fixed (ISO) order of arguments."
5606 (with-no-warnings
5607 (let ((calendar-date-style 'iso))
5608 (diary-block Y1 M1 D1 Y2 M2 D2 mark))))
5609 (defun org-date (year month day &optional mark)
5610 "Like `diary-date', but with fixed (ISO) order of arguments."
5611 (with-no-warnings
5612 (let ((calendar-date-style 'iso))
5613 (diary-date year month day mark))))
5615 ;; Define the `org-class' function
5616 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5617 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5618 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5619 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5620 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5621 `holidays', then any date that is known by the Emacs calendar to be a
5622 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5623 then those holidays will be skipped."
5624 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5625 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5626 (d (calendar-absolute-from-gregorian date))
5627 (h (when skip-weeks (calendar-check-holidays date))))
5628 (and
5629 (<= date1 d)
5630 (<= d date2)
5631 (= (calendar-day-of-week date) dayname)
5632 (or (not skip-weeks)
5633 (progn
5634 (require 'cal-iso)
5635 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5636 (not (or (and h (memq 'holidays skip-weeks))
5637 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5638 entry)))
5640 (defalias 'org-get-closed 'org-agenda-get-progress)
5641 (defun org-agenda-get-progress ()
5642 "Return the logged TODO entries for agenda display."
5643 (let* ((props (list 'mouse-face 'highlight
5644 'org-not-done-regexp org-not-done-regexp
5645 'org-todo-regexp org-todo-regexp
5646 'org-complex-heading-regexp org-complex-heading-regexp
5647 'help-echo
5648 (format "mouse-2 or RET jump to org file %s"
5649 (abbreviate-file-name buffer-file-name))))
5650 (items (if (consp org-agenda-show-log-scoped)
5651 org-agenda-show-log-scoped
5652 (if (eq org-agenda-show-log-scoped 'clockcheck)
5653 '(clock)
5654 org-agenda-log-mode-items)))
5655 (parts
5656 (delq nil
5657 (list
5658 (if (memq 'closed items) (concat "\\<" org-closed-string))
5659 (if (memq 'clock items) (concat "\\<" org-clock-string))
5660 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5661 (parts-re (if parts (mapconcat 'identity parts "\\|")
5662 (error "`org-agenda-log-mode-items' is empty")))
5663 (regexp (concat
5664 "\\(" parts-re "\\)"
5665 " *\\["
5666 (regexp-quote
5667 (substring
5668 (format-time-string
5669 (car org-time-stamp-formats)
5670 (apply 'encode-time ; DATE bound by calendar
5671 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5672 1 11))))
5673 (org-agenda-search-headline-for-time nil)
5674 marker hdmarker priority category level tags closedp
5675 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5676 (goto-char (point-min))
5677 (while (re-search-forward regexp nil t)
5678 (catch :skip
5679 (org-agenda-skip)
5680 (setq marker (org-agenda-new-marker (match-beginning 0))
5681 closedp (equal (match-string 1) org-closed-string)
5682 statep (equal (string-to-char (match-string 1)) ?-)
5683 clockp (not (or closedp statep))
5684 state (and statep (match-string 2))
5685 category (org-get-category (match-beginning 0))
5686 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5687 (when (string-match "\\]" timestr)
5688 ;; substring should only run to end of time stamp
5689 (setq rest (substring timestr (match-end 0))
5690 timestr (substring timestr 0 (match-end 0)))
5691 (if (and (not closedp) (not statep)
5692 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5693 rest))
5694 (progn (setq timestr (concat (substring timestr 0 -1)
5695 "-" (match-string 1 rest) "]"))
5696 (setq clocked (match-string 2 rest)))
5697 (setq clocked "-")))
5698 (save-excursion
5699 (setq extra
5700 (cond
5701 ((not org-agenda-log-mode-add-notes) nil)
5702 (statep
5703 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5704 (match-string 1)))
5705 (clockp
5706 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5707 (match-string 1)))))
5708 (if (not (re-search-backward org-outline-regexp-bol nil t))
5709 (throw :skip nil)
5710 (goto-char (match-beginning 0))
5711 (setq hdmarker (org-agenda-new-marker)
5712 inherited-tags
5713 (or (eq org-agenda-show-inherited-tags 'always)
5714 (and (listp org-agenda-show-inherited-tags)
5715 (memq 'todo org-agenda-show-inherited-tags))
5716 (and (eq org-agenda-show-inherited-tags t)
5717 (or (eq org-agenda-use-tag-inheritance t)
5718 (memq 'todo org-agenda-use-tag-inheritance))))
5719 tags (org-get-tags-at nil (not inherited-tags))
5720 level (make-string (org-reduced-level (org-outline-level)) ? ))
5721 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5722 (setq txt (match-string 1))
5723 (when extra
5724 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5725 (setq txt (concat (substring txt 0 (match-beginning 1))
5726 " - " extra " " (match-string 2 txt)))
5727 (setq txt (concat txt " - " extra))))
5728 (setq txt (org-agenda-format-item
5729 (cond
5730 (closedp "Closed: ")
5731 (statep (concat "State: (" state ")"))
5732 (t (concat "Clocked: (" clocked ")")))
5733 txt level category tags timestr)))
5734 (setq priority 100000)
5735 (org-add-props txt props
5736 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5737 'priority priority 'level level
5738 'type "closed" 'date date
5739 'undone-face 'org-warning 'done-face 'org-agenda-done)
5740 (push txt ee))
5741 (goto-char (point-at-eol))))
5742 (nreverse ee)))
5744 (defun org-agenda-show-clocking-issues ()
5745 "Add overlays, showing issues with clocking.
5746 See also the user option `org-agenda-clock-consistency-checks'."
5747 (interactive)
5748 (let* ((org-time-clocksum-use-effort-durations nil)
5749 (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-hh:mm-string-to-minutes
5758 (or (plist-get pl :max-duration) "24:00")))
5759 (mintime (org-hh:mm-string-to-minutes
5760 (or (plist-get pl :min-duration) 0)))
5761 (maxgap (org-hh:mm-string-to-minutes
5762 ;; default 30:00 means never complain
5763 (or (plist-get pl :max-gap) "30:00")))
5764 (gapok (mapcar 'org-hh:mm-string-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-minutes-to-clocksum-string
5799 (floor (/ (float dt) 60.))))
5800 face (or (plist-get pl :long-face) face)))
5801 ((< dt (* 60 mintime))
5802 ;; a very short clocking chunk
5803 (setq issue (format "Clocking interval is very short: %s"
5804 (org-minutes-to-clocksum-string
5805 (floor (/ (float dt) 60.))))
5806 face (or (plist-get pl :short-face) face)))
5807 ((and (> tlend 0) (< ts tlend))
5808 ;; Two clock entries are overlapping
5809 (setq issue (format "Clocking overlap: %d minutes"
5810 (/ (- tlend ts) 60))
5811 face (or (plist-get pl :overlap-face) face)))
5812 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5813 ;; There is a gap, lets see if we need to report it
5814 (unless (org-agenda-check-clock-gap tlend ts gapok)
5815 (setq issue (format "Clocking gap: %d minutes"
5816 (/ (- ts tlend) 60))
5817 face (or (plist-get pl :gap-face) face))))
5818 (t nil)))
5819 (setq tlend (or te tlend) tlstart (or ts tlstart))
5820 (when issue
5821 ;; OK, there was some issue, add an overlay to show the issue
5822 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5823 (overlay-put ov 'before-string
5824 (concat
5825 (org-add-props
5826 (format "%-43s" (concat " " issue))
5828 'face face)
5829 "\n"))
5830 (overlay-put ov 'evaporate t)))))
5832 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5833 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5834 (catch 'exit
5835 (unless ok-list
5836 ;; there are no OK times for gaps...
5837 (throw 'exit nil))
5838 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5839 ;; This is more than 24 hours, so it is OK.
5840 ;; because we have at least one OK time, that must be in the
5841 ;; 24 hour interval.
5842 (throw 'exit t))
5843 ;; We have a shorter gap.
5844 ;; Now we have to get the minute of the day when these times are
5845 (let* ((t1dec (decode-time (seconds-to-time t1)))
5846 (t2dec (decode-time (seconds-to-time t2)))
5847 ;; compute the minute on the day
5848 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5849 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5850 (when (< min2 min1)
5851 ;; if min2 is smaller than min1, this means it is on the next day.
5852 ;; Wrap it to after midnight.
5853 (setq min2 (+ min2 1440)))
5854 ;; Now check if any of the OK times is in the gap
5855 (mapc (lambda (x)
5856 ;; Wrap the time to after midnight if necessary
5857 (if (< x min1) (setq x (+ x 1440)))
5858 ;; Check if in interval
5859 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5860 ok-list)
5861 ;; Nope, this gap is not OK
5862 nil)))
5864 (defun org-agenda-get-deadlines (&optional with-hour)
5865 "Return the deadline information for agenda display.
5866 When WITH-HOUR is non-nil, only return deadlines with an hour
5867 specification like [h]h:mm."
5868 (let* ((props (list 'mouse-face 'highlight
5869 'org-not-done-regexp org-not-done-regexp
5870 'org-todo-regexp org-todo-regexp
5871 'org-complex-heading-regexp org-complex-heading-regexp
5872 'help-echo
5873 (format "mouse-2 or RET jump to org file %s"
5874 (abbreviate-file-name buffer-file-name))))
5875 (regexp (if with-hour
5876 org-deadline-time-hour-regexp
5877 org-deadline-time-regexp))
5878 (today (org-today))
5879 (today? (org-agenda-today-p date)) ; DATE bound by calendar.
5880 (current (calendar-absolute-from-gregorian date))
5881 deadline-items)
5882 (goto-char (point-min))
5883 (while (re-search-forward regexp nil t)
5884 (catch :skip
5885 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
5886 (org-agenda-skip)
5887 (let* ((s (match-string 1))
5888 (pos (1- (match-beginning 1)))
5889 (todo-state (save-match-data (org-get-todo-state)))
5890 (done? (member todo-state org-done-keywords))
5891 ;; DEADLINE is the deadline date for the entry. It is
5892 ;; either the base date or the last repeat, according
5893 ;; to `org-agenda-prefer-last-repeat'.
5894 (deadline
5895 (if (or (eq org-agenda-prefer-last-repeat t)
5896 (member todo-state org-agenda-prefer-last-repeat))
5897 (org-agenda--timestamp-to-absolute
5898 s today 'past (current-buffer) pos)
5899 (org-agenda--timestamp-to-absolute s)))
5900 ;; REPEAT is the future repeat closest from CURRENT,
5901 ;; according to `org-agenda-show-future-repeats'. If
5902 ;; the latter is nil, or if the time stamp has no
5903 ;; repeat part, default to DEADLINE.
5904 (repeat
5905 (cond
5906 ((<= current today) deadline)
5907 ((not org-agenda-show-future-repeats) deadline)
5909 (let ((base (if (eq org-agenda-show-future-repeats 'next)
5910 (1+ today)
5911 current)))
5912 (org-agenda--timestamp-to-absolute
5913 s base 'future (current-buffer) pos)))))
5914 (diff (- deadline current))
5915 (suppress-prewarning
5916 (let ((scheduled
5917 (and org-agenda-skip-deadline-prewarning-if-scheduled
5918 (org-entry-get nil "SCHEDULED"))))
5919 (cond
5920 ((not scheduled) nil)
5921 ;; The current item has a scheduled date, so
5922 ;; evaluate its prewarning lead time.
5923 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
5924 ;; Use global prewarning-restart lead time.
5925 org-agenda-skip-deadline-prewarning-if-scheduled)
5926 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
5927 'pre-scheduled)
5928 ;; Set pre-warning to no earlier than SCHEDULED.
5929 (min (- deadline
5930 (org-agenda--timestamp-to-absolute scheduled))
5931 org-deadline-warning-days))
5932 ;; Set pre-warning to deadline.
5933 (t 0))))
5934 (wdays (if suppress-prewarning
5935 (let ((org-deadline-warning-days suppress-prewarning))
5936 (org-get-wdays s))
5937 (org-get-wdays s))))
5938 ;; Display deadlines items at base date (DEADLINE), today,
5939 ;; if deadline is overdue or if the expiration of the
5940 ;; upcoming deadline is within WDAYS warning time. Also,
5941 ;; show any repeat past today.
5942 (when (or (and (/= current deadline)
5943 (/= current today)
5944 (/= current repeat))
5945 (and today?
5946 (> deadline current)
5947 (> diff wdays)))
5948 (throw :skip nil))
5949 ;; Possibly skip done tasks.
5950 (when (and done?
5951 (or org-agenda-skip-deadline-if-done
5952 (/= deadline current)))
5953 (throw :skip nil))
5954 (save-excursion
5955 (re-search-backward "^\\*+[ \t]+" nil t)
5956 (goto-char (match-end 0))
5957 (let* ((category (org-get-category))
5958 (level (make-string (org-reduced-level (org-outline-level))
5959 ?\s))
5960 (head (buffer-substring (point) (line-end-position)))
5961 (inherited-tags
5962 (or (eq org-agenda-show-inherited-tags 'always)
5963 (and (listp org-agenda-show-inherited-tags)
5964 (memq 'agenda org-agenda-show-inherited-tags))
5965 (and (eq org-agenda-show-inherited-tags t)
5966 (or (eq org-agenda-use-tag-inheritance t)
5967 (memq 'agenda
5968 org-agenda-use-tag-inheritance)))))
5969 (tags (org-get-tags-at nil (not inherited-tags)))
5970 (time
5971 (cond
5972 ;; No time of day designation if it is only
5973 ;; a reminder.
5974 ((and (/= current deadline) (/= current repeat)) nil)
5975 ((string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5976 (concat (substring s (match-beginning 1)) " "))
5977 (t 'time)))
5978 (item
5979 (org-agenda-format-item
5980 ;; Insert appropriate suffixes before deadlines.
5981 ;; Those only apply to today agenda.
5982 (pcase-let ((`(,now ,future ,past)
5983 org-agenda-deadline-leaders))
5984 (cond
5985 ((and today? (< deadline today)) (format past (- diff)))
5986 ((and today? (> deadline today)) (format future diff))
5987 (t now)))
5988 head level category tags time))
5989 (face (org-agenda-deadline-face
5990 (- 1 (/ (float diff) (max wdays 1)))))
5991 (upcoming? (and today? (> deadline today)))
5992 (warntime (get-text-property (point) 'org-appt-warntime)))
5993 (org-add-props item props
5994 'org-marker (org-agenda-new-marker pos)
5995 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
5996 'warntime warntime
5997 'level level
5998 'ts-date deadline
5999 'priority
6000 ;; Adjust priority to today reminders about deadlines.
6001 ;; Overdue deadlines get the highest priority
6002 ;; increase, then imminent deadlines and eventually
6003 ;; more distant deadlines.
6004 (let ((adjust (if today? (- diff) 0)))
6005 (+ adjust (org-get-priority item)))
6006 'todo-state todo-state
6007 'type (if upcoming? "upcoming-deadline" "deadline")
6008 'date (if upcoming? date deadline)
6009 'face (if done? 'org-agenda-done face)
6010 'undone-face face
6011 'done-face 'org-agenda-done)
6012 (push item deadline-items))))))
6013 (nreverse deadline-items)))
6015 (defun org-agenda-deadline-face (fraction)
6016 "Return the face to displaying a deadline item.
6017 FRACTION is what fraction of the head-warning time has passed."
6018 (assoc-default fraction org-agenda-deadline-faces #'<=))
6020 (defun org-agenda-get-scheduled (&optional deadlines with-hour)
6021 "Return the scheduled information for agenda display.
6022 Optional argument DEADLINES is a list of deadline items to be
6023 displayed in agenda view. When WITH-HOUR is non-nil, only return
6024 scheduled items with an hour specification like [h]h:mm."
6025 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
6026 'org-todo-regexp org-todo-regexp
6027 'org-complex-heading-regexp org-complex-heading-regexp
6028 'done-face 'org-agenda-done
6029 'mouse-face 'highlight
6030 'help-echo
6031 (format "mouse-2 or RET jump to Org file %s"
6032 (abbreviate-file-name buffer-file-name))))
6033 (regexp (if with-hour
6034 org-scheduled-time-hour-regexp
6035 org-scheduled-time-regexp))
6036 (today (org-today))
6037 (todayp (org-agenda-today-p date)) ; DATE bound by calendar.
6038 (current (calendar-absolute-from-gregorian date))
6039 (deadline-pos
6040 (mapcar (lambda (d)
6041 (let ((m (get-text-property 0 'org-hd-marker d)))
6042 (and m (marker-position m))))
6043 deadlines))
6044 scheduled-items)
6045 (goto-char (point-min))
6046 (while (re-search-forward regexp nil t)
6047 (catch :skip
6048 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
6049 (org-agenda-skip)
6050 (let* ((s (match-string 1))
6051 (pos (1- (match-beginning 1)))
6052 (todo-state (save-match-data (org-get-todo-state)))
6053 (donep (member todo-state org-done-keywords))
6054 ;; SCHEDULE is the scheduled date for the entry. It is
6055 ;; either the bare date or the last repeat, according
6056 ;; to `org-agenda-prefer-last-repeat'.
6057 (schedule
6058 (if (or (eq org-agenda-prefer-last-repeat t)
6059 (member todo-state org-agenda-prefer-last-repeat))
6060 (org-agenda--timestamp-to-absolute
6061 s today 'past (current-buffer) pos)
6062 (org-agenda--timestamp-to-absolute s)))
6063 ;; REPEAT is the future repeat closest from CURRENT,
6064 ;; according to `org-agenda-show-future-repeats'. If
6065 ;; the latter is nil, or if the time stamp has no
6066 ;; repeat part, default to SCHEDULE.
6067 (repeat
6068 (cond
6069 ((<= current today) schedule)
6070 ((not org-agenda-show-future-repeats) schedule)
6072 (let ((base (if (eq org-agenda-show-future-repeats 'next)
6073 (1+ today)
6074 current)))
6075 (org-agenda--timestamp-to-absolute
6076 s base 'future (current-buffer) pos)))))
6077 (diff (- current schedule))
6078 (warntime (get-text-property (point) 'org-appt-warntime))
6079 (pastschedp (< schedule today))
6080 (habitp (and (fboundp 'org-is-habit-p) (org-is-habit-p)))
6081 (suppress-delay
6082 (let ((deadline (and org-agenda-skip-scheduled-delay-if-deadline
6083 (org-entry-get nil "DEADLINE"))))
6084 (cond
6085 ((not deadline) nil)
6086 ;; The current item has a deadline date, so
6087 ;; evaluate its delay time.
6088 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6089 ;; Use global delay time.
6090 (- org-agenda-skip-scheduled-delay-if-deadline))
6091 ((eq org-agenda-skip-scheduled-delay-if-deadline
6092 'post-deadline)
6093 ;; Set delay to no later than DEADLINE.
6094 (min (- schedule
6095 (org-agenda--timestamp-to-absolute deadline))
6096 org-scheduled-delay-days))
6097 (t 0))))
6098 (ddays
6099 (cond
6100 ;; Nullify delay when a repeater triggered already
6101 ;; and the delay is of the form --Xd.
6102 ((and (string-match-p "--[0-9]+[hdwmy]" s)
6103 (> current schedule))
6105 (suppress-delay
6106 (let ((org-scheduled-delay-days suppress-delay))
6107 (org-get-wdays s t t)))
6108 (t (org-get-wdays s t)))))
6109 ;; Display scheduled items at base date (SCHEDULE), today if
6110 ;; scheduled before the current date, and at any repeat past
6111 ;; today. However, skip delayed items and items that have
6112 ;; been displayed for more than `org-scheduled-past-days'.
6113 (unless (and todayp
6114 habitp
6115 (bound-and-true-p org-habit-show-all-today))
6116 (when (or (and (> ddays 0) (< diff ddays))
6117 (> diff org-scheduled-past-days)
6118 (> schedule current)
6119 (and (/= current schedule)
6120 (/= current today)
6121 (/= current repeat)))
6122 (throw :skip nil)))
6123 ;; Possibly skip done tasks.
6124 (when (and donep
6125 (or org-agenda-skip-scheduled-if-done
6126 (/= schedule current)))
6127 (throw :skip nil))
6128 ;; Skip entry if it already appears as a deadline, per
6129 ;; `org-agenda-skip-scheduled-if-deadline-is-shown'. This
6130 ;; doesn't apply to habits.
6131 (when (pcase org-agenda-skip-scheduled-if-deadline-is-shown
6132 ((guard
6133 (or (not (memq (line-beginning-position 0) deadline-pos))
6134 habitp))
6135 nil)
6136 (`repeated-after-deadline
6137 (let ((deadline (time-to-days
6138 (org-get-deadline-time (point)))))
6139 (and (<= schedule deadline) (> current deadline))))
6140 (`not-today pastschedp)
6141 (`t t)
6142 (_ nil))
6143 (throw :skip nil))
6144 ;; Skip habits if `org-habit-show-habits' is nil, or if we
6145 ;; only show them for today. Also skip done habits.
6146 (when (and habitp
6147 (or donep
6148 (not (bound-and-true-p org-habit-show-habits))
6149 (and (not todayp)
6150 (bound-and-true-p
6151 org-habit-show-habits-only-for-today))))
6152 (throw :skip nil))
6153 (save-excursion
6154 (re-search-backward "^\\*+[ \t]+" nil t)
6155 (goto-char (match-end 0))
6156 (let* ((category (org-get-category))
6157 (inherited-tags
6158 (or (eq org-agenda-show-inherited-tags 'always)
6159 (and (listp org-agenda-show-inherited-tags)
6160 (memq 'agenda org-agenda-show-inherited-tags))
6161 (and (eq org-agenda-show-inherited-tags t)
6162 (or (eq org-agenda-use-tag-inheritance t)
6163 (memq 'agenda
6164 org-agenda-use-tag-inheritance)))))
6165 (tags (org-get-tags-at nil (not inherited-tags)))
6166 (level (make-string (org-reduced-level (org-outline-level))
6167 ?\s))
6168 (head (buffer-substring (point) (line-end-position)))
6169 (time
6170 (cond
6171 ;; No time of day designation if it is only
6172 ;; a reminder.
6173 ((and (/= current schedule) (/= current repeat)) nil)
6174 ((string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6175 (concat (substring s (match-beginning 1)) " "))
6176 (t 'time)))
6177 (item
6178 (org-agenda-format-item
6179 (pcase-let ((`(,first ,past) org-agenda-scheduled-leaders))
6180 ;; Show a reminder of a past scheduled today.
6181 (if (and todayp pastschedp)
6182 (format past (1+ diff))
6183 first))
6184 head level category tags time nil habitp))
6185 (face (cond ((and (not habitp) pastschedp)
6186 'org-scheduled-previously)
6187 (todayp 'org-scheduled-today)
6188 (t 'org-scheduled)))
6189 (habitp (and habitp (org-habit-parse-todo))))
6190 (org-add-props item props
6191 'undone-face face
6192 'face (if donep 'org-agenda-done face)
6193 'org-marker (org-agenda-new-marker pos)
6194 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
6195 'type (if pastschedp "past-scheduled" "scheduled")
6196 'date (if pastschedp schedule date)
6197 'ts-date schedule
6198 'warntime warntime
6199 'level level
6200 'priority (if habitp (org-habit-get-priority habitp)
6201 (+ 99 diff (org-get-priority item)))
6202 'org-habit-p habitp
6203 'todo-state todo-state)
6204 (push item scheduled-items))))))
6205 (nreverse scheduled-items)))
6207 (defun org-agenda-get-blocks ()
6208 "Return the date-range information for agenda display."
6209 (let* ((props (list 'face nil
6210 'org-not-done-regexp org-not-done-regexp
6211 'org-todo-regexp org-todo-regexp
6212 'org-complex-heading-regexp org-complex-heading-regexp
6213 'mouse-face 'highlight
6214 'help-echo
6215 (format "mouse-2 or RET jump to org file %s"
6216 (abbreviate-file-name buffer-file-name))))
6217 (regexp org-tr-regexp)
6218 (d0 (calendar-absolute-from-gregorian date))
6219 marker hdmarker ee txt d1 d2 s1 s2 category
6220 level todo-state tags pos head donep inherited-tags)
6221 (goto-char (point-min))
6222 (while (re-search-forward regexp nil t)
6223 (catch :skip
6224 (org-agenda-skip)
6225 (setq pos (point))
6226 (let ((start-time (match-string 1))
6227 (end-time (match-string 2)))
6228 (setq s1 (match-string 1)
6229 s2 (match-string 2)
6230 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
6231 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
6232 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6233 ;; Only allow days between the limits, because the normal
6234 ;; date stamps will catch the limits.
6235 (save-excursion
6236 (setq todo-state (org-get-todo-state))
6237 (setq donep (member todo-state org-done-keywords))
6238 (if (and donep org-agenda-skip-timestamp-if-done)
6239 (throw :skip t))
6240 (setq marker (org-agenda-new-marker (point))
6241 category (org-get-category))
6242 (if (not (re-search-backward org-outline-regexp-bol nil t))
6243 (throw :skip nil)
6244 (goto-char (match-beginning 0))
6245 (setq hdmarker (org-agenda-new-marker (point))
6246 inherited-tags
6247 (or (eq org-agenda-show-inherited-tags 'always)
6248 (and (listp org-agenda-show-inherited-tags)
6249 (memq 'agenda org-agenda-show-inherited-tags))
6250 (and (eq org-agenda-show-inherited-tags t)
6251 (or (eq org-agenda-use-tag-inheritance t)
6252 (memq 'agenda org-agenda-use-tag-inheritance))))
6254 tags (org-get-tags-at nil (not inherited-tags)))
6255 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6256 (looking-at "\\*+[ \t]+\\(.*\\)")
6257 (setq head (match-string 1))
6258 (let ((remove-re
6259 (if org-agenda-remove-timeranges-from-blocks
6260 (concat
6261 "<" (regexp-quote s1) ".*?>"
6262 "--"
6263 "<" (regexp-quote s2) ".*?>")
6264 nil)))
6265 (setq txt (org-agenda-format-item
6266 (format
6267 (nth (if (= d1 d2) 0 1)
6268 org-agenda-timerange-leaders)
6269 (1+ (- d0 d1)) (1+ (- d2 d1)))
6270 head level category tags
6271 (cond ((and (= d1 d0) (= d2 d0))
6272 (concat "<" start-time ">--<" end-time ">"))
6273 ((= d1 d0)
6274 (concat "<" start-time ">"))
6275 ((= d2 d0)
6276 (concat "<" end-time ">")))
6277 remove-re))))
6278 (org-add-props txt props
6279 'org-marker marker 'org-hd-marker hdmarker
6280 'type "block" 'date date
6281 'level level
6282 'todo-state todo-state
6283 'priority (org-get-priority txt))
6284 (push txt ee))))
6285 (goto-char pos)))
6286 ;; Sort the entries by expiration date.
6287 (nreverse ee)))
6289 ;;; Agenda presentation and sorting
6291 (defvar org-prefix-has-time nil
6292 "A flag, set by `org-compile-prefix-format'.
6293 The flag is set if the currently compiled format contains a `%t'.")
6294 (defvar org-prefix-has-tag nil
6295 "A flag, set by `org-compile-prefix-format'.
6296 The flag is set if the currently compiled format contains a `%T'.")
6297 (defvar org-prefix-has-effort nil
6298 "A flag, set by `org-compile-prefix-format'.
6299 The flag is set if the currently compiled format contains a `%e'.")
6300 (defvar org-prefix-has-breadcrumbs nil
6301 "A flag, set by `org-compile-prefix-format'.
6302 The flag is set if the currently compiled format contains a `%b'.")
6303 (defvar org-prefix-category-length nil
6304 "Used by `org-compile-prefix-format' to remember the category field width.")
6305 (defvar org-prefix-category-max-length nil
6306 "Used by `org-compile-prefix-format' to remember the category field width.")
6308 (defun org-agenda-get-category-icon (category)
6309 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6310 (cl-dolist (entry org-agenda-category-icon-alist)
6311 (when (string-match-p (car entry) category)
6312 (if (listp (cadr entry))
6313 (cl-return (cadr entry))
6314 (cl-return (apply #'create-image (cdr entry)))))))
6316 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6317 remove-re habitp)
6318 "Format TXT to be inserted into the agenda buffer.
6319 In particular, add the prefix and corresponding text properties.
6321 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6322 LEVEL may be a string to replace the `%l' specifier.
6323 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6324 category taken from local variable or file name. It will replace the `%c'
6325 specifier in the format.
6326 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6327 TXT for sorting of this entry, and for the `%t' specifier in the format.
6328 When DOTIME is a string, this string is searched for a time before TXT is.
6329 TAGS can be the tags of the headline.
6330 Any match of REMOVE-RE will be removed from TXT."
6331 ;; We keep the org-prefix-* variable values along with a compiled
6332 ;; formatter, so that multiple agendas existing at the same time do
6333 ;; not step on each other toes.
6335 ;; It was inconvenient to make these variables buffer local in
6336 ;; Agenda buffers, because this function expects to be called with
6337 ;; the buffer where item comes from being current, and not agenda
6338 ;; buffer
6339 (let* ((bindings (car org-prefix-format-compiled))
6340 (formatter (cadr org-prefix-format-compiled)))
6341 (cl-loop for (var value) in bindings
6342 do (set var value))
6343 (save-match-data
6344 ;; Diary entries sometimes have extra whitespace at the beginning
6345 (setq txt (org-trim txt))
6347 ;; Fix the tags part in txt
6348 (setq txt (org-agenda-fix-displayed-tags
6349 txt tags
6350 org-agenda-show-inherited-tags
6351 org-agenda-hide-tags-regexp))
6353 (let* ((category (or category
6354 (if buffer-file-name
6355 (file-name-sans-extension
6356 (file-name-nondirectory buffer-file-name))
6357 "")))
6358 (category-icon (org-agenda-get-category-icon category))
6359 (category-icon (if category-icon
6360 (propertize " " 'display category-icon)
6361 ""))
6362 (effort (and (not (string= txt ""))
6363 (get-text-property 1 'effort txt)))
6364 ;; time, tag, effort are needed for the eval of the prefix format
6365 (tag (if tags (nth (1- (length tags)) tags) ""))
6366 time
6367 (ts (if dotime (concat
6368 (if (stringp dotime) dotime "")
6369 (and org-agenda-search-headline-for-time txt))))
6370 (time-of-day (and dotime (org-get-time-of-day ts)))
6371 stamp plain s0 s1 s2 rtn srp l
6372 duration breadcrumbs)
6373 (and (derived-mode-p 'org-mode) buffer-file-name
6374 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6375 (when (and dotime time-of-day)
6376 ;; Extract starting and ending time and move them to prefix
6377 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6378 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6379 (setq s0 (match-string 0 ts)
6380 srp (and stamp (match-end 3))
6381 s1 (match-string (if plain 1 2) ts)
6382 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6384 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6385 ;; them, we might want to remove them there to avoid duplication.
6386 ;; The user can turn this off with a variable.
6387 (if (and org-prefix-has-time
6388 org-agenda-remove-times-when-in-prefix (or stamp plain)
6389 (string-match (concat (regexp-quote s0) " *") txt)
6390 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6391 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6392 (= (match-beginning 0) 0)
6394 (setq txt (replace-match "" nil nil txt))))
6395 ;; Normalize the time(s) to 24 hour
6396 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6397 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6399 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
6400 (let (org-time-clocksum-use-effort-durations)
6401 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6402 (setq s2
6403 (org-minutes-to-clocksum-string
6404 (+ (org-hh:mm-string-to-minutes s1)
6405 org-agenda-default-appointment-duration)))))
6407 ;; Compute the duration
6408 (when s2
6409 (setq duration (- (org-hh:mm-string-to-minutes s2)
6410 (org-hh:mm-string-to-minutes s1)))))
6412 (when (string-match "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" txt)
6413 ;; Tags are in the string
6414 (if (or (eq org-agenda-remove-tags t)
6415 (and org-agenda-remove-tags
6416 org-prefix-has-tag))
6417 (setq txt (replace-match "" t t txt))
6418 (setq txt (replace-match
6419 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6420 (match-string 2 txt))
6421 t t txt))))
6423 (when remove-re
6424 (while (string-match remove-re txt)
6425 (setq txt (replace-match "" t t txt))))
6427 ;; Set org-heading property on `txt' to mark the start of the
6428 ;; heading.
6429 (add-text-properties 0 (length txt) '(org-heading t) txt)
6431 ;; Prepare the variables needed in the eval of the compiled format
6432 (if org-prefix-has-breadcrumbs
6433 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6434 (let ((s (org-display-outline-path nil nil "->" t)))
6435 (if (eq "" s) "" (concat s "->"))))))
6436 (setq time (cond (s2 (concat
6437 (org-agenda-time-of-day-to-ampm-maybe s1)
6438 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6439 (if org-agenda-timegrid-use-ampm " ")))
6440 (s1 (concat
6441 (org-agenda-time-of-day-to-ampm-maybe s1)
6442 (if org-agenda-timegrid-use-ampm
6443 "........ "
6444 "......")))
6445 (t ""))
6446 extra (or (and (not habitp) extra) "")
6447 category (if (symbolp category) (symbol-name category) category)
6448 level (or level ""))
6449 (if (string-match org-bracket-link-regexp category)
6450 (progn
6451 (setq l (if (match-end 3)
6452 (- (match-end 3) (match-beginning 3))
6453 (- (match-end 1) (match-beginning 1))))
6454 (when (< l (or org-prefix-category-length 0))
6455 (setq category (copy-sequence category))
6456 (org-add-props category nil
6457 'extra-space (make-string
6458 (- org-prefix-category-length l 1) ?\ ))))
6459 (if (and org-prefix-category-max-length
6460 (>= (length category) org-prefix-category-max-length))
6461 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6462 ;; Evaluate the compiled format
6463 (setq rtn (concat (eval formatter) txt))
6465 ;; And finally add the text properties
6466 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6467 (org-add-props rtn nil
6468 'org-category category
6469 'tags (mapcar 'org-downcase-keep-props tags)
6470 'org-highest-priority org-highest-priority
6471 'org-lowest-priority org-lowest-priority
6472 'time-of-day time-of-day
6473 'duration duration
6474 'breadcrumbs breadcrumbs
6475 'txt txt
6476 'level level
6477 'time time
6478 'extra extra
6479 'format org-prefix-format-compiled
6480 'dotime dotime)))))
6482 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6483 "Remove tags string from TXT, and add a modified list of tags.
6484 The modified list may contain inherited tags, and tags matched by
6485 `org-agenda-hide-tags-regexp' will be removed."
6486 (when (or add-inherited hide-re)
6487 (if (string-match "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" txt)
6488 (setq txt (substring txt 0 (match-beginning 0))))
6489 (setq tags
6490 (delq nil
6491 (mapcar (lambda (tg)
6492 (if (or (and hide-re (string-match hide-re tg))
6493 (and (not add-inherited)
6494 (get-text-property 0 'inherited tg)))
6496 tg))
6497 tags)))
6498 (when tags
6499 (let ((have-i (get-text-property 0 'inherited (car tags)))
6501 (setq txt (concat txt " :"
6502 (mapconcat
6503 (lambda (x)
6504 (setq i (get-text-property 0 'inherited x))
6505 (if (and have-i (not i))
6506 (progn
6507 (setq have-i nil)
6508 (concat ":" x))
6510 tags ":")
6511 (if have-i "::" ":"))))))
6512 txt)
6514 (defun org-downcase-keep-props (s)
6515 (let ((props (text-properties-at 0 s)))
6516 (setq s (downcase s))
6517 (add-text-properties 0 (length s) props s)
6520 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6522 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6523 "Add a time-grid for agenda items which need it.
6525 LIST is the list of agenda items formatted by `org-agenda-list'.
6526 NDAYS is the span of the current agenda view.
6527 TODAYP is t when the current agenda view is on today."
6528 (catch 'exit
6529 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6530 ((and todayp (member 'today (car org-agenda-time-grid))))
6531 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6532 ((member 'weekly (car org-agenda-time-grid)))
6533 (t (throw 'exit list)))
6534 (let* ((have (delq nil (mapcar
6535 (lambda (x) (get-text-property 1 'time-of-day x))
6536 list)))
6537 (string (nth 1 org-agenda-time-grid))
6538 (gridtimes (nth 2 org-agenda-time-grid))
6539 (req (car org-agenda-time-grid))
6540 (remove (member 'remove-match req))
6541 new time)
6542 (if (and (member 'require-timed req) (not have))
6543 ;; don't show empty grid
6544 (throw 'exit list))
6545 (while (setq time (pop gridtimes))
6546 (unless (and remove (member time have))
6547 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6548 (push (org-agenda-format-item
6549 nil string nil "" nil
6550 (concat (substring time 0 -2) ":" (substring time -2)))
6551 new)
6552 (put-text-property
6553 2 (length (car new)) 'face 'org-time-grid (car new))))
6554 (when (and todayp org-agenda-show-current-time-in-grid)
6555 (push (org-agenda-format-item
6556 nil org-agenda-current-time-string nil "" nil
6557 (format-time-string "%H:%M "))
6558 new)
6559 (put-text-property
6560 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6562 (if (member 'time-up org-agenda-sorting-strategy-selected)
6563 (append new list)
6564 (append list new)))))
6566 (defun org-compile-prefix-format (key)
6567 "Compile the prefix format into a Lisp form that can be evaluated.
6568 The resulting form and associated variable bindings is returned
6569 and stored in the variable `org-prefix-format-compiled'."
6570 (setq org-prefix-has-time nil
6571 org-prefix-has-tag nil
6572 org-prefix-category-length nil
6573 org-prefix-has-effort nil
6574 org-prefix-has-breadcrumbs nil)
6575 (let ((s (cond
6576 ((stringp org-agenda-prefix-format)
6577 org-agenda-prefix-format)
6578 ((assq key org-agenda-prefix-format)
6579 (cdr (assq key org-agenda-prefix-format)))
6580 (t " %-12:c%?-12t% s")))
6581 (start 0)
6582 varform vars var e c f opt)
6583 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6584 s start)
6585 (setq var (or (cdr (assoc (match-string 4 s)
6586 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6587 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6588 'eval)
6589 c (or (match-string 3 s) "")
6590 opt (match-beginning 1)
6591 start (1+ (match-beginning 0)))
6592 (if (eq var 'time) (setq org-prefix-has-time t))
6593 (if (eq var 'tag) (setq org-prefix-has-tag t))
6594 (if (eq var 'effort) (setq org-prefix-has-effort t))
6595 (if (eq var 'breadcrumbs) (setq org-prefix-has-breadcrumbs t))
6596 (setq f (concat "%" (match-string 2 s) "s"))
6597 (when (eq var 'category)
6598 (setq org-prefix-category-length
6599 (floor (abs (string-to-number (match-string 2 s)))))
6600 (setq org-prefix-category-max-length
6601 (let ((x (match-string 2 s)))
6602 (save-match-data
6603 (if (string-match "\\.[0-9]+" x)
6604 (string-to-number (substring (match-string 0 x) 1)))))))
6605 (if (eq var 'eval)
6606 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6607 (if opt
6608 (setq varform
6609 `(if (or (equal "" ,var) (equal nil ,var))
6611 (format ,f (concat ,var ,c))))
6612 (setq varform
6613 `(format ,f (if (or (equal ,var "")
6614 (equal ,var nil)) ""
6615 (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6616 (setq s (replace-match "%s" t nil s))
6617 (push varform vars))
6618 (setq vars (nreverse vars))
6619 (with-current-buffer (or org-agenda-buffer (current-buffer))
6620 (setq org-prefix-format-compiled
6621 (list
6622 `((org-prefix-has-time ,org-prefix-has-time)
6623 (org-prefix-has-tag ,org-prefix-has-tag)
6624 (org-prefix-category-length ,org-prefix-category-length)
6625 (org-prefix-has-effort ,org-prefix-has-effort)
6626 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6627 `(format ,s ,@vars))))))
6629 (defun org-set-sorting-strategy (key)
6630 (if (symbolp (car org-agenda-sorting-strategy))
6631 ;; the old format
6632 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6633 (setq org-agenda-sorting-strategy-selected
6634 (or (cdr (assq key org-agenda-sorting-strategy))
6635 (cdr (assq 'agenda org-agenda-sorting-strategy))
6636 '(time-up category-keep priority-down)))))
6638 (defun org-get-time-of-day (s &optional string mod24)
6639 "Check string S for a time of day.
6640 If found, return it as a military time number between 0 and 2400.
6641 If not found, return nil.
6642 The optional STRING argument forces conversion into a 5 character wide string
6643 HH:MM."
6644 (save-match-data
6645 (when
6646 (and
6647 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6648 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6649 (not (eq (get-text-property 1 'face s) 'org-link)))
6650 (let* ((h (string-to-number (match-string 1 s)))
6651 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6652 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6653 (am-p (equal ampm "am"))
6654 (h1 (cond ((not ampm) h)
6655 ((= h 12) (if am-p 0 12))
6656 (t (+ h (if am-p 0 12)))))
6657 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6658 (mod h1 24) h1))
6659 (t0 (+ (* 100 h2) m))
6660 (t1 (concat (if (>= h1 24) "+" " ")
6661 (if (and org-agenda-time-leading-zero
6662 (< t0 1000)) "0" "")
6663 (if (< t0 100) "0" "")
6664 (if (< t0 10) "0" "")
6665 (int-to-string t0))))
6666 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6668 (defvar org-agenda-before-sorting-filter-function nil
6669 "Function to be applied to agenda items prior to sorting.
6670 Prior to sorting also means just before they are inserted into the agenda.
6672 To aid sorting, you may revisit the original entries and add more text
6673 properties which will later be used by the sorting functions.
6675 The function should take a string argument, an agenda line.
6676 It has access to the text properties in that line, which contain among
6677 other things, the property `org-hd-marker' that points to the entry
6678 where the line comes from. Note that not all lines going into the agenda
6679 have this property, only most.
6681 The function should return the modified string. It is probably best
6682 to ONLY change text properties.
6684 You can also use this function as a filter, by returning nil for lines
6685 you don't want to have in the agenda at all. For this application, you
6686 could bind the variable in the options section of a custom command.")
6688 (defun org-agenda-finalize-entries (list &optional type)
6689 "Sort, limit and concatenate the LIST of agenda items.
6690 The optional argument TYPE tells the agenda type."
6691 (let ((max-effort (cond ((listp org-agenda-max-effort)
6692 (cdr (assoc type org-agenda-max-effort)))
6693 (t org-agenda-max-effort)))
6694 (max-todo (cond ((listp org-agenda-max-todos)
6695 (cdr (assoc type org-agenda-max-todos)))
6696 (t org-agenda-max-todos)))
6697 (max-tags (cond ((listp org-agenda-max-tags)
6698 (cdr (assoc type org-agenda-max-tags)))
6699 (t org-agenda-max-tags)))
6700 (max-entries (cond ((listp org-agenda-max-entries)
6701 (cdr (assoc type org-agenda-max-entries)))
6702 (t org-agenda-max-entries))))
6703 (when org-agenda-before-sorting-filter-function
6704 (setq list
6705 (delq nil
6706 (mapcar
6707 org-agenda-before-sorting-filter-function list))))
6708 (setq list (mapcar 'org-agenda-highlight-todo list)
6709 list (mapcar 'identity (sort list 'org-entries-lessp)))
6710 (when max-effort
6711 (setq list (org-agenda-limit-entries
6712 list 'effort-minutes max-effort
6713 (lambda (e) (or e (if org-sort-agenda-noeffort-is-high
6714 32767 -1))))))
6715 (when max-todo
6716 (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
6717 (when max-tags
6718 (setq list (org-agenda-limit-entries list 'tags max-tags)))
6719 (when max-entries
6720 (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
6721 (mapconcat 'identity list "\n")))
6723 (defun org-agenda-limit-entries (list prop limit &optional fn)
6724 "Limit the number of agenda entries."
6725 (let ((include (and limit (< limit 0))))
6726 (if limit
6727 (let ((fun (or fn (lambda (p) (if p 1))))
6728 (lim 0))
6729 (delq nil
6730 (mapcar
6731 (lambda (e)
6732 (let ((pval (funcall
6733 fun (get-text-property (1- (length e))
6734 prop e))))
6735 (if pval (setq lim (+ lim pval)))
6736 (cond ((and pval (<= lim (abs limit))) e)
6737 ((and include (not pval)) e))))
6738 list)))
6739 list)))
6741 (defun org-agenda-limit-interactively (remove)
6742 "In agenda, interactively limit entries to various maximums."
6743 (interactive "P")
6744 (if remove
6745 (progn (setq org-agenda-max-entries nil
6746 org-agenda-max-todos nil
6747 org-agenda-max-tags nil
6748 org-agenda-max-effort nil)
6749 (org-agenda-redo))
6750 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6751 (msg (cond ((= max ?E) "How many minutes? ")
6752 ((= max ?e) "How many entries? ")
6753 ((= max ?t) "How many TODO entries? ")
6754 ((= max ?T) "How many tagged entries? ")
6755 (t (user-error "Wrong input"))))
6756 (num (string-to-number (read-from-minibuffer msg))))
6757 (cond ((equal max ?e)
6758 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6759 ((equal max ?t)
6760 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6761 ((equal max ?T)
6762 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6763 ((equal max ?E)
6764 (let ((org-agenda-max-effort num)) (org-agenda-redo))))))
6765 (org-agenda-fit-window-to-buffer))
6767 (defun org-agenda-highlight-todo (x)
6768 (let ((org-done-keywords org-done-keywords-for-agenda)
6769 (case-fold-search nil)
6771 (if (eq x 'line)
6772 (save-excursion
6773 (beginning-of-line 1)
6774 (setq re (org-get-at-bol 'org-todo-regexp))
6775 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6776 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6777 (add-text-properties (match-beginning 0) (match-end 1)
6778 (list 'face (org-get-todo-face 1)))
6779 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6780 (delete-region (match-beginning 1) (1- (match-end 0)))
6781 (goto-char (match-beginning 1))
6782 (insert (format org-agenda-todo-keyword-format s)))))
6783 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6784 (setq re (get-text-property 0 'org-todo-regexp x))
6785 (when (and re
6786 ;; Test `pl' because if there's no heading content,
6787 ;; there's no point matching to highlight. Note
6788 ;; that if we didn't test `pl' first, and there
6789 ;; happened to be no keyword from `org-todo-regexp'
6790 ;; on this heading line, then the `equal' comparison
6791 ;; afterwards would spuriously succeed in the case
6792 ;; where `pl' is nil -- causing an args-out-of-range
6793 ;; error when we try to add text properties to text
6794 ;; that isn't there.
6796 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6797 x pl) pl))
6798 (add-text-properties
6799 (or (match-end 1) (match-end 0)) (match-end 0)
6800 (list 'face (org-get-todo-face (match-string 2 x)))
6802 (when (match-end 1)
6803 (setq x (concat (substring x 0 (match-end 1))
6804 (format org-agenda-todo-keyword-format
6805 (match-string 2 x))
6806 (org-add-props " " (text-properties-at 0 x))
6807 (substring x (match-end 3)))))))
6808 x)))
6810 (defsubst org-cmp-values (a b property)
6811 "Compare the numeric value of text PROPERTY for string A and B."
6812 (let ((pa (or (get-text-property (1- (length a)) property a) 0))
6813 (pb (or (get-text-property (1- (length b)) property b) 0)))
6814 (cond ((> pa pb) +1)
6815 ((< pa pb) -1))))
6817 (defsubst org-cmp-effort (a b)
6818 "Compare the effort values of string A and B."
6819 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6820 ;; `effort-minutes' property is not directly accessible from
6821 ;; the strings, but is stored as a property in `txt'.
6822 (ea (or (get-text-property
6823 0 'effort-minutes (get-text-property 0 'txt a))
6824 def))
6825 (eb (or (get-text-property
6826 0 'effort-minutes (get-text-property 0 'txt b))
6827 def)))
6828 (cond ((> ea eb) +1)
6829 ((< ea eb) -1))))
6831 (defsubst org-cmp-category (a b)
6832 "Compare the string values of categories of strings A and B."
6833 (let ((ca (or (get-text-property (1- (length a)) 'org-category a) ""))
6834 (cb (or (get-text-property (1- (length b)) 'org-category b) "")))
6835 (cond ((string-lessp ca cb) -1)
6836 ((string-lessp cb ca) +1))))
6838 (defsubst org-cmp-todo-state (a b)
6839 "Compare the todo states of strings A and B."
6840 (let* ((ma (or (get-text-property 1 'org-marker a)
6841 (get-text-property 1 'org-hd-marker a)))
6842 (mb (or (get-text-property 1 'org-marker b)
6843 (get-text-property 1 'org-hd-marker b)))
6844 (fa (and ma (marker-buffer ma)))
6845 (fb (and mb (marker-buffer mb)))
6846 (todo-kwds
6847 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6848 (and fb (with-current-buffer fb org-todo-keywords-1))))
6849 (ta (or (get-text-property 1 'todo-state a) ""))
6850 (tb (or (get-text-property 1 'todo-state b) ""))
6851 (la (- (length (member ta todo-kwds))))
6852 (lb (- (length (member tb todo-kwds))))
6853 (donepa (member ta org-done-keywords-for-agenda))
6854 (donepb (member tb org-done-keywords-for-agenda)))
6855 (cond ((and donepa (not donepb)) -1)
6856 ((and (not donepa) donepb) +1)
6857 ((< la lb) -1)
6858 ((< lb la) +1))))
6860 (defsubst org-cmp-alpha (a b)
6861 "Compare the headlines, alphabetically."
6862 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6863 (plb (text-property-any 0 (length b) 'org-heading t b))
6864 (ta (and pla (substring a pla)))
6865 (tb (and plb (substring b plb)))
6866 (case-fold-search nil))
6867 (when pla
6868 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6869 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6870 (setq ta (substring ta (match-end 0))))
6871 (setq ta (downcase ta)))
6872 (when plb
6873 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6874 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6875 (setq tb (substring tb (match-end 0))))
6876 (setq tb (downcase tb)))
6877 (cond ((not ta) +1)
6878 ((not tb) -1)
6879 ((string-lessp ta tb) -1)
6880 ((string-lessp tb ta) +1))))
6882 (defsubst org-cmp-tag (a b)
6883 "Compare the string values of the first tags of A and B."
6884 (let ((ta (car (last (get-text-property 1 'tags a))))
6885 (tb (car (last (get-text-property 1 'tags b)))))
6886 (cond ((not ta) +1)
6887 ((not tb) -1)
6888 ((string-lessp ta tb) -1)
6889 ((string-lessp tb ta) +1))))
6891 (defsubst org-cmp-time (a b)
6892 "Compare the time-of-day values of strings A and B."
6893 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6894 (ta (or (get-text-property 1 'time-of-day a) def))
6895 (tb (or (get-text-property 1 'time-of-day b) def)))
6896 (cond ((< ta tb) -1)
6897 ((< tb ta) +1))))
6899 (defsubst org-cmp-ts (a b type)
6900 "Compare the timestamps values of entries A and B.
6901 When TYPE is \"scheduled\", \"deadline\", \"timestamp\" or
6902 \"timestamp_ia\", compare within each of these type. When TYPE
6903 is the empty string, compare all timestamps without respect of
6904 their type."
6905 (let* ((def (if org-sort-agenda-notime-is-late most-positive-fixnum -1))
6906 (ta (or (and (string-match type (or (get-text-property 1 'type a) ""))
6907 (get-text-property 1 'ts-date a))
6908 def))
6909 (tb (or (and (string-match type (or (get-text-property 1 'type b) ""))
6910 (get-text-property 1 'ts-date b))
6911 def)))
6912 (cond ((< ta tb) -1)
6913 ((< tb ta) +1))))
6915 (defsubst org-cmp-habit-p (a b)
6916 "Compare the todo states of strings A and B."
6917 (let ((ha (get-text-property 1 'org-habit-p a))
6918 (hb (get-text-property 1 'org-habit-p b)))
6919 (cond ((and ha (not hb)) -1)
6920 ((and (not ha) hb) +1))))
6922 (defun org-entries-lessp (a b)
6923 "Predicate for sorting agenda entries."
6924 ;; The following variables will be used when the form is evaluated.
6925 ;; So even though the compiler complains, keep them.
6926 (let* ((ss org-agenda-sorting-strategy-selected)
6927 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
6928 (org-cmp-ts a b "")))
6929 (timestamp-down (if timestamp-up (- timestamp-up) nil))
6930 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
6931 (org-cmp-ts a b "scheduled")))
6932 (scheduled-down (if scheduled-up (- scheduled-up) nil))
6933 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
6934 (org-cmp-ts a b "deadline")))
6935 (deadline-down (if deadline-up (- deadline-up) nil))
6936 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
6937 (org-cmp-ts a b "timestamp_ia")))
6938 (tsia-down (if tsia-up (- tsia-up) nil))
6939 (ts-up (and (org-em 'ts-up 'ts-down ss)
6940 (org-cmp-ts a b "timestamp")))
6941 (ts-down (if ts-up (- ts-up) nil))
6942 (time-up (and (org-em 'time-up 'time-down ss)
6943 (org-cmp-time a b)))
6944 (time-down (if time-up (- time-up) nil))
6945 (stats-up (and (org-em 'stats-up 'stats-down ss)
6946 (org-cmp-values a b 'org-stats)))
6947 (stats-down (if stats-up (- stats-up) nil))
6948 (priority-up (and (org-em 'priority-up 'priority-down ss)
6949 (org-cmp-values a b 'priority)))
6950 (priority-down (if priority-up (- priority-up) nil))
6951 (effort-up (and (org-em 'effort-up 'effort-down ss)
6952 (org-cmp-effort a b)))
6953 (effort-down (if effort-up (- effort-up) nil))
6954 (category-up (and (or (org-em 'category-up 'category-down ss)
6955 (memq 'category-keep ss))
6956 (org-cmp-category a b)))
6957 (category-down (if category-up (- category-up) nil))
6958 (category-keep (if category-up +1 nil))
6959 (tag-up (and (org-em 'tag-up 'tag-down ss)
6960 (org-cmp-tag a b)))
6961 (tag-down (if tag-up (- tag-up) nil))
6962 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
6963 (org-cmp-todo-state a b)))
6964 (todo-state-down (if todo-state-up (- todo-state-up) nil))
6965 (habit-up (and (org-em 'habit-up 'habit-down ss)
6966 (org-cmp-habit-p a b)))
6967 (habit-down (if habit-up (- habit-up) nil))
6968 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
6969 (org-cmp-alpha a b)))
6970 (alpha-down (if alpha-up (- alpha-up) nil))
6971 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
6972 user-defined-up user-defined-down)
6973 (if (and need-user-cmp org-agenda-cmp-user-defined
6974 (functionp org-agenda-cmp-user-defined))
6975 (setq user-defined-up
6976 (funcall org-agenda-cmp-user-defined a b)
6977 user-defined-down (if user-defined-up (- user-defined-up) nil)))
6978 (cdr (assoc
6979 (eval (cons 'or org-agenda-sorting-strategy-selected))
6980 '((-1 . t) (1 . nil) (nil . nil))))))
6982 ;;; Agenda restriction lock
6984 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
6985 "Overlay to mark the headline to which agenda commands are restricted.")
6986 (overlay-put org-agenda-restriction-lock-overlay
6987 'face 'org-agenda-restriction-lock)
6988 (overlay-put org-agenda-restriction-lock-overlay
6989 'help-echo "Agendas are currently limited to this subtree.")
6990 (delete-overlay org-agenda-restriction-lock-overlay)
6992 (defun org-agenda-set-restriction-lock-from-agenda (arg)
6993 "Set the restriction lock to the agenda item at point from within the agenda.
6994 When called with a `\\[universal-argument]' prefix, restrict to
6995 the file which contains the item.
6996 Argument ARG is the prefix argument."
6997 (interactive "P")
6998 (unless (derived-mode-p 'org-agenda-mode)
6999 (user-error "Not in an Org agenda buffer"))
7000 (let* ((marker (or (org-get-at-bol 'org-marker)
7001 (org-agenda-error)))
7002 (buffer (marker-buffer marker))
7003 (pos (marker-position marker)))
7004 (with-current-buffer buffer
7005 (goto-char pos)
7006 (org-agenda-set-restriction-lock arg))))
7008 ;;;###autoload
7009 (defun org-agenda-set-restriction-lock (&optional type)
7010 "Set restriction lock for agenda, to current subtree or file.
7011 Restriction will be the file if TYPE is `file', or if type is the
7012 universal prefix \\='(4), or if the cursor is before the first headline
7013 in the file. Otherwise, restriction will be to the current subtree."
7014 (interactive "P")
7015 (org-agenda-remove-restriction-lock 'noupdate)
7016 (and (equal type '(4)) (setq type 'file))
7017 (setq type (cond
7018 (type type)
7019 ((org-at-heading-p) 'subtree)
7020 ((condition-case nil (org-back-to-heading t) (error nil))
7021 'subtree)
7022 (t 'file)))
7023 (if (eq type 'subtree)
7024 (progn
7025 (setq org-agenda-restrict (current-buffer))
7026 (setq org-agenda-overriding-restriction 'subtree)
7027 (put 'org-agenda-files 'org-restrict
7028 (list (buffer-file-name (buffer-base-buffer))))
7029 (org-back-to-heading t)
7030 (move-overlay org-agenda-restriction-lock-overlay
7031 (point)
7032 (if org-agenda-restriction-lock-highlight-subtree
7033 (save-excursion (org-end-of-subtree t t) (point))
7034 (point-at-eol)))
7035 (move-marker org-agenda-restrict-begin (point))
7036 (move-marker org-agenda-restrict-end
7037 (save-excursion (org-end-of-subtree t t)))
7038 (message "Locking agenda restriction to subtree"))
7039 (put 'org-agenda-files 'org-restrict
7040 (list (buffer-file-name (buffer-base-buffer))))
7041 (setq org-agenda-restrict nil)
7042 (setq org-agenda-overriding-restriction 'file)
7043 (move-marker org-agenda-restrict-begin nil)
7044 (move-marker org-agenda-restrict-end nil)
7045 (message "Locking agenda restriction to file"))
7046 (setq current-prefix-arg nil)
7047 (org-agenda-maybe-redo))
7049 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7050 "Remove the agenda restriction lock."
7051 (interactive "P")
7052 (delete-overlay org-agenda-restriction-lock-overlay)
7053 (delete-overlay org-speedbar-restriction-lock-overlay)
7054 (setq org-agenda-overriding-restriction nil)
7055 (setq org-agenda-restrict nil)
7056 (put 'org-agenda-files 'org-restrict nil)
7057 (move-marker org-agenda-restrict-begin nil)
7058 (move-marker org-agenda-restrict-end nil)
7059 (setq current-prefix-arg nil)
7060 (message "Agenda restriction lock removed")
7061 (or noupdate (org-agenda-maybe-redo)))
7063 (defun org-agenda-maybe-redo ()
7064 "If there is any window showing the agenda view, update it."
7065 (let ((w (get-buffer-window (or org-agenda-this-buffer-name
7066 org-agenda-buffer-name)
7068 (w0 (selected-window)))
7069 (when w
7070 (select-window w)
7071 (org-agenda-redo)
7072 (select-window w0)
7073 (if org-agenda-overriding-restriction
7074 (message "Agenda view shifted to new %s restriction"
7075 org-agenda-overriding-restriction)
7076 (message "Agenda restriction lock removed")))))
7078 ;;; Agenda commands
7080 (defun org-agenda-check-type (error &rest types)
7081 "Check if agenda buffer is of allowed type.
7082 If ERROR is non-nil, throw an error, otherwise just return nil.
7083 Allowed types are `agenda' `timeline' `todo' `tags' `search'."
7084 (if (not org-agenda-type)
7085 (error "No Org agenda currently displayed")
7086 (if (memq org-agenda-type types)
7088 (if error
7089 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
7090 nil))))
7092 (defun org-agenda-Quit ()
7093 "Exit the agenda, killing the agenda buffer.
7094 Like `org-agenda-quit', but kill the buffer even when
7095 `org-agenda-sticky' is non-nil."
7096 (interactive)
7097 (org-agenda--quit))
7099 (defun org-agenda-quit ()
7100 "Exit the agenda.
7102 When `org-agenda-sticky' is non-nil, bury the agenda buffer
7103 instead of killing it.
7105 When `org-agenda-restore-windows-after-quit' is non-nil, restore
7106 the pre-agenda window configuration.
7108 When column view is active, exit column view instead of the
7109 agenda."
7110 (interactive)
7111 (org-agenda--quit org-agenda-sticky))
7113 (defun org-agenda--quit (&optional bury)
7114 (if org-agenda-columns-active
7115 (org-columns-quit)
7116 (let ((wconf org-agenda-pre-window-conf)
7117 (buf (current-buffer))
7118 (org-agenda-last-indirect-window
7119 (and (eq org-indirect-buffer-display 'other-window)
7120 org-agenda-last-indirect-buffer
7121 (get-buffer-window org-agenda-last-indirect-buffer))))
7122 (cond
7123 ((eq org-agenda-window-setup 'other-frame)
7124 (delete-frame))
7125 ((and org-agenda-restore-windows-after-quit
7126 wconf)
7127 ;; Maybe restore the pre-agenda window configuration. Reset
7128 ;; `org-agenda-pre-window-conf' before running
7129 ;; `set-window-configuration', which loses the current buffer.
7130 (setq org-agenda-pre-window-conf nil)
7131 (set-window-configuration wconf))
7133 (when org-agenda-last-indirect-window
7134 (delete-window org-agenda-last-indirect-window))
7135 (and (not (eq org-agenda-window-setup 'current-window))
7136 (not (one-window-p))
7137 (delete-window))))
7138 (if bury
7139 ;; Set the agenda buffer as the current buffer instead of
7140 ;; passing it as an argument to `bury-buffer' so that
7141 ;; `bury-buffer' removes it from the window.
7142 (with-current-buffer buf
7143 (bury-buffer))
7144 (kill-buffer buf)
7145 (setq org-agenda-archives-mode nil
7146 org-agenda-buffer nil)))))
7148 (defun org-agenda-exit ()
7149 "Exit the agenda, killing Org buffers loaded by the agenda.
7150 Like `org-agenda-Quit', but kill any buffers that were created by
7151 the agenda. Org buffers visited directly by the user will not be
7152 touched. Also, exit the agenda even if it is in column view."
7153 (interactive)
7154 (when org-agenda-columns-active
7155 (org-columns-quit))
7156 (org-release-buffers org-agenda-new-buffers)
7157 (setq org-agenda-new-buffers nil)
7158 (org-agenda-Quit))
7160 (defun org-agenda-kill-all-agenda-buffers ()
7161 "Kill all buffers in `org-agenda-mode'.
7162 This is used when toggling sticky agendas."
7163 (interactive)
7164 (let (blist)
7165 (dolist (buf (buffer-list))
7166 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7167 (push buf blist)))
7168 (mapc 'kill-buffer blist)))
7170 (defun org-agenda-execute (arg)
7171 "Execute another agenda command, keeping same window.
7172 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7173 in the agenda."
7174 (interactive "P")
7175 (let ((org-agenda-window-setup 'current-window))
7176 (org-agenda arg)))
7178 (defun org-agenda-redo (&optional all)
7179 "Rebuild possibly ALL agenda view(s) in the current buffer."
7180 (interactive "P")
7181 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7182 (cpa (unless (eq all t) current-prefix-arg))
7183 (org-agenda-doing-sticky-redo org-agenda-sticky)
7184 (org-agenda-sticky nil)
7185 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7186 org-agenda-buffer-name))
7187 (org-agenda-keep-modes t)
7188 (tag-filter org-agenda-tag-filter)
7189 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7190 (top-hl-filter org-agenda-top-headline-filter)
7191 (cat-filter org-agenda-category-filter)
7192 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7193 (re-filter org-agenda-regexp-filter)
7194 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7195 (effort-filter org-agenda-effort-filter)
7196 (effort-preset (get 'org-agenda-effort-filter :preset-filter))
7197 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7198 (cols org-agenda-columns-active)
7199 (line (org-current-line))
7200 (window-line (- line (org-current-line (window-start))))
7201 (lprops (get 'org-agenda-redo-command 'org-lprops))
7202 (redo-cmd (get-text-property p 'org-redo-cmd))
7203 (last-args (get-text-property p 'org-last-args))
7204 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7205 (org-agenda-overriding-cmd-arguments
7206 (unless (eq all t)
7207 (cond ((listp last-args)
7208 (cons (or cpa (car last-args)) (cdr last-args)))
7209 ((stringp last-args)
7210 last-args))))
7211 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7212 (put 'org-agenda-tag-filter :preset-filter nil)
7213 (put 'org-agenda-category-filter :preset-filter nil)
7214 (put 'org-agenda-regexp-filter :preset-filter nil)
7215 (put 'org-agenda-effort-filter :preset-filter nil)
7216 (and cols (org-columns-quit))
7217 (message "Rebuilding agenda buffer...")
7218 (if series-redo-cmd
7219 (eval series-redo-cmd)
7220 (org-let lprops redo-cmd))
7221 (setq org-agenda-undo-list nil
7222 org-agenda-pending-undo-list nil
7223 org-agenda-tag-filter tag-filter
7224 org-agenda-category-filter cat-filter
7225 org-agenda-regexp-filter re-filter
7226 org-agenda-effort-filter effort-filter
7227 org-agenda-top-headline-filter top-hl-filter)
7228 (message "Rebuilding agenda buffer...done")
7229 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7230 (put 'org-agenda-category-filter :preset-filter cat-preset)
7231 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7232 (put 'org-agenda-effort-filter :preset-filter effort-preset)
7233 (let ((tag (or tag-filter tag-preset))
7234 (cat (or cat-filter cat-preset))
7235 (effort (or effort-filter effort-preset))
7236 (re (or re-filter re-preset)))
7237 (when tag (org-agenda-filter-apply tag 'tag t))
7238 (when cat (org-agenda-filter-apply cat 'category))
7239 (when effort (org-agenda-filter-apply effort 'effort))
7240 (when re (org-agenda-filter-apply re 'regexp)))
7241 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7242 (and cols (called-interactively-p 'any) (org-agenda-columns))
7243 (org-goto-line line)
7244 (recenter window-line)))
7246 (defvar org-global-tags-completion-table nil)
7247 (defvar org-agenda-filter-form nil)
7248 (defvar org-agenda-filtered-by-category nil)
7250 (defun org-agenda-filter-by-category (strip)
7251 "Filter lines in the agenda buffer that have a specific category.
7252 The category is that of the current line.
7253 Without prefix argument, keep only the lines of that category.
7254 With a prefix argument, exclude the lines of that category.
7256 (interactive "P")
7257 (if (and org-agenda-filtered-by-category
7258 org-agenda-category-filter)
7259 (org-agenda-filter-show-all-cat)
7260 (let ((cat (org-no-properties (org-get-at-eol 'org-category 1))))
7261 (cond
7262 ((and cat strip)
7263 (org-agenda-filter-apply
7264 (push (concat "-" cat) org-agenda-category-filter) 'category))
7265 (cat
7266 (org-agenda-filter-apply
7267 (setq org-agenda-category-filter
7268 (list (concat "+" cat))) 'category))
7269 (t (error "No category at point"))))))
7271 (defun org-find-top-headline (&optional pos)
7272 "Find the topmost parent headline and return it."
7273 (save-excursion
7274 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
7275 (if pos (goto-char pos))
7276 ;; Skip up to the topmost parent
7277 (while (ignore-errors (outline-up-heading 1) t))
7278 (ignore-errors
7279 (nth 4 (org-heading-components))))))
7281 (defvar org-agenda-filtered-by-top-headline nil)
7282 (defun org-agenda-filter-by-top-headline (strip)
7283 "Keep only those lines that are descendants from the same top headline.
7284 The top headline is that of the current line."
7285 (interactive "P")
7286 (if org-agenda-filtered-by-top-headline
7287 (progn
7288 (setq org-agenda-filtered-by-top-headline nil
7289 org-agenda-top-headline-filter nil)
7290 (org-agenda-filter-show-all-top-filter))
7291 (let ((toph (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7292 (if toph (org-agenda-filter-top-headline-apply toph strip)
7293 (error "No top-level headline at point")))))
7295 (defvar org-agenda-regexp-filter nil)
7296 (defun org-agenda-filter-by-regexp (strip)
7297 "Filter agenda entries by a regular expression.
7298 Regexp filters are cumulative.
7299 With no prefix argument, keep entries matching the regexp.
7300 With one prefix argument, filter out entries matching the regexp.
7301 With two prefix arguments, remove the regexp filters."
7302 (interactive "P")
7303 (if (not (equal strip '(16)))
7304 (let ((flt (concat (if (equal strip '(4)) "-" "+")
7305 (read-from-minibuffer
7306 (if (equal strip '(4))
7307 "Filter out entries matching regexp: "
7308 "Narrow to entries matching regexp: ")))))
7309 (push flt org-agenda-regexp-filter)
7310 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
7311 (org-agenda-filter-show-all-re)
7312 (message "Regexp filter removed")))
7314 (defvar org-agenda-effort-filter nil)
7315 (defun org-agenda-filter-by-effort (strip)
7316 "Filter agenda entries by effort.
7317 With no prefix argument, keep entries matching the effort condition.
7318 With one prefix argument, filter out entries matching the condition.
7319 With two prefix arguments, remove the effort filters."
7320 (interactive "P")
7321 (cond
7322 ((member strip '(nil 4))
7323 (let* ((efforts (split-string
7324 (or (cdr (assoc (concat org-effort-property "_ALL")
7325 org-global-properties))
7326 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00")))
7327 ;; XXX: the following handles only up to 10 different
7328 ;; effort values.
7329 (allowed-keys (if (null efforts) nil
7330 (mapcar (lambda (n) (mod n 10)) ;turn 10 into 0
7331 (number-sequence 1 (length efforts)))))
7332 (op nil))
7333 (while (not (memq op '(?< ?> ?=)))
7334 (setq op (read-char-exclusive "Effort operator? (> = or <)")))
7335 ;; Select appropriate duration. Ignore non-digit characters.
7336 (let ((prompt
7337 (apply #'format
7338 (concat "Effort %c "
7339 (mapconcat (lambda (s) (concat "[%d]" s))
7340 efforts
7341 " "))
7342 op allowed-keys))
7343 (eff -1))
7344 (while (not (memq eff allowed-keys))
7345 (message prompt)
7346 (setq eff (- (read-char-exclusive) 48)))
7347 (setq org-agenda-effort-filter
7348 (list (concat (if strip "-" "+")
7349 (char-to-string op)
7350 ;; Numbering is 1 2 3 ... 9 0, but we want
7351 ;; 0 1 2 ... 8 9.
7352 (nth (mod (1- eff) 10) efforts)))))
7353 (org-agenda-filter-apply org-agenda-effort-filter 'effort)))
7354 (t (org-agenda-filter-show-all-effort)
7355 (message "Effort filter removed"))))
7357 (defun org-agenda-filter-remove-all ()
7358 "Remove all filters from the current agenda buffer."
7359 (interactive)
7360 (when org-agenda-tag-filter
7361 (org-agenda-filter-show-all-tag))
7362 (when org-agenda-category-filter
7363 (org-agenda-filter-show-all-cat))
7364 (when org-agenda-regexp-filter
7365 (org-agenda-filter-show-all-re))
7366 (when org-agenda-top-headline-filter
7367 (org-agenda-filter-show-all-top-filter))
7368 (when org-agenda-effort-filter
7369 (org-agenda-filter-show-all-effort))
7370 (org-agenda-finalize))
7372 (defun org-agenda-filter-by-tag (arg &optional char exclude)
7373 "Keep only those lines in the agenda buffer that have a specific tag.
7375 The tag is selected with its fast selection letter, as configured.
7377 With a `\\[universal-argument]' prefix, exclude the agenda search.
7379 With a `\\[universal-argument] \\[universal-argument]' prefix, filter the literal tag, \
7380 i.e. don't
7381 filter on all its group members.
7383 A lisp caller can specify CHAR. EXCLUDE means that the new tag
7384 should be used to exclude the search - the interactive user can
7385 also press `-' or `+' to switch between filtering and excluding."
7386 (interactive "P")
7387 (let* ((alist org-tag-alist-for-agenda)
7388 (tag-chars (mapconcat
7389 (lambda (x) (if (and (not (symbolp (car x)))
7390 (cdr x))
7391 (char-to-string (cdr x))
7392 ""))
7393 org-tag-alist-for-agenda ""))
7394 (valid-char-list (append '(?\t ?\r ?/ ?. ?\s ?q)
7395 (string-to-list tag-chars)))
7396 (exclude (or exclude (equal arg '(4))))
7397 (expand (not (equal arg '(16))))
7398 (inhibit-read-only t)
7399 (current org-agenda-tag-filter)
7400 a n tag)
7401 (unless char
7402 (while (not (memq char valid-char-list))
7403 (message
7404 "%s by tag [%s ], [TAB], %s[/]:off, [+/-]:filter/exclude%s, [q]:quit"
7405 (if exclude "Exclude" "Filter") tag-chars
7406 (if org-agenda-auto-exclude-function "[RET], " "")
7407 (if expand "" ", no grouptag expand"))
7408 (setq char (read-char-exclusive))
7409 ;; Excluding or filtering down
7410 (cond ((eq char ?-) (setq exclude t))
7411 ((eq char ?+) (setq exclude nil)))))
7412 (when (eq char ?\t)
7413 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7414 (setq-local org-global-tags-completion-table
7415 (org-global-tags-completion-table)))
7416 (let ((completion-ignore-case t))
7417 (setq tag (completing-read
7418 "Tag: " org-global-tags-completion-table))))
7419 (cond
7420 ((eq char ?\r)
7421 (org-agenda-filter-show-all-tag)
7422 (when org-agenda-auto-exclude-function
7423 (setq org-agenda-tag-filter nil)
7424 (dolist (tag (org-agenda-get-represented-tags))
7425 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7426 (if modifier
7427 (push modifier org-agenda-tag-filter))))
7428 (if (not (null org-agenda-tag-filter))
7429 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))))
7430 ((eq char ?/)
7431 (org-agenda-filter-show-all-tag)
7432 (when (get 'org-agenda-tag-filter :preset-filter)
7433 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand)))
7434 ((eq char ?.)
7435 (setq org-agenda-tag-filter
7436 (mapcar (lambda(tag) (concat "+" tag))
7437 (org-get-at-bol 'tags)))
7438 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7439 ((eq char ?q)) ;If q, abort (even if there is a q-key for a tag...)
7440 ((or (eq char ?\s)
7441 (setq a (rassoc char alist))
7442 (and tag (setq a (cons tag nil))))
7443 (org-agenda-filter-show-all-tag)
7444 (setq tag (car a))
7445 (setq org-agenda-tag-filter
7446 (cons (concat (if exclude "-" "+") tag)
7447 current))
7448 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7449 (t (error "Invalid tag selection character %c" char)))))
7451 (defun org-agenda-get-represented-tags ()
7452 "Get a list of all tags currently represented in the agenda."
7453 (let (p tags)
7454 (save-excursion
7455 (goto-char (point-min))
7456 (while (setq p (next-single-property-change (point) 'tags))
7457 (goto-char p)
7458 (mapc (lambda (x) (add-to-list 'tags x))
7459 (get-text-property (point) 'tags))))
7460 tags))
7463 (defun org-agenda-filter-make-matcher (filter type &optional expand)
7464 "Create the form that tests a line for agenda filter. Optional
7465 argument EXPAND can be used for the TYPE tag and will expand the
7466 tags in the FILTER if any of the tags in FILTER are grouptags."
7467 (let (f f1)
7468 (cond
7469 ;; Tag filter
7470 ((eq type 'tag)
7471 (setq filter
7472 (delete-dups
7473 (append (get 'org-agenda-tag-filter :preset-filter)
7474 filter)))
7475 (dolist (x filter)
7476 (let ((op (string-to-char x)))
7477 (if expand (setq x (org-agenda-filter-expand-tags (list x) t))
7478 (setq x (list x)))
7479 (setq f1 (org-agenda-filter-make-matcher-tag-exp x op))
7480 (push f1 f))))
7481 ;; Category filter
7482 ((eq type 'category)
7483 (setq filter
7484 (delete-dups
7485 (append (get 'org-agenda-category-filter :preset-filter)
7486 filter)))
7487 (dolist (x filter)
7488 (if (equal "-" (substring x 0 1))
7489 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7490 (setq f1 (list 'equal (substring x 1) 'cat)))
7491 (push f1 f)))
7492 ;; Regexp filter
7493 ((eq type 'regexp)
7494 (setq filter
7495 (delete-dups
7496 (append (get 'org-agenda-regexp-filter :preset-filter)
7497 filter)))
7498 (dolist (x filter)
7499 (if (equal "-" (substring x 0 1))
7500 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7501 (setq f1 (list 'string-match (substring x 1) 'txt)))
7502 (push f1 f)))
7503 ;; Effort filter
7504 ((eq type 'effort)
7505 (setq filter
7506 (delete-dups
7507 (append (get 'org-agenda-effort-filter :preset-filter)
7508 filter)))
7509 (dolist (x filter)
7510 (push (org-agenda-filter-effort-form x) f))))
7511 (cons 'and (nreverse f))))
7513 (defun org-agenda-filter-make-matcher-tag-exp (tags op)
7514 "Return a form associated to tag-expression TAGS.
7515 Build a form testing a line for agenda filter for
7516 tag-expressions. OP is an operator of type CHAR that allows the
7517 function to set the right switches in the returned form."
7518 (let (form)
7519 ;; Any of the expressions can match if OP is +, all must match if
7520 ;; the operator is -.
7521 (dolist (x tags (cons (if (eq op ?-) 'and 'or) form))
7522 (let* ((tag (substring x 1))
7523 (f (cond
7524 ((string= "" tag) '(not tags))
7525 ((and (string-match-p "\\`{" tag) (string-match-p "}\\'" tag))
7526 ;; TAG is a regexp.
7527 (list 'org-match-any-p (substring tag 1 -1) 'tags))
7528 (t (list 'member (downcase tag) 'tags)))))
7529 (push (if (eq op ?-) (list 'not f) f) form)))))
7531 (defun org-agenda-filter-effort-form (e)
7532 "Return the form to compare the effort of the current line with what E says.
7533 E looks like \"+<2:25\"."
7534 (let (op)
7535 (setq e (substring e 1))
7536 (setq op (string-to-char e) e (substring e 1))
7537 (setq op (cond ((equal op ?<) '<=)
7538 ((equal op ?>) '>=)
7539 ((equal op ??) op)
7540 (t '=)))
7541 (list 'org-agenda-compare-effort (list 'quote op)
7542 (org-duration-string-to-minutes e))))
7544 (defun org-agenda-compare-effort (op value)
7545 "Compare the effort of the current line with VALUE, using OP.
7546 If the line does not have an effort defined, return nil."
7547 ;; `effort-minutes' property cannot be extracted directly from
7548 ;; current line but is stored as a property in `txt'.
7549 (let ((effort (get-text-property 0 'effort-minutes (org-get-at-bol 'txt))))
7550 (funcall op
7551 (or effort (if org-sort-agenda-noeffort-is-high 32767 -1))
7552 value)))
7554 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7555 "Expand group tags in FILTER for the agenda.
7556 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7557 (if org-group-tags
7558 (let ((case-fold-search t) rtn)
7559 (mapc
7560 (lambda (f)
7561 (let (f0 dir)
7562 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7563 (setq dir (match-string 1 f) f0 (match-string 2 f))
7564 (setq dir (if no-operator "" "+") f0 f))
7565 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7566 (org-tags-expand f0 t t))
7567 rtn))))
7568 filter)
7569 (reverse rtn))
7570 filter))
7572 (defun org-agenda-filter-apply (filter type &optional expand)
7573 "Set FILTER as the new agenda filter and apply it. Optional
7574 argument EXPAND can be used for the TYPE tag and will expand the
7575 tags in the FILTER if any of the tags in FILTER are grouptags."
7576 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7577 (if org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7578 (let (tags cat txt)
7579 (setq org-agenda-filter-form (org-agenda-filter-make-matcher filter type expand))
7580 ;; Only set `org-agenda-filtered-by-category' to t when a unique
7581 ;; category is used as the filter:
7582 (setq org-agenda-filtered-by-category
7583 (and (eq type 'category)
7584 (not (equal (substring (car filter) 0 1) "-"))))
7585 (org-agenda-set-mode-name)
7586 (save-excursion
7587 (goto-char (point-min))
7588 (while (not (eobp))
7589 (if (org-get-at-bol 'org-marker)
7590 (progn
7591 (setq tags (org-get-at-bol 'tags)
7592 cat (org-get-at-eol 'org-category 1)
7593 txt (org-get-at-bol 'txt))
7594 (if (not (eval org-agenda-filter-form))
7595 (org-agenda-filter-hide-line type))
7596 (beginning-of-line 2))
7597 (beginning-of-line 2))))
7598 (if (get-char-property (point) 'invisible)
7599 (ignore-errors (org-agenda-previous-line)))))
7601 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7602 "Filter by top headline HL."
7603 (org-agenda-set-mode-name)
7604 (save-excursion
7605 (goto-char (point-min))
7606 (while (not (eobp))
7607 (let* ((pos (org-get-at-bol 'org-hd-marker))
7608 (tophl (and pos (org-find-top-headline pos))))
7609 (if (and tophl (funcall (if negative 'identity 'not)
7610 (string= hl tophl)))
7611 (org-agenda-filter-hide-line 'top-headline)))
7612 (beginning-of-line 2)))
7613 (if (get-char-property (point) 'invisible)
7614 (org-agenda-previous-line))
7615 (setq org-agenda-top-headline-filter hl
7616 org-agenda-filtered-by-top-headline t))
7618 (defun org-agenda-filter-hide-line (type)
7619 "Hide lines with TYPE in the agenda buffer."
7620 (let* ((b (max (point-min) (1- (point-at-bol))))
7621 (e (point-at-eol)))
7622 (let ((inhibit-read-only t))
7623 (add-text-properties
7624 b e `(invisible org-filtered org-filter-type ,type)))))
7626 (defun org-agenda-remove-filter (type)
7627 (interactive)
7628 "Remove filter of type TYPE from the agenda buffer."
7629 (save-excursion
7630 (goto-char (point-min))
7631 (let ((inhibit-read-only t) pos)
7632 (while (setq pos (text-property-any (point) (point-max) 'org-filter-type type))
7633 (goto-char pos)
7634 (remove-text-properties
7635 (point) (next-single-property-change (point) 'org-filter-type)
7636 `(invisible org-filtered org-filter-type ,type))))
7637 (set (intern (format "org-agenda-%s-filter" (intern-soft type))) nil)
7638 (setq org-agenda-filter-form nil)
7639 (org-agenda-set-mode-name)
7640 (org-agenda-finalize)))
7642 (defun org-agenda-filter-show-all-tag nil
7643 (org-agenda-remove-filter 'tag))
7644 (defun org-agenda-filter-show-all-re nil
7645 (org-agenda-remove-filter 'regexp))
7646 (defun org-agenda-filter-show-all-effort nil
7647 (org-agenda-remove-filter 'effort))
7648 (defun org-agenda-filter-show-all-cat nil
7649 (org-agenda-remove-filter 'category))
7650 (defun org-agenda-filter-show-all-top-filter nil
7651 (org-agenda-remove-filter 'top-headline))
7653 (defun org-agenda-manipulate-query-add ()
7654 "Manipulate the query by adding a search term with positive selection.
7655 Positive selection means the term must be matched for selection of an entry."
7656 (interactive)
7657 (org-agenda-manipulate-query ?\[))
7658 (defun org-agenda-manipulate-query-subtract ()
7659 "Manipulate the query by adding a search term with negative selection.
7660 Negative selection means term must not be matched for selection of an entry."
7661 (interactive)
7662 (org-agenda-manipulate-query ?\]))
7663 (defun org-agenda-manipulate-query-add-re ()
7664 "Manipulate the query by adding a search regexp with positive selection.
7665 Positive selection means the regexp must match for selection of an entry."
7666 (interactive)
7667 (org-agenda-manipulate-query ?\{))
7668 (defun org-agenda-manipulate-query-subtract-re ()
7669 "Manipulate the query by adding a search regexp with negative selection.
7670 Negative selection means regexp must not match for selection of an entry."
7671 (interactive)
7672 (org-agenda-manipulate-query ?\}))
7673 (defun org-agenda-manipulate-query (char)
7674 (cond
7675 ((memq org-agenda-type '(timeline agenda))
7676 (let ((org-agenda-include-inactive-timestamps t))
7677 (org-agenda-redo))
7678 (message "Display now includes inactive timestamps as well"))
7679 ((eq org-agenda-type 'search)
7680 (org-add-to-string
7681 'org-agenda-query-string
7682 (if org-agenda-last-search-view-search-was-boolean
7683 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7684 (?\{ . " +{}") (?\} . " -{}"))))
7685 " "))
7686 (setq org-agenda-redo-command
7687 (list 'org-search-view
7688 (car (get-text-property (min (1- (point-max)) (point))
7689 'org-last-args))
7690 org-agenda-query-string
7691 (+ (length org-agenda-query-string)
7692 (if (member char '(?\{ ?\})) 0 1))))
7693 (set-register org-agenda-query-register org-agenda-query-string)
7694 (let ((org-agenda-overriding-arguments
7695 (cdr org-agenda-redo-command)))
7696 (org-agenda-redo)))
7697 (t (error "Cannot manipulate query for %s-type agenda buffers"
7698 org-agenda-type))))
7700 (defun org-add-to-string (var string)
7701 (set var (concat (symbol-value var) string)))
7703 (defun org-agenda-goto-date (span)
7704 "Jump to DATE in agenda."
7705 (interactive "P")
7706 (let* ((org-read-date-prefer-future
7707 (eval org-agenda-jump-prefer-future))
7708 (date (org-read-date))
7709 (day (time-to-days (org-time-string-to-time date)))
7710 (org-agenda-sticky-orig org-agenda-sticky)
7711 (org-agenda-buffer-tmp-name (buffer-name))
7712 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7713 (0-arg (or current-prefix-arg (car args)))
7714 (2-arg (nth 2 args))
7715 (with-hour-p (nth 4 org-agenda-redo-command))
7716 (newcmd (list 'org-agenda-list 0-arg date
7717 (org-agenda-span-to-ndays
7718 2-arg (org-time-string-to-absolute date))
7719 with-hour-p))
7720 (newargs (cdr newcmd))
7721 (inhibit-read-only t)
7722 org-agenda-sticky)
7723 (if (not (org-agenda-check-type t 'agenda))
7724 (error "Not available in non-agenda views")
7725 (add-text-properties (point-min) (point-max)
7726 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7727 (org-agenda-redo)
7728 (goto-char (point-min))
7729 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7730 (save-excursion (move-beginning-of-line 2) (eobp))))
7731 (move-beginning-of-line 2))
7732 (setq org-agenda-sticky org-agenda-sticky-orig
7733 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7735 (defun org-agenda-goto-today ()
7736 "Go to today."
7737 (interactive)
7738 (org-agenda-check-type t 'timeline 'agenda)
7739 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7740 (curspan (nth 2 args))
7741 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7742 (cond
7743 (tdpos (goto-char tdpos))
7744 ((eq org-agenda-type 'agenda)
7745 (let* ((sd (org-agenda-compute-starting-span
7746 (org-today) (or curspan org-agenda-span)))
7747 (org-agenda-overriding-arguments args))
7748 (setf (nth 1 org-agenda-overriding-arguments) sd)
7749 (org-agenda-redo)
7750 (org-agenda-find-same-or-today-or-agenda)))
7751 (t (error "Cannot find today")))))
7753 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7754 (goto-char
7755 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7756 (text-property-any (point-min) (point-max) 'org-today t)
7757 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7758 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7759 (org-agenda-backward-block))
7760 (point-min))))
7762 (defun org-agenda-backward-block ()
7763 "Move backward by one agenda block."
7764 (interactive)
7765 (org-agenda-forward-block 'backward))
7767 (defun org-agenda-forward-block (&optional backward)
7768 "Move forward by one agenda block.
7769 When optional argument BACKWARD is set, go backward"
7770 (interactive)
7771 (cond ((not (derived-mode-p 'org-agenda-mode))
7772 (user-error
7773 "Cannot execute this command outside of org-agenda-mode buffers"))
7774 ((looking-at (if backward "\\`" "\\'"))
7775 (message "Already at the %s block" (if backward "first" "last")))
7776 (t (let ((pos (prog1 (point)
7777 (ignore-errors (if backward (backward-char 1)
7778 (move-end-of-line 1)))))
7779 (f (if backward
7780 'previous-single-property-change
7781 'next-single-property-change))
7782 moved dest)
7783 (while (and (setq dest (funcall
7784 f (point) 'org-agenda-structural-header))
7785 (not (get-text-property
7786 (point) 'org-agenda-structural-header)))
7787 (setq moved t)
7788 (goto-char dest))
7789 (if moved (move-beginning-of-line 1)
7790 (goto-char (if backward (point-min) (point-max)))
7791 (move-beginning-of-line 1)
7792 (message "No %s block" (if backward "previous" "further")))))))
7794 (defun org-agenda-later (arg)
7795 "Go forward in time by the current span.
7796 With prefix ARG, go forward that many times the current span."
7797 (interactive "p")
7798 (org-agenda-check-type t 'agenda)
7799 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7800 (span (or (nth 2 args) org-agenda-current-span))
7801 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7802 (greg (calendar-gregorian-from-absolute sd))
7803 (cnt (org-get-at-bol 'org-day-cnt))
7804 greg2)
7805 (cond
7806 ((numberp span)
7807 (setq sd (+ (* span arg) sd)))
7808 ((eq span 'day)
7809 (setq sd (+ arg sd)))
7810 ((eq span 'week)
7811 (setq sd (+ (* 7 arg) sd)))
7812 ((eq span 'fortnight)
7813 (setq sd (+ (* 14 arg) sd)))
7814 ((eq span 'month)
7815 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7816 sd (calendar-absolute-from-gregorian greg2))
7817 (setcar greg2 (1+ (car greg2))))
7818 ((eq span 'year)
7819 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7820 sd (calendar-absolute-from-gregorian greg2))
7821 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7823 (setq sd (+ (* span arg) sd))))
7824 (let ((org-agenda-overriding-cmd
7825 ;; `cmd' may have been set by `org-agenda-run-series' which
7826 ;; uses `org-agenda-overriding-cmd' to decide whether
7827 ;; overriding is allowed for `cmd'
7828 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7829 (org-agenda-overriding-arguments
7830 (list (car args) sd span)))
7831 (org-agenda-redo)
7832 (org-agenda-find-same-or-today-or-agenda cnt))))
7834 (defun org-agenda-earlier (arg)
7835 "Go backward in time by the current span.
7836 With prefix ARG, go backward that many times the current span."
7837 (interactive "p")
7838 (org-agenda-later (- arg)))
7840 (defun org-agenda-view-mode-dispatch ()
7841 "Call one of the view mode commands."
7842 (interactive)
7843 (message "View: [d]ay [w]eek for[t]night [m]onth [y]ear [SPC]reset [q]uit/abort
7844 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7845 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7846 (pcase (read-char-exclusive)
7847 (?\ (call-interactively 'org-agenda-reset-view))
7848 (?d (call-interactively 'org-agenda-day-view))
7849 (?w (call-interactively 'org-agenda-week-view))
7850 (?t (call-interactively 'org-agenda-fortnight-view))
7851 (?m (call-interactively 'org-agenda-month-view))
7852 (?y (call-interactively 'org-agenda-year-view))
7853 (?l (call-interactively 'org-agenda-log-mode))
7854 (?L (org-agenda-log-mode '(4)))
7855 (?c (org-agenda-log-mode 'clockcheck))
7856 ((or ?F ?f) (call-interactively 'org-agenda-follow-mode))
7857 (?a (call-interactively 'org-agenda-archives-mode))
7858 (?A (org-agenda-archives-mode 'files))
7859 ((or ?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7860 ((or ?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7861 (?G (call-interactively 'org-agenda-toggle-time-grid))
7862 (?D (call-interactively 'org-agenda-toggle-diary))
7863 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7864 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7865 (org-agenda-check-type t 'timeline 'agenda)
7866 (org-agenda-redo))
7867 (message "Display now includes inactive timestamps as well"))
7868 (?q (message "Abort"))
7869 (key (user-error "Invalid key: %s" key))))
7871 (defun org-agenda-reset-view ()
7872 "Switch to default view for agenda."
7873 (interactive)
7874 (org-agenda-change-time-span org-agenda-span))
7876 (defun org-agenda-day-view (&optional day-of-month)
7877 "Switch to daily view for agenda.
7878 With argument DAY-OF-MONTH, switch to that day of the month."
7879 (interactive "P")
7880 (org-agenda-change-time-span 'day day-of-month))
7882 (defun org-agenda-week-view (&optional iso-week)
7883 "Switch to weekly view for agenda.
7884 With argument ISO-WEEK, switch to the corresponding ISO week.
7885 If ISO-WEEK has more then 2 digits, only the last two encode
7886 the week. Any digits before this encode a year. So 200712
7887 means week 12 of year 2007. Years ranging from 70 years ago
7888 to 30 years in the future can also be written as 2-digit years."
7889 (interactive "P")
7890 (org-agenda-change-time-span 'week iso-week))
7892 (defun org-agenda-fortnight-view (&optional iso-week)
7893 "Switch to fortnightly view for agenda.
7894 With argument ISO-WEEK, switch to the corresponding ISO week.
7895 If ISO-WEEK has more then 2 digits, only the last two encode
7896 the week. Any digits before this encode a year. So 200712
7897 means week 12 of year 2007. Years ranging from 70 years ago
7898 to 30 years in the future can also be written as 2-digit years."
7899 (interactive "P")
7900 (org-agenda-change-time-span 'fortnight iso-week))
7902 (defun org-agenda-month-view (&optional month)
7903 "Switch to monthly view for agenda.
7904 With argument MONTH, switch to that month. If MONTH has more
7905 then 2 digits, only the last two encode the month. Any digits
7906 before this encode a year. So 200712 means December year 2007.
7907 Years ranging from 70 years ago to 30 years in the future can
7908 also be written as 2-digit years."
7909 (interactive "P")
7910 (org-agenda-change-time-span 'month month))
7912 (defun org-agenda-year-view (&optional year)
7913 "Switch to yearly view for agenda.
7914 With argument YEAR, switch to that year. Years ranging from 70
7915 years ago to 30 years in the future can also be written as
7916 2-digit years."
7917 (interactive "P")
7918 (when year
7919 (setq year (org-small-year-to-year year)))
7920 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
7921 (org-agenda-change-time-span 'year year)
7922 (error "Abort")))
7924 (defun org-agenda-change-time-span (span &optional n)
7925 "Change the agenda view to SPAN.
7926 SPAN may be `day', `week', `fortnight', `month', `year'."
7927 (org-agenda-check-type t 'agenda)
7928 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7929 (curspan (nth 2 args)))
7930 (if (and (not n) (equal curspan span))
7931 (error "Viewing span is already \"%s\"" span))
7932 (let* ((sd (or (org-get-at-bol 'day)
7933 (nth 1 args)
7934 org-starting-day))
7935 (sd (org-agenda-compute-starting-span sd span n))
7936 (org-agenda-overriding-cmd
7937 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7938 (org-agenda-overriding-arguments
7939 (list (car args) sd span)))
7940 (org-agenda-redo)
7941 (org-agenda-find-same-or-today-or-agenda))
7942 (org-agenda-set-mode-name)
7943 (message "Switched to %s view" span)))
7945 (defun org-agenda-compute-starting-span (sd span &optional n)
7946 "Compute starting date for agenda.
7947 SPAN may be `day', `week', `fortnight', `month', `year'. The return value
7948 is a cons cell with the starting date and the number of days,
7949 so that the date SD will be in that range."
7950 (let* ((greg (calendar-gregorian-from-absolute sd))
7951 (dg (nth 1 greg))
7952 (mg (car greg))
7953 (yg (nth 2 greg)))
7954 (cond
7955 ((eq span 'day)
7956 (when n
7957 (setq sd (+ (calendar-absolute-from-gregorian
7958 (list mg 1 yg))
7959 n -1))))
7960 ((or (eq span 'week) (eq span 'fortnight))
7961 (let* ((nt (calendar-day-of-week
7962 (calendar-gregorian-from-absolute sd)))
7963 (d (if org-agenda-start-on-weekday
7964 (- nt org-agenda-start-on-weekday)
7967 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
7968 (when n
7969 (require 'cal-iso)
7970 (when (> n 99)
7971 (setq y1 (org-small-year-to-year (/ n 100))
7972 n (mod n 100)))
7973 (setq sd
7974 (calendar-iso-to-absolute
7975 (list n 1
7976 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
7977 ((eq span 'month)
7978 (let (y1)
7979 (when (and n (> n 99))
7980 (setq y1 (org-small-year-to-year (/ n 100))
7981 n (mod n 100)))
7982 (setq sd (calendar-absolute-from-gregorian
7983 (list (or n mg) 1 (or y1 yg))))))
7984 ((eq span 'year)
7985 (setq sd (calendar-absolute-from-gregorian
7986 (list 1 1 (or n yg))))))
7987 sd))
7989 (defun org-agenda-next-date-line (&optional arg)
7990 "Jump to the next line indicating a date in agenda buffer."
7991 (interactive "p")
7992 (org-agenda-check-type t 'agenda 'timeline)
7993 (beginning-of-line 1)
7994 ;; This does not work if user makes date format that starts with a blank
7995 (if (looking-at "^\\S-") (forward-char 1))
7996 (if (not (re-search-forward "^\\S-" nil t arg))
7997 (progn
7998 (backward-char 1)
7999 (error "No next date after this line in this buffer")))
8000 (goto-char (match-beginning 0)))
8002 (defun org-agenda-previous-date-line (&optional arg)
8003 "Jump to the previous line indicating a date in agenda buffer."
8004 (interactive "p")
8005 (org-agenda-check-type t 'agenda 'timeline)
8006 (beginning-of-line 1)
8007 (if (not (re-search-backward "^\\S-" nil t arg))
8008 (error "No previous date before this line in this buffer")))
8010 ;; Initialize the highlight
8011 (defvar org-hl (make-overlay 1 1))
8012 (overlay-put org-hl 'face 'highlight)
8014 (defun org-highlight (begin end &optional buffer)
8015 "Highlight a region with overlay."
8016 (move-overlay org-hl begin end (or buffer (current-buffer))))
8018 (defun org-unhighlight ()
8019 "Detach overlay INDEX."
8020 (delete-overlay org-hl))
8022 (defun org-unhighlight-once ()
8023 "Remove the highlight from its position, and this function from the hook."
8024 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8025 (org-unhighlight))
8027 (defvar org-agenda-pre-follow-window-conf nil)
8028 (defun org-agenda-follow-mode ()
8029 "Toggle follow mode in an agenda buffer."
8030 (interactive)
8031 (unless org-agenda-follow-mode
8032 (setq org-agenda-pre-follow-window-conf
8033 (current-window-configuration)))
8034 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8035 (unless org-agenda-follow-mode
8036 (set-window-configuration org-agenda-pre-follow-window-conf))
8037 (org-agenda-set-mode-name)
8038 (org-agenda-do-context-action)
8039 (message "Follow mode is %s"
8040 (if org-agenda-follow-mode "on" "off")))
8042 (defun org-agenda-entry-text-mode (&optional arg)
8043 "Toggle entry text mode in an agenda buffer."
8044 (interactive "P")
8045 (if (or org-agenda-tag-filter
8046 org-agenda-category-filter
8047 org-agenda-regexp-filter
8048 org-agenda-top-headline-filter)
8049 (user-error "Can't show entry text in filtered views")
8050 (setq org-agenda-entry-text-mode (or (integerp arg)
8051 (not org-agenda-entry-text-mode)))
8052 (org-agenda-entry-text-hide)
8053 (and org-agenda-entry-text-mode
8054 (let ((org-agenda-entry-text-maxlines
8055 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8056 (org-agenda-entry-text-show)))
8057 (org-agenda-set-mode-name)
8058 (message "Entry text mode is %s%s"
8059 (if org-agenda-entry-text-mode "on" "off")
8060 (if (not org-agenda-entry-text-mode) ""
8061 (format " (maximum number of lines is %d)"
8062 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8064 (defun org-agenda-clockreport-mode ()
8065 "Toggle clocktable mode in an agenda buffer."
8066 (interactive)
8067 (org-agenda-check-type t 'agenda)
8068 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
8069 (setq org-agenda-start-with-clockreport-mode org-agenda-clockreport-mode)
8070 (org-agenda-set-mode-name)
8071 (org-agenda-redo)
8072 (message "Clocktable mode is %s"
8073 (if org-agenda-clockreport-mode "on" "off")))
8075 (defun org-agenda-log-mode (&optional special)
8076 "Toggle log mode in an agenda buffer.
8078 With argument SPECIAL, show all possible log items, not only the ones
8079 configured in `org-agenda-log-mode-items'.
8081 With a `\\[universal-argument] \\[universal-argument]' prefix, show *only* \
8082 log items, nothing else."
8083 (interactive "P")
8084 (org-agenda-check-type t 'agenda 'timeline)
8085 (setq org-agenda-show-log
8086 (cond
8087 ((equal special '(16)) 'only)
8088 ((eq special 'clockcheck)
8089 (if (eq org-agenda-show-log 'clockcheck)
8090 nil 'clockcheck))
8091 (special '(closed clock state))
8092 (t (not org-agenda-show-log))))
8093 (setq org-agenda-start-with-log-mode org-agenda-show-log)
8094 (org-agenda-set-mode-name)
8095 (org-agenda-redo)
8096 (message "Log mode is %s" (if org-agenda-show-log "on" "off")))
8098 (defun org-agenda-archives-mode (&optional with-files)
8099 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8100 When called with a prefix argument, include all archive files as well."
8101 (interactive "P")
8102 (setq org-agenda-archives-mode
8103 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8104 (org-agenda-set-mode-name)
8105 (org-agenda-redo)
8106 (message
8107 "%s"
8108 (cond
8109 ((eq org-agenda-archives-mode nil)
8110 "No archives are included")
8111 ((eq org-agenda-archives-mode 'trees)
8112 (format "Trees with :%s: tag are included" org-archive-tag))
8113 ((eq org-agenda-archives-mode t)
8114 (format "Trees with :%s: tag and all active archive files are included"
8115 org-archive-tag)))))
8117 (defun org-agenda-toggle-diary ()
8118 "Toggle diary inclusion in an agenda buffer."
8119 (interactive)
8120 (org-agenda-check-type t 'agenda)
8121 (setq org-agenda-include-diary (not org-agenda-include-diary))
8122 (org-agenda-redo)
8123 (org-agenda-set-mode-name)
8124 (message "Diary inclusion turned %s"
8125 (if org-agenda-include-diary "on" "off")))
8127 (defun org-agenda-toggle-deadlines ()
8128 "Toggle inclusion of entries with a deadline in an agenda buffer."
8129 (interactive)
8130 (org-agenda-check-type t 'agenda)
8131 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8132 (org-agenda-redo)
8133 (org-agenda-set-mode-name)
8134 (message "Deadlines inclusion turned %s"
8135 (if org-agenda-include-deadlines "on" "off")))
8137 (defun org-agenda-toggle-time-grid ()
8138 "Toggle time grid in an agenda buffer."
8139 (interactive)
8140 (org-agenda-check-type t 'agenda)
8141 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8142 (org-agenda-redo)
8143 (org-agenda-set-mode-name)
8144 (message "Time-grid turned %s"
8145 (if org-agenda-use-time-grid "on" "off")))
8147 (defun org-agenda-set-mode-name ()
8148 "Set the mode name to indicate all the small mode settings."
8149 (setq mode-name
8150 (list "Org-Agenda"
8151 (if (get 'org-agenda-files 'org-restrict) " []" "")
8153 '(:eval (org-agenda-span-name org-agenda-current-span))
8154 (if org-agenda-follow-mode " Follow" "")
8155 (if org-agenda-entry-text-mode " ETxt" "")
8156 (if org-agenda-include-diary " Diary" "")
8157 (if org-agenda-include-deadlines " Ddl" "")
8158 (if org-agenda-use-time-grid " Grid" "")
8159 (if (and (boundp 'org-habit-show-habits)
8160 org-habit-show-habits) " Habit" "")
8161 (cond
8162 ((consp org-agenda-show-log) " LogAll")
8163 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8164 (org-agenda-show-log " Log")
8165 (t ""))
8166 (if (or org-agenda-category-filter
8167 (get 'org-agenda-category-filter :preset-filter))
8168 '(:eval (propertize
8169 (concat " <"
8170 (mapconcat
8171 'identity
8172 (append
8173 (get 'org-agenda-category-filter :preset-filter)
8174 org-agenda-category-filter)
8176 ">")
8177 'face 'org-agenda-filter-category
8178 'help-echo "Category used in filtering")) "")
8179 (if (or org-agenda-tag-filter
8180 (get 'org-agenda-tag-filter :preset-filter))
8181 '(:eval (propertize
8182 (concat " {"
8183 (mapconcat
8184 'identity
8185 (append
8186 (get 'org-agenda-tag-filter :preset-filter)
8187 org-agenda-tag-filter)
8189 "}")
8190 'face 'org-agenda-filter-tags
8191 'help-echo "Tags used in filtering")) "")
8192 (if (or org-agenda-effort-filter
8193 (get 'org-agenda-effort-filter :preset-filter))
8194 '(:eval (propertize
8195 (concat " {"
8196 (mapconcat
8197 'identity
8198 (append
8199 (get 'org-agenda-effort-filter :preset-filter)
8200 org-agenda-effort-filter)
8202 "}")
8203 'face 'org-agenda-filter-effort
8204 'help-echo "Effort conditions used in filtering")) "")
8205 (if (or org-agenda-regexp-filter
8206 (get 'org-agenda-regexp-filter :preset-filter))
8207 '(:eval (propertize
8208 (concat " ["
8209 (mapconcat
8210 'identity
8211 (append
8212 (get 'org-agenda-regexp-filter :preset-filter)
8213 org-agenda-regexp-filter)
8215 "]")
8216 'face 'org-agenda-filter-regexp
8217 'help-echo "Regexp used in filtering")) "")
8218 (if org-agenda-archives-mode
8219 (if (eq org-agenda-archives-mode t)
8220 " Archives"
8221 (format " :%s:" org-archive-tag))
8223 (if org-agenda-clockreport-mode " Clock" "")))
8224 (force-mode-line-update))
8226 (defun org-agenda-update-agenda-type ()
8227 "Update the agenda type after each command."
8228 (setq org-agenda-type
8229 (or (get-text-property (point) 'org-agenda-type)
8230 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8232 (defun org-agenda-next-line ()
8233 "Move cursor to the next line, and show if follow mode is active."
8234 (interactive)
8235 (call-interactively 'next-line)
8236 (org-agenda-do-context-action))
8238 (defun org-agenda-previous-line ()
8239 "Move cursor to the previous line, and show if follow-mode is active."
8240 (interactive)
8241 (call-interactively 'previous-line)
8242 (org-agenda-do-context-action))
8244 (defun org-agenda-next-item (n)
8245 "Move cursor to next agenda item."
8246 (interactive "p")
8247 (let ((col (current-column)))
8248 (dotimes (c n)
8249 (when (next-single-property-change (point-at-eol) 'org-marker)
8250 (move-end-of-line 1)
8251 (goto-char (next-single-property-change (point) 'org-marker))))
8252 (org-move-to-column col))
8253 (org-agenda-do-context-action))
8255 (defun org-agenda-previous-item (n)
8256 "Move cursor to next agenda item."
8257 (interactive "p")
8258 (dotimes (c n)
8259 (let ((col (current-column))
8260 (goto (save-excursion
8261 (move-end-of-line 0)
8262 (previous-single-property-change (point) 'org-marker))))
8263 (if goto (goto-char goto))
8264 (org-move-to-column col)))
8265 (org-agenda-do-context-action))
8267 (defun org-agenda-do-context-action ()
8268 "Show outline path and, maybe, follow mode window."
8269 (let ((m (org-get-at-bol 'org-marker)))
8270 (when (and (markerp m) (marker-buffer m))
8271 (and org-agenda-follow-mode
8272 (if org-agenda-follow-indirect
8273 (org-agenda-tree-to-indirect-buffer nil)
8274 (org-agenda-show)))
8275 (and org-agenda-show-outline-path
8276 (org-with-point-at m (org-display-outline-path t))))))
8278 (defun org-agenda-show-tags ()
8279 "Show the tags applicable to the current item."
8280 (interactive)
8281 (let* ((tags (org-get-at-bol 'tags)))
8282 (if tags
8283 (message "Tags are :%s:"
8284 (org-no-properties (mapconcat 'identity tags ":")))
8285 (message "No tags associated with this line"))))
8287 (defun org-agenda-goto (&optional highlight)
8288 "Go to the entry at point in the corresponding Org file."
8289 (interactive)
8290 (let* ((marker (or (org-get-at-bol 'org-marker)
8291 (org-agenda-error)))
8292 (buffer (marker-buffer marker))
8293 (pos (marker-position marker)))
8294 (switch-to-buffer-other-window buffer)
8295 (widen)
8296 (push-mark)
8297 (goto-char pos)
8298 (when (derived-mode-p 'org-mode)
8299 (org-show-context 'agenda)
8300 (recenter (/ (window-height) 2))
8301 (org-back-to-heading t)
8302 (let ((case-fold-search nil))
8303 (when (re-search-forward org-complex-heading-regexp nil t)
8304 (goto-char (match-beginning 4)))))
8305 (run-hooks 'org-agenda-after-show-hook)
8306 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8308 (defvar org-agenda-after-show-hook nil
8309 "Normal hook run after an item has been shown from the agenda.
8310 Point is in the buffer where the item originated.")
8312 (defun org-agenda-kill ()
8313 "Kill the entry or subtree belonging to the current agenda entry."
8314 (interactive)
8315 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8316 (let* ((bufname-orig (buffer-name))
8317 (marker (or (org-get-at-bol 'org-marker)
8318 (org-agenda-error)))
8319 (buffer (marker-buffer marker))
8320 (pos (marker-position marker))
8321 (type (org-get-at-bol 'type))
8322 dbeg dend (n 0) conf)
8323 (org-with-remote-undo buffer
8324 (with-current-buffer buffer
8325 (save-excursion
8326 (goto-char pos)
8327 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8328 (setq dbeg (progn (org-back-to-heading t) (point))
8329 dend (org-end-of-subtree t t))
8330 (setq dbeg (point-at-bol)
8331 dend (min (point-max) (1+ (point-at-eol)))))
8332 (goto-char dbeg)
8333 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8334 (setq conf (or (eq t org-agenda-confirm-kill)
8335 (and (numberp org-agenda-confirm-kill)
8336 (> n org-agenda-confirm-kill))))
8337 (and conf
8338 (not (y-or-n-p
8339 (format "Delete entry with %d lines in buffer \"%s\"? "
8340 n (buffer-name buffer))))
8341 (error "Abort"))
8342 (let ((org-agenda-buffer-name bufname-orig))
8343 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8344 (with-current-buffer buffer (delete-region dbeg dend))
8345 (message "Agenda item and source killed"))))
8347 (defvar org-archive-default-command) ; defined in org-archive.el
8348 (defun org-agenda-archive-default ()
8349 "Archive the entry or subtree belonging to the current agenda entry."
8350 (interactive)
8351 (require 'org-archive)
8352 (org-agenda-archive-with org-archive-default-command))
8354 (defun org-agenda-archive-default-with-confirmation ()
8355 "Archive the entry or subtree belonging to the current agenda entry."
8356 (interactive)
8357 (require 'org-archive)
8358 (org-agenda-archive-with org-archive-default-command 'confirm))
8360 (defun org-agenda-archive ()
8361 "Archive the entry or subtree belonging to the current agenda entry."
8362 (interactive)
8363 (org-agenda-archive-with 'org-archive-subtree))
8365 (defun org-agenda-archive-to-archive-sibling ()
8366 "Move the entry to the archive sibling."
8367 (interactive)
8368 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8370 (defun org-agenda-archive-with (cmd &optional confirm)
8371 "Move the entry to the archive sibling."
8372 (interactive)
8373 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8374 (let* ((bufname-orig (buffer-name))
8375 (marker (or (org-get-at-bol 'org-marker)
8376 (org-agenda-error)))
8377 (buffer (marker-buffer marker))
8378 (pos (marker-position marker)))
8379 (org-with-remote-undo buffer
8380 (with-current-buffer buffer
8381 (if (derived-mode-p 'org-mode)
8382 (if (and confirm
8383 (not (y-or-n-p "Archive this subtree or entry? ")))
8384 (error "Abort")
8385 (save-window-excursion
8386 (goto-char pos)
8387 (let ((org-agenda-buffer-name bufname-orig))
8388 (org-remove-subtree-entries-from-agenda))
8389 (org-back-to-heading t)
8390 (funcall cmd)))
8391 (error "Archiving works only in Org files"))))))
8393 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8394 "Remove all lines in the agenda that correspond to a given subtree.
8395 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8396 If this information is not given, the function uses the tree at point."
8397 (let ((buf (or buf (current-buffer))) m p)
8398 (save-excursion
8399 (unless (and beg end)
8400 (org-back-to-heading t)
8401 (setq beg (point))
8402 (org-end-of-subtree t)
8403 (setq end (point)))
8404 (set-buffer (get-buffer org-agenda-buffer-name))
8405 (save-excursion
8406 (goto-char (point-max))
8407 (beginning-of-line 1)
8408 (while (not (bobp))
8409 (when (and (setq m (org-get-at-bol 'org-marker))
8410 (equal buf (marker-buffer m))
8411 (setq p (marker-position m))
8412 (>= p beg)
8413 (< p end))
8414 (let ((inhibit-read-only t))
8415 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8416 (beginning-of-line 0))))))
8418 (defun org-agenda-refile (&optional goto rfloc no-update)
8419 "Refile the item at point.
8421 When called with `\\[universal-argument] \\[universal-argument]', \
8422 go to the location of the last
8423 refiled item.
8425 When called with `\\[universal-argument] \\[universal-argument] \
8426 \\[universal-argument]' prefix or when GOTO is 0, clear
8427 the refile cache.
8429 RFLOC can be a refile location obtained in a different way.
8431 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8432 (interactive "P")
8433 (cond
8434 ((member goto '(0 (64)))
8435 (org-refile-cache-clear))
8436 ((equal goto '(16))
8437 (org-refile-goto-last-stored))
8439 (let* ((buffer-orig (buffer-name))
8440 (marker (or (org-get-at-bol 'org-hd-marker)
8441 (org-agenda-error)))
8442 (buffer (marker-buffer marker))
8443 (pos (marker-position marker))
8444 (rfloc (or rfloc
8445 (org-refile-get-location
8446 (if goto "Goto" "Refile to") buffer
8447 org-refile-allow-creating-parent-nodes))))
8448 (with-current-buffer buffer
8449 (org-with-wide-buffer
8450 (goto-char marker)
8451 (let ((org-agenda-buffer-name buffer-orig))
8452 (org-remove-subtree-entries-from-agenda))
8453 (org-refile goto buffer rfloc))))
8454 (unless no-update (org-agenda-redo)))))
8456 (defun org-agenda-open-link (&optional arg)
8457 "Open the link(s) in the current entry, if any.
8458 This looks for a link in the displayed line in the agenda.
8459 It also looks at the text of the entry itself."
8460 (interactive "P")
8461 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8462 (org-get-at-bol 'org-marker)))
8463 (buffer (and marker (marker-buffer marker)))
8464 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8465 (lkall (and buffer (org-offer-links-in-entry
8466 buffer marker arg prefix)))
8467 (lk0 (car lkall))
8468 (lk (if (stringp lk0) (list lk0) lk0))
8469 (lkend (cdr lkall))
8470 trg)
8471 (cond
8472 ((and buffer lk)
8473 (mapcar (lambda(l)
8474 (with-current-buffer buffer
8475 (setq trg (and (string-match org-bracket-link-regexp l)
8476 (match-string 1 l)))
8477 (if (or (not trg) (string-match org-any-link-re trg))
8478 (org-with-wide-buffer
8479 (goto-char marker)
8480 (when (search-forward l nil lkend)
8481 (goto-char (match-beginning 0))
8482 (org-open-at-point)))
8483 ;; This is an internal link, widen the buffer
8484 (switch-to-buffer-other-window buffer)
8485 (widen)
8486 (goto-char marker)
8487 (when (search-forward l nil lkend)
8488 (goto-char (match-beginning 0))
8489 (org-open-at-point)))))
8490 lk))
8491 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8492 (save-excursion
8493 (beginning-of-line 1)
8494 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8495 (org-open-link-from-string (match-string 1)))
8496 (t (message "No link to open here")))))
8498 (defun org-agenda-copy-local-variable (var)
8499 "Get a variable from a referenced buffer and install it here."
8500 (let ((m (org-get-at-bol 'org-marker)))
8501 (when (and m (buffer-live-p (marker-buffer m)))
8502 (set (make-local-variable var)
8503 (with-current-buffer (marker-buffer m)
8504 (symbol-value var))))))
8506 (defun org-agenda-switch-to (&optional delete-other-windows)
8507 "Go to the Org mode file which contains the item at point.
8508 When optional argument DELETE-OTHER-WINDOWS is non-nil, the
8509 displayed Org file fills the frame."
8510 (interactive)
8511 (if (and org-return-follows-link
8512 (not (org-get-at-bol 'org-marker))
8513 (org-in-regexp org-bracket-link-regexp))
8514 (org-open-link-from-string (match-string 0))
8515 (let* ((marker (or (org-get-at-bol 'org-marker)
8516 (org-agenda-error)))
8517 (buffer (marker-buffer marker))
8518 (pos (marker-position marker)))
8519 (unless buffer (user-error "Trying to switch to non-existent buffer"))
8520 (pop-to-buffer-same-window buffer)
8521 (when delete-other-windows (delete-other-windows))
8522 (widen)
8523 (goto-char pos)
8524 (when (derived-mode-p 'org-mode)
8525 (org-show-context 'agenda)
8526 (run-hooks 'org-agenda-after-show-hook)))))
8528 (defun org-agenda-goto-mouse (ev)
8529 "Go to the Org file which contains the item at the mouse click."
8530 (interactive "e")
8531 (mouse-set-point ev)
8532 (org-agenda-goto))
8534 (defun org-agenda-show (&optional full-entry)
8535 "Display the Org file which contains the item at point.
8536 With prefix argument FULL-ENTRY, make the entire entry visible
8537 if it was hidden in the outline."
8538 (interactive "P")
8539 (let ((win (selected-window)))
8540 (org-agenda-goto t)
8541 (when full-entry (org-show-entry))
8542 (select-window win)))
8544 (defvar org-agenda-show-window nil)
8545 (defun org-agenda-show-and-scroll-up (&optional arg)
8546 "Display the Org file which contains the item at point.
8548 When called repeatedly, scroll the window that is displaying the buffer.
8550 With a `\\[universal-argument]' prefix, use `org-show-entry' instead of \
8551 `outline-show-subtree'
8552 to display the item, so that drawers and logbooks stay folded."
8553 (interactive "P")
8554 (let ((win (selected-window)))
8555 (if (and (window-live-p org-agenda-show-window)
8556 (eq this-command last-command))
8557 (progn
8558 (select-window org-agenda-show-window)
8559 (ignore-errors (scroll-up)))
8560 (org-agenda-goto t)
8561 (if arg (org-show-entry) (outline-show-subtree))
8562 (setq org-agenda-show-window (selected-window)))
8563 (select-window win)))
8565 (defun org-agenda-show-scroll-down ()
8566 "Scroll down the window showing the agenda."
8567 (interactive)
8568 (let ((win (selected-window)))
8569 (when (window-live-p org-agenda-show-window)
8570 (select-window org-agenda-show-window)
8571 (ignore-errors (scroll-down))
8572 (select-window win))))
8574 (defun org-agenda-show-1 (&optional more)
8575 "Display the Org file which contains the item at point.
8576 The prefix arg selects the amount of information to display:
8578 0 hide the subtree
8579 1 just show the entry according to defaults.
8580 2 show the children view
8581 3 show the subtree view
8582 4 show the entire subtree and any LOGBOOK drawers
8583 5 show the entire subtree and any drawers
8584 With prefix argument FULL-ENTRY, make the entire entry visible
8585 if it was hidden in the outline."
8586 (interactive "p")
8587 (let ((win (selected-window)))
8588 (org-agenda-goto t)
8589 (org-back-to-heading)
8590 (set-window-start (selected-window) (point-at-bol))
8591 (cond
8592 ((= more 0)
8593 (outline-hide-subtree)
8594 (save-excursion
8595 (org-back-to-heading)
8596 (run-hook-with-args 'org-cycle-hook 'folded))
8597 (message "Remote: FOLDED"))
8598 ((and (called-interactively-p 'any) (= more 1))
8599 (message "Remote: show with default settings"))
8600 ((= more 2)
8601 (outline-show-entry)
8602 (org-show-children)
8603 (save-excursion
8604 (org-back-to-heading)
8605 (run-hook-with-args 'org-cycle-hook 'children))
8606 (message "Remote: CHILDREN"))
8607 ((= more 3)
8608 (outline-show-subtree)
8609 (save-excursion
8610 (org-back-to-heading)
8611 (run-hook-with-args 'org-cycle-hook 'subtree))
8612 (message "Remote: SUBTREE"))
8613 ((= more 4)
8614 (outline-show-subtree)
8615 (save-excursion
8616 (org-back-to-heading)
8617 (org-cycle-hide-drawers 'subtree '("LOGBOOK")))
8618 (message "Remote: SUBTREE AND LOGBOOK"))
8619 ((> more 4)
8620 (outline-show-subtree)
8621 (message "Remote: SUBTREE AND ALL DRAWERS")))
8622 (select-window win)))
8624 (defvar org-agenda-cycle-counter nil)
8625 (defun org-agenda-cycle-show (&optional n)
8626 "Show the current entry in another window, with default settings.
8628 Default settings are taken from `org-show-context-detail'. When
8629 use repeatedly in immediate succession, the remote entry will
8630 cycle through visibility
8632 children -> subtree -> folded
8634 When called with a numeric prefix arg, that arg will be passed through to
8635 `org-agenda-show-1'. For the interpretation of that argument, see the
8636 docstring of `org-agenda-show-1'."
8637 (interactive "P")
8638 (if (integerp n)
8639 (setq org-agenda-cycle-counter n)
8640 (if (not (eq last-command this-command))
8641 (setq org-agenda-cycle-counter 1)
8642 (if (equal org-agenda-cycle-counter 0)
8643 (setq org-agenda-cycle-counter 2)
8644 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8645 (if (> org-agenda-cycle-counter 3)
8646 (setq org-agenda-cycle-counter 0)))))
8647 (org-agenda-show-1 org-agenda-cycle-counter))
8649 (defun org-agenda-recenter (arg)
8650 "Display the Org file which contains the item at point and recenter."
8651 (interactive "P")
8652 (let ((win (selected-window)))
8653 (org-agenda-goto t)
8654 (recenter arg)
8655 (select-window win)))
8657 (defun org-agenda-show-mouse (ev)
8658 "Display the Org file which contains the item at the mouse click."
8659 (interactive "e")
8660 (mouse-set-point ev)
8661 (org-agenda-show))
8663 (defun org-agenda-check-no-diary ()
8664 "Check if the entry is a diary link and abort if yes."
8665 (if (org-get-at-bol 'org-agenda-diary-link)
8666 (org-agenda-error)))
8668 (defun org-agenda-error ()
8669 "Throw an error when a command is not allowed in the agenda."
8670 (user-error "Command not allowed in this line"))
8672 (defun org-agenda-tree-to-indirect-buffer (arg)
8673 "Show the subtree corresponding to the current entry in an indirect buffer.
8674 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8676 With a numerical prefix ARG, go up to this level and then take that tree.
8677 With a negative numeric ARG, go up by this number of levels.
8679 With a `\\[universal-argument]' prefix, make a separate frame for this tree, \
8680 i.e. don't use
8681 the dedicated frame."
8682 (interactive "P")
8683 (if current-prefix-arg
8684 (org-agenda-do-tree-to-indirect-buffer arg)
8685 (let ((agenda-buffer (buffer-name))
8686 (agenda-window (selected-window))
8687 (indirect-window
8688 (and org-last-indirect-buffer
8689 (get-buffer-window org-last-indirect-buffer))))
8690 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8691 (unless (or (eq org-indirect-buffer-display 'new-frame)
8692 (eq org-indirect-buffer-display 'dedicated-frame))
8693 (unwind-protect
8694 (unless (and indirect-window (window-live-p indirect-window))
8695 (setq indirect-window (split-window agenda-window)))
8696 (and indirect-window (select-window indirect-window))
8697 (switch-to-buffer org-last-indirect-buffer :norecord)
8698 (fit-window-to-buffer indirect-window)))
8699 (select-window (get-buffer-window agenda-buffer))
8700 (setq org-agenda-last-indirect-buffer org-last-indirect-buffer))))
8702 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8703 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8704 (org-agenda-check-no-diary)
8705 (let* ((marker (or (org-get-at-bol 'org-marker)
8706 (org-agenda-error)))
8707 (buffer (marker-buffer marker))
8708 (pos (marker-position marker)))
8709 (with-current-buffer buffer
8710 (save-excursion
8711 (goto-char pos)
8712 (funcall 'org-tree-to-indirect-buffer arg)))))
8714 (defvar org-last-heading-marker (make-marker)
8715 "Marker pointing to the headline that last changed its TODO state
8716 by a remote command from the agenda.")
8718 (defun org-agenda-todo-nextset ()
8719 "Switch TODO entry to next sequence."
8720 (interactive)
8721 (org-agenda-todo 'nextset))
8723 (defun org-agenda-todo-previousset ()
8724 "Switch TODO entry to previous sequence."
8725 (interactive)
8726 (org-agenda-todo 'previousset))
8728 (defun org-agenda-todo (&optional arg)
8729 "Cycle TODO state of line at point, also in Org file.
8730 This changes the line at point, all other lines in the agenda referring to
8731 the same tree node, and the headline of the tree node in the Org file."
8732 (interactive "P")
8733 (org-agenda-check-no-diary)
8734 (let* ((col (current-column))
8735 (marker (or (org-get-at-bol 'org-marker)
8736 (org-agenda-error)))
8737 (buffer (marker-buffer marker))
8738 (pos (marker-position marker))
8739 (hdmarker (org-get-at-bol 'org-hd-marker))
8740 (todayp (org-agenda-today-p (org-get-at-bol 'day)))
8741 (inhibit-read-only t)
8742 org-agenda-headline-snapshot-before-repeat newhead just-one)
8743 (org-with-remote-undo buffer
8744 (with-current-buffer buffer
8745 (widen)
8746 (goto-char pos)
8747 (org-show-context 'agenda)
8748 (let ((current-prefix-arg arg))
8749 (call-interactively 'org-todo))
8750 (and (bolp) (forward-char 1))
8751 (setq newhead (org-get-heading))
8752 (when (and (bound-and-true-p
8753 org-agenda-headline-snapshot-before-repeat)
8754 (not (equal org-agenda-headline-snapshot-before-repeat
8755 newhead))
8756 todayp)
8757 (setq newhead org-agenda-headline-snapshot-before-repeat
8758 just-one t))
8759 (save-excursion
8760 (org-back-to-heading)
8761 (move-marker org-last-heading-marker (point))))
8762 (beginning-of-line 1)
8763 (save-window-excursion
8764 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8765 (when (bound-and-true-p org-clock-out-when-done)
8766 (string-match (concat "^" (regexp-opt org-done-keywords-for-agenda))
8767 newhead)
8768 (org-agenda-unmark-clocking-task))
8769 (org-move-to-column col)
8770 (org-agenda-mark-clocking-task))))
8772 (defun org-agenda-add-note (&optional arg)
8773 "Add a time-stamped note to the entry at point."
8774 (interactive "P")
8775 (org-agenda-check-no-diary)
8776 (let* ((marker (or (org-get-at-bol 'org-marker)
8777 (org-agenda-error)))
8778 (buffer (marker-buffer marker))
8779 (pos (marker-position marker))
8780 (hdmarker (org-get-at-bol 'org-hd-marker))
8781 (inhibit-read-only t))
8782 (with-current-buffer buffer
8783 (widen)
8784 (goto-char pos)
8785 (org-show-context 'agenda)
8786 (org-add-note))))
8788 (defun org-agenda-change-all-lines (newhead hdmarker
8789 &optional fixface just-this)
8790 "Change all lines in the agenda buffer which match HDMARKER.
8791 The new content of the line will be NEWHEAD (as modified by
8792 `org-agenda-format-item'). HDMARKER is checked with
8793 `equal' against all `org-hd-marker' text properties in the file.
8794 If FIXFACE is non-nil, the face of each item is modified according to
8795 the new TODO state.
8796 If JUST-THIS is non-nil, change just the current line, not all.
8797 If FORCE-TAGS is non nil, the car of it returns the new tags."
8798 (let* ((inhibit-read-only t)
8799 (line (org-current-line))
8800 (org-agenda-buffer (current-buffer))
8801 (thetags (with-current-buffer (marker-buffer hdmarker)
8802 (org-with-wide-buffer
8803 (goto-char hdmarker)
8804 (org-get-tags-at))))
8805 props m pl undone-face done-face finish new dotime level cat tags)
8806 (save-excursion
8807 (goto-char (point-max))
8808 (beginning-of-line 1)
8809 (while (not finish)
8810 (setq finish (bobp))
8811 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8812 (or (not just-this) (= (org-current-line) line))
8813 (equal m hdmarker))
8814 (setq props (text-properties-at (point))
8815 dotime (org-get-at-bol 'dotime)
8816 cat (org-get-at-eol 'org-category 1)
8817 level (org-get-at-bol 'level)
8818 tags thetags
8820 (let ((org-prefix-format-compiled
8821 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8822 org-prefix-format-compiled))
8823 (extra (org-get-at-bol 'extra)))
8824 (with-current-buffer (marker-buffer hdmarker)
8825 (org-with-wide-buffer
8826 (org-agenda-format-item extra newhead level cat tags dotime))))
8827 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8828 undone-face (org-get-at-bol 'undone-face)
8829 done-face (org-get-at-bol 'done-face))
8830 (beginning-of-line 1)
8831 (cond
8832 ((equal new "") (delete-region (point) (line-beginning-position 2)))
8833 ((looking-at ".*")
8834 ;; When replacing the whole line, preserve bulk mark
8835 ;; overlay, if any.
8836 (let ((mark (catch :overlay
8837 (dolist (o (overlays-in (point) (+ 2 (point))))
8838 (when (eq (overlay-get o 'type)
8839 'org-marked-entry-overlay)
8840 (throw :overlay o))))))
8841 (replace-match new t t)
8842 (beginning-of-line)
8843 (when mark (move-overlay mark (point) (+ 2 (point)))))
8844 (add-text-properties (point-at-bol) (point-at-eol) props)
8845 (when fixface
8846 (add-text-properties
8847 (point-at-bol) (point-at-eol)
8848 (list 'face
8849 (if org-last-todo-state-is-todo
8850 undone-face done-face))))
8851 (org-agenda-highlight-todo 'line)
8852 (beginning-of-line 1))
8853 (t (error "Line update did not work")))
8854 (save-restriction
8855 (narrow-to-region (point-at-bol) (point-at-eol))
8856 (org-agenda-finalize)))
8857 (beginning-of-line 0)))))
8859 (defun org-agenda-align-tags (&optional line)
8860 "Align all tags in agenda items to `org-agenda-tags-column'."
8861 (let ((inhibit-read-only t) l c)
8862 (save-excursion
8863 (goto-char (if line (point-at-bol) (point-min)))
8864 (while (re-search-forward "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"
8865 (if line (point-at-eol) nil) t)
8866 (add-text-properties
8867 (match-beginning 2) (match-end 2)
8868 (list 'face (delq nil (let ((prop (get-text-property
8869 (match-beginning 2) 'face)))
8870 (or (listp prop) (setq prop (list prop)))
8871 (if (memq 'org-tag prop)
8872 prop
8873 (cons 'org-tag prop))))))
8874 (setq l (- (match-end 2) (match-beginning 2))
8875 c (if (< org-agenda-tags-column 0)
8876 (- (abs org-agenda-tags-column) l)
8877 org-agenda-tags-column))
8878 (delete-region (match-beginning 1) (match-end 1))
8879 (goto-char (match-beginning 1))
8880 (insert (org-add-props
8881 (make-string (max 1 (- c (current-column))) ?\ )
8882 (plist-put (copy-sequence (text-properties-at (point)))
8883 'face nil))))
8884 (goto-char (point-min))
8885 (org-font-lock-add-tag-faces (point-max)))))
8887 (defun org-agenda-priority-up ()
8888 "Increase the priority of line at point, also in Org file."
8889 (interactive)
8890 (org-agenda-priority 'up))
8892 (defun org-agenda-priority-down ()
8893 "Decrease the priority of line at point, also in Org file."
8894 (interactive)
8895 (org-agenda-priority 'down))
8897 (defun org-agenda-priority (&optional force-direction)
8898 "Set the priority of line at point, also in Org file.
8899 This changes the line at point, all other lines in the agenda referring to
8900 the same tree node, and the headline of the tree node in the Org file.
8901 Called with a universal prefix arg, show the priority instead of setting it."
8902 (interactive "P")
8903 (if (equal force-direction '(4))
8904 (org-show-priority)
8905 (unless org-enable-priority-commands
8906 (error "Priority commands are disabled"))
8907 (org-agenda-check-no-diary)
8908 (let* ((col (current-column))
8909 (marker (or (org-get-at-bol 'org-marker)
8910 (org-agenda-error)))
8911 (hdmarker (org-get-at-bol 'org-hd-marker))
8912 (buffer (marker-buffer hdmarker))
8913 (pos (marker-position hdmarker))
8914 (inhibit-read-only t)
8915 newhead)
8916 (org-with-remote-undo buffer
8917 (with-current-buffer buffer
8918 (widen)
8919 (goto-char pos)
8920 (org-show-context 'agenda)
8921 (funcall 'org-priority force-direction)
8922 (end-of-line 1)
8923 (setq newhead (org-get-heading)))
8924 (org-agenda-change-all-lines newhead hdmarker)
8925 (org-move-to-column col)))))
8927 ;; FIXME: should fix the tags property of the agenda line.
8928 (defun org-agenda-set-tags (&optional tag onoff)
8929 "Set tags for the current headline."
8930 (interactive)
8931 (org-agenda-check-no-diary)
8932 (if (and (org-region-active-p) (called-interactively-p 'any))
8933 (call-interactively 'org-change-tag-in-region)
8934 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8935 (org-agenda-error)))
8936 (buffer (marker-buffer hdmarker))
8937 (pos (marker-position hdmarker))
8938 (inhibit-read-only t)
8939 newhead)
8940 (org-with-remote-undo buffer
8941 (with-current-buffer buffer
8942 (widen)
8943 (goto-char pos)
8944 (org-show-context 'agenda)
8945 (if tag
8946 (org-toggle-tag tag onoff)
8947 (call-interactively 'org-set-tags))
8948 (end-of-line 1)
8949 (setq newhead (org-get-heading)))
8950 (org-agenda-change-all-lines newhead hdmarker)
8951 (beginning-of-line 1)))))
8953 (defun org-agenda-set-property ()
8954 "Set a property for the current headline."
8955 (interactive)
8956 (org-agenda-check-no-diary)
8957 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8958 (org-agenda-error)))
8959 (buffer (marker-buffer hdmarker))
8960 (pos (marker-position hdmarker))
8961 (inhibit-read-only t)
8962 newhead)
8963 (org-with-remote-undo buffer
8964 (with-current-buffer buffer
8965 (widen)
8966 (goto-char pos)
8967 (org-show-context 'agenda)
8968 (call-interactively 'org-set-property)))))
8970 (defun org-agenda-set-effort ()
8971 "Set the effort property for the current headline."
8972 (interactive)
8973 (org-agenda-check-no-diary)
8974 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8975 (org-agenda-error)))
8976 (buffer (marker-buffer hdmarker))
8977 (pos (marker-position hdmarker))
8978 (inhibit-read-only t)
8979 newhead)
8980 (org-with-remote-undo buffer
8981 (with-current-buffer buffer
8982 (widen)
8983 (goto-char pos)
8984 (org-show-context 'agenda)
8985 (call-interactively 'org-set-effort)
8986 (end-of-line 1)
8987 (setq newhead (org-get-heading)))
8988 (org-agenda-change-all-lines newhead hdmarker))))
8990 (defun org-agenda-toggle-archive-tag ()
8991 "Toggle the archive tag for the current entry."
8992 (interactive)
8993 (org-agenda-check-no-diary)
8994 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8995 (org-agenda-error)))
8996 (buffer (marker-buffer hdmarker))
8997 (pos (marker-position hdmarker))
8998 (inhibit-read-only t)
8999 newhead)
9000 (org-with-remote-undo buffer
9001 (with-current-buffer buffer
9002 (widen)
9003 (goto-char pos)
9004 (org-show-context 'agenda)
9005 (call-interactively 'org-toggle-archive-tag)
9006 (end-of-line 1)
9007 (setq newhead (org-get-heading)))
9008 (org-agenda-change-all-lines newhead hdmarker)
9009 (beginning-of-line 1))))
9011 (defun org-agenda-do-date-later (arg)
9012 (interactive "P")
9013 (cond
9014 ((or (equal arg '(16))
9015 (memq last-command
9016 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9017 (setq this-command 'org-agenda-date-later-minutes)
9018 (org-agenda-date-later-minutes 1))
9019 ((or (equal arg '(4))
9020 (memq last-command
9021 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9022 (setq this-command 'org-agenda-date-later-hours)
9023 (org-agenda-date-later-hours 1))
9025 (org-agenda-date-later (prefix-numeric-value arg)))))
9027 (defun org-agenda-do-date-earlier (arg)
9028 (interactive "P")
9029 (cond
9030 ((or (equal arg '(16))
9031 (memq last-command
9032 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9033 (setq this-command 'org-agenda-date-earlier-minutes)
9034 (org-agenda-date-earlier-minutes 1))
9035 ((or (equal arg '(4))
9036 (memq last-command
9037 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9038 (setq this-command 'org-agenda-date-earlier-hours)
9039 (org-agenda-date-earlier-hours 1))
9041 (org-agenda-date-earlier (prefix-numeric-value arg)))))
9043 (defun org-agenda-date-later (arg &optional what)
9044 "Change the date of this item to ARG day(s) later."
9045 (interactive "p")
9046 (org-agenda-check-type t 'agenda 'timeline)
9047 (org-agenda-check-no-diary)
9048 (let* ((marker (or (org-get-at-bol 'org-marker)
9049 (org-agenda-error)))
9050 (buffer (marker-buffer marker))
9051 (pos (marker-position marker))
9052 cdate today)
9053 (org-with-remote-undo buffer
9054 (with-current-buffer buffer
9055 (widen)
9056 (goto-char pos)
9057 (if (not (org-at-timestamp-p))
9058 (error "Cannot find time stamp"))
9059 (when (and org-agenda-move-date-from-past-immediately-to-today
9060 (equal arg 1)
9061 (or (not what) (eq what 'day))
9062 (not (save-match-data (org-at-date-range-p))))
9063 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9064 cdate (calendar-absolute-from-gregorian
9065 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9066 today (org-today))
9067 (if (> today cdate)
9068 ;; immediately shift to today
9069 (setq arg (- today cdate))))
9070 (org-timestamp-change arg (or what 'day))
9071 (when (and (org-at-date-range-p)
9072 (re-search-backward org-tr-regexp-both (point-at-bol)))
9073 (let ((end org-last-changed-timestamp))
9074 (org-timestamp-change arg (or what 'day))
9075 (setq org-last-changed-timestamp
9076 (concat org-last-changed-timestamp "--" end)))))
9077 (org-agenda-show-new-time marker org-last-changed-timestamp))
9078 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9080 (defun org-agenda-date-earlier (arg &optional what)
9081 "Change the date of this item to ARG day(s) earlier."
9082 (interactive "p")
9083 (org-agenda-date-later (- arg) what))
9085 (defun org-agenda-date-later-minutes (arg)
9086 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9087 (interactive "p")
9088 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9089 (org-agenda-date-later arg 'minute))
9091 (defun org-agenda-date-earlier-minutes (arg)
9092 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9093 (interactive "p")
9094 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9095 (org-agenda-date-earlier arg 'minute))
9097 (defun org-agenda-date-later-hours (arg)
9098 "Change the time of this item, in hour steps."
9099 (interactive "p")
9100 (org-agenda-date-later arg 'hour))
9102 (defun org-agenda-date-earlier-hours (arg)
9103 "Change the time of this item, in hour steps."
9104 (interactive "p")
9105 (org-agenda-date-earlier arg 'hour))
9107 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9108 "Show new date stamp via text properties."
9109 ;; We use text properties to make this undoable
9110 (let ((inhibit-read-only t))
9111 (setq stamp (concat prefix " => " stamp " "))
9112 (save-excursion
9113 (goto-char (point-max))
9114 (while (not (bobp))
9115 (when (equal marker (org-get-at-bol 'org-marker))
9116 (remove-text-properties (point-at-bol) (point-at-eol) '(display))
9117 (org-move-to-column (- (window-width) (length stamp)) t)
9118 (add-text-properties
9119 (1- (point)) (point-at-eol)
9120 (list 'display (org-add-props stamp nil
9121 'face '(secondary-selection default))))
9122 (beginning-of-line 1))
9123 (beginning-of-line 0)))))
9125 (defun org-agenda-date-prompt (arg)
9126 "Change the date of this item. Date is prompted for, with default today.
9127 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9128 be used to request time specification in the time stamp."
9129 (interactive "P")
9130 (org-agenda-check-type t 'agenda 'timeline)
9131 (org-agenda-check-no-diary)
9132 (let* ((marker (or (org-get-at-bol 'org-marker)
9133 (org-agenda-error)))
9134 (buffer (marker-buffer marker))
9135 (pos (marker-position marker)))
9136 (org-with-remote-undo buffer
9137 (with-current-buffer buffer
9138 (widen)
9139 (goto-char pos)
9140 (if (not (org-at-timestamp-p t))
9141 (error "Cannot find time stamp"))
9142 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
9143 (org-agenda-show-new-time marker org-last-changed-timestamp))
9144 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9146 (defun org-agenda-schedule (arg &optional time)
9147 "Schedule the item at point.
9148 ARG is passed through to `org-schedule'."
9149 (interactive "P")
9150 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9151 (org-agenda-check-no-diary)
9152 (let* ((marker (or (org-get-at-bol 'org-marker)
9153 (org-agenda-error)))
9154 (type (marker-insertion-type marker))
9155 (buffer (marker-buffer marker))
9156 (pos (marker-position marker))
9158 (set-marker-insertion-type marker t)
9159 (org-with-remote-undo buffer
9160 (with-current-buffer buffer
9161 (widen)
9162 (goto-char pos)
9163 (setq ts (org-schedule arg time)))
9164 (org-agenda-show-new-time marker ts " S"))
9165 (message "%s" ts)))
9167 (defun org-agenda-deadline (arg &optional time)
9168 "Schedule the item at point.
9169 ARG is passed through to `org-deadline'."
9170 (interactive "P")
9171 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9172 (org-agenda-check-no-diary)
9173 (let* ((marker (or (org-get-at-bol 'org-marker)
9174 (org-agenda-error)))
9175 (buffer (marker-buffer marker))
9176 (pos (marker-position marker))
9178 (org-with-remote-undo buffer
9179 (with-current-buffer buffer
9180 (widen)
9181 (goto-char pos)
9182 (setq ts (org-deadline arg time)))
9183 (org-agenda-show-new-time marker ts " D"))
9184 (message "%s" ts)))
9186 (defun org-agenda-clock-in (&optional arg)
9187 "Start the clock on the currently selected item."
9188 (interactive "P")
9189 (org-agenda-check-no-diary)
9190 (if (equal arg '(4))
9191 (org-clock-in arg)
9192 (let* ((marker (or (org-get-at-bol 'org-marker)
9193 (org-agenda-error)))
9194 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9195 (pos (marker-position marker))
9196 (col (current-column))
9197 newhead)
9198 (org-with-remote-undo (marker-buffer marker)
9199 (with-current-buffer (marker-buffer marker)
9200 (widen)
9201 (goto-char pos)
9202 (org-show-context 'agenda)
9203 (org-cycle-hide-drawers 'children)
9204 (org-clock-in arg)
9205 (setq newhead (org-get-heading)))
9206 (org-agenda-change-all-lines newhead hdmarker))
9207 (org-move-to-column col))))
9209 (defun org-agenda-clock-out ()
9210 "Stop the currently running clock."
9211 (interactive)
9212 (unless (marker-buffer org-clock-marker)
9213 (error "No running clock"))
9214 (let ((marker (make-marker)) (col (current-column)) newhead)
9215 (org-with-remote-undo (marker-buffer org-clock-marker)
9216 (with-current-buffer (marker-buffer org-clock-marker)
9217 (org-with-wide-buffer
9218 (goto-char org-clock-marker)
9219 (org-back-to-heading t)
9220 (move-marker marker (point))
9221 (org-clock-out)
9222 (setq newhead (org-get-heading)))))
9223 (org-agenda-change-all-lines newhead marker)
9224 (move-marker marker nil)
9225 (org-move-to-column col)
9226 (org-agenda-unmark-clocking-task)))
9228 (defun org-agenda-clock-cancel (&optional arg)
9229 "Cancel the currently running clock."
9230 (interactive "P")
9231 (unless (marker-buffer org-clock-marker)
9232 (user-error "No running clock"))
9233 (org-with-remote-undo (marker-buffer org-clock-marker)
9234 (org-clock-cancel)))
9236 (defun org-agenda-clock-goto ()
9237 "Jump to the currently clocked in task within the agenda.
9238 If the currently clocked in task is not listed in the agenda
9239 buffer, display it in another window."
9240 (interactive)
9241 (let (pos)
9242 (mapc (lambda (o)
9243 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
9244 (setq pos (overlay-start o))))
9245 (overlays-in (point-min) (point-max)))
9246 (cond (pos (goto-char pos))
9247 ;; If the currently clocked entry is not in the agenda
9248 ;; buffer, we visit it in another window:
9249 ((bound-and-true-p org-clock-current-task)
9250 (org-switch-to-buffer-other-window (org-clock-goto)))
9251 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9253 (defun org-agenda-diary-entry-in-org-file ()
9254 "Make a diary entry in the file `org-agenda-diary-file'."
9255 (let (d1 d2 char (text "") dp1 dp2)
9256 (if (equal (buffer-name) "*Calendar*")
9257 (setq d1 (calendar-cursor-to-date t)
9258 d2 (car calendar-mark-ring))
9259 (setq dp1 (get-text-property (point-at-bol) 'day))
9260 (unless dp1 (user-error "No date defined in current line"))
9261 (setq d1 (calendar-gregorian-from-absolute dp1)
9262 d2 (and (ignore-errors (mark))
9263 (save-excursion
9264 (goto-char (mark))
9265 (setq dp2 (get-text-property (point-at-bol) 'day)))
9266 (calendar-gregorian-from-absolute dp2))))
9267 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9268 (setq char (read-char-exclusive))
9269 (cond
9270 ((equal char ?d)
9271 (setq text (read-string "Day entry: "))
9272 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9273 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9274 ((equal char ?a)
9275 (setq d1 (list (car d1) (nth 1 d1)
9276 (read-number (format "Reference year [%d]: " (nth 2 d1))
9277 (nth 2 d1))))
9278 (setq text (read-string "Anniversary (use %d to show years): "))
9279 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9280 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9281 ((equal char ?b)
9282 (setq text (read-string "Block entry: "))
9283 (unless (and d1 d2 (not (equal d1 d2)))
9284 (user-error "No block of days selected"))
9285 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9286 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9287 ((equal char ?j)
9288 (org-switch-to-buffer-other-window
9289 (find-file-noselect org-agenda-diary-file))
9290 (require 'org-datetree)
9291 (org-datetree-find-date-create d1)
9292 (org-reveal t))
9293 (t (user-error "Invalid selection character `%c'" char)))))
9295 (defcustom org-agenda-insert-diary-strategy 'date-tree
9296 "Where in `org-agenda-diary-file' should new entries be added?
9297 Valid values:
9299 date-tree in the date tree, as first child of the date
9300 date-tree-last in the date tree, as last child of the date
9301 top-level as top-level entries at the end of the file."
9302 :group 'org-agenda
9303 :type '(choice
9304 (const :tag "first in a date tree" date-tree)
9305 (const :tag "last in a date tree" date-tree-last)
9306 (const :tag "as top level at end of file" top-level)))
9308 (defcustom org-agenda-insert-diary-extract-time nil
9309 "Non-nil means extract any time specification from the diary entry."
9310 :group 'org-agenda
9311 :version "24.1"
9312 :type 'boolean)
9314 (defcustom org-agenda-bulk-mark-char ">"
9315 "A single-character string to be used as the bulk mark."
9316 :group 'org-agenda
9317 :version "24.1"
9318 :type 'string)
9320 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9321 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9322 If TEXT is not empty, it will become the headline of the new entry, and
9323 the resulting entry will not be shown. When TEXT is empty, switch to
9324 `org-agenda-diary-file' and let the user finish the entry there."
9325 (let ((cw (current-window-configuration)))
9326 (org-switch-to-buffer-other-window
9327 (find-file-noselect org-agenda-diary-file))
9328 (widen)
9329 (goto-char (point-min))
9330 (cond
9331 ((eq type 'anniversary)
9332 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
9333 (progn
9334 (or (org-at-heading-p t)
9335 (progn
9336 (outline-next-heading)
9337 (insert "* Anniversaries\n\n")
9338 (beginning-of-line -1)))))
9339 (outline-next-heading)
9340 (org-back-over-empty-lines)
9341 (backward-char 1)
9342 (insert "\n")
9343 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9344 (nth 2 d1) (car d1) (nth 1 d1) text)))
9345 ((eq type 'day)
9346 (let ((org-prefix-has-time t)
9347 (org-agenda-time-leading-zero t)
9348 fmt time time2)
9349 (if org-agenda-insert-diary-extract-time
9350 ;; Use org-agenda-format-item to parse text for a time-range and
9351 ;; remove it. FIXME: This is a hack, we should refactor
9352 ;; that function to make time extraction available separately
9353 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9354 time (get-text-property 0 'time fmt)
9355 time2 (if (> (length time) 0)
9356 ;; split-string removes trailing ...... if
9357 ;; no end time given. First space
9358 ;; separates time from date.
9359 (concat " " (car (split-string time "\\.")))
9360 nil)
9361 text (get-text-property 0 'txt fmt)))
9362 (if (eq org-agenda-insert-diary-strategy 'top-level)
9363 (org-agenda-insert-diary-as-top-level text)
9364 (require 'org-datetree)
9365 (org-datetree-find-date-create d1)
9366 (org-agenda-insert-diary-make-new-entry text))
9367 (org-insert-time-stamp (org-time-from-absolute
9368 (calendar-absolute-from-gregorian d1))
9369 nil nil nil nil time2))
9370 (end-of-line 0))
9371 ((eq type 'block)
9372 (if (> (calendar-absolute-from-gregorian d1)
9373 (calendar-absolute-from-gregorian d2))
9374 (setq d1 (prog1 d2 (setq d2 d1))))
9375 (if (eq org-agenda-insert-diary-strategy 'top-level)
9376 (org-agenda-insert-diary-as-top-level text)
9377 (require 'org-datetree)
9378 (org-datetree-find-date-create d1)
9379 (org-agenda-insert-diary-make-new-entry text))
9380 (org-insert-time-stamp (org-time-from-absolute
9381 (calendar-absolute-from-gregorian d1)))
9382 (insert "--")
9383 (org-insert-time-stamp (org-time-from-absolute
9384 (calendar-absolute-from-gregorian d2)))
9385 (end-of-line 0)))
9386 (if (string-match "\\S-" text)
9387 (progn
9388 (set-window-configuration cw)
9389 (message "%s entry added to %s"
9390 (capitalize (symbol-name type))
9391 (abbreviate-file-name org-agenda-diary-file)))
9392 (org-reveal t)
9393 (message "Please finish entry here"))))
9395 (defun org-agenda-insert-diary-as-top-level (text)
9396 "Make new entry as a top-level entry at the end of the file.
9397 Add TEXT as headline, and position the cursor in the second line so that
9398 a timestamp can be added there."
9399 (widen)
9400 (goto-char (point-max))
9401 (unless (bolp) (insert "\n"))
9402 (org-insert-heading nil t t)
9403 (insert text)
9404 (org-end-of-meta-data)
9405 (unless (bolp) (insert "\n"))
9406 (when org-adapt-indentation (indent-to-column 2)))
9408 (defun org-agenda-insert-diary-make-new-entry (text)
9409 "Make a new entry with TEXT as a child of the current subtree.
9410 Position the point in the heading's first body line so that
9411 a timestamp can be added there."
9412 (cond
9413 ((eq org-agenda-insert-diary-strategy 'date-tree-last)
9414 (end-of-line)
9415 (org-insert-heading '(4) t)
9416 (org-do-demote))
9418 (outline-next-heading)
9419 (org-back-over-empty-lines)
9420 (unless (looking-at "[ \t]*$") (save-excursion (insert "\n")))
9421 (org-insert-heading nil t)
9422 (org-do-demote)))
9423 (let ((col (current-column)))
9424 (insert text)
9425 (org-end-of-meta-data)
9426 ;; Ensure point is left on a blank line, at proper indentation.
9427 (unless (bolp) (insert "\n"))
9428 (unless (looking-at-p "^[ \t]*$") (save-excursion (insert "\n")))
9429 (when org-adapt-indentation (indent-to-column col)))
9430 (org-show-set-visibility 'lineage))
9432 (defun org-agenda-diary-entry ()
9433 "Make a diary entry, like the `i' command from the calendar.
9434 All the standard commands work: block, weekly etc.
9435 When `org-agenda-diary-file' points to a file,
9436 `org-agenda-diary-entry-in-org-file' is called instead to create
9437 entries in that Org file."
9438 (interactive)
9439 (if (not (eq org-agenda-diary-file 'diary-file))
9440 (org-agenda-diary-entry-in-org-file)
9441 (require 'diary-lib)
9442 (let* ((char (progn
9443 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
9444 (read-char-exclusive)))
9445 (cmd (cdr (assoc char
9446 '((?d . diary-insert-entry)
9447 (?w . diary-insert-weekly-entry)
9448 (?m . diary-insert-monthly-entry)
9449 (?y . diary-insert-yearly-entry)
9450 (?a . diary-insert-anniversary-entry)
9451 (?b . diary-insert-block-entry)
9452 (?c . diary-insert-cyclic-entry)))))
9453 (oldf (symbol-function 'calendar-cursor-to-date))
9454 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9455 (point (point))
9456 (mark (or (mark t) (point))))
9457 (unless cmd
9458 (user-error "No command associated with <%c>" char))
9459 (unless (and (get-text-property point 'day)
9460 (or (not (equal ?b char))
9461 (get-text-property mark 'day)))
9462 (user-error "Don't know which date to use for diary entry"))
9463 ;; We implement this by hacking the `calendar-cursor-to-date' function
9464 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9465 (let ((calendar-mark-ring
9466 (list (calendar-gregorian-from-absolute
9467 (or (get-text-property mark 'day)
9468 (get-text-property point 'day))))))
9469 (unwind-protect
9470 (progn
9471 (fset 'calendar-cursor-to-date
9472 (lambda (&optional error dummy)
9473 (calendar-gregorian-from-absolute
9474 (get-text-property point 'day))))
9475 (call-interactively cmd))
9476 (fset 'calendar-cursor-to-date oldf))))))
9478 (defun org-agenda-execute-calendar-command (cmd)
9479 "Execute a calendar command from the agenda with date from cursor."
9480 (org-agenda-check-type t 'agenda 'timeline)
9481 (require 'diary-lib)
9482 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9483 (user-error "Don't know which date to use for the calendar command"))
9484 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9485 (point (point))
9486 (date (calendar-gregorian-from-absolute
9487 (get-text-property point 'day)))
9488 ;; the following 2 vars are needed in the calendar
9489 (displayed-month (car date))
9490 (displayed-year (nth 2 date)))
9491 (unwind-protect
9492 (progn
9493 (fset 'calendar-cursor-to-date
9494 (lambda (&optional error dummy)
9495 (calendar-gregorian-from-absolute
9496 (get-text-property point 'day))))
9497 (call-interactively cmd))
9498 (fset 'calendar-cursor-to-date oldf))))
9500 (defun org-agenda-phases-of-moon ()
9501 "Display the phases of the moon for the 3 months around the cursor date."
9502 (interactive)
9503 (org-agenda-execute-calendar-command 'calendar-lunar-phases))
9505 (defun org-agenda-holidays ()
9506 "Display the holidays for the 3 months around the cursor date."
9507 (interactive)
9508 (org-agenda-execute-calendar-command 'calendar-list-holidays))
9510 (defvar calendar-longitude) ; defined in calendar.el
9511 (defvar calendar-latitude) ; defined in calendar.el
9512 (defvar calendar-location-name) ; defined in calendar.el
9514 (defun org-agenda-sunrise-sunset (arg)
9515 "Display sunrise and sunset for the cursor date.
9516 Latitude and longitude can be specified with the variables
9517 `calendar-latitude' and `calendar-longitude'. When called with prefix
9518 argument, latitude and longitude will be prompted for."
9519 (interactive "P")
9520 (require 'solar)
9521 (let ((calendar-longitude (if arg nil calendar-longitude))
9522 (calendar-latitude (if arg nil calendar-latitude))
9523 (calendar-location-name
9524 (if arg "the given coordinates" calendar-location-name)))
9525 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9527 (defun org-agenda-goto-calendar ()
9528 "Open the Emacs calendar with the date at the cursor."
9529 (interactive)
9530 (org-agenda-check-type t 'agenda 'timeline)
9531 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9532 (user-error "Don't know which date to open in calendar")))
9533 (date (calendar-gregorian-from-absolute day))
9534 (calendar-move-hook nil)
9535 (calendar-view-holidays-initially-flag nil)
9536 (calendar-view-diary-initially-flag nil))
9537 (calendar)
9538 (calendar-goto-date date)))
9540 ;;;###autoload
9541 (defun org-calendar-goto-agenda ()
9542 "Compute the Org agenda for the calendar date displayed at the cursor.
9543 This is a command that has to be installed in `calendar-mode-map'."
9544 (interactive)
9545 ;; Temporarily disable sticky agenda since user clearly wants to
9546 ;; refresh view anyway.
9547 (let ((org-agenda-buffer-tmp-name "*Org Agenda(a)*")
9548 (org-agenda-sticky nil))
9549 (org-agenda-list nil (calendar-absolute-from-gregorian
9550 (calendar-cursor-to-date))
9551 nil)))
9553 (defun org-agenda-convert-date ()
9554 (interactive)
9555 (org-agenda-check-type t 'agenda 'timeline)
9556 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9557 date s)
9558 (unless day
9559 (user-error "Don't know which date to convert"))
9560 (setq date (calendar-gregorian-from-absolute day))
9561 (setq s (concat
9562 "Gregorian: " (calendar-date-string date) "\n"
9563 "ISO: " (calendar-iso-date-string date) "\n"
9564 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9565 "Julian: " (calendar-julian-date-string date) "\n"
9566 "Astron. JD: " (calendar-astro-date-string date)
9567 " (Julian date number at noon UTC)\n"
9568 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9569 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9570 "French: " (calendar-french-date-string date) "\n"
9571 "Bahá’í: " (calendar-bahai-date-string date) " (until sunset)\n"
9572 "Mayan: " (calendar-mayan-date-string date) "\n"
9573 "Coptic: " (calendar-coptic-date-string date) "\n"
9574 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9575 "Persian: " (calendar-persian-date-string date) "\n"
9576 "Chinese: " (calendar-chinese-date-string date) "\n"))
9577 (with-output-to-temp-buffer "*Dates*"
9578 (princ s))
9579 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9581 ;;; Bulk commands
9583 (defun org-agenda-bulk-marked-p ()
9584 "Non-nil when current entry is marked for bulk action."
9585 (eq (get-char-property (point-at-bol) 'type)
9586 'org-marked-entry-overlay))
9588 (defun org-agenda-bulk-mark (&optional arg)
9589 "Mark the entry at point for future bulk action."
9590 (interactive "p")
9591 (dotimes (i (or arg 1))
9592 (unless (org-get-at-bol 'org-agenda-diary-link)
9593 (let* ((m (org-get-at-bol 'org-hd-marker))
9595 (unless (org-agenda-bulk-marked-p)
9596 (unless m (user-error "Nothing to mark at point"))
9597 (push m org-agenda-bulk-marked-entries)
9598 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9599 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9600 (org-get-todo-face "TODO")
9601 'evaporate)
9602 (overlay-put ov 'type 'org-marked-entry-overlay))
9603 (end-of-line 1)
9604 (or (ignore-errors
9605 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9606 (beginning-of-line 2))
9607 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9608 (beginning-of-line 2))
9609 (message "%d entries marked for bulk action"
9610 (length org-agenda-bulk-marked-entries))))))
9612 (defun org-agenda-bulk-mark-all ()
9613 "Mark all entries for future agenda bulk action."
9614 (interactive)
9615 (org-agenda-bulk-mark-regexp "."))
9617 (defun org-agenda-bulk-mark-regexp (regexp)
9618 "Mark entries matching REGEXP for future agenda bulk action."
9619 (interactive "sMark entries matching regexp: ")
9620 (let ((entries-marked 0) txt-at-point)
9621 (save-excursion
9622 (goto-char (point-min))
9623 (goto-char (next-single-property-change (point) 'org-hd-marker))
9624 (while (and (re-search-forward regexp nil t)
9625 (setq txt-at-point (get-text-property (point) 'txt)))
9626 (if (get-char-property (point) 'invisible)
9627 (beginning-of-line 2)
9628 (when (string-match regexp txt-at-point)
9629 (setq entries-marked (1+ entries-marked))
9630 (call-interactively 'org-agenda-bulk-mark)))))
9632 (if (not entries-marked)
9633 (message "No entry matching this regexp."))))
9635 (defun org-agenda-bulk-unmark (&optional arg)
9636 "Unmark the entry at point for future bulk action."
9637 (interactive "P")
9638 (if arg
9639 (org-agenda-bulk-unmark-all)
9640 (cond ((org-agenda-bulk-marked-p)
9641 (org-agenda-bulk-remove-overlays
9642 (point-at-bol) (+ 2 (point-at-bol)))
9643 (setq org-agenda-bulk-marked-entries
9644 (delete (org-get-at-bol 'org-hd-marker)
9645 org-agenda-bulk-marked-entries))
9646 (end-of-line 1)
9647 (or (ignore-errors
9648 (goto-char (next-single-property-change (point) 'txt)))
9649 (beginning-of-line 2))
9650 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9651 (beginning-of-line 2))
9652 (message "%d entries left marked for bulk action"
9653 (length org-agenda-bulk-marked-entries)))
9654 (t (message "No entry to unmark here")))))
9656 (defun org-agenda-bulk-toggle-all ()
9657 "Toggle all marks for bulk action."
9658 (interactive)
9659 (save-excursion
9660 (goto-char (point-min))
9661 (while (ignore-errors
9662 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9663 (org-agenda-bulk-toggle))))
9665 (defun org-agenda-bulk-toggle ()
9666 "Toggle the mark at point for bulk action."
9667 (interactive)
9668 (if (org-agenda-bulk-marked-p)
9669 (org-agenda-bulk-unmark)
9670 (org-agenda-bulk-mark)))
9672 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9673 "Remove the mark overlays between BEG and END in the agenda buffer.
9674 BEG and END default to the buffer limits.
9676 This only removes the overlays, it does not remove the markers
9677 from the list in `org-agenda-bulk-marked-entries'."
9678 (interactive)
9679 (mapc (lambda (ov)
9680 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9681 (delete-overlay ov)))
9682 (overlays-in (or beg (point-min)) (or end (point-max)))))
9684 (defun org-agenda-bulk-unmark-all ()
9685 "Remove all marks in the agenda buffer.
9686 This will remove the markers and the overlays."
9687 (interactive)
9688 (if (null org-agenda-bulk-marked-entries)
9689 (message "No entry to unmark")
9690 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
9691 (setq org-agenda-bulk-marked-entries nil)
9692 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9694 (defcustom org-agenda-persistent-marks nil
9695 "Non-nil means marked items will stay marked after a bulk action.
9696 You can toggle this interactively by typing `p' when prompted for a
9697 bulk action."
9698 :group 'org-agenda
9699 :version "24.1"
9700 :type 'boolean)
9702 (defun org-agenda-bulk-action (&optional arg)
9703 "Execute an remote-editing action on all marked entries.
9704 The prefix arg is passed through to the command if possible."
9705 (interactive "P")
9706 ;; Make sure we have markers, and only valid ones
9707 (unless org-agenda-bulk-marked-entries (user-error "No entries are marked"))
9708 (mapc
9709 (lambda (m)
9710 (unless (and (markerp m)
9711 (marker-buffer m)
9712 (buffer-live-p (marker-buffer m))
9713 (marker-position m))
9714 (user-error "Marker %s for bulk command is invalid" m)))
9715 org-agenda-bulk-marked-entries)
9717 ;; Prompt for the bulk command
9718 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
9719 (message (concat msg "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9720 "[S]catter [f]unction "
9721 (when org-agenda-bulk-custom-functions
9722 (concat " Custom: ["
9723 (mapconcat (lambda(f) (char-to-string (car f)))
9724 org-agenda-bulk-custom-functions "")
9725 "]"))))
9726 (catch 'exit
9727 (let* ((action (read-char-exclusive))
9728 (org-log-refile (if org-log-refile 'time nil))
9729 (entries (reverse org-agenda-bulk-marked-entries))
9730 (org-overriding-default-time
9731 (if (get-text-property (point) 'org-agenda-date-header)
9732 (org-get-cursor-date)))
9733 redo-at-end
9734 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
9735 (cond
9736 ((equal action ?p)
9737 (let ((org-agenda-persistent-marks
9738 (not org-agenda-persistent-marks)))
9739 (org-agenda-bulk-action)
9740 (throw 'exit nil)))
9742 ((equal action ?$)
9743 (setq cmd '(org-agenda-archive)))
9745 ((equal action ?A)
9746 (setq cmd '(org-agenda-archive-to-archive-sibling)))
9748 ((member action '(?r ?w))
9749 (setq rfloc (org-refile-get-location
9750 "Refile to"
9751 (marker-buffer (car entries))
9752 org-refile-allow-creating-parent-nodes))
9753 (if (nth 3 rfloc)
9754 (setcar (nthcdr 3 rfloc)
9755 (move-marker (make-marker) (nth 3 rfloc)
9756 (or (get-file-buffer (nth 1 rfloc))
9757 (find-buffer-visiting (nth 1 rfloc))
9758 (error "This should not happen")))))
9760 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
9761 redo-at-end t))
9763 ((equal action ?t)
9764 (setq state (completing-read
9765 "Todo state: "
9766 (with-current-buffer (marker-buffer (car entries))
9767 (mapcar #'list org-todo-keywords-1))))
9768 (setq cmd `(let ((org-inhibit-blocking t)
9769 (org-inhibit-logging 'note))
9770 (org-agenda-todo ,state))))
9772 ((memq action '(?- ?+))
9773 (setq tag (completing-read
9774 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9775 (with-current-buffer (marker-buffer (car entries))
9776 (delq nil
9777 (mapcar (lambda (x) (and (stringp (car x)) x))
9778 org-current-tag-alist)))))
9779 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
9781 ((memq action '(?s ?d))
9782 (let* ((time
9783 (unless arg
9784 (org-read-date
9785 nil nil nil
9786 (if (eq action ?s) "(Re)Schedule to" "(Re)Set Deadline to")
9787 org-overriding-default-time)))
9788 (c1 (if (eq action ?s) 'org-agenda-schedule
9789 'org-agenda-deadline)))
9790 ;; Make sure to not prompt for a note when bulk
9791 ;; rescheduling as Org cannot cope with simultaneous Org.
9792 ;; Besides, it could be annoying depending on the number
9793 ;; of items re-scheduled.
9794 (setq cmd `(eval '(let ((org-log-reschedule
9795 (and org-log-reschedule 'time)))
9796 (,c1 arg ,time))))))
9798 ((equal action ?S)
9799 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
9800 (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
9801 (let ((days (read-number
9802 (format "Scatter tasks across how many %sdays: "
9803 (if arg "week" "")) 7)))
9804 (setq cmd
9805 `(let ((distance (1+ (random ,days))))
9806 (if arg
9807 (let ((dist distance)
9808 (day-of-week
9809 (calendar-day-of-week
9810 (calendar-gregorian-from-absolute (org-today)))))
9811 (dotimes (i (1+ dist))
9812 (while (member day-of-week org-agenda-weekend-days)
9813 (cl-incf distance)
9814 (cl-incf day-of-week)
9815 (when (= day-of-week 7)
9816 (setq day-of-week 0)))
9817 (cl-incf day-of-week)
9818 (when (= day-of-week 7)
9819 (setq day-of-week 0)))))
9820 ;; silently fail when try to replan a sexp entry
9821 (condition-case nil
9822 (let* ((date (calendar-gregorian-from-absolute
9823 (+ (org-today) distance)))
9824 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9825 (nth 2 date))))
9826 (org-agenda-schedule nil time))
9827 (error nil)))))))
9829 ((assoc action org-agenda-bulk-custom-functions)
9830 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
9831 redo-at-end t))
9833 ((equal action ?f)
9834 (setq cmd (list (intern
9835 (completing-read "Function: "
9836 obarray 'fboundp t nil nil)))))
9838 (t (user-error "Invalid bulk action")))
9840 ;; Sort the markers, to make sure that parents are handled before children
9841 (setq entries (sort entries
9842 (lambda (a b)
9843 (cond
9844 ((equal (marker-buffer a) (marker-buffer b))
9845 (< (marker-position a) (marker-position b)))
9847 (string< (buffer-name (marker-buffer a))
9848 (buffer-name (marker-buffer b))))))))
9850 ;; Now loop over all markers and apply cmd
9851 (while (setq e (pop entries))
9852 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
9853 (if (not pos)
9854 (progn (message "Skipping removed entry at %s" e)
9855 (setq cntskip (1+ cntskip)))
9856 (goto-char pos)
9857 (let (org-loop-over-headlines-in-active-region)
9858 (eval cmd))
9859 ;; `post-command-hook' is not run yet. We make sure any
9860 ;; pending log note is processed.
9861 (when (or (memq 'org-add-log-note (default-value 'post-command-hook))
9862 (memq 'org-add-log-note post-command-hook))
9863 (org-add-log-note))
9864 (setq cnt (1+ cnt))))
9865 (when redo-at-end (org-agenda-redo))
9866 (unless org-agenda-persistent-marks
9867 (org-agenda-bulk-unmark-all))
9868 (message "Acted on %d entries%s%s"
9870 (if (= cntskip 0)
9872 (format ", skipped %d (disappeared before their turn)"
9873 cntskip))
9874 (if (not org-agenda-persistent-marks)
9875 "" " (kept marked)"))))))
9877 (defun org-agenda-capture (&optional with-time)
9878 "Call `org-capture' with the date at point.
9879 With a `C-1' prefix, use the HH:MM value at point (if any) or the
9880 current HH:MM time."
9881 (interactive "P")
9882 (if (not (eq major-mode 'org-agenda-mode))
9883 (user-error "You cannot do this outside of agenda buffers")
9884 (let ((org-overriding-default-time
9885 (org-get-cursor-date (equal with-time 1))))
9886 (call-interactively 'org-capture))))
9888 ;;; Dragging agenda lines forward/backward
9890 (defun org-agenda-reapply-filters ()
9891 "Re-apply all agenda filters."
9892 (mapcar
9893 (lambda(f) (when (car f) (org-agenda-filter-apply (car f) (cadr f) t)))
9894 `((,org-agenda-tag-filter tag)
9895 (,org-agenda-category-filter category)
9896 (,org-agenda-regexp-filter regexp)
9897 (,org-agenda-effort-filter effort)
9898 (,(get 'org-agenda-tag-filter :preset-filter) tag)
9899 (,(get 'org-agenda-category-filter :preset-filter) category)
9900 (,(get 'org-agenda-effort-filter :preset-filter) effort)
9901 (,(get 'org-agenda-regexp-filter :preset-filter) regexp))))
9903 (defun org-agenda-drag-line-forward (arg &optional backward)
9904 "Drag an agenda line forward by ARG lines.
9905 When the optional argument `backward' is non-nil, move backward."
9906 (interactive "p")
9907 (let ((inhibit-read-only t) lst line)
9908 (if (or (not (get-text-property (point) 'txt))
9909 (save-excursion
9910 (dotimes (n arg)
9911 (move-beginning-of-line (if backward 0 2))
9912 (push (not (get-text-property (point) 'txt)) lst))
9913 (delq nil lst)))
9914 (message "Cannot move line forward")
9915 (let ((end (save-excursion (move-beginning-of-line 2) (point))))
9916 (move-beginning-of-line 1)
9917 (setq line (buffer-substring (point) end))
9918 (delete-region (point) end)
9919 (move-beginning-of-line (funcall (if backward '1- '1+) arg))
9920 (insert line)
9921 (org-agenda-reapply-filters)
9922 (org-agenda-mark-clocking-task)
9923 (move-beginning-of-line 0)))))
9925 (defun org-agenda-drag-line-backward (arg)
9926 "Drag an agenda line backward by ARG lines."
9927 (interactive "p")
9928 (org-agenda-drag-line-forward arg t))
9930 ;;; Flagging notes
9932 (defun org-agenda-show-the-flagging-note ()
9933 "Display the flagging note in the other window.
9934 When called a second time in direct sequence, offer to remove the FLAGGING
9935 tag and (if present) the flagging note."
9936 (interactive)
9937 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
9938 (win (selected-window))
9939 note heading newhead)
9940 (unless hdmarker
9941 (user-error "No linked entry at point"))
9942 (if (and (eq this-command last-command)
9943 (y-or-n-p "Unflag and remove any flagging note? "))
9944 (progn
9945 (org-agenda-remove-flag hdmarker)
9946 (let ((win (get-buffer-window "*Flagging Note*")))
9947 (and win (delete-window win)))
9948 (message "Entry unflagged"))
9949 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
9950 (unless note
9951 (user-error "No flagging note"))
9952 (org-kill-new note)
9953 (org-switch-to-buffer-other-window "*Flagging Note*")
9954 (erase-buffer)
9955 (insert note)
9956 (goto-char (point-min))
9957 (while (re-search-forward "\\\\n" nil t)
9958 (replace-match "\n" t t))
9959 (goto-char (point-min))
9960 (select-window win)
9961 (message "%s" (substitute-command-keys "Flagging note pushed to \
9962 kill ring. Press `\\[org-agenda-show-the-flagging-note]' again to remove \
9963 tag and note")))))
9965 (defun org-agenda-remove-flag (marker)
9966 "Remove the FLAGGED tag and any flagging note in the entry."
9967 (let (newhead)
9968 (org-with-point-at marker
9969 (org-toggle-tag "FLAGGED" 'off)
9970 (org-entry-delete nil "THEFLAGGINGNOTE")
9971 (setq newhead (org-get-heading)))
9972 (org-agenda-change-all-lines newhead marker)
9973 (message "Entry unflagged")))
9975 (defun org-agenda-get-any-marker (&optional pos)
9976 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
9977 (get-text-property (or pos (point-at-bol)) 'org-marker)))
9979 ;;; Appointment reminders
9981 (defvar appt-time-msg-list) ; defined in appt.el
9983 ;;;###autoload
9984 (defun org-agenda-to-appt (&optional refresh filter &rest args)
9985 "Activate appointments found in `org-agenda-files'.
9987 With a `\\[universal-argument]' prefix, refresh the list of \
9988 appointments.
9990 If FILTER is t, interactively prompt the user for a regular
9991 expression, and filter out entries that don't match it.
9993 If FILTER is a string, use this string as a regular expression
9994 for filtering entries out.
9996 If FILTER is a function, filter out entries against which
9997 calling the function returns nil. This function takes one
9998 argument: an entry from `org-agenda-get-day-entries'.
10000 FILTER can also be an alist with the car of each cell being
10001 either `headline' or `category'. For example:
10003 \\='((headline \"IMPORTANT\")
10004 (category \"Work\"))
10006 will only add headlines containing IMPORTANT or headlines
10007 belonging to the \"Work\" category.
10009 ARGS are symbols indicating what kind of entries to consider.
10010 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
10011 \(i.e., deadlines and scheduled items with a hh:mm specification)
10012 and :timestamp entries. See the docstring of `org-diary' for
10013 details and examples.
10015 If an entry has a APPT_WARNTIME property, its value will be used
10016 to override `appt-message-warning-time'."
10017 (interactive "P")
10018 (if refresh (setq appt-time-msg-list nil))
10019 (if (eq filter t)
10020 (setq filter (read-from-minibuffer "Regexp filter: ")))
10021 (let* ((cnt 0) ; count added events
10022 (scope (or args '(:deadline* :scheduled* :timestamp)))
10023 (org-agenda-new-buffers nil)
10024 (org-deadline-warning-days 0)
10025 ;; Do not use `org-today' here because appt only takes
10026 ;; time and without date as argument, so it may pass wrong
10027 ;; information otherwise
10028 (today (org-date-to-gregorian
10029 (time-to-days (current-time))))
10030 (org-agenda-restrict nil)
10031 (files (org-agenda-files 'unrestricted)) entries file
10032 (org-agenda-buffer nil))
10033 ;; Get all entries which may contain an appt
10034 (org-agenda-prepare-buffers files)
10035 (while (setq file (pop files))
10036 (setq entries
10037 (delq nil
10038 (append entries
10039 (apply 'org-agenda-get-day-entries
10040 file today scope)))))
10041 ;; Map thru entries and find if we should filter them out
10042 (mapc
10043 (lambda (x)
10044 (let* ((evt (org-trim
10045 (replace-regexp-in-string
10046 org-bracket-link-regexp "\\3"
10047 (or (get-text-property 1 'txt x) ""))))
10048 (cat (get-text-property (1- (length x)) 'org-category x))
10049 (tod (get-text-property 1 'time-of-day x))
10050 (ok (or (null filter)
10051 (and (stringp filter) (string-match filter evt))
10052 (and (functionp filter) (funcall filter x))
10053 (and (listp filter)
10054 (let ((cat-filter (cadr (assq 'category filter)))
10055 (evt-filter (cadr (assq 'headline filter))))
10056 (or (and (stringp cat-filter)
10057 (string-match cat-filter cat))
10058 (and (stringp evt-filter)
10059 (string-match evt-filter evt)))))))
10060 (wrn (get-text-property 1 'warntime x)))
10061 ;; FIXME: Shall we remove text-properties for the appt text?
10062 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
10063 (when (and ok tod (not (string-match "\\`DONE\\|CANCELLED" evt)))
10064 (setq tod (concat "00" (number-to-string tod)))
10065 (setq tod (when (string-match
10066 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
10067 (concat (match-string 1 tod) ":"
10068 (match-string 2 tod))))
10069 (when (if (version< emacs-version "23.3")
10070 (appt-add tod evt)
10071 (appt-add tod evt wrn))
10072 (setq cnt (1+ cnt))))))
10073 entries)
10074 (org-release-buffers org-agenda-new-buffers)
10075 (if (eq cnt 0)
10076 (message "No event to add")
10077 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
10079 (defun org-agenda-today-p (date)
10080 "Non nil when DATE means today.
10081 DATE is either a list of the form (month day year) or a number of
10082 days as returned by `calendar-absolute-from-gregorian' or
10083 `org-today'. This function considers `org-extend-today-until'
10084 when defining today."
10085 (eq (org-today)
10086 (if (consp date) (calendar-absolute-from-gregorian date) date)))
10088 (defun org-agenda-todo-yesterday (&optional arg)
10089 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
10090 (interactive "P")
10091 (let* ((org-use-effective-time t)
10092 (hour (nth 2 (decode-time (org-current-time))))
10093 (org-extend-today-until (1+ hour)))
10094 (org-agenda-todo arg)))
10096 (provide 'org-agenda)
10098 ;;; org-agenda.el ends here